1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9 * for more details.
10 *
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, see <http://www.gnu.org/licenses/>.
13 *
14 *
15 * Authors:
16 * Johnny Jacob <jjohnny@novell.com>
17 *
18 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19 *
20 */
21
22 #include "evolution-config.h"
23
24 #include <gtk/gtk.h>
25 #include <glib/gi18n.h>
26 #include <string.h>
27
28 #include <camel/camel.h>
29 #include <camel/camel-search-private.h>
30
31 #include <e-util/e-util.h>
32
33 #include <mail/em-config.h>
34 #include <mail/em-event.h>
35
36 #include <mail/em-composer-utils.h>
37 #include <mail/em-utils.h>
38
39 #include "composer/e-msg-composer.h"
40 #include "composer/e-composer-actions.h"
41
42 #define CONF_KEY_ATTACH_REMINDER_CLUES "attachment-reminder-clues"
43
44 typedef struct {
45 GSettings *settings;
46 GtkWidget *treeview;
47 GtkWidget *clue_add;
48 GtkWidget *clue_edit;
49 GtkWidget *clue_remove;
50 GtkListStore *store;
51 } UIData;
52
53 enum {
54 CLUE_KEYWORD_COLUMN,
55 CLUE_N_COLUMNS
56 };
57
58 enum {
59 AR_IS_PLAIN,
60 AR_IS_FORWARD,
61 AR_IS_REPLY
62 };
63
64 gint e_plugin_lib_enable (EPlugin *ep,
65 gint enable);
66 GtkWidget * e_plugin_lib_get_configure_widget
67 (EPlugin *plugin);
68 void org_gnome_evolution_attachment_reminder
69 (EPlugin *ep,
70 EMEventTargetComposer *t);
71 GtkWidget * org_gnome_attachment_reminder_config_option
72 (EPlugin *plugin,
73 EConfigHookItemFactoryData *data);
74
75 static gboolean ask_for_missing_attachment (EPlugin *ep, GtkWindow *widget);
76 static gboolean check_for_attachment_clues (EMsgComposer *composer, GByteArray *msg_text, guint32 ar_flags);
77 static gboolean check_for_attachment (EMsgComposer *composer);
78 static guint32 get_flags_from_composer (EMsgComposer *composer);
79 static void commit_changes (UIData *ui);
80
81 gint
e_plugin_lib_enable(EPlugin * ep,gint enable)82 e_plugin_lib_enable (EPlugin *ep,
83 gint enable)
84 {
85 return 0;
86 }
87
88 void
org_gnome_evolution_attachment_reminder(EPlugin * ep,EMEventTargetComposer * t)89 org_gnome_evolution_attachment_reminder (EPlugin *ep,
90 EMEventTargetComposer *t)
91 {
92 GByteArray *raw_msg_barray;
93
94 /* no need to check for content, when there are attachments */
95 if (check_for_attachment (t->composer))
96 return;
97
98 raw_msg_barray =
99 e_msg_composer_get_raw_message_text_without_signature (t->composer);
100 if (!raw_msg_barray)
101 return;
102
103 /* Set presend_check_status for the composer*/
104 if (check_for_attachment_clues (t->composer, raw_msg_barray, get_flags_from_composer (t->composer))) {
105 if (!ask_for_missing_attachment (ep, (GtkWindow *) t->composer))
106 g_object_set_data (
107 G_OBJECT (t->composer),
108 "presend_check_status",
109 GINT_TO_POINTER (1));
110 }
111
112 g_byte_array_free (raw_msg_barray, TRUE);
113 }
114
115 static guint32
get_flags_from_composer(EMsgComposer * composer)116 get_flags_from_composer (EMsgComposer *composer)
117 {
118 const gchar *header;
119
120 g_return_val_if_fail (E_IS_MSG_COMPOSER (composer), AR_IS_PLAIN);
121
122 header = e_msg_composer_get_header (composer, "X-Evolution-Source-Flags", 0);
123 if (!header || !*header)
124 return AR_IS_PLAIN;
125
126 if (e_util_utf8_strstrcase (header, "FORWARDED")) {
127 GSettings *settings;
128 EMailForwardStyle style;
129
130 settings = e_util_ref_settings ("org.gnome.evolution.mail");
131 style = g_settings_get_enum (settings, "forward-style-name");
132 g_object_unref (settings);
133
134 return style == E_MAIL_FORWARD_STYLE_INLINE ? AR_IS_FORWARD : AR_IS_PLAIN;
135 }
136
137 if (e_util_utf8_strstrcase (header, "ANSWERED") ||
138 e_util_utf8_strstrcase (header, "ANSWERED_ALL")) {
139 GSettings *settings;
140 EMailReplyStyle style;
141
142 settings = e_util_ref_settings ("org.gnome.evolution.mail");
143 style = g_settings_get_enum (settings, "reply-style-name");
144 g_object_unref (settings);
145
146 return style == E_MAIL_REPLY_STYLE_OUTLOOK ? AR_IS_REPLY : AR_IS_PLAIN;
147 }
148
149 return AR_IS_PLAIN;
150 }
151
152 static gboolean
ask_for_missing_attachment(EPlugin * ep,GtkWindow * window)153 ask_for_missing_attachment (EPlugin *ep,
154 GtkWindow *window)
155 {
156 GtkWidget *check;
157 GtkWidget *dialog;
158 GtkWidget *container;
159 gint response;
160
161 dialog = e_alert_dialog_new_for_args (
162 window, "org.gnome.evolution.plugins.attachment_reminder:"
163 "attachment-reminder", NULL);
164
165 container = e_alert_dialog_get_content_area (E_ALERT_DIALOG (dialog));
166
167 /*Check buttons*/
168 check = gtk_check_button_new_with_mnemonic (
169 _("_Do not show this message again."));
170 gtk_box_pack_start (GTK_BOX (container), check, FALSE, FALSE, 0);
171 gtk_widget_show (check);
172
173 response = gtk_dialog_run (GTK_DIALOG (dialog));
174
175 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)))
176 e_plugin_enable (ep, FALSE);
177
178 gtk_widget_destroy (dialog);
179
180 if (response == GTK_RESPONSE_OK)
181 gtk_action_activate (E_COMPOSER_ACTION_ATTACH (window));
182
183 return response == GTK_RESPONSE_YES;
184 }
185
186 static void
censor_quoted_lines(GByteArray * msg_text,const gchar * until_marker)187 censor_quoted_lines (GByteArray *msg_text,
188 const gchar *until_marker)
189 {
190 gchar *ptr;
191 gboolean in_quotation = FALSE;
192 gint marker_len;
193
194 g_return_if_fail (msg_text != NULL);
195
196 if (until_marker)
197 marker_len = strlen (until_marker);
198 else
199 marker_len = 0;
200
201 ptr = (gchar *) msg_text->data;
202
203 if (marker_len &&
204 strncmp (ptr, until_marker, marker_len) == 0 &&
205 (ptr[marker_len] == '\r' || ptr[marker_len] == '\n')) {
206 /* Simply cut everything below the marker and the marker itself */
207 if (marker_len > 3) {
208 ptr[0] = '\r';
209 ptr[1] = '\n';
210 ptr[2] = '\0';
211 } else {
212 *ptr = '\0';
213 }
214
215 return;
216 }
217
218 for (ptr = (gchar *) msg_text->data; ptr && *ptr; ptr++) {
219 if (*ptr == '\n') {
220 in_quotation = ptr[1] == '>';
221 if (!in_quotation && marker_len &&
222 strncmp (ptr + 1, until_marker, marker_len) == 0 &&
223 (ptr[1 + marker_len] == '\r' || ptr[1 + marker_len] == '\n')) {
224 /* Simply cut everything below the marker and the marker itself */
225 if (marker_len > 3) {
226 ptr[0] = '\r';
227 ptr[1] = '\n';
228 ptr[2] = '\0';
229 } else {
230 *ptr = '\0';
231 }
232 break;
233 }
234 } else if (*ptr != '\r' && in_quotation) {
235 *ptr = ' ';
236 }
237 }
238 }
239
240 /* check for the clues */
241 static gboolean
check_for_attachment_clues(EMsgComposer * composer,GByteArray * msg_text,guint32 ar_flags)242 check_for_attachment_clues (EMsgComposer *composer,
243 GByteArray *msg_text,
244 guint32 ar_flags)
245 {
246 GSettings *settings;
247 gchar **clue_list;
248 gchar *marker = NULL;
249 gboolean found = FALSE;
250
251 if (ar_flags == AR_IS_FORWARD)
252 marker = em_composer_utils_get_forward_marker (composer);
253 else if (ar_flags == AR_IS_REPLY)
254 marker = em_composer_utils_get_original_marker (composer);
255
256 settings = e_util_ref_settings ("org.gnome.evolution.plugin.attachment-reminder");
257
258 /* Get the list from GSettings */
259 clue_list = g_settings_get_strv (settings, CONF_KEY_ATTACH_REMINDER_CLUES);
260
261 g_object_unref (settings);
262
263 if (clue_list && clue_list[0]) {
264 gint ii, jj, to;
265
266 g_byte_array_append (msg_text, (const guint8 *) "\r\n\0", 3);
267
268 censor_quoted_lines (msg_text, marker);
269
270 for (ii = 0; clue_list[ii] && !found; ii++) {
271 GString *word;
272 const gchar *clue = clue_list[ii];
273
274 if (!*clue)
275 continue;
276
277 word = g_string_new ("\"");
278
279 to = word->len;
280 g_string_append (word, clue);
281
282 for (jj = word->len - 1; jj >= to; jj--) {
283 if (word->str[jj] == '\\' || word->str[jj] == '\"')
284 g_string_insert_c (word, jj, '\\');
285 }
286
287 g_string_append_c (word, '\"');
288
289 found = camel_search_header_match ((const gchar *) msg_text->data, word->str, CAMEL_SEARCH_MATCH_WORD, CAMEL_SEARCH_TYPE_ASIS, NULL);
290
291 g_string_free (word, TRUE);
292 }
293 }
294
295 g_strfreev (clue_list);
296 g_free (marker);
297
298 return found;
299 }
300
301 /* check for the any attachment */
302 static gboolean
check_for_attachment(EMsgComposer * composer)303 check_for_attachment (EMsgComposer *composer)
304 {
305 EAttachmentView *view;
306 EAttachmentStore *store;
307
308 view = e_msg_composer_get_attachment_view (composer);
309 store = e_attachment_view_get_store (view);
310
311 return (e_attachment_store_get_num_attachments (store) > 0);
312 }
313
314 static void
commit_changes(UIData * ui)315 commit_changes (UIData *ui)
316 {
317 GtkTreeModel *model = NULL;
318 GVariantBuilder b;
319 GVariant *v;
320 GtkTreeIter iter;
321 gboolean valid;
322
323 model = gtk_tree_view_get_model (GTK_TREE_VIEW (ui->treeview));
324 valid = gtk_tree_model_get_iter_first (model, &iter);
325
326 g_variant_builder_init (&b, G_VARIANT_TYPE ("as"));
327 while (valid) {
328 gchar *keyword;
329
330 gtk_tree_model_get (
331 model, &iter, CLUE_KEYWORD_COLUMN, &keyword, -1);
332
333 /* Check if the keyword is not empty */
334 if ((keyword) && (g_utf8_strlen (g_strstrip (keyword), -1) > 0))
335 g_variant_builder_add (&b, "s", keyword);
336 g_free (keyword);
337
338 valid = gtk_tree_model_iter_next (model, &iter);
339 }
340
341 /* A floating GVariant is returned, which is consumed by the g_settings_set_value() */
342 v = g_variant_builder_end (&b);
343 g_settings_set_value (ui->settings, CONF_KEY_ATTACH_REMINDER_CLUES, v);
344 }
345
346 static void
cell_edited_cb(GtkCellRendererText * cell,gchar * path_string,gchar * new_text,UIData * ui)347 cell_edited_cb (GtkCellRendererText *cell,
348 gchar *path_string,
349 gchar *new_text,
350 UIData *ui)
351 {
352 GtkTreeModel *model;
353 GtkTreeIter iter;
354
355 model = gtk_tree_view_get_model (GTK_TREE_VIEW (ui->treeview));
356 gtk_tree_model_get_iter_from_string (model, &iter, path_string);
357
358 if (new_text == NULL || *g_strstrip (new_text) == '\0')
359 gtk_button_clicked (GTK_BUTTON (ui->clue_remove));
360 else {
361 gtk_list_store_set (
362 GTK_LIST_STORE (model), &iter,
363 CLUE_KEYWORD_COLUMN, new_text, -1);
364 commit_changes (ui);
365 }
366 }
367
368 static void
cell_editing_canceled_cb(GtkCellRenderer * cell,UIData * ui)369 cell_editing_canceled_cb (GtkCellRenderer *cell,
370 UIData *ui)
371 {
372 gtk_button_clicked (GTK_BUTTON (ui->clue_remove));
373 }
374
375 static void
clue_add_clicked(GtkButton * button,UIData * ui)376 clue_add_clicked (GtkButton *button,
377 UIData *ui)
378 {
379 GtkTreeModel *model;
380 GtkTreeView *tree_view;
381 GtkTreeViewColumn *column;
382 GtkTreePath *path;
383 GtkTreeIter iter;
384
385 tree_view = GTK_TREE_VIEW (ui->treeview);
386 model = gtk_tree_view_get_model (tree_view);
387
388 gtk_list_store_append (GTK_LIST_STORE (model), &iter);
389
390 path = gtk_tree_model_get_path (model, &iter);
391 column = gtk_tree_view_get_column (tree_view, CLUE_KEYWORD_COLUMN);
392 gtk_tree_view_set_cursor (tree_view, path, column, TRUE);
393 gtk_tree_view_row_activated (tree_view, path, column);
394 gtk_tree_path_free (path);
395 }
396
397 static void
clue_remove_clicked(GtkButton * button,UIData * ui)398 clue_remove_clicked (GtkButton *button,
399 UIData *ui)
400 {
401 GtkTreeSelection *selection;
402 GtkTreeModel *model;
403 GtkTreeIter iter;
404 GtkTreePath *path;
405 gboolean valid;
406 gint len;
407
408 valid = FALSE;
409 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ui->treeview));
410 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
411 return;
412
413 /* Get the path and move to the previous node :) */
414 path = gtk_tree_model_get_path (model, &iter);
415 if (path)
416 valid = gtk_tree_path_prev (path);
417
418 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
419
420 len = gtk_tree_model_iter_n_children (model, NULL);
421 if (len > 0) {
422 if (gtk_list_store_iter_is_valid (GTK_LIST_STORE (model), &iter)) {
423 gtk_tree_selection_select_iter (selection, &iter);
424 } else {
425 if (path && valid) {
426 gtk_tree_model_get_iter (model, &iter, path);
427 gtk_tree_selection_select_iter (selection, &iter);
428 }
429 }
430 } else {
431 gtk_widget_set_sensitive (ui->clue_edit, FALSE);
432 gtk_widget_set_sensitive (ui->clue_remove, FALSE);
433 }
434
435 gtk_widget_grab_focus (ui->treeview);
436 gtk_tree_path_free (path);
437
438 commit_changes (ui);
439 }
440
441 static void
clue_edit_clicked(GtkButton * button,UIData * ui)442 clue_edit_clicked (GtkButton *button,
443 UIData *ui)
444 {
445 GtkTreeSelection *selection;
446 GtkTreeModel *model;
447 GtkTreePath *path;
448 GtkTreeIter iter;
449 GtkTreeViewColumn *focus_col;
450
451 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ui->treeview));
452 if (!gtk_tree_selection_get_selected (selection, &model, &iter))
453 return;
454
455 focus_col = gtk_tree_view_get_column (
456 GTK_TREE_VIEW (ui->treeview), CLUE_KEYWORD_COLUMN);
457 path = gtk_tree_model_get_path (model, &iter);
458
459 if (path) {
460 gtk_tree_view_set_cursor (
461 GTK_TREE_VIEW (ui->treeview),
462 path, focus_col, TRUE);
463 gtk_tree_path_free (path);
464 }
465 }
466
467 static void
selection_changed(GtkTreeSelection * selection,UIData * ui)468 selection_changed (GtkTreeSelection *selection,
469 UIData *ui)
470 {
471 GtkTreeModel *model;
472 GtkTreeIter iter;
473
474 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
475 gtk_widget_set_sensitive (ui->clue_edit, TRUE);
476 gtk_widget_set_sensitive (ui->clue_remove, TRUE);
477 } else {
478 gtk_widget_set_sensitive (ui->clue_edit, FALSE);
479 gtk_widget_set_sensitive (ui->clue_remove, FALSE);
480 }
481 }
482
483 static void
destroy_ui_data(gpointer data)484 destroy_ui_data (gpointer data)
485 {
486 UIData *ui = (UIData *) data;
487
488 if (!ui)
489 return;
490
491 g_object_unref (ui->settings);
492 g_free (ui);
493 }
494
495 GtkWidget *
e_plugin_lib_get_configure_widget(EPlugin * plugin)496 e_plugin_lib_get_configure_widget (EPlugin *plugin)
497 {
498 GtkCellRenderer *renderer;
499 GtkTreeSelection *selection;
500 GtkTreeIter iter;
501 GtkWidget *hbox;
502 gchar **clue_list;
503 gint i;
504
505 GtkWidget *reminder_configuration_box;
506 GtkWidget *clue_container;
507 GtkWidget *scrolledwindow1;
508 GtkWidget *clue_treeview;
509 GtkWidget *vbuttonbox2;
510 GtkWidget *clue_add;
511 GtkWidget *clue_edit;
512 GtkWidget *clue_remove;
513
514 UIData *ui = g_new0 (UIData, 1);
515
516 reminder_configuration_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
517 gtk_widget_show (reminder_configuration_box);
518 gtk_widget_set_size_request (reminder_configuration_box, 385, 189);
519
520 clue_container = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
521 gtk_widget_show (clue_container);
522 gtk_box_pack_start (
523 GTK_BOX (reminder_configuration_box),
524 clue_container, TRUE, TRUE, 0);
525
526 scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
527 gtk_widget_show (scrolledwindow1);
528 gtk_box_pack_start (GTK_BOX (clue_container), scrolledwindow1, TRUE, TRUE, 0);
529 gtk_scrolled_window_set_policy (
530 GTK_SCROLLED_WINDOW (scrolledwindow1),
531 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
532
533 clue_treeview = gtk_tree_view_new ();
534 gtk_widget_show (clue_treeview);
535 gtk_container_add (GTK_CONTAINER (scrolledwindow1), clue_treeview);
536 gtk_container_set_border_width (GTK_CONTAINER (clue_treeview), 1);
537
538 vbuttonbox2 = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
539 gtk_widget_show (vbuttonbox2);
540 gtk_box_pack_start (GTK_BOX (clue_container), vbuttonbox2, FALSE, TRUE, 0);
541 gtk_button_box_set_layout (GTK_BUTTON_BOX (vbuttonbox2), GTK_BUTTONBOX_START);
542 gtk_box_set_spacing (GTK_BOX (vbuttonbox2), 6);
543
544 clue_add = e_dialog_button_new_with_icon ("list-add", _("_Add"));
545 gtk_widget_show (clue_add);
546 gtk_container_add (GTK_CONTAINER (vbuttonbox2), clue_add);
547 gtk_widget_set_can_default (clue_add, TRUE);
548
549 clue_edit = gtk_button_new_with_mnemonic (_("_Edit"));
550 gtk_widget_show (clue_edit);
551 gtk_container_add (GTK_CONTAINER (vbuttonbox2), clue_edit);
552 gtk_widget_set_can_default (clue_edit, TRUE);
553
554 clue_remove = e_dialog_button_new_with_icon ("list-remove", _("_Remove"));
555 gtk_widget_show (clue_remove);
556 gtk_container_add (GTK_CONTAINER (vbuttonbox2), clue_remove);
557 gtk_widget_set_can_default (clue_remove, TRUE);
558
559 ui->settings = e_util_ref_settings ("org.gnome.evolution.plugin.attachment-reminder");
560
561 ui->treeview = clue_treeview;
562
563 ui->store = gtk_list_store_new (CLUE_N_COLUMNS, G_TYPE_STRING);
564
565 gtk_tree_view_set_model (
566 GTK_TREE_VIEW (ui->treeview),
567 GTK_TREE_MODEL (ui->store));
568
569 renderer = gtk_cell_renderer_text_new ();
570 gtk_tree_view_insert_column_with_attributes (
571 GTK_TREE_VIEW (ui->treeview), -1, _("Keywords"),
572 renderer, "text", CLUE_KEYWORD_COLUMN, NULL);
573 g_object_set (renderer, "editable", TRUE, NULL);
574 g_signal_connect (
575 renderer, "edited",
576 G_CALLBACK (cell_edited_cb), ui);
577 g_signal_connect (
578 renderer, "editing-canceled",
579 G_CALLBACK (cell_editing_canceled_cb), ui);
580
581 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (ui->treeview));
582 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
583 g_signal_connect (
584 selection, "changed",
585 G_CALLBACK (selection_changed), ui);
586 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (ui->treeview), TRUE);
587
588 ui->clue_add = clue_add;
589 g_signal_connect (
590 ui->clue_add, "clicked",
591 G_CALLBACK (clue_add_clicked), ui);
592
593 ui->clue_remove = clue_remove;
594 g_signal_connect (
595 ui->clue_remove, "clicked",
596 G_CALLBACK (clue_remove_clicked), ui);
597 gtk_widget_set_sensitive (ui->clue_remove, FALSE);
598
599 ui->clue_edit = clue_edit;
600 g_signal_connect (
601 ui->clue_edit, "clicked",
602 G_CALLBACK (clue_edit_clicked), ui);
603 gtk_widget_set_sensitive (ui->clue_edit, FALSE);
604
605 /* Populate tree view with values from GSettings */
606 clue_list = g_settings_get_strv (ui->settings, CONF_KEY_ATTACH_REMINDER_CLUES);
607
608 for (i = 0; clue_list[i] != NULL; i++) {
609 gtk_list_store_append (ui->store, &iter);
610 gtk_list_store_set (ui->store, &iter, CLUE_KEYWORD_COLUMN, clue_list[i], -1);
611 }
612
613 g_strfreev (clue_list);
614
615 /* Add the list here */
616
617 hbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
618
619 gtk_box_pack_start (GTK_BOX (hbox), reminder_configuration_box, TRUE, TRUE, 0);
620
621 /* to let free data properly on destroy of configuration widget */
622 g_object_set_data_full (G_OBJECT (hbox), "myui-data", ui, destroy_ui_data);
623
624 return hbox;
625 }
626
627 /* Configuration in Mail Prefs Page goes here */
628
629 GtkWidget *
org_gnome_attachment_reminder_config_option(EPlugin * plugin,struct _EConfigHookItemFactoryData * data)630 org_gnome_attachment_reminder_config_option (EPlugin *plugin,
631 struct _EConfigHookItemFactoryData *data)
632 {
633 /* This function and the hook needs to be removed,
634 once the configure code is thoroughly tested */
635
636 return NULL;
637
638 }
639
640