aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-import-dialog.c
blob: 80e2d747abe67a5e398d19bb7fcee523e2738803 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (C) 2008 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
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * 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.
 *
 * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
 * */

#include <config.h>

#include <string.h>

#include <glib.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <glib/gi18n.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>

#include <libempathy-gtk/empathy-ui-utils.h>

typedef struct
{
  GtkWidget *window;
  GtkWidget *treeview;
  GtkWidget *button_ok;
  GtkWidget *button_cancel;
  GList *accounts;
} EmpathyImportDialog;

enum
{
  COL_IMPORT = 0,
  COL_PROTOCOL,
  COL_NAME,
  COL_SOURCE,
  COL_ACCOUNT_DATA,
  COL_COUNT
};

EmpathyImportAccountData *
empathy_import_account_data_new (const gchar *source)
{
  EmpathyImportAccountData *data;

  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);

  return data;
}

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 void
import_dialog_add_account (EmpathyImportAccountData *data)
{
  McAccount *account;
  GHashTableIter iter;
  gpointer key, value;
  gchar *display_name;
  GValue *username;

  account = mc_account_create (data->profile);

  g_hash_table_iter_init (&iter, data->settings);
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      const gchar *param = key;
      GValue *gvalue = value;

      switch (G_VALUE_TYPE (gvalue))
        {
          case G_TYPE_STRING:
            DEBUG ("Set param '%s' to '%s' (string)",
                param, g_value_get_string (gvalue));
            mc_account_set_param_string (account,
                param, g_value_get_string (gvalue));
            break;

          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,
                param, g_value_get_boolean (gvalue));
            break;

          case G_TYPE_INT:
            DEBUG ("Set param '%s' to '%i' (integer)",
                param, g_value_get_int (gvalue));
            mc_account_set_param_int (account,
                param, g_value_get_int (gvalue));
            break;
        }
    }

  /* 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 (display_name);
  g_object_unref (account);
}

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
import_dialog_add_accounts_to_model (EmpathyImportDialog *dialog)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  GList *l;

  model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));

  for (l = dialog->accounts; l; l = l->next)
    {
      GValue *value;
      EmpathyImportAccountData *data = l->data;
      gboolean import;
      GList *accounts;

      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
import_dialog_cell_toggled_cb (GtkCellRendererToggle *cell_renderer,
                               const gchar *path_str,
                               EmpathyImportDialog *dialog)
{
  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));

  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);

  g_signal_connect (cell, "toggled",
      G_CALLBACK (import_dialog_cell_toggled_cb), dialog);

  /* 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);

  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;

  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
import_dialog_response_cb (GtkWidget *widget,
                           gint response,
                           EmpathyImportDialog *dialog)
{
  if (response == GTK_RESPONSE_OK)
    {
      GtkTreeModel *model;

      model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->treeview));
      gtk_tree_model_foreach (model, import_dialog_tree_model_foreach, dialog);
    }

  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);
}

void
empathy_import_dialog_show (GtkWindow *parent,
                            gboolean warning)
{
  static EmpathyImportDialog *dialog = NULL;
  GladeXML *glade;
  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;
    }

  /* Load all accounts from all supported applications */
  accounts = g_list_concat (accounts, empathy_import_pidgin_load ());

  /* 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."));

          gtk_dialog_run (GTK_DIALOG (message));
          gtk_widget_destroy (message);
        }
      else
        DEBUG ("No accounts to import; closing dialog silently.");

      return;
    }
  
  /* We have accounts, let's display the window with them */
  dialog = g_slice_new0 (EmpathyImportDialog);
  dialog->accounts = accounts;  

  filename = empathy_file_lookup ("empathy-import-dialog.glade", "src");
  glade = empathy_glade_get_file (filename,
      "import_dialog",
      NULL,
      "import_dialog", &dialog->window,
      "treeview", &dialog->treeview,
      NULL);

  empathy_glade_connect (glade,
      dialog,
      "import_dialog", "destroy", import_dialog_destroy_cb,
      "import_dialog", "response", import_dialog_response_cb,
      NULL);

  g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog);

  g_free (filename);
  g_object_unref (glade);

  if (parent)
    gtk_window_set_transient_for (GTK_WINDOW (dialog->window), parent);

  import_dialog_set_up_account_list (dialog);

  gtk_widget_show (dialog->window);
}