Commit a06e4484 authored by Matthew Barnes's avatar Matthew Barnes

Give MailSession a permanent home.

Global variables in shared libraries are a bad idea.  EMailBackend now
owns the MailSession instance, which is actually now EMailSession.

Move the blocking utility functions in mail-tools.c to e-mail-session.c
and add asynchronous variants.  Same approach as Camel.

Replace EMailReader.get_shell_backend() with EMailReader.get_backend(),
which returns an EMailBackend.  Easier access to the EMailSession.
parent 5e0758bb
......@@ -33,7 +33,7 @@
#include "mail-view.h"
#include "e-util/e-config.h"
#include "mail/mail-config.h"
#include "mail/mail-session.h"
#include "mail/e-mail-session.h"
#include "mail-guess-servers.h"
struct _MailAccountViewPrivate {
......@@ -1019,7 +1019,8 @@ next_page (GtkWidget *entry, MailAccountView *mav)
}
static void
mail_account_view_construct (MailAccountView *view)
mail_account_view_construct (MailAccountView *view,
EMailSession *session)
{
gint i;
EShell *shell;
......@@ -1041,7 +1042,9 @@ mail_account_view_construct (MailAccountView *view)
gtk_widget_show (view->pages[0]->box);
view->current_page = 0;
gtk_box_pack_start ((GtkBox *)view, view->scroll, TRUE, TRUE, 0);
view->edit = em_account_editor_new_for_pages (view->original, EMAE_PAGES, "org.gnome.evolution.mail.config.accountWizard", view->wpages);
view->edit = em_account_editor_new_for_pages (
view->original, EMAE_PAGES, session,
"org.gnome.evolution.mail.config.accountWizard", view->wpages);
gtk_widget_hide (e_config_create_widget (E_CONFIG (view->edit->config)));
view->edit->emae_check_servers = emae_check_servers;
if (!view->original) {
......@@ -1077,13 +1080,14 @@ mail_account_view_construct (MailAccountView *view)
}
MailAccountView *
mail_account_view_new (EAccount *account)
mail_account_view_new (EAccount *account,
EMailSession *session)
{
MailAccountView *view = g_object_new (MAIL_ACCOUNT_VIEW_TYPE, NULL);
view->type = MAIL_VIEW_ACCOUNT;
view->uri = "account://";
view->original = account;
mail_account_view_construct (view);
mail_account_view_construct (view, session);
return view;
}
......
......@@ -87,7 +87,7 @@ typedef struct _MailAccountViewClass {
} MailAccountViewClass;
GType mail_account_view_get_type (void);
MailAccountView *mail_account_view_new (EAccount *account);
MailAccountView *mail_account_view_new (EAccount *account, EMailSession *session);
GtkWidget * mail_account_view_get_tab_widget (MailAccountView *mcv);
void mail_account_view_activate (MailAccountView *mcv, GtkWidget *tree, GtkWidget *folder_tree, GtkWidget *check_mail, GtkWidget *sort_by, gboolean act);
#endif
......@@ -38,7 +38,6 @@
#include "mail-decoration.h"
#include <mail/em-utils.h>
#include <mail/em-composer-utils.h>
#include <mail/mail-session.h>
#include <mail/mail-mt.h>
#include <mail/e-mail-store.h>
......@@ -68,6 +67,7 @@ static guint mail_capplet_shell_signals[LAST_SIGNAL];
struct _MailCappletShellPrivate {
EMailSession *session;
GtkWidget *box;
GtkWidget * top_bar;
......@@ -237,17 +237,23 @@ mail_capplet_shell_construct (MailCappletShell *shell, gint socket_id, gboolean
}
if (camel_init (e_get_user_data_dir (), TRUE) != 0)
exit (0);
camel_provider_init ();
shell->priv->session = e_mail_session_new ();
shell->view = mail_view_new ();
shell->view->session = shell->priv->session;
gtk_widget_show ((GtkWidget *)shell->view);
tmp = gtk_vbox_new (FALSE, 0);
gtk_box_pack_end ((GtkBox *)priv->box, (GtkWidget *)shell->view, TRUE, TRUE, 2);
/* This also initializes Camel, so it needs to happen early. */
mail_session_start ();
mail_config_init ();
mail_config_init (CAMEL_SESSION (shell->priv->session));
mail_msg_init ();
custom_dir = g_build_filename (e_get_user_data_dir (), "mail", NULL);
e_mail_store_init (custom_dir);
e_mail_store_init (shell->priv->session, custom_dir);
g_free (custom_dir);
if (just_druid) {
......
......@@ -438,11 +438,14 @@ mail_view_switch_to_settings (MailView* mv, MailViewChild *mpv)
}
static MailViewChild *
mail_view_add_account (MailView *mv, gpointer data, gboolean block)
mail_view_add_account (MailView *mv,
gpointer data,
gboolean block)
{
MailAccountView *msv = mail_account_view_new (data);
MailAccountView *msv;
gint position = 0;
msv = mail_account_view_new (data, mv->session);
gtk_widget_show ((GtkWidget *)msv);
if (!block)
mv->priv->current_view = (MailViewChild *)msv;
......
......@@ -24,6 +24,7 @@
#define _MAIL_VIEW_H_
#include <shell/e-shell-view.h>
#include <mail/e-mail-session.h>
#include "anjal-mail-view.h"
......@@ -48,6 +49,7 @@ typedef struct _MailViewPrivate MailViewPrivate;
typedef struct _MailView {
AnjalMailView parent;
EMailSession *session;
GtkWidget *tree; /* Actual tree */
GtkWidget *folder_tree;
GtkWidget *slider;
......
......@@ -77,7 +77,7 @@ filter_int_clone (EFilterElement *element)
EFilterInt *filter_int = E_FILTER_INT (element);
EFilterInt *clone;
clone = e_filter_int_new_type (
clone = (EFilterInt *) e_filter_int_new_type (
filter_int->type, filter_int->min, filter_int->max);
clone->val = filter_int->val;
......@@ -200,33 +200,26 @@ e_filter_int_init (EFilterInt *filter_int)
filter_int->max = G_MAXINT;
}
/**
* filter_int_new:
*
* Create a new EFilterInt object.
*
* Return value: A new #EFilterInt object.
**/
EFilterInt *
EFilterElement *
e_filter_int_new (void)
{
return g_object_new (E_TYPE_FILTER_INT, NULL);
}
EFilterInt *
EFilterElement *
e_filter_int_new_type (const gchar *type,
gint min,
gint max)
{
EFilterInt *filter_int;
filter_int = e_filter_int_new ();
filter_int = g_object_new (E_TYPE_FILTER_INT, NULL);
filter_int->type = g_strdup (type);
filter_int->min = min;
filter_int->max = max;
return filter_int;
return E_FILTER_ELEMENT (filter_int);
}
void
......
......@@ -65,8 +65,8 @@ struct _EFilterIntClass {
};
GType e_filter_int_get_type (void);
EFilterInt * e_filter_int_new (void);
EFilterInt * e_filter_int_new_type (const gchar *type,
EFilterElement *e_filter_int_new (void);
EFilterElement *e_filter_int_new_type (const gchar *type,
gint min,
gint max);
void e_filter_int_set_value (EFilterInt *f_int,
......
......@@ -425,14 +425,7 @@ e_filter_option_init (EFilterOption *option)
option->dynamic_func = NULL;
}
/**
* filter_option_new:
*
* Create a new EFilterOption object.
*
* Return value: A new #EFilterOption object.
**/
EFilterOption *
EFilterElement *
e_filter_option_new (void)
{
return g_object_new (E_TYPE_FILTER_OPTION, NULL);
......
......@@ -75,7 +75,7 @@ struct _EFilterOptionClass {
};
GType e_filter_option_get_type (void);
EFilterOption * e_filter_option_new (void);
EFilterElement *e_filter_option_new (void);
void e_filter_option_set_current (EFilterOption *option,
const gchar *name);
const gchar * e_filter_option_get_current (EFilterOption *option);
......
......@@ -54,6 +54,7 @@ mailinclude_HEADERS = \
e-mail-migrate.h \
e-mail-reader.h \
e-mail-reader-utils.h \
e-mail-session.h \
e-mail-sidebar.h \
e-mail-store.h \
e-mail-tag-editor.h \
......@@ -92,7 +93,6 @@ mailinclude_HEADERS = \
mail-mt.h \
mail-ops.h \
mail-send-recv.h \
mail-session.h \
mail-tools.h \
mail-vfolder.h \
message-list.h
......@@ -123,6 +123,7 @@ libevolution_mail_la_SOURCES = \
e-mail-migrate.c \
e-mail-reader.c \
e-mail-reader-utils.c \
e-mail-session.c \
e-mail-sidebar.c \
e-mail-store.c \
e-mail-tag-editor.c \
......@@ -161,7 +162,6 @@ libevolution_mail_la_SOURCES = \
mail-mt.c \
mail-ops.c \
mail-send-recv.c \
mail-session.c \
mail-tools.c \
mail-vfolder.c \
message-list.c
......
......@@ -22,9 +22,11 @@
*
*/
#include <string.h>
#include "e-mail-backend.h"
#include <string.h>
#include <libedataserver/e-data-server-util.h>
#include "e-util/e-account-utils.h"
#include "e-util/e-alert-dialog.h"
#include "e-util/e-binding.h"
......@@ -35,6 +37,7 @@
#include "mail/e-mail-local.h"
#include "mail/e-mail-migrate.h"
#include "mail/e-mail-session.h"
#include "mail/e-mail-store.h"
#include "mail/em-event.h"
#include "mail/em-folder-tree-model.h"
......@@ -42,7 +45,6 @@
#include "mail/mail-autofilter.h"
#include "mail/mail-folder-cache.h"
#include "mail/mail-ops.h"
#include "mail/mail-session.h"
#include "mail/mail-vfolder.h"
#define E_MAIL_BACKEND_GET_PRIVATE(obj) \
......@@ -52,14 +54,22 @@
#define QUIT_POLL_INTERVAL 1 /* seconds */
struct _EMailBackendPrivate {
gint placeholder; /* for future expansion */
EMailSession *session;
};
static gpointer parent_class;
enum {
PROP_0,
PROP_SESSION
};
/* FIXME Kill this thing. It's a horrible hack. */
extern gint camel_application_is_exiting;
G_DEFINE_ABSTRACT_TYPE (
EMailBackend,
e_mail_backend,
E_TYPE_SHELL_BACKEND)
static const gchar *
mail_shell_backend_get_data_dir (EShellBackend *backend)
{
......@@ -100,9 +110,11 @@ mail_backend_prepare_for_offline_cb (EShell *shell,
EMailBackend *backend)
{
GtkWindow *window;
EMailSession *session;
gboolean synchronize = FALSE;
window = e_shell_get_active_window (shell);
session = e_mail_backend_get_session (backend);
if (e_shell_get_network_available (shell))
synchronize = em_utils_prompt_user (
......@@ -110,7 +122,8 @@ mail_backend_prepare_for_offline_cb (EShell *shell,
if (!synchronize) {
mail_cancel_all ();
camel_session_set_network_available (session, FALSE);
camel_session_set_network_available (
CAMEL_SESSION (session), FALSE);
}
e_mail_store_foreach (
......@@ -135,7 +148,10 @@ mail_backend_prepare_for_online_cb (EShell *shell,
EActivity *activity,
EMailBackend *backend)
{
camel_session_set_online (session, TRUE);
EMailSession *session;
session = e_mail_backend_get_session (backend);
camel_session_set_online (CAMEL_SESSION (session), TRUE);
e_mail_store_foreach (
(GHFunc) mail_store_prepare_for_online_cb, activity);
......@@ -203,7 +219,7 @@ mail_backend_poll_to_quit (EActivity *activity)
static void
mail_backend_ready_to_quit (EActivity *activity)
{
mail_session_shutdown ();
camel_shutdown ();
emu_free_mail_cache ();
/* Do this last. It may terminate the process. */
......@@ -304,18 +320,26 @@ mail_backend_quit_requested_cb (EShell *shell,
static void
mail_backend_folder_deleted_cb (MailFolderCache *folder_cache,
CamelStore *store,
const gchar *uri)
const gchar *uri,
EMailBackend *backend)
{
mail_filter_delete_uri (store, uri);
EMailSession *session;
session = e_mail_backend_get_session (backend);
mail_filter_delete_uri (session, store, uri);
}
static void
mail_backend_folder_renamed_cb (MailFolderCache *folder_cache,
CamelStore *store,
const gchar *old_uri,
const gchar *new_uri)
const gchar *new_uri,
EMailBackend *backend)
{
mail_filter_rename_uri (store, old_uri, new_uri);
EMailSession *session;
session = e_mail_backend_get_session (backend);
mail_filter_rename_uri (session, store, old_uri, new_uri);
}
static void
......@@ -370,31 +394,86 @@ mail_backend_folder_changed_cb (MailFolderCache *folder_cache,
}
static gboolean
mail_backend_idle_cb (EShellBackend *shell_backend)
mail_backend_idle_cb (EMailBackend *backend)
{
EMailSession *session;
EShellBackend *shell_backend;
const gchar *data_dir;
session = e_mail_backend_get_session (backend);
shell_backend = E_SHELL_BACKEND (backend);
data_dir = e_shell_backend_get_data_dir (shell_backend);
e_mail_store_init (data_dir);
e_mail_store_init (session, data_dir);
return FALSE;
}
static void
mail_backend_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_SESSION:
g_value_set_object (
value,
e_mail_backend_get_session (
E_MAIL_BACKEND (object)));
return;
}
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
mail_backend_dispose (GObject *object)
{
EMailBackendPrivate *priv;
priv = E_MAIL_BACKEND_GET_PRIVATE (object);
if (priv->session != NULL) {
g_object_unref (priv->session);
priv->session = NULL;
}
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_mail_backend_parent_class)->dispose (object);
}
static void
mail_backend_constructed (GObject *object)
{
EMailBackendPrivate *priv;
EShell *shell;
EShellBackend *shell_backend;
MailFolderCache *folder_cache;
EMFolderTreeModel *folder_tree_model;
priv = E_MAIL_BACKEND_GET_PRIVATE (object);
shell_backend = E_SHELL_BACKEND (object);
shell = e_shell_backend_get_shell (shell_backend);
/* This also initializes Camel, so it needs to happen early. */
mail_session_start ();
if (camel_init (e_get_user_data_dir (), TRUE) != 0)
exit (0);
e_binding_new (shell, "online", session, "online");
e_account_combo_box_set_session (session); /* XXX Don't ask... */
camel_provider_init ();
priv->session = e_mail_session_new ();
e_binding_new (shell, "online", priv->session, "online");
/* FIXME This is an evil hack that needs to die.
* Give EAccountComboBox a CamelSession property. */
e_account_combo_box_set_session (CAMEL_SESSION (priv->session));
/* FIXME EMailBackend should own the default EMFolderTreeModel. */
folder_tree_model = em_folder_tree_model_get_default ();
em_folder_tree_model_set_session (folder_tree_model, priv->session);
folder_cache = mail_folder_cache_get_default ();
......@@ -420,17 +499,19 @@ mail_backend_constructed (GObject *object)
g_signal_connect (
folder_cache, "folder-deleted",
G_CALLBACK (mail_backend_folder_deleted_cb), NULL);
G_CALLBACK (mail_backend_folder_deleted_cb),
shell_backend);
g_signal_connect (
folder_cache, "folder-renamed",
G_CALLBACK (mail_backend_folder_renamed_cb), NULL);
G_CALLBACK (mail_backend_folder_renamed_cb),
shell_backend);
g_signal_connect (
folder_cache, "folder-changed",
G_CALLBACK (mail_backend_folder_changed_cb), shell);
mail_config_init ();
mail_config_init (CAMEL_SESSION (priv->session));
mail_msg_init ();
/* Defer initializing CamelStores until after the main loop
......@@ -439,54 +520,46 @@ mail_backend_constructed (GObject *object)
}
static void
mail_backend_class_init (EMailBackendClass *class)
e_mail_backend_class_init (EMailBackendClass *class)
{
GObjectClass *object_class;
EShellBackendClass *shell_backend_class;
parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (EMailBackendPrivate));
object_class = G_OBJECT_CLASS (class);
object_class->get_property = mail_backend_get_property;
object_class->dispose = mail_backend_dispose;
object_class->constructed = mail_backend_constructed;
shell_backend_class = E_SHELL_BACKEND_CLASS (class);
shell_backend_class->migrate = e_mail_migrate;
shell_backend_class->get_data_dir = mail_shell_backend_get_data_dir;
shell_backend_class->get_config_dir = mail_shell_backend_get_config_dir;
g_object_class_install_property (
object_class,
PROP_SESSION,
g_param_spec_object (
"session",
NULL,
NULL,
E_TYPE_MAIL_SESSION,
G_PARAM_READABLE));
}
static void
mail_backend_init (EMailBackend *backend)
e_mail_backend_init (EMailBackend *backend)
{
backend->priv = E_MAIL_BACKEND_GET_PRIVATE (backend);
}
GType
e_mail_backend_get_type (void)
EMailSession *
e_mail_backend_get_session (EMailBackend *backend)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static const GTypeInfo type_info = {
sizeof (EMailBackendClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) mail_backend_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (EMailBackend),
0, /* n_preallocs */
(GInstanceInitFunc) mail_backend_init,
NULL /* value_table */
};
type = g_type_register_static (
E_TYPE_SHELL_BACKEND, "EMailBackend", &type_info,
G_TYPE_FLAG_ABSTRACT);
}
g_return_val_if_fail (E_IS_MAIL_BACKEND (backend), NULL);
return type;
return backend->priv->session;
}
gboolean
......
......@@ -26,6 +26,7 @@
#ifndef E_MAIL_BACKEND_H
#define E_MAIL_BACKEND_H
#include <mail/e-mail-session.h>
#include <shell/e-shell-backend.h>
/* Standard GObject macros */
......@@ -69,6 +70,7 @@ struct _EMailBackendClass {
};
GType e_mail_backend_get_type (void);
EMailSession * e_mail_backend_get_session (EMailBackend *backend);
gboolean e_mail_backend_delete_junk_policy_decision
(EMailBackend *backend);
gboolean e_mail_backend_empty_trash_policy_decision
......
......@@ -48,9 +48,9 @@
#define MAIL_BROWSER_GCONF_PREFIX "/apps/evolution/mail/mail_browser"
struct _EMailBrowserPrivate {
EMailBackend *backend;
GtkUIManager *ui_manager;
EFocusTracker *focus_tracker;
EShellBackend *shell_backend;
GtkActionGroup *action_group;
EMFormatHTMLDisplay *formatter;
......@@ -65,9 +65,9 @@ struct _EMailBrowserPrivate {
enum {
PROP_0,
PROP_BACKEND,
PROP_FOCUS_TRACKER,
PROP_GROUP_BY_THREADS,
PROP_SHELL_BACKEND,
PROP_SHOW_DELETED,
PROP_UI_MANAGER
};
......@@ -339,12 +339,13 @@ mail_browser_status_message_cb (EMailBrowser *browser,
}
static void
mail_browser_set_shell_backend (EMailBrowser *browser,
EShellBackend *shell_backend)
mail_browser_set_backend (EMailBrowser *browser,
EMailBackend *backend)
{
g_return_if_fail (browser->priv->shell_backend == NULL);
g_return_if_fail (E_IS_MAIL_BACKEND (backend));
g_return_if_fail (browser->priv->backend == NULL);
browser->priv->shell_backend = g_object_ref (shell_backend);
browser->priv->backend = g_object_ref (backend);
}
static void
......@@ -354,18 +355,18 @@ mail_browser_set_property (GObject *object,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_BACKEND:
mail_browser_set_backend (
E_MAIL_BROWSER (object),
g_value_get_object (value));
return;
case PROP_GROUP_BY_THREADS:
e_mail_reader_set_group_by_threads (
E_MAIL_READER (object),
g_value_get_boolean (value));
return;
case PROP_SHELL_BACKEND:
mail_browser_set_shell_backend (
E_MAIL_BROWSER (object),
g_value_get_object (value));
return;
case PROP_SHOW_DELETED:
e_mail_browser_set_show_deleted (
E_MAIL_BROWSER (object),
......@@ -383,6 +384,12 @@ mail_browser_get_property (GObject *object,
GParamSpec *pspec)
{
switch (property_id) {
case PROP_BACKEND:
g_value_set_object (
value, e_mail_reader_get_backend (
E_MAIL_READER (object)));
return;
case PROP_FOCUS_TRACKER:
g_value_set_object (
value, e_mail_browser_get_focus_tracker (
......@@ -395,12 +402,6 @@ mail_browser_get_property (GObject *object,
E_MAIL_READER (object)));
return;
case PROP_SHELL_BACKEND:
g_value_set_object (
value, e_mail_reader_get_shell_backend (
E_MAIL_READER (object)));
return;
case PROP_SHOW_DELETED:
g_value_set_boolean (
value, e_mail_browser_get_show_deleted (
......@@ -424,6 +425,11 @@ mail_browser_dispose (GObject *object)
priv = E_MAIL_BROWSER_GET_PRIVATE (object);
if (priv->backend != NULL) {
g_object_unref (priv->backend);
priv->backend = NULL;
}
if (priv->ui_manager != NULL) {
g_object_unref (priv->ui_manager);
priv->ui_manager = NULL;
......@@ -434,11 +440,6 @@ mail_browser_dispose (GObject *object)
priv->focus_tracker = NULL;
}
if (priv->shell_backend != NULL) {
g_object_unref (priv->shell_backend);
priv->shell_backend = NULL;
}
if (priv->action_group != NULL) {
g_object_unref (priv->action_group);
priv->action_group = NULL;
......@@ -485,6 +486,7 @@ mail_browser_constructed (GObject *object)
EMailBrowserPrivate *priv;
EMFormatHTML *formatter;
EMailReader *reader;
EMailBackend *backend;
EShellBackend *shell_backend;
EShell *shell;
EFocusTracker *focus_tracker;
......@@ -509,8 +511,9 @@ mail_browser_constructed (GObject *object)
priv = E_MAIL_BROWSER_GET_PRIVATE (object);
reader = E_MAIL_READER (object);
backend = e_mail_reader_get_backend (reader);
shell_backend = e_mail_reader_get_shell_backend (reader);
shell_backend = E_SHELL_BACKEND (backend);
shell = e_shell_backend_get_shell (shell_backend);
ui_manager = e_ui_manager_new ();
......@@ -527,7 +530,7 @@ mail_browser_constructed (GObject *object)
/* The message list is a widget, but it is not shown in the browser.
* Unfortunately, the widget is inseparable from its model, and the
* model is all we need. */
priv->message_list = message_list_new (shell_backend);
priv->message_list = message_list_new (backend);
g_object_ref_sink (priv->message_list);