Fix some runtime errors with bad class initialization.
[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 = (GObjectClass *) klass;
90         gobject_class->finalize = modest_hildon_find_toolbar_finalize;
91
92         klass->highlight_entry_func = modest_hildon_find_toolbar_highlight_entry_default;
93         klass->set_label_func = modest_hildon_find_toolbar_set_label_default;
94         klass->get_search_func = modest_hildon_find_toolbar_get_search_default;
95
96         g_type_class_add_private (gobject_class, sizeof(ModestHildonFindToolbarPrivate));
97 }
98
99 static void
100 modest_hildon_find_toolbar_init (ModestHildonFindToolbar *self)
101 {
102         ModestHildonFindToolbarPrivate *priv;
103
104         priv = MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
105         priv->current_prefix = NULL;
106         g_signal_connect (G_OBJECT (self), "close", G_CALLBACK (on_find_toolbar_close), self);
107         g_signal_connect (G_OBJECT (self), "search", G_CALLBACK (on_find_toolbar_search), self);
108 }
109
110 static void
111 modest_isearch_toolbar_iface_init (gpointer g, gpointer iface_data)
112 {
113         ModestISearchToolbarIface *iface = (ModestISearchToolbarIface *) g;
114
115         iface->highlight_entry = modest_hildon_find_toolbar_highlight_entry;
116         iface->set_label = modest_hildon_find_toolbar_set_label;
117         iface->get_search = modest_hildon_find_toolbar_get_search;
118 }
119
120 static void 
121 modest_hildon_find_toolbar_finalize (GObject *obj)
122 {
123         ModestHildonFindToolbarPrivate *priv;
124
125         priv = MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE (obj);
126         g_free (priv->current_prefix);
127         G_OBJECT_CLASS(parent_class)->finalize (obj);           
128 }
129
130 static void
131 modest_hildon_find_toolbar_highlight_entry (ModestISearchToolbar *self,
132                                             gboolean get_focus)
133 {
134         MODEST_HILDON_FIND_TOOLBAR_GET_CLASS (self)->highlight_entry_func (self, get_focus);
135 }
136
137 static void
138 modest_hildon_find_toolbar_highlight_entry_default (ModestISearchToolbar *self,
139                                                     gboolean get_focus)
140 {
141         hildon_find_toolbar_highlight_entry (HILDON_FIND_TOOLBAR (self), get_focus);
142 }
143
144 static void
145 modest_hildon_find_toolbar_set_label (ModestISearchToolbar *self,
146                                       const gchar *label)
147 {
148         MODEST_HILDON_FIND_TOOLBAR_GET_CLASS (self)->set_label_func (self, label);
149 }
150
151 static void
152 modest_hildon_find_toolbar_set_label_default (ModestISearchToolbar *self,
153                                               const gchar *label)
154 {
155         g_object_set (G_OBJECT (self), "label", label, NULL);
156 }
157
158 static const gchar *
159 modest_hildon_find_toolbar_get_search (ModestISearchToolbar *self)
160 {
161         return MODEST_HILDON_FIND_TOOLBAR_GET_CLASS (self)->get_search_func (self);
162 }
163
164 static const gchar *
165 modest_hildon_find_toolbar_get_search_default (ModestISearchToolbar *self)
166 {
167         ModestHildonFindToolbarPrivate *priv;
168
169         priv = MODEST_HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
170
171         if (priv->current_prefix) {
172                 g_free (priv->current_prefix);
173                 priv->current_prefix = NULL;
174         }
175
176         g_object_get (G_OBJECT (self), "prefix", &(priv->current_prefix), NULL);
177
178         return priv->current_prefix;
179 }
180
181 static void 
182 on_find_toolbar_close (HildonFindToolbar *self, gpointer userdata)
183 {
184         g_signal_emit_by_name (G_OBJECT (self), "isearch-close");
185 }
186
187 static void
188 on_find_toolbar_search (HildonFindToolbar *self, gpointer userdata)
189 {
190         g_signal_emit_by_name (G_OBJECT (self), "isearch-search");
191 }