#include <gtk/gtk.h>
#include <glib/gi18n.h>
-#include <libxml/parser.h>
-#include <libxml/tree.h>
-
#include <libmissioncontrol/mc-account.h>
+#include <telepathy-glib/util.h>
#include "empathy-import-dialog.h"
+#include "empathy-import-pidgin.h"
#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>
+#include <libempathy/empathy-utils.h>
-typedef enum
-{
- EMPATHY_IMPORT_SETTING_TYPE_STRING,
- EMPATHY_IMPORT_SETTING_TYPE_BOOL,
- EMPATHY_IMPORT_SETTING_TYPE_INT,
-} EmpathyImportSettingType;
+#include <libempathy-gtk/empathy-ui-utils.h>
typedef struct
{
- gpointer value;
- EmpathyImportSettingType type;
-} EmpathyImportSetting;
-
-
-/* Pidgin to MC map */
-typedef struct
-{
- gchar *protocol;
- gchar *pidgin_name;
- gchar *mc_name;
-} PidginMcMapItem;
+ GtkWidget *window;
+ GtkWidget *treeview;
+ GtkWidget *button_ok;
+ GtkWidget *button_cancel;
+ GList *accounts;
+} EmpathyImportDialog;
-static PidginMcMapItem pidgin_mc_map[] =
+enum
{
- { "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" },
+ COL_IMPORT = 0,
+ COL_PROTOCOL,
+ COL_NAME,
+ COL_SOURCE,
+ COL_ACCOUNT_DATA,
+ COL_COUNT
};
-typedef struct
+EmpathyImportAccountData *
+empathy_import_account_data_new (const gchar *source)
{
- GtkWidget *window;
- GtkWidget *label_select;
- GtkWidget *combo;
-} EmpathyImportDialog;
+ EmpathyImportAccountData *data;
-#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);
+ g_return_val_if_fail (!EMP_STR_EMPTY (source), NULL);
+ data = g_slice_new0 (EmpathyImportAccountData);
+ data->settings = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+ (GDestroyNotify) tp_g_value_slice_free);
+ data->source = g_strdup (source);
-static void
-empathy_import_dialog_add_setting (GHashTable *settings,
- gchar *key,
- gpointer value,
- EmpathyImportSettingType type)
-{
- EmpathyImportSetting *set = g_slice_new0 (EmpathyImportSetting);
-
- set->value = value;
- set->type = type;
+ return data;
+}
- g_hash_table_insert (settings, key, set);
+void
+empathy_import_account_data_free (EmpathyImportAccountData *data)
+{
+ if (data == NULL)
+ return;
+ if (data->profile != NULL)
+ g_object_unref (data->profile);
+ if (data->settings != NULL)
+ g_hash_table_destroy (data->settings);
+ if (data->source != NULL)
+ g_free (data->source);
+
+ g_slice_free (EmpathyImportAccountData, data);
}
-static gboolean
-empathy_import_dialog_add_account (gchar *protocol_name,
- GHashTable *settings)
+static void
+import_dialog_add_account (EmpathyImportAccountData *data)
{
- McProfile *profile;
McAccount *account;
- gchar *key_char;
GHashTableIter iter;
gpointer key, value;
- EmpathyImportSetting *set;
gchar *display_name;
- gchar *username;
-
- DEBUG ("Looking up profile with protocol '%s'", protocol_name);
- profile = mc_profile_lookup (protocol_name);
-
- if (profile == NULL)
- return FALSE;
+ GValue *username;
- account = mc_account_create (profile);
+ account = mc_account_create (data->profile);
+ if (account == NULL)
+ {
+ DEBUG ("Failed to create account");
+ return;
+ }
- g_hash_table_iter_init (&iter, settings);
+ g_hash_table_iter_init (&iter, data->settings);
while (g_hash_table_iter_next (&iter, &key, &value))
{
- set = (EmpathyImportSetting *) value;
- key_char = (gchar *) key;
- switch (((EmpathyImportSetting *) value)->type)
+ const gchar *param = key;
+ GValue *gvalue = value;
+
+ switch (G_VALUE_TYPE (gvalue))
{
- case EMPATHY_IMPORT_SETTING_TYPE_STRING:
- DEBUG ("Setting %s to (string) %s",
- key_char, (gchar *) set->value);
+ case G_TYPE_STRING:
+ DEBUG ("Set param '%s' to '%s' (string)",
+ param, g_value_get_string (gvalue));
mc_account_set_param_string (account,
- key_char, (gchar *) set->value);
+ param, g_value_get_string (gvalue));
break;
- case EMPATHY_IMPORT_SETTING_TYPE_BOOL:
- DEBUG ("Setting %s to (bool) %i",
- key_char, (gboolean) set->value);
+ case G_TYPE_BOOLEAN:
+ DEBUG ("Set param '%s' to %s (boolean)",
+ param, g_value_get_boolean (gvalue) ? "TRUE" : "FALSE");
mc_account_set_param_boolean (account,
- key_char, (gboolean) set->value);
+ param, g_value_get_boolean (gvalue));
break;
- case EMPATHY_IMPORT_SETTING_TYPE_INT:
- DEBUG ("Setting %s to (int) %i",
- key_char, (gint) set->value);
+ case G_TYPE_INT:
+ DEBUG ("Set param '%s' to '%i' (integer)",
+ param, g_value_get_int (gvalue));
mc_account_set_param_int (account,
- key_char, (gint) set->value);
+ param, g_value_get_int (gvalue));
break;
}
}
- mc_account_get_param_string (account, "account", &username);
- display_name = g_strdup_printf ("%s (%s)", username,
- mc_profile_get_display_name (profile));
+ /* Set the display name of the account */
+ username = g_hash_table_lookup (data->settings, "account");
+ display_name = g_strdup_printf ("%s (%s)",
+ mc_profile_get_display_name (data->profile),
+ g_value_get_string (username));
mc_account_set_display_name (account, display_name);
- g_free (username);
g_free (display_name);
g_object_unref (account);
- g_object_unref (profile);
- return TRUE;
+}
+
+static gboolean
+import_dialog_account_id_in_list (GList *accounts,
+ const gchar *account_id)
+{
+ GList *l;
+
+ for (l = accounts; l; l = l->next)
+ {
+ McAccount *account = l->data;
+ gchar *value;
+ gboolean result;
+
+ if (mc_account_get_param_string (account, "account", &value)
+ == MC_ACCOUNT_SETTING_ABSENT)
+ continue;
+
+ result = tp_strdiff (value, account_id);
+
+ g_free (value);
+
+ if (!result)
+ return TRUE;
+ }
+
+ return FALSE;
}
static void
-empathy_import_dialog_pidgin_parse_setting (gchar *protocol,
- xmlNodePtr setting,
- GHashTable *settings)
+import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
{
- int i;
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ GList *l;
- if (!xmlHasProp (setting, PIDGIN_ACCOUNT_TAG_NAME))
- return;
+ model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
- for (i = 0; i < G_N_ELEMENTS (pidgin_mc_map); i++)
+ for (l = dialog->accounts; l; l = l->next)
{
- if (strcmp(protocol, pidgin_mc_map[i].protocol) != 0)
- continue;
+ GValue *value;
+ EmpathyImportAccountData *data = l->data;
+ gboolean import;
+ GList *accounts;
- 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);
- }
- }
+ value = g_hash_table_lookup (data->settings, "account");
+
+ accounts = mc_accounts_list_by_profile (data->profile);
+
+ /* Only set the "Import" cell to be active if there isn't already an
+ * account set up with the same account id. */
+ import = !import_dialog_account_id_in_list (accounts,
+ g_value_get_string (value));
+
+ mc_accounts_list_free (accounts);
+
+ gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+
+ gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+ COL_IMPORT, import,
+ COL_PROTOCOL, mc_profile_get_display_name (data->profile),
+ COL_NAME, g_value_get_string (value),
+ COL_SOURCE, data->source,
+ COL_ACCOUNT_DATA, data,
+ -1);
}
}
static void
-empathy_import_dialog_pidgin_import_accounts ()
+import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
+ const gchar *path_str,
+ EmpathyImportDialog *dialog)
{
- xmlNodePtr rootnode, node, child, setting;
- xmlParserCtxtPtr ctxt;
- xmlDocPtr doc;
- gchar *filename;
- gchar *protocol = NULL;
- gchar *name = NULL;
- gchar *username = NULL;
- GHashTable *settings;
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ GtkTreePath *path;
+
+ path = gtk_tree_path_new_from_string (path_str);
+ model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
- ctxt = xmlNewParserCtxt ();
- filename = g_build_filename (g_get_home_dir (), ".purple", "accounts.xml",
+ gtk_tree_model_get_iter (model, &iter, path);
+
+ gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+ COL_IMPORT, !gtk_cell_renderer_toggle_get_active (cell_renderer),
+ -1);
+
+ gtk_tree_path_free (path);
+}
+
+static void
+import_dialog_set_up_account_list (EmpathyImportDialog *dialog)
+{
+ GtkListStore *store;
+ GtkTreeView *view;
+ GtkTreeViewColumn *column;
+ GtkCellRenderer *cell;
+
+ store = gtk_list_store_new (COL_COUNT, G_TYPE_BOOLEAN, G_TYPE_STRING,
+ G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
+
+ gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->treeview),
+ GTK_TREE_MODEL (store));
+
+ g_object_unref (store);
+
+ view = GTK_TREE_VIEW (dialog->treeview);
+ gtk_tree_view_set_headers_visible (view, TRUE);
+
+ /* Import column */
+ cell = gtk_cell_renderer_toggle_new ();
+ gtk_tree_view_insert_column_with_attributes (view, -1,
+ /* Translators: this is the header of a treeview column */
+ _("Import"), cell,
+ "active", COL_IMPORT,
NULL);
- doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
- g_free (filename);
- rootnode = xmlDocGetRootElement (doc);
- if (rootnode == NULL)
- return;
+ g_signal_connect (cell, "toggled",
+ G_CALLBACK (import_dialog_cell_toggled_cb), dialog);
- node = rootnode->children;
- while (node)
- {
- 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);
- }
+ /* Protocol column */
+ column = gtk_tree_view_column_new ();
+ gtk_tree_view_column_set_title (column, _("Protocol"));
+ gtk_tree_view_column_set_expand (column, TRUE);
+ gtk_tree_view_append_column (view, column);
- node = node->next;
- }
+ cell = gtk_cell_renderer_text_new ();
+ g_object_set (cell,
+ "editable", FALSE,
+ NULL);
+ gtk_tree_view_column_pack_start (column, cell, TRUE);
+ gtk_tree_view_column_add_attribute (column, cell, "text", COL_PROTOCOL);
+
+ /* Account column */
+ column = gtk_tree_view_column_new ();
+ gtk_tree_view_column_set_title (column, _("Account"));
+ gtk_tree_view_column_set_expand (column, TRUE);
+ gtk_tree_view_append_column (view, column);
+
+ cell = gtk_cell_renderer_text_new ();
+ g_object_set (cell,
+ "editable", FALSE,
+ NULL);
+ gtk_tree_view_column_pack_start (column, cell, TRUE);
+ gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
+
+ /* Source column */
+ column = gtk_tree_view_column_new ();
+ gtk_tree_view_column_set_title (column, _("Source"));
+ gtk_tree_view_column_set_expand (column, TRUE);
+ gtk_tree_view_append_column (view, column);
+
+ cell = gtk_cell_renderer_text_new ();
+ g_object_set (cell,
+ "editable", FALSE,
+ NULL);
+ gtk_tree_view_column_pack_start (column, cell, TRUE);
+ gtk_tree_view_column_add_attribute (column, cell, "text", COL_SOURCE);
+
+ import_dialog_add_accounts_to_model (dialog);
+}
+
+static gboolean
+import_dialog_tree_model_foreach (GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer user_data)
+{
+ gboolean to_import;
+ EmpathyImportAccountData *data;
- xmlFreeDoc(doc);
- xmlFreeParserCtxt (ctxt);
+ gtk_tree_model_get (model, iter,
+ COL_IMPORT, &to_import,
+ COL_ACCOUNT_DATA, &data,
+ -1);
+
+ if (to_import)
+ import_dialog_add_account (data);
+
+ return FALSE;
}
static void
-empathy_import_dialog_response_cb (GtkDialog *dialog_window,
- gint response,
- EmpathyImportDialog *dialog)
+import_dialog_response_cb (GtkWidget *widget,
+ gint response,
+ EmpathyImportDialog *dialog)
{
- gchar *from = NULL;
if (response == GTK_RESPONSE_OK)
{
- from = gtk_combo_box_get_active_text (GTK_COMBO_BOX (dialog->combo));
+ GtkTreeModel *model;
- if (strcmp (from, "Pidgin") == 0)
- empathy_import_dialog_pidgin_import_accounts ();
-
- g_free (from);
+ model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
+ gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
}
- gtk_widget_hide (GTK_WIDGET (dialog_window));
+ gtk_widget_destroy (dialog->window);
+}
+
+static void
+import_dialog_destroy_cb (GtkWidget *widget,
+ EmpathyImportDialog *dialog)
+{
+ g_list_foreach (dialog->accounts, (GFunc) empathy_import_account_data_free,
+ NULL);
+ g_list_free (dialog->accounts);
+ g_slice_free (EmpathyImportDialog, dialog);
+}
+
+gboolean
+empathy_import_dialog_accounts_to_import (void)
+{
+ return empathy_import_pidgin_accounts_to_import ();
}
void
-empathy_import_dialog_show (GtkWindow *parent)
+empathy_import_dialog_show (GtkWindow *parent,
+ gboolean warning)
{
static EmpathyImportDialog *dialog = NULL;
+ GtkBuilder *gui;
+ gchar *filename;
+ GList *accounts = NULL;
+ /* This window is a singleton. If it already exist, present it */
if (dialog)
{
gtk_window_present (GTK_WINDOW (dialog->window));
return;
}
- dialog = g_slice_new0 (EmpathyImportDialog);
+ /* Load all accounts from all supported applications */
+ accounts = g_list_concat (accounts, empathy_import_pidgin_load ());
- 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);
+ /* Check if we have accounts to import before creating the window */
+ if (!accounts)
+ {
+ GtkWidget *message;
+
+ if (warning)
+ {
+ message = gtk_message_dialog_new (parent,
+ GTK_DIALOG_MODAL, GTK_MESSAGE_WARNING, GTK_BUTTONS_CLOSE,
+ _("No accounts to import could be found. Empathy currently only "
+ "supports importing accounts from Pidgin."));
- g_signal_connect (G_OBJECT (dialog->window), "response",
- G_CALLBACK (empathy_import_dialog_response_cb),
- dialog);
+ gtk_dialog_run (GTK_DIALOG (message));
+ gtk_widget_destroy (message);
+ }
+ else
+ DEBUG ("No accounts to import; closing dialog silently.");
- dialog->label_select = gtk_label_new (
- _("Select the program to import accounts from:"));
- gtk_widget_show (dialog->label_select);
+ return;
+ }
+
+ /* We have accounts, let's display the window with them */
+ dialog = g_slice_new0 (EmpathyImportDialog);
+ dialog->accounts = accounts;
- dialog->combo = gtk_combo_box_new_text ();
- gtk_widget_show (dialog->combo);
+ filename = empathy_file_lookup ("empathy-import-dialog.ui", "src");
+ gui = empathy_builder_get_file (filename,
+ "import_dialog", &dialog->window,
+ "treeview", &dialog->treeview,
+ NULL);
- gtk_combo_box_append_text (GTK_COMBO_BOX (dialog->combo), "Pidgin");
- gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->combo), 0);
+ empathy_builder_connect (gui, dialog,
+ "import_dialog", "destroy", import_dialog_destroy_cb,
+ "import_dialog", "response", import_dialog_response_cb,
+ NULL);
- gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
- dialog->label_select);
+ g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);
- gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog->window)->vbox),
- dialog->combo);
+ g_free (filename);
+ g_object_unref (gui);
if (parent)
gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);
+ import_dialog_set_up_account_list (dialog);
+
gtk_widget_show (dialog->window);
}
+