Use g_dir_open() and friends instead of opendir() and friends.
[claws.git] / src / plugins / notification / notification_hotkeys.c
1 /* Notification plugin for Claws-Mail
2  * Copyright (C) 2005-2009 Holger Berndt and the Claws Mail Team.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #  include "claws-features.h"
21 #endif
22
23 #ifdef NOTIFICATION_HOTKEYS
24
25 #include "notification_hotkeys.h"
26 #include "notification_prefs.h"
27 #include "notification_core.h"
28
29 #include "gtkhotkey.h"
30
31 #define HOTKEYS_APP_ID "claws-mail"
32 #define HOTKEY_KEY_ID_TOGGLED "toggle-mainwindow"
33
34 static GtkHotkeyInfo *hotkey_toggle_mainwindow = NULL;
35
36 static void hotkey_toggle_mainwindow_activated(GtkHotkeyInfo *hotkey, guint event_time, gpointer data)
37 {
38   g_return_if_fail(GTK_HOTKEY_IS_INFO(hotkey));
39   debug_print("Notification plugin: Toggled hide/show window due to hotkey %s activation\n", gtk_hotkey_info_get_signature(hotkey));
40   notification_toggle_hide_show_window();
41 }
42
43 static void unbind_toggle_mainwindow()
44 {
45   GError *error = NULL;
46   GtkHotkeyRegistry *registry;
47
48   /* clean up old hotkey */
49   if(hotkey_toggle_mainwindow) {
50     if(gtk_hotkey_info_is_bound(hotkey_toggle_mainwindow)) {
51       error = NULL;
52       gtk_hotkey_info_unbind(hotkey_toggle_mainwindow, &error);
53       if(error) {
54         debug_print("Notification plugin: Failed to unbind toggle hotkey\n");
55         g_error_free(error);
56         return;
57       }
58     }
59     g_object_unref(hotkey_toggle_mainwindow);
60     hotkey_toggle_mainwindow = NULL;
61   }
62   registry = gtk_hotkey_registry_get_default();
63   if(gtk_hotkey_registry_has_hotkey(registry, HOTKEYS_APP_ID, HOTKEY_KEY_ID_TOGGLED)) {
64     error = NULL;
65     gtk_hotkey_registry_delete_hotkey(registry, HOTKEYS_APP_ID, HOTKEY_KEY_ID_TOGGLED, &error);
66     if(error) {
67       debug_print("Notification plugin: Failed to unregister toggle hotkey: %s\n", error->message);
68       g_error_free(error);
69       return;
70     }
71   }
72 }
73
74 static void update_hotkey_binding_toggle_mainwindow()
75 {
76   GError *error = NULL;
77
78   /* don't do anything if no signature is given */
79   if(!notify_config.hotkeys_toggle_mainwindow || !strcmp(notify_config.hotkeys_toggle_mainwindow, ""))
80     return;
81
82   unbind_toggle_mainwindow();
83
84   /* (re)create hotkey info */
85   hotkey_toggle_mainwindow = gtk_hotkey_info_new(HOTKEYS_APP_ID, HOTKEY_KEY_ID_TOGGLED, notify_config.hotkeys_toggle_mainwindow, NULL);
86   if(!hotkey_toggle_mainwindow) {
87     debug_print("Notification plugin: Failed to create toggle hotkey for '%s'\n", notify_config.hotkeys_toggle_mainwindow);
88     return;
89   }
90
91   /* try to register hotkey */
92   error = NULL;
93   gtk_hotkey_info_bind(hotkey_toggle_mainwindow, &error);
94   if(error) {
95     debug_print("Notification plugin: Failed to bind toggle hotkey to '%s': %s\n", notify_config.hotkeys_toggle_mainwindow, error->message);
96     g_error_free(error);
97     return;
98   }
99
100   g_signal_connect(hotkey_toggle_mainwindow, "activated", G_CALLBACK(hotkey_toggle_mainwindow_activated), NULL);
101 }
102
103 void notification_hotkeys_update_bindings()
104 {
105   debug_print("Notification plugin: Updating keybindings..\n");
106   if(notify_config.hotkeys_enabled) {
107     update_hotkey_binding_toggle_mainwindow();
108   }
109   else
110     notification_hotkeys_unbind_all();
111 }
112
113 void notification_hotkeys_unbind_all()
114 {
115   debug_print("Notification plugin: Unbinding all keybindings..\n");
116   unbind_toggle_mainwindow();
117 }
118
119 #endif /* NOTIFICATION_HOTKEYS */