Fix frequently ocurring typos :)
[claws.git] / src / plugins / notification / gtkhotkey / gtk-hotkey-registry.c
1 /*
2  * This file is part of GtkHotkey.
3  * Copyright Mikkel Kamstrup Erlandsen, March, 2008
4  *
5  *   GtkHotkey is free software: you can redistribute it and/or modify
6  *   it under the terms of the GNU Lesser General Public License as published by
7  *   the Free Software Foundation, either version 3 of the License, or
8  *   (at your option) any later version.
9  *
10  *   GtkHotkey 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
13  *   GNU Lesser General Public License for more details.
14  *
15  *   You should have received a copy of the GNU Lesser General Public License
16  *   along with GtkHotkey.  If not, see <http://www.gnu.org/licenses/>.
17  */
18  
19 #include "gtk-hotkey-registry.h"
20 #include "gtk-hotkey-key-file-registry.h"
21
22 enum  {
23         GTK_HOTKEY_REGISTRY_DUMMY_PROPERTY
24 };
25
26 enum {
27         HOTKEY_STORED,
28         HOTKEY_DELETED,
29         
30         LAST_SIGNAL
31 };
32
33 static gpointer         gtk_hotkey_registry_parent_class = NULL;
34
35 static GType            default_registry_type = G_TYPE_INVALID;
36
37 static GtkHotkeyRegistry
38                                         *default_registry = NULL;
39
40 #define DEFAULT_REGISTRY_TYPE GTK_HOTKEY_TYPE_KEY_FILE_REGISTRY
41
42 guint                           storage_signals[LAST_SIGNAL] = { 0 };
43
44 /**
45  * SECTION:gtk-hotkey-registry
46  * @short_description: Abstract base class for services storing and loading hotkeys
47  * @see_also: #GtkHotkeyKeyFileRegistry
48  *
49  * #GtkHotkeyRegistry is an abstract base class for implementing a platform
50  * specific service for storing and loading hotkey configurations.
51  *
52  * The actual binding of the hotkey into the environment is done by a
53  * #GtkHotkeyListener. This class is only meant to do the management part of the
54  * hotkey handling.
55  *
56  * The reasong why applications should use a #GtkHotkeyRegistry and not just a
57  * flat text file with the hotkey signature is to make sure we don't overwrite
58  * or interfere with the hotkeys of other applications. And possibly providing
59  * a unified user interface for managing the hotkeys of all applications.
60  *
61  * To obtain a #GtkHotkeyRegistry matching your desktop environment use
62  * the factory method gtk_hotkey_registry_get_default().
63  *
64  **/    
65
66 /**
67  * gtk_hotkey_registry_get_default
68  * @returns: A reference to a #GtkHotkeyRegistry matching your platform
69  *
70  * Currently the only implementation of this class is #GtkHotkeyKeyFileRegistry.
71  */
72 GtkHotkeyRegistry*
73 gtk_hotkey_registry_get_default (void)
74 {       
75         if (G_UNLIKELY(default_registry == NULL)) {
76                 
77                 /* Set the default type of registry to create */
78                 if (default_registry_type == G_TYPE_INVALID)
79                         default_registry_type = DEFAULT_REGISTRY_TYPE;
80                 
81                 default_registry = GTK_HOTKEY_REGISTRY (g_object_new (GTK_HOTKEY_TYPE_KEY_FILE_REGISTRY,
82                                                                                                                         NULL));
83                 g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(default_registry), NULL);
84                 /* We always keep a ref to the registry here */
85         }
86         return g_object_ref(default_registry);
87 }
88
89 /**
90  * gtk_hotkey_registry_get_hotkey
91  * @self: The registry to search in
92  * @app_id: The name under which the application has registered it self
93  *          when it created the #GtkHotkeyInfo in the first place.
94  * @key_id: The id assignedd to the actual hotkey on the moment of its creation
95  * @error: Place to store a #GError in case of errors, or %NULL to ignore
96  * @returns: The #GtkHotkeyInfo for the requested parameters or %NULL is none
97  *           where found. In the case %NULL is returned @error will be set
98  *           accordingly. Free the hotkey with g_object_unref() when you are done
99  *           using it.
100  *
101  * Look up a hotkey given its id and application id.
102  */
103 GtkHotkeyInfo*
104 gtk_hotkey_registry_get_hotkey (GtkHotkeyRegistry        *self,
105                                                            const char           *app_id,
106                                                            const char           *key_id,
107                                                            GError                       **error)
108 {
109         g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), NULL);
110         return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->get_hotkey (self, app_id, key_id,
111                                                                                                                         error);
112 }
113
114 /**
115  * gtk_hotkey_registry_get_application_hotkeys
116  * @self: The #GtkHotkeyRegistry to look hotkeys up in
117  * @app_id: Unique application id
118  * @error: Place to return a #GError or %NULL
119  * @returns: A list of #GtkHotkeyInfo objects. The list should be with freed with
120  *           g_list_free() and the hotkey objects should be freed with
121  *           g_object_unref().
122  * 
123  * Look up all hotkeys registered by a given application.
124  */
125 GList*
126 gtk_hotkey_registry_get_application_hotkeys (GtkHotkeyRegistry  *self,
127                                                                                         const char                      *app_id,
128                                                                                         GError                          **error)
129 {
130         g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), NULL);
131         return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->get_application_hotkeys (self, app_id, error);
132 }
133
134 /**
135  * gtk_hotkey_registry_get_all_hotkeys
136  * @self: The #GtkHotkeyRegistry to look hotkeys up in
137  * @returns: A list of all valid #GtkHotkeyInfo<!-- -->s stored in the registry. 
138  *           The list should be with freed with g_list_free() and the hotkey 
139  *           objects should be freed with g_object_unref().
140  * 
141  * Look up all hotkeys registered by a given application.
142  */
143 GList*
144 gtk_hotkey_registry_get_all_hotkeys (GtkHotkeyRegistry  *self)
145 {
146         g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), NULL);
147         return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->get_all_hotkeys (self);
148 }
149
150 /**
151  * gtk_hotkey_registry_store_hotkey
152  * @self: The #GtkHotkeyRegistry in which to store the hotkey
153  * @info: The #GtkHotkeyInfo to store
154  * @error: Place to return a #GError or %NULL to ignore
155  * @returns: %TRUE on success and %FALS otherwise. In case of errors @error
156  *           will be set accordingly.
157  *           
158  * Store a hotkey in the registry for later usage. In case of success the
159  * #GtkHotkeyRegistry::hotkey-stored signal will be emitted.
160  */
161 gboolean
162 gtk_hotkey_registry_store_hotkey (GtkHotkeyRegistry   *self,
163                                                                  GtkHotkeyInfo          *info,
164                                                                  GError                         **error)
165 {
166         g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), FALSE);
167         return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->store_hotkey (self, info, error);
168 }
169
170 /**
171  * gtk_hotkey_registry_delete_hotkey
172  * @self: The #GtkHotkeyRegistry from which to delete the hotkey
173  * @app_id: The value of the #GtkHotkeyInfo:application-id property of the stored
174  *          hotkey
175  * @key_id: The value of the #GtkHotkeyInfo:key-id property of the stored hotkey
176  * @error: Place to return a #GError or %NULL to ignore
177  * @returns: %TRUE on success and %FALS otherwise. In case of errors @error
178  *           will be set accordingly.
179  *           
180  * Delete a hotkey from the registry. In case of success the
181  * #GtkHotkeyRegistry::hotkey-deleted signal will be emitted.
182  */
183 gboolean
184 gtk_hotkey_registry_delete_hotkey (GtkHotkeyRegistry    *self,
185                                                                   const gchar           *app_id,
186                                                                   const gchar           *key_id,
187                                                                   GError                        **error)
188 {
189         g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), FALSE);
190         return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->delete_hotkey (self, app_id,
191                                                                                                                            key_id, error);
192 }
193
194 /**
195  * gtk_hotkey_registry_has_hotkey
196  * @self: The #GtkHotkeyRegistry to look hotkeys up in
197  * @app_id: The value of the #GtkHotkeyInfo:application-id property of the stored
198  *          hotkey
199  * @key_id: The value of the #GtkHotkeyInfo:key-id property of the stored hotkey
200  * @returns: %TRUE if the registry has stored a hotkey with with application id
201  *           @app_id and hotkey id @key_id.
202  * 
203  * Look up all hotkeys registered by a given application.
204  */
205 gboolean
206 gtk_hotkey_registry_has_hotkey (GtkHotkeyRegistry               *self,
207                                                            const gchar                  *app_id,
208                                                            const gchar                  *key_id)
209 {
210         g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), FALSE);
211         return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->has_hotkey (self, app_id, key_id);
212 }
213
214 /**
215  * gtk_hotkey_registry_hotkey_stored
216  * @self: The #GtkHotkeyRegistry to emit the signal on
217  * @info: The #GtkHotkeyInfo that was stored
218  * 
219  * Emit the #GtkHotkeyRegistry::hotkey-stored signal on @self. This method should
220  * only be used by child classes of #GtkHotkeyRegistry.
221  */
222 void
223 gtk_hotkey_registry_hotkey_stored (GtkHotkeyRegistry    *self,
224                                                                  GtkHotkeyInfo          *info)
225 {
226         g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
227         g_return_if_fail (GTK_HOTKEY_IS_INFO(info));
228         
229         GTK_HOTKEY_REGISTRY_GET_CLASS (self)->hotkey_stored (self, info);
230 }
231
232 /**
233  * gtk_hotkey_registry_hotkey_deleted
234  * @self: The #GtkHotkeyRegistry to emit the signal on
235  * @info: The #GtkHotkeyInfo that was deleted
236  * 
237  * Emit the #GtkHotkeyRegistry::hotkey-deleted signal on @self. This method should
238  * only be used by child classes of #GtkHotkeyRegistry.
239  */
240 void
241 gtk_hotkey_registry_hotkey_deleted (GtkHotkeyRegistry           *self,
242                                                                    GtkHotkeyInfo                *info)
243 {
244         g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
245         GTK_HOTKEY_REGISTRY_GET_CLASS (self)->hotkey_deleted (self, info);
246 }
247
248 static void
249 gtk_hotkey_registry_hotkey_stored_real (GtkHotkeyRegistry       *self,
250                                                                            GtkHotkeyInfo        *info)
251 {
252         g_return_if_fail (GTK_HOTKEY_IS_INFO(info));
253         g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
254         
255         g_signal_emit (self, storage_signals[HOTKEY_STORED], 0, info);
256 }
257
258 static void
259 gtk_hotkey_registry_hotkey_deleted_real (GtkHotkeyRegistry      *self,
260                                                                                 GtkHotkeyInfo           *info)
261 {
262         g_return_if_fail (GTK_HOTKEY_IS_INFO(info));
263         g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
264         
265         g_signal_emit (self, storage_signals[HOTKEY_DELETED], 0, info);
266 }
267
268 static void
269 gtk_hotkey_registry_class_init (GtkHotkeyRegistryClass *klass)
270 {
271         gtk_hotkey_registry_parent_class = g_type_class_peek_parent (klass);
272         
273         klass->hotkey_stored = gtk_hotkey_registry_hotkey_stored_real;
274         klass->hotkey_deleted = gtk_hotkey_registry_hotkey_deleted_real;
275         
276         /**
277          * GtkHotkeyRegistry::hotkey-stored
278          * @hotkey:The hotkey that was stored
279          *
280          * Emitted when a hotkey has been stored in the registry
281          */
282         storage_signals[HOTKEY_STORED] = \
283         g_signal_new ("hotkey_stored",
284                                   GTK_HOTKEY_TYPE_STORAGE,
285                                   G_SIGNAL_RUN_LAST,
286                                   0, NULL, NULL,
287                                   g_cclosure_marshal_VOID__OBJECT,
288                                   G_TYPE_NONE, 1,
289                                   G_TYPE_OBJECT);
290         
291         /**
292          * GtkHotkeyRegistry::hotkey-deleted
293          * @hotkey:The hotkey that was deleted
294          *
295          * Emitted when a hotkey has been deleted from the registry
296          */
297         storage_signals[HOTKEY_DELETED] = \
298         g_signal_new ("hotkey_deleted",
299                                   GTK_HOTKEY_TYPE_STORAGE,
300                                   G_SIGNAL_RUN_LAST,
301                                   0, NULL, NULL,
302                                   g_cclosure_marshal_VOID__OBJECT,
303                                   G_TYPE_NONE, 1,
304                                   G_TYPE_OBJECT);
305 }
306
307
308 static void
309 gtk_hotkey_registry_init (GtkHotkeyRegistry * self)
310 {
311         
312 }
313
314 static void
315 gtk_hotkey_registry_finalize (GtkHotkeyRegistry * self)
316 {
317         
318 }
319
320 GType
321 gtk_hotkey_registry_get_type (void)
322 {
323         static GType gtk_hotkey_registry_type_id = 0;
324         
325         if (G_UNLIKELY (gtk_hotkey_registry_type_id == 0)) {
326                 static const GTypeInfo g_define_type_info = {
327                         sizeof (GtkHotkeyRegistryClass),
328                         (GBaseInitFunc) gtk_hotkey_registry_init,
329                         (GBaseFinalizeFunc) gtk_hotkey_registry_finalize,
330                         (GClassInitFunc) gtk_hotkey_registry_class_init,
331                         (GClassFinalizeFunc) NULL,
332                         NULL,
333                         sizeof (GtkHotkeyRegistry),
334                         0,
335                         (GInstanceInitFunc) gtk_hotkey_registry_init 
336                 };
337                 
338                 gtk_hotkey_registry_type_id = g_type_register_static (G_TYPE_OBJECT, "GtkHotkeyRegistry", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
339         }
340         return gtk_hotkey_registry_type_id;
341 }