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-transport-account.h>
40 #include <tny-camel-imap-store-account.h>
41 #include <tny-camel-pop-store-account.h>
43 #include <modest-defs.h>
44 #include "modest-utils.h"
45 #include "modest-platform.h"
46 #include <modest-local-folder-info.h>
49 modest_utils_get_supported_secure_authentication_error_quark (void)
51 return g_quark_from_static_string("modest-utils-get-supported-secure-authentication-error-quark");
55 modest_utils_folder_writable (const gchar *filename)
57 g_return_val_if_fail (filename, FALSE);
62 if (g_strncasecmp (filename, "obex", 4) != 0) {
63 GnomeVFSFileInfo folder_info;
65 folder = g_path_get_dirname (filename);
66 gnome_vfs_get_file_info (folder, &folder_info,
67 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
69 if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
70 (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
78 modest_utils_file_exists (const gchar *filename)
80 GnomeVFSURI *uri = NULL;
81 gboolean result = FALSE;
83 g_return_val_if_fail (filename, FALSE);
85 uri = gnome_vfs_uri_new (filename);
87 result = gnome_vfs_uri_exists (uri);
88 gnome_vfs_uri_unref (uri);
94 modest_utils_create_temp_stream (const gchar *orig_name, const gchar *hash_base, gchar **path)
97 gchar *filepath = NULL;
101 /* hmmm... maybe we need a modest_text_utils_validate_file_name? */
102 g_return_val_if_fail (orig_name && strlen(orig_name) != 0, NULL);
104 if (strlen(orig_name) > 200) {
105 g_warning ("%s: filename too long ('%s')",
106 __FUNCTION__, orig_name);
110 if (g_strstr_len (orig_name, strlen(orig_name), "/") != NULL) {
111 g_warning ("%s: filename contains '/' character(s) (%s)",
112 __FUNCTION__, orig_name);
116 /* make a random subdir under /tmp or /var/tmp */
117 if (hash_base != NULL) {
118 hash_number = g_str_hash (hash_base);
120 hash_number = (guint) random ();
122 tmpdir = g_strdup_printf ("%s/%u", g_get_tmp_dir (), hash_number);
123 if ((g_access (tmpdir, R_OK) == -1) && (g_mkdir (tmpdir, 0755) == -1)) {
124 g_warning ("%s: failed to create dir '%s': %s",
125 __FUNCTION__, tmpdir, g_strerror(errno));
130 filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
131 /* don't overwrite if it already exists, even if it is writable */
132 if (modest_utils_file_exists (filepath)) {
141 /* try to write the file there */
142 fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
144 g_warning ("%s: failed to create '%s': %s",
145 __FUNCTION__, filepath, g_strerror(errno));
157 return TNY_FS_STREAM (tny_fs_stream_new (fd));
167 } ModestGetSupportedAuthInfo;
169 static void on_camel_account_get_supported_secure_authentication_status (
170 GObject *self, TnyStatus *status, gpointer user_data)
172 /*ModestGetSupportedAuthInfo* info = (ModestGetSupportedAuthInfo*) user_data;*/
176 on_idle_secure_auth_finished (gpointer user_data)
178 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
179 /* Operation has finished, close the dialog. Control continues after
180 * gtk_dialog_run in modest_utils_get_supported_secure_authentication_methods() */
182 /* This is a GDK lock because we are an idle callback and
183 * the code below is or does Gtk+ code */
185 gdk_threads_enter(); /* CHECKED */
186 gtk_dialog_response (GTK_DIALOG (info->dialog), GTK_RESPONSE_ACCEPT);
187 gdk_threads_leave(); /* CHECKED */
193 on_camel_account_get_supported_secure_authentication (TnyCamelAccount *self, gboolean cancelled,
194 TnyList *auth_types, GError *err, gpointer user_data)
196 g_return_if_fail (TNY_IS_CAMEL_ACCOUNT(self));
197 g_return_if_fail (TNY_IS_LIST(auth_types));
199 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
200 g_return_if_fail (info);
203 /* Free everything if the actual action was canceled */
205 /* The operation was canceled and the ownership of the info given to us
206 * so that we could still check the cancel flag. */
207 g_slice_free (ModestGetSupportedAuthInfo, info);
214 g_error_free (info->error);
218 info->error = g_error_copy (err);
222 g_warning ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
224 else if (tny_list_get_length(auth_types) == 0)
225 g_warning ("DEBUG: %s: auth_types is an empty TnyList.\n", __FUNCTION__);
227 ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list ();
229 /* Get the enum value for the strings: */
230 GList *result = NULL;
231 TnyIterator* iter = tny_list_create_iterator(auth_types);
232 while (!tny_iterator_is_done(iter)) {
233 TnyPair *pair = TNY_PAIR(tny_iterator_get_current(iter));
234 const gchar *auth_name = NULL;
236 auth_name = tny_pair_get_name(pair);
237 g_object_unref (pair);
241 printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
243 ModestAuthProtocol proto = modest_protocol_info_get_auth_protocol (auth_name);
244 if(proto != MODEST_PROTOCOL_AUTH_NONE)
245 result = g_list_prepend(result, GINT_TO_POINTER(proto));
247 tny_iterator_next(iter);
249 g_object_unref (iter);
251 modest_pair_list_free (pairs);
253 info->result = result;
256 printf("DEBUG: finished\n");
258 /* Close the dialog in a main thread */
259 g_idle_add(on_idle_secure_auth_finished, info);
264 on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
266 g_return_if_fail (GTK_IS_WIDGET(dialog));
268 if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT) {
269 ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
270 g_return_if_fail(info);
271 /* This gives the ownership of the info to the worker thread. */
278 modest_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto,
279 const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error)
281 g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
283 /* We need a connection to get the capabilities; */
284 if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL))
288 result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
291 /* Create a TnyCamelAccount so we can use
292 * tny_camel_account_get_supported_secure_authentication(): */
293 TnyAccount * tny_account = NULL;
295 case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
296 case MODEST_PROTOCOL_TRANSPORT_SMTP:
297 tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
298 case MODEST_PROTOCOL_STORE_POP:
299 tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
300 case MODEST_PROTOCOL_STORE_IMAP:
301 tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
302 case MODEST_PROTOCOL_STORE_MAILDIR:
303 case MODEST_PROTOCOL_STORE_MBOX:
304 tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
311 g_printerr ("%s could not create tny account.", __FUNCTION__);
315 /* Set proto, so that the prepare_func() vfunc will work when we call
317 /* TODO: Why isn't this done in account_new()? */
318 tny_account_set_proto (tny_account,
319 modest_protocol_info_get_transport_store_protocol_name(proto));
321 tny_account_set_hostname (tny_account, hostname);
322 /* Required for POP, at least */
323 tny_account_set_user (tny_account, username);
326 tny_account_set_port (tny_account, port);
328 /* Set the session for the account, so we can use it: */
329 ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
330 TnySessionCamel *session =
331 modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
332 g_return_val_if_fail (session, NULL);
333 tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
336 /* Ask camel to ask the server, asynchronously: */
337 ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
339 info->cancel = FALSE;
341 info->progress = gtk_progress_bar_new();
342 /* TODO: Need logical_ID for the title: */
343 info->dialog = gtk_dialog_new_with_buttons(_("Authentication"),
344 parent_window, GTK_DIALOG_MODAL,
345 _("mcen_bd_dialog_cancel"),
348 //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100);
350 g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info);
352 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
353 gtk_label_new("Checking for supported authentication types..."));
354 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress);
355 gtk_widget_show_all(info->dialog);
356 gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress));
358 printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
359 tny_camel_account_get_supported_secure_authentication (
360 TNY_CAMEL_ACCOUNT (tny_account),
361 on_camel_account_get_supported_secure_authentication,
362 on_camel_account_get_supported_secure_authentication_status,
365 gtk_dialog_run (GTK_DIALOG (info->dialog));
367 gtk_widget_destroy(info->dialog);
369 GList *result = info->result;
373 gchar * debug_url_string = tny_account_get_url_string (tny_account);
374 g_warning ("DEBUG: %s:\n error: %s\n account url: %s", __FUNCTION__, info->error->message,
376 g_free (debug_url_string);
378 g_propagate_error(error, info->error);
382 g_slice_free (ModestGetSupportedAuthInfo, info);
387 // Tell the caller that the operation was canceled so it can
390 modest_utils_get_supported_secure_authentication_error_quark(),
391 MODEST_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED,
392 "User has canceled query");
399 modest_utils_show_dialog_and_forget (GtkWindow *parent_window,
402 g_return_if_fail (GTK_IS_WINDOW(parent_window));
403 g_return_if_fail (GTK_IS_DIALOG(dialog));
405 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent_window);
407 /* Destroy the dialog when it is closed: */
408 g_signal_connect_swapped (dialog,
410 G_CALLBACK (gtk_widget_destroy),
413 gtk_widget_show (GTK_WIDGET (dialog));
417 modest_utils_toggle_action_set_active_block_notify (GtkToggleAction *action, gboolean value)
419 GSList *proxies = NULL;
421 g_return_if_fail (GTK_IS_TOGGLE_ACTION (action));
423 for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
424 proxies != NULL; proxies = g_slist_next (proxies)) {
425 GtkWidget *widget = (GtkWidget *) proxies->data;
426 gtk_action_block_activate_from (GTK_ACTION (action), widget);
429 gtk_toggle_action_set_active (action, value);
431 for (proxies = gtk_action_get_proxies (GTK_ACTION (action));
432 proxies != NULL; proxies = g_slist_next (proxies)) {
433 GtkWidget *widget = (GtkWidget *) proxies->data;
434 gtk_action_unblock_activate_from (GTK_ACTION (action), widget);
441 modest_list_index (TnyList *list, GObject *object)
446 g_return_val_if_fail (TNY_IS_LIST(list), -1);
447 g_return_val_if_fail (G_IS_OBJECT(object), -1);
449 iter = tny_list_create_iterator (list);
450 while (!tny_iterator_is_done (iter)) {
451 GObject *current = tny_iterator_get_current (iter);
453 g_object_unref (current);
454 if (current == object)
457 tny_iterator_next (iter);
461 if (tny_iterator_is_done (iter))
463 g_object_unref (iter);
468 modest_folder_available_space (const gchar *maildir_path)
473 GnomeVFSFileSize size;
475 folder = modest_local_folder_info_get_maildir_path (maildir_path);
476 uri_string = gnome_vfs_get_uri_from_local_path (folder);
477 uri = gnome_vfs_uri_new (uri_string);
482 if (gnome_vfs_get_volume_free_space (uri, &size) != GNOME_VFS_OK)
484 gnome_vfs_uri_unref (uri);
489 return (guint64) size;