From 8cb5d470d68036f3fc73156897144bafb83c78ef Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 6 Feb 2007 10:06:18 +0000 Subject: [PATCH] * updates: cleanup, caching accounts, refactoring and work on the send-queue (not working yet) * modest-tny-account, tny-account-store: - refactory, move account creation to modest-tny-account * modest-tny-account-store: - cache accounts; add functions for retrieving tnyaccounts corresponding to account-mgr accounts * modest-tny-send-queue: - fix some bugs, getting much further now * cleanups, cosmetics pmo-trunk-r790 --- src/modest-account-mgr-helpers.c | 67 -------- src/modest-account-mgr-helpers.h | 19 --- src/modest-defs.h | 6 +- src/modest-mail-operation.c | 6 +- src/modest-main.c | 13 +- src/modest-runtime.c | 5 +- src/modest-runtime.h | 1 - src/modest-singletons.c | 6 +- src/modest-singletons.h | 1 - src/modest-tny-account-store.c | 336 ++++++++++++++++++++++++-------------- src/modest-tny-account-store.h | 39 ++++- src/modest-tny-account.c | 85 ++++++++-- src/modest-tny-account.h | 24 ++- src/modest-tny-send-queue.c | 27 +-- src/modest-ui.c | 10 +- 15 files changed, 371 insertions(+), 274 deletions(-) diff --git a/src/modest-account-mgr-helpers.c b/src/modest-account-mgr-helpers.c index c540875..72ab530 100644 --- a/src/modest-account-mgr-helpers.c +++ b/src/modest-account-mgr-helpers.c @@ -248,73 +248,6 @@ modest_account_mgr_set_default_account (ModestAccountMgr *self, const gchar* ac } - -TnyAccount* -modest_account_mgr_get_tny_account (ModestAccountMgr *self, const gchar* account_name, - TnyAccountType type) -{ - TnyAccount *account = NULL; - TnyList *accounts; - TnyIterator *iter; - gchar *server_account; - const gchar *conf_key; - TnyGetAccountsRequestType request_type; /* really confusing.... */ - - g_return_val_if_fail (self, NULL); - g_return_val_if_fail (account_name, NULL); - - switch (type) { - case TNY_ACCOUNT_TYPE_STORE: - conf_key = MODEST_ACCOUNT_STORE_ACCOUNT; - request_type = TNY_ACCOUNT_STORE_STORE_ACCOUNTS; - break; - case TNY_ACCOUNT_TYPE_TRANSPORT: - conf_key = MODEST_ACCOUNT_TRANSPORT_ACCOUNT; - request_type = TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS; - break; - default: - g_return_val_if_reached (NULL); - } - - server_account = modest_account_mgr_get_string (self, account_name, conf_key, FALSE, NULL); - if (!server_account) { - g_printerr ("modest: no %s account specified for %s\n", - type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name); - return NULL; - } - - accounts = tny_simple_list_new (); - tny_account_store_get_accounts (TNY_ACCOUNT_STORE(modest_runtime_get_account_store()), - accounts, request_type); - iter = tny_list_create_iterator (accounts); - while (tny_iterator_is_done (iter)) { - account = TNY_ACCOUNT(tny_iterator_get_current(iter)); - if (strcmp (tny_account_get_id (account), server_account) == 0) - break; - g_object_unref (G_OBJECT(account)); - } - - g_object_unref (G_OBJECT(iter)); - g_object_unref (G_OBJECT(accounts)); - - if (!account) - g_printerr ("modest: no tny %s account found for %s\n", - type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name); - else { - /* sanity check */ - if ((type == TNY_ACCOUNT_TYPE_TRANSPORT && !TNY_IS_TRANSPORT_ACCOUNT(account)) || - (type == TNY_ACCOUNT_TYPE_STORE && !TNY_IS_STORE_ACCOUNT(account))) { - g_printerr ("modest: tny %s acccount found for %s, but was expecting %s account\n", - type == TNY_ACCOUNT_TYPE_TRANSPORT ? "transport" : "store", account_name, - type == TNY_ACCOUNT_TYPE_TRANSPORT ? "store" : "transport"); - g_object_unref (G_OBJECT(account)); - account = NULL; - } - } - return account; -} - - gchar* modest_account_mgr_get_from_string (ModestAccountMgr *self, const gchar* name) { diff --git a/src/modest-account-mgr-helpers.h b/src/modest-account-mgr-helpers.h index c4411dd..86224ba 100644 --- a/src/modest-account-mgr-helpers.h +++ b/src/modest-account-mgr-helpers.h @@ -41,7 +41,6 @@ G_BEGIN_DECLS typedef struct { - TnyAccount *tny_account; gchar *account_name; gchar *hostname; gchar *username; @@ -113,8 +112,6 @@ gboolean modest_account_mgr_set_default_account (ModestAccountMgr *self, void modest_account_mgr_free_account_data (ModestAccountMgr *self, ModestAccountData *data); - - /** * modest_account_mgr_set_enabled * @self: a ModestAccountMgr instance @@ -140,22 +137,6 @@ gboolean modest_account_mgr_set_enabled (ModestAccountMgr *self, const gchar* na gboolean modest_account_mgr_get_enabled (ModestAccountMgr *self, const gchar* name); - -/** - * modest_account_mgr_get_tny_account: - * @self: a #ModestAccountMgr instance - * @name: the account name - * @type: the #TnyAccountType to check; either TNY_ACCOUNT_TYPE_TRANSPORT or TNY_ACCOUNT_TYPE_STORE - * - * get the TnyAccount corresponding to the store/transport (server) accounts for some account. - * ie., every account has two server accounts, and for every server account there is a corresponding - * TnyAccount - * - * Returns: the requested TnyAccount, or NULL in case of error - */ -TnyAccount* modest_account_mgr_get_tny_account (ModestAccountMgr *self, const gchar* name, - TnyAccountType type); - /** * modest_account_mgr_get_from_string * @self: a #ModestAccountMgr instance diff --git a/src/modest-defs.h b/src/modest-defs.h index e0dbc52..a0b62ec 100644 --- a/src/modest-defs.h +++ b/src/modest-defs.h @@ -56,9 +56,11 @@ enum { */ #define MODEST_DIR ".modest" #define MODEST_CACHE_DIR "cache" -#define MODEST_LOCAL_FOLDERS_MAILDIR "local_folders" #define MODEST_LOCAL_FOLDERS_ACCOUNT_NAME "local_folders" -#define MODEST_LOCAL_FOLDERS_DISPLAY_NAME N_("Local folders") +#define MODEST_LOCAL_FOLDERS_ACCOUNT_ID MODEST_LOCAL_FOLDERS_ACCOUNT_NAME +#define MODEST_LOCAL_FOLDERS_MAILDIR MODEST_LOCAL_FOLDERS_ACCOUNT_NAME + +#define MODEST_LOCAL_FOLDERS_DISPLAY_NAME N_(MODEST_LOCAL_FOLDERS_ACCOUNT_NAME) /* configuration key definitions for modest */ #define MODEST_CONF_NAMESPACE "/apps/modest" diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index afa4f5b..9903409 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -221,8 +221,10 @@ modest_mail_operation_send_mail (ModestMailOperation *self, g_return_if_fail (TNY_IS_TRANSPORT_ACCOUNT (transport_account)); send_queue = TNY_SEND_QUEUE (modest_runtime_get_send_queue (transport_account)); - - tny_send_queue_add (send_queue, msg); + if (!TNY_IS_SEND_QUEUE(send_queue)) + g_printerr ("modest: could not find send queue for account\n"); + else + tny_send_queue_add (send_queue, msg); } void diff --git a/src/modest-main.c b/src/modest-main.c index bb9d856..82aa982 100644 --- a/src/modest-main.c +++ b/src/modest-main.c @@ -45,7 +45,7 @@ #include #include #include - +#include #include #include @@ -199,20 +199,20 @@ send_mail (const gchar* account_name, int retval; TnyTransportAccount *account; ModestMailOperation *mail_operation = NULL; - ModestAccountMgr *account_mgr; gchar *from_string; g_return_val_if_fail (account_name, MODEST_ERR_SEND); - account_mgr = modest_runtime_get_account_mgr (); - account = TNY_TRANSPORT_ACCOUNT(modest_account_mgr_get_tny_account (account_mgr, account_name, - TNY_ACCOUNT_TYPE_TRANSPORT)); + account = TNY_TRANSPORT_ACCOUNT (modest_tny_account_store_get_tny_account_by_account + (modest_runtime_get_account_store(), account_name, + TNY_ACCOUNT_TYPE_TRANSPORT)); if (!account) { g_printerr ("modest: no transport defined account for %s\n", account_name); return MODEST_ERR_SEND; } - from_string = modest_account_mgr_get_from_string (account_mgr, account_name); + from_string = modest_account_mgr_get_from_string (modest_runtime_get_account_mgr(), + account_name); mail_operation = modest_mail_operation_new (); modest_mail_operation_send_new_mail (mail_operation, account, @@ -225,6 +225,7 @@ send_mail (const gchar* account_name, } else retval = MODEST_ERR_NONE; /* hurray! */ + g_object_unref (G_OBJECT(account)); g_object_unref (G_OBJECT(mail_operation)); g_free (from_string); diff --git a/src/modest-runtime.c b/src/modest-runtime.c index 6fab641..61750db 100644 --- a/src/modest-runtime.c +++ b/src/modest-runtime.c @@ -246,11 +246,12 @@ modest_runtime_get_send_queue (TnyTransportAccount *account) gpointer orig_key, send_queue; g_return_val_if_fail (_singletons, NULL); - g_return_val_if_fail (!TNY_IS_TRANSPORT_ACCOUNT(account), NULL); + g_return_val_if_fail (TNY_IS_TRANSPORT_ACCOUNT(account), NULL); cache_mgr = modest_singletons_get_cache_mgr (_singletons); send_queue_cache = modest_cache_mgr_get_cache (cache_mgr, - MODEST_CACHE_MGR_CACHE_TYPE_PIXBUF); + MODEST_CACHE_MGR_CACHE_TYPE_SEND_QUEUE); + if (!g_hash_table_lookup_extended (send_queue_cache, account, &orig_key, &send_queue)) { send_queue = (gpointer)modest_tny_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT(account)); g_hash_table_insert (send_queue_cache, account, send_queue); diff --git a/src/modest-runtime.h b/src/modest-runtime.h index be84fbb..c02c1e8 100644 --- a/src/modest-runtime.h +++ b/src/modest-runtime.h @@ -183,7 +183,6 @@ ModestWidgetFactory* modest_runtime_get_widget_factory (void); ModestTnySendQueue* modest_runtime_get_send_queue (TnyTransportAccount *account); - G_END_DECLS #endif /*__MODEST_RUNTIME_H__*/ diff --git a/src/modest-singletons.c b/src/modest-singletons.c index e7b15d9..9385799 100644 --- a/src/modest-singletons.c +++ b/src/modest-singletons.c @@ -109,13 +109,13 @@ modest_singletons_init (ModestSingletons *obj) g_printerr ("modest: cannot create modest account mgr instance\n"); return; } - + priv->account_store = modest_tny_account_store_new (priv->account_mgr); if (!priv->account_store) { g_printerr ("modest: cannot create modest tny account store instance\n"); return; } - + priv->cache_mgr = modest_cache_mgr_new (); if (!priv->cache_mgr) { g_printerr ("modest: cannot create modest cache mgr instance\n"); @@ -161,6 +161,7 @@ modest_singletons_finalize (GObject *obj) priv->account_store = NULL; } + if (priv->account_mgr) { g_object_unref (G_OBJECT(priv->account_mgr)); check_object_is_dead ((GObject*)priv->account_mgr, @@ -234,7 +235,6 @@ modest_singletons_get_cache_mgr (ModestSingletons *self) return MODEST_SINGLETONS_GET_PRIVATE(self)->cache_mgr; } - ModestMailOperationQueue* modest_singletons_get_mail_operation_queue (ModestSingletons *self) { diff --git a/src/modest-singletons.h b/src/modest-singletons.h index d71ab6f..eee58b0 100644 --- a/src/modest-singletons.h +++ b/src/modest-singletons.h @@ -40,7 +40,6 @@ #include #include - G_BEGIN_DECLS /* convenience macros */ diff --git a/src/modest-tny-account-store.c b/src/modest-tny-account-store.c index 383a852..a5ae4a3 100644 --- a/src/modest-tny-account-store.c +++ b/src/modest-tny-account-store.c @@ -59,7 +59,7 @@ static void modest_tny_account_store_finalize (GObject *obj); /* implementations for tny-account-store-iface */ static void modest_tny_account_store_instance_init (ModestTnyAccountStore *obj); -static void modest_tny_account_store_init (gpointer g, gpointer iface_data); +static void modest_tny_account_store_init (gpointer g, gpointer iface_data); /* list my signals */ @@ -70,15 +70,15 @@ enum { typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate; struct _ModestTnyAccountStorePrivate { - - gchar *cache_dir; - + gchar *cache_dir; GHashTable *password_hash; TnyDevice *device; - TnySessionCamel *tny_session_camel; - ModestAccountMgr *account_mgr; - TnyAccount *local_folders; + TnySessionCamel *session; + + /* we cache them here */ + GSList *store_accounts; + GSList *transport_accounts; }; #define MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ @@ -149,8 +149,7 @@ modest_tny_account_store_class_init (ModestTnyAccountStoreClass *klass) G_STRUCT_OFFSET(ModestTnyAccountStoreClass, account_update), NULL, NULL, g_cclosure_marshal_VOID__STRING, - G_TYPE_NONE, 1, G_TYPE_STRING); - + G_TYPE_NONE, 1, G_TYPE_STRING); } @@ -162,76 +161,43 @@ modest_tny_account_store_instance_init (ModestTnyAccountStore *obj) priv->cache_dir = NULL; priv->account_mgr = NULL; - priv->tny_session_camel = NULL; priv->device = NULL; + priv->session = NULL; priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - - priv->local_folders = NULL; } - -/* we need these dummy functions, or tinymail will complain */ -static gchar* -get_password_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel) -{ - return NULL; -} static void -forget_password_dummy (TnyAccount *account) +account_list_free (GSList *accounts) { - return; -} - -/* create a pseudo-account for our local folders */ -static TnyAccount* -get_local_folders_account (ModestTnyAccountStore *self) -{ - TnyStoreAccount *tny_account; - CamelURL *url; - gchar *maildir, *url_string; - ModestTnyAccountStorePrivate *priv; - - priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); - - tny_account = tny_camel_store_account_new (); - if (!tny_account) { - g_printerr ("modest: cannot create account for local folders"); - return NULL; + GSList *cursor = accounts; + while (cursor) { + g_object_unref (G_OBJECT(cursor->data)); + cursor = cursor->next; } - - tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account),priv->tny_session_camel); - - maildir = modest_local_folder_info_get_maildir_path (); - url = camel_url_new ("maildir:", NULL); - camel_url_set_path (url, maildir); - url_string = camel_url_to_string (url, 0); - - tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string); - tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); - tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); - tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_password_dummy); - tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_password_dummy); - - camel_url_free (url); - g_free (maildir); - g_free (url_string); - - return TNY_ACCOUNT(tny_account); + g_slist_free (accounts); } - static void on_account_removed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean server_account, gpointer user_data) { - ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data); + ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data); + ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + /* FIXME: make this more finegrained; changes do not really affect _all_ + * accounts, and some do not affect tny accounts at all (such as 'last_update') + */ + account_list_free (priv->store_accounts); + priv->store_accounts = NULL; + + account_list_free (priv->transport_accounts); + priv->transport_accounts = NULL; + g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, account); - } @@ -240,6 +206,17 @@ on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean se const gchar *key, gpointer user_data) { ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(user_data); + ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + + /* FIXME: make this more finegrained; changes do not really affect _all_ + * accounts, and some do not affect tny accounts at all (such as 'last_update') + */ + account_list_free (priv->store_accounts); + priv->store_accounts = NULL; + + account_list_free (priv->transport_accounts); + priv->transport_accounts = NULL; + g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, account); @@ -317,7 +294,6 @@ get_password (TnyAccount *account, const gchar *prompt, gboolean *cancel) gchar *pwd = NULL; gpointer pwd_ptr; gboolean already_asked; - key = tny_account_get_id (account); account_store = TNY_ACCOUNT_STORE(get_account_store_for_account (account)); @@ -409,11 +385,6 @@ modest_tny_account_store_finalize (GObject *obj) ModestTnyAccountStore *self = MODEST_TNY_ACCOUNT_STORE(obj); ModestTnyAccountStorePrivate *priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); - if (priv->tny_session_camel) { - camel_object_unref (CAMEL_OBJECT(priv->tny_session_camel)); - priv->tny_session_camel = NULL; - } - g_free (priv->cache_dir); priv->cache_dir = NULL; @@ -432,6 +403,18 @@ modest_tny_account_store_finalize (GObject *obj) priv->account_mgr = NULL; } + if (priv->session) { + camel_object_unref (CAMEL_OBJECT(priv->session)); + priv->session = NULL; + } + + /* this includes the local folder */ + account_list_free (priv->store_accounts); + priv->store_accounts = NULL; + + account_list_free (priv->transport_accounts); + priv->transport_accounts = NULL; + G_OBJECT_CLASS(parent_class)->finalize (obj); } @@ -441,6 +424,7 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) { GObject *obj; ModestTnyAccountStorePrivate *priv; + TnyList *list; g_return_val_if_fail (account_mgr, NULL); @@ -449,17 +433,17 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) { priv->account_mgr = account_mgr; g_object_ref (G_OBJECT(priv->account_mgr)); + + priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj)); - /* The session needs the platform factory */ - priv->tny_session_camel = tny_session_camel_new (TNY_ACCOUNT_STORE(obj)); - if (!priv->tny_session_camel) { - g_printerr ("modest: cannot create TnySessionCamel instance\n"); - g_object_unref (obj); - return NULL; - } - - tny_session_camel_set_ui_locker (priv->tny_session_camel, tny_gtk_lockable_new ()); + tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ()); /* FIXME: unref this in the end? */ + + /* force a cache fill... ugly */ + list = TNY_LIST(tny_simple_list_new()); + tny_account_store_get_accounts (TNY_ACCOUNT_STORE(obj), list, + TNY_ACCOUNT_STORE_BOTH); + g_object_unref(list); /* Connect signals */ g_signal_connect (G_OBJECT(account_mgr), "account_changed", @@ -472,71 +456,102 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) { static void -modest_tny_account_store_get_accounts (TnyAccountStore *account_store, TnyList *list, - TnyGetAccountsRequestType request_type) +get_cached_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type) { - TnyAccountType type; - ModestTnyAccountStore *self; ModestTnyAccountStorePrivate *priv; GSList *accounts, *cursor; - g_return_if_fail (account_store); - g_return_if_fail (TNY_IS_LIST(list)); - g_return_if_fail (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS || - request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS || - request_type == TNY_ACCOUNT_STORE_BOTH); - - self = MODEST_TNY_ACCOUNT_STORE(account_store); - priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); - - if (request_type == TNY_ACCOUNT_STORE_BOTH) { - modest_tny_account_store_get_accounts (account_store, list, - TNY_ACCOUNT_STORE_STORE_ACCOUNTS); - modest_tny_account_store_get_accounts (account_store, list, - TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS); - return; - } + priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + accounts = (type == TNY_ACCOUNT_TYPE_STORE ? priv->store_accounts : priv->transport_accounts); - /* - * confusingly, tinymail uses both TnyAccountRequestType and TnyAccountType - */ - switch (request_type) { - case TNY_ACCOUNT_STORE_STORE_ACCOUNTS : type = TNY_ACCOUNT_TYPE_STORE; break; - case TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS: type = TNY_ACCOUNT_TYPE_TRANSPORT; break; - default: g_return_if_reached (); /* 'BOTH' is not possible here */ + cursor = accounts; + while (cursor) { + tny_list_prepend (list, G_OBJECT(cursor->data)); + cursor = cursor->next; } +} + +/* this function fills the TnyList, and also returns a GSList of the accounts, + * for caching purposes + */ +static GSList* +get_accounts (TnyAccountStore *self, TnyList *list, TnyAccountType type) +{ + ModestTnyAccountStorePrivate *priv; + GSList *account_names, *cursor; + GSList *accounts = NULL; + + priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + + account_names = modest_account_mgr_account_names (priv->account_mgr, NULL); - accounts = modest_account_mgr_account_names (priv->account_mgr, NULL); - for (cursor = accounts; cursor; cursor = cursor->next) { + for (cursor = account_names; cursor; cursor = cursor->next) { + gchar *account_name = (gchar*)cursor->data; - if (modest_account_mgr_get_enabled(priv->account_mgr, account_name) == TRUE) { - TnyAccount *tny_account = modest_tny_account_new_from_account (priv->account_mgr, account_name, - type); - if (tny_account) { - tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), priv->tny_session_camel); - - tny_account_set_forget_pass_func (tny_account, forget_password); - tny_account_set_pass_func (tny_account, get_password); + + /* only return enabled accounts */ + if (modest_account_mgr_get_enabled(priv->account_mgr, account_name)) { + TnyAccount *tny_account = + modest_tny_account_new_from_account (priv->account_mgr, account_name, + type, priv->session, get_password, + forget_password); + if (tny_account) { /* something went wrong */ g_object_set_data (G_OBJECT(tny_account), "account_store", (gpointer)self); - tny_list_prepend (list, G_OBJECT(tny_account)); - g_object_unref (G_OBJECT(tny_account)); - } + accounts = g_slist_append (accounts, tny_account); /* cache it */ + } else + g_printerr ("modest: failed to create account for %s\n", account_name); } g_free (account_name); } - g_slist_free (accounts); - + g_slist_free (account_names); + /* also, add the local folder pseudo-account */ - if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) { - if (!priv->local_folders) - priv->local_folders = get_local_folders_account (self); - if (!priv->local_folders) - g_printerr ("modest: no local folders account\n"); - else - tny_list_prepend (list, G_OBJECT(priv->local_folders)); + if (type == TNY_ACCOUNT_TYPE_STORE) { + TnyAccount *tny_account = + modest_tny_account_new_for_local_folders (priv->account_mgr, priv->session); + tny_list_prepend (list, G_OBJECT(tny_account)); + accounts = g_slist_append (accounts, tny_account); /* cache it */ } + + return accounts; } + + +static void +modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list, + TnyGetAccountsRequestType request_type) +{ + ModestTnyAccountStorePrivate *priv; + + g_return_if_fail (self); + g_return_if_fail (TNY_IS_LIST(list)); + + priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + + if (request_type == TNY_ACCOUNT_STORE_BOTH) { + modest_tny_account_store_get_accounts (self, list, TNY_ACCOUNT_STORE_STORE_ACCOUNTS); + modest_tny_account_store_get_accounts (self, list, TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS); + return; + } + + if (request_type == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) { + + if (!priv->store_accounts) + priv->store_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_STORE); + else + get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_STORE); + + } else if (request_type == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) { + + if (!priv->transport_accounts) + priv->transport_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT); + else + get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT); + } else + g_return_if_reached (); /* incorrect req type */ +} + static const gchar* modest_tny_account_store_get_cache_dir (TnyAccountStore *self) @@ -661,14 +676,81 @@ TnySessionCamel* tny_account_store_get_session (TnyAccountStore *self) { g_return_val_if_fail (self, NULL); - return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self)->tny_session_camel; + return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->session; } TnyAccount* -modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self) +modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, const gchar *id) { + TnyAccount *account = NULL; + ModestTnyAccountStorePrivate *priv; + GSList *cursor; + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (id, NULL); + + priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + + for (cursor = priv->store_accounts; cursor ; cursor = cursor->next) { + const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data)); + if (acc_id && strcmp (acc_id, id) == 0) { + account = TNY_ACCOUNT(cursor->data); + break; + } + } + + /* if we already found something, no need to search the transport accounts */ + for (cursor = priv->transport_accounts; !account && cursor ; cursor = cursor->next) { + const gchar *acc_id = tny_account_get_id (TNY_ACCOUNT(cursor->data)); + if (acc_id && strcmp (acc_id, id) == 0) { + account = TNY_ACCOUNT(cursor->data); + break; + } + } + + if (account) + g_object_ref (G_OBJECT(account)); - return MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self)->local_folders; + return account; +} + + +TnyAccount* +modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self, + const gchar *account_name, + TnyAccountType type) +{ + TnyAccount *account = NULL; + ModestAccountData *account_data; + const gchar *id = NULL; + ModestTnyAccountStorePrivate *priv; + + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (account_name, NULL); + g_return_val_if_fail (type == TNY_ACCOUNT_TYPE_STORE || type == TNY_ACCOUNT_TYPE_TRANSPORT, + NULL); + + priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + + account_data = modest_account_mgr_get_account_data (priv->account_mgr, account_name); + if (!account_data) { + g_printerr ("modest: cannot get account data for account '%s'\n", account_name); + return NULL; + } + + if (type == TNY_ACCOUNT_TYPE_STORE && account_data->store_account) + id = account_data->store_account->account_name; + else if (account_data->transport_account) + id = account_data->transport_account->account_name; + + if (id) + account = modest_tny_account_store_get_tny_account_by_id (self, id); + if (!account) + g_printerr ("modest: could not get tny %s account for %s (id=%s)\n", + type == TNY_ACCOUNT_TYPE_STORE? "store" : "transport", + account_name, id ? id : ""); + + modest_account_mgr_free_account_data (priv->account_mgr, account_data); + return account; } diff --git a/src/modest-tny-account-store.h b/src/modest-tny-account-store.h index 861209a..e67ccf9 100644 --- a/src/modest-tny-account-store.h +++ b/src/modest-tny-account-store.h @@ -86,6 +86,36 @@ GType modest_tny_account_store_get_type (void) G_GNUC_CONST; */ ModestTnyAccountStore* modest_tny_account_store_new (ModestAccountMgr *account_mgr); + +/** + * modest_tny_account_store_get_account_by_id + * @self: a ModestTnyAccountStore instance + * @id: some ID + * + * get the account with the given ID or NULL if it's not found + * + * Returns: the tnyaccount or NULL, + * g_object_unref when it's no longer needed + */ +TnyAccount* modest_tny_account_store_get_tny_account_by_id (ModestTnyAccountStore *self, + const gchar *id); + + +/** + * modest_tny_account_store_get_tny_account_by_account + * @self: a ModestTnyAccountStore instance + * @account_name: an account name + * @type: the tny account type + * + * get the tny account corresponding to one of the server_accounts for account with @account_name + * + * Returns: the tnyaccount or NULL in case it's not found or error, + * g_object_unref when it's no longer needed + */ +TnyAccount* modest_tny_account_store_get_tny_account_by_account (ModestTnyAccountStore *self, + const gchar *account_name, + TnyAccountType type); + /** * tny_account_store_get_session * @self: a TnyAccountStore instance @@ -97,15 +127,6 @@ ModestTnyAccountStore* modest_tny_account_store_new (ModestAccountMgr *accoun */ TnySessionCamel* tny_account_store_get_session (TnyAccountStore *self); -/** - * tny_account_store_get_local_folders_account - * @self: a ModestTnyAccountStore instance - * - * return the account corresponding to the local folders - * - * Returns: the tnyaccount for the local folders - */ -TnyAccount* modest_tny_account_store_get_local_folders_account (ModestTnyAccountStore *self); G_END_DECLS diff --git a/src/modest-tny-account.c b/src/modest-tny-account.c index 6b38518..fc30538 100644 --- a/src/modest-tny-account.c +++ b/src/modest-tny-account.c @@ -54,9 +54,9 @@ modest_tny_account_get_special_folder (TnyAccount *account, g_return_val_if_fail (account, NULL); g_return_val_if_fail (0 <= special_type && special_type < TNY_FOLDER_TYPE_NUM, NULL); - - local_account = modest_tny_account_store_get_local_folders_account - (modest_runtime_get_account_store()); + + local_account = modest_tny_account_store_get_tny_account_by_id (modest_runtime_get_account_store(), + MODEST_LOCAL_FOLDERS_ACCOUNT_ID); if (!local_account) { g_printerr ("modest: cannot get local account\n"); return NULL; @@ -105,14 +105,14 @@ modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr, g_return_val_if_fail (account_mgr, NULL); g_return_val_if_fail (account_data, NULL); - + /* sanity checks */ if (account_data->proto == MODEST_PROTOCOL_UNKNOWN) { g_printerr ("modest: '%s' does not provide a protocol\n", account_data->account_name); return NULL; } - + switch (account_data->proto) { case MODEST_PROTOCOL_TRANSPORT_SENDMAIL: case MODEST_PROTOCOL_TRANSPORT_SMTP: @@ -134,20 +134,9 @@ modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr, } tny_account_set_id (tny_account, account_data->account_name); - /* - * FIXME --> bug in tinymail - */ - if (account_data->proto == MODEST_PROTOCOL_TRANSPORT_SENDMAIL || - account_data->proto == MODEST_PROTOCOL_TRANSPORT_SMTP) { - g_printerr ("modest: BUG: cannot create transports accounts... stay tuned\n"); - g_object_unref (G_OBJECT(tny_account)); - return NULL; - } - /* Proto */ tny_account_set_proto (tny_account, modest_protocol_info_get_protocol_name(account_data->proto)); - if (account_data->uri) tny_account_set_url_string (TNY_ACCOUNT(tny_account), account_data->uri); else { @@ -168,14 +157,32 @@ modest_tny_account_new_from_server_account (ModestAccountMgr *account_mgr, } +/* we need these dummy functions, or tinymail will complain */ +static gchar* +get_pass_dummy (TnyAccount *account, const gchar *prompt, gboolean *cancel) +{ + return NULL; +} +static void +forget_pass_dummy (TnyAccount *account) +{ + /* intentionally left blank */ +} + TnyAccount* modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name, - TnyAccountType type) + TnyAccountType type, + TnySessionCamel *session, + TnyGetPassFunc get_pass_func, + TnyForgetPassFunc forget_pass_func) { TnyAccount *tny_account = NULL; ModestAccountData *account_data = NULL; ModestServerAccountData *server_data = NULL; - + + g_return_val_if_fail (account_mgr, NULL); + g_return_val_if_fail (account_name, NULL); + account_data = modest_account_mgr_get_account_data (account_mgr, account_name); if (!account_data) { g_printerr ("modest: cannot get account data for account %s\n", account_name); @@ -202,6 +209,12 @@ modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar return NULL; } + tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session); + tny_account_set_forget_pass_func (tny_account, + forget_pass_func ? forget_pass_func : forget_pass_dummy); + tny_account_set_pass_func (tny_account, + get_pass_func ? get_pass_func: get_pass_dummy); + /* this name is what shows up in the folder view -- so for some POP/IMAP/... server * account, we set its name to the acount of which it is part */ if (account_data->display_name) @@ -211,3 +224,39 @@ modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar return tny_account; } + +TnyAccount* +modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, TnySessionCamel *session) +{ + TnyStoreAccount *tny_account; + CamelURL *url; + gchar *maildir, *url_string; + + g_return_val_if_fail (account_mgr, NULL); + + tny_account = tny_camel_store_account_new (); + if (!tny_account) { + g_printerr ("modest: cannot create account for local folders"); + return NULL; + } + tny_camel_account_set_session (TNY_CAMEL_ACCOUNT(tny_account), session); + + maildir = modest_local_folder_info_get_maildir_path (); + url = camel_url_new ("maildir:", NULL); + camel_url_set_path (url, maildir); + url_string = camel_url_to_string (url, 0); + + tny_account_set_url_string (TNY_ACCOUNT(tny_account), url_string); + tny_account_set_name (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_NAME); + tny_account_set_id (TNY_ACCOUNT(tny_account), MODEST_LOCAL_FOLDERS_ACCOUNT_ID); + tny_account_set_forget_pass_func (TNY_ACCOUNT(tny_account), forget_pass_dummy); + tny_account_set_pass_func (TNY_ACCOUNT(tny_account), get_pass_dummy); + + camel_url_free (url); + g_free (maildir); + g_free (url_string); + + return TNY_ACCOUNT(tny_account); +} + + diff --git a/src/modest-tny-account.h b/src/modest-tny-account.h index a0c50b3..5624920 100644 --- a/src/modest-tny-account.h +++ b/src/modest-tny-account.h @@ -38,6 +38,7 @@ #include #include #include +#include G_BEGIN_DECLS @@ -46,17 +47,34 @@ G_BEGIN_DECLS * @account_mgr: a valid account mgr instance * @account_name: the account name for which to create a corresponding tny account * @type: the type of account to create (TNY_ACCOUNT_TYPE_STORE or TNY_ACCOUNT_TYPE_TRANSPORT) + * @session: a tny camel session + * @get_pass_func: the get-password function + * @forget_pass_func: the forget-password function * * get a tnyaccount corresponding to the server_accounts (store or transport) for this account. * NOTE: this function does not set the camel session or the get/forget password functions * * Returns: a new TnyAccount or NULL in case of error. */ -TnyAccount* modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, - const gchar *account_name, TnyAccountType type); - +TnyAccount* +modest_tny_account_new_from_account (ModestAccountMgr *account_mgr, const gchar *account_name, + TnyAccountType type, + TnySessionCamel *session, + TnyGetPassFunc get_pass_func, + TnyForgetPassFunc forget_pass_func); +/** + * modest_tny_account_new_for_local_folders: + * @account_mgr: a valid account mgr instance + * @session: a tny camel session + * + * get the local folders (pseudo) account; you should only need one such account. + * + * Returns: a new local folders TnyAccount or NULL in case of error. + */ +TnyAccount* modest_tny_account_new_for_local_folders (ModestAccountMgr *account_mgr, + TnySessionCamel *session); /** * modest_tny_account_get_special_folder: diff --git a/src/modest-tny-send-queue.c b/src/modest-tny-send-queue.c index 11d31af..46755d9 100644 --- a/src/modest-tny-send-queue.c +++ b/src/modest-tny-send-queue.c @@ -60,7 +60,6 @@ static TnyCamelSendQueueClass *parent_class = NULL; /* uncomment the following if you have defined any signals */ /* static guint signals[LAST_SIGNAL] = {0}; */ - /* * this thread actually tries to send all the mails in the outbox */ @@ -69,7 +68,9 @@ static TnyCamelSendQueueClass *parent_class = NULL; static void modest_tny_send_queue_cancel (TnySendQueue *self, gboolean remove) { - TNY_CAMEL_SEND_QUEUE_GET_CLASS(parent_class)->cancel_func (self, remove); + g_warning (__FUNCTION__); + + TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->cancel_func (self, remove); } static void @@ -77,14 +78,16 @@ modest_tny_send_queue_add (TnySendQueue *self, TnyMsg *msg) { ModestTnySendQueuePrivate *priv; - g_return_if_fail (self); + g_return_if_fail (TNY_IS_SEND_QUEUE(self)); g_return_if_fail (TNY_IS_CAMEL_MSG(msg)); + + g_warning (__FUNCTION__); priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); /* FIXME: do something smart here... */ - - TNY_CAMEL_SEND_QUEUE_GET_CLASS(parent_class)->add_func (self, msg); + + TNY_CAMEL_SEND_QUEUE_CLASS(parent_class)->add_func (self, msg); } static TnyFolder* @@ -92,6 +95,8 @@ modest_tny_send_queue_get_sentbox (TnySendQueue *self) { ModestTnySendQueuePrivate *priv; + g_warning (__FUNCTION__); + g_return_val_if_fail (self, NULL); priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); @@ -107,6 +112,8 @@ modest_tny_send_queue_get_outbox (TnySendQueue *self) ModestTnySendQueuePrivate *priv; g_return_val_if_fail (self, NULL); + + g_warning (__FUNCTION__); priv = MODEST_TNY_SEND_QUEUE_GET_PRIVATE (self); @@ -133,7 +140,7 @@ modest_tny_send_queue_get_type (void) (GInstanceInitFunc) modest_tny_send_queue_instance_init, NULL }; - my_type = g_type_register_static (G_TYPE_OBJECT, + my_type = g_type_register_static (TNY_TYPE_CAMEL_SEND_QUEUE, "ModestTnySendQueue", &my_info, 0); } @@ -151,10 +158,10 @@ modest_tny_send_queue_class_init (ModestTnySendQueueClass *klass) parent_class = g_type_class_peek_parent (klass); gobject_class->finalize = modest_tny_send_queue_finalize; - parent_class->add_func = modest_tny_send_queue_add; - parent_class->get_outbox_func = modest_tny_send_queue_get_outbox; - parent_class->get_sentbox_func = modest_tny_send_queue_get_sentbox; - parent_class->cancel_func = modest_tny_send_queue_cancel; + TNY_CAMEL_SEND_QUEUE_CLASS(klass)->add_func = modest_tny_send_queue_add; + TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_outbox_func = modest_tny_send_queue_get_outbox; + TNY_CAMEL_SEND_QUEUE_CLASS(klass)->get_sentbox_func = modest_tny_send_queue_get_sentbox; + TNY_CAMEL_SEND_QUEUE_CLASS(klass)->cancel_func = modest_tny_send_queue_cancel; g_type_class_add_private (gobject_class, sizeof(ModestTnySendQueuePrivate)); } diff --git a/src/modest-ui.c b/src/modest-ui.c index 5733df8..d82e223 100644 --- a/src/modest-ui.c +++ b/src/modest-ui.c @@ -1109,11 +1109,12 @@ _modest_ui_actions_on_send (GtkWidget *widget, return; } transport_account = - TNY_TRANSPORT_ACCOUNT(modest_account_mgr_get_tny_account (account_mgr, - account_name, - TNY_ACCOUNT_TYPE_TRANSPORT)); + TNY_TRANSPORT_ACCOUNT(modest_tny_account_store_get_tny_account_by_account + (modest_runtime_get_account_store(), + account_name, + TNY_ACCOUNT_TYPE_TRANSPORT)); if (!transport_account) { - g_printerr ("modest: no transport account found\n"); + g_printerr ("modest: no transport account found for '%s'\n", account_name); g_free (account_name); modest_edit_msg_window_free_msg_data (edit_window, data); return; @@ -1135,6 +1136,7 @@ _modest_ui_actions_on_send (GtkWidget *widget, g_free (account_name); g_object_unref (G_OBJECT (mail_operation)); g_object_unref (G_OBJECT (transport_account)); + modest_edit_msg_window_free_msg_data (edit_window, data); /* Save settings and close the window */ -- 1.7.9.5