~ubuntu-branches/debian/squeeze/geany-plugins/squeeze

« back to all changes in this revision

Viewing changes to geanysendmail/src/geanysendmail.c

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-07-10 22:56:41 UTC
  • Revision ID: james.westby@ubuntu.com-20090710225641-xc1126t7pq0jmpos
Tags: upstream-0.17.1
ImportĀ upstreamĀ versionĀ 0.17.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      geanysendmail.c
 
3
 *
 
4
 *      Copyright 2007-2009 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
 
5
 *      Copyright 2007 Enrico Trƶger <enrico(dot)troeger(at)uvena(dot)de>
 
6
 *      Copyright 2007, 2008 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
 
7
 *      Copyright 2008, 2009 Timothy Boronczyk <tboronczyk(at)gmail(dot)com>
 
8
 *
 
9
 *      This program is free software; you can redistribute it and/or modify
 
10
 *      it under the terms of the GNU General Public License as published by
 
11
 *      the Free Software Foundation; either version 2 of the License, or
 
12
 *      (at your option) any later version.
 
13
 *
 
14
 *      This program is distributed in the hope that it will be useful,
 
15
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *      GNU General Public License for more details.
 
18
 *
 
19
 *      You should have received a copy of the GNU General Public License
 
20
 *      along with this program; if not, write to the Free Software
 
21
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
 */
 
23
 
 
24
/* A little plugin to send a document as attachment using the preferred mail client */
 
25
 
 
26
#include "geany.h"
 
27
#include "ui_utils.h"
 
28
#include "support.h"
 
29
#include "plugindata.h"
 
30
#include "document.h"
 
31
#include "filetypes.h"
 
32
#include "utils.h"
 
33
#include "keybindings.h"
 
34
#include "icon.h"
 
35
#include "geanyfunctions.h"
 
36
 
 
37
#ifdef HAVE_LOCALE_H
 
38
# include <locale.h>
 
39
#endif
 
40
 
 
41
GeanyPlugin             *geany_plugin;
 
42
GeanyData               *geany_data;
 
43
GeanyFunctions  *geany_functions;
 
44
 
 
45
PLUGIN_VERSION_CHECK(116)
 
46
 
 
47
PLUGIN_SET_INFO(_("GeanySendMail"), _("A little plugin to send the current \
 
48
file as attachment by user's favorite mailer"), "0.4.2", "Frank Lanitz <frank@frank.uvena.de>")
 
49
 
 
50
/* Keybinding(s) */
 
51
enum
 
52
{
 
53
        SENDMAIL_KB,
 
54
        COUNT_KB
 
55
};
 
56
 
 
57
PLUGIN_KEY_GROUP(sendmail, COUNT_KB)
 
58
 
 
59
static gchar *config_file = NULL;
 
60
static gchar *mailer = NULL;
 
61
static gchar *address = NULL;
 
62
gboolean icon_in_toolbar = FALSE;
 
63
gboolean use_address_dialog = FALSE;
 
64
/* Needed global to remove from toolbar again */
 
65
GtkWidget *mailbutton = NULL;
 
66
static GtkWidget *main_menu_item = NULL;
 
67
 
 
68
/* Callback for sending file as attachment */
 
69
static void
 
70
send_as_attachment(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata)
 
71
{
 
72
        GeanyDocument *doc;
 
73
        gchar   *locale_filename = NULL;
 
74
        gchar   *command = NULL;
 
75
        GError  *error = NULL;
 
76
        GString *cmd_str = NULL;
 
77
        GtkWidget       *dialog = NULL;
 
78
        GtkWidget       *label = NULL;
 
79
        GtkWidget       *entry = NULL;
 
80
        GtkWidget       *vbox = NULL;
 
81
        GKeyFile        *config = g_key_file_new();
 
82
        gchar           *config_dir = g_path_get_dirname(config_file);
 
83
        gchar           *data;
 
84
 
 
85
        doc = document_get_current();
 
86
 
 
87
        if (doc->file_name == NULL)
 
88
        {
 
89
                dialogs_show_save_as();
 
90
        }
 
91
        else
 
92
        {
 
93
                document_save_file(doc, FALSE);
 
94
        }
 
95
 
 
96
    if (doc->file_name != NULL)
 
97
        {
 
98
                if (mailer)
 
99
                {
 
100
                        locale_filename = utils_get_locale_from_utf8(doc->file_name);
 
101
                        cmd_str = g_string_new(mailer);
 
102
 
 
103
                        if ((use_address_dialog == TRUE) && (g_strrstr(mailer, "%r") != NULL))
 
104
                        {
 
105
                                gint tmp;
 
106
 
 
107
                                dialog = gtk_dialog_new_with_buttons(_("Recipient's Address"),
 
108
                                                                                GTK_WINDOW(geany->main_widgets->window),
 
109
                                                                                GTK_DIALOG_DESTROY_WITH_PARENT,
 
110
                                                                                GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
111
                                                                                GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
 
112
                                                                                NULL);
 
113
                                gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
 
114
                                vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
 
115
                                gtk_widget_set_name(dialog, "GeanyDialog");
 
116
                                gtk_box_set_spacing(GTK_BOX(vbox), 10);
 
117
 
 
118
                                label = gtk_label_new(_("Enter the recipient's e-mail address:"));
 
119
                                gtk_widget_show(label);
 
120
                                gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
 
121
                                entry = gtk_entry_new();
 
122
                                gtk_widget_show(entry);
 
123
                                if (address != NULL)
 
124
                                        gtk_entry_set_text(GTK_ENTRY(entry), address);
 
125
 
 
126
                                gtk_container_add(GTK_CONTAINER(vbox), label);
 
127
                                gtk_container_add(GTK_CONTAINER(vbox), entry);
 
128
                                gtk_widget_show(vbox);
 
129
 
 
130
                                tmp = gtk_dialog_run(GTK_DIALOG(dialog));
 
131
 
 
132
                                if (tmp != GTK_RESPONSE_ACCEPT)
 
133
                                {
 
134
                                        gtk_widget_destroy(dialog);
 
135
                                        return;
 
136
                                }
 
137
                                else
 
138
                                {
 
139
                                        g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
 
140
 
 
141
                                        g_free(address);
 
142
                                        address = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
 
143
 
 
144
                                        g_key_file_set_string(config, "tools", "address", address);
 
145
                                }
 
146
 
 
147
                                if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) &&
 
148
                                      utils_mkdir(config_dir, TRUE) != 0)
 
149
                                {
 
150
                                        dialogs_show_msgbox(GTK_MESSAGE_ERROR,
 
151
                                                _("Plugin configuration directory could not be created."));
 
152
                                }
 
153
                                else
 
154
                                {
 
155
                                        /* write config to file */
 
156
                                        data = g_key_file_to_data(config, NULL, NULL);
 
157
                                        utils_write_file(config_file, data);
 
158
                                        g_free(data);
 
159
                                        g_key_file_free(config);
 
160
                                        g_free(config_dir);
 
161
                                }
 
162
                        }
 
163
 
 
164
                        if (! utils_string_replace_all(cmd_str, "%f", locale_filename))
 
165
                                ui_set_statusbar(FALSE,
 
166
                                _("Filename placeholder not found. The executed command might have failed."));
 
167
 
 
168
                        if (use_address_dialog == TRUE && address != NULL)
 
169
                        {
 
170
                                if (! utils_string_replace_all(cmd_str, "%r", address))
 
171
                                        ui_set_statusbar(FALSE,
 
172
                                        _("Recipient address placeholder not found. The executed command might have failed."));
 
173
                        }
 
174
                        else
 
175
                                /* Removes %r if option was not activ but was included into command */
 
176
                                utils_string_replace_all(cmd_str, "%r", NULL);
 
177
 
 
178
                        utils_string_replace_all(cmd_str, "%b", g_path_get_basename(locale_filename));
 
179
 
 
180
                        command = g_string_free(cmd_str, FALSE);
 
181
                        g_spawn_command_line_async(command, &error);
 
182
                        if (error != NULL)
 
183
                        {
 
184
                                ui_set_statusbar(FALSE, _("Could not execute mailer. Please check your configuration."));
 
185
                                g_error_free(error);
 
186
                        }
 
187
 
 
188
                        g_free(locale_filename);
 
189
                        g_free(command);
 
190
 
 
191
                        if (dialog != NULL)
 
192
                                gtk_widget_destroy(dialog);
 
193
                }
 
194
                else
 
195
                {
 
196
                        ui_set_statusbar(FALSE, _("Please define a mail client first."));
 
197
                }
 
198
        }
 
199
        else
 
200
        {
 
201
                ui_set_statusbar(FALSE, _("File has to be saved before sending."));
 
202
        }
 
203
}
 
204
 
 
205
static void key_send_as_attachment(G_GNUC_UNUSED guint key_id)
 
206
{
 
207
        send_as_attachment(NULL, NULL);
 
208
}
 
209
 
 
210
#define GEANYSENDMAIL_STOCK_MAIL "geanysendmail-mail"
 
211
 
 
212
static void add_stock_item(void)
 
213
{
 
214
        GtkIconSet *icon_set;
 
215
        GtkIconFactory *factory = gtk_icon_factory_new();
 
216
        GtkIconTheme *theme = gtk_icon_theme_get_default();
 
217
        GtkStockItem item = { GEANYSENDMAIL_STOCK_MAIL, _("Mail"), 0, 0, GETTEXT_PACKAGE };
 
218
 
 
219
        if (gtk_icon_theme_has_icon(theme, "mail-message-new"))
 
220
        {
 
221
                GtkIconSource *icon_source = gtk_icon_source_new();
 
222
                icon_set = gtk_icon_set_new();
 
223
                gtk_icon_source_set_icon_name(icon_source, "mail-message-new");
 
224
                gtk_icon_set_add_source(icon_set, icon_source);
 
225
                gtk_icon_source_free(icon_source);
 
226
        }
 
227
        else
 
228
        {
 
229
                GdkPixbuf *pb = gdk_pixbuf_new_from_inline(-1, mail_pixbuf, FALSE, NULL);
 
230
                icon_set = gtk_icon_set_new_from_pixbuf(pb);
 
231
                g_object_unref(pb);
 
232
        }
 
233
        gtk_icon_factory_add(factory, item.stock_id, icon_set);
 
234
        gtk_stock_add(&item, 1);
 
235
        gtk_icon_factory_add_default(factory);
 
236
 
 
237
        g_object_unref(factory);
 
238
        gtk_icon_set_unref(icon_set);
 
239
}
 
240
 
 
241
 
 
242
void show_icon()
 
243
{
 
244
        mailbutton = GTK_WIDGET(gtk_tool_button_new_from_stock(GEANYSENDMAIL_STOCK_MAIL));
 
245
        plugin_add_toolbar_item(geany_plugin, GTK_TOOL_ITEM(mailbutton));
 
246
        ui_add_document_sensitive(mailbutton);
 
247
#if GTK_CHECK_VERSION(2, 12, 0)
 
248
        gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(mailbutton), _("Send by mail"));
 
249
#endif
 
250
        g_signal_connect (G_OBJECT(mailbutton), "clicked", G_CALLBACK(send_as_attachment), NULL);
 
251
        gtk_widget_show_all (mailbutton);
 
252
}
 
253
 
 
254
void cleanup_icon()
 
255
{
 
256
        if (mailbutton != NULL)
 
257
        {
 
258
                gtk_container_remove(GTK_CONTAINER (geany->main_widgets->toolbar), mailbutton);
 
259
        }
 
260
}
 
261
 
 
262
 
 
263
static struct
 
264
{
 
265
        GtkWidget *entry;
 
266
        GtkWidget *checkbox_icon_to_toolbar;
 
267
        GtkWidget *checkbox_use_addressdialog;
 
268
}
 
269
pref_widgets;
 
270
 
 
271
static void
 
272
on_configure_response(G_GNUC_UNUSED GtkDialog *dialog, gint response, G_GNUC_UNUSED  gpointer user_data)
 
273
{
 
274
        if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
 
275
        {
 
276
                GKeyFile        *config = g_key_file_new();
 
277
                gchar           *config_dir = g_path_get_dirname(config_file);
 
278
 
 
279
                g_free(mailer);
 
280
                mailer = g_strdup(gtk_entry_get_text(GTK_ENTRY(pref_widgets.entry)));
 
281
 
 
282
                if (icon_in_toolbar == FALSE &&
 
283
                    gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar)) == TRUE)
 
284
                {
 
285
                        icon_in_toolbar = TRUE;
 
286
                        show_icon();
 
287
                }
 
288
                else if (icon_in_toolbar == TRUE &&
 
289
                    gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar)) == FALSE)
 
290
                {
 
291
                        cleanup_icon();
 
292
                        icon_in_toolbar = FALSE;
 
293
                }
 
294
                else
 
295
                {
 
296
                        icon_in_toolbar = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar));
 
297
                }
 
298
 
 
299
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_use_addressdialog)) == TRUE)
 
300
                        use_address_dialog = TRUE;
 
301
                else
 
302
                        use_address_dialog = FALSE;
 
303
 
 
304
                g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
 
305
                g_key_file_set_string(config, "tools", "mailer", mailer);
 
306
                g_key_file_set_boolean(config, "tools", "address_usage", use_address_dialog);
 
307
                g_key_file_set_boolean(config, "icon", "show_icon", icon_in_toolbar);
 
308
 
 
309
                if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
 
310
                {
 
311
                        dialogs_show_msgbox(GTK_MESSAGE_ERROR,
 
312
                                _("Plugin configuration directory could not be created."));
 
313
                }
 
314
                else
 
315
                {
 
316
                        /* write config to file */
 
317
                        gchar *data = g_key_file_to_data(config, NULL, NULL);
 
318
                        utils_write_file(config_file, data);
 
319
                        g_free(data);
 
320
                }
 
321
                g_key_file_free(config);
 
322
                g_free(config_dir);
 
323
        }
 
324
}
 
325
 
 
326
GtkWidget *plugin_configure(GtkDialog *dialog)
 
327
{
 
328
        GtkWidget       *label1, *label2, *vbox;
 
329
 
 
330
        vbox = gtk_vbox_new(FALSE, 6);
 
331
 
 
332
        /* add a label and a text entry to the dialog */
 
333
        label1 = gtk_label_new(_("Path and options for the mail client:"));
 
334
        gtk_widget_show(label1);
 
335
        gtk_misc_set_alignment(GTK_MISC(label1), 0, 0.5);
 
336
        pref_widgets.entry = gtk_entry_new();
 
337
        gtk_widget_show(pref_widgets.entry);
 
338
        if (mailer != NULL)
 
339
                gtk_entry_set_text(GTK_ENTRY(pref_widgets.entry), mailer);
 
340
 
 
341
        label2 = gtk_label_new(_("Note: \n\t\%f will be replaced by your file."\
 
342
                "\n\t\%r will be replaced by recipient's email address."\
 
343
                "\n\t\%b will be replaced by basename of a file"\
 
344
                "\n\tExamples:"\
 
345
                "\n\tsylpheed --attach \"\%f\" --compose \"\%r\""\
 
346
                "\n\tmutt -s \"Sending \'\%b\'\" -a \"\%f\" \"\%r\""));
 
347
        gtk_label_set_selectable(GTK_LABEL(label2), TRUE);
 
348
        gtk_widget_show(label2);
 
349
        gtk_misc_set_alignment(GTK_MISC(label2), 0, 0.5);
 
350
 
 
351
        pref_widgets.checkbox_icon_to_toolbar = gtk_check_button_new_with_label(_("Showing icon in toolbar"));
 
352
        ui_widget_set_tooltip_text(pref_widgets.checkbox_icon_to_toolbar,
 
353
                _("Shows a icon in the toolbar to send file more easy."));
 
354
        gtk_button_set_focus_on_click(GTK_BUTTON(pref_widgets.checkbox_icon_to_toolbar), FALSE);
 
355
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar), icon_in_toolbar);
 
356
        gtk_widget_show(pref_widgets.checkbox_icon_to_toolbar);
 
357
 
 
358
        pref_widgets.checkbox_use_addressdialog = gtk_check_button_new_with_label(_
 
359
                ("Using dialog for entering email address of recipients"));
 
360
 
 
361
        gtk_button_set_focus_on_click(GTK_BUTTON(pref_widgets.checkbox_use_addressdialog), FALSE);
 
362
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_use_addressdialog), use_address_dialog);
 
363
        gtk_widget_show(pref_widgets.checkbox_use_addressdialog);
 
364
 
 
365
        gtk_container_add(GTK_CONTAINER(vbox), label1);
 
366
        gtk_container_add(GTK_CONTAINER(vbox), pref_widgets.entry);
 
367
        gtk_container_add(GTK_CONTAINER(vbox), label2);
 
368
        gtk_box_pack_start(GTK_BOX(vbox), pref_widgets.checkbox_icon_to_toolbar, TRUE, FALSE, 2);
 
369
        gtk_box_pack_start(GTK_BOX(vbox), pref_widgets.checkbox_use_addressdialog, TRUE, FALSE, 2);
 
370
 
 
371
        gtk_widget_show(vbox);
 
372
 
 
373
        g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
 
374
        return vbox;
 
375
}
 
376
 
 
377
/* Called by Geany to initialize the plugin */
 
378
void plugin_init(GeanyData G_GNUC_UNUSED *data)
 
379
{
 
380
        GtkTooltips *tooltips = NULL;
 
381
 
 
382
        GKeyFile *config = g_key_file_new();
 
383
 
 
384
        gchar *kb_label = _("Send file by mail");
 
385
 
 
386
        GtkWidget *menu_mail = NULL;
 
387
 
 
388
        main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
 
389
 
 
390
        config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
 
391
                "geanysendmail", G_DIR_SEPARATOR_S, "mail.conf", NULL);
 
392
 
 
393
        /* Initialising options from config file */
 
394
        g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
 
395
        mailer = g_key_file_get_string(config, "tools", "mailer", NULL);
 
396
        address = g_key_file_get_string(config, "tools", "address", NULL);
 
397
        use_address_dialog = g_key_file_get_boolean(config, "tools", "address_usage", NULL);
 
398
        icon_in_toolbar = g_key_file_get_boolean(config, "icon", "show_icon", NULL);
 
399
 
 
400
        g_key_file_free(config);
 
401
 
 
402
        tooltips = gtk_tooltips_new();
 
403
 
 
404
        add_stock_item();
 
405
        if (icon_in_toolbar == TRUE)
 
406
        {
 
407
                show_icon();
 
408
        }
 
409
 
 
410
        /* Build up menu entry */
 
411
        menu_mail = gtk_menu_item_new_with_mnemonic(_("_Mail document"));
 
412
        gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_mail);
 
413
        ui_widget_set_tooltip_text(menu_mail,
 
414
                _("Sends the opened file as unzipped attachment by any mailer from your $PATH"));
 
415
        g_signal_connect(G_OBJECT(menu_mail), "activate", G_CALLBACK(send_as_attachment), NULL);
 
416
 
 
417
        /* setup keybindings */
 
418
        keybindings_set_item(plugin_key_group, SENDMAIL_KB, key_send_as_attachment,
 
419
                0, 0, "send_file_as_attachment", kb_label, menu_mail);
 
420
 
 
421
        gtk_widget_show_all(menu_mail);
 
422
        ui_add_document_sensitive(menu_mail);
 
423
        main_menu_item = menu_mail;
 
424
}
 
425
 
 
426
 
 
427
void plugin_cleanup()
 
428
{
 
429
        gtk_widget_destroy(main_menu_item);
 
430
        cleanup_icon();
 
431
        g_free(mailer);
 
432
        g_free(address);
 
433
        g_free(config_file);
 
434
}