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"
50 * For getting and tracking the Bluetooth name
52 #define BTNAME_SERVICE "org.bluez"
53 #define BTNAME_REQUEST_IF "org.bluez.Adapter"
54 #define BTNAME_SIGNAL_IF "org.bluez.Adapter"
55 #define BTNAME_REQUEST_PATH "/org/bluez/hci0"
56 #define BTNAME_SIGNAL_PATH "/org/bluez/hci0"
58 #define BTNAME_REQ_GET "GetName"
59 #define BTNAME_SIG_CHANGED "NameChanged"
61 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
62 "',member='" BTNAME_SIG_CHANGED "'"
64 GQuark modest_maemo_utils_get_supported_secure_authentication_error_quark (void)
66 return g_quark_from_static_string("modest-maemo-utils-get-supported-secure-authentication-error-quark");
70 modest_maemo_utils_menubar_to_menu (GtkUIManager *ui_manager)
76 g_return_val_if_fail (ui_manager, NULL);
78 /* Create new main menu */
79 main_menu = gtk_menu_new();
81 /* Get the menubar from the UI manager */
82 menubar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
84 iter = gtk_container_get_children (GTK_CONTAINER (menubar));
88 menu = GTK_WIDGET (iter->data);
89 gtk_widget_reparent(menu, main_menu);
91 iter = g_list_next (iter);
98 update_device_name_from_msg (DBusMessage *message)
101 DBusMessageIter iter;
103 dbus_error_init (&error);
105 if (dbus_set_error_from_message (&error, message)) {
106 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
107 dbus_error_free (&error);
109 const gchar *device_name;
110 if (!dbus_message_iter_init (message, &iter)) {
111 g_printerr ("modest: message did not have argument\n");
114 dbus_message_iter_get_basic (&iter, &device_name);
115 g_warning ("update device name: %s", device_name);
116 modest_conf_set_string (modest_runtime_get_conf(),
117 MODEST_CONF_DEVICE_NAME, device_name,
124 on_device_name_received (DBusPendingCall *call, void *user_data)
126 DBusMessage *message;
128 g_return_if_fail (dbus_pending_call_get_completed (call));
130 message = dbus_pending_call_steal_reply (call);
132 g_printerr ("modest: no reply on device name query\n");
136 update_device_name_from_msg (message);
137 dbus_message_unref (message);
141 static DBusHandlerResult
142 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
144 if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
145 update_device_name_from_msg (msg);
147 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152 get_device_name_from_dbus ()
154 static DBusConnection *conn = NULL;
155 DBusMessage *request;
157 DBusPendingCall *call = NULL;
159 dbus_error_init (&error);
161 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
163 g_printerr ("modest: cannot get on the dbus: %s: %s\n",
164 error.name, error.message);
165 dbus_error_free (&error);
170 request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
171 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
173 /* should we free the connection? */
174 g_printerr ("modest: dbus_message_new_method_call failed\n");
177 dbus_message_set_auto_start (request, TRUE);
178 if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
179 dbus_pending_call_set_notify (call, on_device_name_received,
181 dbus_pending_call_unref (call);
183 dbus_message_unref (request);
185 dbus_connection_setup_with_g_main (conn, NULL);
186 dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
187 if (dbus_error_is_set(&error)) {
188 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
189 dbus_error_free (&error);
192 if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
193 g_printerr ("modest: dbus_connection_add_filter failed\n");
198 modest_maemo_utils_get_device_name (void)
200 get_device_name_from_dbus ();
204 modest_maemo_utils_folder_writable (const gchar *filename)
206 if (g_strncasecmp (filename, "obex", 4) != 0) {
207 GnomeVFSFileInfo folder_info;
209 folder = g_path_get_dirname (filename);
210 gnome_vfs_get_file_info (folder, &folder_info,
211 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
213 if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
214 (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
222 modest_maemo_utils_file_exists (const gchar *filename)
224 GnomeVFSURI *uri = NULL;
225 gboolean result = FALSE;
227 uri = gnome_vfs_uri_new (filename);
229 result = gnome_vfs_uri_exists (uri);
230 gnome_vfs_uri_unref (uri);
236 modest_maemo_utils_create_temp_stream (const gchar *extension, gchar **path)
238 TnyStream *tmp_fs_stream = NULL;
240 gchar *filepath = NULL;
241 gchar *template = NULL;
243 if (extension != NULL)
244 template = g_strdup_printf ("XXXXXX.%s", extension);
246 fd = g_file_open_tmp (template, &filepath, NULL);
251 g_message ("TODO BANNER: Error saving stream");
254 tmp_fs_stream = tny_fs_stream_new (fd);
256 return TNY_FS_STREAM (tmp_fs_stream);
267 } ModestGetSupportedAuthInfo;
269 static void on_camel_account_get_supported_secure_authentication_status (
270 GObject *self, TnyStatus *status, gpointer user_data)
272 /*ModestGetSupportedAuthInfo* info = (ModestGetSupportedAuthInfo*) user_data;*/
276 on_camel_account_get_supported_secure_authentication (
277 TnyCamelAccount *self, gboolean cancelled,
278 TnyList *auth_types, GError **err,
282 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
283 g_return_if_fail (info);
285 /* Free everything if the actual action was canceled */
288 g_slice_free (ModestGetSupportedAuthInfo, info);
293 /* TODO: Why is this a pointer to a pointer? We are not supposed to
295 if(err != NULL && *err != NULL)
297 if(info->error != NULL) g_error_free(info->error);
298 info->error = g_error_copy(*err);
302 printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
303 info->finished = TRUE; /* We are blocking, waiting for this. */
307 ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list ();
309 /* Get the enum value for the strings: */
310 GList *result = NULL;
311 TnyIterator* iter = tny_list_create_iterator(auth_types);
312 while (!tny_iterator_is_done(iter)) {
313 const gchar *auth_name = tny_pair_get_name(TNY_PAIR(tny_iterator_get_current(iter)));
314 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
315 ModestPair *matching = modest_pair_list_find_by_first_as_string (pairs,
319 result = g_list_append (result, GINT_TO_POINTER((ModestConnectionProtocol)matching->first));
321 tny_iterator_next(iter);
324 g_object_unref(auth_types);
326 modest_pair_list_free (pairs);
328 info->result = result;
329 printf("DEBUG: finished\n");
330 info->finished = TRUE; /* We are blocking, waiting for this. */
333 static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
335 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
336 g_return_if_fail(info);
337 /* We are blocking */
342 GList* modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto,
343 const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error)
345 g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
348 result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
351 /* Create a TnyCamelAccount so we can use
352 * tny_camel_account_get_supported_secure_authentication(): */
353 TnyAccount * tny_account = NULL;
355 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
356 case MODEST_PROTOCOL_TRANSPORT_SMTP:
357 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
358 case MODEST_PROTOCOL_STORE_POP:
359 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
360 case MODEST_PROTOCOL_STORE_IMAP:
361 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
362 case MODEST_PROTOCOL_STORE_MAILDIR:
363 case MODEST_PROTOCOL_STORE_MBOX:
364 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
370 g_printerr ("%s could not create tny account.", __FUNCTION__);
374 /* Set proto, so that the prepare_func() vfunc will work when we call
376 /* TODO: Why isn't this done in account_new()? */
377 tny_account_set_proto (tny_account,
378 modest_protocol_info_get_transport_store_protocol_name(proto));
380 tny_account_set_hostname (tny_account, hostname);
381 /* Required for POP, at least */
382 tny_account_set_user (tny_account, username);
384 /* Set the session for the account, so we can use it: */
385 ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
386 TnySessionCamel *session =
387 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
388 g_return_val_if_fail (session, NULL);
389 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
392 tny_account_set_port (tny_account, port);
395 /* Ask camel to ask the server, asynchronously: */
396 ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
397 info->finished = FALSE;
399 info->cancel = FALSE;
401 info->progress = gtk_progress_bar_new();
402 info->dialog = gtk_dialog_new_with_buttons(_("Authentication"),
403 parent_window, GTK_DIALOG_MODAL,
407 //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100);
409 g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info);
411 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
412 gtk_label_new("Checking for supported authentication types..."));
413 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress);
414 gtk_widget_show_all(info->dialog);
415 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress));
417 printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
418 tny_camel_account_get_supported_secure_authentication (
419 TNY_CAMEL_ACCOUNT (tny_account),
420 on_camel_account_get_supported_secure_authentication,
421 on_camel_account_get_supported_secure_authentication_status,
424 /* Block until the callback has been called,
425 * driving the main context, so that the (idle handler) callback can be
426 * called, and so that our dialog is clickable: */
427 while (!(info->finished) && !(info->cancel)) {
428 gtk_main_iteration_do(FALSE);
431 gtk_widget_destroy(info->dialog);
433 GList *result = info->result;
436 if(info->error != NULL)
437 g_propagate_error(error, info->error);
439 g_slice_free (ModestGetSupportedAuthInfo, info);
444 // Tell the caller that the operation was canceled so it can
447 modest_maemo_utils_get_supported_secure_authentication_error_quark(),
448 MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED,
449 "User has canceled query");
456 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
458 gchar *images_folder;
459 GtkFileFilter *file_filter;
460 GList *image_mimetypes_list;
463 g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
465 /* Set the default folder to images folder */
466 images_folder = g_build_filename (g_get_home_dir (),
467 MODEST_MAEMO_UTILS_MYDOCS_FOLDER,
468 MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
469 gtk_file_chooser_set_current_folder (chooser, images_folder);
470 g_free (images_folder);
472 /* Set the images mime filter */
473 file_filter = gtk_file_filter_new ();
474 #ifdef MODEST_HAVE_HILDON0_WIDGETS
475 image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
477 image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
479 for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
480 gtk_file_filter_add_mime_type (file_filter, node->data);
482 gtk_file_chooser_set_filter (chooser, file_filter);
483 #ifdef MODEST_HAVE_HILDON0_WIDGETS
484 osso_mime_types_list_free (image_mimetypes_list);
486 hildon_mime_types_list_free (image_mimetypes_list);