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/>.
20
/******************************************************************************
22
This file is a part of the cairo-dock program,
23
released under the terms of the GNU General Public License.
25
Written by Fabrice Rey (for any bug report, please mail me to fabounet@users.berlios.de)
27
******************************************************************************/
30
#include <cairo-dock.h>
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"
38
struct storage_type_def {
41
cd_mail_fill_account pfillFunc;
42
cd_mail_create_account pcreateFunc;
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"
56
{"feed", "RSS/Feed", cd_mail_retrieve_feed_params, cd_mail_create_feed_params },
60
const int MAIL_NB_STORAGE_TYPES = sizeof(storage_tab) / sizeof(struct storage_type_def);
62
static void _get_mail_accounts (GKeyFile *pKeyFile, CairoDockModuleInstance *myApplet)
64
//\_______________ On remet a zero les comptes mail.
65
cd_mail_free_all_accounts (myApplet);
67
myData.iPrevNbUnreadMails = 0;
68
myData.iNbUnreadMails = 0;
70
//\_______________ On recupere les comptes mail dans le fichier de conf.
71
CDMailAccount *pMailAccount;
72
gchar *cMailAccountName;
75
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
76
myData.pMailAccounts = g_ptr_array_sized_new (length - 3);
78
g_print ("recuperons les comptes ...\n");
79
for (i = 3; i < length; i ++) // Icon, Desklet, Configuration + mail groups
81
cMailAccountName = pGroupList[i];
82
g_print ("+ on recupere le compte '%s'\n", cMailAccountName);
84
/* Get the type of the account */
85
if (! g_key_file_has_key (pKeyFile, cMailAccountName, "type", NULL))
88
gchar *cMailAccountType = g_key_file_get_string (pKeyFile, cMailAccountName, "type", NULL);
90
for( j = 0; j < MAIL_NB_STORAGE_TYPES; j++ )
92
if (g_strcasecmp(storage_tab[j].name, cMailAccountType) == 0)
98
/* in case the account type is unknown, just ignore... */
99
if( j >= MAIL_NB_STORAGE_TYPES )
101
g_print (" mail type : %d\n", j);
103
pMailAccount = g_new0 (CDMailAccount, 1);
104
g_ptr_array_add (myData.pMailAccounts, pMailAccount);
106
pMailAccount->name = g_strdup (cMailAccountName);
108
pMailAccount->pAppletInstance = myApplet;
109
(storage_tab[account_type].pfillFunc)( pMailAccount, pKeyFile, cMailAccountName );
111
g_strfreev (pGroupList);
114
CD_APPLET_GET_CONFIG_BEGIN
115
//\_________________ On recupere toutes les valeurs de notre fichier de conf.
117
path = CD_CONFIG_GET_STRING ("Configuration", "no mail image");
118
myConfig.cNoMailUserImage = (path?cairo_dock_generate_file_path (path):NULL);
120
path = CD_CONFIG_GET_STRING ("Configuration", "has mail image");
121
myConfig.cHasMailUserImage = (path?cairo_dock_generate_file_path (path):NULL);
123
path = CD_CONFIG_GET_STRING ("Configuration", "new mail sound");
124
myConfig.cNewMailUserSound = (path?cairo_dock_generate_file_path (path):NULL);
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;
133
myConfig.cThemePath = CD_CONFIG_GET_THEME_PATH ("Configuration", "theme", "themes", "Default");
135
myConfig.cRenderer = CD_CONFIG_GET_STRING ("Configuration", "renderer");
137
myConfig.bCheckOnStartup = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT ("Configuration", "check", TRUE);
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
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
156
CD_APPLET_RESET_DATA_BEGIN
157
cd_mail_free_all_accounts (myApplet);
158
CD_APPLET_DELETE_MY_ICONS_LIST;
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
170
static void _cd_mail_add_new_account (GtkComboBox *pMailTypesCombo, GtkEntry *pMailNameEntry, CairoDockModuleInstance *myApplet)
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 )
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");
183
const gchar *pMailAccountName = gtk_entry_get_text(pMailNameEntry);
184
if( !pMailNameEntry || *pMailAccountName == '\0' || strcmp (pMailAccountName, D_("New account's name")) == 0)
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");
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);
195
if (g_key_file_has_group (pKeyFile, pMailAccountName))
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);
202
//\____________ On rajoute les champs du nouveau compte mail.
203
(storage_tab[iChosenAccountType].pcreateFunc)( pKeyFile, pMailAccountName );
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);
208
cairo_dock_write_keys_to_file (pKeyFile, CD_APPLET_MY_CONF_FILE);
210
//\____________ On recharge le panneau de config.
212
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
213
g_strfreev (pGroupList);
215
cairo_dock_reload_current_group_widget_full (myApplet, length-1); // on se place sur le dernier onglet, qui est celui du nouveau compte.
217
g_key_file_free (pKeyFile);
219
static void _cd_mail_activate_account (GtkEntry *pEntry, CairoDockModuleInstance *myApplet)
221
GtkComboBox *pMailTypesCombo = GTK_COMBO_BOX(g_object_get_data(G_OBJECT (pEntry), "MailTypesCombo"));
222
_cd_mail_add_new_account (pMailTypesCombo, pEntry, myApplet);
224
static void _cd_mail_add_account (GtkButton *pButton, CairoDockModuleInstance *myApplet)
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);
231
static void _cd_mail_remove_account (GtkButton *pButton, CairoDockModuleInstance *myApplet)
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);
238
guint iNumAccount = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (pButton), "AccountIndex"));
239
g_return_if_fail (iNumAccount > 2);
241
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
242
g_return_if_fail (iNumAccount < length);
244
gchar *cMailAccount = pGroupList[iNumAccount];
245
g_key_file_remove_group (pKeyFile, cMailAccount, NULL);
247
cairo_dock_write_keys_to_file (pKeyFile, CD_APPLET_MY_CONF_FILE);
249
g_key_file_free (pKeyFile);
251
//\____________ On recharge le panneau de config.
252
cairo_dock_reload_current_group_widget (myApplet);
254
//\____________ On supprime le compte et son icone de la liste.
255
CDMailAccount *pMailAccount;
257
for (i = 0; i < myData.pMailAccounts->len; i ++)
259
pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);
260
if( !pMailAccount ) continue;
262
if (strcmp (cMailAccount, pMailAccount->name) == 0)
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;
269
cairo_dock_remove_one_icon_from_dock (CAIRO_DOCK (pContainer), pIcon);
272
CairoDesklet *pDesklet = CAIRO_DESKLET (pContainer);
273
pDesklet->icons = g_list_remove (pDesklet->icons, pIcon);
274
cairo_dock_redraw_container (pContainer);
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);
284
if (myData.pMailAccounts->len <= 1)
286
CD_APPLET_DELETE_MY_ICONS_LIST;
287
if (myData.pMailAccounts->len == 1)
289
pMailAccount = g_ptr_array_index (myData.pMailAccounts, 0);
291
pMailAccount->icon = NULL;
296
g_strfreev (pGroupList);
299
void cd_mail_load_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile* pKeyFile)
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);
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 )
310
for( int j = 0; j < MAIL_NB_STORAGE_TYPES; j++ )
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"));
316
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
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),
329
G_CALLBACK (_cd_mail_activate_account),
331
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
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),
343
G_CALLBACK (_cd_mail_add_account),
345
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
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;
355
gchar **pGroupList = g_key_file_get_groups (pKeyFile, &length);
356
gchar *cMailAccountName;
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.
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))
367
cd_warning ("mail : no group for mail account '%s'", cMailAccountName);
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 )
375
cd_warning ("mail : oups, there is a problem in the conf file");
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),
384
G_CALLBACK (_cd_mail_remove_account),
386
gtk_box_pack_start (GTK_BOX (pCustomWidgetBox),
392
g_strfreev (pGroupList);
396
void cd_mail_save_custom_widget (CairoDockModuleInstance *myApplet, GKeyFile *pKeyFile)
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.