~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to mail/src/cd-mail-applet-config.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
ImportĀ upstreamĀ versionĀ 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program,
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
 
26
 
 
27
******************************************************************************/
 
28
 
 
29
#include <string.h>
 
30
#include <cairo-dock.h>
 
31
 
 
32
#include "cd-mail-applet-struct.h"
 
33
#include "cd-mail-applet-config.h"
 
34
#include "cd-mail-applet-accounts.h"
 
35
#include "cd-mail-applet-etpan.h"
 
36
 
 
37
 
 
38
struct storage_type_def {
 
39
  char * name;
 
40
  char * description;
 
41
  cd_mail_fill_account pfillFunc;
 
42
  cd_mail_create_account pcreateFunc;
 
43
};
 
44
 
 
45
static struct storage_type_def storage_tab[] = {
 
46
  {"pop3", "POP3", cd_mail_retrieve_pop3_params, cd_mail_create_pop3_params },
 
47
  {"imap", "IMAP", cd_mail_retrieve_imap_params, cd_mail_create_imap_params },
 
48
  {"mbox", "MBox", cd_mail_retrieve_mbox_params, cd_mail_create_mbox_params },
 
49
  {"mh", "MH", cd_mail_retrieve_mh_params, cd_mail_create_mh_params },
 
50
  {"maildir", "MailDir", cd_mail_retrieve_maildir_params , cd_mail_create_maildir_params},
 
51
  {"gmail", "GMail", cd_mail_retrieve_gmail_params, cd_mail_create_gmail_params },
 
52
#if ( __WORDSIZE == 64 )
 
53
/* in 64bit libetpan crashes with RSS, so... avoid it. */
 
54
#warning "Compilation 64bits: avoiding RSS accounts"
 
55
#else
 
56
  {"feed", "RSS/Feed", cd_mail_retrieve_feed_params, cd_mail_create_feed_params },
 
57
#endif
 
58
};
 
59
 
 
60
const int MAIL_NB_STORAGE_TYPES = sizeof(storage_tab) / sizeof(struct storage_type_def);
 
61
 
 
62
static void _get_mail_accounts (GKeyFile *pKeyFile, CairoDockModuleInstance *myApplet)
 
63
{
 
64
        //\_______________ On remet a zero les comptes mail.
 
65
        cd_mail_free_all_accounts (myApplet);
 
66
        
 
67
        myData.iPrevNbUnreadMails = 0;
 
68
        myData.iNbUnreadMails = 0;
 
69
        
 
70
        //\_______________ On recupere les comptes mail dans le fichier de conf.
 
71
        CDMailAccount *pMailAccount;
 
72
        gchar *cMailAccountName;
 
73
        int j, account_type;
 
74
        gsize i, length = 0;
 
75
        gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
 
76
        myData.pMailAccounts = g_ptr_array_sized_new (length - 3);
 
77
        
 
78
        g_print ("recuperons les comptes ...\n");
 
79
        for (i = 3; i < length; i ++)  // Icon, Desklet, Configuration + mail groups
 
80
        {
 
81
                cMailAccountName = pGroupList[i];
 
82
                g_print ("+ on recupere le compte '%s'\n", cMailAccountName);
 
83
                
 
84
                /* Get the type of the account */
 
85
                if (! g_key_file_has_key (pKeyFile, cMailAccountName, "type", NULL))
 
86
                        continue ;
 
87
                
 
88
                gchar *cMailAccountType = g_key_file_get_string (pKeyFile, cMailAccountName, "type", NULL);
 
89
 
 
90
                for( j = 0; j < MAIL_NB_STORAGE_TYPES; j++ )
 
91
                {
 
92
                        if (g_strcasecmp(storage_tab[j].name, cMailAccountType) == 0)
 
93
                        {
 
94
                                account_type = j;
 
95
                                break;
 
96
                        }
 
97
                }
 
98
                /* in case the account type is unknown, just ignore... */
 
99
                if( j >= MAIL_NB_STORAGE_TYPES )
 
100
                        continue;
 
101
                g_print ("  mail type : %d\n", j);
 
102
                
 
103
                pMailAccount = g_new0 (CDMailAccount, 1);
 
104
                g_ptr_array_add (myData.pMailAccounts, pMailAccount);
 
105
 
 
106
                pMailAccount->name = g_strdup (cMailAccountName);
 
107
                
 
108
                pMailAccount->pAppletInstance = myApplet;
 
109
                (storage_tab[account_type].pfillFunc)( pMailAccount, pKeyFile, cMailAccountName );
 
110
        }
 
111
        g_strfreev (pGroupList);
 
112
}
 
113
 
 
114
CD_APPLET_GET_CONFIG_BEGIN
 
115
        //\_________________ On recupere toutes les valeurs de notre fichier de conf.
 
116
        gchar *path;
 
117
        path = CD_CONFIG_GET_STRING ("Configuration", "no mail image");
 
118
        myConfig.cNoMailUserImage = (path?cairo_dock_generate_file_path (path):NULL);
 
119
        g_free (path);
 
120
        path = CD_CONFIG_GET_STRING ("Configuration", "has mail image");
 
121
        myConfig.cHasMailUserImage = (path?cairo_dock_generate_file_path (path):NULL);
 
122
        g_free (path);
 
123
        path = CD_CONFIG_GET_STRING ("Configuration", "new mail sound");
 
124
        myConfig.cNewMailUserSound = (path?cairo_dock_generate_file_path (path):NULL);
 
125
        g_free (path);
 
126
 
 
127
        myConfig.cMailApplication = CD_CONFIG_GET_STRING ("Configuration", "mail application");
 
128
        myConfig.cMailClass = CD_CONFIG_GET_STRING ("Configuration", "mail class");
 
129
        myConfig.bStealTaskBarIcon = myConfig.cMailApplication && CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "inhibate appli", TRUE);
 
130
        myConfig.bShowMessageContent = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "show content", TRUE);
 
131
        myConfig.bShowMessageContent = FALSE;
 
132
        
 
133
        myConfig.cThemePath = CD_CONFIG_GET_THEME_PATH ("Configuration", "theme", "themes", "Default");
 
134
        
 
135
        myConfig.cRenderer = CD_CONFIG_GET_STRING ("Configuration", "renderer");
 
136
        
 
137
        myConfig.bCheckOnStartup = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "check", TRUE);
 
138
        
 
139
        //\_________________ On recupere les comptes mail.
 
140
        if (myConfig.bCheckOnStartup)
 
141
                _get_mail_accounts (CD_APPLET_MY_KEY_FILE, myApplet);
 
142
CD_APPLET_GET_CONFIG_END
 
143
 
 
144
 
 
145
CD_APPLET_RESET_CONFIG_BEGIN
 
146
        g_free( myConfig.cNoMailUserImage );
 
147
        g_free( myConfig.cHasMailUserImage );
 
148
        g_free( myConfig.cNewMailUserSound );
 
149
        g_free( myConfig.cMailApplication );
 
150
        g_free( myConfig.cMailClass );
 
151
        g_free (myConfig.cThemePath);
 
152
        g_free (myConfig.cRenderer);
 
153
CD_APPLET_RESET_CONFIG_END
 
154
 
 
155
 
 
156
CD_APPLET_RESET_DATA_BEGIN
 
157
        cd_mail_free_all_accounts (myApplet);
 
158
        CD_APPLET_DELETE_MY_ICONS_LIST;
 
159
 
 
160
        if (myData.iCubeCallList != 0)
 
161
                glDeleteLists (myData.iCubeCallList, 1);
 
162
        if (myData.iNoMailTexture != 0)
 
163
                glDeleteTextures (1, &myData.iNoMailTexture);
 
164
        if (myData.iHasMailTexture != 0)
 
165
                glDeleteTextures (1, &myData.iHasMailTexture);
 
166
CD_APPLET_RESET_DATA_END
 
167
 
 
168
 
 
169
 
 
170
static void _cd_mail_add_new_account (GtkComboBox *pMailTypesCombo, GtkEntry *pMailNameEntry, CairoDockModuleInstance *myApplet)
 
171
{
 
172
        cd_debug ("");
 
173
        
 
174
        //\____________ On recupere le type et le nom du nouveau compte.
 
175
        gint iChosenAccountType = gtk_combo_box_get_active(pMailTypesCombo);
 
176
        if( iChosenAccountType < 0 || iChosenAccountType >= MAIL_NB_STORAGE_TYPES )
 
177
        {
 
178
                cd_warning ("while trying get chosen account type (%d) : out of range.", iChosenAccountType);
 
179
                cairo_dock_show_temporary_dialog_with_icon (D_("Please choose an account type."), myIcon, myContainer, 3000, "same icon");
 
180
                return ;
 
181
        }
 
182
        
 
183
        const gchar *pMailAccountName = gtk_entry_get_text(pMailNameEntry);
 
184
        if( !pMailNameEntry || *pMailAccountName == '\0' || strcmp (pMailAccountName, D_("New account's name")) == 0)
 
185
        {
 
186
                cd_warning ("while trying get name of account to create : empty name");
 
187
                cairo_dock_show_temporary_dialog_with_icon (D_("Please enter a name for this account."), myIcon, myContainer, 3000, "same icon");
 
188
                return ;
 
189
        }
 
190
        
 
191
        //\____________ On ouvre notre fichier de conf.
 
192
        GKeyFile* pKeyFile = cairo_dock_open_key_file (CD_APPLET_MY_CONF_FILE);
 
193
        g_return_if_fail (pKeyFile != NULL);
 
194
        
 
195
        if (g_key_file_has_group (pKeyFile, pMailAccountName))
 
196
        {
 
197
                cairo_dock_show_temporary_dialog_with_icon (D_("This account already exists.\nPlease choose another name for the new account."), myIcon, myContainer, 5000, "same icon");
 
198
                g_key_file_free (pKeyFile);
 
199
                return ;
 
200
        }
 
201
        
 
202
        //\____________ On rajoute les champs du nouveau compte mail.
 
203
        (storage_tab[iChosenAccountType].pcreateFunc)( pKeyFile, pMailAccountName );
 
204
 
 
205
        g_key_file_set_string (pKeyFile, pMailAccountName, "remove account", "");
 
206
        g_key_file_set_comment(pKeyFile, pMailAccountName, "remove account", "_0 Remove this account", NULL);
 
207
        
 
208
        cairo_dock_write_keys_to_file (pKeyFile, CD_APPLET_MY_CONF_FILE);
 
209
        
 
210
        //\____________ On recharge le panneau de config.
 
211
        gsize length = 0;
 
212
        gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
 
213
        g_strfreev (pGroupList);
 
214
        
 
215
        cairo_dock_reload_current_group_widget_full (myApplet, length-1);  // on se place sur le dernier onglet, qui est celui du nouveau compte.
 
216
        
 
217
        g_key_file_free (pKeyFile);
 
218
}
 
219
static void _cd_mail_activate_account (GtkEntry *pEntry, CairoDockModuleInstance *myApplet)
 
220
{
 
221
        GtkComboBox *pMailTypesCombo = GTK_COMBO_BOX(g_object_get_data(G_OBJECT (pEntry), "MailTypesCombo"));
 
222
        _cd_mail_add_new_account (pMailTypesCombo, pEntry, myApplet);
 
223
}
 
224
static void _cd_mail_add_account (GtkButton *pButton, CairoDockModuleInstance *myApplet)
 
225
{
 
226
        GtkComboBox *pMailTypesCombo = GTK_COMBO_BOX(g_object_get_data(G_OBJECT (pButton), "MailTypesCombo"));
 
227
        GtkEntry *pMailNameEntry = GTK_ENTRY(g_object_get_data(G_OBJECT (pButton), "MailNameEntry"));
 
228
        _cd_mail_add_new_account (pMailTypesCombo, pMailNameEntry, myApplet);
 
229
}
 
230
 
 
231
static void _cd_mail_remove_account (GtkButton *pButton, CairoDockModuleInstance *myApplet)
 
232
{
 
233
        cd_debug ("");
 
234
        //\____________ On supprime le groupe correspondant dans le fichier de conf.
 
235
        GKeyFile* pKeyFile = cairo_dock_open_key_file (CD_APPLET_MY_CONF_FILE);
 
236
        g_return_if_fail (pKeyFile != NULL);
 
237
        
 
238
        guint iNumAccount = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pButton), "AccountIndex"));
 
239
        g_return_if_fail (iNumAccount > 2);
 
240
        gsize length = 0;
 
241
        gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
 
242
        g_return_if_fail (iNumAccount < length);
 
243
        
 
244
        gchar *cMailAccount = pGroupList[iNumAccount];
 
245
        g_key_file_remove_group (pKeyFile, cMailAccount, NULL);
 
246
        
 
247
        cairo_dock_write_keys_to_file (pKeyFile, CD_APPLET_MY_CONF_FILE);
 
248
        
 
249
        g_key_file_free (pKeyFile);
 
250
        
 
251
        //\____________ On recharge le panneau de config.
 
252
        cairo_dock_reload_current_group_widget (myApplet);
 
253
        
 
254
        //\____________ On supprime le compte et son icone de la liste.
 
255
        CDMailAccount *pMailAccount;
 
256
        guint i;
 
257
        for (i = 0; i < myData.pMailAccounts->len; i ++)
 
258
        {
 
259
                pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);
 
260
                if( !pMailAccount ) continue;
 
261
                
 
262
                if (strcmp (cMailAccount, pMailAccount->name) == 0)
 
263
                {
 
264
                        cd_debug ("mail : found old account");
 
265
                        CDMailAccount *pRemovedMailAccount = g_ptr_array_remove_index (myData.pMailAccounts, i); // decale tout de 1 vers la gauche.
 
266
                        Icon *pIcon = pRemovedMailAccount->icon;
 
267
                        CairoContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
 
268
                        if (myDock)
 
269
                                cairo_dock_remove_one_icon_from_dock (CAIRO_DOCK (pContainer), pIcon);
 
270
                        else
 
271
                        {
 
272
                                CairoDesklet *pDesklet = CAIRO_DESKLET (pContainer);
 
273
                                pDesklet->icons = g_list_remove (pDesklet->icons, pIcon);
 
274
                                cairo_dock_redraw_container (pContainer);
 
275
                        }
 
276
                        cd_debug ("mail : delete old icon");
 
277
                        cairo_dock_free_icon (pIcon);
 
278
                        cd_debug ("mail : delete old account");
 
279
                        cd_mail_free_account (pRemovedMailAccount);
 
280
                        break ;
 
281
                }
 
282
        }
 
283
        
 
284
        if (myData.pMailAccounts->len <= 1)
 
285
        {
 
286
                CD_APPLET_DELETE_MY_ICONS_LIST;
 
287
                if (myData.pMailAccounts->len == 1)
 
288
                {
 
289
                        pMailAccount = g_ptr_array_index (myData.pMailAccounts, 0);
 
290
                        if (pMailAccount)
 
291
                                pMailAccount->icon = NULL;
 
292
                }
 
293
        }
 
294
        
 
295
        
 
296
        g_strfreev (pGroupList);
 
297
}
 
298
 
 
299
void cd_mail_load_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile* pKeyFile)
 
300
{
 
301
        cd_debug ("");
 
302
        //\____________ On recupere notre widget personnalise (un simple container vide qu'on va remplir avec nos trucs).
 
303
        GtkWidget *pCustomWidgetBox = cairo_dock_get_widget_from_name ("Configuration", "add account");
 
304
        g_return_if_fail (pCustomWidgetBox != NULL);
 
305
        
 
306
        //\____________ On cree un combo pour selectionner le type de compte mail qu'on voudrait ajouter
 
307
        GtkWidget *pMailTypesCombo = gtk_combo_box_new_text();
 
308
        if( pMailTypesCombo )
 
309
        {
 
310
        for( int j = 0; j < MAIL_NB_STORAGE_TYPES; j++ )
 
311
        {
 
312
          gtk_combo_box_append_text( GTK_COMBO_BOX (pMailTypesCombo), storage_tab[j].description );
 
313
          //gtk_widget_set_tooltip_text (pMenuItem, D_("description du type de compte"));
 
314
        }
 
315
        }
 
316
        gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
 
317
                pMailTypesCombo,
 
318
                FALSE,
 
319
                FALSE,
 
320
                0);
 
321
        
 
322
        //\____________ On cree une entree de texte pour le nom du compte mail et on l'ajoute dans notre container.
 
323
        GtkWidget *pEntry = gtk_entry_new ();
 
324
        //gtk_entry_set_text (GTK_ENTRY (pEntry), D_("New account's name"));
 
325
        gtk_widget_set_tooltip_text (pEntry, D_("Enter a name for this account. You can give it any name you want."));
 
326
        g_object_set_data (G_OBJECT (pEntry), "MailTypesCombo", pMailTypesCombo);
 
327
        g_signal_connect (G_OBJECT (pEntry),
 
328
                "activate",
 
329
                G_CALLBACK (_cd_mail_activate_account),
 
330
                myApplet);
 
331
        gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
 
332
                pEntry,
 
333
                FALSE,
 
334
                FALSE,
 
335
                0);
 
336
 
 
337
        //\____________ On cree un bouton pour ajouter un compte mail et on l'ajoute dans notre container.
 
338
        GtkWidget *pButton = gtk_button_new_from_stock (GTK_STOCK_ADD);
 
339
    g_object_set_data (G_OBJECT (pButton), "MailTypesCombo", pMailTypesCombo); // associer le bouton add avec le combo
 
340
    g_object_set_data (G_OBJECT (pButton), "MailNameEntry", pEntry); // associer le bouton add avec le texte du nom
 
341
        g_signal_connect (G_OBJECT (pButton),
 
342
                "clicked",
 
343
                G_CALLBACK (_cd_mail_add_account),
 
344
                myApplet);
 
345
        gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
 
346
                pButton,
 
347
                FALSE,
 
348
                FALSE,
 
349
                0);
 
350
 
 
351
        //\____________ Pour chaque compte mail, on va creer un bouton "Remove" dans l'onglet correspondant
 
352
        //g_return_if_fail (myData.pMailAccounts != NULL);
 
353
        //CDMailAccount *pMailAccount;
 
354
        gsize length = 0;
 
355
        gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
 
356
        gchar *cMailAccountName;
 
357
        guint i;
 
358
        for( i = 3; i < length; i++ )
 
359
        //for( i = 0; i < myData.pMailAccounts->len; i++ )  // on utilise myData.pMailAccounts plutot que repartir de la liste des groupes, car on veut passer le nom en entree de la callback, il faut donc qu'il soit persistent, donc c'est plus simple comme ca.
 
360
        {
 
361
                //pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);  // i-3
 
362
                //cMailAccountName = pMailAccount->name;
 
363
                cMailAccountName = pGroupList[i];
 
364
                g_print ("- on ajoute le bouton remove au compte '%s'\n", cMailAccountName);
 
365
                if (! g_key_file_has_group (pKeyFile, cMailAccountName))
 
366
                {
 
367
                        cd_warning ("mail : no group for mail account '%s'", cMailAccountName);
 
368
                        continue;
 
369
                }
 
370
                
 
371
                //\____________ On recupere notre widget personnalise (un simple container vide qu'on va remplir avec nos trucs).
 
372
                GtkWidget *pCustomWidgetBox = cairo_dock_get_widget_from_name (cMailAccountName, "remove account");
 
373
                if( pCustomWidgetBox == NULL )
 
374
                {
 
375
                        cd_warning ("mail : oups, there is a problem in the conf file");
 
376
                        continue;
 
377
                }
 
378
                
 
379
                //\____________ On cree un bouton pour supprimer le compte et on l'ajoute dans notre container.
 
380
                pButton = gtk_button_new_with_label (D_("Remove Account"));
 
381
                g_object_set_data (G_OBJECT (pButton), "AccountIndex", GINT_TO_POINTER (i));
 
382
                g_signal_connect (G_OBJECT (pButton),
 
383
                        "clicked",
 
384
                        G_CALLBACK (_cd_mail_remove_account),
 
385
                        myApplet);
 
386
                gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
 
387
                        pButton,
 
388
                        FALSE,
 
389
                        FALSE,
 
390
                        0);
 
391
        }
 
392
        g_strfreev (pGroupList);
 
393
}
 
394
 
 
395
 
 
396
void cd_mail_save_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile *pKeyFile)
 
397
{
 
398
        g_print ("%s (%s)\n", __func__, myIcon->acName);
 
399
        // ca c'est si on avait des valeurs a recuperer dans nos widgets personnalises, et a stocker dans le pKeyFile. mais ici ils sont simple, et donc tous pris en charge par le dock.
 
400
}