1 /* Copyright (c) 2007, 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.
31 #include <glib/gstdio.h>
33 #include <string.h> /* for strlen */
34 #include <modest-runtime.h>
35 #include <libgnomevfs/gnome-vfs.h>
36 #include <tny-fs-stream.h>
37 #include <tny-camel-account.h>
38 #include <tny-status.h>
39 #include <tny-camel-send-queue.h>
41 #include <modest-defs.h>
42 #include "modest-utils.h"
43 #include "modest-platform.h"
44 #include <modest-account-protocol.h>
45 #include "modest-account-mgr-helpers.h"
46 #include "modest-text-utils.h"
47 #include <modest-local-folder-info.h>
48 #include "widgets/modest-header-view.h"
49 #include "widgets/modest-main-window.h"
50 #include "modest-widget-memory.h"
51 #include "widgets/modest-sort-criterium-view.h"
52 #ifdef MODEST_TOOLKIT_HILDON2
53 #include "modest-header-window.h"
58 modest_utils_get_supported_secure_authentication_error_quark (void)
60 return g_quark_from_static_string("modest-utils-get-supported-secure-authentication-error-quark");
64 modest_utils_folder_writable (const gchar *filename)
66 g_return_val_if_fail (filename, FALSE);
71 if (g_ascii_strncasecmp (filename, "obex", 4) != 0) {
72 GnomeVFSFileInfo *folder_info = NULL;
73 GnomeVFSResult result = GNOME_VFS_OK;
74 GnomeVFSURI *uri = NULL;
75 GnomeVFSURI *folder_uri = NULL;
77 uri = gnome_vfs_uri_new (filename);
78 folder_uri = gnome_vfs_uri_get_parent (uri);
80 if (folder_uri != NULL) {
81 folder_info = gnome_vfs_file_info_new ();
82 result = gnome_vfs_get_file_info_uri (folder_uri, folder_info,
83 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
84 gnome_vfs_uri_unref (folder_uri);
86 gnome_vfs_uri_unref (uri);
88 if (folder_uri == NULL)
91 if ((result != GNOME_VFS_OK) ||
92 (!((folder_info->permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
93 (folder_info->permissions & GNOME_VFS_PERM_USER_WRITE)))) {
95 gnome_vfs_file_info_unref (folder_info);
98 gnome_vfs_file_info_unref (folder_info);
104 modest_utils_file_exists (const gchar *filename)
106 gboolean result = FALSE;
109 g_return_val_if_fail (filename, FALSE);
111 path = strstr (filename, "file://");
113 path = (gchar *) filename;
115 path = (gchar *) filename + strlen ("file://");
117 if (g_access (path, F_OK) == 0)
124 modest_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path)
127 gchar *filepath = NULL;
131 /* hmmm... maybe we need a modest_text_utils_validate_file_name? */
132 g_return_val_if_fail (orig_name && strlen(orig_name) != 0, NULL);
134 if (strlen(orig_name) > 200) {
135 g_warning ("%s: filename too long ('%s')",
136 __FUNCTION__, orig_name);
140 if (g_strstr_len (orig_name, strlen (orig_name), "/") != NULL) {
141 g_warning ("%s: filename contains '/' character(s) (%s)",
142 __FUNCTION__, orig_name);
146 /* make a random subdir under /tmp or /var/tmp */
147 if (hash_base != NULL) {
148 hash_number = g_str_hash (hash_base);
150 hash_number = (guint) random ();
152 tmpdir = g_strdup_printf ("%s/%u", g_get_tmp_dir (), hash_number);
153 if ((g_access (tmpdir, R_OK) == -1) && (g_mkdir (tmpdir, 0755) == -1)) {
154 g_warning ("%s: failed to create dir '%s': %s",
155 __FUNCTION__, tmpdir, g_strerror(errno));
160 filepath = g_build_filename (tmpdir, orig_name, NULL);
162 /* if file exists, first we try to remove it */
163 if (g_access (filepath, F_OK) == 0)
166 /* don't overwrite if it already exists, even if it is writable */
167 if (g_access (filepath, F_OK) == 0) {
176 /* try to write the file there */
177 fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
179 g_warning ("%s: failed to create '%s': %s",
180 __FUNCTION__, filepath, g_strerror(errno));
194 return TNY_FS_STREAM (tny_fs_stream_new (fd));
202 } ModestGetSupportedAuthInfo;
205 on_idle_secure_auth_finished (gpointer user_data)
207 /* Operation has finished, close the dialog. Control continues after
208 * gtk_dialog_run in modest_utils_get_supported_secure_authentication_methods() */
209 gdk_threads_enter(); /* CHECKED */
210 gtk_dialog_response (GTK_DIALOG (user_data), GTK_RESPONSE_ACCEPT);
211 gdk_threads_leave(); /* CHECKED */
217 on_camel_account_get_supported_secure_authentication (TnyCamelAccount *self,
223 ModestPairList *pairs;
225 ModestProtocolRegistry *protocol_registry;
226 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
229 g_return_if_fail (user_data);
230 g_return_if_fail (TNY_IS_CAMEL_ACCOUNT(self));
231 g_return_if_fail (TNY_IS_LIST(auth_types));
233 info = (ModestGetSupportedAuthInfo *) user_data;
235 /* Free everything if the actual action was canceled */
237 g_debug ("%s: operation canceled\n", __FUNCTION__);
242 g_debug ("%s: error getting the supported auth methods\n", __FUNCTION__);
247 g_debug ("%s: auth_types is NULL.\n", __FUNCTION__);
251 if (tny_list_get_length(auth_types) == 0) {
252 g_debug ("%s: auth_types is an empty TnyList.\n", __FUNCTION__);
256 protocol_registry = modest_runtime_get_protocol_registry ();
257 pairs = modest_protocol_registry_get_pair_list_by_tag (protocol_registry, MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS);
259 /* Get the enum value for the strings: */
261 iter = tny_list_create_iterator(auth_types);
262 while (!tny_iterator_is_done(iter)) {
264 const gchar *auth_name;
265 ModestProtocolType protocol_type;
267 pair = TNY_PAIR(tny_iterator_get_current(iter));
270 auth_name = tny_pair_get_name(pair);
271 g_object_unref (pair);
275 g_debug ("%s: auth_name=%s\n", __FUNCTION__, auth_name);
277 protocol_type = modest_protocol_get_type_id (modest_protocol_registry_get_protocol_by_name (protocol_registry,
278 MODEST_PROTOCOL_REGISTRY_AUTH_PROTOCOLS,
281 if (modest_protocol_registry_protocol_type_is_secure (protocol_registry, protocol_type))
282 result = g_list_prepend(result, GINT_TO_POINTER(protocol_type));
284 tny_iterator_next(iter);
286 g_object_unref (iter);
288 modest_pair_list_free (pairs);
289 *(info->result) = result;
292 /* Close the dialog in a main thread */
293 g_idle_add(on_idle_secure_auth_finished, info->dialog);
296 g_slice_free (ModestGetSupportedAuthInfo, info);
301 gboolean not_finished;
305 keep_pulsing (gpointer user_data)
307 KeepPulsing *info = (KeepPulsing *) user_data;
309 if (!info->not_finished) {
310 g_slice_free (KeepPulsing, info);
314 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (info->progress));
319 modest_utils_get_supported_secure_authentication_methods (ModestProtocolType protocol_type,
320 const gchar* hostname,
322 const gchar* username,
323 GtkWindow *parent_window,
326 TnyAccount * tny_account = NULL;
327 ModestProtocolRegistry *protocol_registry;
330 ModestTnyAccountStore *account_store;
331 TnySessionCamel *session = NULL;
332 ModestProtocol *protocol = NULL;
333 GList *result = NULL;
336 g_return_val_if_fail (protocol_type != MODEST_PROTOCOL_REGISTRY_TYPE_INVALID, NULL);
338 protocol_registry = modest_runtime_get_protocol_registry ();
340 /* We need a connection to get the capabilities; */
341 if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL))
344 /* Create a TnyCamelAccount so we can use
345 * tny_camel_account_get_supported_secure_authentication(): */
346 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
348 if (MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
349 tny_account = modest_account_protocol_create_account (MODEST_ACCOUNT_PROTOCOL (protocol));
353 g_printerr ("%s could not create tny account.", __FUNCTION__);
357 /* Set proto, so that the prepare_func() vfunc will work when
358 * we call set_session(): */
359 protocol = modest_protocol_registry_get_protocol_by_type (protocol_registry, protocol_type);
360 tny_account_set_id (tny_account, "temp_account");
361 tny_account_set_proto (tny_account, modest_protocol_get_name (protocol));
362 tny_account_set_hostname (tny_account, hostname);
363 tny_account_set_user (tny_account, username);
366 tny_account_set_port (tny_account, port);
368 /* Set the session for the account, so we can use it: */
369 account_store = modest_runtime_get_account_store ();
370 session = modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
371 g_return_val_if_fail (session, NULL);
372 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
374 dialog = gtk_dialog_new_with_buttons(" ",
376 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
377 _("mcen_bd_dialog_cancel"),
381 /* Ask camel to ask the server, asynchronously: */
382 ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
383 info->result = &result;
384 info->dialog = dialog;
386 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
387 gtk_label_new(_("emev_ni_checking_supported_auth_methods")));
388 progress = gtk_progress_bar_new();
389 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), progress);
390 gtk_widget_show_all(info->dialog);
392 KeepPulsing *pi = g_slice_new (KeepPulsing);
393 pi->progress = progress;
394 pi->not_finished = TRUE;
396 /* Starts the pulsing of the progressbar */
397 g_timeout_add (500, keep_pulsing, pi);
399 tny_camel_account_get_supported_secure_authentication (TNY_CAMEL_ACCOUNT (tny_account),
400 on_camel_account_get_supported_secure_authentication,
404 retval = gtk_dialog_run (GTK_DIALOG (info->dialog));
406 pi->not_finished = FALSE;
407 /* pi is freed in the timeout itself to avoid a GCond here */
409 gtk_widget_destroy(dialog);
412 tny_account_cancel (tny_account);
413 g_object_unref (tny_account);
419 modest_utils_show_dialog_and_forget (GtkWindow *parent_window,
422 g_return_if_fail (GTK_IS_WINDOW(parent_window));
423 g_return_if_fail (GTK_IS_DIALOG(dialog));
425 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
427 /* Destroy the dialog when it is closed: */
428 g_signal_connect_swapped (dialog,
430 G_CALLBACK (gtk_widget_destroy),
433 gtk_widget_show (GTK_WIDGET (dialog));
437 modest_utils_toggle_action_set_active_block_notify (GtkToggleAction *action, gboolean value)
439 GSList *proxies = NULL;
441 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
443 for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
444 proxies != NULL; proxies = g_slist_next (proxies)) {
445 GtkWidget *widget = (GtkWidget *) proxies->data;
446 gtk_action_block_activate_from (GTK_ACTION (action), widget);
449 gtk_toggle_action_set_active (action, value);
451 for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
452 proxies != NULL; proxies = g_slist_next (proxies)) {
453 GtkWidget *widget = (GtkWidget *) proxies->data;
454 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
461 modest_list_index (TnyList *list, GObject *object)
466 g_return_val_if_fail (TNY_IS_LIST(list), -1);
467 g_return_val_if_fail (G_IS_OBJECT(object), -1);
469 iter = tny_list_create_iterator (list);
470 while (!tny_iterator_is_done (iter)) {
471 GObject *current = tny_iterator_get_current (iter);
473 g_object_unref (current);
474 if (current == object)
477 tny_iterator_next (iter);
481 if (tny_iterator_is_done (iter))
483 g_object_unref (iter);
488 modest_utils_get_available_space (const gchar *maildir_path)
493 GnomeVFSFileSize size;
495 folder = modest_local_folder_info_get_maildir_path (maildir_path);
496 uri_string = gnome_vfs_get_uri_from_local_path (folder);
497 uri = gnome_vfs_uri_new (uri_string);
502 if (gnome_vfs_get_volume_free_space (uri, &size) != GNOME_VFS_OK)
504 gnome_vfs_uri_unref (uri);
509 return (guint64) size;
512 on_destroy_dialog (GtkDialog *dialog)
514 gtk_widget_destroy (GTK_WIDGET(dialog));
515 if (gtk_events_pending ())
516 gtk_main_iteration ();
520 checked_modest_sort_criterium_view_add_sort_key (ModestSortCriteriumView *view, const gchar* key, guint max)
524 g_return_val_if_fail (view && MODEST_IS_SORT_CRITERIUM_VIEW(view), 0);
525 g_return_val_if_fail (key, 0);
527 sort_key = modest_sort_criterium_view_add_sort_key (view, key);
528 if (sort_key < 0 || sort_key >= max) {
529 g_warning ("%s: out of range (%d) for %s", __FUNCTION__, sort_key, key);
532 return (guint)sort_key;
536 launch_sort_headers_dialog (GtkWindow *parent_window,
539 ModestHeaderView *header_view = NULL;
541 GtkSortType sort_type;
543 gint default_key = 0;
545 gboolean outgoing = FALSE;
546 gint current_sort_colid = -1;
547 GtkSortType current_sort_type;
548 gint attachments_sort_id;
549 gint priority_sort_id;
550 GtkTreeSortable *sortable;
551 GtkTreeModel *filter;
553 /* Get header window */
554 if (MODEST_IS_MAIN_WINDOW (parent_window)) {
555 header_view = MODEST_HEADER_VIEW(modest_main_window_get_child_widget (MODEST_MAIN_WINDOW(parent_window),
556 MODEST_MAIN_WINDOW_WIDGET_TYPE_HEADER_VIEW));
557 #ifdef MODEST_TOOLKIT_HILDON2
558 } else if (MODEST_IS_HEADER_WINDOW (parent_window)) {
559 header_view = MODEST_HEADER_VIEW (modest_header_window_get_header_view (MODEST_HEADER_WINDOW (parent_window)));
566 /* Add sorting keys */
567 cols = modest_header_view_get_columns (header_view);
570 #define SORT_ID_NUM 6
571 int sort_model_ids[SORT_ID_NUM];
572 int sort_ids[SORT_ID_NUM];
574 outgoing = (GPOINTER_TO_INT (g_object_get_data(G_OBJECT(cols->data), MODEST_HEADER_VIEW_COLUMN))==
575 MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT);
577 sort_key = checked_modest_sort_criterium_view_add_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), _("mcen_li_sort_sender_recipient"),
580 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_TO_COLUMN;
581 sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT;
583 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_FROM_COLUMN;
584 sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN;
587 sort_key = checked_modest_sort_criterium_view_add_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), _("mcen_li_sort_date"),
590 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_DATE_SENT_TIME_T_COLUMN;
591 sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_SENT_DATE;
593 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_DATE_RECEIVED_TIME_T_COLUMN;
594 sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_RECEIVED_DATE;
596 default_key = sort_key;
598 sort_key = checked_modest_sort_criterium_view_add_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), _("mcen_li_sort_subject"),
600 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_SUBJECT_COLUMN;
602 sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_OUT;
604 sort_ids[sort_key] = MODEST_HEADER_VIEW_COLUMN_COMPACT_HEADER_IN;
606 sort_key = checked_modest_sort_criterium_view_add_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), _("mcen_li_sort_attachment"),
608 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN;
609 sort_ids[sort_key] = TNY_HEADER_FLAG_ATTACHMENTS;
610 attachments_sort_id = sort_key;
612 sort_key = checked_modest_sort_criterium_view_add_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), _("mcen_li_sort_size"),
614 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_MESSAGE_SIZE_COLUMN;
615 sort_ids[sort_key] = 0;
617 sort_key = checked_modest_sort_criterium_view_add_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), _("mcen_li_sort_priority"),
619 sort_model_ids[sort_key] = TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN;
620 sort_ids[sort_key] = TNY_HEADER_FLAG_PRIORITY_MASK;
621 priority_sort_id = sort_key;
623 filter = gtk_tree_view_get_model (GTK_TREE_VIEW (header_view));
624 sortable = GTK_TREE_SORTABLE (gtk_tree_model_filter_get_model (GTK_TREE_MODEL_FILTER (filter)));
626 if (!gtk_tree_sortable_get_sort_column_id (sortable,
627 ¤t_sort_colid, ¤t_sort_type)) {
628 modest_sort_criterium_view_set_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), default_key);
629 modest_sort_criterium_view_set_sort_order (MODEST_SORT_CRITERIUM_VIEW (dialog), GTK_SORT_DESCENDING);
631 modest_sort_criterium_view_set_sort_order (MODEST_SORT_CRITERIUM_VIEW (dialog), current_sort_type);
632 if (current_sort_colid == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN) {
633 gpointer flags_sort_type_pointer;
634 flags_sort_type_pointer = g_object_get_data (G_OBJECT (cols->data), MODEST_HEADER_VIEW_FLAG_SORT);
635 if (GPOINTER_TO_INT (flags_sort_type_pointer) == TNY_HEADER_FLAG_PRIORITY_MASK)
636 modest_sort_criterium_view_set_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), priority_sort_id);
638 modest_sort_criterium_view_set_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), attachments_sort_id);
640 gint current_sort_keyid = 0;
641 while (current_sort_keyid < SORT_ID_NUM) {
642 if (sort_model_ids[current_sort_keyid] == current_sort_colid)
645 current_sort_keyid++;
647 modest_sort_criterium_view_set_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog), current_sort_keyid);
651 result = gtk_dialog_run (GTK_DIALOG (dialog));
652 if (result == GTK_RESPONSE_OK) {
653 sort_key = modest_sort_criterium_view_get_sort_key (MODEST_SORT_CRITERIUM_VIEW (dialog));
654 if (sort_key < 0 || sort_key > SORT_ID_NUM -1) {
655 g_warning ("%s: out of range (%d)", __FUNCTION__, sort_key);
659 sort_type = modest_sort_criterium_view_get_sort_order (MODEST_SORT_CRITERIUM_VIEW (dialog));
660 if (sort_model_ids[sort_key] == TNY_GTK_HEADER_LIST_MODEL_FLAGS_COLUMN) {
661 g_object_set_data (G_OBJECT(cols->data), MODEST_HEADER_VIEW_FLAG_SORT,
662 GINT_TO_POINTER (sort_ids[sort_key]));
663 /* This is a hack to make it resort rows always when flag fields are
664 * selected. If we do not do this, changing sort field from priority to
665 * attachments does not work */
666 modest_header_view_sort_by_column_id (header_view, 0, sort_type);
668 gtk_tree_view_column_set_sort_column_id (GTK_TREE_VIEW_COLUMN (cols->data),
669 sort_model_ids[sort_key]);
672 modest_header_view_sort_by_column_id (header_view, sort_model_ids[sort_key], sort_type);
673 gtk_tree_sortable_sort_column_changed (sortable);
676 modest_widget_memory_save (modest_runtime_get_conf (),
677 G_OBJECT (header_view), MODEST_CONF_HEADER_VIEW_KEY);
684 modest_utils_run_sort_dialog (GtkWindow *parent_window,
685 ModestSortDialogType type)
687 GtkWidget *dialog = NULL;
690 dialog = modest_platform_create_sort_dialog (parent_window);
693 modest_window_mgr_set_modal (modest_runtime_get_window_mgr (),
694 GTK_WINDOW (dialog), parent_window);
698 case MODEST_SORT_HEADERS:
699 launch_sort_headers_dialog (parent_window,
705 on_destroy_dialog (GTK_DIALOG(dialog));
710 modest_images_cache_get_id (const gchar *account, const gchar *uri)
712 GnomeVFSURI *vfs_uri;
715 vfs_uri = gnome_vfs_uri_new (uri);
719 result = g_strdup_printf ("%s__%x", account, gnome_vfs_uri_hash (vfs_uri));
720 gnome_vfs_uri_unref (vfs_uri);
726 modest_utils_get_account_name_from_recipient (const gchar *from_header, gchar **mailbox)
728 gchar *account_name = NULL;
729 ModestAccountMgr *mgr = NULL;
730 GSList *accounts = NULL, *node = NULL;
734 g_return_val_if_fail (from_header, NULL);
736 mgr = modest_runtime_get_account_mgr ();
737 accounts = modest_account_mgr_account_names (mgr, TRUE);
739 for (node = accounts; node != NULL; node = g_slist_next (node)) {
741 gchar *transport_account;
743 if (!strcmp (from_header, node->data)) {
744 account_name = g_strdup (node->data);
748 transport_account = modest_account_mgr_get_server_account_name (modest_runtime_get_account_mgr (),
749 (const gchar *) node->data,
750 TNY_ACCOUNT_TYPE_TRANSPORT);
751 if (transport_account) {
753 proto = modest_account_mgr_get_string (mgr, transport_account, MODEST_ACCOUNT_PROTO, TRUE);
756 ModestProtocol *protocol =
757 modest_protocol_registry_get_protocol_by_name (modest_runtime_get_protocol_registry (),
758 MODEST_PROTOCOL_REGISTRY_TRANSPORT_PROTOCOLS,
760 if (protocol && MODEST_IS_ACCOUNT_PROTOCOL (protocol)) {
761 ModestPairList *pair_list;
763 gchar *from_header_email =
764 modest_text_utils_get_email_address (from_header);
765 pair_list = modest_account_protocol_get_from_list (MODEST_ACCOUNT_PROTOCOL (protocol),
768 pair = modest_pair_list_find_by_first_as_string (pair_list, from_header_email);
770 account_name = g_strdup (node->data);
772 *mailbox = g_strdup (from_header_email);
775 modest_pair_list_free (pair_list);
780 g_free (transport_account);
782 if (mailbox && *mailbox)
786 modest_account_mgr_get_from_string (mgr, node->data, NULL);
790 modest_text_utils_get_email_address (from);
791 gchar *from_header_email =
792 modest_text_utils_get_email_address (from_header);
794 if (from_email && from_header_email) {
795 if (!modest_text_utils_utf8_strcmp (from_header_email, from_email, TRUE)) {
796 account_name = g_strdup (node->data);
803 g_free (from_header_email);
809 g_slist_foreach (accounts, (GFunc) g_free, NULL);
810 g_slist_free (accounts);
816 modest_utils_on_entry_invalid_character (ModestValidatingEntry *self,
817 const gchar* character,
820 gchar *message = NULL;
821 const gchar *show_char = NULL;
824 show_char = character;
829 message = g_strdup_printf (_CS("ckdg_ib_illegal_characters_entered"), show_char);
830 modest_platform_information_banner (GTK_WIDGET (self), NULL, message);
835 modest_utils_open_mcc_mapping_file (void)
840 if (access (MODEST_OPERATOR_WIZARD_MCC_MAPPING, R_OK) == 0) {
841 path = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
842 } else if (access (MODEST_MCC_MAPPING, R_OK) == 0) {
843 path = MODEST_MCC_MAPPING;
845 g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
846 __FUNCTION__, MODEST_OPERATOR_WIZARD_MCC_MAPPING, MODEST_MCC_MAPPING);
850 result = fopen (path, "r");
852 g_warning ("%s: error opening mapping file '%s': %s",
853 __FUNCTION__, path, strerror(errno));
859 /* each line is of the form:
862 NOTE: this function is NOT re-entrant, the out-param country
863 are static strings that should NOT be freed. and will change when
864 calling this function again
867 parse_mcc_mapping_line (const char* line, char** country)
869 char mcc[4]; /* the mcc code, always 3 bytes*/
870 gchar *iter, *tab, *final;
877 /* Go to the first tab (Country separator) */
878 tab = g_utf8_strrchr (line, -1, '\t');
882 *country = g_utf8_find_next_char (tab, NULL);
884 /* Replace by end of string. We need to use strlen, because
885 g_utf8_strrchr expects bytes and not UTF8 characters. File
886 lines end with \r\n */
887 final = g_utf8_strrchr (tab, strlen (tab) + 1, '\r');
888 if (G_LIKELY (final))
891 tab[strlen(tab) - 1] = '\0';
894 mcc[0] = g_utf8_get_char (line);
895 iter = g_utf8_find_next_char (line, NULL);
896 mcc[1] = g_utf8_get_char (iter);
897 iter = g_utf8_find_next_char (iter, NULL);
898 mcc[2] = g_utf8_get_char (iter);
901 return (int) strtol ((const char*)mcc, NULL, 10);
904 #define MCC_FILE_MAX_LINE_LEN 128 /* max length of a line in MCC file */
906 /** Note that the mcc_mapping file is installed
907 * by the operator-wizard-settings package.
910 modest_utils_create_country_model (void)
914 model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT));
920 modest_utils_fill_country_model (GtkTreeModel *model, gint *locale_mcc)
922 char line[MCC_FILE_MAX_LINE_LEN];
923 guint previous_mcc = 0;
925 GHashTable *country_hash;
928 /* First we need to know our current region */
929 file = modest_utils_open_mcc_mapping_file ();
931 g_warning ("Could not open mcc_mapping file");
935 /* Get the territory specified for the current locale */
936 territory = nl_langinfo (_NL_IDENTIFICATION_TERRITORY);
938 setlocale (LC_MESSAGES, "en_GB");
940 while (fgets (line, MCC_FILE_MAX_LINE_LEN, file) != NULL) {
942 char *country = NULL;
944 mcc = parse_mcc_mapping_line (line, &country);
945 if (!country || mcc == 0) {
946 g_warning ("%s: error parsing line: '%s'", __FUNCTION__, line);
950 if (mcc == previous_mcc) {
951 /* g_warning ("already seen: %s", line); */
956 if (!(*locale_mcc)) {
957 gchar *translation = dgettext ("osso-countries", country);
958 if (!g_utf8_collate (translation, territory))
963 setlocale (LC_MESSAGES, "");
965 /* Now we fill the model */
967 country_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
968 while (fgets (line, MCC_FILE_MAX_LINE_LEN, file) != NULL) {
971 char *country = NULL;
973 const gchar *name_translated;
975 mcc = parse_mcc_mapping_line (line, &country);
976 if (!country || mcc == 0) {
977 g_warning ("%s: error parsing line: '%s'", __FUNCTION__, line);
981 if (mcc == previous_mcc ||
982 g_hash_table_lookup (country_hash, country)) {
983 g_debug ("already seen: '%s' %d", country, mcc);
988 g_hash_table_insert (country_hash, g_strdup (country), GINT_TO_POINTER (mcc));
990 name_translated = dgettext ("osso-countries", country);
992 /* Add the row to the model if we have translation for it */
993 if (g_utf8_collate (country, name_translated)) {
994 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
995 gtk_list_store_set(GTK_LIST_STORE (model), &iter,
996 MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, mcc,
997 MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, name_translated,
1000 g_debug ("%s no translation for %s", __FUNCTION__, country);
1005 g_hash_table_unref (country_hash);
1008 /* Fallback to Finland */
1012 /* Sort the items: */
1013 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
1014 MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, GTK_SORT_ASCENDING);
1018 modest_utils_create_notification_list_from_header_list (TnyList *header_list)
1020 GList *new_headers_list;
1023 g_return_val_if_fail (TNY_IS_LIST (header_list), NULL);
1024 g_return_val_if_fail (tny_list_get_length (header_list) > 0, NULL);
1026 new_headers_list = NULL;
1027 iter = tny_list_create_iterator (header_list);
1028 while (!tny_iterator_is_done (iter)) {
1029 ModestMsgNotificationData *data;
1033 header = (TnyHeader *) tny_iterator_get_current (iter);
1035 folder = tny_header_get_folder (header);
1040 uid = tny_header_dup_uid (header);
1041 uri = g_strdup_printf ("%s/%s",
1042 tny_folder_get_url_string (folder),
1046 /* Create data & add to list */
1047 data = g_slice_new0 (ModestMsgNotificationData);
1048 data->subject = tny_header_dup_subject (header);
1049 data->from = tny_header_dup_from (header);
1052 new_headers_list = g_list_append (new_headers_list, data);
1054 g_object_unref (folder);
1056 g_object_unref (header);
1058 tny_iterator_next (iter);
1060 g_object_unref (iter);
1062 return new_headers_list;
1066 free_notification_data (gpointer data,
1069 ModestMsgNotificationData *notification_data = (ModestMsgNotificationData *) data;
1071 g_free (notification_data->from);
1072 g_free (notification_data->subject);
1073 g_free (notification_data->uri);
1075 g_slice_free (ModestMsgNotificationData, notification_data);
1079 modest_utils_free_notification_list (GList *notification_list)
1081 g_return_if_fail (g_list_length (notification_list) > 0);
1083 g_list_foreach (notification_list, free_notification_data, NULL);
1084 g_list_free (notification_list);
1088 modest_utils_flush_send_queue (const gchar *account_id)
1090 TnyTransportAccount *account;
1092 /* Get the transport account */
1093 account = (TnyTransportAccount *)
1094 modest_tny_account_store_get_server_account (modest_runtime_get_account_store (),
1096 TNY_ACCOUNT_TYPE_TRANSPORT);
1098 ModestMailOperation *wakeup_op;
1099 ModestTnySendQueue *send_queue = modest_runtime_get_send_queue (account, TRUE);
1102 wakeup_op = modest_mail_operation_new (NULL);
1103 modest_mail_operation_queue_add (modest_runtime_get_mail_operation_queue (),
1105 modest_mail_operation_queue_wakeup (wakeup_op, send_queue);
1107 g_object_unref (account);