This is a manual merge of branch drop split view intro trunk.
[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 #include "widgets/modest-details-dialog.h"
45
46 gboolean
47 modest_platform_init (int argc, char *argv[])
48 {       
49         return TRUE; /* nothing to do */
50 }
51
52
53 gboolean modest_platform_uninit (void)
54 {
55         return TRUE; /*nothing to do */
56 }
57
58
59 TnyDevice*
60 modest_platform_get_new_device (void)
61 {
62         return TNY_DEVICE (tny_gnome_device_new ());
63 }
64
65
66 gchar*
67 modest_platform_get_file_icon_name (const gchar* name, const gchar* mime_type,
68                                           gchar **effective_mime_type)
69 {
70         GString *mime_str = NULL;
71         gchar *icon_name  = NULL;
72         gchar *uri;
73         const static gchar* octet_stream = "application/octet-stream";
74         
75         g_return_val_if_fail (name || mime_type, NULL);
76
77         if (!mime_type || g_ascii_strcasecmp (mime_type, octet_stream)) 
78                 mime_str = g_string_new(gnome_vfs_mime_type_from_name_or_default
79                                         (name, "application/octet-stream"));
80         else {
81                 mime_str = g_string_new (mime_type);
82                 g_string_ascii_down (mime_str);
83         }
84
85         uri = g_strconcat ("file:///", name ? name : "dummy", NULL);
86         icon_name  = gnome_icon_lookup (gtk_icon_theme_get_default(), NULL,
87                                         uri, NULL, NULL, mime_str->str, 0, 0);
88         g_free (uri);
89
90         if (effective_mime_type)
91                 *effective_mime_type = g_string_free (mime_str, FALSE);
92         else
93                 g_string_free (mime_str, TRUE);
94
95         return icon_name;
96 }
97
98 gboolean 
99 modest_platform_activate_uri (const gchar *uri)
100 {
101         g_message ("NOT IMPLEMENTED");;
102         return FALSE;
103 }
104
105 gboolean 
106 modest_platform_activate_file (const gchar *path, const gchar *mime_type)
107 {
108         g_message ("NOT IMPLEMENTED");;
109         return FALSE;
110 }
111
112 gboolean 
113 modest_platform_show_uri_popup (const gchar *uri)
114 {
115         g_message ("NOT IMPLEMENTED");;
116         return FALSE;
117 }
118
119 GdkPixbuf*
120 modest_platform_get_icon (const gchar *name, guint icon_size)
121 {
122         GError *err = NULL;
123         GdkPixbuf* pixbuf;
124
125         g_return_val_if_fail (name, NULL);
126
127         pixbuf = gdk_pixbuf_new_from_file (name, &err);
128
129         if (!pixbuf) {
130 /*              g_printerr ("modest: error while loading icon '%s': %s\n", */
131 /*                          name, err->message); */
132                 g_error_free (err);
133                 err = NULL;
134
135                 pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), name, icon_size, 0, &err);
136                 if (!pixbuf) {
137                         g_error_free (err);
138                 }
139         }
140         
141         return pixbuf;
142 }
143
144
145 const gchar*
146 modest_platform_get_app_name (void)
147 {
148         return ("Modest");
149 }
150
151 gint 
152 modest_platform_run_new_folder_dialog (GtkWindow *parent_window,
153                                        TnyFolderStore *parent_folder,
154                                        gchar *suggested_name,
155                                        gchar **folder_name)
156 {
157         GtkWidget *dialog, *entry;
158         gint result;
159
160         /* Ask the user for the folder name */
161         dialog = gtk_dialog_new_with_buttons (_("New Folder Name"),
162                                               parent_window,
163                                               GTK_DIALOG_MODAL,
164                                               GTK_STOCK_CANCEL,
165                                               GTK_RESPONSE_REJECT,
166                                               GTK_STOCK_OK,
167                                               GTK_RESPONSE_ACCEPT,
168                                               NULL);
169         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
170                             gtk_label_new (_("Please enter a name for the new folder")),
171                             FALSE, FALSE, 0);
172                 
173         entry = gtk_entry_new_with_max_length (40);
174         gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog)->vbox), 
175                             entry,
176                             TRUE, FALSE, 0);
177         
178         gtk_widget_show_all (GTK_WIDGET(GTK_DIALOG(dialog)->vbox));
179
180         result = gtk_dialog_run (GTK_DIALOG(dialog));
181         if (result == GTK_RESPONSE_ACCEPT)
182                 *folder_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
183
184         gtk_widget_destroy (dialog);
185
186         return result;
187 }
188
189
190 gint
191 modest_platform_run_confirmation_dialog (GtkWindow *parent_window,
192                                          const gchar *msg)
193 {
194         gint response;
195         GtkWidget *dialog;
196
197         dialog = gtk_message_dialog_new (parent_window,
198                                          GTK_DIALOG_MODAL,
199                                          GTK_MESSAGE_QUESTION,
200                                          GTK_BUTTONS_OK_CANCEL,
201                                          msg);
202
203         response = gtk_dialog_run (GTK_DIALOG(dialog));
204         gtk_widget_destroy (dialog);
205         
206         return response;
207 }
208
209 gint
210 modest_platform_run_confirmation_dialog_with_buttons (GtkWindow *parent_window,
211                                                       const gchar *message,
212                                                       const gchar *button_accept,
213                                                       const gchar *button_cancel)
214 {
215         gint response;
216         GtkWidget *dialog;
217
218         dialog = gtk_dialog_new_with_buttons (message,
219                                               parent_window,
220                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
221                                               button_accept,
222                                               GTK_RESPONSE_ACCEPT,
223                                               button_cancel,
224                                               GTK_RESPONSE_CANCEL,
225                                               NULL);
226
227         response = gtk_dialog_run (GTK_DIALOG(dialog));
228         gtk_widget_destroy (dialog);
229         
230         return response;
231 }
232
233 void
234 modest_platform_run_information_dialog (GtkWindow *parent_window,
235                                         const gchar *message,
236                                         gboolean block)
237 {
238         GtkWidget *dialog;
239
240         dialog = gtk_message_dialog_new (parent_window,
241                                          GTK_DIALOG_MODAL,
242                                          GTK_MESSAGE_INFO,
243                                          GTK_BUTTONS_OK,
244                                          message);
245
246         gtk_dialog_run (GTK_DIALOG (dialog));
247         gtk_widget_destroy (dialog);
248         
249 }
250
251 gboolean modest_platform_connect_and_wait (GtkWindow *parent_window, TnyAccount *account)
252 {
253         /* TODO: Do something with network-manager? 
254            Otherwise, maybe it is safe to assume that we would already be online if we could be. */
255         return TRUE;
256 }
257
258 gboolean modest_platform_connect_and_wait_if_network_account (GtkWindow *parent_window, TnyAccount *account)
259 {
260         /* TODO: Do something with network-manager? 
261            Otherwise, maybe it is safe to assume that we would already be online if we could be. */
262         return TRUE;
263 }
264
265
266 void
267 modest_platform_connect_if_remote_and_perform (GtkWindow *parent_window, 
268                                                gboolean force,
269                                                TnyFolderStore *folder_store, 
270                                                ModestConnectedPerformer callback, 
271                                                gpointer user_data)
272 {
273         TnyAccount *account = NULL;
274         
275         if (!folder_store) {
276                 /* We promise to instantly perform the callback, so ... */
277                 if (callback) {
278                         callback (FALSE, NULL, parent_window, NULL, user_data);
279                 }
280                 return; 
281                 
282                 /* Original comment: Maybe it is something local. */
283                 /* PVH's comment: maybe we should KNOW this in stead of assuming? */
284                 
285         } else if (TNY_IS_FOLDER (folder_store)) {
286                 /* Get the folder's parent account: */
287                 account = tny_folder_get_account(TNY_FOLDER (folder_store));
288         } else if (TNY_IS_ACCOUNT (folder_store)) {
289                 /* Use the folder store as an account: */
290                 account = TNY_ACCOUNT (folder_store);
291         }
292  
293         if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
294                 if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
295                     !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
296                         
297                         /* This IS a local account like a maildir account, which does not require 
298                          * a connection. (original comment had a vague assumption in its language
299                          * usage. There's no assuming needed, this IS what it IS: a local account), */
300  
301                         /* We promise to instantly perform the callback, so ... */
302                         if (callback) {
303                                 callback (FALSE, NULL, parent_window, account, user_data);
304                         }
305                         
306                         return;
307                 }
308         }
309  
310         modest_platform_connect_and_perform (parent_window, force, account, callback, user_data);
311  
312         return;
313 }
314
315
316 gboolean modest_platform_set_update_interval (guint minutes)
317 {
318         /* TODO. */
319         return FALSE;
320 }
321
322 GtkWidget *
323 modest_platform_create_sort_dialog (GtkWindow *parent_window)
324 {
325         return modest_gnome_sort_dialog_new (parent_window);
326 }
327
328 GtkWidget *
329 modest_platform_get_global_settings_dialog ()
330 {
331         return modest_gnome_global_settings_dialog_new ();
332 }
333
334 void
335 modest_platform_push_email_notification(void)
336 {
337         /* TODO: implement this */
338         g_print ("--------------- NEW MESSAGE ARRIVED ---------------\n");
339 }
340
341 void 
342 modest_platform_on_new_headers_received (TnyList *header_list,
343                                          gboolean show_visual)
344 {
345         /* TODO: implement this */
346         g_print ("--------------- NEW MESSAGE ARRIVED ---------------\n");
347 }
348
349
350
351 void
352 modest_platform_show_help (GtkWindow *parent_window, const gchar *help_id)
353 {
354         return; /* TODO */
355 }
356
357 void 
358 modest_platform_information_banner (GtkWidget *widget,
359                                     const gchar *icon_name,
360                                     const gchar *text)
361 {
362         g_message ("NOT IMPLEMENTED");;
363 }
364
365 void
366 modest_platform_information_banner_with_timeout (GtkWidget *widget,
367                                                  const gchar *icon_name,
368                                                  const gchar *text,
369                                                  gint timeout)
370 {
371         g_message ("NOT IMPLEMENTED");;
372 }
373
374 GtkWidget *
375 modest_platform_animation_banner (GtkWidget *widget,
376                                   const gchar *icon_name,
377                                   const gchar *text)
378 {
379         g_message ("NOT IMPLEMENTED");
380         return NULL;
381 }
382
383
384 void
385 modest_platform_show_search_messages (GtkWindow *parent_window)
386 {
387         g_message ("NOT IMPLEMENTED");;
388 }
389
390 GtkWidget *
391 modest_platform_create_folder_view (TnyFolderStoreQuery *query)
392 {
393         GtkWidget *widget = modest_folder_view_new (query);
394
395         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (widget), FALSE);
396         /* Show all accounts by default */
397         modest_folder_view_set_style (MODEST_FOLDER_VIEW (widget),
398                                       MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
399
400         return widget;
401 }
402
403 gboolean
404 modest_platform_run_alert_dialog (const gchar* prompt, 
405                                   gboolean is_question)
406 {
407         /* TODO */
408         return TRUE;
409 }
410
411 void 
412 modest_platform_connect_and_perform (GtkWindow *parent_window, 
413                                      gboolean force,
414                                      TnyAccount *account, 
415                                      ModestConnectedPerformer callback, 
416                                      gpointer user_data)
417 {
418         if (callback)
419                 callback (FALSE, NULL, parent_window, account, user_data);
420 }
421
422 void 
423 modest_platform_double_connect_and_perform (GtkWindow *parent_window, 
424                                             gboolean force,
425                                             TnyFolderStore *folder_store,
426                                             DoubleConnectionInfo *connect_info)
427 {
428         if (connect_info->callback)
429                 connect_info->callback (FALSE, NULL, parent_window, TNY_ACCOUNT (folder_store), connect_info->data);
430 }
431
432 void 
433 modest_platform_connect_and_perform_if_network_account (GtkWindow *parent_window, 
434                                                         gboolean force,
435                                                         TnyAccount *account,
436                                                         ModestConnectedPerformer callback, 
437                                                         gpointer user_data)
438 {
439         if (callback)
440                 callback (FALSE, NULL, parent_window, account, user_data);
441 }
442
443 void
444 modest_platform_connect_and_perform_if_network_folderstore (GtkWindow *parent_window, 
445                                                             TnyFolderStore *folder_store, 
446                                                             ModestConnectedPerformer callback, 
447                                                             gpointer user_data)
448 {
449         if (callback)
450                 callback (FALSE, NULL, parent_window, NULL, user_data);
451 }
452
453
454 void 
455 modest_platform_remove_new_mail_notifications (gboolean only_visuals)
456 {
457         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
458 }
459
460 gboolean 
461 modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
462 {
463         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
464         return TRUE;
465 }
466
467 gboolean 
468 modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
469                                                      const gchar *certificate)
470 {
471         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
472         return TRUE;
473 }
474
475 gint
476 modest_platform_run_rename_folder_dialog (GtkWindow *parent_window,
477                                           TnyFolderStore *parent_folder,
478                                           const gchar *suggested_name,
479                                           gchar **folder_name)
480 {
481         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
482         return GTK_RESPONSE_CANCEL;
483 }
484
485 void 
486 modest_platform_show_addressbook (GtkWindow *parent_window)
487 {
488         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
489 }
490
491 GtkWidget *
492 modest_platform_get_account_settings_dialog (ModestAccountSettings *settings)
493 {
494         ModestAccountSettingsDialog *dialog = modest_account_settings_dialog_new ();
495
496         modest_account_settings_dialog_set_account (dialog, settings);
497         return GTK_WIDGET (dialog);
498 }
499
500 GtkWidget *
501 modest_platform_get_account_settings_wizard (void)
502 {
503         GtkWidget *widget = modest_account_assistant_new (modest_runtime_get_account_mgr ());
504         return widget;
505 }
506
507
508
509 gboolean modest_platform_check_memory_low (ModestWindow *win,
510                                            gboolean visuals)
511 {
512         g_debug ("%s not implemented", __FUNCTION__);
513         return FALSE;
514 }
515
516 void 
517 modest_platform_run_folder_details_dialog (GtkWindow *parent_window,
518                                            TnyFolder *folder)
519 {
520         GtkWidget *dialog;
521         
522         /* Create dialog */
523         dialog = modest_details_dialog_new_with_folder (parent_window, folder);
524
525         /* Run dialog */
526         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
527                                      GTK_WINDOW (dialog), 
528                                      parent_window);
529         gtk_widget_show_all (dialog);
530
531         g_signal_connect_swapped (dialog, "response", 
532                                   G_CALLBACK (gtk_widget_destroy),
533                                   dialog);
534 }
535
536 void
537 modest_platform_run_header_details_dialog (GtkWindow *parent_window,
538                                            TnyHeader *header)
539 {
540         GtkWidget *dialog;
541         
542         /* Create dialog */
543         dialog = modest_details_dialog_new_with_header (parent_window, header);
544
545         /* Run dialog */
546         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
547                                      GTK_WINDOW (dialog),
548                                      parent_window);
549         gtk_widget_show_all (dialog);
550
551         g_signal_connect_swapped (dialog, "response", 
552                                   G_CALLBACK (gtk_widget_destroy),
553                                   dialog);
554 }
555
556 GtkWidget* 
557 modest_platform_create_move_to_dialog (GtkWindow *parent_window,
558                                        GtkWidget **folder_view)
559 {
560         GtkWidget *dialog, *folder_view_container;
561
562         dialog = gtk_dialog_new_with_buttons (_("mcen_ti_moveto_folders_title"),
563                                               GTK_WINDOW (parent_window),
564                                               GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR |
565                                               GTK_DIALOG_DESTROY_WITH_PARENT,
566                                               _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK,
567                                               _("mcen_bd_new"), MODEST_GTK_RESPONSE_NEW_FOLDER,
568                                               _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL,
569                                               NULL);
570
571         /* Create folder view */
572         *folder_view = modest_platform_create_folder_view (NULL);
573
574         /* Create pannable and add it to the dialog */
575         folder_view_container = gtk_scrolled_window_new (NULL, NULL);
576         gtk_scrolled_window_set_policy  (GTK_SCROLLED_WINDOW (*folder_view_container),
577                                          GTK_POLICY_AUTOMATIC,
578                                          GTK_POLICY_AUTOMATIC);
579         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), folder_view_container);
580         gtk_container_add (GTK_CONTAINER (folder_view_container), *folder_view);
581
582         gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 300);
583
584         return dialog;
585 }