-gboolean
-modest_maemo_utils_folder_writable (const gchar *filename)
-{
- g_return_val_if_fail (filename, FALSE);
-
- if (!filename)
- return FALSE;
-
- if (g_strncasecmp (filename, "obex", 4) != 0) {
- GnomeVFSFileInfo folder_info;
- gchar *folder;
- folder = g_path_get_dirname (filename);
- gnome_vfs_get_file_info (folder, &folder_info,
- GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
- g_free (folder);
- if (!((folder_info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE) ||
- (folder_info.permissions & GNOME_VFS_PERM_USER_WRITE))) {
- return FALSE;
- }
- }
- return TRUE;
-}
-
-gboolean
-modest_maemo_utils_file_exists (const gchar *filename)
-{
- GnomeVFSURI *uri = NULL;
- gboolean result = FALSE;
-
- uri = gnome_vfs_uri_new (filename);
- if (uri) {
- result = gnome_vfs_uri_exists (uri);
- gnome_vfs_uri_unref (uri);
- }
- return result;
-}
-
-TnyFsStream *
-modest_maemo_utils_create_temp_stream (const gchar *orig_name, gchar **path)
-{
- gint fd;
- gchar *filepath = NULL;
- gchar *tmpdir;
-
- /* hmmm... maybe we need a modest_text_utils_validate_file_name? */
- g_return_val_if_fail (orig_name || strlen(orig_name) == 0, NULL);
- if (strlen(orig_name) > 200) {
- g_warning ("%s: filename too long ('%s')",
- __FUNCTION__, orig_name);
- return NULL;
- }
-
- if (g_strstr_len (orig_name, strlen(orig_name), "/") != NULL) {
- g_warning ("%s: filename contains '/' character(s) (%s)",
- __FUNCTION__, orig_name);
- return NULL;
- }
-
- /* make a random subdir under /tmp or /var/tmp */
- tmpdir = g_strdup_printf ("%s/%d", g_get_tmp_dir (), (guint)random());
- if (g_mkdir (tmpdir, 0755) == -1) {
- g_warning ("%s: failed to create dir '%s': %s",
- __FUNCTION__, tmpdir, g_strerror(errno));
- g_free (tmpdir);
- return NULL;
- }
-
- /* try to write the file there */
- filepath = g_strconcat (tmpdir, "/", orig_name, NULL);
- fd = g_open (filepath, O_CREAT|O_WRONLY|O_TRUNC, 0644);
- if (fd == -1) {
- g_warning ("%s: failed to create '%s': %s",
- __FUNCTION__, filepath, g_strerror(errno));
- g_free (tmpdir);
- g_free (filepath);
- return NULL;
- }
-
- g_free (tmpdir);
-
- if (path)
- *path = filepath;
-
- return TNY_FS_STREAM (tny_fs_stream_new (fd));
-}
-
-typedef struct
-{
- gboolean cancel;
- GList *result;
- GtkWidget* dialog;
- GtkWidget* progress;
- GError* error;
-} ModestGetSupportedAuthInfo;
-
-static void on_camel_account_get_supported_secure_authentication_status (
- GObject *self, TnyStatus *status, gpointer user_data)
-{
- /*ModestGetSupportedAuthInfo* info = (ModestGetSupportedAuthInfo*) user_data;*/
-}
-
-static gboolean
-on_idle_secure_auth_finished (gpointer user_data)
-{
- ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
- /* Operation has finished, close the dialog. Control continues after
- * gtk_dialog_run in modest_maemo_utils_get_supported_secure_authentication_methods() */
-
- /* This is a GDK lock because we are an idle callback and
- * the code below is or does Gtk+ code */
-
- gdk_threads_enter(); /* CHECKED */
- gtk_dialog_response (GTK_DIALOG (info->dialog), GTK_RESPONSE_ACCEPT);
- gdk_threads_leave(); /* CHECKED */
-
- return FALSE;
-}
-
-static void
-on_camel_account_get_supported_secure_authentication (
- TnyCamelAccount *self, gboolean cancelled,
- TnyList *auth_types, GError **err,
- gpointer user_data)
-{
-
- ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
- g_return_if_fail (info);
-
- /* Free everything if the actual action was canceled */
- if (info->cancel)
- {
- /* The operation was canceled and the ownership of the info given to us
- * so that we could still check the cancel flag. */
- g_slice_free (ModestGetSupportedAuthInfo, info);
- info = NULL;
- }
- else
- {
- /* TODO: Why is this a pointer to a pointer? We are not supposed to
- * set it, are we? */
- if(err != NULL && *err != NULL)
- {
- if(info->error != NULL) g_error_free(info->error);
- info->error = g_error_copy(*err);
- }
-
- if (!auth_types) {
- printf ("DEBUG: %s: auth_types is NULL.\n", __FUNCTION__);
- }
- else
- {
- ModestPairList* pairs = modest_protocol_info_get_auth_protocol_pair_list ();
-
- /* Get the enum value for the strings: */
- GList *result = NULL;
- TnyIterator* iter = tny_list_create_iterator(auth_types);
- while (!tny_iterator_is_done(iter)) {
- TnyPair *pair = TNY_PAIR(tny_iterator_get_current(iter));
- const gchar *auth_name = NULL;
- if (pair) {
- auth_name = tny_pair_get_name(pair);
- g_object_unref (pair);
- pair = NULL;
- }
-
- printf("DEBUG: %s: auth_name=%s\n", __FUNCTION__, auth_name);
-
- ModestAuthProtocol proto = modest_protocol_info_get_auth_protocol (auth_name);
- if(proto != MODEST_PROTOCOL_AUTH_NONE)
- result = g_list_prepend(result, GINT_TO_POINTER(proto));
-
- tny_iterator_next(iter);
- }
- g_object_unref (iter);
-
- modest_pair_list_free (pairs);
-
- info->result = result;
- }
-
- printf("DEBUG: finished\n");
-
- /* Close the dialog in a main thread */
- g_idle_add(on_idle_secure_auth_finished, info);
- }
-}
-
-static void on_secure_auth_cancel(GtkWidget* dialog, int response, gpointer user_data)
-{
- if(response == GTK_RESPONSE_REJECT || response == GTK_RESPONSE_DELETE_EVENT)
- {
- ModestGetSupportedAuthInfo *info = (ModestGetSupportedAuthInfo*)user_data;
- g_return_if_fail(info);
- /* This gives the ownership of the info to the worker thread. */
- info->result = NULL;
- info->cancel = TRUE;
- }
-}
-
-
-typedef struct
-{
- GMainLoop* loop;
-} UserData;
-
-static UserData *user_data = NULL;
-
-static void
-on_account_online (TnyCamelAccount *account, GError *err)
-{
- printf ("DEBUGa1: %s\n", __FUNCTION__);
-
- if (err) {
- printf("DEBUG: %s: error=\n %s\n", __FUNCTION__, err->message);
- }
-
- /* Allow the function that requested this callback to continue: */
- /* TODO: Tinymail should really give us user_data with this callback. */
- if (user_data && user_data->loop)
- g_main_loop_quit (user_data->loop);
-}
-
-GList*
-modest_maemo_utils_get_supported_secure_authentication_methods (ModestTransportStoreProtocol proto,
- const gchar* hostname, gint port, const gchar* username, GtkWindow *parent_window, GError** error)
-{
- g_return_val_if_fail (proto != MODEST_PROTOCOL_TRANSPORT_STORE_UNKNOWN, NULL);
-
- /* We need a connection to get the capabilities; */
- if (!modest_platform_connect_and_wait (GTK_WINDOW (parent_window), NULL))
- return NULL;
-
- /*
- result = g_list_append (result, GINT_TO_POINTER (MODEST_PROTOCOL_AUTH_CRAMMD5));
- */
-
- /* Create a TnyCamelAccount so we can use
- * tny_camel_account_get_supported_secure_authentication(): */
- TnyAccount * tny_account = NULL;
- switch (proto) {
- case MODEST_PROTOCOL_TRANSPORT_SENDMAIL:
- case MODEST_PROTOCOL_TRANSPORT_SMTP:
- tny_account = TNY_ACCOUNT(tny_camel_transport_account_new ()); break;
- case MODEST_PROTOCOL_STORE_POP:
- tny_account = TNY_ACCOUNT(tny_camel_pop_store_account_new ()); break;
- case MODEST_PROTOCOL_STORE_IMAP:
- tny_account = TNY_ACCOUNT(tny_camel_imap_store_account_new ()); break;
- case MODEST_PROTOCOL_STORE_MAILDIR:
- case MODEST_PROTOCOL_STORE_MBOX:
- tny_account = TNY_ACCOUNT(tny_camel_store_account_new()); break;
- default:
- tny_account = NULL;
- }
-
-
- if (!tny_account) {
- g_printerr ("%s could not create tny account.", __FUNCTION__);
- return NULL;
- }
-
- /* Set proto, so that the prepare_func() vfunc will work when we call
- * set_session(): */
- /* TODO: Why isn't this done in account_new()? */
- tny_account_set_proto (tny_account,
- modest_protocol_info_get_transport_store_protocol_name(proto));
-
- tny_account_set_hostname (tny_account, hostname);
- /* Required for POP, at least */
- tny_account_set_user (tny_account, username);
-
- if(port > 0)
- tny_account_set_port (tny_account, port);
-
- /* Set the session for the account, so we can use it: */
- ModestTnyAccountStore *account_store = modest_runtime_get_account_store ();
- TnySessionCamel *session =
- modest_tny_account_store_get_session (TNY_ACCOUNT_STORE (account_store));
- g_return_val_if_fail (session, NULL);
- tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session);
-
-
- /* This blocks on the result: */
- /* TODO: Fix tinymail to take user_data for the callback instead of using one static instance: */
- if (user_data) {
- g_slice_free (UserData, user_data);
- user_data = NULL;
- }
-
- user_data = g_slice_new0 (UserData);
- user_data->loop = g_main_loop_new (NULL, FALSE /* not running */);
-
- /* We get a warning if we don't do use tny_camel_account_set_online():
- * GLIB CRITICAL ** camel-lite - camel_service_query_auth_types: assertion `service != NULL' failed.
- */
- tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (tny_account), TRUE, &on_account_online);
- printf ("DEBUGa2: %s\n", __FUNCTION__);
-
- /* This main loop will run until the idle handler has stopped it: */
- printf ("DEBUG: %s: before g_main_loop_run()\n", __FUNCTION__);
- GDK_THREADS_LEAVE();
- g_main_loop_run (user_data->loop);
- GDK_THREADS_ENTER();
- printf ("DEBUG: %s: after g_main_loop_run()\n", __FUNCTION__);
- g_main_loop_unref (user_data->loop);
- /* g_main_context_unref (context); */
-
- g_slice_free (UserData, user_data);
- user_data = NULL;
-
-
- /* Ask camel to ask the server, asynchronously: */
- ModestGetSupportedAuthInfo *info = g_slice_new (ModestGetSupportedAuthInfo);
- info->result = NULL;
- info->cancel = FALSE;
- info->error = NULL;
- info->progress = gtk_progress_bar_new();
- /* TODO: Need logical_ID for the title: */
- info->dialog = gtk_dialog_new_with_buttons(_("Authentication"),
- parent_window, GTK_DIALOG_MODAL,
- _("mcen_bd_dialog_cancel"),
- GTK_RESPONSE_REJECT,
- NULL);
- //gtk_window_set_default_size(GTK_WINDOW(info->dialog), 300, 100);
-
- g_signal_connect(G_OBJECT(info->dialog), "response", G_CALLBACK(on_secure_auth_cancel), info);
-
- gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox),
- gtk_label_new("Checking for supported authentication types..."));
- gtk_container_add(GTK_CONTAINER(GTK_DIALOG(info->dialog)->vbox), info->progress);
- gtk_widget_show_all(info->dialog);
- gtk_progress_bar_pulse(GTK_PROGRESS_BAR(info->progress));
-
- printf ("DEBUG: %s: STARTING.\n", __FUNCTION__);
- tny_camel_account_get_supported_secure_authentication (
- TNY_CAMEL_ACCOUNT (tny_account),
- on_camel_account_get_supported_secure_authentication,
- on_camel_account_get_supported_secure_authentication_status,
- info);
-
- gtk_dialog_run (GTK_DIALOG (info->dialog));
-
- gtk_widget_destroy(info->dialog);
-
- GList *result = info->result;
- if (!info->cancel)
- {
- if(info->error != NULL)
- g_propagate_error(error, info->error);
-
- g_slice_free (ModestGetSupportedAuthInfo, info);
- info = NULL;
- }
- else
- {
- // Tell the caller that the operation was canceled so it can
- // make a difference
- g_set_error(error,
- modest_maemo_utils_get_supported_secure_authentication_error_quark(),
- MODEST_MAEMO_UTILS_GET_SUPPORTED_SECURE_AUTHENTICATION_ERROR_CANCELED,
- "User has canceled query");
- }
-
- return result;
-}
-