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