aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-signature-editor.c
blob: 58c18639c739b3281409693533e554d1dbbb4c92 (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
#include <bonobo.h>
#include <bonobo/bonobo-stream-memory.h>

#include <gal/widgets/e-gui-utils.h>
#include <gal/widgets/e-unicode.h>

#include "e-msg-composer.h"
#include "mail-signature-editor.h"

/*
 * Signature editor
 *
 */

struct _ESignatureEditor {
    GtkWidget *win;
    GtkWidget *control;
    GtkWidget *name_entry;
    
    MailConfigSignature *sig;

    GNOME_GtkHTML_Editor_Engine engine;
};
typedef struct _ESignatureEditor ESignatureEditor;

#define E_SIGNATURE_EDITOR(o) ((ESignatureEditor *) o)

#define DEFAULT_WIDTH 600
#define DEFAULT_HEIGHT 500

enum { REPLY_YES = 0, REPLY_NO, REPLY_CANCEL };

static void
destroy_editor (ESignatureEditor *editor)
{
    gtk_widget_destroy (editor->win);
    g_free (editor);
}

static void
menu_file_save_error (BonoboUIComponent *uic, CORBA_Environment *ev) {
    e_notice (GTK_WINDOW (uic), GNOME_MESSAGE_BOX_ERROR,
          _("Could not save signature file."));
    
    g_warning ("Exception while saving signature (%s)",
           bonobo_exception_get_text (ev));
}

static void
menu_file_save_cb (BonoboUIComponent *uic,
           void *data,
           const char *path)
{
    ESignatureEditor *editor;
    Bonobo_PersistFile pfile_iface;
    CORBA_Environment ev;
    
    editor = E_SIGNATURE_EDITOR (data);
    if (editor->sig->html) {
        CORBA_exception_init (&ev);
        
        pfile_iface = bonobo_object_client_query_interface (bonobo_widget_get_server (BONOBO_WIDGET (editor->control)),
                                    "IDL:Bonobo/PersistFile:1.0", NULL);
        Bonobo_PersistFile_save (pfile_iface, editor->sig->filename, &ev);

        if (ev._major != CORBA_NO_EXCEPTION)
            menu_file_save_error (uic, &ev);

        CORBA_exception_free (&ev);
    } else {
        BonoboStream *stream;
        CORBA_Environment ev;
        Bonobo_PersistStream pstream_iface;
        
        CORBA_exception_init (&ev);
    
        stream = bonobo_stream_open (BONOBO_IO_DRIVER_FS, editor->sig->filename,
                         Bonobo_Storage_WRITE | Bonobo_Storage_CREATE, 0);

        pstream_iface = bonobo_object_client_query_interface
            (bonobo_widget_get_server (BONOBO_WIDGET (editor->control)),
             "IDL:Bonobo/PersistStream:1.0", NULL);

        Bonobo_PersistStream_save (pstream_iface, 
                       (Bonobo_Stream) bonobo_object_corba_objref (BONOBO_OBJECT (stream)),
                       "text/plain", &ev);

        if (ev._major != CORBA_NO_EXCEPTION)
            menu_file_save_error (uic, &ev);
    
        CORBA_exception_free (&ev);
        bonobo_object_unref (BONOBO_OBJECT (stream));
    }
    mail_config_signature_emit_event (MAIL_CONFIG_SIG_EVENT_CONTENT_CHANGED, editor->sig);
}

static void
exit_dialog_cb (int reply, ESignatureEditor *editor)
{
    switch (reply) {
    case REPLY_YES:
        menu_file_save_cb (NULL, editor, NULL);
        destroy_editor (editor);
        break;
    case REPLY_NO:
        destroy_editor (editor);
        break;
    case REPLY_CANCEL:
    default:
    }
}

static void
do_exit (ESignatureEditor *editor)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    if (GNOME_GtkHTML_Editor_Engine_hasUndo (editor->engine, &ev)) {
        GtkWidget *dialog;
        GtkWidget *label;
        gint button;
        
        dialog = gnome_dialog_new (_("Save signature"),
                       GNOME_STOCK_BUTTON_YES,      /* Save */
                       GNOME_STOCK_BUTTON_NO,       /* Don't save */
                       GNOME_STOCK_BUTTON_CANCEL,   /* Cancel */
                       NULL);
        
        label = gtk_label_new (_("This signature has been changed, but hasn't been saved.\n"
                     "\nDo you wish to save your changes?"));
        gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox), label, TRUE, TRUE, 0);
        gtk_widget_show (label);
        gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (editor->win));
        gnome_dialog_set_default (GNOME_DIALOG (dialog), 0);
        button = gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
        
        exit_dialog_cb (button, editor);
    } else
        destroy_editor (editor);
    CORBA_exception_free (&ev);
}

static int
delete_event_cb (GtkWidget *w, GdkEvent *event, ESignatureEditor *editor)
{
    do_exit (editor);
    
    return FALSE;
}

static void
menu_file_close_cb (BonoboUIComponent *uic, gpointer data, const gchar *path)
{
    ESignatureEditor *editor;
    
    editor = E_SIGNATURE_EDITOR (data);
    do_exit (editor);
}

static void
menu_file_save_close_cb (BonoboUIComponent *uic, gpointer data, const gchar *path)
{
    ESignatureEditor *editor;
    
    editor = E_SIGNATURE_EDITOR (data);

    menu_file_save_cb (uic, editor, path);
    destroy_editor (editor);
}

static BonoboUIVerb verbs [] = {

    BONOBO_UI_VERB ("FileSave",       menu_file_save_cb),
    BONOBO_UI_VERB ("FileClose",      menu_file_close_cb),
    BONOBO_UI_VERB ("FileSaveClose",  menu_file_save_close_cb),

    BONOBO_UI_VERB_END
};

static void
load_signature (ESignatureEditor *editor)
{
    CORBA_Environment ev;
    
    if (editor->sig->html) {
        Bonobo_PersistFile pfile_iface;
        
        pfile_iface = bonobo_object_client_query_interface (bonobo_widget_get_server (BONOBO_WIDGET (editor->control)),
                                    "IDL:Bonobo/PersistFile:1.0", NULL);
        CORBA_exception_init (&ev);
        Bonobo_PersistFile_load (pfile_iface, editor->sig->filename, &ev);
        CORBA_exception_free (&ev);
    } else {
        Bonobo_PersistStream pstream_iface;
        BonoboStream *stream;
        gchar *data, *html;
        
        data = e_msg_composer_get_sig_file_content (editor->sig->filename, FALSE);
        html = g_strdup_printf ("<PRE>\n%s", data);
        g_free (data);
        
        pstream_iface = bonobo_object_client_query_interface
            (bonobo_widget_get_server (BONOBO_WIDGET (editor->control)),
             "IDL:Bonobo/PersistStream:1.0", NULL);
        CORBA_exception_init (&ev);
        stream = bonobo_stream_mem_create (html, strlen (html), TRUE, FALSE);
        
        if (stream == NULL) {
            g_warning ("Couldn't create memory stream\n");
        } else {
            BonoboObject *stream_object;
            Bonobo_Stream corba_stream;
            
            stream_object = BONOBO_OBJECT (stream);
            corba_stream = bonobo_object_corba_objref (stream_object);
            Bonobo_PersistStream_load (pstream_iface, corba_stream,
                           "text/html", &ev);
        }
        
        Bonobo_Unknown_unref (pstream_iface, &ev);
        CORBA_Object_release (pstream_iface, &ev);
        CORBA_exception_free (&ev);
        bonobo_object_unref (BONOBO_OBJECT (stream));
        
        g_free (html);
    }
}

static void
sig_name_changed (GtkWidget *w, ESignatureEditor *editor)
{
    mail_config_signature_set_name (editor->sig, e_utf8_gtk_entry_get_text (GTK_ENTRY (editor->name_entry)));
}

void
mail_signature_editor (MailConfigSignature *sig)
{
    CORBA_Environment ev;
    ESignatureEditor *editor;
    BonoboUIComponent *component;
    BonoboUIContainer *container;
    GtkWidget *vbox, *hbox, *label;
    gchar *title;
    
    if (!sig->filename || !*sig->filename)
        return;
    
    editor = g_new0 (ESignatureEditor, 1);
    
    editor->sig = sig;

    title       = g_strdup_printf ("Edit signature (%s)", sig->filename);
    editor->win = bonobo_window_new ("e-sig-editor", title);
    gtk_window_set_default_size (GTK_WINDOW (editor->win), DEFAULT_WIDTH, DEFAULT_HEIGHT);
    gtk_window_set_policy (GTK_WINDOW (editor->win), FALSE, TRUE, FALSE);
    gtk_window_set_modal (GTK_WINDOW (editor->win), TRUE);
    g_free (title);
    
    container = bonobo_ui_container_new ();
    bonobo_ui_container_set_win (container, BONOBO_WINDOW (editor->win));
    
    component = bonobo_ui_component_new_default ();
    bonobo_ui_component_set_container (component, bonobo_object_corba_objref (BONOBO_OBJECT (container)));
    bonobo_ui_component_add_verb_list_with_data (component, verbs, editor);
    bonobo_ui_util_set_ui (component, EVOLUTION_DATADIR, "evolution-signature-editor.xml", "evolution-signature-editor");
    
    editor->control = bonobo_widget_new_control ("OAFIID:GNOME_GtkHTML_Editor",
                             bonobo_ui_component_get_container (component));
    
    if (editor->control == NULL) {
        g_warning ("Cannot get 'OAFIID:GNOME_GtkHTML_Editor'.");
        
        destroy_editor (editor);
        return;
    }

    editor->engine = (GNOME_GtkHTML_Editor_Engine) bonobo_object_client_query_interface
        (bonobo_widget_get_server (BONOBO_WIDGET (editor->control)), "IDL:GNOME/GtkHTML/Editor/Engine:1.0", NULL);
    
    load_signature (editor);

    gtk_signal_connect (GTK_OBJECT (editor->win), "delete_event",
                GTK_SIGNAL_FUNC (delete_event_cb), editor);

    vbox = gtk_vbox_new (FALSE, 0);
    hbox = gtk_hbox_new (FALSE, 0);
    label = gtk_label_new (_("Signature name:"));
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 4);
    editor->name_entry = gtk_entry_new ();
    e_utf8_gtk_entry_set_text (GTK_ENTRY (editor->name_entry), sig->name);
    gtk_signal_connect (GTK_OBJECT (editor->name_entry), "changed", GTK_SIGNAL_FUNC (sig_name_changed), editor);
    gtk_box_pack_start_defaults (GTK_BOX (hbox), editor->name_entry);
    gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 3);
    gtk_widget_show_all (vbox);
    gtk_box_pack_start_defaults (GTK_BOX (vbox), editor->control);

    bonobo_window_set_contents (BONOBO_WINDOW (editor->win), vbox);
    bonobo_widget_set_property (BONOBO_WIDGET (editor->control), "FormatHTML", sig->html, NULL);
    gtk_widget_show (GTK_WIDGET (editor->win));
    gtk_widget_show (GTK_WIDGET (editor->control));

    CORBA_exception_init (&ev);
    GNOME_GtkHTML_Editor_Engine_runCommand (editor->engine, "grab-focus", &ev);
    CORBA_exception_free (&ev);
}