root/trunk/src/tmut-account-editor.c

Revision 93 (checked in by pvanhoof, 2 months ago)

2008-09-25 martin bonnin <martinbonnin@gmail.com>

Allows to change port and authentication mechanism of an account using
the account manager UI for testing purposes. This particularly makes
it possible to send emails using gmail smtp server with tmut.

Line 
1 /* TMut
2  * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with self library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #if HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <glib/gi18n-lib.h>
25
26 #include "tmut-account-editor.h"
27 #include "tmut-shell-window.h"
28 #include "tmut-shell-child.h"
29
30 #include <tny-camel-account.h>
31 #include <tny-camel-transport-account.h>
32 #include <tny-simple-list.h>
33 #include <tny-list.h>
34 #include <tny-iterator.h>
35
36
37 static GObjectClass *parent_class = NULL;
38
39 typedef struct _TMutAccountEditorPriv TMutAccountEditorPriv;
40
41 typedef enum {
42         TMUT_ACCOUNT_EDITOR_OK_CLICKED,
43         TMUT_ACCOUNT_EDITOR_LAST_SIGNAL
44 } TMutAccountEditorSignal;
45
46 static guint tmut_account_editor_signals [TMUT_ACCOUNT_EDITOR_LAST_SIGNAL];
47
48 struct _TMutAccountEditorPriv
49 {
50         TnyAccount *account;
51         TMutShellWindow *shell;
52
53         GtkWidget *name_entry, *hostname_entry, *enabled_checkbutton,
54                   *proto_entry, *type_entry, *user_entry, *options_entry,
55                   *port_entry, *auth_mech_entry, *from_entry;
56 };
57
58 #define TMUT_ACCOUNT_EDITOR_GET_PRIVATE(o) \
59         (G_TYPE_INSTANCE_GET_PRIVATE ((o), TMUT_TYPE_ACCOUNT_EDITOR, TMutAccountEditorPriv))
60
61
62 gboolean
63 tmut_account_editor_get_enabled (TMutAccountEditor *self)
64 {
65         return TRUE;
66 }
67
68 const gchar*
69 tmut_account_editor_get_name (TMutAccountEditor *self)
70 {
71         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
72         return gtk_entry_get_text (GTK_ENTRY (priv->name_entry));
73 }
74
75 const gchar*
76 tmut_account_editor_get_hostname (TMutAccountEditor *self)
77 {
78         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
79         return gtk_entry_get_text (GTK_ENTRY (priv->hostname_entry));
80 }
81
82 const gchar*
83 tmut_account_editor_get_from (TMutAccountEditor *self)
84 {
85         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
86         return gtk_entry_get_text (GTK_ENTRY (priv->from_entry));
87 }
88
89
90 const gchar*
91 tmut_account_editor_get_proto (TMutAccountEditor *self)
92 {
93         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
94         return gtk_entry_get_text (GTK_ENTRY (priv->proto_entry));
95 }
96
97 const gchar*
98 tmut_account_editor_get_account_type (TMutAccountEditor *self)
99 {
100         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
101         return gtk_entry_get_text (GTK_ENTRY (priv->type_entry));
102 }
103
104 const gchar*
105 tmut_account_editor_get_user (TMutAccountEditor *self)
106 {
107         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
108         return gtk_entry_get_text (GTK_ENTRY (priv->user_entry));
109 }
110
111 gchar**
112 tmut_account_editor_get_options (TMutAccountEditor *self)
113 {
114         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
115         const gchar *str = gtk_entry_get_text (GTK_ENTRY (priv->options_entry));
116         return g_strsplit (str?str:"", ",", -1);
117 }
118
119 gint
120 tmut_account_editor_get_port (TMutAccountEditor *self)
121 {
122         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
123         return atoi(gtk_entry_get_text (GTK_ENTRY (priv->port_entry)));
124 }
125
126 const gchar*
127 tmut_account_editor_get_auth_mech (TMutAccountEditor *self)
128 {
129         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
130         const gchar *mech = gtk_entry_get_text (GTK_ENTRY (priv->auth_mech_entry));
131        
132         return mech[0] == '\0' ? NULL : mech;
133 }
134
135 TnyAccount *
136 tmut_account_editor_get_account (TMutAccountEditor *self)
137 {
138         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
139         if (priv->account)
140                 g_object_ref (priv->account);
141         return priv->account;
142 }
143
144 static void
145 load_account (TMutAccountEditor *self, TnyAccount *account)
146 {
147         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
148
149         if (account) {
150                 TnyList *options = tny_simple_list_new ();
151                 gboolean first = TRUE;
152                 TnyIterator *iter;
153                 GString *options_string = g_string_new ("");
154
155                 gtk_entry_set_text (GTK_ENTRY (priv->name_entry),
156                         tny_account_get_name (account)?tny_account_get_name (account):"");
157                 gtk_entry_set_text (GTK_ENTRY (priv->proto_entry),
158                         tny_account_get_proto (account)?tny_account_get_proto (account):"");
159                 gtk_entry_set_text (GTK_ENTRY (priv->hostname_entry),
160                         tny_account_get_hostname (account)?tny_account_get_hostname (account):"");
161
162                 gtk_widget_set_sensitive (GTK_WIDGET (priv->type_entry), FALSE);
163
164                 if (tny_account_get_proto (account) && strcmp (tny_account_get_proto (account), "smtp") == 0) {
165                         gtk_entry_set_text (GTK_ENTRY (priv->type_entry), "transport");
166                         gtk_widget_show (GTK_WIDGET (priv->from_entry));
167                 } else {
168                         gtk_entry_set_text (GTK_ENTRY (priv->type_entry), "store");
169                         gtk_widget_hide (GTK_WIDGET (priv->from_entry));
170                 }
171
172                 gtk_entry_set_text (GTK_ENTRY (priv->user_entry),
173                         tny_account_get_user (account)?tny_account_get_user (account):"");
174
175                 if (TNY_IS_CAMEL_TRANSPORT_ACCOUNT (account)) {
176                         TnyCamelTransportAccount *ta = (TnyCamelTransportAccount *) account;
177                         gtk_entry_set_text (GTK_ENTRY (priv->from_entry),
178                                 tny_camel_transport_account_get_from (ta)?tny_camel_transport_account_get_from (ta):"");
179                 }
180
181                 tny_camel_account_get_options (TNY_CAMEL_ACCOUNT (account), options);
182                 iter = tny_list_create_iterator (options);
183
184                 while (!tny_iterator_is_done (iter)) {
185                         TnyPair *pair = TNY_PAIR (tny_iterator_get_current (iter));
186                         const gchar *value = tny_pair_get_value (pair);
187
188                         if (!first)
189                                 g_string_append_c (options_string, ',');
190
191                         g_string_append (options_string,
192                                 tny_pair_get_name (pair));
193
194                         if (value) {
195                                 g_string_append_c (options_string, '=');
196                                 g_string_append (options_string, value);
197                         }
198
199                         first = FALSE;
200
201                         tny_iterator_next (iter);
202                 }
203
204                 g_object_unref (iter);
205                 g_object_unref (options);
206
207                 gtk_entry_set_text (GTK_ENTRY (priv->options_entry),
208                         options_string->str);
209
210                 gchar tmp[32] = {0};
211                 g_snprintf(tmp, sizeof(tmp) - 1, "%d", tny_account_get_port (account));
212                 gtk_entry_set_text (GTK_ENTRY (priv->port_entry), tmp);
213
214                 gtk_entry_set_text (GTK_ENTRY (priv->auth_mech_entry),
215                         tny_account_get_secure_auth_mech (account)?tny_account_get_secure_auth_mech (account):"");
216
217                 g_string_free (options_string, TRUE);
218
219         } else {
220
221                 gtk_widget_set_sensitive (GTK_WIDGET (priv->type_entry), TRUE);
222                 gtk_widget_show (GTK_WIDGET (priv->from_entry));
223
224                 gtk_entry_set_text (GTK_ENTRY (priv->name_entry), _("Account name"));
225                 gtk_entry_set_text (GTK_ENTRY (priv->proto_entry), "imap");
226                 gtk_entry_set_text (GTK_ENTRY (priv->hostname_entry), _("imap.gmail.com"));
227                 gtk_entry_set_text (GTK_ENTRY (priv->type_entry), "store");
228                 gtk_entry_set_text (GTK_ENTRY (priv->user_entry), _("user.name"));
229                 gtk_entry_set_text (GTK_ENTRY (priv->options_entry), _("use_ssl=wrapped"));
230                 gtk_entry_set_text (GTK_ENTRY (priv->port_entry), "-1");
231                 gtk_entry_set_text (GTK_ENTRY (priv->auth_mech_entry), "");
232         }
233 }
234
235 static void
236 tmut_account_editor_on_ok_clicked (GtkWidget *button, gpointer user_data)
237 {
238         TMutAccountEditor *self = (TMutAccountEditor *) user_data;
239         g_signal_emit (G_OBJECT (self),
240                 tmut_account_editor_signals [TMUT_ACCOUNT_EDITOR_OK_CLICKED], 0);
241         tmut_shell_window_back (tmut_shell_child_get_window (TMUT_SHELL_CHILD (user_data)));
242         return;
243 }
244
245 static void
246 tmut_account_editor_on_cancel_clicked (GtkWidget *button, gpointer user_data)
247 {
248         tmut_shell_window_back (tmut_shell_child_get_window (TMUT_SHELL_CHILD (user_data)));
249         return;
250 }
251
252 static void
253 tmut_account_editor_instance_init (GTypeInstance *instance, gpointer g_class)
254 {
255         TMutAccountEditor *self = (TMutAccountEditor *) instance;
256         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
257         GtkWidget *label, *table;
258         GtkWidget *ok_button, *cancel_button;
259         GtkWidget *hbox = gtk_hbox_new (FALSE, 0);
260         GtkWidget *mhbox = gtk_hbox_new (FALSE, 0);
261
262         table = gtk_table_new (6, 2, FALSE);
263
264         gtk_widget_show (hbox);
265         ok_button = gtk_button_new_with_label (_("Ok"));
266         cancel_button = gtk_button_new_with_label (_("Cancel"));
267
268         gtk_widget_show (cancel_button);
269         gtk_widget_show (ok_button);
270         gtk_widget_show (mhbox);
271
272         gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (cancel_button),
273                 TRUE, TRUE, 0);
274         gtk_box_pack_start (GTK_BOX (hbox), GTK_WIDGET (ok_button),
275                 TRUE, TRUE, 0);
276
277         label = gtk_label_new (_("Account name"));
278         gtk_widget_show (label);
279         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
280                 (GtkAttachOptions) (GTK_FILL),
281                 (GtkAttachOptions) (0), 0, 0);
282         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
283
284         label = gtk_label_new (_("Protocol"));
285         gtk_widget_show (label);
286         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
287         (GtkAttachOptions) (GTK_FILL),
288         (GtkAttachOptions) (0), 0, 0);
289         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
290
291         label = gtk_label_new (_("Hostname"));
292         gtk_widget_show (label);
293         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
294                 (GtkAttachOptions) (GTK_FILL),
295                 (GtkAttachOptions) (0), 0, 0);
296         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
297
298         label = gtk_label_new (_("User"));
299         gtk_widget_show (label);
300         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 3, 4,
301         (GtkAttachOptions) (GTK_FILL),
302         (GtkAttachOptions) (0), 0, 0);
303         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
304
305         label = gtk_label_new (_("Type (+ From)"));
306         gtk_widget_show (label);
307         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 4, 5,
308         (GtkAttachOptions) (GTK_FILL),
309         (GtkAttachOptions) (0), 0, 0);
310         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
311
312         label = gtk_label_new (_("Options"));
313         gtk_widget_show (label);
314         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 5, 6,
315         (GtkAttachOptions) (GTK_FILL),
316         (GtkAttachOptions) (0), 0, 0);
317         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
318
319         label = gtk_label_new (_("Port"));
320         gtk_widget_show (label);
321         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 6, 7,
322         (GtkAttachOptions) (GTK_FILL),
323         (GtkAttachOptions) (0), 0, 0);
324         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
325
326         label = gtk_label_new (_("Auth mech"));
327         gtk_widget_show (label);
328         gtk_table_attach (GTK_TABLE (table), label, 0, 1, 7, 8,
329         (GtkAttachOptions) (GTK_FILL),
330         (GtkAttachOptions) (0), 0, 0);
331         gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
332
333         priv->name_entry = gtk_entry_new ();
334         gtk_widget_show (priv->name_entry);
335         gtk_table_attach (GTK_TABLE (table), priv->name_entry, 1, 2, 0, 1,
336                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
337                 (GtkAttachOptions) (0), 0, 0);
338
339         priv->proto_entry = gtk_entry_new ();
340         gtk_widget_show (priv->proto_entry);
341         gtk_table_attach (GTK_TABLE (table), priv->proto_entry, 1, 2, 1, 2,
342                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
343                 (GtkAttachOptions) (0), 0, 0);
344
345         priv->hostname_entry = gtk_entry_new ();
346         gtk_widget_show (priv->hostname_entry);
347         gtk_table_attach (GTK_TABLE (table), priv->hostname_entry, 1, 2, 2, 3,
348                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
349                 (GtkAttachOptions) (0), 0, 0);
350
351         priv->user_entry = gtk_entry_new ();
352         gtk_widget_show (priv->user_entry);
353         gtk_table_attach (GTK_TABLE (table), priv->user_entry, 1, 2, 3, 4,
354                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
355                 (GtkAttachOptions) (0), 0, 0);
356
357         priv->type_entry = gtk_entry_new ();
358         gtk_widget_show (priv->type_entry);
359
360         priv->from_entry = gtk_entry_new ();
361         gtk_widget_show (priv->from_entry);
362
363         gtk_box_pack_start (GTK_BOX (mhbox), GTK_WIDGET (priv->type_entry),
364                 TRUE, TRUE, 0);
365         gtk_box_pack_start (GTK_BOX (mhbox), GTK_WIDGET (priv->from_entry),
366                 TRUE, TRUE, 0);
367
368         gtk_table_attach (GTK_TABLE (table), mhbox, 1, 2, 4, 5,
369                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
370                 (GtkAttachOptions) (0), 0, 0);
371
372
373         priv->options_entry = gtk_entry_new ();
374         gtk_widget_show (priv->options_entry);
375         gtk_table_attach (GTK_TABLE (table), priv->options_entry, 1, 2, 5, 6,
376                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
377                 (GtkAttachOptions) (0), 0, 0);
378
379         priv->port_entry = gtk_entry_new ();
380         gtk_widget_show (priv->port_entry);
381         gtk_table_attach (GTK_TABLE (table), priv->port_entry, 1, 2, 6, 7,
382                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
383                 (GtkAttachOptions) (0), 0, 0);
384
385         priv->auth_mech_entry = gtk_entry_new ();
386         gtk_widget_show (priv->auth_mech_entry);
387         gtk_table_attach (GTK_TABLE (table), priv->auth_mech_entry, 1, 2, 7, 8,
388                 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
389                 (GtkAttachOptions) (0), 0, 0);
390
391         gtk_widget_show (table);
392
393
394         g_signal_connect (G_OBJECT (ok_button), "clicked",
395                 G_CALLBACK (tmut_account_editor_on_ok_clicked), self);
396         g_signal_connect (G_OBJECT (cancel_button), "clicked",
397                 G_CALLBACK (tmut_account_editor_on_cancel_clicked), self);
398
399         gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (table),
400                 TRUE, TRUE, 0);
401         gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (hbox),
402                 TRUE, TRUE, 0);
403
404         return;
405 }
406
407 static void
408 tmut_account_editor_finalize (GObject *object)
409 {
410         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (object);
411
412         if (priv->account)
413                 g_object_unref (priv->account);
414
415         (*parent_class->finalize) (object);
416
417         return;
418 }
419
420
421 static void
422 tmut_account_editor_class_init (TMutAccountEditorClass *class)
423 {
424         GObjectClass *object_class;
425
426         parent_class = g_type_class_peek_parent (class);
427         object_class = (GObjectClass*) class;
428
429         object_class->finalize = tmut_account_editor_finalize;
430
431         g_type_class_add_private (object_class, sizeof (TMutAccountEditorPriv));
432
433         return;
434 }
435
436
437 /**
438  * tmut_account_editor_new:
439  * @account: (null-ok): a #TnyAccount
440  *
441  * Return value: A new #TMutAccountEditor instance
442  **/
443 TMutAccountEditor*
444 tmut_account_editor_new (TnyAccount *account)
445 {
446         TMutAccountEditor *self = g_object_new (TMUT_TYPE_ACCOUNT_EDITOR, NULL);
447         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
448
449         if (account)
450                 priv->account = TNY_ACCOUNT (g_object_ref (account));
451         else
452                 priv->account = NULL;
453
454         load_account (self, priv->account);
455
456         return TMUT_ACCOUNT_EDITOR (self);
457 }
458
459
460 TMutShellWindow*
461 tmut_account_editor_get_window (TMutShellChild *self)
462 {
463         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
464         return priv->shell;
465 }
466
467 void 
468 tmut_account_editor_set_window (TMutShellChild *self, TMutShellWindow *window)
469 {
470         TMutAccountEditorPriv *priv = TMUT_ACCOUNT_EDITOR_GET_PRIVATE (self);
471         priv->shell = window;
472 }
473
474 static void
475 tmut_shell_child_init (gpointer g, gpointer iface_data)
476 {
477         TMutShellChildIface *klass = (TMutShellChildIface *)g;
478
479         klass->get_window= tmut_account_editor_get_window;
480         klass->set_window= tmut_account_editor_set_window;
481
482         return;
483 }
484
485
486
487 static void
488 tmut_account_editor_base_init (gpointer g_class)
489 {
490         static gboolean tmut_account_editor_initialized = FALSE;
491
492         if (!tmut_account_editor_initialized)
493         {
494                 tmut_account_editor_signals[TMUT_ACCOUNT_EDITOR_OK_CLICKED] =
495                    g_signal_new ("ok_clicked",
496                         TMUT_TYPE_ACCOUNT_EDITOR,
497                         G_SIGNAL_RUN_FIRST,
498                         G_STRUCT_OFFSET (TMutAccountEditorClass, ok_clicked),
499                         NULL, NULL,
500                         g_cclosure_marshal_VOID__VOID,
501                         G_TYPE_NONE, 0);
502
503                 tmut_account_editor_initialized = TRUE;
504         }
505 }
506
507
508 GType
509 tmut_account_editor_get_type (void)
510 {
511         static GType type = 0;
512
513         if (G_UNLIKELY(type == 0))
514         {
515                 static const GTypeInfo info =
516                 {
517                   sizeof (TMutAccountEditorClass),
518                   tmut_account_editor_base_init,   /* base_init */
519                   NULL,   /* base_finalize */
520                   (GClassInitFunc) tmut_account_editor_class_init,   /* class_init */
521                   NULL,   /* class_finalize */
522                   NULL,   /* class_data */
523                   sizeof (TMutAccountEditor),
524                   0,      /* n_preallocs */
525                   tmut_account_editor_instance_init    /* instance_init */
526                 };
527
528                 static const GInterfaceInfo tmut_shell_child_info =
529                 {
530                   (GInterfaceInitFunc) tmut_shell_child_init, /* interface_init */
531                   NULL,         /* interface_finalize */
532                   NULL          /* interface_data */
533                 };
534
535                 type = g_type_register_static (GTK_TYPE_VBOX,
536                         "TMutAccountEditor",
537                         &info, 0);
538
539                 g_type_add_interface_static (type, TMUT_TYPE_SHELL_CHILD,
540                         &tmut_shell_child_info);
541
542         }
543
544         return type;
545 }
546
Note: See TracBrowser for help on using the browser.