~meese/pantheon-photos/fix-uglyicons

« back to all changes in this revision

Viewing changes to src/searches/SavedSearchDialog.vala

  • Committer: Eric Gregory
  • Date: 2011-05-03 01:36:07 UTC
  • Revision ID: git-v1:661313b1a07d26b3784fab6fa241a737cd321580
3511 search editing and 3558 search criteria removed when search deleted

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
        
19
19
        private SearchRow? my_row = null;
20
20
        
21
 
        public SearchRowContainer(SearchCondition.SearchType starting_type = 0) {
 
21
        public SearchRowContainer() {
 
22
            setup_gui();
 
23
            set_type(SearchCondition.SearchType.ANY_TEXT);
 
24
        }
 
25
        
 
26
        public SearchRowContainer.edit_existing(SearchCondition sc) {
 
27
            setup_gui();
 
28
            set_type(sc.search_type);
 
29
            type_combo.set_active(sc.search_type);
 
30
            my_row.populate(sc);
 
31
        }
 
32
        
 
33
        // Creates the GUI for this row.
 
34
        private void setup_gui() {
22
35
            // Ordering must correspond with SearchCondition.SearchType
23
36
            type_combo = new Gtk.ComboBox.text();
24
37
            type_combo.append_text(_("Any text"));
29
42
            type_combo.append_text(_("Media type"));
30
43
            type_combo.append_text(_("Flag state"));
31
44
            type_combo.append_text(_("Rating"));
32
 
            type_combo.set_active(starting_type); // Sets default.
 
45
            type_combo.set_active(0); // Sets default.
33
46
            type_combo.changed.connect(on_type_changed);
34
47
            
35
48
            remove_button = new Gtk.Button();
36
 
            remove_button.set_label(" - ");
 
49
            remove_button.set_label(" – ");
37
50
            remove_button.button_press_event.connect(on_removed);
38
51
            
39
52
            align = new Gtk.Alignment(0,0,0,0);
44
57
            box.pack_start(new Gtk.Alignment(0,0,0,0), true, true, 0); // Fill space.
45
58
            box.pack_start(remove_button, false, false, 0);
46
59
            box.show_all();
47
 
            
48
 
            set_type(SearchCondition.SearchType.ANY_TEXT);
49
60
        }
50
61
        
51
62
        private void on_type_changed() {
109
120
    
110
121
    // Represents a row-type.
111
122
    private abstract class SearchRow {
 
123
        // Returns the GUI widget for this row. 
112
124
        public abstract Gtk.Widget get_widget();
 
125
        
 
126
        // Returns the search condition for this row.
113
127
        public abstract SearchCondition get_search_condition();
 
128
        
 
129
        // Fills out the fields in this row based on an existing search condition (for edit mode.)
 
130
        public abstract void populate(SearchCondition sc);
114
131
    }
115
132
    
116
133
    private class SearchRowText : SearchRow {
154
171
            SearchConditionText c = new SearchConditionText(type, text, context);
155
172
            return c;
156
173
        }
 
174
        
 
175
        public override void populate(SearchCondition sc) {
 
176
            SearchConditionText? text = sc as SearchConditionText;
 
177
            assert(text != null);
 
178
            text_context.set_active(text.context);
 
179
            entry.set_text(text.text);
 
180
        }
157
181
    }
158
182
    
159
183
    private class SearchRowMediaType : SearchRow {
196
220
            SearchConditionMediaType c = new SearchConditionMediaType(search_type, context, type);
197
221
            return c;
198
222
        }
 
223
        
 
224
        public override void populate(SearchCondition sc) {
 
225
            SearchConditionMediaType? media = sc as SearchConditionMediaType;
 
226
            assert(media != null);
 
227
            media_context.set_active(media.context);
 
228
            media_type.set_active(media.media_type);
 
229
        }
199
230
    }
200
231
    
201
232
    private class SearchRowFlagged : SearchRow {
229
260
            SearchConditionFlagged c = new SearchConditionFlagged(search_type, state);
230
261
            return c;
231
262
        }
 
263
        
 
264
        public override void populate(SearchCondition sc) {
 
265
            SearchConditionFlagged? f = sc as SearchConditionFlagged;
 
266
            assert(f != null);
 
267
            flagged_state.set_active(f.state);
 
268
        }
232
269
    }
233
270
    
234
271
    private class SearchRowRating : SearchRow {
276
313
            SearchConditionRating c = new SearchConditionRating(search_type, search_rating, search_context);
277
314
            return c;
278
315
        }
 
316
        
 
317
        public override void populate(SearchCondition sc) {
 
318
            SearchConditionRating? r = sc as SearchConditionRating;
 
319
            assert(r != null);
 
320
            context.set_active(r.context);
 
321
            rating.set_active(r.rating - Rating.REJECTED);
 
322
        }
279
323
    }
280
324
    
281
325
    private Gtk.Builder builder;
285
329
    private Gtk.VBox row_box;
286
330
    private Gtk.Entry search_title;
287
331
    private Gee.ArrayList<SearchRowContainer> row_list = new Gee.ArrayList<SearchRowContainer>();
 
332
    private bool edit_mode = false;
 
333
    private SavedSearch? previous_search = null;
288
334
    
289
335
    public SavedSearchDialog() {
 
336
        setup_dialog();
 
337
        
 
338
        // Default is text search.
 
339
        add_text_search();
 
340
        row_list.get(0).allow_removal(false);
 
341
        
 
342
        // Add buttons for new search.
 
343
        Gtk.Button ok_button = new Gtk.Button.from_stock(Gtk.Stock.OK);
 
344
        ok_button.can_default = true;
 
345
        dialog.add_action_widget(ok_button, Gtk.ResponseType.OK);
 
346
        dialog.add_action_widget(new Gtk.Button.from_stock(Gtk.Stock.CANCEL), Gtk.ResponseType.CANCEL);
 
347
        dialog.set_default_response(Gtk.ResponseType.OK);
 
348
        
 
349
        dialog.show_all();
 
350
    }
 
351
    
 
352
    public SavedSearchDialog.edit_existing(SavedSearch saved_search) {
 
353
        previous_search = saved_search;
 
354
        edit_mode = true;
 
355
        setup_dialog();
 
356
        
 
357
        // Load existing search into dialog.
 
358
        operator.set_active((SearchOperator) saved_search.get_operator());
 
359
        search_title.set_text(saved_search.get_name());
 
360
        foreach (SearchCondition sc in saved_search.get_conditions()) {
 
361
            add_row(new SearchRowContainer.edit_existing(sc));
 
362
        }
 
363
        
 
364
        if (row_list.size == 1)
 
365
            row_list.get(0).allow_removal(false);
 
366
        
 
367
        // Add close button.
 
368
        Gtk.Button close_button = new Gtk.Button.from_stock(Gtk.Stock.CLOSE);
 
369
        close_button.can_default = true;
 
370
        dialog.add_action_widget(close_button, Gtk.ResponseType.OK);
 
371
        dialog.set_default_response(Gtk.ResponseType.OK);
 
372
        
 
373
        dialog.show_all();
 
374
    }
 
375
    
 
376
    // Builds the dialog UI.  Doesn't add buttons to the dialog or call dialog.show_all().
 
377
    private void setup_dialog() {
290
378
        builder = AppWindow.create_builder();
291
379
        
292
380
        dialog = builder.get_object("Search criteria") as Gtk.Dialog;
302
390
        
303
391
        row_box = builder.get_object("row_box") as Gtk.VBox;
304
392
        
305
 
        add_text_search();
306
 
        row_list.get(0).allow_removal(false);
307
 
        
308
393
        operator = builder.get_object("Type of search criteria") as Gtk.ComboBox;
309
394
        gtk_combo_box_set_as_text(operator);
310
395
        operator.append_text(_("any"));
311
396
        operator.append_text(_("all"));
312
397
        operator.append_text(_("none"));
313
398
        operator.set_active(0);
314
 
        
315
 
        // Add buttons for new search.
316
 
        Gtk.Button ok_button = new Gtk.Button.from_stock(Gtk.Stock.OK);
317
 
        ok_button.can_default = true;
318
 
        dialog.add_action_widget(ok_button, Gtk.ResponseType.OK);
319
 
        dialog.add_action_widget(new Gtk.Button.from_stock(Gtk.Stock.CANCEL), Gtk.ResponseType.CANCEL);
320
 
        dialog.set_default_response(Gtk.ResponseType.OK);
321
 
        
322
 
        dialog.show_all();
323
 
    }
324
 
    
325
 
    public SavedSearchDialog.edit_existing(SavedSearch saved_search) {
326
 
        // TODO
327
399
    }
328
400
    
329
401
    // Displays the dialog.
363
435
 
364
436
    private void on_response(int response_id) {
365
437
        if (response_id == Gtk.ResponseType.OK) {
366
 
            if (SavedSearchTable.get_instance().exists(search_title.get_text())) {
 
438
            if (SavedSearchTable.get_instance().exists(search_title.get_text()) && 
 
439
                !(edit_mode && previous_search.get_name() == search_title.get_text())) {
367
440
                AppWindow.error_message(Resources.rename_search_exists_message(search_title.get_text()));
368
441
                return;
369
442
            }
370
 
                
 
443
            
 
444
            if (edit_mode) {
 
445
                // Remove previous search.
 
446
                SavedSearchTable.get_instance().remove(previous_search);
 
447
            }
 
448
            
371
449
            // Build the condition list from the search rows, and add our new saved search to the table.
372
450
            Gee.ArrayList<SearchCondition> conditions = new Gee.ArrayList<SearchCondition>();
373
451
            foreach (SearchRowContainer c in row_list) {