]> git.0d.be Git - empathy.git/blobdiff - src/empathy-import-dialog.c
Be more compatible with Facebook emoticon codes
[empathy.git] / src / empathy-import-dialog.c
index 31a6c0133237989dfe68b8c79e09e8c3e70d126b..9d86dd1dec4a69044220c6b23f21abaa10f706e4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Collabora Ltd.
+ * Copyright (C) 2008-2009 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  *
  * You should have received a copy of the GNU General Public
  * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
  *
  * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
- * */
+ *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
+ */
 
-#include <config.h>
-
-#include <string.h>
-
-#include <glib.h>
-#include <gtk/gtk.h>
-#include <glib/gi18n.h>
-
-#include <libxml/parser.h>
-#include <libxml/tree.h>
+#include "config.h"
+#include "empathy-import-dialog.h"
 
-#include <libmissioncontrol/mc-account.h>
+#include <glib/gi18n-lib.h>
 
-#include "empathy-import-dialog.h"
+#include "empathy-import-widget.h"
+#include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
-typedef enum {
-        EMPATHY_IMPORT_SETTING_TYPE_STRING,
-        EMPATHY_IMPORT_SETTING_TYPE_BOOL,
-        EMPATHY_IMPORT_SETTING_TYPE_INT,
-} EmpathyImportSettingType;
+enum {
+  PROP_PARENT = 1,
+  PROP_SHOW_WARNING,
+  PROP_CMS,
+};
 
 typedef struct {
-        gpointer     value;
-        EmpathyImportSettingType  type;
-} EmpathyImportSetting;
-
-
-/* Pidgin to MC map */
-const struct {
-        gchar *protocol;
-        gchar *pidgin_name;
-        gchar *mc_name;
-} pidgin_mc_map[] = {
-        { "msn", "server", "server" },
-        { "msn", "port", "port" },
-
-        { "jabber", "connect_server", "server" },
-        { "jabber", "port", "port" },
-        { "jabber", "require_tls", "require-encryption" },
-        { "jabber", "old_ssl", "old-ssl" },
-
-        { "aim", "server", "server" },
-        { "aim", "port", "port" },
-
-        { "salut", "first", "first-name" },
-        { "salut", "last", "last-name" },
-        { "salut", "jid", "jid" },
-        { "salut", "email", "email" },
-
-        { "groupwise", "server", "server" },
-        { "groupwise", "port", "port" },
-
-        { "icq", "server", "server" },
-        { "icq", "port", "port" },
-
-        { "irc", "realname", "fullname" },
-        { "irc", "ssl", "use-ssl" },
-        { "irc", "port", "port" },
-
-        { "yahoo", "server", "server" },
-        { "yahoo", "port", "port" },
-        { "yahoo", "xfer_port", "xfer-port" },
-        { "yahoo", "ignore_invites", "ignore-invites" },
-        { "yahoo", "yahoojp", "yahoojp" },
-        { "yahoo", "xferjp_host", "xferjp-host" },
-        { "yahoo", "serverjp", "serverjp" },
-        { "yahoo", "xfer_host", "xfer-host" },
-};
+  GtkWindow *parent_window;
 
-#define PIDGIN_MC_MAP_ITEMS 27
+  EmpathyImportWidget *iw;
 
-typedef struct {
-        GtkWidget       *window;
-        GtkWidget       *label_select;
-        GtkWidget       *combo;
-} EmpathyImportDialog;
-
-static void     empathy_import_dialog_add_setting               (GHashTable               *settings,
-                                                                 gchar                    *key,
-                                                                 gpointer                  value,
-                                                                 EmpathyImportSettingType  type);
-static gboolean empathy_import_dialog_add_account               (gchar                    *protocol_name,
-                                                                 GHashTable               *settings);
-static void     empathy_import_dialog_pidgin_parse_setting      (gchar                    *protocol,
-                                                                 xmlNodePtr                setting,
-                                                                 GHashTable               *settings);
-static void     empathy_import_dialog_pidgin_import_accounts    ();
-static void     empathy_import_dialog_response_cb               (GtkDialog                *dialog_window,
-                                                                 gint                      response,
-                                                                 EmpathyImportDialog      *dialog);
-
-static EmpathyImportDialog *dialog_p = NULL;
+  gboolean show_warning;
+  TpawConnectionManagers *cms;
+} EmpathyImportDialogPriv;
+
+G_DEFINE_TYPE (EmpathyImportDialog, empathy_import_dialog, GTK_TYPE_DIALOG)
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportDialog)
 
 static void
-empathy_import_dialog_add_setting (GHashTable              *settings,
-                                   gchar                   *key,
-                                   gpointer                 value,
-                                   EmpathyImportSettingType type)
+import_dialog_add_import_widget (EmpathyImportDialog *self)
 {
-        EmpathyImportSetting *set = g_new0 (EmpathyImportSetting, 1);
+  EmpathyImportWidget *iw;
+  EmpathyImportDialogPriv *priv = GET_PRIV (self);
+  GtkWidget *widget, *area;
 
-        set->value = value;
-        set->type = type;
+  area = gtk_dialog_get_content_area (GTK_DIALOG (self));
 
-        g_hash_table_insert (settings, key, set);
-}
+  iw = empathy_import_widget_new (EMPATHY_IMPORT_APPLICATION_ALL, priv->cms);
+  widget = empathy_import_widget_get_widget (iw);
+  gtk_box_pack_start (GTK_BOX (area), widget, TRUE, TRUE, 0);
+  gtk_widget_show (widget);
 
-static gboolean
-empathy_import_dialog_add_account (gchar      *protocol_name,
-                                   GHashTable *settings)
-{
-        McProfile *profile;
-        McAccount *account;
-
-        DEBUG ("Looking up profile with protocol '%s'", protocol_name);
-        profile = mc_profile_lookup (protocol_name);
-
-        if (profile != NULL) {
-                account = mc_account_create (profile);
-
-                if (account != NULL) {
-                        const gchar *unique_name;
-                        GHashTableIter iter;
-                        gpointer key, value;
-                        EmpathyImportSetting *set;
-
-                        unique_name = mc_account_get_unique_name (account);
-                        mc_account_set_display_name (account, unique_name);
-
-                        g_hash_table_iter_init (&iter, settings);
-                        while (g_hash_table_iter_next (&iter, &key, &value)) {
-                                set = (EmpathyImportSetting *) value;
-                                switch (((EmpathyImportSetting *) value)->type) {
-                                        case EMPATHY_IMPORT_SETTING_TYPE_STRING:
-                                                DEBUG ("Setting %s to (string) %s",
-                                                        (gchar *) key, (gchar *) set->value);
-                                                mc_account_set_param_string (account,
-                                                        (gchar *) key, (gchar *) set->value);
-                                                break;
-
-                                        case EMPATHY_IMPORT_SETTING_TYPE_BOOL:
-                                                DEBUG ("Setting %s to (bool) %i",
-                                                        (gchar *) key, (gboolean) set->value);
-                                                mc_account_set_param_boolean (account,
-                                                        (gchar *) key, (gboolean) set->value);
-                                                break;
-
-                                        case EMPATHY_IMPORT_SETTING_TYPE_INT:
-                                                DEBUG ("Setting %s to (int) %i",
-                                                        (gchar *) key, (gint) set->value);
-                                                mc_account_set_param_int (account,
-                                                        (gchar *) key, (gint) set->value);
-                                                break;
-                                }
-                        }
-                        g_object_unref (account);
-                }
-
-                g_object_unref (profile);
-                return TRUE;
-        }
-        return FALSE;
-}
-
-static void
-empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
-                                            xmlNodePtr setting,
-                                            GHashTable *settings)
-{
-        int i;
-
-        if (!xmlHasProp (setting, (xmlChar *) "name"))
-                return;
-
-        for (i = 0; i < PIDGIN_MC_MAP_ITEMS; i++) {
-                if (strcmp(protocol, pidgin_mc_map[i].protocol) != 0) {
-                        continue;
-                }
-
-                if (strcmp ((gchar *) xmlGetProp (setting, (xmlChar *) "name"),
-                        pidgin_mc_map[i].pidgin_name) == 0) {
-
-                        int arg;
-                        gchar *type = NULL;
-
-                        type = (gchar *) xmlGetProp (setting, (xmlChar *) "type");
-
-                        if (strcmp (type, "bool") == 0) {
-                                sscanf ((gchar *) xmlNodeGetContent (setting),"%i", &arg);
-                                empathy_import_dialog_add_setting (settings, pidgin_mc_map[i].mc_name,
-                                        (gpointer) arg,
-                                        EMPATHY_IMPORT_SETTING_TYPE_BOOL);
-                        } else if (strcmp (type, "int") == 0) {
-                                sscanf ((gchar *) xmlNodeGetContent (setting),
-                                        "%i", &arg);
-                                empathy_import_dialog_add_setting (settings, pidgin_mc_map[i].mc_name,
-                                        (gpointer) arg,
-                                        EMPATHY_IMPORT_SETTING_TYPE_INT);
-                        } else if (strcmp (type, "string") == 0) {
-                                empathy_import_dialog_add_setting (settings, pidgin_mc_map[i].mc_name,
-                                        (gpointer) xmlNodeGetContent (setting),
-                                        EMPATHY_IMPORT_SETTING_TYPE_STRING);
-                        }
-                }
-        }
+  priv->iw = iw;
 
+  gtk_dialog_add_buttons (GTK_DIALOG (self),
+      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+      _("_Import"), GTK_RESPONSE_OK,
+      NULL);
 }
 
 static void
-empathy_import_dialog_pidgin_import_accounts ()
+import_dialog_show_warning_message (EmpathyImportDialog *self)
 {
-        xmlNodePtr rootnode, node, child, setting;
-        xmlParserCtxtPtr ctxt;
-        xmlDocPtr doc;
-        gchar *filename;
-        gchar *protocol = NULL;
-        gchar *name = NULL;
-        gchar *username = NULL;
-        GHashTable *settings;
-
-        ctxt = xmlNewParserCtxt ();
-
-        filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml", NULL);
-
-        doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
-
-        g_free (filename);
-
-        rootnode = xmlDocGetRootElement (doc);
-
-        if (rootnode == NULL) {
-                return;
-        }
-
-        node = rootnode->children;
-
-        while (node) {
-                if (strcmp ((gchar *) node->name, "account") == 0) {
-                        child = node->children;
-
-                        settings = g_hash_table_new (g_str_hash, g_str_equal);
-
-                        while (child) {
-
-                                if (strcmp ((gchar *) child->name, "protocol") == 0) {
-                                        protocol = (gchar *) xmlNodeGetContent (child);
-
-                                        if (g_str_has_prefix (protocol, "prpl-")) {
-                                                protocol = strchr (protocol, '-') + 1;
-                                        }
+  GtkWidget *hbox, *vbox, *w;
 
-                                        if (strcmp (protocol, "bonjour") == 0) {
-                                                protocol = "salut";
-                                        } else if (strcmp (protocol, "novell") == 0) {
-                                                protocol = "groupwise";
-                                        }
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
 
-                                        empathy_import_dialog_add_setting (settings, "protocol",
-                                                (gpointer) protocol,
-                                                EMPATHY_IMPORT_SETTING_TYPE_STRING);
+  w = gtk_label_new (_("No accounts to import could be found. Empathy "
+          "currently only supports importing accounts from Pidgin."));
+  gtk_label_set_line_wrap  (GTK_LABEL (w), TRUE);
+  gtk_label_set_selectable (GTK_LABEL (w), TRUE);
+  gtk_misc_set_alignment   (GTK_MISC  (w), 0.0, 0.0);
+  gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
 
-                                } else if (strcmp ((gchar *) child->name, "name") == 0) {
-                                        name = (gchar *) xmlNodeGetContent (child);
+  w = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING,
+      GTK_ICON_SIZE_DIALOG);
+  gtk_misc_set_alignment (GTK_MISC (w), 0.5, 0.0);
+  gtk_box_pack_start (GTK_BOX (hbox), w, FALSE, FALSE, 0);
 
-                                        if (g_strrstr (name, "/") != NULL) {
-                                                gchar **name_resource;
-                                                name_resource = g_strsplit (name, "/", 2);
-                                                username = name_resource[0];
-                                        } else {
-                                                username = name;
-                                        }
+  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
 
-                                        if (strstr (name, "@") && strcmp (protocol, "irc") == 0) {
-                                                gchar **nick_server;
-                                                nick_server = g_strsplit (name, "@", 2);
-                                                username = nick_server[0];
-                                                empathy_import_dialog_add_setting (settings,
-                                                        "server", (gpointer) nick_server[1],
-                                                        EMPATHY_IMPORT_SETTING_TYPE_STRING);
-                                        }
+  w = gtk_dialog_get_content_area (GTK_DIALOG (self));
+  gtk_box_pack_start (GTK_BOX (w), hbox, FALSE, FALSE, 0);
 
-                                        empathy_import_dialog_add_setting (settings, "account",
-                                                (gpointer) username, EMPATHY_IMPORT_SETTING_TYPE_STRING);
+  gtk_box_set_spacing (GTK_BOX (w), 14); /* 14 + 2 * 5 = 24 */
 
-                                } else if (strcmp ((gchar *) child->name, "password") == 0) {
-                                        empathy_import_dialog_add_setting (settings, "password",
-                                                (gpointer) xmlNodeGetContent (child),
-                                                EMPATHY_IMPORT_SETTING_TYPE_STRING);
+  gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CLOSE,
+      GTK_RESPONSE_CLOSE);
 
-                                } else if (strcmp ((gchar *) child->name, "settings") == 0) {
-
-                                        setting = child->children;
-
-                                        while (setting) {
-                                                empathy_import_dialog_pidgin_parse_setting (protocol,
-                                                        setting, settings);
-                                                setting = setting->next;
-                                        }
+  gtk_widget_show_all (w);
+}
 
-                                }
-                                child = child->next;
-                        }
+static void
+impl_signal_response (GtkDialog *dialog,
+    gint response_id)
+{
+  EmpathyImportDialogPriv *priv = GET_PRIV (dialog);
 
-                        if (g_hash_table_size (settings) > 0) {
-                                empathy_import_dialog_add_account (protocol, settings);
-                        }
-                        g_free (username);
-                        g_hash_table_unref (settings);
+  if (response_id == GTK_RESPONSE_OK)
+    empathy_import_widget_add_selected_accounts (priv->iw);
 
-                }
-                node = node->next;
-        }
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+}
 
-        xmlFreeDoc(doc);
-        xmlFreeParserCtxt (ctxt);
+static void
+do_get_property (GObject *object,
+    guint property_id,
+    GValue *value,
+    GParamSpec *pspec)
+{
+  EmpathyImportDialogPriv *priv = GET_PRIV (object);
+
+  switch (property_id)
+    {
+    case PROP_PARENT:
+      g_value_set_object (value, priv->parent_window);
+      break;
+    case PROP_SHOW_WARNING:
+      g_value_set_boolean (value, priv->show_warning);
+      break;
+    case PROP_CMS:
+      g_value_set_object (value, priv->cms);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
 
+static void
+do_set_property (GObject *object,
+    guint property_id,
+    const GValue *value,
+    GParamSpec *pspec)
+{
+  EmpathyImportDialogPriv *priv = GET_PRIV (object);
+
+  switch (property_id)
+    {
+    case PROP_PARENT:
+      priv->parent_window = g_value_get_object (value);
+      break;
+    case PROP_SHOW_WARNING:
+      priv->show_warning = g_value_get_boolean (value);
+      break;
+    case PROP_CMS:
+      priv->cms = g_value_dup_object (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
 }
 
 static void
-empathy_import_dialog_response_cb (GtkDialog           *dialog_window,
-                                   gint                 response,
-                                   EmpathyImportDialog *dialog)
+do_constructed (GObject *obj)
 {
-        gchar *from = NULL;
-        if (response == GTK_RESPONSE_OK) {
+  EmpathyImportDialog *self = EMPATHY_IMPORT_DIALOG (obj);
+  EmpathyImportDialogPriv *priv = GET_PRIV (self);
+  gboolean have_accounts;
 
-                from = gtk_combo_box_get_active_text (GTK_COMBO_BOX (dialog->combo));
+  have_accounts = empathy_import_accounts_to_import ();
 
-                if (strcmp (from, "Pidgin") == 0) {
-                        empathy_import_dialog_pidgin_import_accounts ();
-                }
+  if (!have_accounts)
+    {
+      if (priv->show_warning)
+        {
+          import_dialog_show_warning_message (self);
         }
-
-        gtk_widget_destroy (GTK_WIDGET (dialog_window));
-        dialog_p = NULL;
-        g_free (dialog);
+      else
+        DEBUG ("No accounts to import; closing dialog silently.");
+    }
+  else
+    {
+      import_dialog_add_import_widget (self);
+    }
+
+  if (priv->parent_window)
+    gtk_window_set_transient_for (GTK_WINDOW (self), priv->parent_window);
 }
 
-void
-empathy_import_dialog_show (GtkWindow *parent)
+static void
+empathy_import_dialog_init (EmpathyImportDialog *self)
 {
-        EmpathyImportDialog *dialog;
+  EmpathyImportDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_IMPORT_DIALOG, EmpathyImportDialogPriv);
 
-        if (dialog_p) {
-                gtk_window_present (GTK_WINDOW (dialog_p->window));
-                return;
-        }
+  self->priv = priv;
 
-        dialog_p = dialog = g_new0 (EmpathyImportDialog, 1);
-
-        dialog->window = gtk_dialog_new_with_buttons (_("Import accounts"),
-                                                      NULL,
-                                                      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
-                                                      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-                                                      GTK_STOCK_OK, GTK_RESPONSE_OK,
-                                                      NULL);
-
-        g_signal_connect (G_OBJECT (dialog->window), "response",
-                          G_CALLBACK (empathy_import_dialog_response_cb),
-                          dialog);
-
-        dialog->label_select = gtk_label_new (_("Select the program to import accounts from:"));
-
-        dialog->combo = gtk_combo_box_new_text ();
-
-        gtk_combo_box_append_text (GTK_COMBO_BOX (dialog->combo), "Pidgin");
-        gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
+  gtk_container_set_border_width (GTK_CONTAINER (self), 5);
+  gtk_window_set_title (GTK_WINDOW (self), _("Import Accounts"));
+  gtk_window_set_modal (GTK_WINDOW (self), TRUE);
+}
+static void
+do_dispose (GObject *obj)
+{
+  EmpathyImportDialogPriv *priv = GET_PRIV (obj);
 
-        gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
-                dialog->label_select);
+  g_clear_object (&priv->cms);
 
-        gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
-                dialog->combo);
+  G_OBJECT_CLASS (empathy_import_dialog_parent_class)->dispose (obj);
+}
 
+static void
+empathy_import_dialog_class_init (EmpathyImportDialogClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+  GtkDialogClass *gtkclass = GTK_DIALOG_CLASS (klass);
+  GParamSpec *param_spec;
+
+  oclass->constructed = do_constructed;
+  oclass->get_property = do_get_property;
+  oclass->set_property = do_set_property;
+  oclass->dispose = do_dispose;
+
+  gtkclass->response = impl_signal_response;
+
+  param_spec = g_param_spec_object ("parent-window",
+      "parent-window", "The parent window",
+      GTK_TYPE_WINDOW,
+      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (oclass, PROP_PARENT, param_spec);
+
+  param_spec = g_param_spec_boolean ("show-warning",
+      "show-warning", "Whether a warning should be shown when there are no "
+       "sources for importing accounts.",
+      FALSE,
+      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (oclass, PROP_SHOW_WARNING, param_spec);
+
+  param_spec = g_param_spec_object ("cms",
+      "TpawConnectionManagers", "EmpathyConnectionManager",
+      TPAW_TYPE_CONNECTION_MANAGERS,
+      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
+  g_object_class_install_property (oclass, PROP_CMS, param_spec);
+
+  g_type_class_add_private (klass, sizeof (EmpathyImportDialogPriv));
+}
 
-        if (parent) {
-                gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
-                                              parent);
-        }
+GtkWidget *
+empathy_import_dialog_new (GtkWindow *parent,
+    gboolean warning,
+    TpawConnectionManagers *cms)
+{
+  g_return_val_if_fail (TPAW_IS_CONNECTION_MANAGERS (cms), NULL);
 
-        gtk_widget_show_all (dialog->window);
+  return g_object_new (EMPATHY_TYPE_IMPORT_DIALOG,
+      "parent-window", parent,
+      "show-warning", warning,
+      "cms", cms,
+      NULL);
 }