1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 * Copyright (C) 2004 Fernando Herrera <fherrera@onirica.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
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 GNU
13 * General Public License for more details.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "gconf-search.h"
23 #include "gconf-search-dialog.h"
24 #include "gconf-editor-window.h"
25 #include "gconf-tree-model.h"
26 #include "gconf-list-model.h"
28 typedef struct _Node Node;
43 gconf_tree_model_search_iter_foreach (GtkTreeModel *model, GtkTreePath *path,
44 GtkTreeIter *iter, gpointer data)
49 GSList *values, *list;
51 st = (SearchIter *) data;
53 if (st->searching == NULL) {
58 gtk_widget_show (GTK_WIDGET (st->output_window));
60 while (gtk_events_pending ())
61 gtk_main_iteration ();
63 node = iter->user_data;
64 found = g_strrstr ((char*) node->path, (char*) st->pattern);
67 /* We found the pattern in the tree */
68 gchar *key = gconf_tree_model_get_gconf_path (GCONF_TREE_MODEL (model), iter);
69 gedit_output_window_append_line (st->output_window, key, FALSE);
75 if (!st->search_keys && !st->search_values) {
78 values = gconf_client_all_entries (GCONF_TREE_MODEL (model)->client, (const char*) node->path , NULL);
79 for (list = values; list; list = list->next) {
81 GConfEntry *entry = list->data;
82 key = gconf_entry_get_key (entry);
83 /* Search in the key names */
84 if (st->search_keys) {
85 found = g_strrstr (key, (char*) st->pattern);
87 /* We found the pattern in the final key name */
88 gedit_output_window_append_line (st->output_window, key, FALSE);
90 gconf_entry_free (entry);
91 /* After finding an entry continue the list to find other matches */
96 /* Search in the values */
97 if (st->search_values) {
98 const char *gconf_string;
99 GConfValue *gconf_value = gconf_entry_get_value (entry);
101 /* FIXME: We are only looking into strings... should we do in
103 if (gconf_value != NULL && gconf_value->type == GCONF_VALUE_STRING)
104 gconf_string = gconf_value_get_string (gconf_value);
106 gconf_entry_free (entry);
110 found = g_strrstr (gconf_string, (char*) st->pattern);
112 /* We found the pattern in the key value */
113 gedit_output_window_append_line (st->output_window, key, FALSE);
115 gconf_entry_free (entry);
119 gconf_entry_free (entry);
126 gconf_tree_model_build_match_list (GConfTreeModel *tree_model, GeditOutputWindow *output_window,
127 const char *pattern, gboolean search_keys, gboolean search_values,
130 GtkTreeIter iter_root;
134 st = g_new0 (SearchIter, 1);
135 st->pattern = pattern;
136 st->search_keys = search_keys;
137 st->search_values = search_values;
138 st->output_window = output_window;
140 st->searching = dialog;
142 g_object_add_weak_pointer (st->searching, (gpointer)&st->searching);
144 if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (tree_model), &iter_root)) {
146 /* Ugh, something is terribly wrong */
150 gtk_tree_model_foreach (GTK_TREE_MODEL (tree_model),
151 gconf_tree_model_search_iter_foreach, st);
155 g_free (st); /* This causes invalid memory access according to valgrind */
156 #endif /* FIXME: This introduces a small leak (24 bytes) */