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