aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-import-dialog.c
blob: 77d11298f3de3c7648ff9d6fcc6351bf4e1d697b (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
/*
 * 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
 * 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., 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 <glib.h>
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>

#include <telepathy-glib/util.h>

#include "empathy-import-dialog.h"
#include "empathy-import-pidgin.h"
#include "empathy-import-widget.h"

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include <libempathy/empathy-debug.h>
#include <libempathy/empathy-utils.h>
#include <libempathy-gtk/empathy-ui-utils.h>

enum {
  PROP_PARENT = 1,
  PROP_SHOW_WARNING
};

typedef struct {
  GtkWindow *parent_window;

  EmpathyImportWidget *iw;

  gboolean show_warning;
} EmpathyImportDialogPriv;

G_DEFINE_TYPE (EmpathyImportDialog, empathy_import_dialog, GTK_TYPE_DIALOG)
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyImportDialog)

static void
import_dialog_add_import_widget (EmpathyImportDialog *self)
{
  EmpathyImportWidget *iw;
  EmpathyImportDialogPriv *priv = GET_PRIV (self);
  GtkWidget *widget, *area;

  area = gtk_dialog_get_content_area (GTK_DIALOG (self));

  iw = empathy_import_widget_new (EMPATHY_IMPORT_APPLICATION_ALL);
  widget = empathy_import_widget_get_widget (iw);
  gtk_box_pack_start (GTK_BOX (area), widget, FALSE, FALSE, 0);
  gtk_widget_show (widget);

  priv->iw = iw;

  gtk_dialog_add_buttons (GTK_DIALOG (self), GTK_STOCK_CANCEL,
      GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
}

static void
import_dialog_show_warning_message (EmpathyImportDialog *self)
{
  GtkWidget *hbox, *vbox, *w;

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);

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

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

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

  gtk_dialog_add_button (GTK_DIALOG (self), GTK_STOCK_CLOSE,
      GTK_RESPONSE_CLOSE);

  gtk_widget_show_all (w);
}

static void
impl_signal_response (GtkDialog *dialog,
    gint response_id)
{
  EmpathyImportDialogPriv *priv = GET_PRIV (dialog);

  if (response_id == GTK_RESPONSE_OK)
    empathy_import_widget_add_selected_accounts (priv->iw);

  gtk_widget_destroy (GTK_WIDGET (dialog));
}

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;
    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;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
do_constructed (GObject *obj)
{
  EmpathyImportDialog *self = EMPATHY_IMPORT_DIALOG (obj);
  EmpathyImportDialogPriv *priv = GET_PRIV (self);
  gboolean have_accounts;

  have_accounts = empathy_import_accounts_to_import ();

  if (!have_accounts)
    {
      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);
    }

  if (priv->parent_window)
    gtk_window_set_transient_for (GTK_WINDOW (self), priv->parent_window);
}

static void
empathy_import_dialog_init (EmpathyImportDialog *self)
{
  EmpathyImportDialogPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_IMPORT_DIALOG, EmpathyImportDialogPriv);

  self->priv = priv;

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

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

  g_type_class_add_private (klass, sizeof (EmpathyImportDialogPriv));
}

GtkWidget *
empathy_import_dialog_new (GtkWindow *parent,
    gboolean warning)
{
  return g_object_new (EMPATHY_TYPE_IMPORT_DIALOG, "parent-window",
      parent, "show-warning", warning, NULL);
}