2 * Copyright (C) 2010 Collabora Ltd.
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 * Philip Withnall <philip.withnall@collabora.co.uk>
27 * Command line application to import meta-contact information from various
28 * places into libfolks' key file backend.
31 * folks-import [--source=pidgin] [--source-filename=~/.purple/blist.xml]
34 public class Folks.ImportTool : Object
36 private static string source;
37 private static string source_filename;
39 private static const string DEFAULT_SOURCE = "pidgin";
41 private static const OptionEntry[] options =
43 { "source", 's', 0, OptionArg.STRING, ref ImportTool.source,
44 N_("Source backend name (default: 'pidgin')"), "name" },
45 { "source-filename", 0, 0, OptionArg.FILENAME,
46 ref ImportTool.source_filename,
47 N_("Source filename (default: specific to source backend)"), null },
51 public static int main (string[] args)
53 Intl.setlocale (LocaleCategory.ALL, "");
54 Intl.bindtextdomain (BuildConf.GETTEXT_PACKAGE, BuildConf.LOCALE_DIR);
55 Intl.textdomain (BuildConf.GETTEXT_PACKAGE);
57 OptionContext context = new OptionContext (
58 _("— import meta-contact information to libfolks"));
59 context.add_main_entries (ImportTool.options, "folks");
63 context.parse (ref args);
67 /* Translators: the parameter is an error message. */
68 stderr.printf (_("Couldn't parse command line options: %s") + "\n",
73 /* We only support importing from Pidgin at the moment */
74 if (source == null || source.strip () == "")
75 source = ImportTool.DEFAULT_SOURCE;
77 /* FIXME: We need to create this, even though we don't use it, to prevent
78 * debug message spew, as its constructor initialises the log handling.
80 IndividualAggregator aggregator = new IndividualAggregator ();
83 /* Create a main loop and start importing */
84 MainLoop main_loop = new MainLoop ();
87 ImportTool.import.begin ((o, r) =>
89 success = ImportTool.import.end (r);
95 return success ? 0 : 1;
98 private static async bool import ()
100 BackendStore backend_store = BackendStore.dup ();
104 yield backend_store.load_backends ();
106 catch (GLib.Error e1)
108 /* Translators: the parameter is an error message. */
109 stderr.printf (_("Couldn't load the backends: %s") + "\n",
114 /* Get the key-file backend */
115 Backend kf_backend = backend_store.dup_backend_by_name ("key-file");
117 if (kf_backend == null)
119 /* Translators: the parameter is a backend identifier. */
120 stderr.printf (_("Couldn't load the ‘%s’ backend.") + "\n",
127 yield kf_backend.prepare ();
129 catch (GLib.Error e2)
131 /* Translators: the first parameter is a backend identifier and the
132 * second parameter is an error message. */
133 stderr.printf (_("Couldn't prepare the ‘%s’ backend: %s") + "\n",
134 "key-file", e2.message);
138 /* Get its only PersonaStore */
139 PersonaStore destination_store = null;
140 var stores = kf_backend.persona_stores.values;
142 if (stores.size == 0)
145 /* Translators: the parameter is a backend identifier. */
146 _("Couldn't load the ‘%s’ backend's persona store.") + "\n",
153 /* Get the first persona store */
154 foreach (var persona_store in stores)
156 destination_store = persona_store;
160 yield destination_store.prepare ();
162 catch (GLib.Error e3)
164 /* Translators: the first parameter is a backend identifier and the
165 * second parameter is an error message. */
167 _("Couldn't prepare the ‘%s’ backend's persona store: %s") + "\n",
172 if (source == "pidgin")
174 Importer importer = new Importers.Pidgin ();
179 yield importer.import (destination_store,
180 ImportTool.source_filename);
182 catch (ImportError e)
184 /* Translators: the parameter is an error message. */
185 stderr.printf (_("Error importing contacts: %s") + "\n",
190 /* Wait for the PersonaStore to finish writing its changes to disk */
191 yield destination_store.flush ();
198 /* Translators: both parameters are identifiers for backends. */
199 _("Unrecognized source backend name ‘%s’. ‘%s’ is currently the only supported source backend.") + "\n",
206 public errordomain Folks.ImportError
211 public abstract class Folks.Importer : Object
213 public abstract async uint import (PersonaStore destination_store,
214 string? source_filename) throws ImportError;