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