]> git.0d.be Git - empathy.git/blobdiff - src/empathy-import-dialog.c
Updated Oriya translation
[empathy.git] / src / empathy-import-dialog.c
index 87606c2341c7f66425f3bf44759609fc5f0e719f..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;
+typedef struct {
+  GtkWindow *parent_window;
 
+  EmpathyImportWidget *iw;
 
-/* Pidgin to MC map */
-typedef struct
-{
-  gchar *protocol;
-  gchar *pidgin_name;
-  gchar *mc_name;
-} PidginMcMapItem;
+  gboolean show_warning;
+  TpawConnectionManagers *cms;
+} EmpathyImportDialogPriv;
 
-static PidginMcMapItem 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" },
-};
+G_DEFINE_TYPE (EmpathyImportDialog, empathy_import_dialog, GTK_TYPE_DIALOG)
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportDialog)
 
-typedef struct
+static void
+import_dialog_add_import_widget (EmpathyImportDialog *self)
 {
-  GtkWidget *window;
-  GtkWidget *label_select;
-  GtkWidget *combo;
-} EmpathyImportDialog;
-
-#define PIDGIN_ACCOUNT_TAG_NAME "name"
-#define PIDGIN_ACCOUNT_TAG_ACCOUNT "account"
-#define PIDGIN_ACCOUNT_TAG_PROTOCOL "protocol"
-#define PIDGIN_ACCOUNT_TAG_PASSWORD "password"
-#define PIDGIN_ACCOUNT_TAG_SETTINGS "settings"
-#define PIDGIN_SETTING_PROP_TYPE "type"
-#define PIDGIN_PROTOCOL_BONJOUR "bonjour"
-#define PIDGIN_PROTOCOL_NOVELL "novell"
-
-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);
+  EmpathyImportWidget *iw;
+  EmpathyImportDialogPriv *priv = GET_PRIV (self);
+  GtkWidget *widget, *area;
 
+  area = gtk_dialog_get_content_area (GTK_DIALOG (self));
 
-static void
-empathy_import_dialog_add_setting (GHashTable *settings,
-                                   gchar *key,
-                                   gpointer value,
-                                   EmpathyImportSettingType type)
-{
-  EmpathyImportSetting *set = g_slice_new0 (EmpathyImportSetting);
+  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);
 
-  set->value = value;
-  set->type = type;
+  priv->iw = iw;
 
-  g_hash_table_insert (settings, key, set);
+  gtk_dialog_add_buttons (GTK_DIALOG (self),
+      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+      _("_Import"), GTK_RESPONSE_OK,
+      NULL);
 }
 
-static gboolean
-empathy_import_dialog_add_account (gchar *protocol_name,
-                                   GHashTable *settings)
+static void
+import_dialog_show_warning_message (EmpathyImportDialog *self)
 {
-  McProfile *profile;
-  McAccount *account;
-  gchar *key_char;
-  GHashTableIter iter;
-  gpointer key, value;
-  EmpathyImportSetting *set;
-  gchar *display_name;
-  gchar *username;
+  GtkWidget *hbox, *vbox, *w;
 
-  DEBUG ("Looking up profile with protocol '%s'", protocol_name);
-  profile = mc_profile_lookup (protocol_name);
+  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
 
-  if (profile == NULL)
-    return FALSE;
+  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);
 
-  account = mc_account_create (profile);
+  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);
 
-  g_hash_table_iter_init (&iter, settings);
-  while (g_hash_table_iter_next (&iter, &key, &value))
-    {
-      set = (EmpathyImportSetting *) value;
-      key_char = (gchar *) key;
-      switch (((EmpathyImportSetting *) value)->type)
-        {
-          case EMPATHY_IMPORT_SETTING_TYPE_STRING:
-            DEBUG ("Setting %s to (string) %s",
-                key_char, (gchar *) set->value);
-            mc_account_set_param_string (account,
-                key_char, (gchar *) set->value);
-            break;
-
-          case EMPATHY_IMPORT_SETTING_TYPE_BOOL:
-            DEBUG ("Setting %s to (bool) %i",
-                key_char, (gboolean) set->value);
-            mc_account_set_param_boolean (account,
-                key_char, (gboolean) set->value);
-            break;
-
-          case EMPATHY_IMPORT_SETTING_TYPE_INT:
-            DEBUG ("Setting %s to (int) %i",
-                key_char, (gint) set->value);
-            mc_account_set_param_int (account,
-                key_char, (gint) set->value);
-            break;
-        }
-    }
+  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
+
+  w = gtk_dialog_get_content_area (GTK_DIALOG (self));
+  gtk_box_pack_start (GTK_BOX (w), hbox, FALSE, FALSE, 0);
+
+  gtk_box_set_spacing (GTK_BOX (w), 14); /* 14 + 2 * 5 = 24 */
 
-  mc_account_get_param_string (account, "account", &username);
-  display_name = g_strdup_printf ("%s (%s)", username,
-      mc_profile_get_display_name (profile));
-  mc_account_set_display_name (account, display_name);
+  gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CLOSE,
+      GTK_RESPONSE_CLOSE);
 
-  g_free (username);
-  g_free (display_name);
-  g_object_unref (account);
-  g_object_unref (profile);
-  return TRUE;
+  gtk_widget_show_all (w);
 }
 
 static void
-empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
-                                            xmlNodePtr setting,
-                                            GHashTable *settings)
+impl_signal_response (GtkDialog *dialog,
+    gint response_id)
 {
-  int i;
+  EmpathyImportDialogPriv *priv = GET_PRIV (dialog);
 
-  if (!xmlHasProp (setting, PIDGIN_ACCOUNT_TAG_NAME))
-    return;
+  if (response_id == GTK_RESPONSE_OK)
+    empathy_import_widget_add_selected_accounts (priv->iw);
 
-  for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
-    {
-      if (strcmp(protocol, pidgin_mc_map[i].protocol) != 0)
-        continue;
-
-      if (strcmp ((gchar *) xmlGetProp (setting, PIDGIN_ACCOUNT_TAG_NAME),
-        pidgin_mc_map[i].pidgin_name) == 0)
-        {
-          gint arg;
-          gchar *type = NULL;
-
-          type = (gchar *) xmlGetProp (setting, PIDGIN_SETTING_PROP_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);
-            }
-        }
-    }
+  gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
 static void
-empathy_import_dialog_pidgin_import_accounts ()
+do_get_property (GObject *object,
+    guint property_id,
+    GValue *value,
+    GParamSpec *pspec)
 {
-  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;
+  EmpathyImportDialogPriv *priv = GET_PRIV (object);
 
-  node = rootnode->children;
-  while (node)
+  switch (property_id)
     {
-      if (strcmp ((gchar *) node->name, PIDGIN_ACCOUNT_TAG_ACCOUNT) == 0)
-        {
-          child = node->children;
-
-          settings = g_hash_table_new (g_str_hash, g_str_equal);
-
-          while (child)
-            {
-
-              if (strcmp ((gchar *) child->name,
-                  PIDGIN_ACCOUNT_TAG_PROTOCOL) == 0)
-                {
-                  protocol = (gchar *) xmlNodeGetContent (child);
-
-                  if (g_str_has_prefix (protocol, "prpl-"))
-                    protocol = strchr (protocol, '-') + 1;
-
-                  if (strcmp (protocol, PIDGIN_PROTOCOL_BONJOUR) == 0)
-                    protocol = "salut";
-                  else if (strcmp (protocol, PIDGIN_PROTOCOL_NOVELL) == 0)
-                    protocol = "groupwise";
-
-                  empathy_import_dialog_add_setting (settings, "protocol",
-                      (gpointer) protocol,
-                      EMPATHY_IMPORT_SETTING_TYPE_STRING);
-
-                }
-              else if (strcmp ((gchar *) child->name,
-                  PIDGIN_ACCOUNT_TAG_NAME) == 0)
-                {
-                  name = (gchar *) xmlNodeGetContent (child);
-
-                  if (g_strrstr (name, "/") != NULL)
-                    {
-                      gchar **name_resource;
-                      name_resource = g_strsplit (name, "/", 2);
-                      username = g_strdup(name_resource[0]);
-                      g_free (name_resource);
-                    }
-                  else
-                    username = name;
-
-                 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);
-                  }
-
-                  empathy_import_dialog_add_setting (settings, "account",
-                      (gpointer) username, EMPATHY_IMPORT_SETTING_TYPE_STRING);
-
-                }
-              else if (strcmp ((gchar *) child->name,
-                  PIDGIN_ACCOUNT_TAG_PASSWORD) == 0)
-                {
-                  empathy_import_dialog_add_setting (settings, "password",
-                      (gpointer) xmlNodeGetContent (child),
-                      EMPATHY_IMPORT_SETTING_TYPE_STRING);
-
-                }
-              else if (strcmp ((gchar *) child->name,
-                  PIDGIN_ACCOUNT_TAG_SETTINGS) == 0)
-                {
-                  setting = child->children;
-
-                  while (setting)
-                    {
-                      empathy_import_dialog_pidgin_parse_setting (protocol,
-                          setting, settings);
-                          setting = setting->next;
-                    }
-
-                }
-              child = child->next;
-            }
-
-          if (g_hash_table_size (settings) > 0)
-              empathy_import_dialog_add_account (protocol, settings);
-
-          g_free (username);
-          g_hash_table_unref (settings);
-        }
-
-      node = node->next;
+    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);
     }
-
-  xmlFreeDoc(doc);
-  xmlFreeParserCtxt (ctxt);
 }
 
 static void
-empathy_import_dialog_response_cb (GtkDialog *dialog_window,
-                                   gint response,
-                                   EmpathyImportDialog *dialog)
+do_set_property (GObject *object,
+    guint property_id,
+    const GValue *value,
+    GParamSpec *pspec)
 {
-  gchar *from = NULL;
-  if (response == GTK_RESPONSE_OK)
-    {
-      from = gtk_combo_box_get_active_text (GTK_COMBO_BOX (dialog->combo));
-
-      if (strcmp (from, "Pidgin") == 0)
-        empathy_import_dialog_pidgin_import_accounts ();
+  EmpathyImportDialogPriv *priv = GET_PRIV (object);
 
-      g_free (from);
+  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);
     }
-
-  gtk_widget_hide (GTK_WIDGET (dialog_window));
 }
 
-void
-empathy_import_dialog_show (GtkWindow *parent)
+static void
+do_constructed (GObject *obj)
 {
-  static EmpathyImportDialog *dialog = NULL;
+  EmpathyImportDialog *self = EMPATHY_IMPORT_DIALOG (obj);
+  EmpathyImportDialogPriv *priv = GET_PRIV (self);
+  gboolean have_accounts;
+
+  have_accounts = empathy_import_accounts_to_import ();
 
-  if (dialog)
+  if (!have_accounts)
     {
-      gtk_window_present (GTK_WINDOW (dialog->window));
-      return;
+      if (priv->show_warning)
+        {
+          import_dialog_show_warning_message (self);
+        }
+      else
+        DEBUG ("No accounts to import; closing dialog silently.");
+    }
+  else
+    {
+      import_dialog_add_import_widget (self);
     }
 
-  dialog = g_slice_new0 (EmpathyImportDialog);
-
-  dialog->window = gtk_dialog_new_with_buttons (_("Import accounts"),
-      NULL,
-      GTK_DIALOG_MODAL,
-      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-      GTK_STOCK_OK, GTK_RESPONSE_OK,
-      NULL);
+  if (priv->parent_window)
+    gtk_window_set_transient_for (GTK_WINDOW (self), priv->parent_window);
+}
 
-  g_signal_connect (G_OBJECT (dialog->window), "response",
-      G_CALLBACK (empathy_import_dialog_response_cb),
-      dialog);
+static void
+empathy_import_dialog_init (EmpathyImportDialog *self)
+{
+  EmpathyImportDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_IMPORT_DIALOG, EmpathyImportDialogPriv);
 
-  dialog->label_select = gtk_label_new (
-      _("Select the program to import accounts from:"));
-  gtk_widget_show (dialog->label_select);
+  self->priv = priv;
 
-  dialog->combo = gtk_combo_box_new_text ();
-  gtk_widget_show (dialog->combo);
+  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_combo_box_append_text (GTK_COMBO_BOX (dialog->combo), "Pidgin");
-  gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
+  g_clear_object (&priv->cms);
 
-  gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
-      dialog->label_select);
+  G_OBJECT_CLASS (empathy_import_dialog_parent_class)->dispose (obj);
+}
 
-  gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
-      dialog->combo);
+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 (dialog->window);
+  return g_object_new (EMPATHY_TYPE_IMPORT_DIALOG,
+      "parent-window", parent,
+      "show-warning", warning,
+      "cms", cms,
+      NULL);
 }