HildonFindToolbar based implementation of ModestISearchToolbar
[modest] / src / hildon2 / modest-hildon-find-toolbar.c
1 /* Copyright (c) 2009, Igalia
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib/gi18n.h>
31 #include "modest-hildon-find-toolbar.h"
32
33 typedef struct _ModestHildonFindToolbarPrivate ModestHildonFindToolbarPrivate;
34 struct _ModestHildonFindToolbarPrivate {
35         gchar *current_prefix;
36 };
37
38
39 static void modest_hildon_find_toolbar_class_init (ModestHildonFindToolbarClass *klass);
40 static void modest_hildon_find_toolbar_init (ModestHildonFindToolbar *self);
41 static void modest_isearch_toolbar_iface_init (gpointer g, gpointer iface_data);
42
43 /* GObject interface */
44 static void modest_hildon_find_toolbar_finalize (GObject *obj);
45
46 /* ModestISearchToolbar interface */
47 static void modest_hildon_find_toolbar_highlight_entry (ModestISearchToolbar *self, gboolean focus);
48 static void modest_hildon_find_toolbar_highlight_entry_default (ModestISearchToolbar *self, gboolean focus);
49 static void modest_hildon_find_toolbar_set_label (ModestISearchToolbar *self, const gchar *label);
50 static void modest_hildon_find_toolbar_set_label_default (ModestISearchToolbar *self, const gchar *label);
51 static const gchar *modest_hildon_find_toolbar_get_search (ModestISearchToolbar *self);
52 static const gchar *modest_hildon_find_toolbar_get_search_default (ModestISearchToolbar *self);
53
54 /* signals */
55 static void on_find_toolbar_close (HildonFindToolbar *self, gpointer userdata);
56 static void on_find_toolbar_search (HildonFindToolbar *self, gpointer userdata);
57
58 #define MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59                                                                                     MODEST_TYPE_HILDON_FIND_TOOLBAR, \
60                                                                                     ModestHildonFindToolbarPrivate))
61 /* globals */
62 static HildonFindToolbarClass *parent_class = NULL;
63
64 G_DEFINE_TYPE_EXTENDED    (ModestHildonFindToolbar,
65                            modest_hildon_find_toolbar,
66                            HILDON_TYPE_FIND_TOOLBAR,
67                            0,
68                            {
69                              G_IMPLEMENT_INTERFACE (MODEST_TYPE_ISEARCH_TOOLBAR, modest_isearch_toolbar_iface_init);
70                              g_type_interface_add_prerequisite (g_define_type_id, GTK_TYPE_TOOLBAR);
71                            }
72                            );
73
74 GtkWidget *
75 modest_hildon_find_toolbar_new                            (const gchar *label)
76 {
77         GtkWidget *toolbar = g_object_new (MODEST_TYPE_HILDON_FIND_TOOLBAR, NULL);
78         modest_isearch_toolbar_set_label (MODEST_ISEARCH_TOOLBAR (toolbar), label);
79
80         return toolbar;
81 }
82
83 static void
84 modest_hildon_find_toolbar_class_init (ModestHildonFindToolbarClass *klass)
85 {
86         GObjectClass *gobject_class;
87
88         parent_class = g_type_class_peek_parent (klass);
89         gobject_class->finalize = modest_hildon_find_toolbar_finalize;
90
91         klass->highlight_entry_func = modest_hildon_find_toolbar_highlight_entry_default;
92         klass->set_label_func = modest_hildon_find_toolbar_set_label_default;
93         klass->get_search_func = modest_hildon_find_toolbar_get_search_default;
94
95         g_type_class_add_private (gobject_class, sizeof(ModestHildonFindToolbarPrivate));
96 }
97
98 static void
99 modest_hildon_find_toolbar_init (ModestHildonFindToolbar *self)
100 {
101         ModestHildonFindToolbarPrivate *priv;
102
103         priv = MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
104         priv->current_prefix = NULL;
105         g_signal_connect (G_OBJECT (self), "close", G_CALLBACK (on_find_toolbar_close), self);
106         g_signal_connect (G_OBJECT (self), "search", G_CALLBACK (on_find_toolbar_search), self);
107 }
108
109 static void
110 modest_isearch_toolbar_iface_init (gpointer g, gpointer iface_data)
111 {
112         ModestISearchToolbarIface *iface = (ModestISearchToolbarIface *) g;
113
114         iface->highlight_entry = modest_hildon_find_toolbar_highlight_entry;
115         iface->set_label = modest_hildon_find_toolbar_set_label;
116         iface->get_search = modest_hildon_find_toolbar_get_search;
117 }
118
119 static void 
120 modest_hildon_find_toolbar_finalize (GObject *obj)
121 {
122         ModestHildonFindToolbarPrivate *priv;
123
124         priv = MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE (obj);
125         g_free (priv->current_prefix);
126         G_OBJECT_CLASS(parent_class)->finalize (obj);           
127 }
128
129 static void
130 modest_hildon_find_toolbar_highlight_entry (ModestISearchToolbar *self,
131                                             gboolean get_focus)
132 {
133         MODEST_HILDON_FIND_TOOLBAR_GET_CLASS (self)->highlight_entry_func (self, get_focus);
134 }
135
136 static void
137 modest_hildon_find_toolbar_highlight_entry_default (ModestISearchToolbar *self,
138                                                     gboolean get_focus)
139 {
140         hildon_find_toolbar_highlight_entry (HILDON_FIND_TOOLBAR (self), get_focus);
141 }
142
143 static void
144 modest_hildon_find_toolbar_set_label (ModestISearchToolbar *self,
145                                       const gchar *label)
146 {
147         MODEST_HILDON_FIND_TOOLBAR_GET_CLASS (self)->set_label_func (self, label);
148 }
149
150 static void
151 modest_hildon_find_toolbar_set_label_default (ModestISearchToolbar *self,
152                                               const gchar *label)
153 {
154         g_object_set (G_OBJECT (self), "label", label, NULL);
155 }
156
157 static const gchar *
158 modest_hildon_find_toolbar_get_search (ModestISearchToolbar *self)
159 {
160         return MODEST_HILDON_FIND_TOOLBAR_GET_CLASS (self)->get_search_func (self);
161 }
162
163 static const gchar *
164 modest_hildon_find_toolbar_get_search_default (ModestISearchToolbar *self)
165 {
166         ModestHildonFindToolbarPrivate *priv;
167
168         priv = MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
169
170         if (priv->current_prefix) {
171                 g_free (priv->current_prefix);
172                 priv->current_prefix = NULL;
173         }
174
175         g_object_get (G_OBJECT (self), "prefix", &(priv->current_prefix), NULL);
176
177         return priv->current_prefix;
178 }
179
180 static void 
181 on_find_toolbar_close (HildonFindToolbar *self, gpointer userdata)
182 {
183         g_signal_emit_by_name (G_OBJECT (self), "isearch-close");
184 }
185
186 static void
187 on_find_toolbar_search (HildonFindToolbar *self, gpointer userdata)
188 {
189         g_signal_emit_by_name (G_OBJECT (self), "isearch-search");
190 }