aboutsummaryrefslogtreecommitdiffstats
path: root/mail/folder-browser.c
blob: 297279c73f2d2e2cf8e3b3e8f8aa477f03e11590 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * folder-browser.c: Folder browser top level component
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
 *
 * (C) 2000 Helix Code, Inc.
 */
#include <config.h>
#include <gnome.h>
#include "e-util/e-util.h"
#include "folder-browser.h"
#include "mail.h"
#include "message-list.h"
#include <widgets/e-paned/e-vpaned.h>


#define PARENT_TYPE (gtk_table_get_type ())

static GtkObjectClass *folder_browser_parent_class;


#define PROPERTY_FOLDER_URI          "folder_uri"
#define PROPERTY_MESSAGE_PREVIEW     "message_preview"

#define PROPERTY_FOLDER_URI_IDX      1
#define PROPERTY_MESSAGE_PREVIEW_IDX 2



static void
folder_browser_destroy (GtkObject *object)
{
    FolderBrowser *folder_browser = FOLDER_BROWSER (object);
    
    if (folder_browser->shell) {
        CORBA_Environment ev;

        CORBA_exception_init (&ev);
        Bonobo_Unknown_unref (folder_browser->shell, &ev);
        CORBA_exception_free (&ev);
    }
    
    if (folder_browser->uri)
        g_free (folder_browser->uri);

    if (folder_browser->folder)
        gtk_object_unref (GTK_OBJECT (folder_browser->folder));
    
    if (folder_browser->message_list)
        bonobo_object_unref (BONOBO_OBJECT (folder_browser->message_list));

    folder_browser_parent_class->destroy (object);
}

static void
folder_browser_class_init (GtkObjectClass *object_class)
{
    object_class->destroy = folder_browser_destroy;

    folder_browser_parent_class = gtk_type_class (PARENT_TYPE);
}

static gboolean
folder_browser_load_folder (FolderBrowser *fb, const char *name)
{
    char *store_name, *msg;
    CamelStore *store;
    CamelFolder *new_folder;
    CamelException *ex;
    gboolean new_folder_exists = FALSE;

    if (strncmp (name, "file:", 5) != 0) {
        char *msg;

        msg = g_strdup_printf ("Can't open URI %s", name);
        gnome_error_dialog (msg);
        g_free (msg);
        return FALSE;
    }

    /* Change "file:" to "mbox:". */
    store_name = g_strdup_printf ("mbox:%s", name + 5);

    ex = camel_exception_new ();
    store = camel_session_get_store (session, store_name, ex);
    g_free (store_name);
    if (store) {
        new_folder = camel_store_get_folder (store, "mbox", ex);
        gtk_object_unref (GTK_OBJECT (store));
    }

    if (camel_exception_get_id (ex)) {
        msg = g_strdup_printf ("Unable to get folder %s: %s\n", name,
                       camel_exception_get_description (ex));
        gnome_error_dialog (msg);
        camel_exception_free (ex);
        return FALSE;
    }
    
    /* If the folder does not exist, we don't want to show it */
    new_folder_exists = camel_folder_exists (new_folder, ex);
    if (camel_exception_get_id (ex)) {
        msg = g_strdup_printf ("Unable to test if folder %s "
                       "exists: %s\n", name,
                       camel_exception_get_description (ex));
        gnome_error_dialog (msg);
        camel_exception_free (ex);
        return FALSE;
    }
    camel_exception_free (ex);
        
    if (!new_folder_exists) {
        gtk_object_unref (GTK_OBJECT (new_folder));
        return FALSE;
    }
    
    if (fb->folder)
        gtk_object_unref (GTK_OBJECT (fb->folder));
    
    fb->folder = new_folder;
    
    message_list_set_folder (fb->message_list, new_folder);

    return TRUE;
}

#define EQUAL(a,b) (strcmp (a,b) == 0)

void
folder_browser_set_uri (FolderBrowser *folder_browser, const char *uri)
{
    if (folder_browser->uri)
        g_free (folder_browser->uri);

    folder_browser->uri = g_strdup (uri);
    folder_browser_load_folder (folder_browser, folder_browser->uri);
}

void
folder_browser_set_message_preview (FolderBrowser *folder_browser, gboolean show_message_preview)
{
    if (folder_browser->preview_shown == show_message_preview)
        return;

    g_warning ("FIXME: implement me");
}

static void
get_prop (BonoboPropertyBag *bag,
      BonoboArg         *arg,
      guint              arg_id,
      gpointer           user_data)
{
    FolderBrowser *fb = user_data;

    switch (arg_id) {

    case PROPERTY_FOLDER_URI_IDX:
        if (fb && fb->uri)
            BONOBO_ARG_SET_STRING (arg, fb->uri);
        else
            BONOBO_ARG_SET_STRING (arg, "");
        break;

    case PROPERTY_MESSAGE_PREVIEW_IDX:
        g_warning ("Implement me; no return value");
        BONOBO_ARG_SET_BOOLEAN (arg, FALSE);
        break;

    default:
        g_warning ("Unhandled arg %d\n", arg_id);
    }
}

static void
set_prop (BonoboPropertyBag *bag,
      const BonoboArg   *arg,
      guint              arg_id,
      gpointer           user_data)
{
    FolderBrowser *fb = user_data;

    switch (arg_id) {

    case PROPERTY_FOLDER_URI_IDX:
        folder_browser_set_uri (fb, BONOBO_ARG_GET_STRING (arg));
        break;

    case PROPERTY_MESSAGE_PREVIEW_IDX:
        folder_browser_set_message_preview (fb, BONOBO_ARG_GET_BOOLEAN (arg));
        break;

    default:
        g_warning ("Unhandled arg %d\n", arg_id);
        break;
    }
}

static void
folder_browser_properties_init (FolderBrowser *fb)
{
    fb->properties = bonobo_property_bag_new (get_prop, set_prop, fb);

    bonobo_property_bag_add (
        fb->properties, PROPERTY_FOLDER_URI, PROPERTY_FOLDER_URI_IDX,
        BONOBO_ARG_STRING, NULL, _("The URI that the Folder Browser will display"), 0);
    bonobo_property_bag_add (
        fb->properties, PROPERTY_MESSAGE_PREVIEW, PROPERTY_MESSAGE_PREVIEW_IDX,
        BONOBO_ARG_BOOLEAN, NULL, _("Whether a message preview should be shown"), 0);
}

static char * search_options[] = {
    "Body or subject contains",
    "Body contains",
    "Subject contains",
    "Body does not contain",
    "Subject does not contain",
    NULL
};

/* %s is replaced by the whole search string in quotes ...
   possibly could split the search string into words as well ? */
static char * search_string[] = {
    "(or (body-contains %s) (match-all (header-contains \"Subject\" %s)))",
    "(body-contains %s)",
    "(match-all (header-contains \"Subject\" %s)",
    "(match-all (not (body-contains %s)))",
    "(match-all (not (header-contains \"Subject\" %s)))"
};

static void
search_set(FolderBrowser *fb)
{
    GtkWidget *widget;
    GString *out;
    char *str;
    int index;
    char *text;

    text = gtk_entry_get_text((GtkEntry *)fb->search_entry);

    if (text == NULL || text[0] == 0) {
        message_list_set_search (fb->message_list, NULL);
        return;
    }

    widget = gtk_menu_get_active (GTK_MENU(GTK_OPTION_MENU(fb->search_menu)->menu));
    index = (int)gtk_object_get_data((GtkObject *)widget, "search_option");
    if (index > sizeof(search_string)/sizeof(search_string[0]))
        index = 0;
    str = search_string[index];

    out = g_string_new("");
    while (*str) {
        if (str[0] == '%' && str[1]=='s') {
            str+=2;
            g_string_sprintfa(out, "\"%s\"", text);
        } else {
            g_string_append_c(out, *str);
            str++;
        }
    }
    message_list_set_search (fb->message_list, out->str);
    g_string_free(out, TRUE);
}

static void
search_menu_deactivate(GtkWidget *menu, FolderBrowser *fb)
{
    search_set(fb);
}

static GtkWidget *
create_option_menu (char **menu_list, int item, void *data)
{
    GtkWidget *omenu;
    GtkWidget *menu;
    int i = 0;
       
    omenu = gtk_option_menu_new ();
    menu = gtk_menu_new ();
    while (*menu_list){
        GtkWidget *entry;

        entry = gtk_menu_item_new_with_label (*menu_list);
        gtk_widget_show (entry);
        gtk_object_set_data((GtkObject *)entry, "search_option", (void *)i);
        gtk_menu_append (GTK_MENU (menu), entry);
        menu_list++;
        i++;
    }
    gtk_option_menu_set_menu (GTK_OPTION_MENU (omenu), menu);
    gtk_option_menu_set_history (GTK_OPTION_MENU (omenu), item);
    gtk_widget_show (omenu);

    gtk_signal_connect (GTK_OBJECT (menu), 
                "deactivate",
                GTK_SIGNAL_FUNC (search_menu_deactivate), data);

    return omenu;
}

static void
search_activate(GtkEntry *entry, FolderBrowser *fb)
{
    search_set(fb);
}

static void
folder_browser_gui_init (FolderBrowser *fb)
{
    GtkWidget *hbox, *label;

    /*
     * The panned container
     */
    fb->vpaned = e_vpaned_new ();
    gtk_widget_show (fb->vpaned);

    gtk_table_attach (
        GTK_TABLE (fb), fb->vpaned,
        0, 1, 1, 3,
        GTK_FILL | GTK_EXPAND,
        GTK_FILL | GTK_EXPAND,
        0, 0);

    /* quick-search entry */
    hbox = gtk_hbox_new(FALSE, 3);
    gtk_widget_show(hbox);
    fb->search_entry = gtk_entry_new();
    gtk_widget_show(fb->search_entry);
    gtk_signal_connect(GTK_OBJECT (fb->search_entry), "activate", search_activate, fb);
    /* gtk_signal_connect(fb->search_entry, "changed", search_activate, fb); */
    label = gtk_label_new("Search");
    gtk_widget_show(label);
    fb->search_menu = create_option_menu(search_options, 0, fb);
    gtk_box_pack_end((GtkBox *)hbox, fb->search_entry, FALSE, FALSE, 3);
    gtk_box_pack_end((GtkBox *)hbox, fb->search_menu, FALSE, FALSE, 3);
    gtk_box_pack_end((GtkBox *)hbox, label, FALSE, FALSE, 3);
    gtk_table_attach (
        GTK_TABLE (fb), hbox,
        0, 1, 0, 1,
        GTK_FILL | GTK_EXPAND,
        0,
        0, 0);

    fb->message_list_w = message_list_get_widget (fb->message_list);
    e_paned_add1 (E_PANED (fb->vpaned), fb->message_list_w);
    gtk_widget_show (fb->message_list_w);

    e_paned_add2 (E_PANED (fb->vpaned), GTK_WIDGET (fb->mail_display));
    e_paned_set_position (E_PANED (fb->vpaned), 200);

    gtk_widget_show (GTK_WIDGET (fb->mail_display));
    gtk_widget_show (GTK_WIDGET (fb));

}

static void
folder_browser_init (GtkObject *object)
{
}

static void
my_folder_browser_init (GtkObject *object)
{
    FolderBrowser *fb = FOLDER_BROWSER (object);

    /*
     * Setup parent class fields.
     */ 
    GTK_TABLE (fb)->homogeneous = FALSE;
    gtk_table_resize (GTK_TABLE (fb), 1, 2);

    /*
     * Our instance data
     */
    fb->message_list = MESSAGE_LIST (message_list_new (fb));
    fb->mail_display = MAIL_DISPLAY (mail_display_new (fb));

    folder_browser_properties_init (fb);
    folder_browser_gui_init (fb);
}

GtkWidget *
folder_browser_new (void)
{
    FolderBrowser *folder_browser = gtk_type_new (folder_browser_get_type ());

    my_folder_browser_init (GTK_OBJECT (folder_browser));
    folder_browser->uri = NULL;

    return GTK_WIDGET (folder_browser);
}


E_MAKE_TYPE (folder_browser, "FolderBrowser", FolderBrowser, folder_browser_class_init, folder_browser_init, PARENT_TYPE);