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>
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.
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.
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.
24
/* A little plugin to send a document as attachment using the preferred mail client */
29
#include "plugindata.h"
31
#include "filetypes.h"
33
#include "keybindings.h"
35
#include "geanyfunctions.h"
41
GeanyPlugin *geany_plugin;
42
GeanyData *geany_data;
43
GeanyFunctions *geany_functions;
45
PLUGIN_VERSION_CHECK(116)
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>")
57
PLUGIN_KEY_GROUP(sendmail, COUNT_KB)
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;
68
/* Callback for sending file as attachment */
70
send_as_attachment(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata)
73
gchar *locale_filename = NULL;
74
gchar *command = 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);
85
doc = document_get_current();
87
if (doc->file_name == NULL)
89
dialogs_show_save_as();
93
document_save_file(doc, FALSE);
96
if (doc->file_name != NULL)
100
locale_filename = utils_get_locale_from_utf8(doc->file_name);
101
cmd_str = g_string_new(mailer);
103
if ((use_address_dialog == TRUE) && (g_strrstr(mailer, "%r") != NULL))
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,
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);
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);
124
gtk_entry_set_text(GTK_ENTRY(entry), address);
126
gtk_container_add(GTK_CONTAINER(vbox), label);
127
gtk_container_add(GTK_CONTAINER(vbox), entry);
128
gtk_widget_show(vbox);
130
tmp = gtk_dialog_run(GTK_DIALOG(dialog));
132
if (tmp != GTK_RESPONSE_ACCEPT)
134
gtk_widget_destroy(dialog);
139
g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
142
address = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
144
g_key_file_set_string(config, "tools", "address", address);
147
if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) &&
148
utils_mkdir(config_dir, TRUE) != 0)
150
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
151
_("Plugin configuration directory could not be created."));
155
/* write config to file */
156
data = g_key_file_to_data(config, NULL, NULL);
157
utils_write_file(config_file, data);
159
g_key_file_free(config);
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."));
168
if (use_address_dialog == TRUE && address != NULL)
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."));
175
/* Removes %r if option was not activ but was included into command */
176
utils_string_replace_all(cmd_str, "%r", NULL);
178
utils_string_replace_all(cmd_str, "%b", g_path_get_basename(locale_filename));
180
command = g_string_free(cmd_str, FALSE);
181
g_spawn_command_line_async(command, &error);
184
ui_set_statusbar(FALSE, _("Could not execute mailer. Please check your configuration."));
188
g_free(locale_filename);
192
gtk_widget_destroy(dialog);
196
ui_set_statusbar(FALSE, _("Please define a mail client first."));
201
ui_set_statusbar(FALSE, _("File has to be saved before sending."));
205
static void key_send_as_attachment(G_GNUC_UNUSED guint key_id)
207
send_as_attachment(NULL, NULL);
210
#define GEANYSENDMAIL_STOCK_MAIL "geanysendmail-mail"
212
static void add_stock_item(void)
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 };
219
if (gtk_icon_theme_has_icon(theme, "mail-message-new"))
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);
229
GdkPixbuf *pb = gdk_pixbuf_new_from_inline(-1, mail_pixbuf, FALSE, NULL);
230
icon_set = gtk_icon_set_new_from_pixbuf(pb);
233
gtk_icon_factory_add(factory, item.stock_id, icon_set);
234
gtk_stock_add(&item, 1);
235
gtk_icon_factory_add_default(factory);
237
g_object_unref(factory);
238
gtk_icon_set_unref(icon_set);
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"));
250
g_signal_connect (G_OBJECT(mailbutton), "clicked", G_CALLBACK(send_as_attachment), NULL);
251
gtk_widget_show_all (mailbutton);
256
if (mailbutton != NULL)
258
gtk_container_remove(GTK_CONTAINER (geany->main_widgets->toolbar), mailbutton);
266
GtkWidget *checkbox_icon_to_toolbar;
267
GtkWidget *checkbox_use_addressdialog;
272
on_configure_response(G_GNUC_UNUSED GtkDialog *dialog, gint response, G_GNUC_UNUSED gpointer user_data)
274
if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_APPLY)
276
GKeyFile *config = g_key_file_new();
277
gchar *config_dir = g_path_get_dirname(config_file);
280
mailer = g_strdup(gtk_entry_get_text(GTK_ENTRY(pref_widgets.entry)));
282
if (icon_in_toolbar == FALSE &&
283
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar)) == TRUE)
285
icon_in_toolbar = TRUE;
288
else if (icon_in_toolbar == TRUE &&
289
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar)) == FALSE)
292
icon_in_toolbar = FALSE;
296
icon_in_toolbar = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_icon_to_toolbar));
299
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(pref_widgets.checkbox_use_addressdialog)) == TRUE)
300
use_address_dialog = TRUE;
302
use_address_dialog = FALSE;
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);
309
if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
311
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
312
_("Plugin configuration directory could not be created."));
316
/* write config to file */
317
gchar *data = g_key_file_to_data(config, NULL, NULL);
318
utils_write_file(config_file, data);
321
g_key_file_free(config);
326
GtkWidget *plugin_configure(GtkDialog *dialog)
328
GtkWidget *label1, *label2, *vbox;
330
vbox = gtk_vbox_new(FALSE, 6);
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);
339
gtk_entry_set_text(GTK_ENTRY(pref_widgets.entry), mailer);
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"\
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);
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);
358
pref_widgets.checkbox_use_addressdialog = gtk_check_button_new_with_label(_
359
("Using dialog for entering email address of recipients"));
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);
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);
371
gtk_widget_show(vbox);
373
g_signal_connect(dialog, "response", G_CALLBACK(on_configure_response), NULL);
377
/* Called by Geany to initialize the plugin */
378
void plugin_init(GeanyData G_GNUC_UNUSED *data)
380
GtkTooltips *tooltips = NULL;
382
GKeyFile *config = g_key_file_new();
384
gchar *kb_label = _("Send file by mail");
386
GtkWidget *menu_mail = NULL;
388
main_locale_init(LOCALEDIR, GETTEXT_PACKAGE);
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);
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);
400
g_key_file_free(config);
402
tooltips = gtk_tooltips_new();
405
if (icon_in_toolbar == TRUE)
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);
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);
421
gtk_widget_show_all(menu_mail);
422
ui_add_document_sensitive(menu_mail);
423
main_menu_item = menu_mail;
427
void plugin_cleanup()
429
gtk_widget_destroy(main_menu_item);