aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-popup.c
blob: 4a8d86aeecb3581dc6db85ec294032a7e9b8346b (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Michael Zucchi <notzed@ximian.com>
 *
 *  Copyright 2004 Ximian, Inc. (www.ximian.com)
 *
 *  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 Street #330, Boston, MA 02111-1307, USA.
 *
 */


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

#include <string.h>
#include <stdlib.h>

#include <glib.h>

#include <gtk/gtkmenu.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkimagemenuitem.h>
#include <gtk/gtkcheckmenuitem.h>
#include <gtk/gtkradiomenuitem.h>
#include <gtk/gtkseparatormenuitem.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkimage.h>

#include <libgnome/gnome-url.h>
#include <libgnomevfs/gnome-vfs-mime.h>

#include "em-popup.h"
#include "e-util/e-msgport.h"
#include "em-utils.h"

#include <camel/camel-folder.h>
#include <camel/camel-mime-message.h>
#include <camel/camel-string-utils.h>
#include <camel/camel-mime-utils.h>
#include <camel/camel-mime-part.h>
#include <camel/camel-url.h>

#include <camel/camel-vee-folder.h>
#include <camel/camel-vtrash-folder.h>

#include <gconf/gconf.h>
#include <gconf/gconf-client.h>

#include <gal/util/e-util.h>

static void emp_standard_menu_factory(EMPopup *emp, EMPopupTarget *target, void *data);

struct _EMPopupFactory {
    struct _EMPopupFactory *next, *prev;

    char *menuid;
    EMPopupFactoryFunc factory;
    void *factory_data;
};

struct _menu_node {
    struct _menu_node *next, *prev;

    GSList *menu;
    GDestroyNotify freefunc;
};

struct _EMPopupPrivate {
    EDList menus;
};

static EDList emp_factories = E_DLIST_INITIALISER(emp_factories);

static GObjectClass *emp_parent;

static void
emp_init(GObject *o)
{
    EMPopup *emp = (EMPopup *)o;
    struct _EMPopupPrivate *p;

    p = emp->priv = g_malloc0(sizeof(struct _EMPopupPrivate));

    e_dlist_init(&p->menus);
}

static void
emp_finalise(GObject *o)
{
    EMPopup *emp = (EMPopup *)o;
    struct _EMPopupPrivate *p = emp->priv;
    struct _menu_node *mnode, *nnode;

    g_free(emp->menuid);

    mnode = (struct _menu_node *)p->menus.head;
    nnode = mnode->next;
    while (nnode) {
        if (mnode->freefunc)
            mnode->freefunc(mnode->menu);

        g_free(mnode);
        mnode = nnode;
        nnode = nnode->next;
    }

    g_free(p);

    ((GObjectClass *)emp_parent)->finalize(o);
}

static void
emp_class_init(GObjectClass *klass)
{
    klass->finalize = emp_finalise;
}

GType
em_popup_get_type(void)
{
    static GType type = 0;

    if (type == 0) {
        static const GTypeInfo info = {
            sizeof(EMPopupClass),
            NULL, NULL,
            (GClassInitFunc)emp_class_init,
            NULL, NULL,
            sizeof(EMPopup), 0,
            (GInstanceInitFunc)emp_init
        };
        emp_parent = g_type_class_ref(G_TYPE_OBJECT);
        type = g_type_register_static(G_TYPE_OBJECT, "EMPopup", &info, 0);

        /* FIXME: this should probably sit somewhere in global setup */
        em_popup_static_add_factory(NULL, (EMPopupFactoryFunc)emp_standard_menu_factory, NULL);
    }

    return type;
}

EMPopup *em_popup_new(const char *menuid)
{
    EMPopup *emp = g_object_new(em_popup_get_type(), 0);

    emp->menuid = g_strdup(menuid);

    return emp;
}

/**
 * em_popup_add_items:
 * @emp: 
 * @items: 
 * @freefunc: 
 * 
 * Add new EMPopupItems to the menu's.  Any with the same path
 * will override previously defined menu items, at menu building
 * time.
 **/
void
em_popup_add_items(EMPopup *emp, GSList *items, GDestroyNotify freefunc)
{
    struct _menu_node *node;

    node = g_malloc(sizeof(*node));
    node->menu = items;
    node->freefunc = freefunc;
    e_dlist_addtail(&emp->priv->menus, (EDListNode *)node);
}

/**
 * em_popup_add_static_items:
 * @emp: 
 * @target: Target of this menu.
 * 
 * Will load up any matching menu items from an installed
 * popup factory.  If the menuid of @emp is NULL, then this
 * has no effect.
 *
 **/
void
em_popup_add_static_items(EMPopup *emp, EMPopupTarget *target)
{
    struct _EMPopupFactory *f;

    if (emp->menuid == NULL || target == NULL)
        return;

    /* setup the menu itself */
    f = (struct _EMPopupFactory *)emp_factories.head;
    while (f->next) {
        if (f->menuid == NULL
            || !strcmp(f->menuid, emp->menuid)) {
            f->factory(emp, target, f->factory_data);
        }
        f = f->next;
    }
}

static int
emp_cmp(const void *ap, const void *bp)
{
    struct _EMPopupItem *a = *((void **)ap);
    struct _EMPopupItem *b = *((void **)bp);

    return strcmp(a->path, b->path);
}

/**
 * em_popup_create:
 * @menuitems: 
 * @hide_mask: used to hide menu items, not sure of it's utility,
 * since you could just 'not add them' in the first place.  Saves
 * copying logic anyway.
 * @disable_mask: used to disable menu items.
 * 
 * TEMPORARY code to create a menu from a list of items.
 * 
 * The menu items are merged based on their path element, and
 * built into a menu tree.
 *
 * Return value: 
 **/
GtkMenu *
em_popup_create_menu(EMPopup *emp, guint32 hide_mask, guint32 disable_mask)
{
    struct _EMPopupPrivate *p = emp->priv;
    struct _menu_node *mnode, *nnode;
    GPtrArray *items = g_ptr_array_new();
    GSList *l;
    GString *ppath = g_string_new("");
    GtkMenu *topmenu;
    GHashTable *menu_hash = g_hash_table_new(g_str_hash, g_str_equal),
        *group_hash = g_hash_table_new(g_str_hash, g_str_equal);
    /*char *domain = NULL;*/
    int i;

    /* FIXME: need to override old ones with new names */
    mnode = (struct _menu_node *)p->menus.head;
    nnode = mnode->next;
    while (nnode) {
        for (l=mnode->menu; l; l = l->next)
            g_ptr_array_add(items, l->data);
        mnode = nnode;
        nnode = nnode->next;
    }

    qsort(items->pdata, items->len, sizeof(items->pdata[0]), emp_cmp);

    topmenu = (GtkMenu *)gtk_menu_new();
    for (i=0;i<items->len;i++) {
        GtkWidget *label;
        struct _EMPopupItem *item = items->pdata[i];
        GtkMenu *thismenu;
        GtkMenuItem *menuitem;
        char *tmp;

        /* for bar's, the mask is exclusive or */
        if (item->mask) {
            if ((item->type & EM_POPUP_TYPE_MASK) == EM_POPUP_BAR) {
                if ((item->mask & hide_mask) == item->mask)
                    continue;
            } else if (item->mask & hide_mask)
                continue;
        }

        g_string_truncate(ppath, 0);
        tmp = strrchr(item->path, '/');
        if (tmp) {
            g_string_append_len(ppath, item->path, tmp-item->path);
            thismenu = g_hash_table_lookup(menu_hash, ppath->str);
            g_assert(thismenu != NULL);
        } else {
            thismenu = topmenu;
        }

        switch (item->type & EM_POPUP_TYPE_MASK) {
        case EM_POPUP_ITEM:
            if (item->image) {
                char *path;
                GtkWidget *image;

                path = g_build_filename(EVOLUTION_IMAGES, (char *)item->image, NULL);
                image = gtk_image_new_from_file(path);
                g_free(path);

                gtk_widget_show(image);
                menuitem = (GtkMenuItem *)gtk_image_menu_item_new();
                gtk_image_menu_item_set_image((GtkImageMenuItem *)menuitem, image);
            } else {
                menuitem = (GtkMenuItem *)gtk_menu_item_new();
            }
            break;
        case EM_POPUP_TOGGLE:
            menuitem = (GtkMenuItem *)gtk_check_menu_item_new();
            gtk_check_menu_item_set_active((GtkCheckMenuItem *)menuitem, item->type & EM_POPUP_ACTIVE);
            break;
        case EM_POPUP_RADIO:
            menuitem = (GtkMenuItem *)gtk_radio_menu_item_new(g_hash_table_lookup(group_hash, ppath->str));
            g_hash_table_insert(group_hash, ppath->str, gtk_radio_menu_item_get_group((GtkRadioMenuItem *)menuitem));
            gtk_check_menu_item_set_active((GtkCheckMenuItem *)menuitem, item->type & EM_POPUP_ACTIVE);
            break;
        case EM_POPUP_IMAGE:
            menuitem = (GtkMenuItem *)gtk_image_menu_item_new();
            gtk_image_menu_item_set_image((GtkImageMenuItem *)menuitem, item->image);
            break;
        case EM_POPUP_SUBMENU: {
            GtkMenu *submenu = (GtkMenu *)gtk_menu_new();

            g_hash_table_insert(menu_hash, item->path, submenu);
            menuitem = (GtkMenuItem *)gtk_menu_item_new();
            gtk_menu_item_set_submenu(menuitem, (GtkWidget *)submenu);
            break; }
        case EM_POPUP_BAR:
            /* TODO: double-bar, end-bar stuff? */
            menuitem = (GtkMenuItem *)gtk_separator_menu_item_new();
            break;
        default:
            continue;
        }

        if (item->label) {
            label = gtk_label_new_with_mnemonic(_(item->label));
            gtk_misc_set_alignment((GtkMisc *)label, 0.0, 0.5);
            gtk_widget_show(label);
            gtk_container_add((GtkContainer *)menuitem, label);
        }

        if (item->activate)
            g_signal_connect(menuitem, "activate", item->activate, item->activate_data);

        gtk_menu_shell_append((GtkMenuShell *)thismenu, (GtkWidget *)menuitem);

        if (item->mask & disable_mask)
            gtk_widget_set_sensitive((GtkWidget *)menuitem, FALSE);

        gtk_widget_show((GtkWidget *)menuitem);
    }

    g_string_free(ppath, TRUE);
    g_ptr_array_free(items, TRUE);
    g_hash_table_destroy(menu_hash);
    g_hash_table_destroy(group_hash);

    return topmenu;
}

static void
emp_popup_done(GtkWidget *w, EMPopup *emp)
{
    gtk_widget_destroy(w);
    g_object_unref(emp);
}

/**
 * em_popup_create_menu_once:
 * @emp: EMPopup, once the menu is shown, this cannot be
 * considered a valid pointer.
 * @target: If set, the target of the selection.  Static menu
 * items will be added.  The target will be freed once complete.
 * @hide_mask: 
 * @disable_mask: 
 * 
 * Like popup_create_menu, but automatically sets up the menu
 * so that it is destroyed once a selection takes place, and
 * the EMPopup is unreffed.
 * 
 * Return value: A menu, to popup.
 **/
GtkMenu *
em_popup_create_menu_once(EMPopup *emp, EMPopupTarget *target, guint32 hide_mask, guint32 disable_mask)
{
    GtkMenu *menu;

    if (target)
        em_popup_add_static_items(emp, target);

    menu = em_popup_create_menu(emp, hide_mask, disable_mask);

    if (target)
        g_signal_connect_swapped(menu, "selection_done", G_CALLBACK(em_popup_target_free), target);
    g_signal_connect(menu, "selection_done", G_CALLBACK(emp_popup_done), emp);

    return menu;
}

/* ********************************************************************** */

/**
 * em_popup_static_add_factory:
 * @menuid: 
 * @func: 
 * @data: 
 * 
 * Add a popup factory which will be called to add_items() any
 * extra menu's if wants to do the current PopupTarget.
 *
 * TODO: Make the menuid a pattern?
 * 
 * Return value: A handle to the factory.
 **/
EMPopupFactory *
em_popup_static_add_factory(const char *menuid, EMPopupFactoryFunc func, void *data)
{
    struct _EMPopupFactory *f = g_malloc0(sizeof(*f));

    f->menuid = g_strdup(menuid);
    f->factory = func;
    f->factory_data = data;
    e_dlist_addtail(&emp_factories, (EDListNode *)f);

    return f;
}

/**
 * em_popup_static_remove_factory:
 * @f: 
 * 
 * Remove a popup factory.
 **/
void
em_popup_static_remove_factory(EMPopupFactory *f)
{
    e_dlist_remove((EDListNode *)f);
    g_free(f->menuid);
    g_free(f);
}

/**
 * em_popup_target_new_select:
 * @folder: The selection will ref this for the life of it.
 * @folder_uri: 
 * @uids: The selection will free this when done with it.
 * 
 * Create a new selection popup target.
 * 
 * Return value: 
 **/
EMPopupTarget *
em_popup_target_new_select(struct _CamelFolder *folder, const char *folder_uri, GPtrArray *uids)
{
    EMPopupTarget *t = g_malloc0(sizeof(*t));
    guint32 mask = ~0;
    int i;
    const char *tmp;

    t->type = EM_POPUP_TARGET_SELECT;
    t->data.select.uids = uids;
    t->data.select.folder = folder;
    t->data.select.folder_uri = g_strdup(folder_uri);

    if (folder == NULL) {
        t->mask = mask;

        return t;
    }

    camel_object_ref(folder);
    mask &= ~EM_POPUP_SELECT_FOLDER;

    if (em_utils_folder_is_sent(folder, folder_uri))
        mask &= ~EM_POPUP_SELECT_RESEND;

    if (!(em_utils_folder_is_drafts(folder, folder_uri)
          || em_utils_folder_is_outbox(folder, folder_uri)))
        mask &= ~EM_POPUP_SELECT_ADD_SENDER;

    if (uids->len == 1)
        mask &= ~EM_POPUP_SELECT_ONE;

    if (uids->len >= 1)
        mask &= ~EM_POPUP_SELECT_MANY;

    for (i = 0; i < uids->len; i++) {
        CamelMessageInfo *info = camel_folder_get_message_info(folder, uids->pdata[i]);

        if (info == NULL)
            continue;

        if (info->flags & CAMEL_MESSAGE_SEEN)
            mask &= ~EM_POPUP_SELECT_MARK_UNREAD;
        else
            mask &= ~EM_POPUP_SELECT_MARK_READ;
        
        if (info->flags & CAMEL_MESSAGE_DELETED)
            mask &= ~EM_POPUP_SELECT_UNDELETE;
        else
            mask &= ~EM_POPUP_SELECT_DELETE;

        if (info->flags & CAMEL_MESSAGE_FLAGGED)
            mask &= ~EM_POPUP_SELECT_MARK_UNIMPORTANT;
        else
            mask &= ~EM_POPUP_SELECT_MARK_IMPORTANT;

        if (info->flags & CAMEL_MESSAGE_JUNK)
            mask &= ~EM_POPUP_SELECT_MARK_NOJUNK;
        else
            mask &= ~EM_POPUP_SELECT_MARK_JUNK;
            
        tmp = camel_tag_get (&info->user_tags, "follow-up");
        if (tmp && *tmp) {
            mask &= ~EM_POPUP_SELECT_FLAG_CLEAR;
            tmp = camel_tag_get(&info->user_tags, "completed-on");
            if (tmp == NULL || *tmp == 0)
                mask &= ~EM_POPUP_SELECT_FLAG_COMPLETED;
        } else
            mask &= ~EM_POPUP_SELECT_FLAG_FOLLOWUP;

        if (i == 0 && uids->len == 1
            && (tmp = camel_message_info_mlist(info))
            && tmp[0] != 0)
            mask &= ~EM_POPUP_SELECT_MAILING_LIST;

        camel_folder_free_message_info(folder, info);
    }

    t->mask = mask;

    return t;
}

EMPopupTarget *
em_popup_target_new_uri(const char *uri)
{
    EMPopupTarget *t = g_malloc0(sizeof(*t));
    guint32 mask = ~0;

    t->type = EM_POPUP_TARGET_URI;
    t->data.uri = g_strdup(uri);

    if (g_ascii_strncasecmp(uri, "http:", 5) == 0
        || g_ascii_strncasecmp(uri, "https:", 6) == 0)
        mask &= ~EM_POPUP_URI_HTTP;
    if (g_ascii_strncasecmp(uri, "mailto:", 7) == 0)
        mask &= ~EM_POPUP_URI_MAILTO;
    else
        mask &= ~EM_POPUP_URI_NOT_MAILTO;

    t->mask = mask;

    return t;
}

EMPopupTarget *
em_popup_target_new_part(struct _CamelMimePart *part, const char *mime_type)
{
    EMPopupTarget *t = g_malloc0(sizeof(*t));
    guint32 mask = ~0;

    t->type = EM_POPUP_TARGET_PART;
    t->data.part.part = part;
    camel_object_ref(part);
    if (mime_type)
        t->data.part.mime_type = g_strdup(mime_type);
    else
        t->data.part.mime_type = camel_data_wrapper_get_mime_type((CamelDataWrapper *)part);

    camel_strdown(t->data.part.mime_type);

    if (CAMEL_IS_MIME_MESSAGE(camel_medium_get_content_object((CamelMedium *)part)))
        mask &= ~EM_POPUP_PART_MESSAGE;

    if (strncmp(t->data.part.mime_type, "image/", 6) == 0)
        mask &= ~EM_POPUP_PART_IMAGE;

    t->mask = mask;

    return t;
}

/* TODO: This should be based on the CamelFolderInfo, but ... em-folder-tree doesn't keep it? */
EMPopupTarget *
em_popup_target_new_folder(const char *uri, int isstore)
{
    EMPopupTarget *t = g_malloc0(sizeof(*t));
    guint32 mask = ~0;
    CamelURL *url;

    t->type = EM_POPUP_TARGET_FOLDER;
    t->data.folder.folder_uri = g_strdup(uri);

    if (isstore)
        mask &= ~(EM_POPUP_FOLDER_STORE|EM_POPUP_FOLDER_INFERIORS);
    else
        mask &= ~EM_POPUP_FOLDER_FOLDER;

    url = camel_url_new(uri, NULL);
    if (url == NULL)
        goto done;
    
    if (!isstore) {
        const char *path;

        /* We have no way to find out FOLDER_INFERIORS without
         * the FolderInfo, so turn it on always (except vtrash/junk below) */
        mask &= ~EM_POPUP_FOLDER_INFERIORS;

        /* FIXME: this is a total hack, but i think all we can do at present */
        path = url->fragment?url->fragment:url->path;
        mask &= ~EM_POPUP_FOLDER_DELETE;
        if (path && path[0]
            && (strcmp(path, CAMEL_VTRASH_NAME) == 0
            || strcmp(path, CAMEL_VJUNK_NAME) == 0
            || strcmp(path, CAMEL_UNMATCHED_NAME) == 0
            /* more hack, for maildir root */
            || strcmp(path, ".") == 0))
            mask |= EM_POPUP_FOLDER_DELETE|EM_POPUP_FOLDER_INFERIORS;

        /* since vtrash/vjunk currently make scarily bogus
         * url's, we have to check this too */
        if (strcmp(url->protocol, "vtrash") == 0
            || strcmp(url->protocol, "vjunk") == 0)
            mask |= EM_POPUP_FOLDER_DELETE|EM_POPUP_FOLDER_INFERIORS;
        /* end hack bit */

        if (camel_url_get_param(url, "noselect") == NULL)
            mask &= ~EM_POPUP_FOLDER_SELECT;
    }

    camel_url_free(url);
done:
    t->mask = mask;

    return t;
}

void
em_popup_target_free(EMPopupTarget *t)
{
    switch (t->type) {
    case EM_POPUP_TARGET_SELECT:
        if (t->data.select.folder)
            camel_object_unref(t->data.select.folder);
        g_free(t->data.select.folder_uri);
        if (t->data.select.uids)
            em_utils_uids_free(t->data.select.uids);
        break;
    case EM_POPUP_TARGET_URI:
        g_free(t->data.uri);
        break;
    case EM_POPUP_TARGET_PART:
        camel_object_unref(t->data.part.part);
        g_free(t->data.part.mime_type);
        break;
    case EM_POPUP_TARGET_FOLDER:
        g_free(t->data.folder.folder_uri);
        break;
    }

    g_free(t);
}

/* ********************************************************************** */

#if 0
/* TODO: flesh these out where possible */
static void
emp_popup_open(GtkWidget *w, EMFolderView *emfv)
{
    em_folder_view_open_selected(emfv);
}

static void
emp_popup_resend(GtkWidget *w, EMPopupTarget *t)
{
    if (!em_utils_check_user_can_send_mail(t->widget))
        return;
    
    em_utils_edit_messages(t->widget, t->data.select.folder, em_utils_uids_copy(t->data.select.uids));
}

static void
emp_popup_saveas(GtkWidget *w, EMPopupTarget *t)
{
    em_utils_save_messages(t->widget, t->data.select.folder, em_utils_uids_copy(t->data.select.uids));
}

static EMPopupItem emp_standard_select_popups[] = {
    /*{ EM_POPUP_ITEM, "00.select.00", N_("_Open"), G_CALLBACK(emp_popup_open), NULL, NULL, 0 },*/
    { EM_POPUP_ITEM, "00.select.01", N_("_Edit as New Message..."), G_CALLBACK(emp_popup_resend), NULL, NULL, EM_POPUP_SELECT_RESEND },
    { EM_POPUP_ITEM, "00.select.02", N_("_Save As..."), G_CALLBACK(emp_popup_saveas), NULL, "save-as-16.png", 0 },  
};
#endif

/* ********************************************************************** */

static void
emp_part_popup_saveas(GtkWidget *w, EMPopupTarget *t)
{
    em_utils_save_part(w, _("Save As..."), t->data.part.part);
}

static void
emp_part_popup_set_background(GtkWidget *w, EMPopupTarget *t)
{
    GConfClient *gconf;
    char *str, *filename, *path, *extension;
    unsigned int i=1;
    
    filename = g_strdup(camel_mime_part_get_filename(t->data.part.part));
       
    /* if filename is blank, create a default filename based on MIME type */
    if (!filename || !filename[0]) {
        CamelContentType *ct;

        ct = camel_mime_part_get_content_type(t->data.part.part);
        g_free (filename);
        filename = g_strdup_printf (_("untitled_image.%s"), ct->subtype);
    }

    e_filename_make_safe(filename);
    
    path = g_build_filename(g_get_home_dir(), ".gnome2", "wallpapers", filename, NULL);
    
    extension = strrchr(filename, '.');
    if (extension)
        *extension++ = 0;
    
    /* if file exists, stick a (number) on the end */
    while (g_file_test(path, G_FILE_TEST_EXISTS)) {
        char *name;
        name = g_strdup_printf(extension?"%s (%d).%s":"%s (%d)", filename, i++, extension);
        g_free(path);
        path = g_build_filename(g_get_home_dir(), ".gnome2", "wallpapers", name, NULL);
        g_free(name);
    }
    
    g_free(filename);
    
    if (em_utils_save_part_to_file(w, path, t->data.part.part)) {
        gconf = gconf_client_get_default();
        
        /* if the filename hasn't changed, blank the filename before 
        *  setting it so that gconf detects a change and updates it */
        if ((str = gconf_client_get_string(gconf, "/desktop/gnome/background/picture_filename", NULL)) != NULL 
             && strcmp (str, path) == 0) {
            gconf_client_set_string(gconf, "/desktop/gnome/background/picture_filename", "", NULL);
        }
        
        g_free (str);
        gconf_client_set_string(gconf, "/desktop/gnome/background/picture_filename", path, NULL);
        
        /* if GNOME currently doesn't display a picture, set to "wallpaper"
         * display mode, otherwise leave it alone */
        if ((str = gconf_client_get_string(gconf, "/desktop/gnome/background/picture_options", NULL)) == NULL 
             || strcmp(str, "none") == 0) {
            gconf_client_set_string(gconf, "/desktop/gnome/background/picture_options", "wallpaper", NULL);
        }
        
        gconf_client_suggest_sync(gconf, NULL);
        
        g_free(str);
        g_object_unref(gconf);
    }
    
    g_free(path);
}

static void
emp_part_popup_reply_sender (GtkWidget *w, EMPopupTarget *t)
{
    CamelMimeMessage *message;
    
    message = (CamelMimeMessage *) camel_medium_get_content_object ((CamelMedium *) t->data.part.part);
    em_utils_reply_to_message (message, REPLY_MODE_SENDER);
}

static void
emp_part_popup_reply_list (GtkWidget *w, EMPopupTarget *t)
{
    CamelMimeMessage *message;
    
    message = (CamelMimeMessage *) camel_medium_get_content_object ((CamelMedium *) t->data.part.part);
    em_utils_reply_to_message (message, REPLY_MODE_LIST);
}

static void
emp_part_popup_reply_all (GtkWidget *w, EMPopupTarget *t)
{
    CamelMimeMessage *message;
    
    message = (CamelMimeMessage *) camel_medium_get_content_object ((CamelMedium *) t->data.part.part);
    em_utils_reply_to_message (message, REPLY_MODE_ALL);
}

static void
emp_part_popup_forward (GtkWidget *w, EMPopupTarget *t)
{
    CamelMimeMessage *message;
    
    message = (CamelMimeMessage *) camel_medium_get_content_object ((CamelMedium *) t->data.part.part);
    em_utils_forward_message (message);
}

static EMPopupItem emp_standard_object_popups[] = {
    { EM_POPUP_ITEM, "00.part.00", N_("_Save As..."), G_CALLBACK(emp_part_popup_saveas), NULL, "save-as-16.png", 0 },
    { EM_POPUP_ITEM, "00.part.10", N_("Set as _Background"), G_CALLBACK(emp_part_popup_set_background), NULL, NULL, EM_POPUP_PART_IMAGE },
    { EM_POPUP_BAR, "10.part", NULL, NULL, NULL, NULL, EM_POPUP_PART_MESSAGE },
    { EM_POPUP_ITEM, "10.part.00", N_("_Reply to sender"), G_CALLBACK(emp_part_popup_reply_sender), NULL, "reply.xpm" , EM_POPUP_PART_MESSAGE },
    { EM_POPUP_ITEM, "10.part.01", N_("Reply to _List"), G_CALLBACK(emp_part_popup_reply_list), NULL, NULL, EM_POPUP_PART_MESSAGE},
    { EM_POPUP_ITEM, "10.part.03", N_("Reply to _All"), G_CALLBACK(emp_part_popup_reply_all), NULL, "reply_to_all.xpm", EM_POPUP_PART_MESSAGE},
    { EM_POPUP_BAR, "20.part", NULL, NULL, NULL, NULL, EM_POPUP_PART_MESSAGE },
    { EM_POPUP_ITEM, "20.part.00", N_("_Forward"), G_CALLBACK(emp_part_popup_forward), NULL, "forward.xpm", EM_POPUP_PART_MESSAGE },

};

static const EMPopupItem emp_standard_part_apps_bar = { EM_POPUP_BAR, "99.object" };

/* ********************************************************************** */

static void
emp_uri_popup_link_open(GtkWidget *w, EMPopupTarget *t)
{
    GError *err = NULL;
        
    gnome_url_show(t->data.uri, &err);
    if (err) {
        g_warning("gnome_url_show: %s", err->message);
        g_error_free(err);
    }
}

static void
emp_uri_popup_address_send (GtkWidget *w, EMPopupTarget *t)
{
    em_utils_compose_new_message_with_mailto (t->data.uri);
}

static void
emp_uri_popup_address_add(GtkWidget *w, EMPopupTarget *t)
{
    CamelURL *url;

    url = camel_url_new(t->data.uri, NULL);
    if (url == NULL) {
        g_warning("cannot parse url '%s'", t->data.uri);
        return;
    }

    if (url->path && url->path[0])
        em_utils_add_address(w, url->path);

    camel_url_free(url);
}

static EMPopupItem emp_standard_uri_popups[] = {
    { EM_POPUP_ITEM, "00.uri.00", N_("_Open Link in Browser"), G_CALLBACK(emp_uri_popup_link_open), NULL, NULL, EM_POPUP_URI_NOT_MAILTO },
    { EM_POPUP_ITEM, "00.uri.10", N_("Se_nd message to..."), G_CALLBACK(emp_uri_popup_address_send), NULL, NULL, EM_POPUP_URI_MAILTO },
    { EM_POPUP_ITEM, "00.uri.20", N_("_Add to Addressbook"), G_CALLBACK(emp_uri_popup_address_add), NULL, NULL, EM_POPUP_URI_MAILTO },
};

/* ********************************************************************** */

#define LEN(x) (sizeof(x)/sizeof(x[0]))

#include <libgnomevfs/gnome-vfs-mime-handlers.h>

struct _open_in_item {
    EMPopupItem item;
    EMPopupTarget *target;
    GnomeVFSMimeApplication *app;
};

static void
emp_apps_open_in(GtkWidget *w, struct _open_in_item *item)
{
    char *path;

    path = em_utils_temp_save_part(item->target->widget, item->target->data.part.part);
    if (path) {
        char *command;
        int douri = (item->app->expects_uris == GNOME_VFS_MIME_APPLICATION_ARGUMENT_TYPE_URIS);
                
        command = g_strdup_printf(douri?"%s file://%s &":"%s %s &", item->app->command, path);

        /* FIXME: Do not use system here */
        system(command);
        g_free(command);
        g_free(path);
    }
}

static void
emp_apps_popup_free(GSList *free_list)
{
    while (free_list) {
        GSList *n = free_list->next;
        struct _open_in_item *item = free_list->data;

        g_free(item->item.path);
        g_free(item->item.label);
        g_free(item);
        g_slist_free_1(free_list);

        free_list = n;
    }
}

static void
emp_standard_menu_factory(EMPopup *emp, EMPopupTarget *target, void *data)
{
    int i, len;
    EMPopupItem *items;
    GSList *menus = NULL;

    switch (target->type) {
#if 0
    case EM_POPUP_TARGET_SELECT:
        return;
        items = emp_standard_select_popups;
        len = LEN(emp_standard_select_popups);
        break;
#endif
    case EM_POPUP_TARGET_URI:
        items = emp_standard_uri_popups;
        len = LEN(emp_standard_uri_popups);
        break;
    case EM_POPUP_TARGET_PART: {
        GList *apps = gnome_vfs_mime_get_short_list_applications(target->data.part.mime_type);

        /* FIXME: use the snoop_part stuff from em-format.c */
        if (apps == NULL && strcmp(target->data.part.mime_type, "application/octet-stream") == 0) {
            const char *filename, *name_type;
            
            filename = camel_mime_part_get_filename(target->data.part.part);

            if (filename) {
                /* GNOME-VFS will misidentify TNEF attachments as MPEG */
                if (!strcmp (filename, "winmail.dat"))
                    name_type = "application/vnd.ms-tnef";
                else
                    name_type = gnome_vfs_mime_type_from_name(filename);
                if (name_type)
                    apps = gnome_vfs_mime_get_short_list_applications(name_type);
            }
        }

        if (apps) {
            GString *label = g_string_new("");
            GSList *open_menus = NULL;
            GList *l;

            menus = g_slist_prepend(menus, (void *)&emp_standard_part_apps_bar);

            for (l = apps, i = 0; l; l = l->next, i++) {
                GnomeVFSMimeApplication *app = l->data;
                struct _open_in_item *item;

                if (app->requires_terminal)
                    continue;

                item = g_malloc0(sizeof(*item));
                item->item.type = EM_POPUP_ITEM;
                item->item.path = g_strdup_printf("99.object.%02d", i);
                item->item.label = g_strdup_printf(_("Open in %s..."), app->name);
                item->item.activate = G_CALLBACK(emp_apps_open_in);
                item->item.activate_data = item;
                item->target = target;
                item->app = app;

                open_menus = g_slist_prepend(open_menus, item);
            }

            if (open_menus)
                em_popup_add_items(emp, open_menus, (GDestroyNotify)emp_apps_popup_free);

            g_string_free(label, TRUE);
            g_list_free(apps);
        }

        items = emp_standard_object_popups;
        len = LEN(emp_standard_object_popups);
        break; }
    default:
        items = NULL;
        len = 0;
    }

    for (i=0;i<len;i++) {
        if ((items[i].mask & target->mask) == 0) {
            items[i].activate_data = target;
            menus = g_slist_prepend(menus, &items[i]);
        }
    }

    if (menus)
        em_popup_add_items(emp, menus, (GDestroyNotify)g_slist_free);
}