summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels De Graef <nielsdegraef@gmail.com>2021-01-11 19:22:17 +0100
committerNiels De Graef <nielsdegraef@gmail.com>2021-01-21 23:08:35 +0100
commitae58c680e1f34a5fa62310d76f55479a21b15e6f (patch)
treef50ad63a974ce8edd832874d0d09156b8393bf56
parent727e625e29bd8391a164ce665f0c978554f38adf (diff)
downloadgnome-contacts-wip/nielsdg/vcard-import.tar.gz
Enable importing VCardswip/nielsdg/vcard-import
This commit enables gnome-contacts to import VCard (*.vcf) files. On a high-level view, the following happens: * Contacts starts a file chooser dialog so the user can choose a file to open * According to the chosen file, Contacts will launch a subprocess to do the actual parsing using a `Contacts.Io.Importer`. * The subprocess serializes the result to a `GLib.Variant` * Contacts receives the result, and on success deserializes the result * After that, we can show it in a contact pane
-rw-r--r--data/ui/contacts-main-window.ui8
-rw-r--r--src/contacts-app.vala42
-rw-r--r--src/contacts-importer-launcher.vala86
-rw-r--r--src/io/contacts-io-import-main.vala58
-rw-r--r--src/io/contacts-io-importer.vala54
-rw-r--r--src/io/contacts-io-vcard-importer.vala280
-rw-r--r--src/io/contacts-io.vala435
-rw-r--r--src/io/meson.build33
-rw-r--r--src/meson.build5
-rw-r--r--tests/io/internal/meson.build29
-rw-r--r--tests/io/internal/test-serialise-birthday.vala40
-rw-r--r--tests/io/internal/test-serialise-common.vala66
-rw-r--r--tests/io/internal/test-serialise-emails.vala41
-rw-r--r--tests/io/internal/test-serialise-full-name.vala42
-rw-r--r--tests/io/internal/test-serialise-nickname.vala40
-rw-r--r--tests/io/internal/test-serialise-structured-name.vala45
-rw-r--r--tests/io/internal/test-serialise-urls.vala41
-rw-r--r--tests/io/meson.build2
-rw-r--r--tests/io/vcard/meson.build21
-rw-r--r--tests/io/vcard/minimal.vcf4
-rw-r--r--tests/io/vcard/test-vcard-minimal.vala54
-rw-r--r--tests/meson.build6
22 files changed, 1428 insertions, 4 deletions
diff --git a/data/ui/contacts-main-window.ui b/data/ui/contacts-main-window.ui
index de3e0de..a3e7bf1 100644
--- a/data/ui/contacts-main-window.ui
+++ b/data/ui/contacts-main-window.ui
@@ -46,6 +46,14 @@
<child>
<object class="GtkModelButton">
<property name="can_focus">True</property>
+ <property name="text" translatable="yes">Import</property>
+ <property name="action-name">app.import</property>
+ <property name="visible">True</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkModelButton">
+ <property name="can_focus">True</property>
<property name="text" translatable="yes">Change Address Book…</property>
<property name="action-name">app.change-book</property>
<property name="visible">True</property>
diff --git a/src/contacts-app.vala b/src/contacts-app.vala
index b1adaf4..4d36d6b 100644
--- a/src/contacts-app.vala
+++ b/src/contacts-app.vala
@@ -31,7 +31,8 @@ public class Contacts.App : Gtk.Application {
{ "change-book", change_address_book },
{ "online-accounts", online_accounts },
{ "new-contact", new_contact },
- { "show-contact", on_show_contact, "s"}
+ { "show-contact", on_show_contact, "s"},
+ { "import", on_import }
};
private const OptionEntry[] options = {
@@ -305,4 +306,43 @@ public class Contacts.App : Gtk.Application {
show_individual.begin (individual);
}
+ private void on_import(SimpleAction action, Variant? param) {
+ var chooser = new Gtk.FileChooserNative ("Select contact file",
+ this.window,
+ Gtk.FileChooserAction.OPEN,
+ _("Import"),
+ _("Cancel"));
+ chooser.modal = true;
+ chooser.select_multiple = false;
+
+ // TODO: somehow get this from the list of importers we have
+ var filter = new Gtk.FileFilter ();
+ filter.set_filter_name ("VCard files");
+ filter.add_pattern ("*.vcf");
+ filter.add_pattern ("*.vcard");
+ chooser.add_filter (filter);
+
+ chooser.response.connect ((response) => {
+ if (response != Gtk.ResponseType.ACCEPT) {
+ chooser.destroy ();
+ return;
+ }
+
+ if (chooser.get_filename () == null) {
+ debug ("No file selected, or no path available");
+ chooser.destroy ();
+ }
+
+ try {
+ var file = File.new_for_path (chooser.get_filename ());
+ var import = new ImportOperation (file);
+ import.execute ();
+ } catch (GLib.Error err) {
+ warning ("Couldn't import file: %s", err.message);
+ }
+
+ chooser.destroy ();
+ });
+ chooser.run ();
+ }
}
diff --git a/src/contacts-importer-launcher.vala b/src/contacts-importer-launcher.vala
new file mode 100644
index 0000000..eeb40c6
--- /dev/null
+++ b/src/contacts-importer-launcher.vala
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+/**
+ * Launches a subprocess to deal with the import of a Contact file
+ */
+public class Contacts.ImportOperation {
+
+ private File input_file;
+
+ public ImportOperation (File file) {
+ this.input_file = file;
+ }
+
+ public async void execute () throws Error {
+ var launcher = new SubprocessLauncher (SubprocessFlags.STDOUT_PIPE);
+ // Make sure we're not accidentally propagating the G_MESSAGES_DEBUG variable
+ launcher.set_environ ({});
+
+ debug ("Spawning import subprocess");
+ var subprocess = launcher.spawnv ({
+ "/home/niels/jhbuild/install/libexec/gnome-contacts/gnome-contacts-import",
+ "vcard",
+ this.input_file.get_path ()
+ });
+
+ // Hook up stdout to a MemoryOutputStream, so we can easily fetch the output
+ var proc_stdout = subprocess.get_stdout_pipe ();
+ var stdout_stream = new MemoryOutputStream.resizable ();
+ try {
+ yield stdout_stream.splice_async (proc_stdout, 0, Priority.DEFAULT, null);
+ } catch (Error err) {
+ warning ("Error fetching stdout of import subprocess: %s", err.message);
+ return;
+ }
+
+ debug ("Waiting for import subprocess to finish");
+ var success = yield subprocess.wait_check_async ();
+ debug ("Import subprocess finished");
+ if (!success) {
+ warning ("Import process exited with error status %d", subprocess.get_exit_status ());
+ return;
+ }
+
+ // Ensure we have a proper string by adding a NULL terminator
+ stdout_stream.write ("\0".data);
+ stdout_stream.close ();
+
+ unowned var serialized_str = (string) stdout_stream.get_data ();
+
+ try {
+ var variant = Variant.parse (VariantType.VARDICT, serialized_str);
+
+ var new_details = Contacts.Io.deserialize_gvariant (variant);
+ if (new_details.size () == 0) {
+ warning ("Imported contact has zero fields");
+ return;
+ }
+
+ // TODO now what? :p
+
+ } catch (VariantParseError err) {
+ Variant.parse_error_print_context (err, serialized_str);
+ }
+
+ // TODO bytes or string?
+ // var variant = Variant.new_from_data<void> (VariantType.VARDICT, stdout_stream.get_data (), false);
+
+ }
+}
diff --git a/src/io/contacts-io-import-main.vala b/src/io/contacts-io-import-main.vala
new file mode 100644
index 0000000..75a6559
--- /dev/null
+++ b/src/io/contacts-io-import-main.vala
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+int main (string[] args) {
+ if (args.length != 3)
+ error ("Expected exactly 2 arguments, but got %d", args.length - 1);
+
+ unowned var import_type = args[1];
+ if (import_type == "")
+ error ("Invalid import type: got empty import type");
+
+ unowned var path = args[2];
+ if (path == "")
+ error ("Invalid path: path is empty");
+
+ Contacts.Io.Importer importer;
+ switch (import_type) {
+ case "vcard":
+ importer = new Contacts.Io.VCardImporter ();
+ break;
+ default:
+ error ("Unknown import type '%s'", import_type);
+ }
+
+ HashTable<string, Value?> details;
+ try {
+ var file = File.new_for_path (path);
+ details = importer.import_file (file);
+ } catch (Error err) {
+ error ("Error while importing file '%s': %s", path, err.message);
+ }
+
+ // Serialize
+ var serialized = Contacts.Io.serialize_to_gvariant (details);
+
+ // TODO: raw bytes (performance) or variant.print/parse?
+ // var bytes = serialized.get_data_as_bytes ();
+ // stdout.write (bytes.get_data (), bytes.get_size ());
+ stdout.write (serialized.print (false).data);
+
+ return 0;
+}
diff --git a/src/io/contacts-io-importer.vala b/src/io/contacts-io-importer.vala
new file mode 100644
index 0000000..f185628
--- /dev/null
+++ b/src/io/contacts-io-importer.vala
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+/**
+ * An Io.Importer is an object that can deal with importing a specific format
+ * of describing a Contact (VCard is the most common example, but there exist
+ * also CSV based formats and others).
+ *
+ * The main purpose of an Io.Importer is to import whatever input into a
+ * {@link GLib.HashTable} with string keys and {@link Value} as values. After
+ * that, we can choose to either serialize (using the serializing methods in
+ * Contacts.Io), or to immediately import it in folks using
+ * {@link Folks.PersonaStore.add_from_details}.
+ */
+public abstract class Contacts.Io.Importer {
+
+ /**
+ * Takes the given {@link GLib.File} containing a VCard string and tries to
+ * parse it into a {@link GLib.HashTable}, which can then be used for methods
+ * like {@link Folks.PersonaStore.add_persona_from_details}.
+ */
+ public HashTable<string, Value?> import_file (GLib.File file) throws GLib.Error {
+ var path = file.get_path ();
+ if (path == null)
+ throw new GLib.IOError.INVALID_FILENAME ("Couldn't import file: file doesn't have a path");
+
+ string vcard_str;
+ FileUtils.get_contents (path, out vcard_str);
+ return import_string (vcard_str);
+ }
+
+ /**
+ * Takes the given input string and tries to parse it into a
+ * {@link GLib.HashTable}, which can then be used for methods like
+ * {@link Folks.PersonaStore.add_persona_from_details}.
+ */
+ public abstract GLib.HashTable<string, Value?> import_string (string vcard_str);
+}
diff --git a/src/io/contacts-io-vcard-importer.vala b/src/io/contacts-io-vcard-importer.vala
new file mode 100644
index 0000000..165c29f
--- /dev/null
+++ b/src/io/contacts-io-vcard-importer.vala
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+/**
+ * a {@link Contacts.Io.Importer} that specifically deals with importing
+ * VCard files/strings.
+ */
+public class Contacts.Io.VCardImporter : Contacts.Io.Importer {
+
+ public VCardImporter () {
+ }
+
+ /**
+ * Takes the given VCard string and tries to parse it into a
+ * {@link GLib.HashTable}, which can then be used for methods like
+ * {@link Folks.PersonaStore.add_persona_from_details}.
+ */
+ public override HashTable<string, Value?> import_string (string vcard_str) {
+ var details = new HashTable<string, Value?> (GLib.str_hash, GLib.str_equal);
+ var vcard = new E.VCard.from_string (vcard_str);
+
+ unowned var vcard_attrs = vcard.get_attributes ();
+ message ("Got %u attributes in this vcard", vcard_attrs.length ());
+
+ foreach (unowned E.VCardAttribute attr in vcard_attrs) {
+ switch (attr.get_name ()) {
+ // Identification Properties
+ case E.EVC_FN:
+ handle_fn (details, attr);
+ break;
+ case E.EVC_N:
+ handle_n (details, attr);
+ break;
+ case E.EVC_NICKNAME:
+ handle_nickname (details, attr);
+ break;
+/*
+ case E.EVC_PHOTO:
+ handle_photo (details, attr);
+ break;
+*/
+ case E.EVC_BDAY:
+ handle_bday (details, attr);
+ break;
+ // Delivery Addressing Properties
+ case E.EVC_ADR:
+ handle_adr (details, attr);
+ break;
+ // Communications Properties
+ case E.EVC_TEL:
+ handle_tel (details, attr);
+ break;
+ case E.EVC_EMAIL:
+ handle_email (details, attr);
+ break;
+ // Explanatory Properties
+ case E.EVC_NOTE:
+ handle_note (details, attr);
+ break;
+ case E.EVC_URL:
+ handle_url (details, attr);
+ break;
+
+ default:
+ debug ("Unknown property name '%s'", attr.get_name ());
+ break;
+ }
+ }
+
+ return details;
+ }
+
+ // Handles the "FN" (Full Name) attribute
+ private void handle_fn (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ var full_name = attr.get_value ();
+ message ("Got FN '%s'", full_name);
+
+ Value? fn_v = Value (typeof (string));
+ fn_v.set_string (full_name);
+ details.insert (Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME),
+ (owned) fn_v);
+ }
+
+ // Handles the "N" (structured Name) attribute
+ private void handle_n (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ unowned var values = attr.get_values ();
+
+ // From the VCard spec:
+ // The structured property value corresponds, in sequence, to the Family
+ // Names (also known as surnames), Given Names, Additional Names, Honorific
+ // Prefixes, and Honorific Suffixes.
+ unowned var family_name = values.nth_data (0) ?? "";
+ unowned var given_name = values.nth_data (1) ?? "";
+ unowned var additional_names = values.nth_data (2) ?? "";
+ unowned var prefixes = values.nth_data (3) ?? "";
+ unowned var suffixes = values.nth_data (4) ?? "";
+
+ var structured_name = new StructuredName (family_name, given_name,
+ additional_names,
+ prefixes, suffixes);
+ Value? n_v = Value (typeof (StructuredName));
+ n_v.take_object ((owned) structured_name);
+ details.insert (Folks.PersonaStore.detail_key (PersonaDetail.STRUCTURED_NAME),
+ (owned) n_v);
+ }
+
+ private void handle_nickname (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ var nickname = attr.get_value ();
+ message ("Got nickname '%s'", nickname);
+
+ Value? nick_v = Value (typeof (string));
+ nick_v.set_string (nickname);
+ details.insert (Folks.PersonaStore.detail_key (PersonaDetail.NICKNAME),
+ (owned) nick_v);
+ }
+
+ // Handles the "BDAY" (birthday) attribute
+ private void handle_bday (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ // Get the attribute valuec
+ var bday = attr.get_value ();
+
+ // Parse it using the logic in E.ContactDate
+ var e_date = E.ContactDate.from_string (bday);
+
+ // Turn it into a GLib.DateTime
+ var datetime = new DateTime.utc ((int) e_date.year,
+ (int) e_date.month,
+ (int) e_date.day,
+ 0, 0, 0.0);
+
+ // Insert it into the hashtable as a GLib.Value
+ Value? bday_val = Value (typeof (DateTime));
+ bday_val.take_boxed ((owned) datetime);
+ details.insert (Folks.PersonaStore.detail_key (PersonaDetail.BIRTHDAY),
+ (owned) bday_val);
+ }
+
+ private void handle_email (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ var email = attr.get_value ();
+ if (email == null || email == "")
+ return;
+
+ var email_fd = new EmailFieldDetails (email);
+ add_params (email_fd, attr);
+ insert_field_details<EmailFieldDetails> (details, PersonaDetail.EMAIL_ADDRESSES,
+ email_fd,
+ AbstractFieldDetails<string>.hash_static,
+ AbstractFieldDetails<string>.equal_static);
+ }
+
+ private void handle_tel (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ var phone_nr = attr.get_value ();
+ if (phone_nr == null || phone_nr == "")
+ return;
+
+ var phone_fd = new PhoneFieldDetails (phone_nr);
+ add_params (phone_fd, attr);
+ insert_field_details<PhoneFieldDetails> (details, PersonaDetail.PHONE_NUMBERS,
+ phone_fd,
+ AbstractFieldDetails<string>.hash_static,
+ AbstractFieldDetails<string>.equal_static);
+ }
+
+ // Handles the ADR (postal address) attributes
+ private void handle_adr (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ unowned var values = attr.get_values ();
+
+ // From the VCard spec:
+ // ADR-value = ADR-component-pobox ";" ADR-component-ext ";"
+ // ADR-component-street ";" ADR-component-locality ";"
+ // ADR-component-region ";" ADR-component-code ";"
+ // ADR-component-country
+ unowned var po_box = values.nth_data (0) ?? "";
+ unowned var extension = values.nth_data (1) ?? "";
+ unowned var street = values.nth_data (2) ?? "";
+ unowned var locality = values.nth_data (3) ?? "";
+ unowned var region = values.nth_data (4) ?? "";
+ unowned var postal_code = values.nth_data (5) ?? "";
+ unowned var country = values.nth_data (6) ?? "";
+
+ var addr = new PostalAddress (po_box, extension, street, locality, region,
+ postal_code, country, "", null);
+ var addr_fd = new PostalAddressFieldDetails ((owned) addr);
+ add_params (addr_fd, attr);
+
+ insert_field_details<PostalAddressFieldDetails> (details,
+ PersonaDetail.POSTAL_ADDRESSES,
+ addr_fd,
+ AbstractFieldDetails<PostalAddress>.hash_static,
+ AbstractFieldDetails<PostalAddress>.equal_static);
+ }
+
+ private void handle_url (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ var url = attr.get_value ();
+ if (url == null || url == "")
+ return;
+
+ var url_fd = new UrlFieldDetails (url);
+ add_params (url_fd, attr);
+ insert_field_details<UrlFieldDetails> (details, PersonaDetail.URLS,
+ url_fd,
+ AbstractFieldDetails<string>.hash_static,
+ AbstractFieldDetails<string>.equal_static);
+ }
+
+ private void handle_note (HashTable<string, Value?> details,
+ E.VCardAttribute attr) {
+ var note = attr.get_value ();
+ if (note == null || note == "")
+ return;
+
+ var note_fd = new NoteFieldDetails (note);
+ add_params (note_fd, attr);
+ insert_field_details<NoteFieldDetails> (details, PersonaDetail.NOTES,
+ note_fd,
+ AbstractFieldDetails<string>.hash_static,
+ AbstractFieldDetails<string>.equal_static);
+
+ }
+
+ // Helper method for inserting aggregated properties
+ private bool insert_field_details<T> (HashTable<string, Value?> details,
+ PersonaDetail key,
+ T field_details,
+ owned Gee.HashDataFunc<T>? hash_func,
+ owned Gee.EqualDataFunc<T>? equal_func) {
+
+ // Get the existing set, or create a new one and add it
+ unowned var old_val = details.lookup (Folks.PersonaStore.detail_key (key));
+ if (old_val != null) {
+ unowned var values = old_val as Gee.HashSet<T>;
+ return values.add (field_details);
+ }
+
+ var values = new Gee.HashSet<T> ((owned) hash_func, (owned) equal_func);
+ Value? new_val = Value (typeof (Gee.Set));
+ new_val.set_object (values);
+ details.insert (Folks.PersonaStore.detail_key (key), (owned) new_val);
+
+ return values.add (field_details);
+ }
+
+ // Helper method to get VCard parameters into an AbstractFieldDetails object.
+ // Will take care of setting the correct "type"
+ private void add_params (AbstractFieldDetails details, E.VCardAttribute attr) {
+ foreach (unowned E.VCardAttributeParam param in attr.get_params ()) {
+ string param_name = param.get_name ().down ();
+ foreach (unowned string param_value in param.get_values ()) {
+ if (param_name == AbstractFieldDetails.PARAM_TYPE)
+ details.add_parameter (param_name, param_value.down ());
+ else
+ details.add_parameter (param_name, param_value);
+ }
+ }
+ }
+}
diff --git a/src/io/contacts-io.vala b/src/io/contacts-io.vala
new file mode 100644
index 0000000..9b8bb5f
--- /dev/null
+++ b/src/io/contacts-io.vala
@@ -0,0 +1,435 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+/**
+ * Everything in the Io namespace deals with importing and exporting contacts,
+ * both internally (between Contacts and a subprocess, using {@link GLib.Variant}
+ * serialization) and externally (VCard, CSV, ...).
+ */
+namespace Contacts.Io {
+
+ /**
+ * Serializes the {@link GLib.HashTable} as returned by a
+ * {@link Contacts.Io.Importer} into a {@link GLib.Variant} so it can be sent
+ * from one process to another.
+ */
+ public GLib.Variant serialize_to_gvariant (HashTable<string, Value?> details) {
+ var dict = new GLib.VariantDict ();
+
+ var iter = HashTableIter<string, Value?> (details);
+ unowned string prop;
+ unowned Value? val;
+ while (iter.next (out prop, out val)) {
+
+ if (prop == Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME)) {
+ serialize_full_name (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.STRUCTURED_NAME)) {
+ serialize_structured_name (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.NICKNAME)) {
+ serialize_nickname (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.BIRTHDAY)) {
+ serialize_birthday (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.POSTAL_ADDRESSES)) {
+ serialize_addresses (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS)) {
+ serialize_phone_nrs (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES)) {
+ serialize_emails (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.NOTES)) {
+ serialize_notes (dict, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.URLS)) {
+ serialize_urls (dict, prop, val);
+ } else {
+ warning ("Couldn't serialize unknown property '%s'", prop);
+ }
+ }
+
+ return dict.end ();
+ }
+
+ /**
+ * Deserializes the {@link GLib.Variant} back into a {@link GLib.HashTable}.
+ */
+ public HashTable<string, Value?> deserialize_gvariant (GLib.Variant variant) {
+ return_val_if_fail (variant.get_type ().equal (VariantType.VARDICT), null);
+
+ var details = new HashTable<string, Value?> (GLib.str_hash, GLib.str_equal);
+
+ var iter = variant.iterator ();
+ string prop;
+ GLib.Variant val;
+ while (iter.next ("{sv}", out prop, out val)) {
+
+ if (prop == Folks.PersonaStore.detail_key (PersonaDetail.FULL_NAME)) {
+ deserialize_full_name (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.STRUCTURED_NAME)) {
+ deserialize_structured_name (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.NICKNAME)) {
+ deserialize_nickname (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.BIRTHDAY)) {
+ deserialize_birthday (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.POSTAL_ADDRESSES)) {
+ deserialize_addresses (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.PHONE_NUMBERS)) {
+ deserialize_phone_nrs (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES)) {
+ deserialize_emails (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.NOTES)) {
+ deserialize_notes (details, prop, val);
+ } else if (prop == Folks.PersonaStore.detail_key (PersonaDetail.URLS)) {
+ deserialize_urls (details, prop, val);
+ } else {
+ warning ("Couldn't serialize unknown property '%s'", prop);
+ }
+ }
+
+ return details;
+ }
+
+ //
+ // FULL NAME
+ // -----------------------------------
+ private const string FULL_NAME_TYPE = "s";
+
+ private bool serialize_full_name (GLib.VariantDict dict, string prop, Value? val) {
+ return_val_if_fail (val.type () == typeof (string), false);
+
+ unowned string full_name = val as string;
+ return_val_if_fail (full_name != null, false);
+
+ dict.insert (prop, FULL_NAME_TYPE, full_name);
+
+ return true;
+ }
+
+ private bool deserialize_full_name (HashTable<string, Value?> details, string prop, Variant variant) {
+ return_val_if_fail (variant.get_type ().equal (VariantType.STRING), false);
+
+ unowned string full_name = variant.get_string ();
+ return_val_if_fail (full_name != null, false);
+
+ details.insert (prop, full_name);
+
+ return true;
+ }
+
+ //
+ // NICKNAME
+ // -----------------------------------
+ private const string STRUCTURED_NAME_TYPE = "(sssss)";
+
+ private bool serialize_structured_name (GLib.VariantDict dict, string prop, Value? val) {
+ return_val_if_fail (val.type () == typeof (StructuredName), false);
+
+ unowned var name = val as StructuredName;
+ return_val_if_fail (name != null, false);
+
+ dict.insert (prop, STRUCTURED_NAME_TYPE,
+ name.family_name, name.given_name, name.additional_names,
+ name.prefixes, name.suffixes);
+
+ return true;
+ }
+
+ private bool deserialize_structured_name (HashTable<string, Value?> details, string prop, Variant variant) {
+ return_val_if_fail (variant.get_type ().equal (new VariantType (STRUCTURED_NAME_TYPE)), false);
+
+ string family_name, given_name, additional_names, prefixes, suffixes;
+ variant.get (STRUCTURED_NAME_TYPE,
+ out family_name,
+ out given_name,
+ out additional_names,
+ out prefixes,
+ out suffixes);
+
+ var structured_name = new StructuredName (family_name, given_name, additional_names,
+ prefixes, suffixes);
+ details.insert (prop, structured_name);
+
+ return true;
+ }
+
+ //
+ // NICKNAME
+ // -----------------------------------
+ private const string NICKNAME_TYPE = "s";
+
+ private bool serialize_nickname (GLib.VariantDict dict, string prop, Value? val) {
+ return_val_if_fail (val.type () == typeof (string), false);
+
+ unowned string nickname = val as string;
+ return_val_if_fail (nickname != null, false);
+
+ dict.insert (prop, NICKNAME_TYPE, nickname);
+
+ return true;
+ }
+
+ private bool deserialize_nickname (HashTable<string, Value?> details, string prop, Variant variant) {
+ return_val_if_fail (variant.get_type ().equal (VariantType.STRING), false);
+
+ unowned string nickname = variant.get_string ();
+ return_val_if_fail (nickname != null, false);
+
+ details.insert (prop, nickname);
+
+ return true;
+ }
+
+ //
+ // BIRTHDAY
+ // -----------------------------------
+ private const string BIRTHDAY_TYPE = "(iii)"; // Year-Month-Day
+
+ private bool serialize_birthday (GLib.VariantDict dict, string prop, Value? val) {
+ return_val_if_fail (val.type () == typeof (DateTime), false);
+
+ unowned var bd = val as DateTime;
+ return_val_if_fail (bd != null, false);
+
+ int year, month, day;
+ bd.get_ymd (out year, out month, out day);
+ dict.insert (prop, BIRTHDAY_TYPE, year, month, day);
+
+ return true;
+ }
+
+ private bool deserialize_birthday (HashTable<string, Value?> details, string prop, Variant variant) {
+ return_val_if_fail (variant.get_type ().equal (new VariantType (BIRTHDAY_TYPE)), false);
+
+ int year, month, day;
+ variant.get (BIRTHDAY_TYPE, out year, out month, out day);
+
+ var bd = new DateTime.utc (year, month, day, 0, 0, 0.0);
+
+ details.insert (prop, bd);
+
+ return true;
+ }
+
+ //
+ // POSTAL ADDRESSES
+ // -----------------------------------
+ private const string ADDRESS_TYPE = "(sssssssv)";
+ private const string ADDRESSES_TYPE = "a" + ADDRESS_TYPE;
+
+ private bool serialize_addresses (GLib.VariantDict dict, string prop, Value? val) {
+ return_val_if_fail (val.type () == typeof (Gee.Set), false);
+
+ // Get the list of field details
+ unowned var afds = val as Gee.Set<PostalAddressFieldDetails>;
+ return_val_if_fail (afds != null, false);
+
+ // Turn the set of field details into an array Variant
+ var builder = new GLib.VariantBuilder (GLib.VariantType.ARRAY);
+ foreach (var afd in afds) {
+ unowned PostalAddress addr = afd.value;
+
+ builder.add (ADDRESS_TYPE,
+ addr.po_box,
+ addr.extension,
+ addr.street,
+ addr.locality,
+ addr.region,
+ addr.postal_code,
+ addr.country,
+ serialize_parameters (afd));
+ }
+
+ dict.insert_value (prop, builder.end ());
+
+ return true;
+ }
+
+ private bool deserialize_addresses (HashTable<string, Value?> details, string prop, Variant variant) {
+ return_val_if_fail (variant.get_type ().equal (new VariantType ("a" + ADDRESS_TYPE)), false);
+
+ var afds = new Gee.HashSet<PostalAddressFieldDetails> ();
+
+ // Turn the array variant into a set of field details
+ var iter = variant.iterator ();
+
+ string po_box, extension, street, locality, region, postal_code, country;
+ GLib.Variant parameters;
+ while (iter.next (ADDRESS_TYPE,
+ out po_box,
+ out extension,
+ out street,
+ out locality,
+ out region,
+ out postal_code,
+ out country,
+ out parameters)) {
+ if (po_box == "" && extension == "" && street == "" && locality == ""
+ && region == "" && postal_code == "" && country == "") {
+ warning ("Got empty postal address");
+ continue;
+ }
+
+ var addr = new PostalAddress (po_box, extension, street, locality, region,
+ postal_code, country, "", null);
+
+ var afd = new PostalAddressFieldDetails (addr);
+ deserialize_parameters (parameters, afd);
+
+ afds.add (afd);
+ }
+
+ details.insert (prop, afds);
+
+ return true;
+ }
+
+ //
+ // PHONE NUMBERS
+ // -----------------------------------
+ private bool serialize_phone_nrs (GLib.VariantDict dict, string prop, Value? val) {
+ return serialize_afd_strings (dict, prop, val);
+ }
+
+ private bool deserialize_phone_nrs (HashTable<string, Value?> details, string prop, Variant variant) {
+ return deserialize_afd_str (details, prop, variant,
+ (str) => { return new PhoneFieldDetails (str); });
+ }
+
+ //
+ // EMAILS
+ // -----------------------------------
+ private bool serialize_emails (GLib.VariantDict dict, string prop, Value? val) {
+ return serialize_afd_strings (dict, prop, val);
+ }
+
+ private bool deserialize_emails (HashTable<string, Value?> details, string prop, Variant variant) {
+ return deserialize_afd_str (details, prop, variant,
+ (str) => { return new EmailFieldDetails (str); });
+ }
+
+ //
+ // NOTES
+ // -----------------------------------
+ private bool serialize_notes (GLib.VariantDict dict, string prop, Value? val) {
+ return serialize_afd_strings (dict, prop, val);
+ }
+
+ private bool deserialize_notes (HashTable<string, Value?> details, string prop, Variant variant) {
+ return deserialize_afd_str (details, prop, variant,
+ (str) => { return new NoteFieldDetails (str); });
+ }
+
+ //
+ // URLS
+ // -----------------------------------
+ private bool serialize_urls (GLib.VariantDict dict, string prop, Value? val) {
+ return serialize_afd_strings (dict, prop, val);
+ }
+
+ private bool deserialize_urls (HashTable<string, Value?> details, string prop, Variant variant) {
+ return deserialize_afd_str (details, prop, variant,
+ (str) => { return new UrlFieldDetails (str); });
+ }
+
+ //
+ // HELPER: AbstractFielDdetail<string>
+ // -----------------------------------
+ private const string AFD_STRING_TYPE = "(sv)";
+
+ private bool serialize_afd_strings (GLib.VariantDict dict, string prop, Value? val) {
+ return_val_if_fail (val.type () == typeof (Gee.Set), false);
+
+ // Get the list of field details
+ unowned var afds = val as Gee.Set<AbstractFieldDetails<string>>;
+ return_val_if_fail (afds != null, false);
+
+ // Turn the set of field details into an array Variant
+ var builder = new GLib.VariantBuilder (GLib.VariantType.ARRAY);
+ foreach (var afd in afds) {
+ builder.add (AFD_STRING_TYPE, afd.value, serialize_parameters (afd));
+ }
+
+ dict.insert_value (prop, builder.end ());
+
+ return true;
+ }
+
+ // In an ideal world, we wouldn't need this delegate and we could just use
+ // GLib.Object.new(), but this is Vala and generics, so we find ourselves in
+ // a big mess here
+ delegate AbstractFieldDetails<string> CreateAbstractFieldStrFunc(string value);
+
+ private bool deserialize_afd_str (HashTable<string, Value?> details,
+ string prop,
+ Variant variant,
+ CreateAbstractFieldStrFunc create_afd_func) {
+ return_val_if_fail (variant.get_type ().equal (new VariantType ("a" + AFD_STRING_TYPE)), false);
+
+ var afds = new Gee.HashSet<AbstractFieldDetails> ();
+
+ // Turn the array variant into a set of field details
+ var iter = variant.iterator ();
+ string str;
+ GLib.Variant parameters;
+ while (iter.next (AFD_STRING_TYPE, out str, out parameters)) {
+ AbstractFieldDetails afd = create_afd_func (str);
+ deserialize_parameters (parameters, afd);
+
+ afds.add (afd);
+ }
+
+ details.insert (prop, afds);
+
+ return true;
+ }
+
+ //
+ // HELPER: Parameters
+ // -----------------------------------
+ // We can't use a vardict here, since one key can map to multiple values.
+ private const string PARAMS_TYPE = "a(ss)";
+
+ private Variant serialize_parameters (AbstractFieldDetails details) {
+
+ if (details.parameters == null || details.parameters.size == 0) {
+ return new GLib.Variant (PARAMS_TYPE, null); // Empty array
+ }
+
+ var builder = new GLib.VariantBuilder (GLib.VariantType.ARRAY);
+ var iter = details.parameters.map_iterator ();
+ while (iter.next ()) {
+ string param_name = iter.get_key ();
+ string param_value = iter.get_value ();
+
+ builder.add ("(ss)", param_name, param_value);
+ }
+
+ return builder.end ();
+ }
+
+ private void deserialize_parameters (Variant parameters, AbstractFieldDetails details) {
+ return_if_fail (parameters.get_type ().is_array ());
+
+ var iter = parameters.iterator ();
+ string param_name, param_value;
+ while (iter.next ("(ss)", out param_name, out param_value)) {
+ if (param_name == AbstractFieldDetails.PARAM_TYPE)
+ details.add_parameter (param_name, param_value.down ());
+ else
+ details.add_parameter (param_name, param_value);
+ }
+ }
+}
diff --git a/src/io/meson.build b/src/io/meson.build
new file mode 100644
index 0000000..7bbead4
--- /dev/null
+++ b/src/io/meson.build
@@ -0,0 +1,33 @@
+# Common library
+contacts_io_sources = files(
+ 'contacts-io.vala',
+ 'contacts-io-importer.vala',
+ 'contacts-io-vcard-importer.vala',
+)
+
+contacts_vala_args = [
+ '--target-glib=@0@'.format(min_glib_version),
+ '--pkg', 'config',
+ '--pkg', 'custom',
+]
+
+contacts_c_args = [
+ '-include', 'config.h',
+ '-DGNOME_DESKTOP_USE_UNSTABLE_API',
+ '-DLOCALEDIR="@0@"'.format(locale_dir),
+]
+
+contacts_io_deps = [
+ folks,
+ gee,
+ gio_unix,
+ glib,
+ libebook,
+]
+
+executable('gnome-contacts-import',
+ [ contacts_io_sources, files('contacts-io-import-main.vala') ],
+ dependencies: contacts_io_deps,
+ install: true,
+ install_dir: get_option('libexecdir') / 'gnome-contacts',
+)
diff --git a/src/meson.build b/src/meson.build
index d977638..3d82dc6 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,3 +1,5 @@
+subdir ('io')
+
# GSettings
compiled = gnome.compile_schemas()
install_data('org.gnome.Contacts.gschema.xml',
@@ -9,6 +11,7 @@ libcontacts_sources = files(
'contacts-esd-setup.vala',
'contacts-fake-persona-store.vala',
'contacts-im-service.vala',
+ 'contacts-importer-launcher.vala',
'contacts-store.vala',
'contacts-typeset.vala',
'contacts-type-descriptor.vala',
@@ -56,7 +59,7 @@ if get_option('telepathy')
endif
libcontacts = static_library('contacts',
- libcontacts_sources,
+ [ libcontacts_sources, contacts_io_sources ],
include_directories: config_h_dir,
vala_args: contacts_vala_args,
c_args: contacts_c_args,
diff --git a/tests/io/internal/meson.build b/tests/io/internal/meson.build
new file mode 100644
index 0000000..82590ef
--- /dev/null
+++ b/tests/io/internal/meson.build
@@ -0,0 +1,29 @@
+io_internal_testlib = library('io-internal-testlib',
+ files('test-serialise-common.vala'),
+ dependencies: libcontacts_dep,
+)
+
+io_internal_testlib_dep = declare_dependency(
+ link_with: io_internal_testlib,
+ include_directories: include_directories('.'),
+)
+
+io_internal_test_names = [
+ 'serialise-full-name',
+ 'serialise-structured-name',
+ 'serialise-nickname',
+ 'serialise-birthday',
+ 'serialise-emails',
+ 'serialise-urls',
+]
+
+foreach _test : io_internal_test_names
+ test_bin = executable(_test,
+ files('test-'+_test+'.vala'),
+ dependencies: [ libcontacts_dep, io_internal_testlib_dep ],
+ )
+
+ test(_test, test_bin,
+ suite: 'io-internal',
+ )
+endforeach
diff --git a/tests/io/internal/test-serialise-birthday.vala b/tests/io/internal/test-serialise-birthday.vala
new file mode 100644
index 0000000..091006f
--- /dev/null
+++ b/tests/io/internal/test-serialise-birthday.vala
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/serialize_birthday",
+ Contacts.Tests.Io.test_serialize_birthday);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+
+ private void test_serialize_birthday () {
+ unowned var bd_key = PersonaStore.detail_key (PersonaDetail.BIRTHDAY);
+
+ DateTime old_bd = new GLib.DateTime.utc (1992, 8, 1, 0, 0, 0);
+ var old_bd_val = Value (typeof (DateTime));
+ old_bd_val.set_boxed (old_bd);
+
+ var new_bd_val = _transform_single_value (bd_key, old_bd_val);
+ assert_true (new_bd_val.type () == typeof (DateTime));
+ assert_true (old_bd.equal ((DateTime) new_bd_val.get_boxed ()));
+ }
+}
diff --git a/tests/io/internal/test-serialise-common.vala b/tests/io/internal/test-serialise-common.vala
new file mode 100644
index 0000000..5ab7a56
--- /dev/null
+++ b/tests/io/internal/test-serialise-common.vala
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+namespace Contacts.Tests.Io {
+
+ // Helper to serialize and deserialize an AbstractFieldDetails
+ public T _transform_single_afd<T> (string prop_key, T afd) {
+ Gee.Set<T> afd_set = new Gee.HashSet<T> ();
+ afd_set.add (afd);
+
+ Value val = Value (typeof (Gee.Set));
+ val.set_object (afd_set);
+
+ Value emails_value = _transform_single_value (prop_key, val);
+ var emails_set = emails_value.get_object () as Gee.Set<T>;
+ if (emails_set == null)
+ error ("GValue has null value");
+ if (emails_set.size != 1)
+ error ("Expected %d elements but got %d", 1, emails_set.size);
+
+ var deserialized_fd = Utils.get_first<T> (emails_set);
+ assert_nonnull (deserialized_fd);
+
+ return deserialized_fd;
+ }
+
+ // Helper to serialize and deserialize a single property with a GLib.Value
+ public GLib.Value _transform_single_value (string prop_key, GLib.Value val) {
+ var details = new HashTable<string, Value?> (GLib.str_hash, GLib.str_equal);
+ details.insert (prop_key, val);
+
+ // Serialize
+ Variant serialized = Contacts.Io.serialize_to_gvariant (details);
+ if (serialized == null)
+ error ("Couldn't serialize single-value table for property %s", prop_key);
+
+ // Deserialize
+ var details_deserialized = Contacts.Io.deserialize_gvariant (serialized);
+ if (details_deserialized == null)
+ error ("Couldn't deserialize details for property %s", prop_key);
+
+ if (!details_deserialized.contains (prop_key))
+ error ("Deserialized details doesn't contain value for property %s", prop_key);
+ Value? val_deserialized = details_deserialized.lookup (prop_key);
+ if (val_deserialized.type() == GLib.Type.NONE)
+ error ("Deserialized Value is unset");
+
+ return val_deserialized;
+ }
+}
diff --git a/tests/io/internal/test-serialise-emails.vala b/tests/io/internal/test-serialise-emails.vala
new file mode 100644
index 0000000..27b15ac
--- /dev/null
+++ b/tests/io/internal/test-serialise-emails.vala
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/serialize_emails",
+ Contacts.Tests.Io.test_serialize_emails);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+
+ private void test_serialize_emails () {
+ unowned var emails_key = PersonaStore.detail_key (PersonaDetail.EMAIL_ADDRESSES);
+
+ var old_fd = new EmailFieldDetails ("nielsdegraef@gmail.com");
+ var new_fd = _transform_single_afd<EmailFieldDetails> (emails_key, old_fd);
+
+ if (!(new_fd is EmailFieldDetails))
+ error ("Expected EmailFieldDetails but got %s", new_fd.get_type ().name ());
+
+ if (old_fd.value != new_fd.value)
+ error ("Expected '%s' but got '%s'", old_fd.value, new_fd.value);
+ }
+}
diff --git a/tests/io/internal/test-serialise-full-name.vala b/tests/io/internal/test-serialise-full-name.vala
new file mode 100644
index 0000000..9da8319
--- /dev/null
+++ b/tests/io/internal/test-serialise-full-name.vala
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/serialize_full_name_simple",
+ Contacts.Tests.Io.test_serialize_full_name_simple);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+
+ private void test_serialize_full_name_simple () {
+ unowned var fn_key = PersonaStore.detail_key (PersonaDetail.FULL_NAME);
+
+ string old_fn = "Niels De Graef";
+ Value old_fn_val = Value (typeof (string));
+ old_fn_val.set_string (old_fn);
+
+ var new_fn_val = _transform_single_value (fn_key, old_fn_val);
+ if (new_fn_val.type () != typeof (string))
+ error ("Expected G_TYPE_STRING but got %s", new_fn_val.type ().name ());
+ if (old_fn != new_fn_val.get_string ())
+ error ("Expected '%s' but got '%s'", old_fn, new_fn_val.get_string ());
+ }
+}
diff --git a/tests/io/internal/test-serialise-nickname.vala b/tests/io/internal/test-serialise-nickname.vala
new file mode 100644
index 0000000..649b638
--- /dev/null
+++ b/tests/io/internal/test-serialise-nickname.vala
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/serialize_nickame",
+ Contacts.Tests.Io.test_serialize_nickname);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+
+ private void test_serialize_nickname () {
+ unowned var nick_key = PersonaStore.detail_key (PersonaDetail.NICKNAME);
+
+ string old_nick = "nielsdg";
+ var old_nick_val = Value (typeof (string));
+ old_nick_val.set_string (old_nick);
+
+ var new_nick_val = _transform_single_value (nick_key, old_nick_val);
+ assert_true (new_nick_val.type () == typeof (string));
+ assert_true (old_nick == new_nick_val.get_string ());
+ }
+}
diff --git a/tests/io/internal/test-serialise-structured-name.vala b/tests/io/internal/test-serialise-structured-name.vala
new file mode 100644
index 0000000..45f2093
--- /dev/null
+++ b/tests/io/internal/test-serialise-structured-name.vala
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/serialize_structured_name_simple",
+ Contacts.Tests.Io.test_serialize_structured_name_simple);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+
+ private void test_serialize_structured_name_simple () {
+ unowned var sn_key = PersonaStore.detail_key (PersonaDetail.STRUCTURED_NAME);
+
+ var old_sn = new StructuredName.simple ("Niels", "De Graef");
+ Value old_sn_val = Value (typeof (StructuredName));
+ old_sn_val.set_object (old_sn);
+
+ var new_sn_val = _transform_single_value (sn_key, old_sn_val);
+
+ if (new_sn_val.type () != typeof (StructuredName))
+ error ("Expected FOLKS_TYPE_STRUCTURED_NAME but got %s", new_sn_val.type ().name ());
+
+ var new_sn = new_sn_val.get_object () as StructuredName;
+ if (!old_sn.equal (new_sn))
+ error ("Expected '%s' but got '%s'", old_sn.to_string (), new_sn.to_string ());
+ }
+}
diff --git a/tests/io/internal/test-serialise-urls.vala b/tests/io/internal/test-serialise-urls.vala
new file mode 100644
index 0000000..cf4cdf9
--- /dev/null
+++ b/tests/io/internal/test-serialise-urls.vala
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/serialize_urls_single",
+ Contacts.Tests.Io.test_serialize_urls_single);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+
+ private void test_serialize_urls_single () {
+ unowned var urls_key = PersonaStore.detail_key (PersonaDetail.URLS);
+
+ var old_fd = new UrlFieldDetails ("http://www.islinuxaboutchoice.com/");
+ var new_fd = _transform_single_afd<UrlFieldDetails> (urls_key, old_fd);
+
+ if (!(new_fd is UrlFieldDetails))
+ error ("Expected UrlFieldDetails but got %s", new_fd.get_type ().name ());
+
+ if (old_fd.value != new_fd.value)
+ error ("Expected '%s' but got '%s'", old_fd.value, new_fd.value);
+ }
+}
diff --git a/tests/io/meson.build b/tests/io/meson.build
new file mode 100644
index 0000000..2f34960
--- /dev/null
+++ b/tests/io/meson.build
@@ -0,0 +1,2 @@
+subdir('internal')
+subdir('vcard')
diff --git a/tests/io/vcard/meson.build b/tests/io/vcard/meson.build
new file mode 100644
index 0000000..e3946e3
--- /dev/null
+++ b/tests/io/vcard/meson.build
@@ -0,0 +1,21 @@
+io_vcard_files = [
+ 'minimal',
+]
+
+foreach vcard_name : io_vcard_files
+ vcf_file = meson.current_source_dir() / vcard_name + '.vcf'
+
+ # Ideally we'd do this using a preprocessor symbol or something
+ vcf_test_env = environment()
+ vcf_test_env.append('_VCF_FILE', vcf_file)
+
+ test_bin = executable(vcard_name,
+ files('test-vcard-'+vcard_name+'.vala'),
+ dependencies: libcontacts_dep,
+ )
+
+ test(vcard_name, test_bin,
+ suite: 'io-vcard',
+ env: vcf_test_env,
+ )
+endforeach
diff --git a/tests/io/vcard/minimal.vcf b/tests/io/vcard/minimal.vcf
new file mode 100644
index 0000000..8ac83c9
--- /dev/null
+++ b/tests/io/vcard/minimal.vcf
@@ -0,0 +1,4 @@
+BEGIN:'''VCARD'''
+VERSION:3.0
+FN:Niels De Graef
+END:VCARD
diff --git a/tests/io/vcard/test-vcard-minimal.vala b/tests/io/vcard/test-vcard-minimal.vala
new file mode 100644
index 0000000..28da64a
--- /dev/null
+++ b/tests/io/vcard/test-vcard-minimal.vala
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+using Folks;
+
+void main (string[] args) {
+ Test.init (ref args);
+ Test.add_func ("/io/test_vcard_minimal",
+ Contacts.Tests.Io.test_vcard_minimal);
+ Test.run ();
+}
+
+namespace Contacts.Tests.Io {
+ private void test_vcard_minimal () {
+ unowned var vcf_path = Environment.get_variable ("_VCF_FILE");
+ if (vcf_path == null || vcf_path == "")
+ error ("No .vcf file set as envvar. Please use the meson test suite");
+
+ var file = GLib.File.new_for_path (vcf_path);
+
+ var importer = new Contacts.Io.VCardImporter ();
+ HashTable<string, Value?> details;
+ try {
+ details = importer.import_file (file);
+ } catch (Error err) {
+ error ("Error while importing: %s", err.message);
+ }
+ if (details == null)
+ error ("VCardImporter returned null");
+
+ unowned var fn_key = PersonaStore.detail_key (PersonaDetail.FULL_NAME);
+ if (!details.contains (fn_key))
+ error ("No FN value");
+
+ var fn_value = details.lookup (fn_key);
+ unowned var fn = fn_value as string;
+ if (fn != "Niels De Graef")
+ error ("Expected '%s' but got '%s'", "Niels De Graef", fn);
+ }
+}
diff --git a/tests/meson.build b/tests/meson.build
index 92c3586..6dcfcf1 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,14 +1,16 @@
+subdir('io')
+
test_names = [
'basic-test',
]
foreach _test : test_names
test_bin = executable(_test,
- '@0@.vala'.format(_test),
+ files('@0@.vala'.format(_test)),
dependencies: libcontacts_dep,
)
test(_test, test_bin,
- suite: 'gnome-contacts',
+ suite: 'src',
)
endforeach