2 * This file is part of GtkHotkey.
3 * Copyright Mikkel Kamstrup Erlandsen, March, 2008
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.
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.
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/>.
19 #include "gtk-hotkey-registry.h"
20 #include "gtk-hotkey-key-file-registry.h"
23 GTK_HOTKEY_REGISTRY_DUMMY_PROPERTY
33 static gpointer gtk_hotkey_registry_parent_class = NULL;
35 static GType default_registry_type = G_TYPE_INVALID;
37 static GtkHotkeyRegistry
38 *default_registry = NULL;
40 #define DEFAULT_REGISTRY_TYPE GTK_HOTKEY_TYPE_KEY_FILE_REGISTRY
42 guint storage_signals[LAST_SIGNAL] = { 0 };
45 * SECTION:gtk-hotkey-registry
46 * @short_description: Abstract base class for services storing and loading hotkeys
47 * @see_also: #GtkHotkeyKeyFileRegistry
49 * #GtkHotkeyRegistry is an abstract base class for implementing a platform
50 * specific service for storing and loading hotkey configurations.
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
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.
61 * To obtain a #GtkHotkeyRegistry matching your desktop environment use
62 * the factory method gtk_hotkey_registry_get_default().
67 * gtk_hotkey_registry_get_default
68 * @returns: A reference to a #GtkHotkeyRegistry matching your platform
70 * Currently the only implementation of this class is #GtkHotkeyKeyFileRegistry.
73 gtk_hotkey_registry_get_default (void)
75 if (G_UNLIKELY(default_registry == NULL)) {
77 /* Set the default type of registry to create */
78 if (default_registry_type == G_TYPE_INVALID)
79 default_registry_type = DEFAULT_REGISTRY_TYPE;
81 default_registry = GTK_HOTKEY_REGISTRY (g_object_new (GTK_HOTKEY_TYPE_KEY_FILE_REGISTRY,
83 g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(default_registry), NULL);
84 /* We always keep a ref to the registry here */
86 return g_object_ref(default_registry);
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
101 * Look up a hotkey given its id and application id.
104 gtk_hotkey_registry_get_hotkey (GtkHotkeyRegistry *self,
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,
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
123 * Look up all hotkeys registered by a given application.
126 gtk_hotkey_registry_get_application_hotkeys (GtkHotkeyRegistry *self,
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);
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().
141 * Look up all hotkeys registered by a given application.
144 gtk_hotkey_registry_get_all_hotkeys (GtkHotkeyRegistry *self)
146 g_return_val_if_fail (GTK_HOTKEY_IS_REGISTRY(self), NULL);
147 return GTK_HOTKEY_REGISTRY_GET_CLASS (self)->get_all_hotkeys (self);
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.
158 * Store a hotkey in the registry for later usage. In case of success the
159 * #GtkHotkeyRegistry::hotkey-stored signal will be emitted.
162 gtk_hotkey_registry_store_hotkey (GtkHotkeyRegistry *self,
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);
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
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.
180 * Delete a hotkey from the registry. In case of success the
181 * #GtkHotkeyRegistry::hotkey-deleted signal will be emitted.
184 gtk_hotkey_registry_delete_hotkey (GtkHotkeyRegistry *self,
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,
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
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.
203 * Look up all hotkeys registered by a given application.
206 gtk_hotkey_registry_has_hotkey (GtkHotkeyRegistry *self,
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);
215 * gtk_hotkey_registry_hotkey_stored
216 * @self: The #GtkHotkeyRegistry to emit the signal on
217 * @info: The #GtkHotkeyInfo that was stored
219 * Emit the #GtkHotkeyRegistry::hotkey-stored signal on @self. This method should
220 * only be used by child classes of #GtkHotkeyRegistry.
223 gtk_hotkey_registry_hotkey_stored (GtkHotkeyRegistry *self,
226 g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
227 g_return_if_fail (GTK_HOTKEY_IS_INFO(info));
229 GTK_HOTKEY_REGISTRY_GET_CLASS (self)->hotkey_stored (self, info);
233 * gtk_hotkey_registry_hotkey_deleted
234 * @self: The #GtkHotkeyRegistry to emit the signal on
235 * @info: The #GtkHotkeyInfo that was deleted
237 * Emit the #GtkHotkeyRegistry::hotkey-deleted signal on @self. This method should
238 * only be used by child classes of #GtkHotkeyRegistry.
241 gtk_hotkey_registry_hotkey_deleted (GtkHotkeyRegistry *self,
244 g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
245 GTK_HOTKEY_REGISTRY_GET_CLASS (self)->hotkey_deleted (self, info);
249 gtk_hotkey_registry_hotkey_stored_real (GtkHotkeyRegistry *self,
252 g_return_if_fail (GTK_HOTKEY_IS_INFO(info));
253 g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
255 g_signal_emit (self, storage_signals[HOTKEY_STORED], 0, info);
259 gtk_hotkey_registry_hotkey_deleted_real (GtkHotkeyRegistry *self,
262 g_return_if_fail (GTK_HOTKEY_IS_INFO(info));
263 g_return_if_fail (GTK_HOTKEY_IS_REGISTRY(self));
265 g_signal_emit (self, storage_signals[HOTKEY_DELETED], 0, info);
269 gtk_hotkey_registry_class_init (GtkHotkeyRegistryClass *klass)
271 gtk_hotkey_registry_parent_class = g_type_class_peek_parent (klass);
273 klass->hotkey_stored = gtk_hotkey_registry_hotkey_stored_real;
274 klass->hotkey_deleted = gtk_hotkey_registry_hotkey_deleted_real;
277 * GtkHotkeyRegistry::hotkey-stored
278 * @hotkey:The hotkey that was stored
280 * Emitted when a hotkey has been stored in the registry
282 storage_signals[HOTKEY_STORED] = \
283 g_signal_new ("hotkey_stored",
284 GTK_HOTKEY_TYPE_STORAGE,
287 g_cclosure_marshal_VOID__OBJECT,
292 * GtkHotkeyRegistry::hotkey-deleted
293 * @hotkey:The hotkey that was deleted
295 * Emitted when a hotkey has been deleted from the registry
297 storage_signals[HOTKEY_DELETED] = \
298 g_signal_new ("hotkey_deleted",
299 GTK_HOTKEY_TYPE_STORAGE,
302 g_cclosure_marshal_VOID__OBJECT,
309 gtk_hotkey_registry_init (GtkHotkeyRegistry * self)
315 gtk_hotkey_registry_finalize (GtkHotkeyRegistry * self)
321 gtk_hotkey_registry_get_type (void)
323 static GType gtk_hotkey_registry_type_id = 0;
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,
333 sizeof (GtkHotkeyRegistry),
335 (GInstanceInitFunc) gtk_hotkey_registry_init
338 gtk_hotkey_registry_type_id = g_type_register_static (G_TYPE_OBJECT, "GtkHotkeyRegistry", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
340 return gtk_hotkey_registry_type_id;