root / src / prefs_search_folder.c @ 891
History | View | Annotate | Download (12.4 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <gdk/gdkkeysyms.h> |
| 29 | #include <gtk/gtkwidget.h> |
| 30 | #include <gtk/gtkwindow.h> |
| 31 | #include <gtk/gtkstyle.h> |
| 32 | #include <gtk/gtkvbox.h> |
| 33 | #include <gtk/gtkoptionmenu.h> |
| 34 | #include <gtk/gtkmenuitem.h> |
| 35 | #include <gtk/gtklabel.h> |
| 36 | #include <gtk/gtkentry.h> |
| 37 | #include <gtk/gtkhbox.h> |
| 38 | #include <gtk/gtkcheckbutton.h> |
| 39 | #include <gtk/gtkhbbox.h> |
| 40 | #include <gtk/gtkbutton.h> |
| 41 | #include <gtk/gtkstock.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | |
| 46 | #include "prefs_search_folder.h" |
| 47 | #include "prefs_ui.h" |
| 48 | #include "mainwindow.h" |
| 49 | #include "manage_window.h" |
| 50 | #include "alertpanel.h" |
| 51 | #include "foldersel.h" |
| 52 | #include "folder.h" |
| 53 | #include "filter.h" |
| 54 | #include "prefs_filter.h" |
| 55 | #include "prefs_filter_edit.h" |
| 56 | #include "menu.h" |
| 57 | #include "utils.h" |
| 58 | #include "gtkutils.h" |
| 59 | |
| 60 | typedef struct _PrefsSearchFolderDialog PrefsSearchFolderDialog; |
| 61 | |
| 62 | struct _PrefsSearchFolderDialog
|
| 63 | {
|
| 64 | PrefsDialog *dialog; |
| 65 | FolderItem *item; |
| 66 | |
| 67 | GtkWidget *name_entry; |
| 68 | |
| 69 | GtkWidget *bool_optmenu; |
| 70 | |
| 71 | FilterCondEdit *cond_edit; |
| 72 | |
| 73 | GtkWidget *folder_entry; |
| 74 | GtkWidget *folder_btn; |
| 75 | |
| 76 | GtkWidget *subfolder_checkbtn; |
| 77 | GtkWidget *case_checkbtn; |
| 78 | }; |
| 79 | |
| 80 | static PrefsSearchFolderDialog *prefs_search_folder_create(FolderItem *item);
|
| 81 | static void prefs_search_folder_set_dialog(PrefsSearchFolderDialog *dialog); |
| 82 | |
| 83 | static gint prefs_search_folder_delete_cb(GtkWidget *widget,
|
| 84 | GdkEventAny *event, |
| 85 | PrefsSearchFolderDialog *dialog); |
| 86 | static gint prefs_search_folder_key_press_cb
|
| 87 | (GtkWidget *widget, |
| 88 | GdkEventKey *event, |
| 89 | PrefsSearchFolderDialog *dialog); |
| 90 | |
| 91 | static void prefs_search_folder_select_folder |
| 92 | (GtkWidget *widget, |
| 93 | PrefsSearchFolderDialog *dialog); |
| 94 | |
| 95 | static void prefs_search_folder_ok_cb (GtkWidget *widget, |
| 96 | PrefsSearchFolderDialog *dialog); |
| 97 | static void prefs_search_folder_apply_cb (GtkWidget *widget, |
| 98 | PrefsSearchFolderDialog *dialog); |
| 99 | static void prefs_search_folder_cancel_cb(GtkWidget *widget, |
| 100 | PrefsSearchFolderDialog *dialog); |
| 101 | |
| 102 | |
| 103 | void prefs_search_folder_open(FolderItem *item)
|
| 104 | {
|
| 105 | PrefsSearchFolderDialog *dialog; |
| 106 | |
| 107 | g_return_if_fail(item != NULL);
|
| 108 | |
| 109 | dialog = prefs_search_folder_create(item); |
| 110 | manage_window_set_transient(GTK_WINDOW(dialog->dialog->window)); |
| 111 | prefs_search_folder_set_dialog(dialog); |
| 112 | gtk_widget_show(dialog->dialog->window); |
| 113 | } |
| 114 | |
| 115 | static PrefsSearchFolderDialog *prefs_search_folder_create(FolderItem *item)
|
| 116 | {
|
| 117 | PrefsSearchFolderDialog *new_dialog; |
| 118 | PrefsDialog *dialog; |
| 119 | gchar *title; |
| 120 | GtkWidget *vbox; |
| 121 | |
| 122 | GtkWidget *name_hbox; |
| 123 | GtkWidget *name_label; |
| 124 | GtkWidget *name_entry; |
| 125 | GtkStyle *style; |
| 126 | |
| 127 | GtkWidget *bool_hbox; |
| 128 | GtkWidget *bool_optmenu; |
| 129 | GtkWidget *bool_menu; |
| 130 | GtkWidget *menuitem; |
| 131 | |
| 132 | GtkWidget *scrolledwin; |
| 133 | FilterCondEdit *cond_edit; |
| 134 | |
| 135 | GtkWidget *folder_hbox; |
| 136 | GtkWidget *folder_label; |
| 137 | GtkWidget *folder_entry; |
| 138 | GtkWidget *folder_btn; |
| 139 | |
| 140 | GtkWidget *checkbtn_hbox; |
| 141 | GtkWidget *subfolder_checkbtn; |
| 142 | GtkWidget *case_checkbtn; |
| 143 | |
| 144 | new_dialog = g_new0(PrefsSearchFolderDialog, 1);
|
| 145 | |
| 146 | dialog = g_new0(PrefsDialog, 1);
|
| 147 | prefs_dialog_create(dialog); |
| 148 | gtk_widget_hide(dialog->apply_btn); |
| 149 | |
| 150 | gtk_widget_set_size_request(dialog->window, 600, -1); |
| 151 | title = g_strdup_printf(_("%s - Edit search condition"), item->name);
|
| 152 | gtk_window_set_title(GTK_WINDOW(dialog->window), title); |
| 153 | g_free(title); |
| 154 | gtk_notebook_set_show_border(GTK_NOTEBOOK(dialog->notebook), FALSE); |
| 155 | gtk_notebook_set_show_tabs(GTK_NOTEBOOK(dialog->notebook), FALSE); |
| 156 | |
| 157 | g_signal_connect(G_OBJECT(dialog->window), "delete_event",
|
| 158 | G_CALLBACK(prefs_search_folder_delete_cb), new_dialog); |
| 159 | g_signal_connect(G_OBJECT(dialog->window), "key_press_event",
|
| 160 | G_CALLBACK(prefs_search_folder_key_press_cb), |
| 161 | new_dialog); |
| 162 | |
| 163 | MANAGE_WINDOW_SIGNALS_CONNECT(dialog->window); |
| 164 | |
| 165 | vbox = gtk_vbox_new(FALSE, 6);
|
| 166 | gtk_widget_show(vbox); |
| 167 | gtk_container_add(GTK_CONTAINER(dialog->notebook), vbox); |
| 168 | |
| 169 | name_hbox = gtk_hbox_new(FALSE, 8);
|
| 170 | gtk_widget_show(name_hbox); |
| 171 | gtk_box_pack_start(GTK_BOX(vbox), name_hbox, FALSE, FALSE, 0);
|
| 172 | |
| 173 | name_label = gtk_label_new(_("Name:"));
|
| 174 | gtk_widget_show(name_label); |
| 175 | gtk_box_pack_start(GTK_BOX(name_hbox), name_label, FALSE, FALSE, 0);
|
| 176 | |
| 177 | name_entry = gtk_entry_new(); |
| 178 | gtk_widget_show(name_entry); |
| 179 | gtk_editable_set_editable(GTK_EDITABLE(name_entry), FALSE); |
| 180 | gtk_box_pack_start(GTK_BOX(name_hbox), name_entry, TRUE, TRUE, 0);
|
| 181 | |
| 182 | style = gtk_style_copy(gtk_widget_get_style(dialog->window)); |
| 183 | style->base[GTK_STATE_NORMAL] = style->bg[GTK_STATE_NORMAL]; |
| 184 | gtk_widget_set_style(name_entry, style); |
| 185 | |
| 186 | bool_hbox = gtk_hbox_new(FALSE, 12);
|
| 187 | gtk_widget_show(bool_hbox); |
| 188 | gtk_box_pack_start(GTK_BOX(vbox), bool_hbox, FALSE, FALSE, 0);
|
| 189 | |
| 190 | bool_optmenu = gtk_option_menu_new(); |
| 191 | gtk_widget_show(bool_optmenu); |
| 192 | gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
|
| 193 | |
| 194 | bool_menu = gtk_menu_new(); |
| 195 | MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"),
|
| 196 | FLT_OR); |
| 197 | MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"),
|
| 198 | FLT_AND); |
| 199 | gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu); |
| 200 | |
| 201 | scrolledwin = gtk_scrolled_window_new(NULL, NULL); |
| 202 | gtk_widget_show(scrolledwin); |
| 203 | gtk_widget_set_size_request(scrolledwin, -1, 150); |
| 204 | gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, TRUE, TRUE, 0);
|
| 205 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin), |
| 206 | GTK_POLICY_AUTOMATIC, |
| 207 | GTK_POLICY_AUTOMATIC); |
| 208 | |
| 209 | cond_edit = prefs_filter_edit_cond_edit_create(); |
| 210 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwin), |
| 211 | cond_edit->cond_vbox); |
| 212 | |
| 213 | folder_hbox = gtk_hbox_new(FALSE, 8);
|
| 214 | gtk_widget_show(folder_hbox); |
| 215 | gtk_box_pack_start(GTK_BOX(vbox), folder_hbox, FALSE, FALSE, 0);
|
| 216 | |
| 217 | folder_label = gtk_label_new(_("Folder:"));
|
| 218 | gtk_widget_show(folder_label); |
| 219 | gtk_box_pack_start(GTK_BOX(folder_hbox), folder_label, FALSE, FALSE, 0);
|
| 220 | |
| 221 | folder_entry = gtk_entry_new(); |
| 222 | gtk_widget_show(folder_entry); |
| 223 | gtk_box_pack_start(GTK_BOX(folder_hbox), folder_entry, TRUE, TRUE, 0);
|
| 224 | |
| 225 | folder_btn = gtk_button_new_with_label("...");
|
| 226 | gtk_widget_show(folder_btn); |
| 227 | gtk_box_pack_start(GTK_BOX(folder_hbox), folder_btn, FALSE, FALSE, 0);
|
| 228 | |
| 229 | checkbtn_hbox = gtk_hbox_new(FALSE, 12);
|
| 230 | gtk_widget_show(checkbtn_hbox); |
| 231 | gtk_box_pack_start(GTK_BOX(vbox), checkbtn_hbox, FALSE, FALSE, 0);
|
| 232 | |
| 233 | subfolder_checkbtn = |
| 234 | gtk_check_button_new_with_label(_("Search subfolders"));
|
| 235 | gtk_widget_show(subfolder_checkbtn); |
| 236 | gtk_box_pack_start(GTK_BOX(checkbtn_hbox), subfolder_checkbtn, |
| 237 | FALSE, FALSE, 0);
|
| 238 | |
| 239 | case_checkbtn = gtk_check_button_new_with_label(_("Case sensitive"));
|
| 240 | gtk_widget_show(case_checkbtn); |
| 241 | gtk_box_pack_start(GTK_BOX(checkbtn_hbox), case_checkbtn, |
| 242 | FALSE, FALSE, 0);
|
| 243 | |
| 244 | g_signal_connect(G_OBJECT(folder_btn), "clicked",
|
| 245 | G_CALLBACK(prefs_search_folder_select_folder), |
| 246 | new_dialog); |
| 247 | g_signal_connect(G_OBJECT(dialog->ok_btn), "clicked",
|
| 248 | G_CALLBACK(prefs_search_folder_ok_cb), new_dialog); |
| 249 | g_signal_connect(G_OBJECT(dialog->apply_btn), "clicked",
|
| 250 | G_CALLBACK(prefs_search_folder_apply_cb), new_dialog); |
| 251 | g_signal_connect(G_OBJECT(dialog->cancel_btn), "clicked",
|
| 252 | G_CALLBACK(prefs_search_folder_cancel_cb), new_dialog); |
| 253 | |
| 254 | gtk_widget_grab_focus(dialog->ok_btn); |
| 255 | |
| 256 | new_dialog->dialog = dialog; |
| 257 | new_dialog->item = item; |
| 258 | new_dialog->name_entry = name_entry; |
| 259 | new_dialog->bool_optmenu = bool_optmenu; |
| 260 | new_dialog->cond_edit = cond_edit; |
| 261 | |
| 262 | new_dialog->folder_entry = folder_entry; |
| 263 | new_dialog->folder_btn = folder_btn; |
| 264 | new_dialog->subfolder_checkbtn = subfolder_checkbtn; |
| 265 | new_dialog->case_checkbtn = case_checkbtn; |
| 266 | |
| 267 | return new_dialog;
|
| 268 | } |
| 269 | |
| 270 | static void prefs_search_folder_set_dialog(PrefsSearchFolderDialog *dialog) |
| 271 | {
|
| 272 | GSList *flist; |
| 273 | FilterRule *rule; |
| 274 | GSList *cur; |
| 275 | gchar *path; |
| 276 | gchar *rule_file; |
| 277 | gint index; |
| 278 | gboolean case_sens = FALSE; |
| 279 | |
| 280 | path = folder_item_get_path(dialog->item); |
| 281 | rule_file = g_strconcat(path, G_DIR_SEPARATOR_S, "filter.xml", NULL); |
| 282 | flist = filter_read_file(rule_file); |
| 283 | g_free(rule_file); |
| 284 | g_free(path); |
| 285 | |
| 286 | if (!flist) {
|
| 287 | g_warning("filter rule not found\n");
|
| 288 | return;
|
| 289 | } |
| 290 | |
| 291 | gtk_entry_set_text(GTK_ENTRY(dialog->name_entry), dialog->item->name); |
| 292 | |
| 293 | rule = (FilterRule *)flist->data; |
| 294 | |
| 295 | index = menu_find_option_menu_index |
| 296 | (GTK_OPTION_MENU(dialog->bool_optmenu), |
| 297 | GINT_TO_POINTER(rule->bool_op), NULL);
|
| 298 | if (index < 0) |
| 299 | index = 0;
|
| 300 | gtk_option_menu_set_history(GTK_OPTION_MENU(dialog->bool_optmenu), |
| 301 | index); |
| 302 | |
| 303 | gtk_entry_set_text(GTK_ENTRY(dialog->folder_entry), |
| 304 | rule->target_folder); |
| 305 | gtk_toggle_button_set_active |
| 306 | (GTK_TOGGLE_BUTTON(dialog->subfolder_checkbtn), |
| 307 | rule->recursive); |
| 308 | for (cur = rule->cond_list; cur != NULL; cur = cur->next) { |
| 309 | FilterCond *cond = (FilterCond *)cur->data; |
| 310 | if (FLT_IS_CASE_SENS(cond->match_flag)) {
|
| 311 | case_sens = TRUE; |
| 312 | break;
|
| 313 | } |
| 314 | } |
| 315 | gtk_toggle_button_set_active |
| 316 | (GTK_TOGGLE_BUTTON(dialog->case_checkbtn), case_sens); |
| 317 | |
| 318 | prefs_filter_set_header_list(NULL);
|
| 319 | prefs_filter_edit_set_header_list(dialog->cond_edit, rule); |
| 320 | prefs_filter_edit_add_rule_cond(dialog->cond_edit, rule); |
| 321 | |
| 322 | filter_rule_list_free(flist); |
| 323 | } |
| 324 | |
| 325 | static void prefs_search_folder_destroy(PrefsSearchFolderDialog *dialog) |
| 326 | {
|
| 327 | prefs_dialog_destroy(dialog->dialog); |
| 328 | g_free(dialog->dialog); |
| 329 | g_free(dialog); |
| 330 | |
| 331 | main_window_popup(main_window_get()); |
| 332 | } |
| 333 | |
| 334 | static gint prefs_search_folder_delete_cb(GtkWidget *widget, GdkEventAny *event,
|
| 335 | PrefsSearchFolderDialog *dialog) |
| 336 | {
|
| 337 | prefs_search_folder_destroy(dialog); |
| 338 | return TRUE;
|
| 339 | } |
| 340 | |
| 341 | static gint prefs_search_folder_key_press_cb(GtkWidget *widget,
|
| 342 | GdkEventKey *event, |
| 343 | PrefsSearchFolderDialog *dialog) |
| 344 | {
|
| 345 | if (event && event->keyval == GDK_Escape) {
|
| 346 | prefs_search_folder_cancel_cb(widget, dialog); |
| 347 | return TRUE;
|
| 348 | } |
| 349 | return FALSE;
|
| 350 | } |
| 351 | |
| 352 | static void prefs_search_folder_select_folder(GtkWidget *widget, |
| 353 | PrefsSearchFolderDialog *dialog) |
| 354 | {
|
| 355 | FolderItem *item; |
| 356 | gchar *id; |
| 357 | |
| 358 | item = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL); |
| 359 | if (!item || item->stype == F_VIRTUAL)
|
| 360 | return;
|
| 361 | |
| 362 | id = folder_item_get_identifier(item); |
| 363 | if (id) {
|
| 364 | gtk_entry_set_text(GTK_ENTRY(dialog->folder_entry), id); |
| 365 | g_free(id); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static void prefs_search_folder_ok_cb(GtkWidget *widget, |
| 370 | PrefsSearchFolderDialog *dialog) |
| 371 | {
|
| 372 | prefs_search_folder_apply_cb(widget, dialog); |
| 373 | prefs_search_folder_destroy(dialog); |
| 374 | } |
| 375 | |
| 376 | static void prefs_search_folder_apply_cb(GtkWidget *widget, |
| 377 | PrefsSearchFolderDialog *dialog) |
| 378 | {
|
| 379 | const gchar *id;
|
| 380 | FolderItem *item; |
| 381 | FilterBoolOp bool_op; |
| 382 | gboolean recursive; |
| 383 | gboolean case_sens; |
| 384 | GSList *cond_list; |
| 385 | FilterRule *rule; |
| 386 | GSList list; |
| 387 | gchar *file; |
| 388 | gchar *path; |
| 389 | |
| 390 | id = gtk_entry_get_text(GTK_ENTRY(dialog->folder_entry)); |
| 391 | item = folder_find_item_from_identifier(id); |
| 392 | if (!item)
|
| 393 | return;
|
| 394 | |
| 395 | bool_op = menu_get_option_menu_active_index |
| 396 | (GTK_OPTION_MENU(dialog->bool_optmenu)); |
| 397 | recursive = gtk_toggle_button_get_active |
| 398 | (GTK_TOGGLE_BUTTON(dialog->subfolder_checkbtn)); |
| 399 | case_sens = gtk_toggle_button_get_active |
| 400 | (GTK_TOGGLE_BUTTON(dialog->case_checkbtn)); |
| 401 | |
| 402 | cond_list = prefs_filter_edit_cond_edit_to_list(dialog->cond_edit, |
| 403 | case_sens); |
| 404 | if (!cond_list)
|
| 405 | return;
|
| 406 | |
| 407 | rule = filter_rule_new(dialog->item->name, bool_op, cond_list, NULL);
|
| 408 | rule->target_folder = g_strdup(id); |
| 409 | rule->recursive = recursive; |
| 410 | list.data = rule; |
| 411 | list.next = NULL;
|
| 412 | |
| 413 | path = folder_item_get_path(dialog->item); |
| 414 | file = g_strconcat(path, G_DIR_SEPARATOR_S, FILTER_LIST, NULL);
|
| 415 | filter_write_file(&list, file); |
| 416 | g_free(file); |
| 417 | file = g_strconcat(path, G_DIR_SEPARATOR_S, FILTER_LIST, ".bak", NULL); |
| 418 | if (is_file_exist(file))
|
| 419 | g_unlink(file); |
| 420 | g_free(file); |
| 421 | file = g_strconcat(path, G_DIR_SEPARATOR_S, SEARCH_CACHE, NULL);
|
| 422 | if (is_file_exist(file))
|
| 423 | g_unlink(file); |
| 424 | g_free(file); |
| 425 | g_free(path); |
| 426 | |
| 427 | filter_rule_free(rule); |
| 428 | } |
| 429 | |
| 430 | static void prefs_search_folder_cancel_cb(GtkWidget *widget, |
| 431 | PrefsSearchFolderDialog *dialog) |
| 432 | {
|
| 433 | prefs_search_folder_destroy(dialog); |
| 434 | } |