]> git.0d.be Git - empathy.git/blob - src/empathy-import-dialog.c
Moved to Collabora coding style. (Jonny Lamb)
[empathy.git] / src / empathy-import-dialog.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program 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 GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
20  * */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32
33 #include <libmissioncontrol/mc-account.h>
34
35 #include "empathy-import-dialog.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39
40 typedef enum
41 {
42   EMPATHY_IMPORT_SETTING_TYPE_STRING,
43   EMPATHY_IMPORT_SETTING_TYPE_BOOL,
44   EMPATHY_IMPORT_SETTING_TYPE_INT,
45 } EmpathyImportSettingType;
46
47 typedef struct
48 {
49   gpointer     value;
50   EmpathyImportSettingType  type;
51 } EmpathyImportSetting;
52
53
54 /* Pidgin to MC map */
55 typedef struct
56 {
57   gchar *protocol;
58   gchar *pidgin_name;
59   gchar *mc_name;
60 } PidginMcMapItem;
61
62 static PidginMcMapItem pidgin_mc_map[] =
63 {
64   { "msn", "server", "server" },
65   { "msn", "port", "port" },
66
67   { "jabber", "connect_server", "server" },
68   { "jabber", "port", "port" },
69   { "jabber", "require_tls", "require-encryption" },
70   { "jabber", "old_ssl", "old-ssl" },
71
72   { "aim", "server", "server" },
73   { "aim", "port", "port" },
74
75   { "salut", "first", "first-name" },
76   { "salut", "last", "last-name" },
77   { "salut", "jid", "jid" },
78   { "salut", "email", "email" },
79
80   { "groupwise", "server", "server" },
81   { "groupwise", "port", "port" },
82
83   { "icq", "server", "server" },
84   { "icq", "port", "port" },
85
86   { "irc", "realname", "fullname" },
87   { "irc", "ssl", "use-ssl" },
88   { "irc", "port", "port" },
89
90   { "yahoo", "server", "server" },
91   { "yahoo", "port", "port" },
92   { "yahoo", "xfer_port", "xfer-port" },
93   { "yahoo", "ignore_invites", "ignore-invites" },
94   { "yahoo", "yahoojp", "yahoojp" },
95   { "yahoo", "xferjp_host", "xferjp-host" },
96   { "yahoo", "serverjp", "serverjp" },
97   { "yahoo", "xfer_host", "xfer-host" },
98 };
99
100 typedef struct
101 {
102   GtkWidget *window;
103   GtkWidget *label_select;
104   GtkWidget *combo;
105 } EmpathyImportDialog;
106
107 #define PIDGIN_ACCOUNT_TAG_NAME "name"
108 #define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
109 #define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
110 #define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
111 #define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
112 #define PIDGIN_SETTING_PROP_TYPE "type"
113 #define PIDGIN_PROTOCOL_BONJOUR "bonjour"
114 #define PIDGIN_PROTOCOL_NOVELL "novell"
115
116 static void empathy_import_dialog_add_setting (GHashTable *settings,
117     gchar *key, gpointer value, EmpathyImportSettingType  type);
118 static gboolean empathy_import_dialog_add_account (gchar *protocol_name,
119     GHashTable *settings);
120 static void empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
121     xmlNodePtr setting, GHashTable *settings);
122 static void empathy_import_dialog_pidgin_import_accounts ();
123 static void empathy_import_dialog_response_cb (GtkDialog *dialog_window,
124     gint response, EmpathyImportDialog *dialog);
125
126 static void
127 empathy_import_dialog_add_setting (GHashTable *settings,
128                                    gchar *key,
129                                    gpointer value,
130                                    EmpathyImportSettingType type)
131 {
132   EmpathyImportSetting *set = g_slice_new0 (EmpathyImportSetting);
133
134   set->value = value;
135   set->type = type;
136
137   g_hash_table_insert (settings, key, set);
138 }
139
140 static gboolean
141 empathy_import_dialog_add_account (gchar *protocol_name,
142                                    GHashTable *settings)
143 {
144   McProfile *profile;
145   McAccount *account;
146   const gchar *unique_name;
147   gchar *key_char;
148   GHashTableIter iter;
149   gpointer key, value;
150   EmpathyImportSetting *set;
151
152
153   DEBUG ("Looking up profile with protocol '%s'", protocol_name);
154   profile = mc_profile_lookup (protocol_name);
155
156   if (profile == NULL)
157     return FALSE;
158
159   account = mc_account_create (profile);
160
161   unique_name = mc_account_get_unique_name (account);
162   mc_account_set_display_name (account, unique_name);
163
164   g_hash_table_iter_init (&iter, settings);
165   while (g_hash_table_iter_next (&iter, &key, &value))
166     {
167       set = (EmpathyImportSetting *) value;
168       key_char = (gchar *) key;
169       switch (((EmpathyImportSetting *) value)->type)
170         {
171           case EMPATHY_IMPORT_SETTING_TYPE_STRING:
172             DEBUG ("Setting %s to (string) %s",
173                 key_char, (gchar *) set->value);
174             mc_account_set_param_string (account,
175                 key_char, (gchar *) set->value);
176             break;
177
178           case EMPATHY_IMPORT_SETTING_TYPE_BOOL:
179             DEBUG ("Setting %s to (bool) %i",
180                 key_char, (gboolean) set->value);
181             mc_account_set_param_boolean (account,
182                 key_char, (gboolean) set->value);
183             break;
184
185           case EMPATHY_IMPORT_SETTING_TYPE_INT:
186             DEBUG ("Setting %s to (int) %i",
187                 key_char, (gint) set->value);
188             mc_account_set_param_int (account,
189                 key_char, (gint) set->value);
190             break;
191         }
192     }
193   g_object_unref (account);
194   g_object_unref (profile);
195   return TRUE;
196 }
197
198 static void
199 empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
200                                             xmlNodePtr setting,
201                                             GHashTable *settings)
202 {
203   int i;
204
205   if (!xmlHasProp (setting, PIDGIN_ACCOUNT_TAG_NAME))
206     return;
207
208   for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
209     {
210       if (strcmp(protocol, pidgin_mc_map[i].protocol) != 0)
211         continue;
212
213       if (strcmp ((gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME),
214         pidgin_mc_map[i].pidgin_name) == 0)
215         {
216           gint arg;
217           gchar *type = NULL;
218
219           type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_TYPE);
220
221           if (strcmp (type, "bool") == 0)
222             {
223               sscanf ((gchar *) xmlNodeGetContent (setting),"%i", &arg);
224               empathy_import_dialog_add_setting (settings,
225                   pidgin_mc_map[i].mc_name,
226                   (gpointer) arg,
227                   EMPATHY_IMPORT_SETTING_TYPE_BOOL);
228             }
229           else if (strcmp (type, "int") == 0)
230             {
231               sscanf ((gchar *) xmlNodeGetContent (setting),
232                   "%i", &arg);
233               empathy_import_dialog_add_setting (settings,
234                   pidgin_mc_map[i].mc_name,
235                   (gpointer) arg,
236                   EMPATHY_IMPORT_SETTING_TYPE_INT);
237             }
238           else if (strcmp (type, "string") == 0)
239             {
240               empathy_import_dialog_add_setting (settings,
241                   pidgin_mc_map[i].mc_name,
242                   (gpointer) xmlNodeGetContent (setting),
243                   EMPATHY_IMPORT_SETTING_TYPE_STRING);
244             }
245         }
246     }
247 }
248
249 static void
250 empathy_import_dialog_pidgin_import_accounts ()
251 {
252   xmlNodePtr rootnode, node, child, setting;
253   xmlParserCtxtPtr ctxt;
254   xmlDocPtr doc;
255   gchar *filename;
256   gchar *protocol = NULL;
257   gchar *name = NULL;
258   gchar *username = NULL;
259   GHashTable *settings;
260
261   ctxt = xmlNewParserCtxt ();
262   filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
263       NULL);
264   doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
265   g_free (filename);
266
267   rootnode = xmlDocGetRootElement (doc);
268   if (rootnode == NULL)
269     return;
270
271   node = rootnode->children;
272   while (node)
273     {
274       if (strcmp ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT) == 0)
275         {
276           child = node->children;
277
278           settings = g_hash_table_new (g_str_hash, g_str_equal);
279
280           while (child)
281             {
282
283               if (strcmp ((gchar *) child->name,
284                   PIDGIN_ACCOUNT_TAG_PROTOCOL) == 0)
285                 {
286                   protocol = (gchar *) xmlNodeGetContent (child);
287
288                   if (g_str_has_prefix (protocol, "prpl-"))
289                     protocol = strchr (protocol, '-') + 1;
290
291                   if (strcmp (protocol, PIDGIN_PROTOCOL_BONJOUR) == 0)
292                     protocol = "salut";
293                   else if (strcmp (protocol, PIDGIN_PROTOCOL_NOVELL) == 0)
294                     protocol = "groupwise";
295
296                   empathy_import_dialog_add_setting (settings, "protocol",
297                       (gpointer) protocol,
298                       EMPATHY_IMPORT_SETTING_TYPE_STRING);
299
300                 }
301               else if (strcmp ((gchar *) child->name,
302                   PIDGIN_ACCOUNT_TAG_NAME) == 0)
303                 {
304                   name = (gchar *) xmlNodeGetContent (child);
305
306                   if (g_strrstr (name, "/") != NULL)
307                     {
308                       gchar **name_resource;
309                       name_resource = g_strsplit (name, "/", 2);
310                       username = g_strdup(name_resource[0]);
311                       g_free (name_resource);
312                     }
313                   else
314                     username = name;
315
316                  if (strstr (name, "@") && strcmp (protocol, "irc") == 0)
317                   {
318                     gchar **nick_server;
319                     nick_server = g_strsplit (name, "@", 2);
320                     username = nick_server[0];
321                     empathy_import_dialog_add_setting (settings,
322                         "server", (gpointer) nick_server[1],
323                         EMPATHY_IMPORT_SETTING_TYPE_STRING);
324                   }
325
326                   empathy_import_dialog_add_setting (settings, "account",
327                       (gpointer) username, EMPATHY_IMPORT_SETTING_TYPE_STRING);
328
329                 }
330               else if (strcmp ((gchar *) child->name,
331                   PIDGIN_ACCOUNT_TAG_PASSWORD) == 0)
332                 {
333                   empathy_import_dialog_add_setting (settings, "password",
334                       (gpointer) xmlNodeGetContent (child),
335                       EMPATHY_IMPORT_SETTING_TYPE_STRING);
336
337                 }
338               else if (strcmp ((gchar *) child->name,
339                   PIDGIN_ACCOUNT_TAG_SETTINGS) == 0)
340                 {
341                   setting = child->children;
342
343                   while (setting)
344                     {
345                       empathy_import_dialog_pidgin_parse_setting (protocol,
346                           setting, settings);
347                           setting = setting->next;
348                     }
349
350                 }
351               child = child->next;
352             }
353
354           if (g_hash_table_size (settings) > 0)
355               empathy_import_dialog_add_account (protocol, settings);
356
357           g_free (username);
358           g_hash_table_unref (settings);
359         }
360
361       node = node->next;
362     }
363
364   xmlFreeDoc(doc);
365   xmlFreeParserCtxt (ctxt);
366 }
367
368 static void
369 empathy_import_dialog_response_cb (GtkDialog *dialog_window,
370                                    gint response,
371                                    EmpathyImportDialog *dialog)
372 {
373   gchar *from = NULL;
374   if (response == GTK_RESPONSE_OK)
375     {
376       from = gtk_combo_box_get_active_text (GTK_COMBO_BOX (dialog->combo));
377
378       if (strcmp (from, "Pidgin") == 0)
379         empathy_import_dialog_pidgin_import_accounts ();
380     }
381
382   gtk_widget_destroy (GTK_WIDGET (dialog_window));
383   g_slice_free (EmpathyImportDialog, dialog);
384 }
385
386 void
387 empathy_import_dialog_show (GtkWindow *parent)
388 {
389   EmpathyImportDialog *dialog;
390
391   dialog = g_slice_new0 (EmpathyImportDialog);
392
393   dialog->window = gtk_dialog_new_with_buttons (_("Import accounts"),
394       NULL,
395       GTK_DIALOG_MODAL,
396       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
397       GTK_STOCK_OK, GTK_RESPONSE_OK,
398       NULL);
399
400   g_signal_connect (G_OBJECT (dialog->window), "response",
401       G_CALLBACK (empathy_import_dialog_response_cb),
402       dialog);
403
404   dialog->label_select = gtk_label_new (
405       _("Select the program to import accounts from:"));
406
407   dialog->combo = gtk_combo_box_new_text ();
408
409   gtk_combo_box_append_text (GTK_COMBO_BOX (dialog->combo), "Pidgin");
410   gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
411
412   gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
413       dialog->label_select);
414
415   gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
416       dialog->combo);
417
418
419   if (parent)
420     gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
421
422   gtk_widget_show_all (dialog->window);
423 }