aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy-gtk/empathy-account-widget-generic.c
blob: 56934bb37585e49d8eed89e4c7e9102569db4ed2 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2006-2007 Imendio AB
 * Copyright (C) 2007 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: Xavier Claessens <xclaesse@gmail.com>
 *          Martyn Russell <martyn@imendio.com>
 */

#include <config.h>

#include <string.h>

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include <libmissioncontrol/mc-account.h>
#include <libmissioncontrol/mc-protocol.h>

#include <libempathy/empathy-debug.h>

#include "empathy-account-widget-generic.h"
#include "empathy-ui-utils.h"

#define DEBUG_DOMAIN "AccountWidgetGeneric"

typedef struct {
    McAccount     *account;

    GtkWidget     *sw;
    GtkWidget     *table_settings;
    GtkSizeGroup  *size_group;

    guint          n_rows;
} EmpathyAccountWidgetGeneric;

static gboolean account_widget_generic_entry_focus_cb         (GtkWidget                  *widget,
                                   GdkEventFocus              *event,
                                   EmpathyAccountWidgetGeneric *settings);
static void     account_widget_generic_int_changed_cb         (GtkWidget                  *widget,
                                   EmpathyAccountWidgetGeneric *settings);
static void     account_widget_generic_checkbutton_toggled_cb (GtkWidget                  *widget,
                                   EmpathyAccountWidgetGeneric *settings);
static gchar *  account_widget_generic_format_param_name      (const gchar                *param_name);
static void     account_widget_generic_setup_foreach          (McProtocolParam            *param,
                                   EmpathyAccountWidgetGeneric *settings);
static void     account_widget_generic_destroy_cb             (GtkWidget                  *widget,
                                   EmpathyAccountWidgetGeneric *settings);

static gboolean 
account_widget_generic_entry_focus_cb (GtkWidget                  *widget,
                       GdkEventFocus              *event,
                       EmpathyAccountWidgetGeneric *settings)
{
    const gchar *str;
    const gchar *param_name;

    str = gtk_entry_get_text (GTK_ENTRY (widget));
    param_name = g_object_get_data (G_OBJECT (widget), "param_name");

    mc_account_set_param_string (settings->account, param_name, str);

    return FALSE;
}

static void
account_widget_generic_int_changed_cb (GtkWidget                  *widget,
                       EmpathyAccountWidgetGeneric *settings)
{
    const gchar *param_name;
    gint         value;

    value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
    param_name = g_object_get_data (G_OBJECT (widget), "param_name");

    mc_account_set_param_int (settings->account, param_name, value);
}

static void  
account_widget_generic_checkbutton_toggled_cb (GtkWidget                  *widget,
                           EmpathyAccountWidgetGeneric *settings)
{
    gboolean     active;
    const gchar *param_name;

    active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
    param_name = g_object_get_data (G_OBJECT (widget), "param_name");

    mc_account_set_param_boolean (settings->account, param_name, active);
}

static gchar *
account_widget_generic_format_param_name (const gchar *param_name)
{
    gchar *str;
    gchar *p;

    str = g_strdup (param_name);
    
    if (str && g_ascii_isalpha (str[0])) {
        str[0] = g_ascii_toupper (str[0]);
    }
    
    while ((p = strchr (str, '-')) != NULL) {
        if (p[1] != '\0' && g_ascii_isalpha (p[1])) {
            p[0] = ' ';
            p[1] = g_ascii_toupper (p[1]);
        }

        p++;
    }
    
    return str;
}

static void
account_widget_generic_setup_foreach (McProtocolParam            *param,
                      EmpathyAccountWidgetGeneric *settings)
{
    GtkWidget *widget;
    gchar     *param_name_formatted;

    param_name_formatted = account_widget_generic_format_param_name (param->name);

    gtk_table_resize (GTK_TABLE (settings->table_settings),
              ++settings->n_rows,
              2);

    if (param->signature[0] == 's') {
        gchar *str = NULL;

        str = g_strdup_printf (_("%s:"), param_name_formatted);
        widget = gtk_label_new (str);
        gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
        g_free (str);

        gtk_size_group_add_widget (settings->size_group, widget);
        gtk_table_attach (GTK_TABLE (settings->table_settings),
                  widget,
                  0, 1,
                  settings->n_rows - 1, settings->n_rows,
                  GTK_FILL, 0,
                  0, 0);

        str = NULL;
        widget = gtk_entry_new ();
        mc_account_get_param_string (settings->account,
                         param->name,
                         &str);
        if (str) {
            gtk_entry_set_text (GTK_ENTRY (widget), str);
            g_free (str);
        }

        if (strstr (param->name, "password")) {
            gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
        }

        g_signal_connect (widget, "focus-out-event",
                  G_CALLBACK (account_widget_generic_entry_focus_cb),
                  settings);

        gtk_table_attach (GTK_TABLE (settings->table_settings),
                  widget,
                  1, 2,
                  settings->n_rows - 1, settings->n_rows,
                  GTK_FILL | GTK_EXPAND, 0,
                  0, 0);
    }
    /* int types: ynqiuxt. double type is 'd' */
    else if (param->signature[0] == 'y' ||
         param->signature[0] == 'n' ||
         param->signature[0] == 'q' ||
         param->signature[0] == 'i' ||
         param->signature[0] == 'u' ||
         param->signature[0] == 'x' ||
         param->signature[0] == 't' ||
         param->signature[0] == 'd') {
        gchar   *str = NULL;
        gint     value = 0;
        gdouble  minint = 0;
        gdouble  maxint = 0;
        gdouble  step = 1;
        switch (param->signature[0]) {
        case 'y': minint = G_MININT8;  maxint = G_MAXINT8;   break;
        case 'n': minint = G_MININT16; maxint = G_MAXINT16;  break;
        case 'q': minint = 0;          maxint = G_MAXUINT16; break;
        case 'i': minint = G_MININT32; maxint = G_MAXINT32;  break;
        case 'u': minint = 0;          maxint = G_MAXUINT32; break;
        case 'x': minint = G_MININT64; maxint = G_MAXINT64;  break;
        case 't': minint = 0;          maxint = G_MAXUINT64; break;
        case 'd': minint = G_MININT32; maxint = G_MAXINT32; step = 0.1; break;
        }

        str = g_strdup_printf (_("%s:"), param_name_formatted);
        widget = gtk_label_new (str);
        gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
        g_free (str);

        gtk_size_group_add_widget (settings->size_group, widget);
        gtk_table_attach (GTK_TABLE (settings->table_settings),
                  widget,
                  0, 1,
                  settings->n_rows - 1, settings->n_rows,
                  GTK_FILL, 0,
                  0, 0);

        widget = gtk_spin_button_new_with_range (minint, maxint, step);
        mc_account_get_param_int (settings->account,
                      param->name,
                      &value);
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);

        g_signal_connect (widget, "value-changed",
                  G_CALLBACK (account_widget_generic_int_changed_cb),
                  settings);

        gtk_table_attach (GTK_TABLE (settings->table_settings),
                  widget,
                  1, 2,
                  settings->n_rows - 1, settings->n_rows,
                  GTK_FILL | GTK_EXPAND, 0,
                  0, 0);
    }
    else if (param->signature[0] == 'b') {
        gboolean value = FALSE;

        mc_account_get_param_boolean (settings->account,
                          param->name,
                          &value);

        widget = gtk_check_button_new_with_label (param_name_formatted);
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);

        g_signal_connect (widget, "toggled",
                  G_CALLBACK (account_widget_generic_checkbutton_toggled_cb),
                  settings);

        gtk_table_attach (GTK_TABLE (settings->table_settings),
                  widget,
                  0, 2,
                  settings->n_rows - 1, settings->n_rows,
                  GTK_FILL | GTK_EXPAND, 0,
                  0, 0);
    } else {
        empathy_debug (DEBUG_DOMAIN,
                   "Unknown signature for param %s: %s\n",
                   param_name_formatted, param->signature);
        g_assert_not_reached ();
    }

    g_free (param_name_formatted);

    g_object_set_data_full (G_OBJECT (widget), "param_name", 
                g_strdup (param->name), g_free);
}

static void
accounts_widget_generic_setup (EmpathyAccountWidgetGeneric *settings)
{
    McProtocol *protocol;
    McProfile  *profile;
    GSList     *params;

    profile = mc_account_get_profile (settings->account);
    protocol = mc_profile_get_protocol (profile);

    if (!protocol) {
        /* The CM is not installed, MC shouldn't list them
         * see SF bug #1688779
         * FIXME: We should display something asking the user to 
         * install the CM
         */
        g_object_unref (profile);
        return;
    }

    params = mc_protocol_get_params (protocol);

    g_slist_foreach (params,
             (GFunc) account_widget_generic_setup_foreach,
             settings);

    g_slist_free (params);
    g_object_unref (profile);
    g_object_unref (protocol);
}

static void
account_widget_generic_destroy_cb (GtkWidget                  *widget,
                   EmpathyAccountWidgetGeneric *settings)
{
    g_object_unref (settings->account);
    g_object_unref (settings->size_group);

    g_free (settings);
}

GtkWidget *
empathy_account_widget_generic_new (McAccount *account)
{
    EmpathyAccountWidgetGeneric *settings;

    g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);

    settings = g_new0 (EmpathyAccountWidgetGeneric, 1);

    settings->account = g_object_ref (account);

    settings->table_settings = gtk_table_new (0, 2, FALSE);
    gtk_table_set_row_spacings (GTK_TABLE (settings->table_settings), 6);
    gtk_table_set_col_spacings (GTK_TABLE (settings->table_settings), 6);
    settings->sw = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (settings->sw),
                    GTK_POLICY_AUTOMATIC,
                    GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (settings->sw),
                           settings->table_settings);
    
    accounts_widget_generic_setup (settings);

    g_signal_connect (settings->sw, "destroy",
              G_CALLBACK (account_widget_generic_destroy_cb),
              settings);

    gtk_widget_show_all (settings->sw);

    return settings->sw;
}