aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-view.c
blob: cd18b4ff69ef57a0067ce8a175b5f7781d1f1f3b (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@helixcode.com>
 *
 *  Copyright 2000 Helix Code, Inc. (www.helixcode.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.
 *
 */

#include <config.h>
#include "mail.h"
#include "mail-ops.h"
#include "camel/camel.h"
#include "mail-display.h"
#include "mail-callbacks.h"

typedef struct mail_view_data_s {
    CamelFolder *source;
    gchar *uid;
    CamelMimeMessage *msg;
    MailDisplay *md;
} mail_view_data;

#define MINIMUM_WIDTH  600
#define MINIMUM_HEIGHT 400

/* Size of the window last time it was changed.  */
static GtkAllocation last_allocation = { 0, 0 };

static void
mail_view_data_free (gpointer mvd)
{
    mail_view_data *data = (mail_view_data *) mvd;

    if (data->uid)
        g_free (data->uid);
    if (data->msg)
        camel_object_unref (CAMEL_OBJECT (data->msg));
    if (data->source)
        camel_object_unref (CAMEL_OBJECT (data->source));

    g_free (data);
}

static mail_view_data *
mail_view_data_new (CamelFolder *source, const gchar *uid, CamelMimeMessage *msg)
{
    mail_view_data *data;

    data = g_new (mail_view_data, 1);
    data->source = source;
    camel_object_ref (CAMEL_OBJECT (data->source));
    data->msg = msg;
    camel_object_ref (CAMEL_OBJECT (data->msg));
    data->uid = g_strdup (uid);

    return data;
}

static void
on_close (GtkWidget *menuitem, gpointer user_data)
{
    GtkWidget *view_window;

    view_window = gtk_object_get_data (GTK_OBJECT (menuitem), "view-window");
    g_return_if_fail (view_window);
    gtk_widget_destroy (GTK_WIDGET (view_window));
}

static void
view_reply_to_sender (GtkWidget *widget, gpointer user_data)
{
    mail_view_data *data = (mail_view_data *) user_data;

    mail_reply (data->source, data->msg, data->uid, FALSE);
}

static void
view_reply_to_all (GtkWidget *widget, gpointer user_data)
{
    mail_view_data *data = (mail_view_data *) user_data;

    mail_reply (data->source, data->msg, data->uid, TRUE);
}

static void
view_forward_msg (GtkWidget *widget, gpointer user_data)
{
    mail_view_data *data = (mail_view_data *) user_data;

    GPtrArray *uids;
    EMsgComposer *composer;

    composer = e_msg_composer_new ();
    if (!composer)
        return;

    uids = g_ptr_array_new();
    g_ptr_array_add (uids, g_strdup (data->uid));

    gtk_signal_connect (GTK_OBJECT (composer), "send",
                GTK_SIGNAL_FUNC (composer_send_cb), NULL);

    mail_do_forward_message (data->msg, data->source, uids, composer, FALSE);
}

static void
view_print_msg (GtkWidget *widget, gpointer user_data)
{
    mail_view_data *data = (mail_view_data *) user_data;

    mail_print_msg (data->md);
}

static void
view_delete_msg (GtkWidget *button, gpointer user_data)
{
    mail_view_data *data = (mail_view_data *) user_data;

    camel_folder_set_message_flags(data->source, data->uid, CAMEL_MESSAGE_DELETED, CAMEL_MESSAGE_DELETED);
}

static void
view_size_allocate_cb (GtkWidget *widget,
               GtkAllocation *allocation)
{
    last_allocation = *allocation;
}

static GnomeUIInfo mail_view_toolbar [] = {
    
    /*GNOMEUIINFO_ITEM_STOCK (N_("Save"), N_("Save this message"),
      save_msg, GNOME_STOCK_PIXMAP_SAVE),*/
    
    GNOMEUIINFO_ITEM_STOCK (N_("Reply"), N_("Reply to the sender of this message"),
                view_reply_to_sender, GNOME_STOCK_PIXMAP_MAIL_RPL),
    
    GNOMEUIINFO_ITEM_STOCK (N_("Reply to All"), N_("Reply to all recipients of this message"),
                view_reply_to_all, GNOME_STOCK_PIXMAP_MAIL_RPL),
    
    GNOMEUIINFO_ITEM_STOCK (N_("Forward"), N_("Forward this message"), view_forward_msg, GNOME_STOCK_PIXMAP_MAIL_FWD),
    
    GNOMEUIINFO_SEPARATOR,
    
    GNOMEUIINFO_ITEM_STOCK (N_("Print"), N_("Print the selected message"), view_print_msg, GNOME_STOCK_PIXMAP_PRINT),
    
    GNOMEUIINFO_ITEM_STOCK (N_("Delete"), N_("Delete this message"), view_delete_msg, GNOME_STOCK_PIXMAP_TRASH),
    
    /*GNOMEUIINFO_SEPARATOR,*/
    
    /*GNOMEUIINFO_ITEM_STOCK (N_("Next"), N_("Next message"), mail_view_next_msg, GNOME_STOCK_PIXMAP_NEXT),
    
      GNOMEUIINFO_ITEM_STOCK (N_("Previous"), N_("Previous message"), mail_view_prev_msg, GNOME_STOCK_PIXMAP_PREVIOUS),*/
    
    GNOMEUIINFO_END
};

static GnomeUIInfo file_menu[] = {
    /*GNOMEUIINFO_MENU_SAVE_ITEM (save, NULL),*/
    /*GNOMEUIINFO_MENU_SAVE_AS_ITEM (save_as, NULL),*/
    /*GNOMEUIINFO_SEPARATOR,*/
    GNOMEUIINFO_MENU_CLOSE_ITEM (on_close, NULL),
    GNOMEUIINFO_END
};

static GnomeUIInfo view_menu[] =
{
    GNOMEUIINFO_END
};

static GnomeUIInfo mail_view_menubar[] =
{
    GNOMEUIINFO_MENU_FILE_TREE (file_menu),
    GNOMEUIINFO_MENU_VIEW_TREE (view_menu),
    GNOMEUIINFO_END
};

static void
set_default_size (GtkWidget *widget)
{
    int width, height;

    width  = MAX (MINIMUM_WIDTH, last_allocation.width);
    height = MAX (MINIMUM_HEIGHT, last_allocation.height);

    gtk_window_set_default_size (GTK_WINDOW (widget), width, height);
}

GtkWidget *
mail_view_create (CamelFolder *source, const char *uid, CamelMimeMessage *msg)
{
    GtkWidget *window;
    GtkWidget *toolbar;
    GtkWidget *mail_display;
    char *subject;
    mail_view_data *data;

    data = mail_view_data_new (source, uid, msg);

    subject = (char *) camel_mime_message_get_subject (msg);
    if (!subject)
        subject = "";
    
    window = gnome_app_new ("Evolution", subject);
    
    toolbar = gtk_toolbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_TOOLBAR_BOTH);
    
    gnome_app_fill_toolbar_with_data (GTK_TOOLBAR (toolbar),
                      mail_view_toolbar,
                      NULL, data);

    gnome_app_set_toolbar (GNOME_APP (window), GTK_TOOLBAR (toolbar));
    gnome_app_create_menus (GNOME_APP (window), mail_view_menubar);

    gtk_object_set_data_full (GTK_OBJECT (window), "mvd", data,
                  mail_view_data_free);

    gtk_widget_ref (mail_view_menubar[0].widget);
    gtk_object_set_data_full (GTK_OBJECT (window), "file",
                  mail_view_menubar[0].widget,
                  (GtkDestroyNotify) gtk_widget_unref);
    
    gtk_widget_ref (file_menu[0].widget);
    gtk_object_set_data (GTK_OBJECT (file_menu[0].widget), "view-window", window);
    gtk_object_set_data_full (GTK_OBJECT (window), "close",
                  file_menu[0].widget,
                  (GtkDestroyNotify) gtk_widget_unref);
    
    gtk_widget_ref (mail_view_menubar[1].widget);
    gtk_object_set_data_full (GTK_OBJECT (window), "view",
                  mail_view_menubar[1].widget,
                  (GtkDestroyNotify) gtk_widget_unref);
    
    mail_display = mail_display_new ();
    mail_display_set_message (MAIL_DISPLAY (mail_display), CAMEL_MEDIUM (msg));
    data->md = MAIL_DISPLAY (mail_display);
    gnome_app_set_contents (GNOME_APP (window), mail_display);
    gtk_widget_grab_focus (GTK_WIDGET (MAIL_DISPLAY (mail_display)->html));

    gtk_signal_connect (GTK_OBJECT (window), "size_allocate",
                GTK_SIGNAL_FUNC (view_size_allocate_cb), NULL);

    set_default_size (window);
    
    return window;
}