Make live update of attributes in the combobox finally work properly :)
[mmediamanager.git] / libmmanager-gtk / mm-gtk-filter-builder.c
blob010f739b9df39d269c8fc55a35346761581a2101
1 /* MManager - a Desktop wide manager for multimedia applications.
3 * Copyright (C) 2008 Cosimo Cecchi <cosimoc@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "mm-gtk-filter-builder.h"
22 #include "mm-gtk-attribute-store.h"
23 #include "libmmanager/mm-attribute-manager.h"
24 #include "libmmanager/mm-attribute-base-manager.h"
26 #include <gtk/gtk.h>
27 #include <glib/gi18n.h>
29 G_DEFINE_TYPE (MMGtkFilterBuilder, mm_gtk_filter_builder, GTK_TYPE_VBOX);
31 #define MM_GTK_FILTER_BUILDER_GET_PRIVATE(o) \
32 (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_GTK_TYPE_FILTER_BUILDER, MMGtkFilterBuilderDetails))
34 struct _MMGtkFilterBuilderDetails {
35 GtkListStore *filter_store;
36 MMGtkAttributeStore *attribute_store;
37 GtkWidget *filter_view;
39 GHashTable *attr_managers;
41 GtkWidget *cat_combo;
42 GtkWidget *attr_combo;
43 GtkWidget *logic_combo;
44 GtkWidget *value_entry;
47 static const char * logic_strs [] = {
48 N_("equal"),
49 N_("greather than"),
50 N_("less than"),
51 N_("greater or equal than"),
52 N_("less or equal than"),
53 };
55 static void
56 mm_gtk_filter_builder_class_init (MMGtkFilterBuilderClass *klass)
58 g_type_class_add_private (klass, sizeof (MMGtkFilterBuilderDetails));
61 static void
62 populate_logic_combo (GtkWidget *combo)
64 int idx;
66 for (idx = 0; idx < G_N_ELEMENTS (logic_strs); idx++) {
67 gtk_combo_box_append_text (GTK_COMBO_BOX (combo), logic_strs[idx]);
70 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
73 static void
74 cat_combo_changed_cb (GtkComboBox *combo,
75 MMGtkFilterBuilder *self)
77 gint idx;
78 MMAttributeManager *attr_manager;
80 idx = gtk_combo_box_get_active (combo);
81 attr_manager = g_hash_table_lookup (self->details->attr_managers,
82 &idx);
83 mm_gtk_attribute_store_set_from_attribute_manager (self->details->attribute_store,
84 attr_manager);
85 gtk_combo_box_set_model (GTK_COMBO_BOX (self->details->attr_combo),
86 GTK_TREE_MODEL (self->details->attribute_store));
87 gtk_combo_box_set_active (GTK_COMBO_BOX (self->details->attr_combo), 0);
90 static void
91 mm_gtk_filter_builder_init (MMGtkFilterBuilder *self)
93 MMGtkFilterBuilderDetails *details = self->details =
94 MM_GTK_FILTER_BUILDER_GET_PRIVATE (self);
95 GtkWidget *filter_view;
96 GtkTreeViewColumn *column;
97 GtkCellRenderer *renderer;
98 GtkWidget *table;
99 GtkWidget *w;
101 details->attribute_store = mm_gtk_attribute_store_new ();
102 details->attr_managers = g_hash_table_new (g_int_hash, g_int_equal);
104 details->filter_store = gtk_list_store_new (3,
105 G_TYPE_STRING, /* attribute name */
106 G_TYPE_STRING, /* operator */
107 G_TYPE_STRING); /* value */
108 filter_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (details->filter_store));
109 renderer = gtk_cell_renderer_text_new ();
110 column = gtk_tree_view_column_new_with_attributes (_("Parameter"),
111 renderer,
112 "text", 0,
113 NULL);
114 gtk_tree_view_append_column (GTK_TREE_VIEW (filter_view), column);
115 column = gtk_tree_view_column_new_with_attributes (_("Logic"),
116 renderer,
117 "text", 1,
118 NULL);
119 gtk_tree_view_append_column (GTK_TREE_VIEW (filter_view), column);
120 column = gtk_tree_view_column_new_with_attributes (_("Value"),
121 renderer,
122 "text", 1,
123 NULL);
124 gtk_tree_view_append_column (GTK_TREE_VIEW (filter_view), column);
125 details->filter_view = filter_view;
126 gtk_box_pack_start (GTK_BOX (self), GTK_WIDGET (filter_view),
127 FALSE, FALSE, 12);
128 gtk_widget_show (filter_view);
130 table = gtk_table_new (3, 4, FALSE);
131 w = gtk_label_new_with_mnemonic (_("Attribute _category:"));
132 gtk_table_attach (GTK_TABLE (table), w,
133 0, 1, 0, 1,
134 0, 0, 0, 0);
135 details->cat_combo = gtk_combo_box_new_text ();
136 g_signal_connect (details->cat_combo, "changed",
137 G_CALLBACK (cat_combo_changed_cb), self);
138 gtk_table_attach (GTK_TABLE (table), details->cat_combo,
139 1, 2, 0, 1,
140 0, 0, 0, 0);
141 w = gtk_label_new_with_mnemonic (_("Attribute _name:"));
142 gtk_table_attach (GTK_TABLE (table), w,
143 2, 3, 0, 1,
144 0, 0, 0, 0);
145 details->attr_combo = gtk_combo_box_new ();
146 renderer = gtk_cell_renderer_text_new ();
147 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (details->attr_combo),
148 renderer, TRUE);
149 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (details->attr_combo),
150 renderer,
151 "text", MM_GTK_ATTR_STORE_NAME_COL);
152 gtk_table_attach (GTK_TABLE (table), details->attr_combo,
153 3, 4, 0, 1,
154 0, 0, 0, 0);
155 w = gtk_label_new_with_mnemonic (_("_Logic:"));
156 gtk_table_attach (GTK_TABLE (table), w,
157 0, 1, 1, 2,
158 0, 0, 0, 0);
159 details->logic_combo = gtk_combo_box_new_text ();
160 gtk_table_attach (GTK_TABLE (table), details->logic_combo,
161 1, 2, 1, 2,
162 0, 0, 0, 0);
163 w = gtk_label_new_with_mnemonic (_("_Value:"));
164 gtk_table_attach (GTK_TABLE (table), w,
165 2, 3, 1, 2,
166 0, 0, 0, 0);
167 details->value_entry = gtk_entry_new ();
168 gtk_table_attach (GTK_TABLE (table), details->value_entry,
169 3, 4, 1, 2,
170 0, 0, 0, 0);
171 w = gtk_button_new_from_stock (GTK_STOCK_ADD);
172 gtk_table_attach (GTK_TABLE (table), w,
173 3, 4, 2, 3,
174 0, 0, 0, 0);
175 gtk_box_pack_start (GTK_BOX (self), table,
176 FALSE, FALSE, 12);
177 gtk_widget_show_all (table);
179 populate_logic_combo (details->logic_combo);
182 static void
183 populate_from_category (MMGtkFilterBuilder *self, MMCategory *cat)
185 MMApplicationType type;
186 MMAttributeManager *attr_manager;
187 gint key = 0;
189 /* TODO: we should take care in some way that applications may want to define
190 * a custom AttributeManager. The API here in libmmanager-gtk is already
191 * setup for that, but this needs an API addition in MMApplication.
194 g_return_if_fail (MM_GTK_IS_FILTER_BUILDER (self));
195 g_return_if_fail (MM_IS_CATEGORY (cat));
197 /* TODO: when I will create Photo, Video and Music attribute managers,
198 * here is the place for adding those custom attributes.
202 attr_manager = mm_attribute_base_manager_get ();
203 mm_gtk_attribute_store_set_from_attribute_manager (self->details->attribute_store,
204 attr_manager);
205 gtk_combo_box_append_text (GTK_COMBO_BOX (self->details->cat_combo),
206 _("Base attributes"));
207 g_hash_table_insert (self->details->attr_managers, &key, attr_manager);
209 /* this should trigger population in the attr combo box */
210 gtk_combo_box_set_active (GTK_COMBO_BOX (self->details->cat_combo), 0);
213 /* public methods */
215 MMGtkFilterBuilder*
216 mm_gtk_filter_builder_new (MMCategory *category)
218 MMGtkFilterBuilder *self;
220 self = MM_GTK_FILTER_BUILDER (g_object_new (MM_GTK_TYPE_FILTER_BUILDER, NULL));
221 populate_from_category (self, category);
223 return self;