1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
30 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
37 #include <modest-runtime.h>
38 #include <libgnomevfs/gnome-vfs.h>
39 #include <tny-fs-stream.h>
40 #include <tny-camel-account.h>
41 #include <tny-status.h>
42 #include <tny-camel-transport-account.h>
43 #include <tny-camel-imap-store-account.h>
44 #include <tny-camel-pop-store-account.h>
45 #include "modest-hildon-includes.h"
47 #include "modest-maemo-utils.h"
48 #include "modest-platform.h"
51 * For getting and tracking the Bluetooth name
53 #define BTNAME_SERVICE "org.bluez"
54 #define BTNAME_REQUEST_IF "org.bluez.Adapter"
55 #define BTNAME_SIGNAL_IF "org.bluez.Adapter"
56 #define BTNAME_REQUEST_PATH "/org/bluez/hci0"
57 #define BTNAME_SIGNAL_PATH "/org/bluez/hci0"
59 #define BTNAME_REQ_GET "GetName"
60 #define BTNAME_SIG_CHANGED "NameChanged"
62 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
63 "',member='" BTNAME_SIG_CHANGED "'"
65 GQuark modest_maemo_utils_get_supported_secure_authentication_error_quark (void)
67 return g_quark_from_static_string("modest-maemo-utils-get-supported-secure-authentication-error-quark");
71 modest_maemo_utils_menubar_to_menu (GtkUIManager *ui_manager)
77 g_return_val_if_fail (ui_manager, NULL);
79 /* Create new main menu */
80 main_menu = gtk_menu_new();
82 /* Get the menubar from the UI manager */
83 menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
85 iter = gtk_container_get_children (GTK_CONTAINER (menubar));
89 menu = GTK_WIDGET (iter->data);
90 gtk_widget_reparent(menu, main_menu);
92 iter = g_list_next (iter);
99 update_device_name_from_msg (DBusMessage *message)
102 DBusMessageIter iter;
104 dbus_error_init (&error);
106 if (dbus_set_error_from_message (&error, message)) {
107 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
108 dbus_error_free (&error);
110 const gchar *device_name;
111 if (!dbus_message_iter_init (message, &iter)) {
112 g_printerr ("modest: message did not have argument\n");
115 dbus_message_iter_get_basic (&iter, &device_name);
116 g_warning ("update device name: %s", device_name);
117 modest_conf_set_string (modest_runtime_get_conf(),
118 MODEST_CONF_DEVICE_NAME, device_name,
125 on_device_name_received (DBusPendingCall *call, void *user_data)
127 DBusMessage *message;
129 g_return_if_fail (dbus_pending_call_get_completed (call));
131 message = dbus_pending_call_steal_reply (call);
133 g_printerr ("modest: no reply on device name query\n");
137 update_device_name_from_msg (message);
138 dbus_message_unref (message);
142 static DBusHandlerResult
143 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
145 if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
146 update_device_name_from_msg (msg);
148 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
153 get_device_name_from_dbus ()
155 static DBusConnection *conn = NULL;
156 DBusMessage *request;
158 DBusPendingCall *call = NULL;
160 dbus_error_init (&error);
162 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
164 g_printerr ("modest: cannot get on the dbus: %s: %s\n",
165 error.name, error.message);
166 dbus_error_free (&error);
171 request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
172 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
174 /* should we free the connection? */
175 g_printerr ("modest: dbus_message_new_method_call failed\n");
178 dbus_message_set_auto_start (request, TRUE);
179 if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
180 dbus_pending_call_set_notify (call, on_device_name_received,
182 dbus_pending_call_unref (call);
184 dbus_message_unref (request);
186 dbus_connection_setup_with_g_main (conn, NULL);
187 dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
188 if (dbus_error_is_set(&error)) {
189 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
190 dbus_error_free (&error);
193 if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
194 g_printerr ("modest: dbus_connection_add_filter failed\n");
199 modest_maemo_utils_get_device_name (void)
201 get_device_name_from_dbus ();
205 modest_maemo_utils_folder_writable (const gchar *filename)
207 g_return_val_if_fail (filename, FALSE);
212 if (g_strncasecmp (filename, "obex", 4) != 0) {
213 GnomeVFSFileInfo folder_info;
215 folder = g_path_get_dirname (filename);
216 gnome_vfs_get_file_info (folder, &folder_info,
217 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
219 if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
220 (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
228 modest_maemo_utils_file_exists (const gchar *filename)
230 GnomeVFSURI *uri = NULL;
231 gboolean result = FALSE;
233 uri = gnome_vfs_uri_new (filename);
235 result = gnome_vfs_uri_exists (uri);
236 gnome_vfs_uri_unref (uri);
242 modest_maemo_utils_create_temp_stream (const gchar *extension, gchar **path)
244 TnyStream *tmp_fs_stream = NULL;
246 gchar *filepath = NULL;
247 gchar *template = NULL;
249 if (extension != NULL)
250 template = g_strdup_printf ("XXXXXX.%s", extension);
252 fd = g_file_open_tmp (template, &filepath, NULL);
257 g_message ("TODO BANNER: Error saving stream");
260 tmp_fs_stream = tny_fs_stream_new (fd);
262 return TNY_FS_STREAM (tmp_fs_stream);
272 } ModestGetSupportedAuthInfo;
274 static void on_camel_account_get_supported_secure_authentication_status (
275 GObject *self, TnyStatus *status, gpointer user_data)
277 /*ModestGetSupportedAuthInfo* info = (ModestGetSupportedAuthInfo*) user_data;*/
281 on_idle_secure_auth_finished (gpointer user_data)
283 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
284 /* Operation has finished, close the dialog. Control continues after
285 * gtk_dialog_run in modest_maemo_utils_get_supported_secure_authentication_methods() */
287 /* This is a GDK lock because we are an idle callback and
288 * the code below is or does Gtk+ code */
290 gdk_threads_enter(); /* CHECKED */
291 gtk_dialog_response (GTK_DIALOG (info->dialog), GTK_RESPONSE_ACCEPT);
292 gdk_threads_leave(); /* CHECKED */
298 on_camel_account_get_supported_secure_authentication (
299 TnyCamelAccount *self, gboolean cancelled,
300 TnyList *auth_types, GError **err,
304 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
305 g_return_if_fail (info);
307 /* Free everything if the actual action was canceled */
310 /* The operation was canceled and the ownership of the info given to us
311 * so that we could still check the cancel flag. */
312 g_slice_free (ModestGetSupportedAuthInfo, info);
317 /* TODO: Why is this a pointer to a pointer? We are not supposed to
319 if(err != NULL && *err != NULL)
321 if(info->error != NULL) g_error_free(info->error);
322 info->error = g_error_copy(*err);
326 printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
330 ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list ();
332 /* Get the enum value for the strings: */
333 GList *result = NULL;
334 TnyIterator* iter = tny_list_create_iterator(auth_types);
335 while (!tny_iterator_is_done(iter)) {
336 TnyPair *pair = TNY_PAIR(tny_iterator_get_current(iter));
337 const gchar *auth_name = NULL;
339 auth_name = tny_pair_get_name(pair);
340 g_object_unref (pair);
344 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
346 ModestAuthProtocol proto = modest_protocol_info_get_auth_protocol (auth_name);
347 if(proto != MODEST_PROTOCOL_AUTH_NONE)
348 result = g_list_prepend(result, GINT_TO_POINTER(proto));
350 tny_iterator_next(iter);
352 g_object_unref (iter);
354 modest_pair_list_free (pairs);
356 info->result = result;
359 printf("DEBUG: finished\n");
361 /* Close the dialog in a main thread */
362 g_idle_add(on_idle_secure_auth_finished, info);
366 static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
368 if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT)
370 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
371 g_return_if_fail(info);
372 /* This gives the ownership of the info to the worker thread. */
384 static UserData *user_data = NULL;
387 on_account_online (TnyCamelAccount *account, GError *err)
389 printf ("DEBUGa1: %s\n", __FUNCTION__);
392 printf("DEBUG: %s: error=\n %s\n", __FUNCTION__, err->message);
395 /* Allow the function that requested this callback to continue: */
396 /* TODO: Tinymail should really give us user_data with this callback. */
397 if (user_data && user_data->loop)
398 g_main_loop_quit (user_data->loop);
402 modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto,
403 const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error)
405 g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
407 /* We need a connection to get the capabilities; */
408 if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL))
412 result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
415 /* Create a TnyCamelAccount so we can use
416 * tny_camel_account_get_supported_secure_authentication(): */
417 TnyAccount * tny_account = NULL;
419 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
420 case MODEST_PROTOCOL_TRANSPORT_SMTP:
421 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
422 case MODEST_PROTOCOL_STORE_POP:
423 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
424 case MODEST_PROTOCOL_STORE_IMAP:
425 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
426 case MODEST_PROTOCOL_STORE_MAILDIR:
427 case MODEST_PROTOCOL_STORE_MBOX:
428 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
435 g_printerr ("%s could not create tny account.", __FUNCTION__);
439 /* Set proto, so that the prepare_func() vfunc will work when we call
441 /* TODO: Why isn't this done in account_new()? */
442 tny_account_set_proto (tny_account,
443 modest_protocol_info_get_transport_store_protocol_name(proto));
445 tny_account_set_hostname (tny_account, hostname);
446 /* Required for POP, at least */
447 tny_account_set_user (tny_account, username);
450 tny_account_set_port (tny_account, port);
452 /* Set the session for the account, so we can use it: */
453 ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
454 TnySessionCamel *session =
455 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
456 g_return_val_if_fail (session, NULL);
457 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
460 /* This blocks on the result: */
461 /* TODO: Fix tinymail to take user_data for the callback instead of using one static instance: */
463 g_slice_free (UserData, user_data);
467 user_data = g_slice_new0 (UserData);
468 user_data->loop = g_main_loop_new (NULL, FALSE /* not running */);
470 /* We get a warning if we don't do use tny_camel_account_set_online():
471 * GLIB CRITICAL ** camel-lite - camel_service_query_auth_types: assertion `service != NULL' failed.
473 tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (tny_account), TRUE, &on_account_online);
474 printf ("DEBUGa2: %s\n", __FUNCTION__);
476 /* This main loop will run until the idle handler has stopped it: */
477 printf ("DEBUG: %s: before g_main_loop_run()\n", __FUNCTION__);
479 g_main_loop_run (user_data->loop);
481 printf ("DEBUG: %s: after g_main_loop_run()\n", __FUNCTION__);
482 g_main_loop_unref (user_data->loop);
483 /* g_main_context_unref (context); */
485 g_slice_free (UserData, user_data);
489 /* Ask camel to ask the server, asynchronously: */
490 ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
492 info->cancel = FALSE;
494 info->progress = gtk_progress_bar_new();
495 /* TODO: Need logical_ID for the title: */
496 info->dialog = gtk_dialog_new_with_buttons(_("Authentication"),
497 parent_window, GTK_DIALOG_MODAL,
498 _("mcen_bd_dialog_cancel"),
501 //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100);
503 g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info);
505 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
506 gtk_label_new("Checking for supported authentication types..."));
507 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress);
508 gtk_widget_show_all(info->dialog);
509 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress));
511 printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
512 tny_camel_account_get_supported_secure_authentication (
513 TNY_CAMEL_ACCOUNT (tny_account),
514 on_camel_account_get_supported_secure_authentication,
515 on_camel_account_get_supported_secure_authentication_status,
518 gtk_dialog_run (GTK_DIALOG (info->dialog));
520 gtk_widget_destroy(info->dialog);
522 GList *result = info->result;
525 if(info->error != NULL)
526 g_propagate_error(error, info->error);
528 g_slice_free (ModestGetSupportedAuthInfo, info);
533 // Tell the caller that the operation was canceled so it can
536 modest_maemo_utils_get_supported_secure_authentication_error_quark(),
537 MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED,
538 "User has canceled query");
545 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
547 gchar *images_folder;
548 GtkFileFilter *file_filter;
549 GList *image_mimetypes_list;
552 g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
554 /* Set the default folder to images folder */
555 images_folder = g_build_filename (g_get_home_dir (),
556 MODEST_MAEMO_UTILS_MYDOCS_FOLDER,
557 MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
558 gtk_file_chooser_set_current_folder (chooser, images_folder);
559 g_free (images_folder);
561 /* Set the images mime filter */
562 file_filter = gtk_file_filter_new ();
563 #ifdef MODEST_HAVE_HILDON0_WIDGETS
564 image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
566 image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
568 for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
569 gtk_file_filter_add_mime_type (file_filter, node->data);
571 gtk_file_chooser_set_filter (chooser, file_filter);
572 #ifdef MODEST_HAVE_HILDON0_WIDGETS
573 osso_mime_types_list_free (image_mimetypes_list);
575 hildon_mime_types_list_free (image_mimetypes_list);
581 on_response (GtkDialog *dialog, gint response, gpointer user_data)
583 /* Just destroy the dialog: */
584 gtk_widget_destroy (GTK_WIDGET (dialog));
588 modest_maemo_show_information_note_and_forget (GtkWindow *parent_window, const gchar* message)
590 GtkDialog *dialog = GTK_DIALOG (hildon_note_new_information (parent_window, message));
592 /* Destroy the dialog when it is closed: */
593 g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (on_response), NULL);
594 gtk_widget_show (GTK_WIDGET (dialog));
599 on_hide (GtkDialog *dialog, gpointer user_data)
601 /* Just destroy the dialog: */
602 gtk_widget_destroy (GTK_WIDGET (dialog));
606 #if 0 /* Not used now. */
607 /* user_data for the idle callback: */
610 GtkWindow *parent_window;
612 } ModestIdleNoteInfo;
615 on_idle_show_information(gpointer user_data)
617 ModestIdleNoteInfo *info = (ModestIdleNoteInfo*)user_data;
619 modest_maemo_show_information_note_and_forget (info->parent_window, info->message);
621 g_free (info->message);
622 g_slice_free (ModestIdleNoteInfo, info);
624 return FALSE; /* Don't call this again. */
627 void modest_maemo_show_information_note_in_main_context_and_forget (GtkWindow *parent_window, const gchar* message)
629 ModestIdleNoteInfo *info = g_slice_new (ModestIdleNoteInfo);
630 info->parent_window = parent_window;
631 info->message = g_strdup (message);
633 g_idle_add (on_idle_show_information, info);
637 void modest_maemo_show_dialog_and_forget (GtkWindow *parent_window, GtkDialog *dialog)
639 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
641 /* Destroy the dialog when it is closed: */
642 g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (on_response), NULL);
643 gtk_widget_show (GTK_WIDGET (dialog));
649 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, gboolean thumbable)
651 g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
652 #ifdef MODEST_HAVE_HILDON1_WIDGETS
653 hildon_helper_set_thumb_scrollbar (win, thumbable);
654 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
658 modest_maemo_toggle_action_set_active_block_notify (GtkToggleAction *action, gboolean value)
660 GSList *proxies = NULL;
662 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
664 for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
665 proxies != NULL; proxies = g_slist_next (proxies)) {
666 GtkWidget *widget = (GtkWidget *) proxies->data;
667 gtk_action_block_activate_from (GTK_ACTION (action), widget);
670 gtk_toggle_action_set_active (action, value);
672 for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
673 proxies != NULL; proxies = g_slist_next (proxies)) {
674 GtkWidget *widget = (GtkWidget *) proxies->data;
675 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);