aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-local-storage.c
blob: 90bd32a081e713d3245b9ac88fce96c010e3a423 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* mail-local-storage.h
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

/* This handles the interfacing with the shell's local storage.  */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib.h>
#include <gtk/gtk.h>
#include <orb/orbit.h>

#include "e-folder-tree.h"
#include "evolution-storage-listener.h"

#include "mail-local-storage.h"


/* Static stuff.  Sigh, it sucks, but it fits in the way the whole mail
   compnent is written.  */

/* The interface to the local storage.  */
static Evolution_LocalStorage corba_local_storage = CORBA_OBJECT_NIL;

/* The listener on our side.  We get notified of things happening in the local
   storage through this.  */
static EvolutionStorageListener *local_storage_listener = NULL;

/* The folder set.  The folder data is an Evolution_Folder, allocated through
   CORBA normally.  */
static EFolderTree *folder_tree = NULL;


/* Folder destroy notification function for the `folder_tree'.  */

static void
folder_tree_folder_notify_cb (EFolderTree *tree,
                  const char *path,
                  void *data,
                  void *closure)
{
    Evolution_Folder *corba_folder;

    corba_folder = (Evolution_Folder *) data;
    CORBA_free (corba_folder);
}


/* Callbacks for the EvolutionStorageListner signals.  */

static void
local_storage_destroyed_cb (EvolutionStorageListener *storage_listener,
                void *data)
{
    /* FIXME: Dunno how to handle this yet.  */
    g_warning ("%s -- The LocalStorage has gone?!", __FILE__);
}

static void
local_storage_new_folder_cb (EvolutionStorageListener *storage_listener,
                 const char *path,
                 const Evolution_Folder *folder,
                 void *data)
{
    Evolution_Folder *copy_of_folder;
    CORBA_Environment ev;

    if (strcmp (folder->type, "mail") != 0)
        return;

    CORBA_exception_init (&ev);

#if 0
    /* This is how we could do to display extra information about the
           folder.  */
    display_name = g_strconcat (folder->display_name, _(" (XXX unread)"), NULL);
    Evolution_LocalStorage_set_display_name (corba_local_storage, path, display_name, &ev);
#endif

    copy_of_folder = Evolution_Folder__alloc ();
    copy_of_folder->type         = CORBA_string_dup (folder->type);
    copy_of_folder->description  = CORBA_string_dup (folder->description);
    copy_of_folder->display_name = CORBA_string_dup (folder->display_name);
    copy_of_folder->physical_uri = CORBA_string_dup (folder->physical_uri);

    e_folder_tree_add (folder_tree, path, copy_of_folder);

    CORBA_exception_free (&ev);
}

static void
local_storage_removed_folder_cb (EvolutionStorageListener *storage_listener,
                 const char *path,
                 void *data)
{
    /* Prevent a warning from `e_folder_tree_remove()'.  */
    if (e_folder_tree_get_folder (folder_tree, path) == NULL)
        return;

    e_folder_tree_remove (folder_tree, path);
}


gboolean
mail_local_storage_startup (EvolutionShellClient *shell_client)
{
    Evolution_StorageListener corba_local_storage_listener;

    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    corba_local_storage = evolution_shell_client_get_local_storage (shell_client);
    if (corba_local_storage == CORBA_OBJECT_NIL) {
        CORBA_exception_free (&ev);
        return FALSE;
    }

    local_storage_listener = evolution_storage_listener_new ();
    corba_local_storage_listener = evolution_storage_listener_corba_objref (local_storage_listener);

    gtk_signal_connect (GTK_OBJECT (local_storage_listener), "destroyed",
                GTK_SIGNAL_FUNC (local_storage_destroyed_cb), NULL);
    gtk_signal_connect (GTK_OBJECT (local_storage_listener), "new_folder",
                GTK_SIGNAL_FUNC (local_storage_new_folder_cb), NULL);
    gtk_signal_connect (GTK_OBJECT (local_storage_listener), "removed_folder",
                GTK_SIGNAL_FUNC (local_storage_removed_folder_cb), NULL);

    Evolution_Storage_add_listener (corba_local_storage, corba_local_storage_listener, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("%s -- Cannot add a listener to the Local Storage.", __FILE__);

        gtk_object_unref (GTK_OBJECT (local_storage_listener));

        Bonobo_Unknown_unref (corba_local_storage, &ev);
        CORBA_Object_release (corba_local_storage, &ev);

        CORBA_exception_free (&ev);
        return FALSE;
    }

    CORBA_exception_free (&ev);

    folder_tree = e_folder_tree_new (folder_tree_folder_notify_cb, NULL);

    return TRUE;
}

void
mail_local_storage_shutdown (void)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    Bonobo_Unknown_unref (corba_local_storage, &ev);
    CORBA_Object_release (corba_local_storage, &ev);
    corba_local_storage = CORBA_OBJECT_NIL;

    gtk_object_unref (GTK_OBJECT (local_storage_listener));
    local_storage_listener = NULL;

    gtk_object_unref (GTK_OBJECT (folder_tree));
    folder_tree = NULL;

    CORBA_exception_free (&ev);
}