a28a8d985d792d34580f5cc7b45af787d5fb28d4
[modest] / src / maemo / modest-maemo-utils.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 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
33
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36 #include <glib.h>
37 #include <glib/gstdio.h>
38 #include <errno.h>
39 #include <string.h> /* for strlen */
40 #include <modest-runtime.h>
41 #include <libgnomevfs/gnome-vfs.h>
42 #include <tny-fs-stream.h>
43 #include <tny-camel-account.h>
44 #include <tny-status.h>
45 #include <tny-camel-transport-account.h>
46 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.h>
48 #include "modest-hildon-includes.h"
49
50 #include <modest-defs.h>
51 #include "modest-maemo-utils.h"
52 #include "modest-platform.h"
53
54 /*
55  * For getting and tracking the Bluetooth name
56  */
57 #define BTNAME_SERVICE                  "org.bluez"
58 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
59 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
60 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
61 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
62
63 #define BTNAME_REQ_GET                  "GetName"
64 #define BTNAME_SIG_CHANGED              "NameChanged"
65
66 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
67                           "',member='" BTNAME_SIG_CHANGED "'"
68
69
70 static osso_context_t *__osso_context = NULL; /* urgh global */
71
72 osso_context_t *
73 modest_maemo_utils_get_osso_context (void)
74 {
75         if (!__osso_context) 
76                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
77         
78         return __osso_context;
79 }
80
81 void
82 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
83 {
84         g_return_if_fail (osso_context);
85         __osso_context = osso_context;
86 }
87
88
89 GQuark
90 modest_maemo_utils_get_supported_secure_authentication_error_quark (void)
91 {
92         return g_quark_from_static_string("modest-maemo-utils-get-supported-secure-authentication-error-quark");
93 }
94
95 GtkWidget*
96 modest_maemo_utils_menubar_to_menu (GtkUIManager *ui_manager)
97 {
98         GtkWidget *main_menu;
99         GtkWidget *menubar;
100         GList *children, *iter;
101
102         g_return_val_if_fail (ui_manager, NULL);
103         
104         /* Create new main menu */
105         main_menu = gtk_menu_new();
106
107         /* Get the menubar from the UI manager */
108         menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
109
110         iter = children = gtk_container_get_children (GTK_CONTAINER (menubar));
111         while (iter) {
112                 GtkWidget *menu;
113
114                 menu = GTK_WIDGET (iter->data);
115                 gtk_widget_reparent(menu, main_menu);
116
117                 iter = g_list_next (iter);
118         }
119         g_list_free (children);
120         return main_menu;
121 }
122
123
124 static void
125 update_device_name_from_msg (DBusMessage *message)
126 {
127         DBusError error;
128         DBusMessageIter iter;
129
130         dbus_error_init (&error);
131
132         if (dbus_set_error_from_message (&error, message)) {
133                 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
134                 dbus_error_free (&error);
135         } else {
136                 const gchar *device_name;
137                 if (!dbus_message_iter_init (message, &iter)) {
138                         g_printerr ("modest: message did not have argument\n");
139                         return;
140                 }
141                 dbus_message_iter_get_basic (&iter, &device_name);
142                 modest_conf_set_string (modest_runtime_get_conf(),
143                                         MODEST_CONF_DEVICE_NAME, device_name,
144                                         NULL);
145         }
146 }
147
148
149 static void
150 on_device_name_received (DBusPendingCall *call, void *user_data)
151 {
152         DBusMessage *message;
153         
154         g_return_if_fail (dbus_pending_call_get_completed (call));
155         
156         message = dbus_pending_call_steal_reply (call);
157         if (!message) {
158                 g_printerr ("modest: no reply on device name query\n");
159                 return;
160         }
161
162         update_device_name_from_msg (message);
163         dbus_message_unref (message);
164 }
165
166
167 static DBusHandlerResult
168 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
169 {
170         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
171                 update_device_name_from_msg (msg);
172
173         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
174 }
175
176
177 static void
178 get_device_name_from_dbus ()
179 {
180         static DBusConnection *conn = NULL;
181         DBusMessage *request;
182         DBusError error;
183         DBusPendingCall *call = NULL;
184         
185         dbus_error_init (&error);
186         if (!conn) {
187                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
188                 if (!conn) {
189                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
190                                     error.name, error.message);
191                         dbus_error_free (&error);
192                         return;
193                 }
194         }
195         
196         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
197                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
198         if (!request) {
199                 /* should we free the connection? */
200                 g_printerr ("modest: dbus_message_new_method_call failed\n");
201                 return;
202         }
203         dbus_message_set_auto_start (request, TRUE);
204         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
205                 dbus_pending_call_set_notify (call, on_device_name_received,
206                                               NULL, NULL);
207                 dbus_pending_call_unref (call);
208         }
209         dbus_message_unref (request);
210         
211         dbus_connection_setup_with_g_main (conn, NULL);
212         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
213         if (dbus_error_is_set(&error)) {
214                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
215                 dbus_error_free (&error);
216         }
217
218         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
219                 g_printerr ("modest: dbus_connection_add_filter failed\n");
220 }
221
222
223 void
224 modest_maemo_utils_get_device_name (void)
225 {
226         get_device_name_from_dbus ();
227 }
228
229 gboolean 
230 modest_maemo_utils_folder_writable (const gchar *filename)
231 {
232         g_return_val_if_fail (filename, FALSE);
233
234         if (!filename)
235                 return FALSE;
236         
237         if (g_strncasecmp (filename, "obex", 4) != 0) {
238                 GnomeVFSFileInfo folder_info;
239                 gchar *folder;
240                 folder = g_path_get_dirname (filename);
241                 gnome_vfs_get_file_info (folder, &folder_info,
242                                          GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
243                 g_free (folder);
244                 if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
245                       (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
246                         return FALSE;
247                 }
248         }
249         return TRUE;
250 }
251
252 gboolean 
253 modest_maemo_utils_file_exists (const gchar *filename)
254 {
255         GnomeVFSURI *uri = NULL;
256         gboolean result = FALSE;
257
258         uri = gnome_vfs_uri_new (filename);
259         if (uri) {
260                 result = gnome_vfs_uri_exists (uri);
261                 gnome_vfs_uri_unref (uri);
262         }
263         return result;
264 }
265
266 TnyFsStream *
267 modest_maemo_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path)
268 {
269         gint fd;
270         gchar *filepath = NULL;
271         gchar *tmpdir;
272         guint hash_number;
273
274         /* hmmm... maybe we need a modest_text_utils_validate_file_name? */
275         g_return_val_if_fail (orig_name || strlen(orig_name) == 0, NULL);
276         if (strlen(orig_name) > 200) {
277                 g_warning ("%s: filename too long ('%s')",
278                            __FUNCTION__, orig_name);
279                 return NULL;
280         }
281         
282         if (g_strstr_len (orig_name, strlen(orig_name), "/") != NULL) {
283                 g_warning ("%s: filename contains '/' character(s) (%s)",
284                            __FUNCTION__, orig_name);
285                 return NULL;
286         }
287                 
288         /* make a random subdir under /tmp or /var/tmp */
289         if (hash_base != NULL) {
290                 hash_number = g_str_hash (hash_base);
291         } else {
292                 hash_number = (guint) random ();
293         }
294         tmpdir = g_strdup_printf ("%s/%u", g_get_tmp_dir (), hash_number);
295         if ((g_access (tmpdir, R_OK) == -1) && (g_mkdir (tmpdir, 0755) == -1)) {
296                 g_warning ("%s: failed to create dir '%s': %s",
297                            __FUNCTION__, tmpdir, g_strerror(errno));
298                 g_free (tmpdir);
299                 return NULL;
300         }
301
302         filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
303         /* don't overwrite if it already exists, even if it is writable */
304         if (modest_maemo_utils_file_exists (filepath)) {
305                 if (path!=NULL) {
306                         *path = filepath;
307                 } else {
308                         g_free (filepath);
309                 }
310                 g_free (tmpdir);
311                 return NULL;
312         } else {
313                 /* try to write the file there */
314                 fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
315                 if (fd == -1) {
316                         g_warning ("%s: failed to create '%s': %s",
317                                         __FUNCTION__, filepath, g_strerror(errno));                     
318                         g_free (filepath);
319                         g_free (tmpdir);
320                         return NULL;
321                 }
322         }
323
324         g_free (tmpdir);
325
326         if (path)
327                 *path = filepath;
328
329         return TNY_FS_STREAM (tny_fs_stream_new (fd));
330 }
331
332 typedef struct 
333 {
334         gboolean cancel;
335         GList *result;
336         GtkWidget* dialog;
337         GtkWidget* progress;
338         GError* error;
339 } ModestGetSupportedAuthInfo;
340
341 static void on_camel_account_get_supported_secure_authentication_status (
342         GObject *self, TnyStatus *status, gpointer user_data)
343 {
344         /*ModestGetSupportedAuthInfo* info = (ModestGetSupportedAuthInfo*) user_data;*/
345 }
346
347 static gboolean
348 on_idle_secure_auth_finished (gpointer user_data)
349 {
350         ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
351         /* Operation has finished, close the dialog. Control continues after
352          * gtk_dialog_run in modest_maemo_utils_get_supported_secure_authentication_methods() */
353
354         /* This is a GDK lock because we are an idle callback and
355          * the code below is or does Gtk+ code */
356
357         gdk_threads_enter(); /* CHECKED */
358         gtk_dialog_response (GTK_DIALOG (info->dialog), GTK_RESPONSE_ACCEPT);
359         gdk_threads_leave(); /* CHECKED */
360
361         return FALSE;
362 }
363
364 static void
365 on_camel_account_get_supported_secure_authentication (
366   TnyCamelAccount *self, gboolean cancelled,
367   TnyList *auth_types, GError *err, 
368   gpointer user_data)
369 {
370         ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
371         g_return_if_fail (info);
372         
373
374         /* Free everything if the actual action was canceled */
375         if (info->cancel)
376         {
377                 /* The operation was canceled and the ownership of the info given to us
378                  * so that we could still check the cancel flag. */
379                 g_slice_free (ModestGetSupportedAuthInfo, info);
380                 info = NULL;
381         }
382         else
383         {
384                 if (err)
385                 {
386                         if (info->error) {
387                                 g_error_free (info->error);
388                                 info->error = NULL;
389                         }
390                         
391                         info->error = g_error_copy (err);
392                 }
393
394                 if (!auth_types) {
395                         g_warning ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
396                 }
397                 else if (tny_list_get_length(auth_types) == 0) {
398                         g_warning ("DEBUG: %s: auth_types is an empty TnyList.\n", __FUNCTION__);
399                 } else
400                 {
401                         ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list ();
402   
403                         /* Get the enum value for the strings: */
404                         GList *result = NULL;
405                         TnyIterator* iter = tny_list_create_iterator(auth_types);
406                         while (!tny_iterator_is_done(iter)) {
407                                 TnyPair *pair = TNY_PAIR(tny_iterator_get_current(iter));
408                                 const gchar *auth_name = NULL;
409                                 if (pair) {
410                                         auth_name = tny_pair_get_name(pair);
411                                         g_object_unref (pair);
412                                         pair = NULL;
413                                 }
414
415                                 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
416
417                                 ModestAuthProtocol proto = modest_protocol_info_get_auth_protocol (auth_name);
418                                 if(proto != MODEST_PROTOCOL_AUTH_NONE)
419                                                 result = g_list_prepend(result, GINT_TO_POINTER(proto));
420
421                                 tny_iterator_next(iter);
422                         }
423                         g_object_unref (iter);
424
425                         modest_pair_list_free (pairs);
426         
427                         info->result = result;
428                 }
429
430                 printf("DEBUG: finished\n");
431                                 
432                 /* Close the dialog in a main thread */
433                 g_idle_add(on_idle_secure_auth_finished, info);
434         }
435 }
436
437 static void
438 on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
439 {
440         if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT)
441         {
442                 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
443                 g_return_if_fail(info);
444                 /* This gives the ownership of the info to the worker thread. */
445                 info->result = NULL;
446                 info->cancel = TRUE;
447         }
448 }
449
450 GList*
451 modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto, 
452         const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error)
453 {
454         g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
455         
456         /* We need a connection to get the capabilities; */
457         if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL))
458                 return NULL;
459          
460         /*
461         result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
462         */
463         
464         /* Create a TnyCamelAccount so we can use 
465          * tny_camel_account_get_supported_secure_authentication(): */
466         TnyAccount * tny_account = NULL;
467         switch (proto) {
468         case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
469         case MODEST_PROTOCOL_TRANSPORT_SMTP:
470                 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
471         case MODEST_PROTOCOL_STORE_POP:
472                 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
473         case MODEST_PROTOCOL_STORE_IMAP:
474                 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
475         case MODEST_PROTOCOL_STORE_MAILDIR:
476         case MODEST_PROTOCOL_STORE_MBOX:
477                 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
478         default:
479                 tny_account = NULL;
480         }
481
482         
483         if (!tny_account) {
484                 g_printerr ("%s could not create tny account.", __FUNCTION__);
485                 return NULL;
486         }
487         
488         /* Set proto, so that the prepare_func() vfunc will work when we call 
489          * set_session(): */
490          /* TODO: Why isn't this done in account_new()? */
491         tny_account_set_proto (tny_account,
492                                modest_protocol_info_get_transport_store_protocol_name(proto));
493
494         tny_account_set_hostname (tny_account, hostname);
495         /* Required for POP, at least */
496         tny_account_set_user (tny_account, username);
497                                
498         if(port > 0)
499                 tny_account_set_port (tny_account, port);
500                 
501         /* Set the session for the account, so we can use it: */
502         ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
503         TnySessionCamel *session = 
504                 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
505         g_return_val_if_fail (session, NULL);
506         tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
507         
508         
509         /* Ask camel to ask the server, asynchronously: */
510         ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
511         info->result = NULL;
512         info->cancel = FALSE;
513         info->error = NULL;
514         info->progress = gtk_progress_bar_new();
515         /* TODO: Need logical_ID for the title: */
516         info->dialog = gtk_dialog_new_with_buttons(_("Authentication"),
517                                                    parent_window, GTK_DIALOG_MODAL,
518                                                    _("mcen_bd_dialog_cancel"),
519                                                    GTK_RESPONSE_REJECT,
520                                                    NULL);
521         //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100);
522         
523         g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info);
524         
525         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
526                           gtk_label_new("Checking for supported authentication types..."));
527         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress);
528         gtk_widget_show_all(info->dialog);
529         gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress));
530         
531         printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
532         tny_camel_account_get_supported_secure_authentication (
533                 TNY_CAMEL_ACCOUNT (tny_account),
534                 on_camel_account_get_supported_secure_authentication,
535                 on_camel_account_get_supported_secure_authentication_status,
536                 info);
537
538         gtk_dialog_run (GTK_DIALOG (info->dialog));
539         
540         gtk_widget_destroy(info->dialog);
541                         
542         GList *result = info->result;
543         if (!info->cancel)
544         {
545                 if (info->error) {
546                         gchar * debug_url_string = tny_account_get_url_string  (tny_account);
547                         g_warning ("DEBUG: %s:\n  error: %s\n  account url: %s", __FUNCTION__, info->error->message, 
548                                 debug_url_string);
549                         g_free (debug_url_string);
550                         
551                         g_propagate_error(error, info->error);
552                         info->error = NULL;
553                 }
554
555                 g_slice_free (ModestGetSupportedAuthInfo, info);
556                 info = NULL;
557         }
558         else
559         {
560                 // Tell the caller that the operation was canceled so it can
561                 // make a difference
562                 g_set_error(error,
563                             modest_maemo_utils_get_supported_secure_authentication_error_quark(),
564                             MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED,
565                             "User has canceled query");
566         }
567
568         return result;
569 }
570
571 void
572 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
573 {
574         gchar *images_folder;
575         GtkFileFilter *file_filter;
576         GList *image_mimetypes_list;
577         GList *node;
578
579         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
580
581         /* Set the default folder to images folder */
582         images_folder = g_build_filename (g_get_home_dir (), 
583                                           MODEST_MAEMO_UTILS_MYDOCS_FOLDER,
584                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
585         gtk_file_chooser_set_current_folder (chooser, images_folder);
586         g_free (images_folder);
587
588         /* Set the images mime filter */
589         file_filter = gtk_file_filter_new ();
590 #ifdef MODEST_HAVE_HILDON0_WIDGETS
591         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
592 #else
593         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
594 #endif
595         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
596                 gtk_file_filter_add_mime_type (file_filter, node->data);
597         }
598         gtk_file_chooser_set_filter (chooser, file_filter);
599 #ifdef MODEST_HAVE_HILDON0_WIDGETS
600         osso_mime_types_list_free (image_mimetypes_list);
601 #else
602         hildon_mime_types_list_free (image_mimetypes_list);
603 #endif
604
605 }
606
607 #if 0
608 static void
609 on_hide (GtkDialog *dialog, gpointer user_data)
610 {
611         /* Just destroy the dialog: */
612         gtk_widget_destroy (GTK_WIDGET (dialog));
613 }
614 #endif
615
616 #if 0 /* Not used now. */
617 /* user_data for the idle callback: */
618 typedef struct 
619 {
620         GtkWindow *parent_window;
621         gchar *message;
622 } ModestIdleNoteInfo;
623
624 static gboolean
625 on_idle_show_information(gpointer user_data)
626 {
627         ModestIdleNoteInfo *info = (ModestIdleNoteInfo*)user_data;
628         
629         modest_maemo_show_information_note_and_forget (info->parent_window, info->message);
630         
631         g_free (info->message);
632         g_slice_free (ModestIdleNoteInfo, info);
633         
634         return FALSE; /* Don't call this again. */
635 }
636
637 void 
638 modest_maemo_show_information_note_in_main_context_and_forget (GtkWindow *parent_window, 
639                                                                const gchar* message)
640 {
641         ModestIdleNoteInfo *info = g_slice_new (ModestIdleNoteInfo);
642         info->parent_window = parent_window;
643         info->message = g_strdup (message);
644         
645         g_idle_add (on_idle_show_information, info);
646 }
647 #endif
648
649 void 
650 modest_maemo_show_dialog_and_forget (GtkWindow *parent_window, 
651                                      GtkDialog *dialog)
652 {
653         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
654         
655         /* Destroy the dialog when it is closed: */
656         g_signal_connect_swapped (dialog, 
657                                   "response", 
658                                   G_CALLBACK (gtk_widget_destroy), 
659                                   dialog);
660
661         gtk_widget_show (GTK_WIDGET (dialog));
662 }
663
664
665
666 void
667 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
668                                       gboolean thumbable)
669 {
670         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
671 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
672         hildon_helper_set_thumb_scrollbar (win, thumbable);
673 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
674 }
675
676 void
677 modest_maemo_toggle_action_set_active_block_notify (GtkToggleAction *action, gboolean value)
678 {
679         GSList *proxies = NULL;
680
681         g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
682
683         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
684              proxies != NULL; proxies = g_slist_next (proxies)) {
685                 GtkWidget *widget = (GtkWidget *) proxies->data;
686                 gtk_action_block_activate_from (GTK_ACTION (action), widget);
687         }
688
689         gtk_toggle_action_set_active (action, value);
690
691         for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
692              proxies != NULL; proxies = g_slist_next (proxies)) {
693                 GtkWidget *widget = (GtkWidget *) proxies->data;
694                 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
695         }
696
697 }
698
699
700 FILE*
701 modest_maemo_open_mcc_mapping_file (void)
702 {
703         FILE* result;
704         
705         const gchar* path;
706         const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
707         const gchar* path2 = MODEST_MCC_MAPPING;
708         
709         if (access(path1, R_OK) == 0) 
710                 path = path1;
711         else if (access(path2, R_OK) == 0)
712                 path = path2;
713         else {
714                 g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
715                            __FUNCTION__, path1, path2);
716                 return NULL;
717         }
718         
719         result = fopen (path, "r");
720         if (!result) {
721                 g_warning ("%s: error opening mapping file '%s': %s",
722                            __FUNCTION__, path, strerror(errno));
723                 return NULL;
724         }
725         return result;
726 }
727