fix .desktop file
[gconf-editor] / src / gconf-search-dialog.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3  * Copyright (C) 2004 Fernando Herrera <fherrera@onirica.com>
4  *
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.
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 GNU
13  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include "gconf-search.h"
20 #include "gconf-search-dialog.h"
21 #include "gconf-editor-window.h"
22 #include "gconf-tree-model.h"
23 #include "gconf-list-model.h"
24 #include "gedit-output-window.h"
25 #include <gconf/gconf-client.h>
26 #include <gtk/gtk.h>
27 #include <glib/gi18n.h>
28 #include <string.h>
29
30 #include "gconf-stock-icons.h"
31
32 G_DEFINE_TYPE(GConfSearchDialog, gconf_search_dialog, GTK_TYPE_DIALOG)
33
34 static void
35 gconf_search_dialog_response (GtkDialog *dialog, gint response_id)
36 {
37         gtk_widget_destroy (GTK_WIDGET (dialog));
38 }
39
40 static void
41 gconf_search_dialog_class_init (GConfSearchDialogClass *klass)
42 {
43         GtkDialogClass *dialog_class;
44
45         dialog_class = (GtkDialogClass *)klass;
46
47         dialog_class->response = gconf_search_dialog_response;
48 }
49
50
51 static void
52 gconf_search_not_found_dialog (GConfSearchDialog *dialog) 
53 {
54         GtkWidget *not_found_dialog;
55         
56         not_found_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
57                                                    GTK_DIALOG_DESTROY_WITH_PARENT,
58                                                    GTK_MESSAGE_ERROR,
59                                                    GTK_BUTTONS_CLOSE,
60                                                    _("Pattern not found"));
61         gtk_dialog_run (GTK_DIALOG (not_found_dialog));
62         gtk_widget_destroy (GTK_WIDGET (not_found_dialog));
63 }
64
65 static void
66 gconf_search_entry_changed (GtkEntry *entry, GConfSearchDialog *dialog)
67 {
68         gboolean find_sensitive;
69         const gchar *text;
70
71         text = gtk_entry_get_text (GTK_ENTRY (entry));
72         find_sensitive = text != NULL && text[0] != '\0';
73
74         gtk_widget_set_sensitive (dialog->search_button, find_sensitive);
75 }
76
77 static void
78 gconf_search_dialog_search (GtkWidget *button, GConfSearchDialog *dialog)
79 {
80         GConfEditorWindow *window;
81         GdkCursor *cursor;
82
83         gchar *pattern;
84         int res;
85         
86         window = g_object_get_data (G_OBJECT (dialog), "editor-window");
87         gedit_output_window_clear (GEDIT_OUTPUT_WINDOW (window->output_window));
88         window->output_window_type = GCONF_EDITOR_WINDOW_OUTPUT_WINDOW_SEARCH;
89
90         pattern = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->entry)));
91
92         cursor = gdk_cursor_new (GDK_WATCH);
93         gdk_window_set_cursor (GTK_WIDGET (dialog)->window, cursor);
94         gdk_cursor_unref (cursor);
95         gdk_display_flush (gtk_widget_get_display (GTK_WIDGET (dialog)));
96
97         res = gconf_tree_model_build_match_list  (GCONF_TREE_MODEL (window->tree_model),
98                                                   GEDIT_OUTPUT_WINDOW (window->output_window), 
99                                                   pattern,
100                                                   gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->search_in_keys)),
101                                                   gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->search_in_values)),
102                                                   G_OBJECT (dialog));
103
104         g_free (pattern);
105
106         if (dialog != NULL) {
107 #if 0
108                 g_signal_handlers_disconnect_by_func (dialog->entry,
109                                                       G_CALLBACK (gconf_search_entry_changed), dialog);
110                 g_signal_handlers_disconnect_by_func (dialog->search_button,
111                                                       G_CALLBACK (gconf_search_dialog_search), dialog);
112 #endif
113                 gdk_window_set_cursor (GTK_WIDGET (dialog)->window, NULL);
114                 gdk_display_flush (gtk_widget_get_display (GTK_WIDGET (dialog)));
115
116                 if (res != 0) {
117                         gtk_widget_destroy (GTK_WIDGET (dialog));
118                 } else {
119                         gconf_search_not_found_dialog (dialog);
120                 }
121         }
122 }
123
124 static void
125 gconf_search_dialog_init (GConfSearchDialog *dialog)
126 {
127         GtkWidget *hbox;
128         GtkWidget *vbox;
129         GtkWidget *label;
130
131         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
132         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
133         gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
134         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
135         
136         hbox = gtk_hbox_new (FALSE, 12);
137         vbox = gtk_vbox_new (FALSE, 6);
138         gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
139
140         gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
141
142         gtk_window_set_title (GTK_WINDOW (dialog), _("Find"));
143
144         label = gtk_label_new_with_mnemonic (_("_Search for: "));
145                         
146         dialog->entry = gtk_entry_new ();
147         g_signal_connect (dialog->entry, "changed",
148                           G_CALLBACK (gconf_search_entry_changed), dialog);
149         gtk_label_set_mnemonic_widget (GTK_LABEL(label), GTK_WIDGET(dialog->entry));
150         
151         dialog->search_button = gtk_button_new_from_stock (GTK_STOCK_FIND);
152         gtk_widget_set_sensitive (dialog->search_button, FALSE);
153         g_signal_connect (dialog->search_button, "clicked",
154                           G_CALLBACK (gconf_search_dialog_search), dialog);
155
156         GTK_WIDGET_SET_FLAGS(dialog->search_button, GTK_CAN_DEFAULT);
157         gtk_window_set_default(GTK_WINDOW(dialog), dialog->search_button);
158         gtk_entry_set_activates_default(GTK_ENTRY(dialog->entry), TRUE);
159         
160         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
161         gtk_box_pack_start (GTK_BOX (hbox), dialog->entry, FALSE, FALSE, 0);
162
163         gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
164
165         dialog->search_in_keys = gtk_check_button_new_with_mnemonic (_("Search also in key _names"));
166         gtk_box_pack_start (GTK_BOX (vbox), dialog->search_in_keys, TRUE, TRUE, 0);
167
168         dialog->search_in_values = gtk_check_button_new_with_mnemonic (_("Search also in key _values"));
169         gtk_box_pack_start (GTK_BOX (vbox), dialog->search_in_values, TRUE, TRUE, 0);
170
171         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), vbox, TRUE, TRUE, 0);
172         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->action_area), dialog->search_button, TRUE, TRUE, 0);
173         gtk_widget_show_all (GTK_DIALOG(dialog)->action_area);
174         gtk_widget_show_all (vbox);
175         
176         gtk_widget_show_all (hbox);
177 }
178
179 GtkWidget *
180 gconf_search_dialog_new (GtkWindow *parent)
181 {
182         GtkWidget *dialog;
183
184         dialog = g_object_new (GCONF_TYPE_SEARCH_DIALOG, NULL);
185         g_object_set_data (G_OBJECT (dialog), "editor-window", parent);
186         gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
187         gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
188
189         return dialog;
190 }