aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-summary.c
blob: 9b9b941bf165f4f86dd2eaebd100f310dd60da20 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* mail-summary.c
 *
 * Authors: Iain Holmes <iain@helixcode.com>
 *
 * 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.
 */

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

#include <bonobo.h>

#include "camel.h"
#include <gnome.h>
#include "mail.h"       /* YUCK FIXME */
#include "mail-tools.h"
#include "mail-ops.h"
#include "mail-vfolder.h"

#include "Evolution.h"
#include "evolution-storage.h"

#include "mail-local-storage.h"

#include "filter/vfolder-context.h"

#include <executive-summary/evolution-services/executive-summary-component.h>

typedef struct {
    CamelFolder *folder;

    char *name;
    int total, unread;
} FolderSummary;

typedef struct {
    ExecutiveSummaryComponent *component;

    GHashTable *folder_to_summary;
    FolderSummary **folders;
    int numfolders;

    char *html;
} MailSummary;

#define SUMMARY_IN() g_print ("IN: %s: %d\n", __FUNCTION__, __LINE__);
#define SUMMARY_OUT() g_print ("OUT: %s: %d\n", __FUNCTION__, __LINE__);

static int queue_len = 0;

extern char *evolution_dir;

#define MAIN_READER main_compipe[0]
#define MAIN_WRITER main_compipe[1]
#define DISPATCH_READER dispatch_compipe[0]
#define DISPATCH_WRITER dispatch_compipe[1]

static int main_compipe[2] = {-1, -1};
static int dispatch_compipe[2] = {-1, -1};

GIOChannel *summary_chan_reader = NULL;

static void do_changed (MailSummary *summary);

/* Read a message from the pipe */
static gboolean
read_msg (GIOChannel *source,
      GIOCondition condition,
      gpointer user_data)
{
    MailSummary *summary;
    int size;

    summary = g_new0 (MailSummary, 1);
    g_io_channel_read (source, (gchar *) summary,
               sizeof (MailSummary) / sizeof (gchar), &size);

    if (size != sizeof (MailSummary)) {
        g_warning (_("Incomplete message written on pipe!"));
        return TRUE;
    }

    do_changed (summary);
    g_free (summary);

    return TRUE;
}

/* check_compipes: */
static void
check_compipes (void)
{
    if (MAIN_READER < 0) {
        if (pipe (main_compipe) < 0) {
            g_warning ("Call to pipe failed");
            return;
        }

        summary_chan_reader = g_io_channel_unix_new (MAIN_READER);
        g_io_add_watch (summary_chan_reader, G_IO_IN, read_msg, NULL);
    }

    if (DISPATCH_READER < 0) {
        if (pipe (dispatch_compipe) < 0) {
            g_warning ("Call to pipe failed");
            return;
        }
    }
}

static char *
generate_html_summary (MailSummary *summary)
{
    char *ret_html = NULL, *tmp;
    FolderSummary *fs;
    int i;

    /* Inbox first */
    fs = summary->folders[0];

    tmp = g_strdup_printf ("<table><tr><td><b>%s:</b>"
                   "<td align=\"right\">%d/%d</td></tr>",
                   fs->name, fs->unread, fs->total);

    ret_html = g_strdup (tmp);
    for (i = 1; i < summary->numfolders; i++) {
        char *tmp2; 

        fs = summary->folders[i];
        tmp2 = g_strdup_printf ("<tr><td><ul><li>%s:</li></ul></td>"
                    "<td align=\"right\">%d/%d</td></tr>",
                    fs->name, fs->unread, fs->total);

        tmp = ret_html;
        ret_html = g_strconcat (ret_html, tmp2, NULL);
        g_free (tmp);
        g_free (tmp2);
    }

    tmp = ret_html;
    ret_html = g_strconcat (ret_html, "</table>", NULL);
    g_free (tmp);

    return ret_html;
}
    
static void
do_changed (MailSummary *summary)
{
    char *ret_html;

    ret_html = generate_html_summary (summary);
    executive_summary_component_update (summary->component, ret_html);
    g_free (ret_html);
}

/* These two callbacks are called from the Camel thread,
   which can't make any CORBA calls, or else ORBit locks up,
   and likewise the thread that can call ORBit, cannot call
   camel.

   So, when the callbacks are triggered, they generate a MailSummary
   structure and write this onto a pipe. The ORBit calling thread
   detects when something is written to the pipe and creates its own
   MailSummary structure, and calls the appropriate CORBA calls.

   Same theory as mail-threads.c, but a lot less complicated
   as there is only one way communication, and only one type of message
*/
static void
folder_changed_cb (CamelObject *folder,
           gpointer event_data,
           gpointer user_data)
{
    MailSummary *summary;
    FolderSummary *fs;

    summary = (MailSummary *) user_data;
    fs = g_hash_table_lookup (summary->folder_to_summary, folder);
    if (fs == NULL) {
        g_warning ("%s: Unknown folder", __FUNCTION__);
        return;
    }

    fs->total = camel_folder_get_message_count (fs->folder);
    fs->unread = camel_folder_get_unread_message_count (fs->folder);

    write (MAIN_WRITER, summary, sizeof (MailSummary));
    queue_len++;

    return;
}

static void
message_changed_cb (CamelObject *folder,
            gpointer event_data,
            gpointer user_data)
{
    MailSummary *summary;
    FolderSummary *fs;

    summary = (MailSummary *)user_data;
    fs = g_hash_table_lookup (summary->folder_to_summary, folder);
    if (fs == NULL) {
        g_warning ("%s: Unknown folder.", __FUNCTION__);
        return;
    }

    fs->unread = camel_folder_get_unread_message_count (fs->folder);
    fs->total = camel_folder_get_message_count (fs->folder);

    write (MAIN_WRITER, summary, sizeof (MailSummary));
    queue_len++;
    
    return;
}

static void
generate_folder_summarys (MailSummary *summary)
{
    int numfolders = 1; /* Always at least the Inbox */
    char *user, *system;
    FilterRule *rule;
    VfolderContext *context;
    FolderSummary *fs;
    CamelException *ex;
    int i;

    user = g_strdup_printf ("%s/vfolders.xml", evolution_dir);
    system = g_strdup_printf ("%s/evolution/vfoldertypes.xml", EVOLUTION_DATADIR);

    context = vfolder_context_new ();
    rule_context_load ((RuleContext *)context, system, user, NULL, NULL);
    g_free (user);
    g_free (system);

    rule = NULL;
    while ((rule = rule_context_next_rule ((RuleContext *)context, rule))){
        g_print ("rule->name: %s\n", rule->name);
        numfolders++;
    }

    summary->folders = g_new (FolderSummary *, numfolders);

    /* Inbox */
    fs = summary->folders[0] = g_new (FolderSummary, 1);
    fs->name = g_strdup ("Inbox");
    mail_tool_camel_lock_up ();
    ex = camel_exception_new ();
    fs->folder = mail_tool_get_local_inbox (ex);
    fs->total = camel_folder_get_message_count (fs->folder);
    fs->unread = camel_folder_get_unread_message_count (fs->folder);
    camel_exception_free (ex);
    mail_tool_camel_lock_down ();
    camel_object_hook_event (CAMEL_OBJECT (fs->folder), "folder_changed",
                 (CamelObjectEventHookFunc) folder_changed_cb,
                 summary);
    camel_object_hook_event (CAMEL_OBJECT (fs->folder), "message_changed",
                 (CamelObjectEventHookFunc) message_changed_cb,
                 summary);
    g_hash_table_insert (summary->folder_to_summary, fs->folder, fs);
    

    summary->numfolders = 1;

    for (i = 1, rule = NULL; i < numfolders; i++) {
        char *uri;

        ex = camel_exception_new ();
        fs = summary->folders[i] = g_new (FolderSummary, 1);
        rule = rule_context_next_rule ((RuleContext *)context, rule);
        fs->name = g_strdup (rule->name);

        uri = g_strconcat ("vfolder:", rule->name, NULL);
        mail_tool_camel_lock_up ();
        fs->folder = vfolder_uri_to_folder (uri, ex);
        g_free (uri);

        fs->total = camel_folder_get_message_count (fs->folder);
        fs->unread = camel_folder_get_unread_message_count (fs->folder);

        /* Connect to each folder */
        camel_object_hook_event (CAMEL_OBJECT (fs->folder), 
                     "folder_changed",
                     (CamelObjectEventHookFunc) folder_changed_cb,
                     summary);
        camel_object_hook_event (CAMEL_OBJECT (fs->folder), 
                     "message_changed",
                     (CamelObjectEventHookFunc) message_changed_cb,
                     summary);
        g_hash_table_insert (summary->folder_to_summary, fs->folder, fs);
        summary->numfolders++;

        camel_exception_free (ex);
        mail_tool_camel_lock_down ();
    }

    gtk_object_destroy (GTK_OBJECT (context));
}

char *
create_summary_view (ExecutiveSummaryComponent *component,
             char **title,
             char **icon,
             void *closure)
{
    char *ret_html;
    MailSummary *summary;

    /* Strdup the title and icon */
    *title = g_strdup ("Mailbox summary");
    *icon = g_strdup ("envelope.png");

    summary = g_new (MailSummary, 1);
    summary->component = component;
    summary->folder_to_summary = g_hash_table_new (NULL, NULL);

    generate_folder_summarys (summary);

    ret_html = generate_html_summary (summary);

    check_compipes ();

    return ret_html;
}