aboutsummaryrefslogtreecommitdiffstats
path: root/mail/message-list.c
blob: 09d268ec615fa994349a44d3d46a987541aa3e1f (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * message-list.c: Displays the messages.
 *                 Implements CORBA's Evolution::MessageList
 *
 * Author:
 *     Miguel de Icaza (miguel@helixcode.com)
 *     Bertrand Guiheneuf (bg@aful.org)
 *
 * (C) 2000 Helix Code, Inc.
 */
#include <config.h>
#include <gnome.h>
#include <bonobo/bonobo-main.h>
#include "e-util/e-util.h"
#include "camel/camel-exception.h"
#include "camel/camel-folder-summary.h"
#include "message-list.h"
#include "Mail.h"
#include "widgets/e-table/e-table-header-item.h"
#include "widgets/e-table/e-table-item.h"

/*
 * Default sizes for the ETable display
 *
 */
#define N_CHARS(x) (CHAR_WIDTH * (x))

#define COL_ICON_WIDTH        16
#define COL_CHECK_BOX_WIDTH   16
#define COL_FROM_WIDTH        N_CHARS(24)
#define COL_FROM_WIDTH_MIN    32
#define COL_SUBJECT_WIDTH     N_CHARS(30)
#define COL_SUBJECT_WIDTH_MIN 32
#define COL_SENT_WIDTH        N_CHARS(20)
#define COL_SENT_WIDTH_MIN    32
#define COL_RECEIVE_WIDTH     N_CHARS(20)
#define COL_RECEIVE_WIDTH_MIN 32
#define COL_TO_WIDTH          N_CHARS(24)
#define COL_TO_WIDTH_MIN      32
#define COL_SIZE_WIDTH        N_CHARS(6)
#define COL_SIZE_WIDTH_MIN    32

#define PARENT_TYPE (bonobo_object_get_type ())

static BonoboObjectClass *message_list_parent_class;
static POA_Evolution_MessageList__vepv evolution_message_list_vepv;

static void
on_row_selection_cmd (ETable *table, 
              int row, 
              gboolean selected,
              gpointer user_data);



/* select a message and display it */
static void
select_msg (MessageList *message_list, gint row)
{
    CamelException ex;
    CamelMimeMessage *message = NULL;

    camel_exception_init (&ex);

    if (camel_folder_has_uid_capability  (message_list->folder)) {
        const GArray *msg_info_array;
        CamelMessageInfo msg_info;
        
        msg_info_array = camel_folder_summary_get_message_info_list 
            (message_list->folder_summary);
        
        if (msg_info_array->len > 0 ) {
            msg_info = g_array_index (msg_info_array, CamelMessageInfo, row);
            
            message = camel_folder_get_message_by_uid (message_list->folder, 
                                   msg_info.uid,
                                   &ex);
            if (camel_exception_get_id (&ex)) {
                printf ("Unable to get message: %s\n",
                    ex.desc?ex.desc:"unknown_reason");
                return;
            }
        }
        
        printf ("Message = %p\n", message);
        if (message)
            mail_display_set_message (message_list->parent_folder_browser->mail_display,
                          CAMEL_MEDIUM (message));
    }
    
}

/*
 * SimpleTableModel::col_count
 */
static int
ml_col_count (ETableModel *etm, void *data)
{
    return COL_LAST;
}

/*
 * SimpleTableModel::row_count
 */
static int
ml_row_count (ETableModel *etm, void *data)
{
    MessageList *message_list = data;
    CamelException ex;
    int v;
    
    if (!message_list->folder)
        return 0;

    camel_exception_init (&ex);
    
    v = camel_folder_get_message_count (message_list->folder, &ex);
    if (camel_exception_get_id (&ex))
        v = 0;
    
    
    /* in the case where no message is available, return 1
     * however, cause we want to be able to show a text */
    return (v ? v:1);
    
}

static void *
ml_value_at (ETableModel *etm, int col, int row, void *data)
{
    static char buffer [10];
    MessageList *message_list = data;
    CamelFolderSummary *summary;
    const GArray *msg_info_array;
    CamelMessageInfo msg_info;
    CamelException ex;

    camel_exception_init (&ex);
    
    summary = message_list->folder_summary;
    if (!summary)
        goto nothing_to_see;
    
    
    /* retrieve the message information array */
    msg_info_array = camel_folder_summary_get_message_info_list (summary);

    /* 
     * in the case where it is zero message long 
     * display nothing 
     */
    if (msg_info_array->len == 0)
        goto nothing_to_see;

    msg_info = g_array_index (msg_info_array, CamelMessageInfo, row);
    
    
    switch (col){
    case COL_ONLINE_STATUS:
        return GINT_TO_POINTER (0);
        
    case COL_MESSAGE_STATUS:
        return GINT_TO_POINTER (1);
        
    case COL_PRIORITY:
        return GINT_TO_POINTER (1);
        
    case COL_ATTACHMENT:
        return GINT_TO_POINTER (0);
        
    case COL_FROM:
        if (msg_info.sender)
            return msg_info.sender;
        else
            return "";
        
    case COL_SUBJECT:
        if (msg_info.subject)
            return msg_info.subject;
        else
            return "";
        
    case COL_SENT:
        return "sent";
        
    case COL_RECEIVE:
        return "receive";
        
    case COL_TO:
        return "dudes@server";
        
    case COL_SIZE:
        sprintf (buffer, "%d", 20);
        return buffer;
            
    default:
        g_assert_not_reached ();
    }
    return NULL;
    
    
    nothing_to_see :
        /* 
         * in the case there is nothing to look at, 
         * notice the user.
         */ 
        if (col == COL_SUBJECT)
            return "No item in this view";
        else 
            return NULL;      
}

static void
ml_set_value_at (ETableModel *etm, int col, int row, const void *value, void *data)
{
}

static gboolean
ml_is_cell_editable (ETableModel *etm, int col, int row, void *data)
{
    return FALSE;
}

static void *
ml_duplicate_value (ETableModel *etm, int col, const void *value, void *data)
{
    switch (col){
    case COL_ONLINE_STATUS:
    case COL_MESSAGE_STATUS:
    case COL_PRIORITY:
    case COL_ATTACHMENT:
      return value;

    case COL_FROM:
    case COL_SUBJECT:
    case COL_SENT:
    case COL_RECEIVE:
    case COL_TO:
    case COL_SIZE:
      return g_strdup(value);
    default:
        g_assert_not_reached ();
    }
    return NULL;
}

static void
ml_free_value (ETableModel *etm, int col, void *value, void *data)
{
    switch (col){
    case COL_ONLINE_STATUS:
    case COL_MESSAGE_STATUS:
    case COL_PRIORITY:
    case COL_ATTACHMENT:
      break;

    case COL_FROM:
    case COL_SUBJECT:
    case COL_SENT:
    case COL_RECEIVE:
    case COL_TO:
    case COL_SIZE:
      g_free(value);
      break;
    default:
        g_assert_not_reached ();
    }
}

static void
ml_thaw (ETableModel *etm, void *data)
{
    e_table_model_changed(etm);
}

static void
message_list_init_renderers (MessageList *message_list)
{
    gchar *attachment_path;
    GdkPixbuf *image; 

    g_assert (message_list);
    g_assert (message_list->table_model);
    
    message_list->render_text = e_cell_text_new (
        message_list->table_model,
        NULL, GTK_JUSTIFY_LEFT, FALSE);

    message_list->render_online_status = e_cell_checkbox_new ();
    message_list->render_message_status = e_cell_checkbox_new ();
#if 1
    message_list->render_attachment = e_cell_checkbox_new (); 
#else
    /*
     * if we want to use a pixmap, use this code
     */
    attachment_path = gnome_unconditional_datadir_file ("evolution/e-attchmt.png");
    image = gdk_pixbuf_new_from_file (attachment_path);
    message_list->render_attachment = e_cell_toggle_new (0, 1, &image);
    g_free (attachment_path);
#endif

    /*
     * FIXME: We need a real renderer here
     */
    message_list->render_priority = e_cell_checkbox_new ();
}

#define CHAR_WIDTH 10
static void
message_list_init_header (MessageList *message_list)
{
    int i;
    
    /*
     * FIXME:
     *
     * Use the font metric to compute this.
     */
    
    message_list->header_model = e_table_header_new ();
    gtk_object_ref(GTK_OBJECT(message_list->header_model));
    gtk_object_sink(GTK_OBJECT(message_list->header_model));

    message_list->table_cols [COL_ONLINE_STATUS] =
        e_table_col_new (COL_ONLINE_STATUS, _("Online status"),
                 COL_CHECK_BOX_WIDTH, COL_CHECK_BOX_WIDTH,
                 message_list->render_online_status,
                 g_int_compare, FALSE);

    message_list->table_cols [COL_MESSAGE_STATUS] =
        e_table_col_new (COL_MESSAGE_STATUS, _("Message status"),
                 COL_CHECK_BOX_WIDTH, COL_CHECK_BOX_WIDTH,
                 message_list->render_message_status,
                 g_int_compare, FALSE);

    message_list->table_cols [COL_PRIORITY] =
        e_table_col_new (COL_PRIORITY, _("Priority"),
                 COL_CHECK_BOX_WIDTH, COL_CHECK_BOX_WIDTH,
                 message_list->render_priority,
                 g_int_compare, FALSE);
    
    message_list->table_cols [COL_ATTACHMENT] =
        e_table_col_new (COL_ATTACHMENT, _("Attachment"),
                 COL_ICON_WIDTH, COL_ICON_WIDTH,
                 message_list->render_attachment,
                 g_int_compare, FALSE);

    message_list->table_cols [COL_FROM] =
        e_table_col_new (COL_FROM, _("From"),
                 COL_FROM_WIDTH, COL_FROM_WIDTH_MIN,
                 message_list->render_text,
                 g_str_compare, TRUE);

    message_list->table_cols [COL_SUBJECT] =
        e_table_col_new (COL_SUBJECT, _("Subject"),
                 COL_SUBJECT_WIDTH, COL_SUBJECT_WIDTH_MIN,
                 message_list->render_text,
                 g_str_compare, TRUE);

    message_list->table_cols [COL_SENT] =
        e_table_col_new (COL_SENT, _("Sent"),
                 COL_SENT_WIDTH, COL_SENT_WIDTH_MIN,
                 message_list->render_text,
                 g_str_compare, TRUE);

    message_list->table_cols [COL_RECEIVE] =
        e_table_col_new (COL_RECEIVE, _("Receive"),
                 COL_RECEIVE_WIDTH, COL_RECEIVE_WIDTH_MIN,
                 message_list->render_text,
                 g_str_compare, TRUE);
    message_list->table_cols [COL_TO] =
        e_table_col_new (COL_TO, _("To"),
                 COL_TO_WIDTH, COL_TO_WIDTH_MIN,
                 message_list->render_text,
                 g_str_compare, TRUE);

    message_list->table_cols [COL_SIZE] =
        e_table_col_new (COL_SIZE, _("Size"),
                 COL_SIZE_WIDTH, COL_SIZE_WIDTH_MIN,
                 message_list->render_text,
                 g_str_compare, TRUE);

    /*
     * Dummy init: It setups the headers to match the order in which
     * they are defined.  In the future e-table widget will take care
     * of this.
     */
    for (i = 0; i < COL_LAST; i++) {
        gtk_object_ref(GTK_OBJECT(message_list->table_cols [i]));
        e_table_header_add_column (message_list->header_model,
                       message_list->table_cols [i], i);
    }
}





/*
 * GtkObject::init
 */
static void
message_list_init (GtkObject *object)
{
    MessageList *message_list = MESSAGE_LIST (object);
    
    message_list->table_model = e_table_simple_new (
        ml_col_count, ml_row_count, ml_value_at,
        ml_set_value_at, ml_is_cell_editable, ml_duplicate_value, ml_free_value,
        ml_thaw, message_list);

    message_list_init_renderers (message_list);
    message_list_init_header (message_list);
    printf ("headers intialized\n");

    /*
     * The etable
     */
    
    message_list->etable = e_table_new (message_list->header_model, message_list->table_model, "<ETableSpecification> <columns-shown> <column> 0 </column> <column> 1 </column> <column> 2 </column> <column> 3 </column> <column> 4 </column> <column> 5 </column> <column> 6 </column> <column> 7 </column> <column> 8 </column> <column> 9 </column> </columns-shown> <grouping> <group column=\"4\" ascending=\"1\"> <leaf column=\"5\" ascending=\"1\"/> </group> </grouping> </ETableSpecification>"); 
    
    gtk_signal_connect(GTK_OBJECT(message_list->etable), "row_selection",
               GTK_SIGNAL_FUNC(on_row_selection_cmd), message_list);

    gtk_widget_show(message_list->etable);
    
    gtk_object_ref(message_list->table_model);
    gtk_object_sink(message_list->table_model);
    
    /*
     * We do own the Etable, not some widget container
     */
    gtk_object_ref (GTK_OBJECT (message_list->etable));
    gtk_object_sink (GTK_OBJECT (message_list->etable));
}

static void
message_list_destroy (GtkObject *object)
{
    MessageList *message_list = MESSAGE_LIST (object);
    int i;

    
    gtk_object_unref (GTK_OBJECT (message_list->table_model));
    gtk_object_unref (GTK_OBJECT (message_list->header_model));
    
    /*
     * Renderers
     */
    gtk_object_unref (GTK_OBJECT (message_list->render_text));
    gtk_object_unref (GTK_OBJECT (message_list->render_online_status));
    gtk_object_unref (GTK_OBJECT (message_list->render_message_status));
    gtk_object_unref (GTK_OBJECT (message_list->render_priority));
    gtk_object_unref (GTK_OBJECT (message_list->render_attachment));
    
    gtk_object_unref (GTK_OBJECT (message_list->etable));

    for (i = 0; i < COL_LAST; i++)
        gtk_object_unref (GTK_OBJECT (message_list->table_cols [i]));
    
    GTK_OBJECT_CLASS (message_list_parent_class)->destroy (object);
}

/*
 * CORBA method: Evolution::MessageList::select_message
 */
static void
MessageList_select_message (PortableServer_Servant _servant,
                const CORBA_long message_number,
                CORBA_Environment *ev)
{
    printf ("FIXME: select message method\n");
}

/*
 * CORBA method: Evolution::MessageList::open_message
 */
static void
MessageList_open_message (PortableServer_Servant _servant,
              const CORBA_long message_number,
              CORBA_Environment *ev)
{
    printf ("FIXME: open message method\n");
}

static POA_Evolution_MessageList__epv *
evolution_message_list_get_epv (void)
{
    POA_Evolution_MessageList__epv *epv;

    epv = g_new0 (POA_Evolution_MessageList__epv, 1);

    epv->select_message = MessageList_select_message;
    epv->open_message   = MessageList_open_message;

    return epv;
}

static void
message_list_corba_class_init (void)
{
    evolution_message_list_vepv.Bonobo_Unknown_epv = bonobo_object_get_epv ();
    evolution_message_list_vepv.Evolution_MessageList_epv = evolution_message_list_get_epv ();
}

/*
 * GtkObjectClass::init
 */
static void
message_list_class_init (GtkObjectClass *object_class)
{
    message_list_parent_class = gtk_type_class (PARENT_TYPE);

    object_class->destroy = message_list_destroy;

    message_list_corba_class_init ();
}

static void
message_list_construct (MessageList *message_list, Evolution_MessageList corba_message_list)
{
    bonobo_object_construct (BONOBO_OBJECT (message_list), corba_message_list);
}

static Evolution_MessageList
create_corba_message_list (BonoboObject *object)
{
    POA_Evolution_MessageList *servant;
    CORBA_Environment ev;

    servant = (POA_Evolution_MessageList *) g_new0 (BonoboObjectServant, 1);
    servant->vepv = &evolution_message_list_vepv;

    CORBA_exception_init (&ev);
    POA_Evolution_MessageList__init ((PortableServer_Servant) servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION){
        g_free (servant);
        CORBA_exception_free (&ev);
        return CORBA_OBJECT_NIL;
    }

    CORBA_exception_free (&ev);
    return (Evolution_MessageList) bonobo_object_activate_servant (object, servant);
}

BonoboObject *
message_list_new (FolderBrowser *parent_folder_browser)
{
    Evolution_MessageList corba_object;
    MessageList *message_list;

    g_assert (parent_folder_browser);

    message_list = gtk_type_new (message_list_get_type ());

    corba_object = create_corba_message_list (BONOBO_OBJECT (message_list));
    if (corba_object == CORBA_OBJECT_NIL){
        gtk_object_destroy (GTK_OBJECT (message_list));
        return NULL;
    }

    message_list->parent_folder_browser = parent_folder_browser;

    message_list_construct (message_list, corba_object);

    return BONOBO_OBJECT (message_list);
}

void
message_list_set_folder (MessageList *message_list, CamelFolder *camel_folder)
{
    CamelException ex;
    gboolean folder_exists;

    g_return_if_fail (message_list != NULL);
    g_return_if_fail (camel_folder != NULL);
    g_return_if_fail (IS_MESSAGE_LIST (message_list));
    g_return_if_fail (CAMEL_IS_FOLDER (camel_folder));
    g_return_if_fail (camel_folder_has_summary_capability (camel_folder));
    
    
    camel_exception_init (&ex);
    
    if (message_list->folder)
        gtk_object_unref (GTK_OBJECT (message_list->folder));

    message_list->folder = camel_folder;
    
    folder_exists = camel_folder_exists (camel_folder, NULL);
    
    if (camel_exception_get_id (&ex)) {
          printf ("Unable to test for folder existence: %s\n",
              ex.desc?ex.desc:"unknown reason");
          return;
    }
    
    if (!folder_exists) {     
        g_warning ("Folder does not exist, creating it\n");
        /* 
           if you don't want the directory to be created
           automatically here remove this.
        */
        camel_folder_create (camel_folder, &ex);
        if (camel_exception_get_id (&ex)) {
          printf ("Unable to create folder: %s\n",
              ex.desc?ex.desc:"unknown_reason");
          return;
        }
       
    }

    
    camel_folder_open (camel_folder, FOLDER_OPEN_RW, &ex);
    if (camel_exception_get_id (&ex)) {
        printf ("Unable to open folder: %s\n",
            ex.desc?ex.desc:"unknown_reason");    
        return;
    }
    
    message_list->folder_summary =
        camel_folder_get_summary (camel_folder, &ex);
    
    
    if (camel_exception_get_id (&ex)) {
        printf ("Unable to get summary: %s\n",
            ex.desc?ex.desc:"unknown_reason");        
        return;
    }
    

    
    gtk_object_ref (GTK_OBJECT (camel_folder));
    
    printf ("Modelo cambio!\n");
    e_table_model_changed (message_list->table_model);

    select_msg (message_list, 0);
}

GtkWidget *
message_list_get_widget (MessageList *message_list)
{
    return message_list->etable;
}

E_MAKE_TYPE (message_list, "MessageList", MessageList, message_list_class_init, message_list_init, PARENT_TYPE);




static void
on_row_selection_cmd (ETable *table, 
              int row, 
              gboolean selected,
              gpointer user_data)
{
    MessageList *message_list;

    message_list = MESSAGE_LIST (user_data);

    if ( selected ) {
        g_print ("Row %d selected\n", row);
        select_msg (message_list, row);
    } else {
        g_print ("Row %d unselected\n", row);
    }
}