GNOME version compiles again. It does not link yet though, because there are some...
[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 #include "gnome/modest-gnome-global-settings-dialog.h"
40 #include "widgets/modest-default-account-settings-dialog.h"
41 #include "gnome/modest-account-assistant.h"
42 #include "gnome/modest-gnome-sort-dialog.h"
43 #include "widgets/modest-details-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 *suggested_parent,
153                                        gchar *suggested_name,
154                                        gchar **folder_name,
155                                        TnyFolderStore **parent_folder)
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         if (parent_folder != NULL) {
187                 parent_folder = suggested_parent?g_object_ref (suggested_parent): NULL;
188         }
189
190         return result;
191 }
192
193
194 gint
195 modest_platform_run_confirmation_dialog (GtkWindow *parent_window,
196                                          const gchar *msg)
197 {
198         gint response;
199         GtkWidget *dialog;
200
201         dialog = gtk_message_dialog_new (parent_window,
202                                          GTK_DIALOG_MODAL,
203                                          GTK_MESSAGE_QUESTION,
204                                          GTK_BUTTONS_OK_CANCEL,
205                                          msg);
206
207         response = gtk_dialog_run (GTK_DIALOG(dialog));
208         gtk_widget_destroy (dialog);
209         
210         return response;
211 }
212
213 gint
214 modest_platform_run_confirmation_dialog_with_buttons (GtkWindow *parent_window,
215                                                       const gchar *message,
216                                                       const gchar *button_accept,
217                                                       const gchar *button_cancel)
218 {
219         gint response;
220         GtkWidget *dialog;
221
222         dialog = gtk_dialog_new_with_buttons (message,
223                                               parent_window,
224                                               GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
225                                               button_accept,
226                                               GTK_RESPONSE_ACCEPT,
227                                               button_cancel,
228                                               GTK_RESPONSE_CANCEL,
229                                               NULL);
230
231         response = gtk_dialog_run (GTK_DIALOG(dialog));
232         gtk_widget_destroy (dialog);
233         
234         return response;
235 }
236
237 void
238 modest_platform_run_information_dialog (GtkWindow *parent_window,
239                                         const gchar *message,
240                                         gboolean block)
241 {
242         GtkWidget *dialog;
243
244         dialog = gtk_message_dialog_new (parent_window,
245                                          GTK_DIALOG_MODAL,
246                                          GTK_MESSAGE_INFO,
247                                          GTK_BUTTONS_OK,
248                                          message);
249
250         gtk_dialog_run (GTK_DIALOG (dialog));
251         gtk_widget_destroy (dialog);
252         
253 }
254
255 gboolean modest_platform_connect_and_wait (GtkWindow *parent_window, TnyAccount *account)
256 {
257         /* TODO: Do something with network-manager? 
258            Otherwise, maybe it is safe to assume that we would already be online if we could be. */
259         return TRUE;
260 }
261
262 gboolean modest_platform_connect_and_wait_if_network_account (GtkWindow *parent_window, TnyAccount *account)
263 {
264         /* TODO: Do something with network-manager? 
265            Otherwise, maybe it is safe to assume that we would already be online if we could be. */
266         return TRUE;
267 }
268
269
270 void
271 modest_platform_connect_if_remote_and_perform (GtkWindow *parent_window, 
272                                                gboolean force,
273                                                TnyFolderStore *folder_store, 
274                                                ModestConnectedPerformer callback, 
275                                                gpointer user_data)
276 {
277         TnyAccount *account = NULL;
278         
279         if (!folder_store) {
280                 /* We promise to instantly perform the callback, so ... */
281                 if (callback) {
282                         callback (FALSE, NULL, parent_window, NULL, user_data);
283                 }
284                 return; 
285                 
286                 /* Original comment: Maybe it is something local. */
287                 /* PVH's comment: maybe we should KNOW this in stead of assuming? */
288                 
289         } else if (TNY_IS_FOLDER (folder_store)) {
290                 /* Get the folder's parent account: */
291                 account = tny_folder_get_account(TNY_FOLDER (folder_store));
292         } else if (TNY_IS_ACCOUNT (folder_store)) {
293                 /* Use the folder store as an account: */
294                 account = TNY_ACCOUNT (folder_store);
295         }
296  
297         if (tny_account_get_account_type (account) == TNY_ACCOUNT_TYPE_STORE) {
298                 if (!TNY_IS_CAMEL_POP_STORE_ACCOUNT (account) &&
299                     !TNY_IS_CAMEL_IMAP_STORE_ACCOUNT (account)) {
300                         
301                         /* This IS a local account like a maildir account, which does not require 
302                          * a connection. (original comment had a vague assumption in its language
303                          * usage. There's no assuming needed, this IS what it IS: a local account), */
304  
305                         /* We promise to instantly perform the callback, so ... */
306                         if (callback) {
307                                 callback (FALSE, NULL, parent_window, account, user_data);
308                         }
309                         
310                         return;
311                 }
312         }
313  
314         modest_platform_connect_and_perform (parent_window, force, account, callback, user_data);
315  
316         return;
317 }
318
319
320 gboolean modest_platform_set_update_interval (guint minutes)
321 {
322         /* TODO. */
323         return FALSE;
324 }
325
326 GtkWidget *
327 modest_platform_create_sort_dialog (GtkWindow *parent_window)
328 {
329         return modest_gnome_sort_dialog_new (parent_window);
330 }
331
332 GtkWidget *
333 modest_platform_get_global_settings_dialog ()
334 {
335         return modest_gnome_global_settings_dialog_new ();
336 }
337
338 void
339 modest_platform_push_email_notification(void)
340 {
341         /* TODO: implement this */
342         g_print ("--------------- NEW MESSAGE ARRIVED ---------------\n");
343 }
344
345 void 
346 modest_platform_on_new_headers_received (TnyList *header_list,
347                                          gboolean show_visual)
348 {
349         /* TODO: implement this */
350         g_print ("--------------- NEW MESSAGE ARRIVED ---------------\n");
351 }
352
353
354
355 void
356 modest_platform_show_help (GtkWindow *parent_window, const gchar *help_id)
357 {
358         return; /* TODO */
359 }
360
361 void 
362 modest_platform_information_banner (GtkWidget *widget,
363                                     const gchar *icon_name,
364                                     const gchar *text)
365 {
366         g_message ("NOT IMPLEMENTED");;
367 }
368
369 void
370 modest_platform_information_banner_with_timeout (GtkWidget *widget,
371                                                  const gchar *icon_name,
372                                                  const gchar *text,
373                                                  gint timeout)
374 {
375         g_message ("NOT IMPLEMENTED");;
376 }
377
378 GtkWidget *
379 modest_platform_animation_banner (GtkWidget *widget,
380                                   const gchar *icon_name,
381                                   const gchar *text)
382 {
383         g_message ("NOT IMPLEMENTED");
384         return NULL;
385 }
386
387
388 void
389 modest_platform_show_search_messages (GtkWindow *parent_window)
390 {
391         g_message ("NOT IMPLEMENTED");;
392 }
393
394 GtkWidget *
395 modest_platform_create_folder_view (TnyFolderStoreQuery *query)
396 {
397         GtkWidget *widget = modest_folder_view_new (query);
398
399         gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (widget), FALSE);
400         /* Show all accounts by default */
401         modest_folder_view_set_style (MODEST_FOLDER_VIEW (widget),
402                                       MODEST_FOLDER_VIEW_STYLE_SHOW_ALL);
403
404         return widget;
405 }
406
407 gboolean
408 modest_platform_run_alert_dialog (const gchar* prompt, 
409                                   gboolean is_question)
410 {
411         /* TODO */
412         return TRUE;
413 }
414
415 void 
416 modest_platform_connect_and_perform (GtkWindow *parent_window, 
417                                      gboolean force,
418                                      TnyAccount *account, 
419                                      ModestConnectedPerformer callback, 
420                                      gpointer user_data)
421 {
422         if (callback)
423                 callback (FALSE, NULL, parent_window, account, user_data);
424 }
425
426 void 
427 modest_platform_double_connect_and_perform (GtkWindow *parent_window, 
428                                             gboolean force,
429                                             TnyFolderStore *folder_store,
430                                             DoubleConnectionInfo *connect_info)
431 {
432         if (connect_info->callback)
433                 connect_info->callback (FALSE, NULL, parent_window, TNY_ACCOUNT (folder_store), connect_info->data);
434 }
435
436 void 
437 modest_platform_connect_and_perform_if_network_account (GtkWindow *parent_window, 
438                                                         gboolean force,
439                                                         TnyAccount *account,
440                                                         ModestConnectedPerformer callback, 
441                                                         gpointer user_data)
442 {
443         if (callback)
444                 callback (FALSE, NULL, parent_window, account, user_data);
445 }
446
447 void
448 modest_platform_connect_and_perform_if_network_folderstore (GtkWindow *parent_window, 
449                                                             TnyFolderStore *folder_store, 
450                                                             ModestConnectedPerformer callback, 
451                                                             gpointer user_data)
452 {
453         if (callback)
454                 callback (FALSE, NULL, parent_window, NULL, user_data);
455 }
456
457
458 void 
459 modest_platform_remove_new_mail_notifications (gboolean only_visuals)
460 {
461         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
462 }
463
464 gboolean 
465 modest_platform_check_and_wait_for_account_is_online(TnyAccount *account)
466 {
467         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
468         return TRUE;
469 }
470
471 gboolean 
472 modest_platform_run_certificate_confirmation_dialog (const gchar* server_name,
473                                                      const gchar *certificate)
474 {
475         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
476         return TRUE;
477 }
478
479 gint
480 modest_platform_run_rename_folder_dialog (GtkWindow *parent_window,
481                                           TnyFolderStore *parent_folder,
482                                           const gchar *suggested_name,
483                                           gchar **folder_name)
484 {
485         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
486         return GTK_RESPONSE_CANCEL;
487 }
488
489 void 
490 modest_platform_show_addressbook (GtkWindow *parent_window)
491 {
492         g_message ("NOT IMPLEMENTED %s", __FUNCTION__);
493 }
494
495 GtkWidget *
496 modest_platform_get_account_settings_dialog (ModestAccountSettings *settings)
497 {
498         ModestAccountSettingsDialog *dialog = modest_default_account_settings_dialog_new ();
499
500         modest_account_settings_dialog_load_settings (dialog, settings);
501         return GTK_WIDGET (dialog);
502 }
503
504 GtkWidget *
505 modest_platform_get_account_settings_wizard (void)
506 {
507         GtkWidget *widget = modest_account_assistant_new (modest_runtime_get_account_mgr ());
508         return widget;
509 }
510
511
512
513 gboolean modest_platform_check_memory_low (ModestWindow *win,
514                                            gboolean visuals)
515 {
516         g_debug ("%s not implemented", __FUNCTION__);
517         return FALSE;
518 }
519
520 void 
521 modest_platform_run_folder_details_dialog (GtkWindow *parent_window,
522                                            TnyFolder *folder)
523 {
524         GtkWidget *dialog;
525         
526         /* Create dialog */
527         dialog = modest_details_dialog_new_with_folder (parent_window, folder);
528
529         /* Run dialog */
530         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
531                                      GTK_WINDOW (dialog), 
532                                      parent_window);
533         gtk_widget_show_all (dialog);
534
535         g_signal_connect_swapped (dialog, "response", 
536                                   G_CALLBACK (gtk_widget_destroy),
537                                   dialog);
538 }
539
540 void
541 modest_platform_run_header_details_dialog (GtkWindow *parent_window,
542                                            TnyHeader *header)
543 {
544         GtkWidget *dialog;
545         
546         /* Create dialog */
547         dialog = modest_details_dialog_new_with_header (parent_window, header);
548
549         /* Run dialog */
550         modest_window_mgr_set_modal (modest_runtime_get_window_mgr (), 
551                                      GTK_WINDOW (dialog),
552                                      parent_window);
553         gtk_widget_show_all (dialog);
554
555         g_signal_connect_swapped (dialog, "response", 
556                                   G_CALLBACK (gtk_widget_destroy),
557                                   dialog);
558 }
559
560 GtkWidget* 
561 modest_platform_create_move_to_dialog (GtkWindow *parent_window,
562                                        GtkWidget **folder_view)
563 {
564         GtkWidget *dialog, *folder_view_container;
565
566         dialog = gtk_dialog_new_with_buttons (_("mcen_ti_moveto_folders_title"),
567                                               GTK_WINDOW (parent_window),
568                                               GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR |
569                                               GTK_DIALOG_DESTROY_WITH_PARENT,
570                                               _("mcen_bd_dialog_ok"), GTK_RESPONSE_OK,
571                                               _("mcen_bd_new"), MODEST_GTK_RESPONSE_NEW_FOLDER,
572                                               _("mcen_bd_dialog_cancel"), GTK_RESPONSE_CANCEL,
573                                               NULL);
574
575         /* Create folder view */
576         *folder_view = modest_platform_create_folder_view (NULL);
577
578         /* Create pannable and add it to the dialog */
579         folder_view_container = gtk_scrolled_window_new (NULL, NULL);
580         gtk_scrolled_window_set_policy  (GTK_SCROLLED_WINDOW (folder_view_container),
581                                          GTK_POLICY_AUTOMATIC,
582                                          GTK_POLICY_AUTOMATIC);
583         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), folder_view_container);
584         gtk_container_add (GTK_CONTAINER (folder_view_container), *folder_view);
585
586         gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 300);
587
588         return dialog;
589 }