diff options
| author | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
| commit | 0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch) | |
| tree | af916a30553c78ce9d4f2d8658656a175c5b6921 /src/prefs_actions.c | |
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/prefs_actions.c')
| -rw-r--r-- | src/prefs_actions.c | 674 |
1 files changed, 674 insertions, 0 deletions
diff --git a/src/prefs_actions.c b/src/prefs_actions.c new file mode 100644 index 0000000..4718493 --- /dev/null +++ b/src/prefs_actions.c | |||
| @@ -0,0 +1,674 @@ | |||
| 1 | /* | ||
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client | ||
| 3 | * Copyright (C) 1999-2015 Hiroyuki Yamamoto & The Sylpheed Claws Team | ||
| 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 <gtk/gtk.h> | ||
| 29 | #include <gdk/gdkkeysyms.h> | ||
| 30 | #include <stdio.h> | ||
| 31 | #include <stdlib.h> | ||
| 32 | #include <string.h> | ||
| 33 | #include <errno.h> | ||
| 34 | |||
| 35 | #include "prefs.h" | ||
| 36 | #include "inc.h" | ||
| 37 | #include "utils.h" | ||
| 38 | #include "gtkutils.h" | ||
| 39 | #include "manage_window.h" | ||
| 40 | #include "mainwindow.h" | ||
| 41 | #include "prefs_common.h" | ||
| 42 | #include "alertpanel.h" | ||
| 43 | #include "prefs_actions.h" | ||
| 44 | #include "action.h" | ||
| 45 | |||
| 46 | static struct Actions | ||
| 47 | { | ||
| 48 | GtkWidget *window; | ||
| 49 | |||
| 50 | GtkWidget *confirm_area; | ||
| 51 | GtkWidget *ok_btn; | ||
| 52 | |||
| 53 | GtkWidget *name_entry; | ||
| 54 | GtkWidget *cmd_entry; | ||
| 55 | |||
| 56 | GtkWidget *actions_clist; | ||
| 57 | } actions; | ||
| 58 | |||
| 59 | /* widget creating functions */ | ||
| 60 | static void prefs_actions_create (MainWindow *mainwin); | ||
| 61 | static void prefs_actions_set_dialog (void); | ||
| 62 | static gint prefs_actions_clist_set_row (gint row); | ||
| 63 | |||
| 64 | /* callback functions */ | ||
| 65 | static void prefs_actions_help_cb (GtkWidget *w, | ||
| 66 | gpointer data); | ||
| 67 | static void prefs_actions_register_cb (GtkWidget *w, | ||
| 68 | gpointer data); | ||
| 69 | static void prefs_actions_substitute_cb (GtkWidget *w, | ||
| 70 | gpointer data); | ||
| 71 | static void prefs_actions_delete_cb (GtkWidget *w, | ||
| 72 | gpointer data); | ||
| 73 | static void prefs_actions_up (GtkWidget *w, | ||
| 74 | gpointer data); | ||
| 75 | static void prefs_actions_down (GtkWidget *w, | ||
| 76 | gpointer data); | ||
| 77 | static void prefs_actions_select (GtkCList *clist, | ||
| 78 | gint row, | ||
| 79 | gint column, | ||
| 80 | GdkEvent *event); | ||
| 81 | static void prefs_actions_row_move (GtkCList *clist, | ||
| 82 | gint source_row, | ||
| 83 | gint dest_row); | ||
| 84 | static gint prefs_actions_deleted (GtkWidget *widget, | ||
| 85 | GdkEventAny *event, | ||
| 86 | gpointer *data); | ||
| 87 | static gboolean prefs_actions_key_pressed (GtkWidget *widget, | ||
| 88 | GdkEventKey *event, | ||
| 89 | gpointer data); | ||
| 90 | static void prefs_actions_cancel (GtkWidget *w, | ||
| 91 | gpointer data); | ||
| 92 | static void prefs_actions_ok (GtkWidget *w, | ||
| 93 | gpointer data); | ||
| 94 | |||
| 95 | |||
| 96 | void prefs_actions_open(MainWindow *mainwin) | ||
| 97 | { | ||
| 98 | inc_lock(); | ||
| 99 | |||
| 100 | if (!actions.window) | ||
| 101 | prefs_actions_create(mainwin); | ||
| 102 | |||
| 103 | gtkut_box_set_reverse_order(GTK_BOX(actions.confirm_area), | ||
| 104 | !prefs_common.comply_gnome_hig); | ||
| 105 | manage_window_set_transient(GTK_WINDOW(actions.window)); | ||
| 106 | gtk_widget_grab_focus(actions.ok_btn); | ||
| 107 | |||
| 108 | prefs_actions_set_dialog(); | ||
| 109 | |||
| 110 | gtk_widget_show(actions.window); | ||
| 111 | } | ||
| 112 | |||
| 113 | static void prefs_actions_create(MainWindow *mainwin) | ||
| 114 | { | ||
| 115 | GtkWidget *window; | ||
| 116 | GtkWidget *vbox; | ||
| 117 | GtkWidget *ok_btn; | ||
| 118 | GtkWidget *cancel_btn; | ||
| 119 | GtkWidget *confirm_area; | ||
| 120 | |||
| 121 | GtkWidget *vbox1; | ||
| 122 | |||
| 123 | GtkWidget *entry_vbox; | ||
| 124 | GtkWidget *hbox; | ||
| 125 | GtkWidget *name_label; | ||
| 126 | GtkWidget *name_entry; | ||
| 127 | GtkWidget *cmd_label; | ||
| 128 | GtkWidget *cmd_entry; | ||
| 129 | |||
| 130 | GtkWidget *reg_hbox; | ||
| 131 | GtkWidget *btn_hbox; | ||
| 132 | GtkWidget *arrow; | ||
| 133 | GtkWidget *reg_btn; | ||
| 134 | GtkWidget *subst_btn; | ||
| 135 | GtkWidget *del_btn; | ||
| 136 | |||
| 137 | GtkWidget *cond_hbox; | ||
| 138 | GtkWidget *cond_scrolledwin; | ||
| 139 | GtkWidget *cond_clist; | ||
| 140 | |||
| 141 | GtkWidget *help_vbox; | ||
| 142 | GtkWidget *help_label; | ||
| 143 | GtkWidget *help_toggle; | ||
| 144 | |||
| 145 | GtkWidget *btn_vbox; | ||
| 146 | GtkWidget *up_btn; | ||
| 147 | GtkWidget *down_btn; | ||
| 148 | |||
| 149 | gchar *title[1]; | ||
| 150 | |||
| 151 | debug_print("Creating actions configuration window...\n"); | ||
| 152 | |||
| 153 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); | ||
| 154 | |||
| 155 | gtk_container_set_border_width(GTK_CONTAINER (window), 8); | ||
| 156 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); | ||
| 157 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); | ||
| 158 | gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE); | ||
| 159 | gtk_window_set_default_size(GTK_WINDOW(window), 400, -1); | ||
| 160 | |||
| 161 | vbox = gtk_vbox_new(FALSE, 6); | ||
| 162 | gtk_widget_show(vbox); | ||
| 163 | gtk_container_add(GTK_CONTAINER(window), vbox); | ||
| 164 | |||
| 165 | gtkut_stock_button_set_create(&confirm_area, &ok_btn, GTK_STOCK_OK, | ||
| 166 | &cancel_btn, GTK_STOCK_CANCEL, | ||
| 167 | NULL, NULL); | ||
| 168 | gtk_widget_show(confirm_area); | ||
| 169 | gtk_box_pack_end(GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0); | ||
| 170 | gtk_widget_grab_default(ok_btn); | ||
| 171 | |||
| 172 | gtk_window_set_title(GTK_WINDOW(window), _("Actions configuration")); | ||
| 173 | g_signal_connect(G_OBJECT(window), "delete_event", | ||
| 174 | G_CALLBACK(prefs_actions_deleted), NULL); | ||
| 175 | g_signal_connect(G_OBJECT(window), "key_press_event", | ||
| 176 | G_CALLBACK(prefs_actions_key_pressed), NULL); | ||
| 177 | MANAGE_WINDOW_SIGNALS_CONNECT(window); | ||
| 178 | g_signal_connect(G_OBJECT(ok_btn), "clicked", | ||
| 179 | G_CALLBACK(prefs_actions_ok), mainwin); | ||
| 180 | g_signal_connect(G_OBJECT(cancel_btn), "clicked", | ||
| 181 | G_CALLBACK(prefs_actions_cancel), NULL); | ||
| 182 | |||
| 183 | vbox1 = gtk_vbox_new(FALSE, 8); | ||
| 184 | gtk_widget_show(vbox1); | ||
| 185 | gtk_box_pack_start(GTK_BOX(vbox), vbox1, TRUE, TRUE, 0); | ||
| 186 | gtk_container_set_border_width(GTK_CONTAINER(vbox1), 2); | ||
| 187 | |||
| 188 | entry_vbox = gtk_vbox_new(FALSE, 4); | ||
| 189 | gtk_box_pack_start(GTK_BOX(vbox1), entry_vbox, FALSE, FALSE, 0); | ||
| 190 | |||
| 191 | hbox = gtk_hbox_new(FALSE, 8); | ||
| 192 | gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, FALSE, FALSE, 0); | ||
| 193 | |||
| 194 | name_label = gtk_label_new(_("Menu name:")); | ||
| 195 | gtk_box_pack_start(GTK_BOX(hbox), name_label, FALSE, FALSE, 0); | ||
| 196 | |||
| 197 | name_entry = gtk_entry_new(); | ||
| 198 | gtk_box_pack_start(GTK_BOX(hbox), name_entry, TRUE, TRUE, 0); | ||
| 199 | |||
| 200 | hbox = gtk_hbox_new(FALSE, 8); | ||
| 201 | gtk_box_pack_start(GTK_BOX(entry_vbox), hbox, TRUE, TRUE, 0); | ||
| 202 | |||
| 203 | cmd_label = gtk_label_new(_("Command line:")); | ||
| 204 | gtk_box_pack_start(GTK_BOX(hbox), cmd_label, FALSE, FALSE, 0); | ||
| 205 | |||
| 206 | cmd_entry = gtk_entry_new(); | ||
| 207 | gtk_box_pack_start(GTK_BOX(hbox), cmd_entry, TRUE, TRUE, 0); | ||
| 208 | |||
| 209 | gtk_widget_show_all(entry_vbox); | ||
| 210 | |||
| 211 | help_vbox = gtk_vbox_new(FALSE, 8); | ||
| 212 | gtk_box_pack_start(GTK_BOX(vbox1), help_vbox, FALSE, FALSE, 0); | ||
| 213 | |||
| 214 | help_label = gtk_label_new | ||
| 215 | (_("Menu name:\n" | ||
| 216 | " Use / in menu name to make submenus.\n" | ||
| 217 | "Command line:\n" | ||
| 218 | " Begin with:\n" | ||
| 219 | " | to send message body or selection to command\n" | ||
| 220 | " > to send user provided text to command\n" | ||
| 221 | " * to send user provided hidden text to command\n" | ||
| 222 | " End with:\n" | ||
| 223 | " | to replace message body or selection with command output\n" | ||
| 224 | " > to insert command's output without replacing old text\n" | ||
| 225 | " & to run command asynchronously\n" | ||
| 226 | " Use:\n" | ||
| 227 | " %f for message file name\n" | ||
| 228 | " %F for the list of the file names of selected messages\n" | ||
| 229 | " %p for the selected message part\n" | ||
| 230 | " %u for a user provided argument\n" | ||
| 231 | " %h for a user provided hidden argument\n" | ||
| 232 | " %s for the text selection")); | ||
| 233 | gtk_misc_set_alignment(GTK_MISC(help_label), 0, 0.5); | ||
| 234 | gtk_label_set_justify(GTK_LABEL(help_label), GTK_JUSTIFY_LEFT); | ||
| 235 | gtk_widget_show(help_label); | ||
| 236 | gtk_box_pack_start(GTK_BOX(help_vbox), help_label, FALSE, FALSE, 0); | ||
| 237 | gtk_widget_hide(help_vbox); | ||
| 238 | |||
| 239 | /* register / substitute / delete */ | ||
| 240 | |||
| 241 | reg_hbox = gtk_hbox_new(FALSE, 4); | ||
| 242 | gtk_widget_show(reg_hbox); | ||
| 243 | gtk_box_pack_start(GTK_BOX(vbox1), reg_hbox, FALSE, FALSE, 0); | ||
| 244 | |||
| 245 | arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_OUT); | ||
| 246 | gtk_widget_show(arrow); | ||
| 247 | gtk_box_pack_start(GTK_BOX(reg_hbox), arrow, FALSE, FALSE, 0); | ||
| 248 | gtk_widget_set_size_request(arrow, -1, 16); | ||
| 249 | |||
| 250 | btn_hbox = gtk_hbox_new(TRUE, 4); | ||
| 251 | gtk_widget_show(btn_hbox); | ||
| 252 | gtk_box_pack_start(GTK_BOX(reg_hbox), btn_hbox, FALSE, FALSE, 0); | ||
| 253 | |||
| 254 | reg_btn = gtk_button_new_with_label(_("Add")); | ||
| 255 | gtk_widget_show(reg_btn); | ||
| 256 | gtk_box_pack_start(GTK_BOX(btn_hbox), reg_btn, FALSE, TRUE, 0); | ||
| 257 | g_signal_connect(G_OBJECT(reg_btn), "clicked", | ||
| 258 | G_CALLBACK(prefs_actions_register_cb), NULL); | ||
| 259 | |||
| 260 | subst_btn = gtk_button_new_with_label(_(" Replace ")); | ||
| 261 | gtk_widget_show(subst_btn); | ||
| 262 | gtk_box_pack_start(GTK_BOX(btn_hbox), subst_btn, FALSE, TRUE, 0); | ||
| 263 | g_signal_connect(G_OBJECT(subst_btn), "clicked", | ||
| 264 | G_CALLBACK(prefs_actions_substitute_cb), NULL); | ||
| 265 | |||
| 266 | del_btn = gtk_button_new_with_label(_("Delete")); | ||
| 267 | gtk_widget_show(del_btn); | ||
| 268 | gtk_box_pack_start(GTK_BOX(btn_hbox), del_btn, FALSE, TRUE, 0); | ||
| 269 | g_signal_connect(G_OBJECT(del_btn), "clicked", | ||
| 270 | G_CALLBACK(prefs_actions_delete_cb), NULL); | ||
| 271 | |||
| 272 | help_toggle = gtk_toggle_button_new_with_label(_(" Syntax help ")); | ||
| 273 | gtk_widget_show(help_toggle); | ||
| 274 | gtk_box_pack_end(GTK_BOX(reg_hbox), help_toggle, FALSE, FALSE, 0); | ||
| 275 | g_signal_connect(G_OBJECT(help_toggle), "toggled", | ||
| 276 | G_CALLBACK(prefs_actions_help_cb), help_vbox); | ||
| 277 | |||
| 278 | cond_hbox = gtk_hbox_new(FALSE, 8); | ||
| 279 | gtk_widget_show(cond_hbox); | ||
| 280 | gtk_box_pack_start(GTK_BOX(vbox1), cond_hbox, TRUE, TRUE, 0); | ||
| 281 | |||
| 282 | cond_scrolledwin = gtk_scrolled_window_new(NULL, NULL); | ||
| 283 | gtk_widget_show(cond_scrolledwin); | ||
| 284 | gtk_widget_set_size_request(cond_scrolledwin, -1, | ||
| 285 | 150 * gtkut_get_dpi_multiplier()); | ||
| 286 | gtk_box_pack_start(GTK_BOX(cond_hbox), cond_scrolledwin, | ||
| 287 | TRUE, TRUE, 0); | ||
| 288 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (cond_scrolledwin), | ||
| 289 | GTK_POLICY_AUTOMATIC, | ||
| 290 | GTK_POLICY_AUTOMATIC); | ||
| 291 | |||
| 292 | title[0] = _("Registered actions"); | ||
| 293 | cond_clist = gtk_clist_new_with_titles(1, title); | ||
| 294 | gtk_widget_show(cond_clist); | ||
| 295 | gtk_container_add(GTK_CONTAINER (cond_scrolledwin), cond_clist); | ||
| 296 | gtk_clist_set_column_width(GTK_CLIST (cond_clist), 0, 80); | ||
| 297 | gtk_clist_set_selection_mode(GTK_CLIST (cond_clist), | ||
| 298 | GTK_SELECTION_BROWSE); | ||
| 299 | gtkut_clist_set_redraw(GTK_CLIST(cond_clist)); | ||
| 300 | GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(cond_clist)->column[0].button, | ||
| 301 | GTK_CAN_FOCUS); | ||
| 302 | g_signal_connect(G_OBJECT(cond_clist), "select_row", | ||
| 303 | G_CALLBACK(prefs_actions_select), NULL); | ||
| 304 | g_signal_connect_after(G_OBJECT(cond_clist), "row_move", | ||
| 305 | G_CALLBACK(prefs_actions_row_move), NULL); | ||
| 306 | |||
| 307 | btn_vbox = gtk_vbox_new(FALSE, 8); | ||
| 308 | gtk_widget_show(btn_vbox); | ||
| 309 | gtk_box_pack_start(GTK_BOX(cond_hbox), btn_vbox, FALSE, FALSE, 0); | ||
| 310 | |||
| 311 | up_btn = gtk_button_new_with_label(_("Up")); | ||
| 312 | gtk_widget_show(up_btn); | ||
| 313 | gtk_box_pack_start(GTK_BOX(btn_vbox), up_btn, FALSE, FALSE, 0); | ||
| 314 | g_signal_connect(G_OBJECT(up_btn), "clicked", | ||
| 315 | G_CALLBACK(prefs_actions_up), NULL); | ||
| 316 | |||
| 317 | down_btn = gtk_button_new_with_label(_("Down")); | ||
| 318 | gtk_widget_show(down_btn); | ||
| 319 | gtk_box_pack_start(GTK_BOX(btn_vbox), down_btn, FALSE, FALSE, 0); | ||
| 320 | g_signal_connect(G_OBJECT(down_btn), "clicked", | ||
| 321 | G_CALLBACK(prefs_actions_down), NULL); | ||
| 322 | |||
| 323 | gtk_widget_show(window); | ||
| 324 | |||
| 325 | actions.window = window; | ||
| 326 | |||
| 327 | actions.confirm_area = confirm_area; | ||
| 328 | actions.ok_btn = ok_btn; | ||
| 329 | |||
| 330 | actions.name_entry = name_entry; | ||
| 331 | actions.cmd_entry = cmd_entry; | ||
| 332 | |||
| 333 | actions.actions_clist = cond_clist; | ||
| 334 | } | ||
| 335 | |||
| 336 | static void prefs_actions_help_cb(GtkWidget *w, gpointer data) | ||
| 337 | { | ||
| 338 | if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w))) | ||
| 339 | gtk_widget_show(GTK_WIDGET(data)); | ||
| 340 | else | ||
| 341 | gtk_widget_hide(GTK_WIDGET(data)); | ||
| 342 | } | ||
| 343 | |||
| 344 | void prefs_actions_read_config(void) | ||
| 345 | { | ||
| 346 | gchar *rcpath; | ||
| 347 | FILE *fp; | ||
| 348 | gchar buf[PREFSBUFSIZE]; | ||
| 349 | gchar *act; | ||
| 350 | |||
| 351 | debug_print("Reading actions configurations...\n"); | ||
| 352 | |||
| 353 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL); | ||
| 354 | if ((fp = g_fopen(rcpath, "rb")) == NULL) { | ||
| 355 | if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen"); | ||
| 356 | g_free(rcpath); | ||
| 357 | return; | ||
| 358 | } | ||
| 359 | g_free(rcpath); | ||
| 360 | |||
| 361 | while (prefs_common.actions_list != NULL) { | ||
| 362 | act = (gchar *)prefs_common.actions_list->data; | ||
| 363 | prefs_common.actions_list = | ||
| 364 | g_slist_remove(prefs_common.actions_list, act); | ||
| 365 | g_free(act); | ||
| 366 | } | ||
| 367 | |||
| 368 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
| 369 | g_strchomp(buf); | ||
| 370 | act = strstr(buf, ": "); | ||
| 371 | if (act && act[2] && | ||
| 372 | action_get_type(&act[2]) != ACTION_ERROR) | ||
| 373 | prefs_common.actions_list = | ||
| 374 | g_slist_append(prefs_common.actions_list, | ||
| 375 | g_strdup(buf)); | ||
| 376 | } | ||
| 377 | fclose(fp); | ||
| 378 | } | ||
| 379 | |||
| 380 | void prefs_actions_write_config(void) | ||
| 381 | { | ||
| 382 | gchar *rcpath; | ||
| 383 | PrefFile *pfile; | ||
| 384 | GSList *cur; | ||
| 385 | |||
| 386 | debug_print("Writing actions configuration...\n"); | ||
| 387 | |||
| 388 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACTIONS_RC, NULL); | ||
| 389 | if ((pfile= prefs_file_open(rcpath)) == NULL) { | ||
| 390 | g_warning("failed to write configuration to file\n"); | ||
| 391 | g_free(rcpath); | ||
| 392 | return; | ||
| 393 | } | ||
| 394 | |||
| 395 | for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) { | ||
| 396 | gchar *act = (gchar *)cur->data; | ||
| 397 | if (fputs(act, pfile->fp) == EOF || | ||
| 398 | fputc('\n', pfile->fp) == EOF) { | ||
| 399 | FILE_OP_ERROR(rcpath, "fputs || fputc"); | ||
| 400 | prefs_file_close_revert(pfile); | ||
| 401 | g_free(rcpath); | ||
| 402 | return; | ||
| 403 | } | ||
| 404 | } | ||
| 405 | |||
| 406 | g_free(rcpath); | ||
| 407 | |||
| 408 | if (prefs_file_close(pfile) < 0) { | ||
| 409 | g_warning("failed to write configuration to file\n"); | ||
| 410 | return; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | static void prefs_actions_set_dialog(void) | ||
| 415 | { | ||
| 416 | GtkCList *clist = GTK_CLIST(actions.actions_clist); | ||
| 417 | GSList *cur; | ||
| 418 | gchar *action_str[1]; | ||
| 419 | gint row; | ||
| 420 | |||
| 421 | gtk_clist_freeze(clist); | ||
| 422 | gtk_clist_clear(clist); | ||
| 423 | |||
| 424 | action_str[0] = _("(New)"); | ||
| 425 | row = gtk_clist_append(clist, action_str); | ||
| 426 | gtk_clist_set_row_data(clist, row, NULL); | ||
| 427 | |||
| 428 | for (cur = prefs_common.actions_list; cur != NULL; cur = cur->next) { | ||
| 429 | gchar *action[1]; | ||
| 430 | |||
| 431 | action[0] = (gchar *)cur->data; | ||
| 432 | row = gtk_clist_append(clist, action); | ||
| 433 | gtk_clist_set_row_data(clist, row, action[0]); | ||
| 434 | } | ||
| 435 | |||
| 436 | gtk_clist_thaw(clist); | ||
| 437 | } | ||
| 438 | |||
| 439 | static void prefs_actions_set_list(void) | ||
| 440 | { | ||
| 441 | gint row = 1; | ||
| 442 | gchar *action; | ||
| 443 | |||
| 444 | g_slist_free(prefs_common.actions_list); | ||
| 445 | prefs_common.actions_list = NULL; | ||
| 446 | |||
| 447 | while ((action = (gchar *)gtk_clist_get_row_data | ||
| 448 | (GTK_CLIST(actions.actions_clist), row)) != NULL) { | ||
| 449 | prefs_common.actions_list = | ||
| 450 | g_slist_append(prefs_common.actions_list, action); | ||
| 451 | row++; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | #define GET_ENTRY(entry) \ | ||
| 456 | entry_text = gtk_entry_get_text(GTK_ENTRY(entry)) | ||
| 457 | |||
| 458 | static gint prefs_actions_clist_set_row(gint row) | ||
| 459 | { | ||
| 460 | GtkCList *clist = GTK_CLIST(actions.actions_clist); | ||
| 461 | const gchar *entry_text; | ||
| 462 | gint len; | ||
| 463 | gchar action[PREFSBUFSIZE]; | ||
| 464 | gchar *buf[1]; | ||
| 465 | |||
| 466 | g_return_val_if_fail(row != 0, -1); | ||
| 467 | |||
| 468 | GET_ENTRY(actions.name_entry); | ||
| 469 | if (entry_text[0] == '\0') { | ||
| 470 | alertpanel_error(_("Menu name is not set.")); | ||
| 471 | return -1; | ||
| 472 | } | ||
| 473 | |||
| 474 | if (strchr(entry_text, ':')) { | ||
| 475 | alertpanel_error(_("Colon ':' is not allowed in the menu name.")); | ||
| 476 | return -1; | ||
| 477 | } | ||
| 478 | |||
| 479 | strncpy(action, entry_text, PREFSBUFSIZE - 1); | ||
| 480 | g_strstrip(action); | ||
| 481 | |||
| 482 | /* Keep space for the ': ' delimiter */ | ||
| 483 | len = strlen(action) + 2; | ||
| 484 | if (len >= PREFSBUFSIZE - 1) { | ||
| 485 | alertpanel_error(_("Menu name is too long.")); | ||
| 486 | return -1; | ||
| 487 | } | ||
| 488 | |||
| 489 | strcat(action, ": "); | ||
| 490 | |||
| 491 | GET_ENTRY(actions.cmd_entry); | ||
| 492 | |||
| 493 | if (entry_text[0] == '\0') { | ||
| 494 | alertpanel_error(_("Command line not set.")); | ||
| 495 | return -1; | ||
| 496 | } | ||
| 497 | |||
| 498 | if (len + strlen(entry_text) >= PREFSBUFSIZE - 1) { | ||
| 499 | alertpanel_error(_("Menu name and command are too long.")); | ||
| 500 | return -1; | ||
| 501 | } | ||
| 502 | |||
| 503 | if (action_get_type(entry_text) == ACTION_ERROR) { | ||
| 504 | alertpanel_error(_("The command\n%s\nhas a syntax error."), | ||
| 505 | entry_text); | ||
| 506 | return -1; | ||
| 507 | } | ||
| 508 | |||
| 509 | strcat(action, entry_text); | ||
| 510 | |||
| 511 | buf[0] = action; | ||
| 512 | if (row < 0) | ||
| 513 | row = gtk_clist_append(clist, buf); | ||
| 514 | else { | ||
| 515 | gchar *old_action; | ||
| 516 | gtk_clist_set_text(clist, row, 0, action); | ||
| 517 | old_action = (gchar *) gtk_clist_get_row_data(clist, row); | ||
| 518 | if (old_action) | ||
| 519 | g_free(old_action); | ||
| 520 | } | ||
| 521 | |||
| 522 | buf[0] = g_strdup(action); | ||
| 523 | |||
| 524 | gtk_clist_set_row_data(clist, row, buf[0]); | ||
| 525 | |||
| 526 | prefs_actions_set_list(); | ||
| 527 | |||
| 528 | return 0; | ||
| 529 | } | ||
| 530 | |||
| 531 | /* callback functions */ | ||
| 532 | |||
| 533 | static void prefs_actions_register_cb(GtkWidget *w, gpointer data) | ||
| 534 | { | ||
| 535 | prefs_actions_clist_set_row(-1); | ||
| 536 | } | ||
| 537 | |||
| 538 | static void prefs_actions_substitute_cb(GtkWidget *w, gpointer data) | ||
| 539 | { | ||
| 540 | GtkCList *clist = GTK_CLIST(actions.actions_clist); | ||
| 541 | gchar *action; | ||
| 542 | gint row; | ||
| 543 | |||
| 544 | if (!clist->selection) return; | ||
| 545 | |||
| 546 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 547 | if (row == 0) return; | ||
| 548 | |||
| 549 | action = gtk_clist_get_row_data(clist, row); | ||
| 550 | if (!action) return; | ||
| 551 | |||
| 552 | prefs_actions_clist_set_row(row); | ||
| 553 | } | ||
| 554 | |||
| 555 | static void prefs_actions_delete_cb(GtkWidget *w, gpointer data) | ||
| 556 | { | ||
| 557 | GtkCList *clist = GTK_CLIST(actions.actions_clist); | ||
| 558 | gchar *action; | ||
| 559 | gint row; | ||
| 560 | |||
| 561 | if (!clist->selection) return; | ||
| 562 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 563 | if (row == 0) return; | ||
| 564 | |||
| 565 | if (alertpanel(_("Delete action"), | ||
| 566 | _("Do you really want to delete this action?"), | ||
| 567 | GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT) | ||
| 568 | return; | ||
| 569 | |||
| 570 | action = gtk_clist_get_row_data(clist, row); | ||
| 571 | g_free(action); | ||
| 572 | gtk_clist_remove(clist, row); | ||
| 573 | prefs_common.actions_list = g_slist_remove(prefs_common.actions_list, | ||
| 574 | action); | ||
| 575 | } | ||
| 576 | |||
| 577 | static void prefs_actions_up(GtkWidget *w, gpointer data) | ||
| 578 | { | ||
| 579 | GtkCList *clist = GTK_CLIST(actions.actions_clist); | ||
| 580 | gint row; | ||
| 581 | |||
| 582 | if (!clist->selection) return; | ||
| 583 | |||
| 584 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 585 | if (row > 1) | ||
| 586 | gtk_clist_row_move(clist, row, row - 1); | ||
| 587 | } | ||
| 588 | |||
| 589 | static void prefs_actions_down(GtkWidget *w, gpointer data) | ||
| 590 | { | ||
| 591 | GtkCList *clist = GTK_CLIST(actions.actions_clist); | ||
| 592 | gint row; | ||
| 593 | |||
| 594 | if (!clist->selection) return; | ||
| 595 | |||
| 596 | row = GPOINTER_TO_INT(clist->selection->data); | ||
| 597 | if (row > 0 && row < clist->rows - 1) | ||
| 598 | gtk_clist_row_move(clist, row, row + 1); | ||
| 599 | } | ||
| 600 | |||
| 601 | #define ENTRY_SET_TEXT(entry, str) \ | ||
| 602 | gtk_entry_set_text(GTK_ENTRY(entry), str ? str : "") | ||
| 603 | |||
| 604 | static void prefs_actions_select(GtkCList *clist, gint row, gint column, | ||
| 605 | GdkEvent *event) | ||
| 606 | { | ||
| 607 | gchar *action; | ||
| 608 | gchar *cmd; | ||
| 609 | gchar buf[PREFSBUFSIZE]; | ||
| 610 | action = gtk_clist_get_row_data(clist, row); | ||
| 611 | |||
| 612 | if (!action) { | ||
| 613 | gtk_entry_set_text(GTK_ENTRY(actions.name_entry), ""); | ||
| 614 | gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), ""); | ||
| 615 | return; | ||
| 616 | } | ||
| 617 | |||
| 618 | strncpy(buf, action, PREFSBUFSIZE - 1); | ||
| 619 | buf[PREFSBUFSIZE - 1] = '\0'; | ||
| 620 | cmd = strstr(buf, ": "); | ||
| 621 | |||
| 622 | if (cmd && cmd[2]) { | ||
| 623 | gtk_entry_set_text(GTK_ENTRY(actions.cmd_entry), &cmd[2]); | ||
| 624 | *cmd = '\0'; | ||
| 625 | gtk_entry_set_text(GTK_ENTRY(actions.name_entry), buf); | ||
| 626 | } | ||
| 627 | } | ||
| 628 | |||
| 629 | static void prefs_actions_row_move(GtkCList *clist, | ||
| 630 | gint source_row, gint dest_row) | ||
| 631 | { | ||
| 632 | prefs_actions_set_list(); | ||
| 633 | if (gtk_clist_row_is_visible(clist, dest_row) != GTK_VISIBILITY_FULL) { | ||
| 634 | gtk_clist_moveto(clist, dest_row, -1, | ||
| 635 | source_row < dest_row ? 1.0 : 0.0, 0.0); | ||
| 636 | } | ||
| 637 | } | ||
| 638 | |||
| 639 | static gint prefs_actions_deleted(GtkWidget *widget, GdkEventAny *event, | ||
| 640 | gpointer *data) | ||
| 641 | { | ||
| 642 | prefs_actions_cancel(widget, data); | ||
| 643 | return TRUE; | ||
| 644 | } | ||
| 645 | |||
| 646 | static gboolean prefs_actions_key_pressed(GtkWidget *widget, GdkEventKey *event, | ||
| 647 | gpointer data) | ||
| 648 | { | ||
| 649 | if (event && event->keyval == GDK_Escape) | ||
| 650 | prefs_actions_cancel(widget, data); | ||
| 651 | return FALSE; | ||
| 652 | } | ||
| 653 | |||
| 654 | static void prefs_actions_cancel(GtkWidget *w, gpointer data) | ||
| 655 | { | ||
| 656 | prefs_actions_read_config(); | ||
| 657 | gtk_widget_hide(actions.window); | ||
| 658 | main_window_popup(main_window_get()); | ||
| 659 | inc_unlock(); | ||
| 660 | } | ||
| 661 | |||
| 662 | static void prefs_actions_ok(GtkWidget *widget, gpointer data) | ||
| 663 | { | ||
| 664 | GtkItemFactory *ifactory; | ||
| 665 | MainWindow *mainwin = (MainWindow *)data; | ||
| 666 | |||
| 667 | prefs_actions_write_config(); | ||
| 668 | ifactory = gtk_item_factory_from_widget(mainwin->menubar); | ||
| 669 | action_update_mainwin_menu(ifactory, mainwin); | ||
| 670 | gtk_widget_hide(actions.window); | ||
| 671 | main_window_popup(main_window_get()); | ||
| 672 | inc_unlock(); | ||
| 673 | } | ||
| 674 | |||
