Updated Spanish translation
[anjuta-git-plugin.git] / plugins / search / search_preferences.c
blob0c41cc1c95907702f4380a3d5a085979c5667c25
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /***************************************************************************
3 * search_preferences.c
5 * Sat Nov 27 18:26:50 2004
6 * Copyright 2004 Jean-Noel GUIHENEUF
7 * guiheneuf.jean-noel@wanadoo.fr
8 ****************************************************************************/
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include <string.h>
33 #include <gnome.h>
34 #include <gconf/gconf-client.h>
36 #include "search-replace_backend.h"
37 #include "search-replace.h"
38 #include "search_preferences.h"
41 enum {PREF_DEFAULT_COLUMN, PREF_NAME_COLUMN, PREF_ACTIVE_COLUMN};
43 #define SEARCH_PREF_PATH "/apps/anjuta/search_preferences"
44 #define BASIC _("Basic Search")
47 static GSList *list_pref = NULL;
48 static gchar *default_pref = NULL;
50 static SearchReplace *sr = NULL;
54 static GSList *search_preferences_find_setting(gchar *name);
55 static gboolean on_search_preferences_clear_default_foreach (GtkTreeModel *model,
56 GtkTreePath *path, GtkTreeIter *iter, gpointer data);
57 static gboolean on_search_preferences_setting_inactive (GtkTreeModel *model,
58 GtkTreePath *path, GtkTreeIter *iter, gpointer data);
59 static void search_preferences_update_entry(gchar *name);
60 static void search_preferences_read_setting(gchar *name);
61 static void search_preferences_setting_by_default(void);
62 static void search_preferences_save_setting(gchar *name);
63 static void search_preferences_save_search_pref(gchar *name);
64 static GtkTreeModel* search_preferences_get_model(void);
65 static void search_preferences_add_treeview(gchar *name);
66 static void search_preferences_remove_setting(gchar *name);
67 static void search_preferences_activate_default(gchar *name);
68 static void search_preferences_active_selection_row(GtkTreeView *view);
69 static void on_search_preferences_colorize_setting (GtkTreeViewColumn *tree_column,
70 GtkCellRenderer *cell, GtkTreeModel *tree_model,
71 GtkTreeIter *iter, gpointer data);
72 static void on_search_preferences_row_activated (GtkTreeView *view,
73 GtkTreePath *tree_path,
74 GtkTreeViewColumn *view_column,
75 GtkCellRenderer *renderer);
76 static void on_search_preferences_treeview_enable_toggle (GtkCellRendererToggle *cell,
77 gchar *path_str, gpointer data);
78 static gboolean search_preferences_name_is_valid(gchar *name);
79 void search_preferences_initialize_setting_treeview(GtkWidget *dialog);
84 static GSList*
85 search_preferences_find_setting(gchar *name)
87 GSList *list;
89 for(list = list_pref; list; list=g_slist_next(list))
91 if (g_ascii_strcasecmp(name, list->data) == 0)
92 return list;
94 return NULL;
98 static gboolean
99 on_search_preferences_clear_default_foreach (GtkTreeModel *model, GtkTreePath *path,
100 GtkTreeIter *iter, gpointer data)
102 gchar *t_name;
103 gint active;
105 gtk_tree_model_get (model, iter, PREF_NAME_COLUMN, &t_name,
106 PREF_ACTIVE_COLUMN, &active, -1);
107 if ((data != NULL) && (g_ascii_strcasecmp(t_name, (gchar*) data) == 0))
108 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
109 PREF_DEFAULT_COLUMN, TRUE,
110 PREF_ACTIVE_COLUMN, TRUE, -1);
111 else
112 gtk_tree_store_set (GTK_TREE_STORE (model), iter,
113 PREF_DEFAULT_COLUMN, FALSE,
114 -1);
115 return FALSE;
119 static gboolean
120 on_search_preferences_setting_inactive (GtkTreeModel *model, GtkTreePath *path,
121 GtkTreeIter *iter, gpointer data)
123 gtk_tree_store_set (GTK_TREE_STORE (model), iter, PREF_ACTIVE_COLUMN, FALSE, -1);
124 return FALSE;
128 static void
129 search_preferences_update_entry(gchar *name)
131 GtkWidget *pref_entry;
133 pref_entry = sr_get_gladewidget(SETTING_PREF_ENTRY)->widget;
134 gtk_entry_set_text(GTK_ENTRY(pref_entry), name);
138 static void
139 search_preferences_read_setting(gchar *name)
141 GConfClient *client;
143 client = gconf_client_get_default();
145 sr->search.expr.regex = gconf_client_get_bool(client,
146 gconf_concat_dir_and_key(name, "regex"), NULL);
147 sr->search.expr.greedy = gconf_client_get_bool(client,
148 gconf_concat_dir_and_key(name, "greedy"), NULL);
149 sr->search.expr.ignore_case = gconf_client_get_bool(client,
150 gconf_concat_dir_and_key(name, "ignore_case"), NULL);
151 sr->search.expr.whole_word = gconf_client_get_bool(client,
152 gconf_concat_dir_and_key(name, "whole_word"), NULL);
153 sr->search.expr.whole_line = gconf_client_get_bool(client,
154 gconf_concat_dir_and_key(name, "whole_line"), NULL);
155 sr->search.expr.word_start = gconf_client_get_bool(client,
156 gconf_concat_dir_and_key(name, "word_start"), NULL);
157 sr->search.expr.no_limit = gconf_client_get_bool(client,
158 gconf_concat_dir_and_key(name, "no_limit"), NULL);
159 sr->search.expr.actions_max = gconf_client_get_int(client,
160 gconf_concat_dir_and_key(name, "actions_max"), NULL);
161 sr->search.range.type = gconf_client_get_int(client,
162 gconf_concat_dir_and_key(name, "type"), NULL);
163 sr->search.range.direction = gconf_client_get_int(client,
164 gconf_concat_dir_and_key(name, "direction"), NULL);
165 sr->search.action = gconf_client_get_int(client,
166 gconf_concat_dir_and_key(name, "action"), NULL);
167 sr->search.basic_search = gconf_client_get_bool(client,
168 gconf_concat_dir_and_key(name, "basic_search"), NULL);
170 search_update_dialog();
174 static void
175 search_preferences_setting_by_default(void)
177 sr->search.expr.regex =FALSE;
178 sr->search.expr.greedy = FALSE;
179 sr->search.expr.ignore_case = FALSE;
180 sr->search.expr.whole_word = FALSE;
181 sr->search.expr.whole_line = FALSE;
182 sr->search.expr.word_start = FALSE;
183 sr->search.expr.no_limit =TRUE;
184 sr->search.expr.actions_max = 200;
185 sr->search.range.type = SR_BUFFER;
186 sr->search.range.direction = SD_FORWARD;
187 sr->search.action = SA_SELECT;
188 sr->search.basic_search = TRUE;
190 search_update_dialog();
194 static void
195 search_preferences_save_setting(gchar *name)
197 GConfClient *client;
198 gchar *path;
200 search_replace_populate();
202 client = gconf_client_get_default();
203 path = gconf_concat_dir_and_key(SEARCH_PREF_PATH, name);
205 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "regex"),
206 sr->search.expr.regex, NULL);
207 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "greedy"),
208 sr->search.expr.greedy, NULL);
209 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "ignore_case"),
210 sr->search.expr.ignore_case, NULL);
211 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "whole_word"),
212 sr->search.expr.whole_word, NULL);
213 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "whole_line"),
214 sr->search.expr.whole_line, NULL);
215 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "word_start"),
216 sr->search.expr.word_start, NULL);
217 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "no_limit"),
218 sr->search.expr.no_limit, NULL);
219 gconf_client_set_int(client, gconf_concat_dir_and_key(path, "actions_max"),
220 sr->search.expr.actions_max, NULL);
221 gconf_client_set_int(client, gconf_concat_dir_and_key(path, "type"),
222 sr->search.range.type, NULL);
223 gconf_client_set_int(client, gconf_concat_dir_and_key(path, "direction"),
224 sr->search.range.direction, NULL);
225 gconf_client_set_int(client, gconf_concat_dir_and_key(path, "action"),
226 sr->search.action, NULL);
227 gconf_client_set_bool(client, gconf_concat_dir_and_key(path, "basic_search"),
228 sr->search.basic_search, NULL);
232 static void
233 search_preferences_save_search_pref(gchar *name)
235 GConfClient *client;
236 gchar *path;
238 client = gconf_client_get_default();
239 gconf_client_set_list(client, gconf_concat_dir_and_key(SEARCH_PREF_PATH,
240 "list_pref"), GCONF_VALUE_STRING, list_pref, NULL);
242 path = gconf_concat_dir_and_key(SEARCH_PREF_PATH, name);
243 gconf_client_add_dir(client, path, GCONF_CLIENT_PRELOAD_NONE, NULL);
245 search_preferences_save_setting(name);
249 static GtkTreeModel*
250 search_preferences_get_model(void)
252 GtkTreeView *view;
254 view = GTK_TREE_VIEW (sr_get_gladewidget(SETTING_PREF_TREEVIEW)->widget);
255 return gtk_tree_view_get_model(view);
259 static void
260 search_preferences_add_treeview(gchar *name)
262 GtkTreeModel *model;
263 GtkTreeIter iter;
265 model = search_preferences_get_model();
266 gtk_tree_model_foreach (model, on_search_preferences_setting_inactive, NULL);
267 gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);
268 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
269 PREF_DEFAULT_COLUMN, FALSE,
270 PREF_NAME_COLUMN, name,
271 PREF_ACTIVE_COLUMN, TRUE, -1);
275 static void
276 search_preferences_remove_setting(gchar *name)
278 GConfClient *client;
280 client = gconf_client_get_default();
281 gconf_client_set_list(client, gconf_concat_dir_and_key(SEARCH_PREF_PATH,
282 "list_pref"), GCONF_VALUE_STRING, list_pref, NULL);
283 // FIXME : Remove Setting Directory
284 gconf_client_remove_dir(client, gconf_concat_dir_and_key(SEARCH_PREF_PATH, name), NULL);
289 static void
290 search_preferences_activate_default(gchar *name)
292 GtkTreeView *view;
293 GtkTreeModel *model;
295 view = GTK_TREE_VIEW (sr_get_gladewidget(SETTING_PREF_TREEVIEW)->widget);
296 model = gtk_tree_view_get_model (view);
298 gtk_tree_model_foreach (model, on_search_preferences_clear_default_foreach, name);
302 static void
303 search_preferences_active_selection_row(GtkTreeView *view)
305 GtkTreeSelection *selection;
306 GtkTreeModel *model;
307 GtkTreeIter iter;
308 gchar *name;
310 selection = gtk_tree_view_get_selection (view);
312 if (gtk_tree_selection_get_selected (selection, &model, &iter) == TRUE)
314 gtk_tree_model_foreach (model, on_search_preferences_setting_inactive, NULL);
315 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
316 PREF_ACTIVE_COLUMN, TRUE,
317 -1);
318 gtk_tree_model_get (model, &iter, PREF_NAME_COLUMN, &name, -1);
319 search_preferences_update_entry(name);
321 if (g_strcasecmp(name, BASIC))
322 search_preferences_read_setting(gconf_concat_dir_and_key(
323 SEARCH_PREF_PATH, name));
324 else
325 search_preferences_setting_by_default();
330 static void
331 on_search_preferences_colorize_setting (GtkTreeViewColumn *tree_column,
332 GtkCellRenderer *cell, GtkTreeModel *tree_model,
333 GtkTreeIter *iter, gpointer data)
335 gboolean active;
336 static const gchar *colors[] = {"black", "red"};
337 GValue gvalue = {0, };
339 gtk_tree_model_get (tree_model, iter, PREF_ACTIVE_COLUMN, &active, -1);
340 g_value_init (&gvalue, G_TYPE_STRING);
341 g_value_set_static_string (&gvalue, colors[active? 1 : 0]);
342 g_object_set_property (G_OBJECT (cell), "foreground", &gvalue);
346 static void
347 on_search_preferences_row_activated (GtkTreeView *view,
348 GtkTreePath *tree_path,
349 GtkTreeViewColumn *view_column,
350 GtkCellRenderer *renderer)
352 search_preferences_active_selection_row(view);
356 static void
357 on_search_preferences_treeview_enable_toggle (GtkCellRendererToggle *cell,
358 gchar *path_str,
359 gpointer data)
361 GtkTreeView *view;
362 GtkTreeModel *model;
363 GtkTreeIter iter;
364 GtkTreePath *path;
365 gboolean state;
366 gchar *name;
367 GConfClient *client;
370 path = gtk_tree_path_new_from_string (path_str);
371 view = GTK_TREE_VIEW (sr_get_gladewidget(SETTING_PREF_TREEVIEW)->widget);
372 model = gtk_tree_view_get_model (view);
373 gtk_tree_model_get_iter (model, &iter, path);
374 gtk_tree_model_get (model, &iter, PREF_NAME_COLUMN, &name,
375 PREF_DEFAULT_COLUMN, &state, -1);
377 client = gconf_client_get_default();
378 if (state)
380 gtk_tree_store_set (GTK_TREE_STORE (model), &iter, PREF_DEFAULT_COLUMN, FALSE, -1);
381 gconf_client_set_string(client, gconf_concat_dir_and_key(SEARCH_PREF_PATH,
382 "search_pref_default"), "", NULL);
384 else
386 gconf_client_set_string(client, gconf_concat_dir_and_key(SEARCH_PREF_PATH,
387 "search_pref_default"), name, NULL);
389 gtk_tree_model_foreach (model, on_search_preferences_clear_default_foreach, NULL);
391 gtk_tree_store_set (GTK_TREE_STORE (model), &iter, PREF_DEFAULT_COLUMN, TRUE, -1);
396 static gboolean
397 search_preferences_name_is_valid(gchar *name)
399 gint i;
401 for(i=0; i<strlen(name); i++)
402 if ((!g_ascii_isalnum(name[i]) && name[i]!='_'))
403 return FALSE;
404 return TRUE;
408 void
409 on_setting_pref_add_clicked(GtkButton *button, gpointer user_data)
411 GtkWidget *pref_entry;
412 gchar *name;
414 pref_entry = sr_get_gladewidget(SETTING_PREF_ENTRY)->widget;
415 name = g_strstrip(gtk_editable_get_chars(GTK_EDITABLE(pref_entry), 0, -1));
417 if (!name || strlen(name) < 1)
418 return;
420 if (!search_preferences_name_is_valid(name))
421 return;
422 if (search_preferences_find_setting(name))
423 return;
425 if (g_strcasecmp(name, BASIC))
427 list_pref = g_slist_append(list_pref, g_strdup(name));
429 search_preferences_save_search_pref(name);
431 search_preferences_add_treeview(name);
433 g_free(name);
437 void
438 on_setting_pref_remove_clicked(GtkButton *button, gpointer user_data)
440 GtkTreeView *view;
441 GtkTreeStore *store;
442 GtkTreeSelection *selection;
443 GtkTreeModel *model;
444 GtkTreeIter iter;
445 gboolean valid;
446 gchar *name;
447 GConfClient *client;
449 view = GTK_TREE_VIEW (sr_get_gladewidget(SETTING_PREF_TREEVIEW)->widget);
450 store = GTK_TREE_STORE (gtk_tree_view_get_model(view));
451 selection = gtk_tree_view_get_selection (view);
452 valid = gtk_tree_selection_get_selected (selection, &model, &iter);
453 if (valid)
455 gtk_tree_model_get (model, &iter, PREF_NAME_COLUMN, &name, -1);
456 if (g_strcasecmp(name, BASIC))
458 gchar *path;
460 client = gconf_client_get_default();
461 path = gconf_client_get_string(client, gconf_concat_dir_and_key(SEARCH_PREF_PATH,
462 "search_pref_default"), NULL);
463 gtk_tree_store_remove(store, &iter);
465 list_pref = g_slist_remove(list_pref, search_preferences_find_setting(name)->data);
467 search_preferences_remove_setting(name);
469 if (!g_strcasecmp(name, path))
471 gconf_client_set_string(client, gconf_concat_dir_and_key(
472 SEARCH_PREF_PATH, "search_pref_default"), "", NULL);
474 g_free(path);
475 search_preferences_update_entry("");
481 void
482 on_setting_pref_modify_clicked(GtkButton *button, gpointer user_data)
484 GtkTreeView *view;
485 GtkTreeStore *store;
486 GtkTreeSelection *selection;
487 GtkTreeModel *model;
488 GtkTreeIter iter;
489 gboolean valid;
490 gchar *name;
492 view = GTK_TREE_VIEW (sr_get_gladewidget(SETTING_PREF_TREEVIEW)->widget);
493 store = GTK_TREE_STORE (gtk_tree_view_get_model(view));
494 selection = gtk_tree_view_get_selection (view);
495 valid = gtk_tree_selection_get_selected (selection, &model, &iter);
496 if (valid)
498 gtk_tree_model_get (model, &iter, PREF_NAME_COLUMN, &name, -1);
499 if (g_strcasecmp(name, BASIC))
501 search_preferences_save_setting(name);
502 search_preferences_update_entry("");
508 void
509 search_preferences_initialize_setting_treeview(GtkWidget *dialog)
511 GtkTreeView *view;
512 GtkTreeStore *store;
513 GtkCellRenderer *renderer;
514 GtkTreeViewColumn *column;
516 view = GTK_TREE_VIEW (sr_get_gladewidget(SETTING_PREF_TREEVIEW)->widget);
517 store = gtk_tree_store_new (3, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_BOOLEAN);
518 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
519 gtk_tree_selection_set_mode (gtk_tree_view_get_selection (view),
520 GTK_SELECTION_SINGLE);
522 renderer = gtk_cell_renderer_toggle_new ();
523 column = gtk_tree_view_column_new_with_attributes (_("Default"),
524 renderer,
525 "active",
526 PREF_DEFAULT_COLUMN,
527 NULL);
528 g_signal_connect (renderer, "toggled",
529 G_CALLBACK (on_search_preferences_treeview_enable_toggle), NULL);
530 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
531 gtk_tree_view_append_column (view, column);
533 renderer = gtk_cell_renderer_text_new ();
534 column = gtk_tree_view_column_new_with_attributes (_("Name"),
535 renderer,
536 "text",
537 PREF_NAME_COLUMN,
538 NULL);
539 g_signal_connect (view, "row-activated",
540 G_CALLBACK (on_search_preferences_row_activated), renderer);
541 gtk_tree_view_column_set_cell_data_func (column, renderer,
542 on_search_preferences_colorize_setting, NULL, NULL);
543 gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
544 gtk_tree_view_append_column (view, column);
547 void
548 search_preferences_init(void)
550 GConfClient *client;
551 GSList *list;
552 GtkTreeModel *model;
553 GtkTreeIter iter;
555 sr = create_search_replace_instance(NULL);
557 search_preferences_add_treeview(BASIC);
559 client = gconf_client_get_default();
560 gconf_client_add_dir(client, SEARCH_PREF_PATH, GCONF_CLIENT_PRELOAD_NONE, NULL);
562 list_pref = gconf_client_get_list(client,gconf_concat_dir_and_key(SEARCH_PREF_PATH,
563 "list_pref"), GCONF_VALUE_STRING, NULL);
565 for (list = list_pref; list != NULL; list = g_slist_next(list))
566 search_preferences_add_treeview(list->data);
568 default_pref = gconf_client_get_string(client,gconf_concat_dir_and_key(SEARCH_PREF_PATH,
569 "search_pref_default"), NULL);
571 model = search_preferences_get_model();
572 gtk_tree_model_foreach (model, on_search_preferences_setting_inactive, NULL);
574 if (default_pref && g_strcasecmp(default_pref, "") && g_strcasecmp(default_pref, BASIC))
575 search_preferences_read_setting(gconf_concat_dir_and_key(SEARCH_PREF_PATH,
576 default_pref));
577 else
579 gtk_tree_model_get_iter_first(model, &iter);
580 gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
581 PREF_ACTIVE_COLUMN, TRUE, -1);
582 search_preferences_setting_by_default();
585 search_preferences_activate_default(default_pref);