From e5d4412a9f76a8ba843fc979f9439bd092095ea8 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 14 Feb 2007 12:24:18 +0000 Subject: [PATCH] * all: make device, platform_factory accessible as singletons through modest_runtime, and use them. this fixes a lot of memleaks pmo-trunk-r828 --- src/modest-formatter.c | 7 +++--- src/modest-mail-operation.c | 10 ++++----- src/modest-runtime.c | 8 +++++++ src/modest-runtime.h | 22 ++++++++++++++----- src/modest-singletons.c | 30 ++++++++++++------------- src/modest-tny-account-store.c | 44 ++++++++++++++++++++++--------------- src/modest-tny-account-store.h | 3 ++- src/modest-tny-msg.c | 13 +++++------ src/modest-tny-platform-factory.c | 3 ++- src/modest-ui-actions.c | 23 +++++-------------- 10 files changed, 86 insertions(+), 77 deletions(-) diff --git a/src/modest-formatter.c b/src/modest-formatter.c index a24f6b7..ff73f89 100644 --- a/src/modest-formatter.c +++ b/src/modest-formatter.c @@ -36,6 +36,7 @@ #include "modest-formatter.h" #include "modest-text-utils.h" #include "modest-tny-platform-factory.h" +#include typedef struct _ModestFormatterPrivate ModestFormatterPrivate; struct _ModestFormatterPrivate { @@ -119,15 +120,13 @@ modest_formatter_do (ModestFormatter *self, TnyMimePart *body, TnyHeader *header TnyMsg *new_msg = NULL; gchar *body_text = NULL, *txt = NULL; ModestFormatterPrivate *priv; - TnyPlatformFactory *fact; g_return_val_if_fail (self, NULL); g_return_val_if_fail (header, NULL); g_return_val_if_fail (func, NULL); /* Build new part */ - fact = modest_tny_platform_factory_get_instance (); - new_msg = tny_platform_factory_new_msg (fact); + new_msg = tny_platform_factory_new_msg (modest_runtime_get_platform_factory()); if (body) body_text = extract_text (self, body); @@ -173,7 +172,7 @@ modest_formatter_attach (ModestFormatter *self, TnyMimePart *body, TnyHeader *he ModestFormatterPrivate *priv; TnyPlatformFactory *fact; - fact = modest_tny_platform_factory_get_instance (); + fact = modest_runtime_get_platform_factory (); /* Build new part */ new_msg = tny_platform_factory_new_msg (fact); body_part = tny_platform_factory_new_mime_part (fact); diff --git a/src/modest-mail-operation.c b/src/modest-mail-operation.c index ba4e00c..27f5894 100644 --- a/src/modest-mail-operation.c +++ b/src/modest-mail-operation.c @@ -281,7 +281,6 @@ add_if_attachment (gpointer data, gpointer user_data) static TnyMsg * create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, guint type) { - TnyPlatformFactory *fact; TnyMsg *new_msg; TnyHeader *new_header, *header; gchar *new_subject; @@ -322,8 +321,8 @@ create_reply_forward_mail (TnyMsg *msg, const gchar *from, gboolean is_reply, gu g_object_unref (G_OBJECT(body)); /* Fill the header */ - fact = modest_tny_platform_factory_get_instance (); - new_header = TNY_HEADER (tny_platform_factory_new_header (fact)); + new_header = TNY_HEADER (tny_platform_factory_new_header + (modest_runtime_get_platform_factory())); tny_msg_set_header (new_msg, new_header); tny_header_set_from (new_header, from); tny_header_set_replyto (new_header, from); @@ -923,15 +922,14 @@ add_attachments (TnyMsg *msg, GList *attachments_list) const gchar *attachment_content_type; const gchar *attachment_filename; TnyStream *attachment_stream; - TnyPlatformFactory *fact; - fact = modest_tny_platform_factory_get_instance (); for (pos = (GList *)attachments_list; pos; pos = pos->next) { old_attachment = pos->data; attachment_filename = tny_mime_part_get_filename (old_attachment); attachment_stream = tny_mime_part_get_stream (old_attachment); - attachment_part = tny_platform_factory_new_mime_part (fact); + attachment_part = tny_platform_factory_new_mime_part + (modest_runtime_get_platform_factory()); attachment_content_type = tny_mime_part_get_content_type (old_attachment); diff --git a/src/modest-runtime.c b/src/modest-runtime.c index 0a30c00..692b89d 100644 --- a/src/modest-runtime.c +++ b/src/modest-runtime.c @@ -242,6 +242,14 @@ modest_runtime_get_device (void) } +TnyPlatformFactory* +modest_runtime_get_platform_factory (void) +{ + g_return_val_if_fail (_singletons, NULL); + return modest_singletons_get_platform_factory (_singletons); +} + + ModestTnySendQueue* diff --git a/src/modest-runtime.h b/src/modest-runtime.h index 5e4d0f6..2319ff7 100644 --- a/src/modest-runtime.h +++ b/src/modest-runtime.h @@ -38,6 +38,7 @@ #include #include #include +#include G_BEGIN_DECLS @@ -143,20 +144,31 @@ ModestTnyAccountStore* modest_runtime_get_account_store (void); * * get the ModestCacheMgr singleton instance * - * Returns: the ModestCacheMgr singleton. This should NOT be unref'd. + * Returns: the #ModestCacheMgr singleton. This should NOT be unref'd. **/ ModestCacheMgr* modest_runtime_get_cache_mgr (void); /** - * modest_runtime_get_cache_mgr: + * modest_runtime_get_device: + * + * get the #TnyDevice singleton instance + * + * Returns: the #TnyDevice singleton. This should NOT be unref'd. + **/ +TnyDevice* modest_runtime_get_device (void); + + +/** + * modest_runtime_get_platform_factory: * - * get the TnyDevice singleton instance + * get the #TnyPlatformFactory singleton instance * - * Returns: the TnyDevice singleton. This should NOT be unref'd. + * Returns: the #TnyPlatformFactory singleton. This should NOT be unref'd. **/ -TnyDevice* modest_runtime_get_device (void); +TnyPlatformFactory* modest_runtime_get_platform_factory (void); + diff --git a/src/modest-singletons.c b/src/modest-singletons.c index 713fc6f..541f4be 100644 --- a/src/modest-singletons.c +++ b/src/modest-singletons.c @@ -111,8 +111,20 @@ modest_singletons_init (ModestSingletons *obj) g_printerr ("modest: cannot create modest account mgr instance\n"); return; } + + priv->platform_fact = modest_tny_platform_factory_get_instance (); + if (!priv->platform_fact) { + g_printerr ("modest: cannot create platform factory instance\n"); + return; + } + + priv->device = tny_platform_factory_new_device (priv->platform_fact); + if (!priv->device) { + g_printerr ("modest: cannot create tny device instance\n"); + return; + } - priv->account_store = modest_tny_account_store_new (priv->account_mgr); + priv->account_store = modest_tny_account_store_new (priv->account_mgr, priv->device); if (!priv->account_store) { g_printerr ("modest: cannot create modest tny account store instance\n"); return; @@ -128,21 +140,7 @@ modest_singletons_init (ModestSingletons *obj) if (!priv->mail_op_queue) { g_printerr ("modest: cannot create modest mail operation queue instance\n"); return; - } - - priv->platform_fact = modest_tny_platform_factory_get_instance (); - if (!priv->platform_fact) { - g_printerr ("modest: cannot create platform factory instance\n"); - return; - } - - priv->device = tny_platform_factory_new_device (priv->platform_fact); - if (!priv->device) { - g_printerr ("modest: cannot create tny device instance\n"); - return; - } - - + } } diff --git a/src/modest-tny-account-store.c b/src/modest-tny-account-store.c index 4d005e0..3d518cd 100644 --- a/src/modest-tny-account-store.c +++ b/src/modest-tny-account-store.c @@ -34,12 +34,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -73,9 +73,9 @@ typedef struct _ModestTnyAccountStorePrivate ModestTnyAccountStorePrivate; struct _ModestTnyAccountStorePrivate { gchar *cache_dir; GHashTable *password_hash; - TnyDevice *device; ModestAccountMgr *account_mgr; TnySessionCamel *session; + TnyDevice *device; /* we cache them here */ GSList *store_accounts; @@ -166,8 +166,8 @@ modest_tny_account_store_instance_init (ModestTnyAccountStore *obj) priv->cache_dir = NULL; priv->account_mgr = NULL; - priv->device = NULL; priv->session = NULL; + priv->device = NULL; priv->password_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); @@ -179,6 +179,9 @@ account_list_free (GSList *accounts) GSList *cursor = accounts; while (cursor) { g_object_unref (G_OBJECT(cursor->data)); +/* if (G_IS_OBJECT(cursor->data)) */ +/* g_warning ("BUG: account %s still holds refs", */ +/* tny_account_get_id (TNY_ACCOUNT(cursor->data))); */ cursor = cursor->next; } g_slist_free (accounts); @@ -222,7 +225,6 @@ on_account_changed (ModestAccountMgr *acc_mgr, const gchar *account, gboolean se account_list_free (priv->transport_accounts); priv->transport_accounts = NULL; - g_signal_emit (G_OBJECT(self), signals[ACCOUNT_UPDATE_SIGNAL], 0, account); } @@ -347,6 +349,11 @@ modest_tny_account_store_finalize (GObject *obj) priv->account_mgr = NULL; } + if (priv->device) { + g_object_unref (G_OBJECT(priv->device)); + priv->device = NULL; + } + /* this includes the local folder */ account_list_free (priv->store_accounts); priv->store_accounts = NULL; @@ -358,25 +365,20 @@ modest_tny_account_store_finalize (GObject *obj) camel_object_unref (CAMEL_OBJECT(priv->session)); priv->session = NULL; } - - /* the device should be unref'ed *AFTER* the session */ - if (priv->device) { - g_object_unref (G_OBJECT(priv->device)); - priv->device = NULL; - } G_OBJECT_CLASS(parent_class)->finalize (obj); } ModestTnyAccountStore* -modest_tny_account_store_new (ModestAccountMgr *account_mgr) { +modest_tny_account_store_new (ModestAccountMgr *account_mgr, TnyDevice *device) { GObject *obj; ModestTnyAccountStorePrivate *priv; TnyList *list; g_return_val_if_fail (account_mgr, NULL); + g_return_val_if_fail (device, NULL); obj = G_OBJECT(g_object_new(MODEST_TYPE_TNY_ACCOUNT_STORE, NULL)); priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(obj); @@ -384,6 +386,9 @@ modest_tny_account_store_new (ModestAccountMgr *account_mgr) { priv->account_mgr = account_mgr; g_object_ref (G_OBJECT(priv->account_mgr)); + priv->device = device; + g_object_ref (priv->device); + priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE(obj)); tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ()); @@ -498,8 +503,10 @@ modest_tny_account_store_get_accounts (TnyAccountStore *self, TnyList *list, priv->transport_accounts = get_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT); else get_cached_accounts (self, list, TNY_ACCOUNT_TYPE_TRANSPORT); - } else + } else { g_return_if_reached (); /* incorrect req type */ + return; + } } @@ -526,13 +533,14 @@ modest_tny_account_store_get_device (TnyAccountStore *self) { ModestTnyAccountStorePrivate *priv; - priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE (self); - - if (!priv->device) - priv->device = tny_platform_factory_new_device - (modest_tny_platform_factory_get_instance()); + g_return_val_if_fail (self, NULL); - return g_object_ref (G_OBJECT(priv->device)); + priv = MODEST_TNY_ACCOUNT_STORE_GET_PRIVATE(self); + + if (priv->device) + return g_object_ref (G_OBJECT(priv->device)); + else + return NULL; } diff --git a/src/modest-tny-account-store.h b/src/modest-tny-account-store.h index 45c267d..07c466c 100644 --- a/src/modest-tny-account-store.h +++ b/src/modest-tny-account-store.h @@ -90,7 +90,8 @@ GType modest_tny_account_store_get_type (void) G_GNUC_CONST; * * Returns: newly created account store or NULL in case of error */ -ModestTnyAccountStore* modest_tny_account_store_new (ModestAccountMgr *account_mgr); +ModestTnyAccountStore* modest_tny_account_store_new (ModestAccountMgr *account_mgr, + TnyDevice *device); /** diff --git a/src/modest-tny-msg.c b/src/modest-tny-msg.c index 31ae202..7a71ad4 100644 --- a/src/modest-tny-msg.c +++ b/src/modest-tny-msg.c @@ -61,7 +61,7 @@ modest_tny_msg_new (const gchar* mailto, const gchar* from, const gchar *cc, gchar *content_type; /* Create new msg */ - fact = modest_tny_platform_factory_get_instance (); + fact = modest_runtime_get_platform_factory (); new_msg = tny_platform_factory_new_msg (fact); header = tny_platform_factory_new_header (fact); @@ -96,9 +96,6 @@ add_body_part (TnyMsg *msg, { TnyMimePart *text_body_part = NULL; TnyStream *text_body_stream; - TnyPlatformFactory *fact; - - fact = modest_tny_platform_factory_get_instance (); /* Create the stream */ text_body_stream = TNY_STREAM (tny_camel_stream_new @@ -107,7 +104,8 @@ add_body_part (TnyMsg *msg, /* Create body part if needed */ if (has_attachments) - text_body_part = tny_platform_factory_new_mime_part (fact); + text_body_part = tny_platform_factory_new_mime_part + (modest_runtime_get_platform_factory ()); else text_body_part = TNY_MIME_PART(msg); @@ -138,15 +136,14 @@ add_attachments (TnyMsg *msg, GList *attachments_list) const gchar *attachment_content_type; const gchar *attachment_filename; TnyStream *attachment_stream; - TnyPlatformFactory *fact; - fact = modest_tny_platform_factory_get_instance (); for (pos = (GList *)attachments_list; pos; pos = pos->next) { old_attachment = pos->data; attachment_filename = tny_mime_part_get_filename (old_attachment); attachment_stream = tny_mime_part_get_stream (old_attachment); - attachment_part = tny_platform_factory_new_mime_part (fact); + attachment_part = tny_platform_factory_new_mime_part ( + modest_runtime_get_platform_factory()); attachment_content_type = tny_mime_part_get_content_type (old_attachment); diff --git a/src/modest-tny-platform-factory.c b/src/modest-tny-platform-factory.c index 1ca0853..4d370a5 100644 --- a/src/modest-tny-platform-factory.c +++ b/src/modest-tny-platform-factory.c @@ -184,7 +184,8 @@ static TnyAccountStore * modest_tny_platform_factory_new_account_store (TnyPlatformFactory *self) { return TNY_ACCOUNT_STORE(modest_tny_account_store_new - (modest_runtime_get_account_mgr())); + (modest_runtime_get_account_mgr(), + modest_runtime_get_device())); } static TnyDevice * diff --git a/src/modest-ui-actions.c b/src/modest-ui-actions.c index 7f859ba..27b7309 100644 --- a/src/modest-ui-actions.c +++ b/src/modest-ui-actions.c @@ -413,17 +413,10 @@ modest_ui_actions_on_prev (GtkWidget *widget, void modest_ui_actions_on_send_receive (GtkWidget *widget, ModestWindow *win) { - TnyDevice *device; - TnyAccountStore *account_store; gchar *account_name; TnyAccount *tny_account; ModestTnySendQueue *send_queue; - /* Get device. Do not ask the platform factory for it, because - it returns always a new one */ - account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()); - device = tny_account_store_get_device (account_store); - account_name = g_strdup(modest_window_get_active_account(MODEST_WINDOW(win))); if (!account_name) @@ -432,10 +425,11 @@ modest_ui_actions_on_send_receive (GtkWidget *widget, ModestWindow *win) g_printerr ("modest: cannot get account\n"); return; } - + tny_account = modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(), - account_name, TNY_ACCOUNT_TYPE_TRANSPORT); + account_name, + TNY_ACCOUNT_TYPE_TRANSPORT); if (!tny_account) { g_printerr ("modest: cannot get tny transport account\n"); return; @@ -771,19 +765,12 @@ modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,ModestItemTyp GtkWidget *dialog; gchar *txt, *item; gboolean online; - TnyDevice *device; - TnyAccountStore *account_store; item = (type == MODEST_ITEM_TYPE_FOLDER) ? "folder" : "message"; - /* Get device. Do not ask the platform factory for it, because - it returns always a new one */ - account_store = TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()); - device = tny_account_store_get_device (account_store); - if (g_main_depth > 0) gdk_threads_enter (); - online = tny_device_is_online (device); + online = tny_device_is_online (modest_runtime_get_device()); if (online) { /* already online -- the item is simply not there... */ @@ -812,7 +799,7 @@ modest_ui_actions_on_item_not_found (ModestHeaderView *header_view,ModestItemTyp gtk_window_set_default_size (GTK_WINDOW(dialog), 300, 300); if (gtk_dialog_run (GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { - tny_device_force_online (device); + tny_device_force_online (modest_runtime_get_device()); } } gtk_widget_destroy (dialog); -- 1.7.9.5