summaryrefslogtreecommitdiff
path: root/src/nautilus-complex-search-bar.c
blob: 255696da6cfc338438d35fee1e153af7806e033d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-complex-search-bar.c - Search bar containing many attributes

   Copyright (C) 2000 Eazel, Inc.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this program; see the file COPYING.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Rebecca Schulman <rebecka@eazel.com>
*/



#include "nautilus-complex-search-bar.h"


#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib.h>
#include <gtk/gtkeventbox.h>
#include <libgnomevfs/gnome-vfs-utils.h>

#include <libnautilus-extensions/nautilus-gtk-macros.h>
#include <libnautilus-extensions/nautilus-search-bar-criterion.h>
#include <libnautilus-extensions/nautilus-search-bar-criterion-private.h>
#include <libnautilus-extensions/nautilus-string.h>

struct NautilusComplexSearchBarDetails {
	GtkVBox *bar_container;
	GtkTable *table;

	GtkWidget *more_options;
	GtkWidget *fewer_options;
	GtkWidget *find_them;

	GSList *search_criteria;
	gchar *undo_text;
	gboolean undo_registered;
};

static char *nautilus_complex_search_bar_get_location     (NautilusNavigationBar         *bar);
static void  nautilus_complex_search_bar_set_location     (NautilusNavigationBar         *bar,
							   const char                    *location);
static void  nautilus_complex_search_bar_initialize_class (NautilusComplexSearchBarClass *class);
static void  nautilus_complex_search_bar_initialize       (NautilusComplexSearchBar      *bar);
static void  attach_criterion_to_search_bar               (NautilusComplexSearchBar      *bar,
							   NautilusSearchBarCriterion    *criterion,
							   int                            row_number);
static void  unattach_criterion_from_search_bar           (NautilusComplexSearchBar      *bar,
							   NautilusSearchBarCriterion    *criterion);
static void  more_options_callback                        (GtkObject                     *object,
							   gpointer                       data);
static void  fewer_options_callback                       (GtkObject                     *object,
							   gpointer                       data);
static void  criterion_callback                           (NautilusSearchBarCriterion *old_criterion,
							   NautilusSearchBarCriterion *new_criterion,
							   gpointer data);
static GtkWidget *  load_find_them_pixmap_widget                 (void);

NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusComplexSearchBar, nautilus_complex_search_bar, NAUTILUS_TYPE_SEARCH_BAR)


     /* called by the criterion when the user choosed 
	a new criterion type */
static void criterion_callback (NautilusSearchBarCriterion *old_criterion,
				NautilusSearchBarCriterion *new_criterion,
				gpointer data)
{ 
	NautilusComplexSearchBar *bar;
	int row;
	GSList *criterion_list;
	GSList *list;

	bar = (NautilusComplexSearchBar *) data;

	unattach_criterion_from_search_bar (bar, old_criterion);
	
	criterion_list = g_slist_find (bar->details->search_criteria, 
				       old_criterion);
	row = g_slist_position (bar->details->search_criteria,
				criterion_list);
	g_print ("row %d\n", row);

	list = g_slist_remove (bar->details->search_criteria,
			       old_criterion);
			
	list = g_slist_insert (list, new_criterion, row);

	nautilus_search_bar_criterion_set_callback (new_criterion, 
						    criterion_callback,
						    (gpointer) bar);

	attach_criterion_to_search_bar (bar, new_criterion, row + 1);

}


     
     
static void
nautilus_complex_search_bar_initialize_class (NautilusComplexSearchBarClass *klass)
{
	NAUTILUS_NAVIGATION_BAR_CLASS (klass)->get_location = nautilus_complex_search_bar_get_location;
	NAUTILUS_NAVIGATION_BAR_CLASS (klass)->set_location = nautilus_complex_search_bar_set_location;
}

static void
nautilus_complex_search_bar_initialize (NautilusComplexSearchBar *bar)
{
	NautilusSearchBarCriterion *file_name_criterion;
	GtkWidget *hbox;
	GtkWidget *find_them_box, *find_them_pixmap_widget, *find_them_label;
	
	bar->details = g_new0 (NautilusComplexSearchBarDetails, 1);
	
	bar->details->bar_container = GTK_VBOX (gtk_vbox_new (FALSE, GNOME_PAD_SMALL));

	bar->details->table = GTK_TABLE (gtk_table_new (1, 3, TRUE));
	gtk_table_set_col_spacings (bar->details->table,
				    1);

	gtk_container_set_resize_mode (GTK_CONTAINER (bar->details->table),
				       GTK_RESIZE_IMMEDIATE);
	file_name_criterion = nautilus_search_bar_criterion_first_new ();
	nautilus_search_bar_criterion_set_callback (file_name_criterion, 
						    criterion_callback,
						    (gpointer) bar);

	bar->details->search_criteria = g_slist_append (NULL,
							file_name_criterion);
	attach_criterion_to_search_bar (bar, file_name_criterion, 1);
	nautilus_search_bar_criterion_show (file_name_criterion);

	gtk_box_pack_start (GTK_BOX (bar->details->bar_container),
			    GTK_WIDGET (bar->details->table),
			    TRUE,
			    FALSE,
			    0);
	gtk_widget_show (GTK_WIDGET (bar->details->table));

	hbox = gtk_hbox_new (FALSE, 1);


	bar->details->find_them = gtk_button_new ();
	find_them_box = gtk_hbox_new (FALSE, 1);
	find_them_pixmap_widget = load_find_them_pixmap_widget ();
	gtk_box_pack_start (GTK_BOX (find_them_box),
			    find_them_pixmap_widget,
			    TRUE,
			    FALSE,
			    1);
	find_them_label = gtk_label_new ("Find Them!");
	gtk_box_pack_start (GTK_BOX (find_them_box),
			    find_them_label,
			    TRUE,
			    FALSE,
			    1);

	gtk_container_add (GTK_CONTAINER (bar->details->find_them), find_them_box);
	gtk_signal_connect_object (GTK_OBJECT (bar->details->find_them), "pressed",
				   nautilus_navigation_bar_location_changed,
				   GTK_OBJECT (bar));

	gtk_box_pack_end (GTK_BOX (hbox), 
			  bar->details->find_them, FALSE, FALSE, 0);
	gtk_widget_show (bar->details->find_them);


	
	bar->details->fewer_options = gtk_button_new_with_label ("Fewer Options");
	gtk_signal_connect (GTK_OBJECT (bar->details->fewer_options), "clicked",
			    fewer_options_callback, bar);

	gtk_box_pack_end (GTK_BOX (hbox),
			    bar->details->fewer_options,
			    FALSE, FALSE, 2 * GNOME_PAD_SMALL);

	gtk_widget_show (bar->details->fewer_options);

	bar->details->more_options = gtk_button_new_with_label ("More Options");
	gtk_signal_connect (GTK_OBJECT (bar->details->more_options), "clicked",
			    more_options_callback, bar);
				
				
	gtk_box_pack_end (GTK_BOX (hbox),
			     bar->details->more_options,
			     FALSE, FALSE, 0);
	gtk_widget_show (bar->details->more_options);

	gtk_box_pack_start (GTK_BOX (bar->details->bar_container),
			    hbox,
			    TRUE,
			    FALSE,
			    0);
	gtk_widget_show (hbox);
	gtk_widget_show (GTK_WIDGET (bar->details->bar_container));
	gtk_container_add (GTK_CONTAINER (bar), GTK_WIDGET (bar->details->bar_container));
}

/* returned string should be g_freed by the caller */
static char *
nautilus_complex_search_bar_get_location (NautilusNavigationBar *navigation_bar)
{
	NautilusComplexSearchBar *bar;
	char *criteria_text, *trimmed_fragment, *escaped_fragment;
	char *search_uri;
	GSList *list;

	bar = NAUTILUS_COMPLEX_SEARCH_BAR (navigation_bar);

	criteria_text = g_strdup ("[file:///]");

	for (list = bar->details->search_criteria; list != NULL; list = list->next) {
		char *temp_criterion, *criterion_text;
		NautilusSearchBarCriterion *criterion;
		criterion = (NautilusSearchBarCriterion *) list->data;
		criterion_text = nautilus_search_bar_criterion_get_location (criterion);
		temp_criterion = g_strconcat (criteria_text, criterion_text, " & ", NULL);
		g_free (criteria_text);
		g_free (criterion_text);
		criteria_text = temp_criterion;
	}
	trimmed_fragment = nautilus_str_strip_trailing_str (criteria_text, " & ");
	g_free (criteria_text);

	escaped_fragment = gnome_vfs_escape_string (trimmed_fragment);
	search_uri = g_strconcat ("search:", escaped_fragment, NULL);
	g_free (escaped_fragment);
	return search_uri;
}

static void                       
nautilus_complex_search_bar_set_location (NautilusNavigationBar *navigation_bar,
					  const char *location)
{
	NautilusComplexSearchBar *bar;

	bar = NAUTILUS_COMPLEX_SEARCH_BAR (navigation_bar);

	/* FIXME: Not implemented. */
}

static void
attach_criterion_to_search_bar (NautilusComplexSearchBar *bar,
				NautilusSearchBarCriterion *criterion,
				int row)
{

	g_return_if_fail (NAUTILUS_IS_COMPLEX_SEARCH_BAR (bar));

	gtk_table_attach_defaults (bar->details->table,
				   GTK_WIDGET (criterion->details->available_criteria),
				   0, 1, row - 1, row);
	
	gtk_table_attach_defaults  (bar->details->table,
				    GTK_WIDGET (criterion->details->relation_menu),
				    1, 2, row - 1, row);
	g_assert (criterion->details->use_value_entry + 
		  criterion->details->use_value_menu == 1);
	if (criterion->details->use_value_entry) {
		gtk_table_attach_defaults (bar->details->table,
					   GTK_WIDGET (criterion->details->value_entry),
					   2, 3, row - 1, row);
	}
	if (criterion->details->use_value_menu) {
		gtk_table_attach_defaults (bar->details->table,
					   GTK_WIDGET (criterion->details->value_menu),
					   2, 3, row - 1, row);
	}
	gtk_table_resize (bar->details->table, row, 3);

}

static void
unattach_criterion_from_search_bar (NautilusComplexSearchBar *bar,
				    NautilusSearchBarCriterion *criterion)
{
	gtk_container_remove (GTK_CONTAINER (bar->details->table),
			      GTK_WIDGET (criterion->details->available_criteria));

	gtk_container_remove (GTK_CONTAINER (bar->details->table),
			      GTK_WIDGET (criterion->details->relation_menu));
	g_assert (criterion->details->use_value_entry + 
		  criterion->details->use_value_menu == 1);
	if (criterion->details->use_value_entry) {
		gtk_container_remove (GTK_CONTAINER (bar->details->table),
				      GTK_WIDGET (criterion->details->value_entry));
	}
	if (criterion->details->use_value_menu) {
		gtk_container_remove (GTK_CONTAINER (bar->details->table),
				      GTK_WIDGET (criterion->details->value_menu));
	}
	gtk_table_resize (bar->details->table, 
			  g_slist_length (bar->details->search_criteria) - 1, 
			  3);

}

static GtkWidget *
load_find_them_pixmap_widget (void)
{
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	GdkBitmap *mask;
	
	pixbuf = gdk_pixbuf_new_from_file ("/gnome/share/pixmaps/nautilus/search.png");
	gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &mask, 128);
	gdk_pixbuf_unref (pixbuf);

	return gtk_pixmap_new (pixmap, mask);
}
				  

GtkWidget *
nautilus_complex_search_bar_new (void)
{
	return gtk_widget_new (NAUTILUS_TYPE_COMPLEX_SEARCH_BAR, NULL);
}


static void                       
more_options_callback (GtkObject *object,
		       gpointer data)
{
	NautilusSearchBarCriterion *criterion, *last_criterion;
	NautilusComplexSearchBar *bar;
	GSList *list;
  
	g_return_if_fail (GTK_IS_BUTTON (object));
	g_return_if_fail (NAUTILUS_IS_COMPLEX_SEARCH_BAR (data));

	bar = NAUTILUS_COMPLEX_SEARCH_BAR (data);

	list = bar->details->search_criteria;
	last_criterion = (NautilusSearchBarCriterion *)((g_slist_last (list))->data);
	criterion = nautilus_search_bar_criterion_next_new (last_criterion->details->type);
	nautilus_search_bar_criterion_set_callback (criterion, 
						    criterion_callback, 
						    (gpointer) bar);
	bar->details->search_criteria = g_slist_append (list, criterion);

	attach_criterion_to_search_bar (bar, criterion, 
					g_slist_length (bar->details->search_criteria));
	nautilus_search_bar_criterion_show (criterion);

}


static void                       
fewer_options_callback (GtkObject *object,
		       gpointer data)
{
	NautilusSearchBarCriterion *criterion;
	NautilusComplexSearchBar *bar;
	GSList *last;
	int old_length, new_length;

	g_return_if_fail (GTK_IS_BUTTON (object));
	g_return_if_fail (NAUTILUS_IS_COMPLEX_SEARCH_BAR (data));

	bar = NAUTILUS_COMPLEX_SEARCH_BAR (data);

	old_length = g_slist_length (bar->details->search_criteria);
	if (old_length == 1) {
		return;
	}

	last = g_slist_last (bar->details->search_criteria);
	criterion = (NautilusSearchBarCriterion *) last->data;
	unattach_criterion_from_search_bar (bar, criterion);
	bar->details->search_criteria = g_slist_remove (bar->details->search_criteria, 
							criterion);

	/* Assert that the old criteria got removed from the criteria list */
	new_length = g_slist_length (bar->details->search_criteria);
	g_assert (new_length + 1 == old_length);

	/* FIXME: the folowing is pretty much evil since it relies on the run-time
	   widget hierarchy of the nautilus toolbar. 
	   Any better fix is wellcome.
	*/
	gtk_widget_queue_resize (GTK_WIDGET (bar->details->bar_container)->parent->parent->parent->parent->parent->parent->parent->parent);
}