3d77dcb67f1404baf32fb8c9b95078115756a409
[modest] / src / gnome / modest-platform.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <libgnomevfs/gnome-vfs-mime.h>
31 #include <libgnomeui/gnome-icon-lookup.h>
32 #include <tny-gnome-device.h>
33 #include <tny-camel-imap-store-account.h>
34 #include <tny-camel-pop-store-account.h>
35
36 #include "modest-platform.h"
37 #include "modest-mail-operation-queue.h"
38 #include "modest-runtime.h"
39
40 #include "gnome/modest-gnome-global-settings-dialog.h"
41 #include "widgets/modest-account-settings-dialog.h"
42 #include "gnome/modest-account-assistant.h"
43 #include "gnome/modest-gnome-sort-dialog.h"
44
45 gboolean
46 modest_platform_init (int argc, char *argv[])
47 {       
48         return TRUE; /* nothing to do */
49 }
50
51
52 gboolean modest_platform_uninit (void)
53 {
54         return TRUE; /*nothing to do */
55 }
56
57
58 TnyDevice*
59 modest_platform_get_new_device (void)
60 {
61         return TNY_DEVICE (tny_gnome_device_new ());
62 }
63
64
65 gchar*
66 modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
67                                           gchar **effective_mime_type)
68 {
69         GString *mime_str = NULL;
70         gchar *icon_name  = NULL;
71         gchar *uri;
72         const static gchar* octet_stream = "application/octet-stream";
73         
74         g_return_val_if_fail (name || mime_type, NULL);
75
76         if (!mime_type || g_ascii_strcasecmp (mime_type, octet_stream)) 
77                 mime_str = g_string_new(gnome_vfs_mime_type_from_name_or_default
78                                         (name, "application/octet-stream"));
79         else {
80                 mime_str = g_string_new (mime_type);
81                 g_string_ascii_down (mime_str);
82         }
83
84         uri = g_strconcat ("file:///", name ? name : "dummy", NULL);
85         icon_name  = gnome_icon_lookup (gtk_icon_theme_get_default(), NULL,
86                                         uri, NULL, NULL, mime_str->str, 0, 0);
87         g_free (uri);
88
89         if (effective_mime_type)
90                 *effective_mime_type = g_string_free (mime_str, FALSE);
91         else
92                 g_string_free (mime_str, TRUE);
93
94         return icon_name;
95 }
96
97 gboolean 
98 modest_platform_activate_uri (const gchar *uri)
99 {
100         g_message ("NOT IMPLEMENTED");;
101         return FALSE;
102 }
103
104 gboolean 
105 modest_platform_activate_file (const gchar *path, const gchar *mime_type)
106 {
107         g_message ("NOT IMPLEMENTED");;
108         return FALSE;
109 }
110
111 gboolean 
112 modest_platform_show_uri_popup (const gchar *uri)
113 {
114         g_message ("NOT IMPLEMENTED");;
115         return FALSE;
116 }
117
118 GdkPixbuf*
119 modest_platform_get_icon (const gchar *name, guint icon_size)
120 {
121         GError *err = NULL;
122         GdkPixbuf* pixbuf;
123
124         g_return_val_if_fail (name, NULL);
125
126         pixbuf = gdk_pixbuf_new_from_file (name, &err);
127
128         if (!pixbuf) {
129 /*              g_printerr ("modest: error while loading icon '%s': %s\n", */
130 /*                          name, err->message); */
131                 g_error_free (err);
132                 err = NULL;
133
134                 pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), name, icon_size, 0, &err);
135                 if (!pixbuf) {
136                         g_error_free (err);
137                 }
138         }
139         
140         return pixbuf;
141 }
142
143
144 const gchar*
145 modest_platform_get_app_name (void)
146 {
147         return ("Modest");
148 }
149
150 gint 
151 modest_platform_run_new_folder_dialog (GtkWindow *parent_window,
152                                        TnyFolderStore *parent_folder,
153                                        gchar *suggested_name,
154                                        gchar **folder_name)
155 {
156         GtkWidget *dialog, *entry;
157         gint result;
158
159         /* Ask the user for the folder name */
160         dialog = gtk_dialog_new_with_buttons (_("New Folder Name"),
161                                               parent_window,
162                                               GTK_DIALOG_MODAL,
163                                               GTK_STOCK_CANCEL,
164                                               GTK_RESPONSE_REJECT,
165                                               GTK_STOCK_OK,
166                                               GTK_RESPONSE_ACCEPT,
167                                               NULL);
168         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
169                             gtk_label_new (_("Please enter a name for the new folder")),
170                             FALSE, FALSE, 0);
171                 
172         entry = gtk_entry_new_with_max_length (40);
173         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
174                             entry,
175                             TRUE, FALSE, 0);
176         
177         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
178
179         result = gtk_dialog_run (GTK_DIALOG(dialog));
180         if (result == GTK_RESPONSE_ACCEPT)
181                 *folder_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
182
183         gtk_widget_destroy (dialog);
184
185         return result;
186 }
187
188
189 gint
190 modest_platform_run_confirmation_dialog (GtkWindow *parent_window,
191                                          const gchar *msg)
192 {
193         gint response;
194         GtkWidget *dialog;
195
196         dialog = gtk_message_dialog_new (parent_window,
197                                          GTK_DIALOG_MODAL,
198                                          GTK_MESSAGE_QUESTION,
199                                          GTK_BUTTONS_OK_CANCEL,
200                                          msg);
201
202         response = gtk_dialog_run (GTK_DIALOG(dialog));
203         gtk_widget_destroy (dialog);
204         
205         return response;
206 }
207
208 gint
209 modest_platform_run_confirmation_dialog_with_buttons (GtkWindow *parent_window,
210                                                       const gchar *message,
211                                                       const gchar *button_accept,
212                                                       const gchar *button_cancel)
213 {
214         gint response;
215         GtkWidget *dialog;
216
217         dialog = gtk_dialog_new_with_buttons (message,
218                                               parent_window,
219                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
220                                               button_accept,
221                                               GTK_RESPONSE_ACCEPT,
222                                               button_cancel,
223                                               GTK_RESPONSE_CANCEL,
224                                               NULL);
225
226         response = gtk_dialog_run (GTK_DIALOG(dialog));
227         gtk_widget_destroy (dialog);
228         
229         return response;
230 }
231
232 void
233 modest_platform_run_information_dialog (GtkWindow *parent_window,
234                                         const gchar *message,
235                                         gboolean block)
236 {
237         GtkWidget *dialog;
238
239         dialog = gtk_message_dialog_new (parent_window,
240                                          GTK_DIALOG_MODAL,
241                                          GTK_MESSAGE_INFO,
242                                          GTK_BUTTONS_OK,
243                                          message);
244
245         gtk_dialog_run (GTK_DIALOG (dialog));
246         gtk_widget_destroy (dialog);
247         
248 }
249
250 gboolean modest_platform_connect_and_wait (GtkWindow *parent_window, TnyAccount *account)
251 {
252         /* TODO: Do something with network-manager? 
253            Otherwise, maybe it is safe to assume that we would already be online if we could be. */
254         return TRUE;
255 }
256
257 gboolean modest_platform_connect_and_wait_if_network_account (GtkWindow *parent_window, TnyAccount *account)
258 {
259         /* TODO: Do something with network-manager? 
260            Otherwise, maybe it is safe to assume that we would already be online if we could be. */
261         return TRUE;
262 }
263
264
265 void
266 modest_platform_connect_if_remote_and_perform (GtkWindow *parent_window, 
267                                                gboolean force,
268                                                TnyFolderStore *folder_store, 
269                                                ModestConnectedPerformer callback, 
270                                                gpointer user_data)
271 {
272         TnyAccount *account = NULL;
273         
274         if (!folder_store) {
275                 /* We promise to instantly perform the callback, so ... */
276                 if (callback) {
277                         callback (FALSE, NULL, parent_window, NULL, user_data);
278                 }
279                 return; 
280                 
281                 /* Original comment: Maybe it is something local. */
282                 /* PVH's comment: maybe we should KNOW this in stead of assuming? */
283                 
284         } else if (TNY_IS_FOLDER (folder_store)) {
285                 /* Get the folder's parent account: */
286                 account = tny_folder_get_account(TNY_FOLDER (folder_store));
287         } else if (TNY_IS_ACCOUNT (folder_store)) {
288                 /* Use the folder store as an account: */
289                 account = TNY_ACCOUNT (folder_store);
290         }
291  
292         if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
293                 if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
294                     !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
295                         
296                         /* This IS a local account like a maildir account, which does not require 
297                          * a connection. (original comment had a vague assumption in its language
298                          * usage. There's no assuming needed, this IS what it IS: a local account), */
299  
300                         /* We promise to instantly perform the callback, so ... */
301                         if (callback) {
302                                 callback (FALSE, NULL, parent_window, account, user_data);
303                         }
304                         
305                         return;
306                 }
307         }
308  
309         modest_platform_connect_and_perform (parent_window, force, account, callback, user_data);
310  
311         return;
312 }
313
314
315 gboolean modest_platform_set_update_interval (guint minutes)
316 {
317         /* TODO. */
318         return FALSE;
319 }
320
321 GtkWidget *
322 modest_platform_create_sort_dialog (GtkWindow *parent_window)
323 {
324         return modest_gnome_sort_dialog_new (parent_window);
325 }
326
327 GtkWidget *
328 modest_platform_get_global_settings_dialog ()
329 {
330         return modest_gnome_global_settings_dialog_new ();
331 }
332
333 void
334 modest_platform_push_email_notification(void)
335 {
336         /* TODO: implement this */
337         g_print ("--------------- NEW MESSAGE ARRIVED ---------------\n");
338 }
339
340 void 
341 modest_platform_on_new_headers_received (TnyList *header_list,
342                                          gboolean show_visual)
343 {
344         /* TODO: implement this */
345         g_print ("--------------- NEW MESSAGE ARRIVED ---------------\n");
346 }
347
348
349
350 void
351 modest_platform_show_help (GtkWindow *parent_window, const gchar *help_id)
352 {
353         return; /* TODO */
354 }
355
356 void 
357 modest_platform_information_banner (GtkWidget *widget,
358                                     const gchar *icon_name,
359                                     const gchar *text)
360 {
361         g_message ("NOT IMPLEMENTED");;
362 }
363
364 void
365 modest_platform_information_banner_with_timeout (GtkWidget *widget,
366                                                  const gchar *icon_name,
367                                                  const gchar *text,
368                                                  gint timeout)
369 {
370         g_message ("NOT IMPLEMENTED");;
371 }
372
373 GtkWidget *
374 modest_platform_animation_banner (GtkWidget *widget,
375                                   const gchar *icon_name,
376                                   const gchar *text)
377 {
378         g_message ("NOT IMPLEMENTED");
379         return NULL;
380 }
381
382
383 void
384 modest_platform_show_search_messages (GtkWindow *parent_window)
385 {
386         g_message ("NOT IMPLEMENTED");;
387 }
388
389 GtkWidget *
390 modest_platform_create_folder_view (TnyFolderStoreQuery *query)
391 {
392         GtkWidget *widget = modest_folder_view_new (query);
393
394         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (widget), FALSE);
395         /* Show all accounts by default */
396         modest_folder_view_set_style (MODEST_FOLDER_VIEW (widget),
397                                       MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
398
399         return widget;
400 }
401
402 gboolean
403 modest_platform_run_alert_dialog (const gchar* prompt, 
404                                   gboolean is_question)
405 {
406         /* TODO */
407         return TRUE;
408 }
409
410 void 
411 modest_platform_connect_and_perform (GtkWindow *parent_window, 
412                                      gboolean force,
413                                      TnyAccount *account, 
414                                      ModestConnectedPerformer callback, 
415                                      gpointer user_data)
416 {
417         if (callback)
418                 callback (FALSE, NULL, parent_window, account, user_data);
419 }
420
421 void 
422 modest_platform_double_connect_and_perform (GtkWindow *parent_window, 
423                                             gboolean force,
424                                             TnyFolderStore *folder_store,
425                                             DoubleConnectionInfo *connect_info)
426 {
427         if (connect_info->callback)
428                 connect_info->callback (FALSE, NULL, parent_window, TNY_ACCOUNT (folder_store), connect_info->data);
429 }
430
431 void 
432 modest_platform_connect_and_perform_if_network_account (GtkWindow *parent_window, 
433                                                         gboolean force,
434                                                         TnyAccount *account,
435                                                         ModestConnectedPerformer callback, 
436                                                         gpointer user_data)
437 {
438         if (callback)
439                 callback (FALSE, NULL, parent_window, account, user_data);
440 }
441
442 void
443 modest_platform_connect_and_perform_if_network_folderstore (GtkWindow *parent_window, 
444                                                             TnyFolderStore *folder_store, 
445                                                             ModestConnectedPerformer callback, 
446                                                             gpointer user_data)
447 {
448         if (callback)
449                 callback (FALSE, NULL, parent_window, NULL, user_data);
450 }
451
452
453 void 
454 modest_platform_remove_new_mail_notifications (gboolean only_visuals)
455 {
456         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
457 }
458
459 gboolean 
460 modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
461 {
462         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
463         return TRUE;
464 }
465
466 gboolean 
467 modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
468                                                      const gchar *certificate)
469 {
470         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
471         return TRUE;
472 }
473
474 gint
475 modest_platform_run_rename_folder_dialog (GtkWindow *parent_window,
476                                           TnyFolderStore *parent_folder,
477                                           const gchar *suggested_name,
478                                           gchar **folder_name)
479 {
480         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
481         return GTK_RESPONSE_CANCEL;
482 }
483
484 void 
485 modest_platform_show_addressbook (GtkWindow *parent_window)
486 {
487         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
488 }
489
490 GtkWidget *
491 modest_platform_get_account_settings_dialog (ModestAccountSettings *settings)
492 {
493         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
494
495         modest_account_settings_dialog_set_account (dialog, settings);
496         return GTK_WIDGET (dialog);
497 }
498
499 GtkWidget *
500 modest_platform_get_account_settings_wizard (void)
501 {
502         GtkWidget *widget = modest_account_assistant_new (modest_runtime_get_account_mgr ());
503         return widget;
504 }
505
506
507
508 gboolean modest_platform_check_memory_low (ModestWindow *win,
509                                            gboolean visuals)
510 {
511         g_debug ("%s not implemented", __FUNCTION__);
512         return FALSE;
513 }