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 #include <string.h>
21 #include <cairo-dock.h>
22 #include <libetpan/libetpan.h>
23 
24 #include "cd-mail-applet-struct.h"
25 #include "cd-mail-applet-etpan.h"
26 #include "cd-mail-applet-accounts.h"
27 
28 #define _add_icon(pMailAccount)\
29 	pIcon = cairo_dock_create_dummy_launcher (g_strdup (pMailAccount->name),\
30 		pMailAccount->cIconName ? g_strdup (pMailAccount->cIconName) : g_strdup (myConfig.cNoMailUserImage),\
31 		g_strdup (pMailAccount->cMailApp),\
32 		g_strdup ("..."),\
33 		i);\
34 	pIcon->cParentDockName = g_strdup (myIcon->cName);\
35 	pIconList = g_list_append (pIconList, pIcon);\
36 	pMailAccount->icon = pIcon;
37 
38 // Translation Hack:
39 const char *strings_to_translate[20] = {N_("Server address:"), N_("myHost"), N_("Username:"), N_("Password:"), N_("The password will be crypted."), N_("Port:"), N_("Enter 0 to use the default port. Default ports are 110 for POP3 or APOP and 995 for POP3S."), N_("Enter 0 to use the default port. Default ports are 143 for IMAP4 and 993 for IMAP4 over SSL."), N_("Use a secure connection (SSL)"), N_("Refresh time:"), N_("In minutes."), N_("Specific mail application"), N_("Leave empty to use the default mail application."), N_("Specific icon file when using multiple accounts"), N_("Directory on server:"), N_("Path of mbox file:"), N_("Path to Mail directory:"), N_("Address of feed:"), N_("Remove this account"), N_("Don't forget to enable IMAP (or POP) service from settings of your mail account.")};
40 
41 // Default parameters (to not copy these parameters each time)
_add_default_create_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)42 void _add_default_create_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
43 {
44 	g_key_file_set_string (pKeyFile, pMailAccountName, "username", pMailAccountName); // most of the time, the name of the account is the username
45 	g_key_file_set_comment (pKeyFile, pMailAccountName, "username", "s0 Username:\n{Don't forget to enable IMAP (or POP) service from settings of your mail account.}", NULL);
46 
47 	g_key_file_set_string (pKeyFile, pMailAccountName, "password", "");
48 	g_key_file_set_comment (pKeyFile, pMailAccountName, "password", "p0 Password:", NULL);
49 
50 	g_key_file_set_integer (pKeyFile, pMailAccountName, "timeout mn", 10);
51 	g_key_file_set_comment (pKeyFile, pMailAccountName, "timeout mn", "I0[1;30] Refresh time:\n{In minutes.}", NULL);
52 
53 	g_key_file_set_string (pKeyFile, pMailAccountName, "mail application", "");
54 	g_key_file_set_comment (pKeyFile, pMailAccountName, "mail application", "s0 Specific mail application\n{Leave empty to use the default mail application.}", NULL);
55 
56 	g_key_file_set_string (pKeyFile, pMailAccountName, "icon name", "");
57 	g_key_file_set_comment (pKeyFile, pMailAccountName, "icon name", "g0 Specific icon file when using multiple accounts\n{Leave empty to use the default icon.}", NULL);
58 }
59 
_retrieve_user_password(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)60 void _retrieve_user_password (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
61 {
62 	gboolean bFlushConfFileNeeded = FALSE;
63 
64 	if (g_key_file_has_key (pKeyFile, mailbox_name, "username", NULL))
65 	{
66 		mailaccount->user = CD_CONFIG_GET_STRING (mailbox_name, "username");
67 	}
68 	if (g_key_file_has_key (pKeyFile, mailbox_name, "password", NULL))
69 	{
70 		gchar *encryptedPassword = CD_CONFIG_GET_STRING (mailbox_name, "password");
71 		cairo_dock_decrypt_string( encryptedPassword,  &(mailaccount->password) );
72 
73 		g_free (encryptedPassword);
74 	}
75 }
76 
cd_mail_create_pop3_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)77 void cd_mail_create_pop3_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
78 {
79 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "pop3");
80 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);  // on lui met un widget pour ne pas que la cle se fasse bazarder lors d'une mise a jour du fichier de conf.
81 
82 	g_key_file_set_string (pKeyFile, pMailAccountName, "host", "pop3.myHost.org");
83 	g_key_file_set_comment (pKeyFile, pMailAccountName, "host", "s0 Server address:", NULL);
84 
85 	_add_default_create_params( pKeyFile, pMailAccountName );
86 
87 	g_key_file_set_integer (pKeyFile, pMailAccountName, "port", 0);
88 	g_key_file_set_comment (pKeyFile, pMailAccountName, "port", "i0 Port:\n{Enter 0 to use the default port. Default ports are 110 for POP3 or APOP and 995 for POP3S.}", NULL);
89 
90 	g_key_file_set_boolean (pKeyFile, pMailAccountName, "use secure connection", FALSE);
91 	g_key_file_set_comment (pKeyFile, pMailAccountName, "use secure connection", "b0 Use a secure connection (SSL)", NULL);
92 }
93 
cd_mail_retrieve_pop3_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)94 void cd_mail_retrieve_pop3_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
95 {
96 	if( !mailaccount || !pKeyFile || !mailbox_name ) return;
97 
98 	gboolean bFlushConfFileNeeded = FALSE;
99 
100 	mailaccount->driver = POP3_STORAGE;
101 	mailaccount->storage = mailstorage_new(NULL);
102 	mailaccount->auth_type = POP3_AUTH_TYPE_TRY_APOP;
103 
104 	if (g_key_file_has_key (pKeyFile, mailbox_name, "host", NULL))
105 	{
106 		mailaccount->server = CD_CONFIG_GET_STRING (mailbox_name, "host");
107 	}
108 
109 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
110 
111 	mailaccount->connection_type = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT (
112 		mailbox_name, "use secure connection", FALSE)
113 		? CONNECTION_TYPE_TLS
114 		: CONNECTION_TYPE_PLAIN;
115 	mailaccount->port = CD_CONFIG_GET_INTEGER_WITH_DEFAULT (mailbox_name, "port", 0);
116 }
117 
cd_mail_create_imap_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)118 void cd_mail_create_imap_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
119 {
120 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "imap");
121 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
122 
123 	g_key_file_set_string (pKeyFile, pMailAccountName, "host", "imap.myHost.org");
124 	g_key_file_set_comment (pKeyFile, pMailAccountName, "host", "s0 Server address:", NULL);
125 
126 	_add_default_create_params( pKeyFile, pMailAccountName );
127 
128 	g_key_file_set_integer (pKeyFile, pMailAccountName, "port", 0);
129 	g_key_file_set_comment (pKeyFile, pMailAccountName, "port", "i0 Port:\n{Enter 0 to use the default port. Default ports are 143 for IMAP4 and 993 for IMAP4 over SSL.}", NULL);
130 
131 	g_key_file_set_boolean (pKeyFile, pMailAccountName, "use secure connection", FALSE);
132 	g_key_file_set_comment (pKeyFile, pMailAccountName, "use secure connection", "b0 Use a secure connection (SSL)", NULL);
133 
134 	g_key_file_set_string (pKeyFile, pMailAccountName, "server_directory", "Inbox");
135 	g_key_file_set_comment (pKeyFile, pMailAccountName, "server_directory", "s0 Directory on server:", NULL);
136 }
137 
cd_mail_retrieve_imap_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)138 void cd_mail_retrieve_imap_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
139 {
140 	if( !mailaccount || !pKeyFile || !mailbox_name )
141 		return;
142 
143 	gboolean bFlushConfFileNeeded = FALSE;
144 
145 	mailaccount->driver = IMAP_STORAGE;
146 	mailaccount->storage = mailstorage_new(NULL);
147 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
148 
149 	if (g_key_file_has_key (pKeyFile, mailbox_name, "host", NULL))
150 	{
151 		mailaccount->server = CD_CONFIG_GET_STRING (mailbox_name, "host");
152 	}
153 
154 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
155 
156 	mailaccount->port = CD_CONFIG_GET_INTEGER_WITH_DEFAULT (mailbox_name, "port", 0);
157 
158 	mailaccount->connection_type = CD_CONFIG_GET_BOOLEAN_WITH_DEFAULT (
159 		mailbox_name, "use secure connection", FALSE)
160 		? CONNECTION_TYPE_TLS
161 		: CONNECTION_TYPE_PLAIN;
162 
163 	/* CONNECTION_TYPE_TLS ? CONNECTION_TYPE_STARTTLS ? */
164 
165 	if (g_key_file_has_key (pKeyFile, mailbox_name, "server_directory", NULL))
166 		mailaccount->path = CD_CONFIG_GET_STRING (mailbox_name, "server_directory");
167 	if (mailaccount->path == NULL)
168 		mailaccount->path = g_strdup("/");
169 }
170 
cd_mail_create_mbox_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)171 void cd_mail_create_mbox_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
172 {
173 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "mbox");
174 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
175 
176 	g_key_file_set_string (pKeyFile, pMailAccountName, "filename", "");
177 	g_key_file_set_comment (pKeyFile, pMailAccountName, "filename", "s0 Path of mbox file:", NULL);
178 
179 	g_key_file_set_integer (pKeyFile, pMailAccountName, "timeout mn", 10);
180 	g_key_file_set_comment (pKeyFile, pMailAccountName, "timeout mn", "I0[1;30] Refresh time:\n{In minutes.}", NULL);
181 
182 	g_key_file_set_string (pKeyFile, pMailAccountName, "mail application", "");
183 	g_key_file_set_comment (pKeyFile, pMailAccountName, "mail application", "s0 Specific mail application\n{Leave empty to use the default mail application.}", NULL);
184 }
185 
cd_mail_retrieve_mbox_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)186 void cd_mail_retrieve_mbox_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
187 {
188 	if( !mailaccount || !pKeyFile || !mailbox_name )
189 		return;
190 
191 	gboolean bFlushConfFileNeeded = FALSE;
192 
193 	mailaccount->driver = MBOX_STORAGE;
194 	mailaccount->storage = mailstorage_new(NULL);
195 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
196 	mailaccount->auth_type = POP3_AUTH_TYPE_PLAIN;
197 
198 	if (g_key_file_has_key (pKeyFile, mailbox_name, "filename", NULL))
199 		mailaccount->path = CD_CONFIG_GET_STRING_WITH_DEFAULT (mailbox_name, "filename", "/");
200 	if (mailaccount->path == NULL)
201 		mailaccount->path = g_strdup("/");
202 
203 	//{"filename", "ctime", "size", "interval", NULL, NULL}
204 }
205 
cd_mail_create_mh_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)206 void cd_mail_create_mh_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
207 {
208 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "mh");
209 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
210 
211 	g_key_file_set_integer (pKeyFile, pMailAccountName, "timeout mn", 10);
212 	g_key_file_set_comment (pKeyFile, pMailAccountName, "timeout mn", "I0[1;30] Refresh time:\n{In minutes.}", NULL);
213 
214 	g_key_file_set_string (pKeyFile, pMailAccountName, "mail application", "");
215 	g_key_file_set_comment (pKeyFile, pMailAccountName, "mail application", "s0 Specific mail application\n{Leave empty to use the default mail application.}", NULL);
216 }
217 
cd_mail_retrieve_mh_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)218 void cd_mail_retrieve_mh_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
219 {
220 	if( !mailaccount || !pKeyFile || !mailbox_name )
221 		return;
222 
223 	mailaccount->driver = MH_STORAGE;
224 	mailaccount->storage = mailstorage_new(NULL);
225 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
226 	mailaccount->auth_type = POP3_AUTH_TYPE_PLAIN;
227 	mailaccount->path = g_strdup("/");
228 }
229 
cd_mail_create_maildir_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)230 void cd_mail_create_maildir_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
231 {
232 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "maildir");
233 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
234 
235 	g_key_file_set_string (pKeyFile, pMailAccountName, "path", "");
236 	g_key_file_set_comment (pKeyFile, pMailAccountName, "path", "s0 Path to Mail directory:", NULL);
237 
238 	g_key_file_set_integer (pKeyFile, pMailAccountName, "timeout mn", 10);
239 	g_key_file_set_comment (pKeyFile, pMailAccountName, "timeout mn", "I0[1;30] Refresh time:\n{In minutes.}", NULL);
240 
241 	g_key_file_set_string (pKeyFile, pMailAccountName, "mail application", "");
242 	g_key_file_set_comment (pKeyFile, pMailAccountName, "mail application", "s0 Specific mail application\n{Leave empty to use the default mail application.}", NULL);
243 }
244 
cd_mail_retrieve_maildir_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)245 void cd_mail_retrieve_maildir_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
246 {
247 	if( !mailaccount || !pKeyFile || !mailbox_name )
248 		return;
249 
250 	gboolean bFlushConfFileNeeded = FALSE;
251 
252 	mailaccount->driver = MAILDIR_STORAGE;
253 	mailaccount->storage = mailstorage_new(NULL);
254 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
255 	mailaccount->auth_type = POP3_AUTH_TYPE_PLAIN;
256 
257 	if (g_key_file_has_key (pKeyFile, mailbox_name, "path", NULL))
258 		mailaccount->path = CD_CONFIG_GET_STRING (mailbox_name, "path");
259 	if (mailaccount->path == NULL)
260 		mailaccount->path = g_strdup("/");
261 	//{"path", "mtime", "interval", NULL, NULL, NULL, NULL}
262 }
263 
264 
cd_mail_create_feed_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)265 void cd_mail_create_feed_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
266 {
267 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "feed");
268 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
269 
270 	g_key_file_set_string (pKeyFile, pMailAccountName, "path", "http://identi.ca/api/statuses/user_timeline/cairodock.rss");
271 	g_key_file_set_comment (pKeyFile, pMailAccountName, "path", "s0 Address of feed:", NULL);
272 
273 	g_key_file_set_integer (pKeyFile, pMailAccountName, "timeout mn", 10);
274 	g_key_file_set_comment (pKeyFile, pMailAccountName, "timeout mn", "I0[1;30] Refresh time:\n{In minutes.}", NULL);
275 
276 	g_key_file_set_string (pKeyFile, pMailAccountName, "mail application", "");
277 	g_key_file_set_comment (pKeyFile, pMailAccountName, "mail application", "s0 Specific mail application\n{Leave empty to use the default mail application.}", NULL);
278 }
279 
cd_mail_retrieve_feed_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)280 void cd_mail_retrieve_feed_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
281 {
282 	if( !mailaccount || !pKeyFile || !mailbox_name ) return;
283 
284 
285 	gboolean bFlushConfFileNeeded = FALSE;
286 
287 	mailaccount->driver = FEED_STORAGE;
288 	mailaccount->storage = mailstorage_new(NULL);
289 	mailaccount->port = 443;
290 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
291 	mailaccount->auth_type = POP3_AUTH_TYPE_PLAIN;
292 
293 	if (g_key_file_has_key (pKeyFile, mailbox_name, "path", NULL))
294 	{
295 		mailaccount->path = CD_CONFIG_GET_STRING (mailbox_name, "path");
296 	}
297 }
298 
299 	//  Some servers are pre-configured
300 
cd_mail_create_gmail_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)301 void cd_mail_create_gmail_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
302 {
303 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "gmail");
304 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
305 
306 	_add_default_create_params( pKeyFile, pMailAccountName );
307 }
308 
cd_mail_retrieve_gmail_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)309 void cd_mail_retrieve_gmail_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
310 {	// FEED ou IMAP
311 	if( !mailaccount || !pKeyFile || !mailbox_name )
312 		return;
313 
314 #if (1 || __WORDSIZE == 64 )  // RSS authentification seems to have changed, and doesn't work anymore here :-/ so use IMAP by default
315 /* in 64bit libetpan crashes with RSS, so use the IMAP feature of GMail
316  * instead of RSS. */
317 	mailaccount->driver = IMAP_STORAGE;
318 	mailaccount->storage = mailstorage_new(NULL);
319 	mailaccount->server = g_strdup("imap.gmail.com");
320 	mailaccount->port = 993;
321 	mailaccount->connection_type = CONNECTION_TYPE_TLS;
322 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
323 	mailaccount->path = g_strdup("Inbox");
324 
325 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
326 #else
327 	mailaccount->driver = FEED_STORAGE;
328 	mailaccount->storage = mailstorage_new(NULL);
329 	mailaccount->port = 443;
330 	mailaccount->connection_type = CONNECTION_TYPE_STARTTLS;
331 	mailaccount->auth_type = POP3_AUTH_TYPE_PLAIN;
332 
333 	if (g_key_file_has_key (pKeyFile, mailbox_name, "username", NULL))
334 	{
335 		mailaccount->user = CD_CONFIG_GET_STRING (mailbox_name, "username");
336 	}
337 	if (g_key_file_has_key (pKeyFile, mailbox_name, "password", NULL))
338 	{
339 		gchar *encryptedPassword = CD_CONFIG_GET_STRING (mailbox_name, "password");
340 		cairo_dock_decrypt_string( encryptedPassword,  &(mailaccount->password) );
341 
342 		if( encryptedPassword ) g_free(encryptedPassword);
343 	}
344 
345 	gchar *user_without_column = NULL;
346 	gchar *password_without_column = NULL;
347 
348 	if( mailaccount->user )
349 	{
350 		gchar **splitString = g_strsplit(mailaccount->user, ":", 0);
351 		user_without_column = g_strjoinv("%3A", splitString);
352 		g_strfreev( splitString );
353 	}
354 	if( mailaccount->password )
355 	{
356 		gchar **splitString = g_strsplit(mailaccount->password, ":", 0);
357 		password_without_column = g_strjoinv("%3A", splitString);
358 		g_strfreev( splitString );
359 	}
360 
361 	if( user_without_column && password_without_column )
362 	{
363 		mailaccount->path = g_strconcat("https://", user_without_column, ":", password_without_column, "@mail.google.com/mail/feed/atom", NULL);
364 	}
365 	else
366 	{
367 		mailaccount->path = g_strdup( "https://mail.google.com/mail/feed/atom" );
368 	}
369 
370 	g_free( user_without_column );
371 	g_free( password_without_column );
372 #endif
373 }
374 
cd_mail_create_yahoo_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)375 void cd_mail_create_yahoo_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
376 {
377 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "yahoo");
378 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
379 
380 	_add_default_create_params( pKeyFile, pMailAccountName );
381 }
382 
cd_mail_retrieve_yahoo_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)383 void cd_mail_retrieve_yahoo_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
384 {	// IMAP
385 	if( !mailaccount || !pKeyFile || !mailbox_name )
386 		return;
387 
388 	mailaccount->driver = IMAP_STORAGE;
389 	mailaccount->storage = mailstorage_new(NULL);
390 	mailaccount->server = g_strdup("imap.mail.yahoo.com");
391 	mailaccount->port = 993;
392 	mailaccount->connection_type = CONNECTION_TYPE_TLS;
393 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
394 	mailaccount->path = g_strdup("Inbox");
395 
396 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
397 }
398 
cd_mail_create_hotmail_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)399 void cd_mail_create_hotmail_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
400 {
401 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "hotmail");
402 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
403 
404 	_add_default_create_params( pKeyFile, pMailAccountName );
405 }
406 
cd_mail_retrieve_hotmail_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)407 void cd_mail_retrieve_hotmail_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
408 {	// POP3
409 	if( !mailaccount || !pKeyFile || !mailbox_name )
410 		return;
411 
412 	mailaccount->driver = POP3_STORAGE;
413 	mailaccount->storage = mailstorage_new(NULL);
414 	mailaccount->server = g_strdup("pop3.live.com");
415 	mailaccount->port = 995;
416 	mailaccount->connection_type = CONNECTION_TYPE_TLS;
417 	mailaccount->auth_type = POP3_AUTH_TYPE_TRY_APOP;
418 
419 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
420 }
421 
cd_mail_create_free_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)422 void cd_mail_create_free_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
423 {
424 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "free");
425 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
426 
427 	_add_default_create_params( pKeyFile, pMailAccountName );
428 }
429 
cd_mail_retrieve_free_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)430 void cd_mail_retrieve_free_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
431 {	// IMAP
432 	if( !mailaccount || !pKeyFile || !mailbox_name )
433 		return;
434 
435 	mailaccount->driver = IMAP_STORAGE;
436 	mailaccount->storage = mailstorage_new(NULL);
437 	mailaccount->server = g_strdup("imap.free.fr");
438 	mailaccount->port = 143;
439 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
440 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
441 	mailaccount->path = g_strdup("Inbox");
442 
443 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
444 }
445 
cd_mail_create_neuf_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)446 void cd_mail_create_neuf_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
447 {
448 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "neuf");
449 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
450 
451 	_add_default_create_params( pKeyFile, pMailAccountName );
452 }
453 
cd_mail_retrieve_neuf_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)454 void cd_mail_retrieve_neuf_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
455 {	// IMAP
456 	if( !mailaccount || !pKeyFile || !mailbox_name )
457 		return;
458 
459 	mailaccount->driver = IMAP_STORAGE;
460 	mailaccount->storage = mailstorage_new(NULL);
461 	mailaccount->server = g_strdup("imap.neuf.fr");
462 	mailaccount->port = 143;
463 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
464 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
465 	mailaccount->path = g_strdup("Inbox");
466 
467 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
468 }
469 
cd_mail_create_sfr_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)470 void cd_mail_create_sfr_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
471 {
472 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "sfr");
473 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
474 
475 	_add_default_create_params( pKeyFile, pMailAccountName );
476 }
477 
cd_mail_retrieve_sfr_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)478 void cd_mail_retrieve_sfr_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
479 {	// IMAP
480 	if( !mailaccount || !pKeyFile || !mailbox_name )
481 		return;
482 
483 	mailaccount->driver = IMAP_STORAGE;
484 	mailaccount->storage = mailstorage_new(NULL);
485 	mailaccount->server = g_strdup("imap.sfr.fr");
486 	mailaccount->port = 143;
487 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
488 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
489 	mailaccount->path = g_strdup("Inbox");
490 
491 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
492 }
493 
cd_mail_create_orange_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)494 void cd_mail_create_orange_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
495 {
496 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "orange");
497 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
498 
499 	_add_default_create_params( pKeyFile, pMailAccountName );
500 }
501 
cd_mail_retrieve_orange_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)502 void cd_mail_retrieve_orange_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
503 {	// IMAP
504 	if( !mailaccount || !pKeyFile || !mailbox_name )
505 		return;
506 
507 	mailaccount->driver = IMAP_STORAGE;
508 	mailaccount->storage = mailstorage_new(NULL);
509 	mailaccount->server = g_strdup("imap.orange.fr");
510 	mailaccount->port = 143;
511 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
512 	mailaccount->auth_type = IMAP_AUTH_TYPE_PLAIN;
513 	mailaccount->path = g_strdup("Inbox");
514 
515 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
516 }
517 
cd_mail_create_skynet_params(GKeyFile * pKeyFile,const gchar * pMailAccountName)518 void cd_mail_create_skynet_params( GKeyFile *pKeyFile, const gchar *pMailAccountName )
519 {
520 	g_key_file_set_string (pKeyFile, pMailAccountName, "type", "skynet");
521 	g_key_file_set_comment (pKeyFile, pMailAccountName, "type", ">0 ", NULL);
522 
523 	_add_default_create_params( pKeyFile, pMailAccountName );
524 }
525 
cd_mail_retrieve_skynet_params(CDMailAccount * mailaccount,GKeyFile * pKeyFile,const gchar * mailbox_name)526 void cd_mail_retrieve_skynet_params (CDMailAccount *mailaccount, GKeyFile *pKeyFile, const gchar *mailbox_name)
527 {	// POP3
528 	if( !mailaccount || !pKeyFile || !mailbox_name )
529 		return;
530 
531 	mailaccount->driver = POP3_STORAGE;
532 	mailaccount->storage = mailstorage_new(NULL);
533 	mailaccount->server = g_strdup("pop.skynet.be");
534 	mailaccount->port = 110;
535 	mailaccount->connection_type = CONNECTION_TYPE_PLAIN;
536 	mailaccount->auth_type = POP3_AUTH_TYPE_TRY_APOP;
537 
538 	_retrieve_user_password( mailaccount, pKeyFile, mailbox_name );
539 }
540 
541 /*{
542 	{POP3_STORAGE, "pop3", {"host", "username", "password", "auth_type", "timeout mn", "port", NULL}},
543 	{IMAP_STORAGE, "imap", {"host", "username", "password", "auth_type", "timeout mn", "port", "server_directory"}},
544 	{NNTP_STORAGE, "nntp", {NULL, NULL, NULL, NULL, NULL, NULL, NULL}},
545 	{MBOX_STORAGE, "mbox", {"filename", "ctime", "size", "interval", NULL, NULL}},
546 	{MH_STORAGE, "mh", {"timeout mn", NULL, NULL, NULL, NULL, NULL, NULL}},
547 	{MAILDIR_STORAGE, "maildir", {"path", "mtime", "interval", NULL, NULL, NULL, NULL}},
548 	{FEED_STORAGE, "feed", {"username", "password", "timeout mn", NULL, NULL, NULL, NULL}},
549 	{FEED_STORAGE, "gmail", {"username", "password", "timeout mn", NULL, NULL, NULL, NULL}},
550 };*/
551 
552 
cd_mail_init_accounts(GldiModuleInstance * myApplet)553 void cd_mail_init_accounts(GldiModuleInstance *myApplet)
554 {
555 	if (myData.pMailAccounts == NULL)
556 		return ;
557 	cd_debug ("%s (%d comptes)", __func__, myData.pMailAccounts->len);
558 
559 	//\_______________________ On initialise chaque compte.
560 	CDMailAccount *pMailAccount;
561 	GList *pIconList = NULL;
562 	Icon *pIcon;
563 	int iNbIcons = 0;
564 	int r;
565 	gboolean bIsGettingMail = FALSE;
566 	guint i;
567 	for (i = 0; i < myData.pMailAccounts->len; i ++)
568 	{
569 		pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);
570 		if( !pMailAccount ) continue;
571 
572 		// init this account
573 		switch (pMailAccount->driver) {
574 			case POP3_STORAGE:
575 				r = pop3_mailstorage_init(pMailAccount->storage, pMailAccount->server, pMailAccount->port,
576 					NULL, pMailAccount->connection_type,
577 					pMailAccount->auth_type, pMailAccount->user, pMailAccount->password,
578 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/, myData.cWorkingDirPath /*flags_directory*/);
579 			break;
580 
581 			case IMAP_STORAGE:
582 				r = imap_mailstorage_init(pMailAccount->storage, pMailAccount->server, pMailAccount->port,
583 					NULL, pMailAccount->connection_type,
584 					IMAP_AUTH_TYPE_PLAIN, pMailAccount->user, pMailAccount->password,
585 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/);
586 			break;
587 
588 			case NNTP_STORAGE:
589 				r = nntp_mailstorage_init(pMailAccount->storage, pMailAccount->server, pMailAccount->port,
590 					NULL, pMailAccount->connection_type,
591 					NNTP_AUTH_TYPE_PLAIN, pMailAccount->user, pMailAccount->password,
592 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/, myData.cWorkingDirPath /*flags_directory*/);
593 			break;
594 
595 			case MBOX_STORAGE:
596 				r = mbox_mailstorage_init(pMailAccount->storage, pMailAccount->path,
597 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/, myData.cWorkingDirPath /*flags_directory*/);
598 			break;
599 
600 			case MH_STORAGE:
601 				r = mh_mailstorage_init(pMailAccount->storage, pMailAccount->path,
602 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/, myData.cWorkingDirPath /*flags_directory*/);
603 			break;
604 
605 			case MAILDIR_STORAGE:
606 				r = maildir_mailstorage_init(pMailAccount->storage, pMailAccount->path,
607 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/, myData.cWorkingDirPath /*flags_directory*/);
608 			break;
609 
610 			case FEED_STORAGE:
611 				r = feed_mailstorage_init(pMailAccount->storage, pMailAccount->path,
612 					myData.cWorkingDirPath!=NULL?TRUE:FALSE /*cached*/, myData.cWorkingDirPath /*cache_directory*/, myData.cWorkingDirPath /*flags_directory*/);
613 			break;
614 			default :
615 				r = -1;
616 		}
617 
618 		// add an icon for this account.
619 		if (myData.pMailAccounts->len == 1)  // 1 seul compte
620 		{
621 			pIcon = myIcon;
622 		}
623 		else
624 		{
625 			_add_icon (pMailAccount);
626 		}
627 		iNbIcons ++;
628 
629 		//  if all is OK, then set a timeout for this mail account
630 		if (r == MAIL_NO_ERROR)
631 		{
632 			gldi_icon_set_quick_info (pIcon, "..."); // on the current icon
633 			pMailAccount->pAccountMailTimer = gldi_task_new (pMailAccount->timeout * 60,
634 				(GldiGetDataAsyncFunc) cd_mail_get_folder_data,
635 				(GldiUpdateSyncFunc) cd_mail_update_account_status,
636 				pMailAccount);
637 			gldi_task_launch (pMailAccount->pAccountMailTimer);
638 			bIsGettingMail = TRUE;
639 		}
640 		else
641 		{
642 			cd_warning ("mail : the mail account %s couldn't be initialized !", pMailAccount->name);
643 			gldi_icon_set_quick_info (pIcon, "N/A");
644 		}
645 	}
646 
647 	//\_______________________ On efface l'ancienne liste.
648 	CD_APPLET_DELETE_MY_ICONS_LIST;
649 
650 	//\_______________________ On charge la nouvelle liste.
651 	if (iNbIcons > 1)
652 	{
653 		gpointer pConfig[2] = {GINT_TO_POINTER (FALSE), GINT_TO_POINTER (FALSE)};
654 		CD_APPLET_LOAD_MY_ICONS_LIST (pIconList, myConfig.cRenderer, "Caroussel", pConfig);
655 	}
656 
657 	//\_______________ On dessine l'icone principale initialement.
658 	CD_APPLET_SET_IMAGE_ON_MY_ICON (myConfig.cNoMailUserImage);
659 	if (bIsGettingMail && myData.iPrevNbUnreadMails == G_MAXUINT) // only at the init...
660 		CD_APPLET_SET_QUICK_INFO_ON_MY_ICON ("...");
661 }
662 
663 
cd_mail_free_account(CDMailAccount * pMailAccount)664 void cd_mail_free_account (CDMailAccount *pMailAccount)
665 {
666 	if (pMailAccount == NULL)
667 		return ;
668 
669 	gldi_task_free( pMailAccount->pAccountMailTimer );
670 
671 	g_free( pMailAccount->name );
672 	g_free( pMailAccount->server );
673 	g_free( pMailAccount->user );
674 	g_free( pMailAccount->password );
675 	g_free( pMailAccount->path );
676 	g_free( pMailAccount->cMailApp );
677 	g_free( pMailAccount->cIconName );
678 
679 	if( pMailAccount->folder )
680 		mailfolder_free(pMailAccount->folder);
681 	if( pMailAccount->storage )
682 		mailstorage_free(pMailAccount->storage);
683 
684 	g_list_foreach (pMailAccount->pUnseenMessageList, (GFunc) g_free, NULL);
685 	g_list_free (pMailAccount->pUnseenMessageList);
686 
687 	g_list_foreach (pMailAccount->pUnseenMessageUid, (GFunc) g_free, NULL);
688 	g_list_free (pMailAccount->pUnseenMessageUid);
689 
690 	g_free( pMailAccount );
691 }
692 
cd_mail_free_all_accounts(GldiModuleInstance * myApplet)693 void cd_mail_free_all_accounts (GldiModuleInstance *myApplet)
694 {
695 	if (myData.pMailAccounts == NULL)
696 		return ;
697 	CDMailAccount *pMailAccount;
698 	guint i;
699 	for (i = 0; i < myData.pMailAccounts->len; i ++)
700 	{
701 		pMailAccount = g_ptr_array_index (myData.pMailAccounts, i);
702 		cd_mail_free_account (pMailAccount);
703 	}
704 	g_ptr_array_free (myData.pMailAccounts, TRUE);
705 	myData.pMailAccounts = NULL;
706 }
707