2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
21
#include <cairo-dock.h>
23
#include "cd-mail-applet-struct.h"
24
#include "cd-mail-applet-config.h"
25
#include "cd-mail-applet-accounts.h"
26
#include "cd-mail-applet-etpan.h"
28
typedef void (*cd_mail_fill_account)(CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name);
29
typedef void (*cd_mail_create_account)( GKeyFile *pKeyFile, const gchar *pMailAccountName );
31
struct storage_type_def {
33
const gchar * description;
34
cd_mail_fill_account pfillFunc;
35
cd_mail_create_account pcreateFunc;
38
static struct storage_type_def storage_tab[] = {
39
{"pop3", "POP3", cd_mail_retrieve_pop3_params, cd_mail_create_pop3_params },
40
{"imap", "IMAP", cd_mail_retrieve_imap_params, cd_mail_create_imap_params },
41
{"mbox", "MBox", cd_mail_retrieve_mbox_params, cd_mail_create_mbox_params },
42
{"mh", "MH", cd_mail_retrieve_mh_params, cd_mail_create_mh_params },
43
{"maildir", "MailDir", cd_mail_retrieve_maildir_params , cd_mail_create_maildir_params},
44
{"gmail", "GMail", cd_mail_retrieve_gmail_params, cd_mail_create_gmail_params }
45
#if ( __WORDSIZE == 64 )
46
/* in 64bit libetpan crashes with RSS, so... avoid it. */
47
#warning "Compilation 64bits: avoiding RSS accounts"
49
,{"feed", "RSS/Feed", cd_mail_retrieve_feed_params, cd_mail_create_feed_params }
53
const guint MAIL_NB_STORAGE_TYPES = sizeof(storage_tab) / sizeof(struct storage_type_def);
55
static void _get_mail_accounts (GKeyFile *pKeyFile, CairoDockModuleInstance *myApplet)
57
//\_______________ On remet a zero les comptes mail.
58
if( myData.pMailAccounts )
61
for (i = 0; i < myData.pMailAccounts->len; i++)
63
CDMailAccount *pMailAccount;
64
pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);
66
if( pMailAccount != NULL && pMailAccount->pAccountMailTimer != NULL )
67
cairo_dock_stop_task (pMailAccount->pAccountMailTimer);
70
cd_mail_free_all_accounts (myApplet);
72
myData.iPrevNbUnreadMails = 0;
73
myData.iNbUnreadMails = 0;
75
//\_______________ On recupere les comptes mail dans le fichier de conf.
76
CDMailAccount *pMailAccount;
77
gchar *cMailAccountName;
80
gboolean bFlushConfFileNeeded = FALSE;
81
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
82
myData.pMailAccounts = g_ptr_array_sized_new (length - 3);
84
cd_debug ("recuperons les comptes ...\n");
85
for (i = 3; i < length; i ++) // Icon, Desklet, Configuration + mail groups
87
cMailAccountName = pGroupList[i];
88
cd_debug ("+ on recupere le compte '%s'\n", cMailAccountName);
90
// Get the type of the account.
91
if (! g_key_file_has_key (pKeyFile, cMailAccountName, "type", NULL))
94
gchar *cMailAccountType = g_key_file_get_string (pKeyFile, cMailAccountName, "type", NULL);
95
if (cMailAccountType == NULL)
98
for( j = 0; j < MAIL_NB_STORAGE_TYPES; j++ )
100
if (g_strcasecmp(storage_tab[j].name, cMailAccountType) == 0)
105
g_free (cMailAccountType);
107
// in case the account type is unknown, just ignore.
108
if( j >= MAIL_NB_STORAGE_TYPES )
110
cd_debug (" mail type : %d\n", j);
112
// create a new mail account.
113
pMailAccount = g_new0 (CDMailAccount, 1);
114
g_ptr_array_add (myData.pMailAccounts, pMailAccount);
116
pMailAccount->name = g_strdup (cMailAccountName);
117
pMailAccount->timeout = CD_CONFIG_GET_INTEGER_WITH_DEFAULT (cMailAccountName, "timeout mn", 10);
118
pMailAccount->pAppletInstance = myApplet;
119
pMailAccount->cMailApp = g_key_file_get_string (pKeyFile, cMailAccountName, "mail application", NULL); // a specific mail application to launch for this account, if any.
120
if (pMailAccount->cMailApp && *pMailAccount->cMailApp == '\0')
122
g_free (pMailAccount->cMailApp);
123
pMailAccount->cMailApp = NULL;
125
(storage_tab[j].pfillFunc)( pMailAccount, pKeyFile, cMailAccountName );
127
g_strfreev (pGroupList);
130
#define _find_image_path(path) \
132
gchar *_found_image = NULL; \
133
if (path != NULL) { \
134
_found_image = cairo_dock_search_image_s_path (path); \
135
if (_found_image == NULL)\
136
_found_image = cairo_dock_search_icon_s_path (path); }\
138
CD_APPLET_GET_CONFIG_BEGIN
139
//\_________________ On recupere toutes les valeurs de notre fichier de conf.
141
path = CD_CONFIG_GET_STRING ("Configuration", "no mail image");
142
myConfig.cNoMailUserImage = _find_image_path (path);
144
path = CD_CONFIG_GET_STRING ("Configuration", "has mail image");
145
myConfig.cHasMailUserImage = _find_image_path (path);
147
myConfig.bPlaySound = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "play sound", TRUE);
148
path = CD_CONFIG_GET_STRING ("Configuration", "new mail sound");
149
myConfig.cNewMailUserSound = (path?cairo_dock_generate_file_path (path):NULL);
152
myConfig.cAnimation = CD_CONFIG_GET_STRING ("Configuration", "animation");
153
myConfig.iAnimationDuration = CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "anim duration", 5);
154
myConfig.cMailApplication = CD_CONFIG_GET_STRING ("Configuration", "mail application");
155
/**myConfig.cMailClass = CD_CONFIG_GET_STRING ("Configuration", "mail class");
156
myConfig.bStealTaskBarIcon = myConfig.cMailApplication && CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "inhibate appli", TRUE);*/
157
myConfig.bShowMessageContent = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "show content", TRUE);
158
myConfig.iNbMaxShown = MIN (30, CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "max shown mails", 10));
159
myConfig.bAlwaysShowMailCount = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "show zero mail", TRUE);
160
//myConfig.bShowMessageContent = FALSE;
162
myConfig.cThemePath = CD_CONFIG_GET_THEME_PATH ("Configuration", "theme", "themes", "Default");
164
myConfig.cRenderer = CD_CONFIG_GET_STRING ("Configuration", "renderer");
166
myConfig.bCheckOnStartup = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "check", TRUE);
168
myConfig.iDialogDuration = 1000 * CD_CONFIG_GET_INTEGER_WITH_DEFAULT ("Configuration", "time_dialog", 5);
170
//\_________________ On recupere les comptes mail.
171
if (myConfig.bCheckOnStartup)
172
_get_mail_accounts (CD_APPLET_MY_KEY_FILE, myApplet);
173
CD_APPLET_GET_CONFIG_END
176
CD_APPLET_RESET_CONFIG_BEGIN
177
g_free( myConfig.cNoMailUserImage );
178
g_free( myConfig.cHasMailUserImage );
179
g_free( myConfig.cNewMailUserSound );
180
g_free( myConfig.cMailApplication );
181
g_free( myConfig.cAnimation );
182
///g_free( myConfig.cMailClass );
183
g_free (myConfig.cThemePath);
184
g_free (myConfig.cRenderer);
186
if( myData.pMessagesDialog != NULL ) // make sure the dialog is closed
188
cairo_dock_dialog_unreference (myData.pMessagesDialog);
189
myData.pMessagesDialog = NULL;
192
myConfig.iNbMaxShown = 0;
193
CD_APPLET_RESET_CONFIG_END
196
CD_APPLET_RESET_DATA_BEGIN
197
cd_mail_free_all_accounts (myApplet);
198
CD_APPLET_DELETE_MY_ICONS_LIST;
200
if (myData.iCubeCallList != 0)
201
glDeleteLists (myData.iCubeCallList, 1);
202
if (myData.iNoMailTexture != 0)
203
glDeleteTextures (1, &myData.iNoMailTexture);
204
if (myData.iHasMailTexture != 0)
205
glDeleteTextures (1, &myData.iHasMailTexture);
207
if (myData.cWorkingDirPath != 0)
208
g_free(myData.cWorkingDirPath);
209
CD_APPLET_RESET_DATA_END
213
static void _cd_mail_add_new_account (GtkComboBox *pMailTypesCombo, GtkEntry *pMailNameEntry, CairoDockModuleInstance *myApplet)
217
//\____________ On recupere le type et le nom du nouveau compte.
218
guint iChosenAccountType = gtk_combo_box_get_active(pMailTypesCombo);
219
if( iChosenAccountType >= MAIL_NB_STORAGE_TYPES )
221
cd_warning ("while trying get chosen account type (%d) : out of range.", iChosenAccountType);
222
cairo_dock_show_temporary_dialog_with_icon (D_("Please choose an account type."), myIcon, myContainer, 3000, "same icon");
226
const gchar *pMailAccountName = gtk_entry_get_text(pMailNameEntry);
227
if( !pMailNameEntry || *pMailAccountName == '\0' || strcmp (pMailAccountName, D_("New account's name")) == 0)
229
cd_warning ("while trying get name of account to create : empty name");
230
cairo_dock_show_temporary_dialog_with_icon (D_("Please enter a name for this account."), myIcon, myContainer, 3000, "same icon");
234
//\____________ On ouvre notre fichier de conf.
235
GKeyFile* pKeyFile = cairo_dock_open_key_file (CD_APPLET_MY_CONF_FILE);
236
g_return_if_fail (pKeyFile != NULL);
238
if (g_key_file_has_group (pKeyFile, pMailAccountName))
240
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");
241
g_key_file_free (pKeyFile);
245
//\____________ On rajoute les champs du nouveau compte mail.
246
(storage_tab[iChosenAccountType].pcreateFunc)( pKeyFile, pMailAccountName );
248
g_key_file_set_string (pKeyFile, pMailAccountName, "remove account", "");
249
g_key_file_set_comment(pKeyFile, pMailAccountName, "remove account", "_0 Remove this account", NULL);
251
cairo_dock_write_keys_to_file (pKeyFile, CD_APPLET_MY_CONF_FILE);
253
//\____________ On recharge le panneau de config.
255
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
256
g_strfreev (pGroupList);
258
CD_APPLET_RELOAD_CONFIG_PANEL_WITH_PAGE (length-1); // on se place sur le dernier onglet, qui est celui du nouveau compte.
260
g_key_file_free (pKeyFile);
262
static void _cd_mail_activate_account (GtkEntry *pEntry, CairoDockModuleInstance *myApplet)
264
GtkComboBox *pMailTypesCombo = GTK_COMBO_BOX(g_object_get_data(G_OBJECT (pEntry), "MailTypesCombo"));
265
_cd_mail_add_new_account (pMailTypesCombo, pEntry, myApplet);
267
static void _cd_mail_add_account (GtkButton *pButton, CairoDockModuleInstance *myApplet)
269
GtkComboBox *pMailTypesCombo = GTK_COMBO_BOX(g_object_get_data(G_OBJECT (pButton), "MailTypesCombo"));
270
GtkEntry *pMailNameEntry = GTK_ENTRY(g_object_get_data(G_OBJECT (pButton), "MailNameEntry"));
271
_cd_mail_add_new_account (pMailTypesCombo, pMailNameEntry, myApplet);
274
static void _cd_mail_remove_account (GtkButton *pButton, CairoDockModuleInstance *myApplet)
277
//\____________ On supprime le groupe correspondant dans le fichier de conf.
278
GKeyFile* pKeyFile = cairo_dock_open_key_file (CD_APPLET_MY_CONF_FILE);
279
g_return_if_fail (pKeyFile != NULL);
281
guint iNumAccount = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pButton), "AccountIndex"));
282
g_return_if_fail (iNumAccount > 2);
284
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
285
g_return_if_fail (iNumAccount < length);
287
gchar *cMailAccount = pGroupList[iNumAccount];
288
g_key_file_remove_group (pKeyFile, cMailAccount, NULL);
290
cairo_dock_write_keys_to_file (pKeyFile, CD_APPLET_MY_CONF_FILE);
292
g_key_file_free (pKeyFile);
294
//\____________ On recharge le panneau de config.
295
CD_APPLET_RELOAD_CONFIG_PANEL;
297
//\____________ On supprime le compte et son icone de la liste.
298
CDMailAccount *pMailAccount;
300
for (i = 0; i < myData.pMailAccounts->len; i ++)
302
pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);
303
if( !pMailAccount ) continue;
305
if (strcmp (cMailAccount, pMailAccount->name) == 0)
307
cd_debug ("mail : found old account");
308
CDMailAccount *pRemovedMailAccount = g_ptr_array_remove_index (myData.pMailAccounts, i); // decale tout de 1 vers la gauche.
309
Icon *pIcon = pRemovedMailAccount->icon;
310
CairoContainer *pContainer = CD_APPLET_MY_ICONS_LIST_CONTAINER;
312
cairo_dock_remove_one_icon_from_dock (CAIRO_DOCK (pContainer), pIcon);
315
CairoDesklet *pDesklet = CAIRO_DESKLET (pContainer);
316
pDesklet->icons = g_list_remove (pDesklet->icons, pIcon);
317
cairo_dock_redraw_container (pContainer);
319
cd_debug ("mail : delete old icon");
320
cairo_dock_free_icon (pIcon);
321
cd_debug ("mail : delete old account");
322
cd_mail_free_account (pRemovedMailAccount);
327
if (myData.pMailAccounts->len <= 1)
329
CD_APPLET_DELETE_MY_ICONS_LIST;
330
if (myData.pMailAccounts->len == 1)
332
pMailAccount = g_ptr_array_index (myData.pMailAccounts, 0);
334
pMailAccount->icon = NULL;
339
g_strfreev (pGroupList);
342
void cd_mail_load_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile* pKeyFile)
345
//\____________ On recupere notre widget personnalise (un simple container vide qu'on va remplir avec nos trucs).
346
CairoDockGroupKeyWidget *pGroupKeyWidget = cairo_dock_get_group_key_widget_from_name ("Configuration", "add account");
347
g_return_if_fail (pGroupKeyWidget != NULL);
349
GtkWidget *pCustomWidgetBox = gtk_hbox_new (FALSE, 3);
350
gtk_box_pack_end (GTK_BOX (pGroupKeyWidget->pKeyBox),
356
//\____________ On cree un combo pour selectionner le type de compte mail qu'on voudrait ajouter
357
GtkWidget *pMailTypesCombo = gtk_combo_box_new_text();
358
if( pMailTypesCombo )
361
for( j = 0; j < MAIL_NB_STORAGE_TYPES; j++ )
363
gtk_combo_box_append_text( GTK_COMBO_BOX (pMailTypesCombo), storage_tab[j].description );
364
//gtk_widget_set_tooltip_text (pMenuItem, D_("description du type de compte"));
367
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
373
//\____________ On cree une entree de texte pour le nom du compte mail et on l'ajoute dans notre container.
374
GtkWidget *pEntry = gtk_entry_new ();
375
//gtk_entry_set_text (GTK_ENTRY (pEntry), D_("New account's name"));
376
gtk_widget_set_tooltip_text (pEntry, D_("Enter a name for this account. You can give it any name you want."));
377
g_object_set_data (G_OBJECT (pEntry), "MailTypesCombo", pMailTypesCombo);
378
g_signal_connect (G_OBJECT (pEntry),
380
G_CALLBACK (_cd_mail_activate_account),
382
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
388
//\____________ On cree un bouton pour ajouter un compte mail et on l'ajoute dans notre container.
389
GtkWidget *pButton = gtk_button_new_from_stock (GTK_STOCK_ADD);
390
g_object_set_data (G_OBJECT (pButton), "MailTypesCombo", pMailTypesCombo); // associer le bouton add avec le combo
391
g_object_set_data (G_OBJECT (pButton), "MailNameEntry", pEntry); // associer le bouton add avec le texte du nom
392
g_signal_connect (G_OBJECT (pButton),
394
G_CALLBACK (_cd_mail_add_account),
396
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
402
//\____________ Pour chaque compte mail, on va creer un bouton "Remove" dans l'onglet correspondant
403
//g_return_if_fail (myData.pMailAccounts != NULL);
404
//CDMailAccount *pMailAccount;
406
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
407
gchar *cMailAccountName;
409
for( i = 3; i < length; i++ )
410
//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.
412
//pMailAccount = g_ptr_array_index (myData.pMailAccounts, i); // i-3
413
//cMailAccountName = pMailAccount->name;
414
cMailAccountName = pGroupList[i];
415
cd_debug ("- on ajoute le bouton remove au compte '%s'\n", cMailAccountName);
416
if (! g_key_file_has_group (pKeyFile, cMailAccountName))
418
cd_warning ("mail : no group for mail account '%s'", cMailAccountName);
422
//\____________ On recupere notre widget personnalise (un simple container vide qu'on va remplir avec nos trucs).
423
CairoDockGroupKeyWidget *pGroupKeyWidget = cairo_dock_get_group_key_widget_from_name (cMailAccountName, "remove account");
424
if( pGroupKeyWidget == NULL )
426
cd_warning ("mail : oups, there is a problem in the conf file");
430
//\____________ On cree un bouton pour supprimer le compte et on l'ajoute dans notre container.
431
pButton = gtk_button_new_with_label (D_("Remove Account"));
432
g_object_set_data (G_OBJECT (pButton), "AccountIndex", GINT_TO_POINTER (i));
433
g_signal_connect (G_OBJECT (pButton),
435
G_CALLBACK (_cd_mail_remove_account),
437
gtk_box_pack_start (GTK_BOX (pGroupKeyWidget->pKeyBox),
443
g_strfreev (pGroupList);
447
void cd_mail_save_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile *pKeyFile)
449
cd_debug ("%s (%s)\n", __func__, myIcon->cName);
450
// 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.