| 1 | /* |
|---|
| 2 | awayonlock - plugin to set away status on screensaver activation |
|---|
| 3 | Copyright (C) 2009 Leo Antunes <leo@costela.net> |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or |
|---|
| 6 | modify it under the terms of the GNU General Public License |
|---|
| 7 | as published by the Free Software Foundation; either version 2 |
|---|
| 8 | of the License, or (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program 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 General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc. |
|---|
| 18 | |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #define PURPLE_PLUGINS |
|---|
| 22 | |
|---|
| 23 | #define AWAYONLOCK_VERSION "0.5.1" |
|---|
| 24 | #define AWAYONLOCK_PLUGIN_ID "core-costela-awayonlock" |
|---|
| 25 | |
|---|
| 26 | #include <dbus/dbus-glib.h> |
|---|
| 27 | |
|---|
| 28 | // purple |
|---|
| 29 | #include <debug.h> |
|---|
| 30 | #include <plugin.h> |
|---|
| 31 | #include <version.h> |
|---|
| 32 | #include <notify.h> |
|---|
| 33 | #include <savedstatuses.h> |
|---|
| 34 | |
|---|
| 35 | #include "i18n.h" |
|---|
| 36 | #include "callback.h" |
|---|
| 37 | #include "prefs.h" |
|---|
| 38 | |
|---|
| 39 | static DBusGConnection *dbus_conn = NULL; |
|---|
| 40 | |
|---|
| 41 | void * |
|---|
| 42 | awayonlock_get_handle(void) |
|---|
| 43 | { |
|---|
| 44 | static char* handle = "awayonlock"; |
|---|
| 45 | return handle; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | static gboolean plugin_load(PurplePlugin *plugin) { |
|---|
| 50 | DBusGProxy *dbus_proxy_gnome = NULL; |
|---|
| 51 | DBusGProxy *dbus_proxy = NULL; |
|---|
| 52 | GError *error = NULL; |
|---|
| 53 | |
|---|
| 54 | bindtextdomain(PACKAGE, LOCALEDIR); |
|---|
| 55 | |
|---|
| 56 | purple_debug(PURPLE_DEBUG_INFO, PACKAGE, N_("plugin_load called\n")); |
|---|
| 57 | |
|---|
| 58 | if(purple_prefs_get_int(AWAYONLOCK_PREF_OLD_STATUS) != 0) { |
|---|
| 59 | PurpleSavedStatus *status_saved = purple_savedstatus_find_by_creation_time((time_t)purple_prefs_get_int(AWAYONLOCK_PREF_OLD_STATUS)); |
|---|
| 60 | purple_debug(PURPLE_DEBUG_INFO, PACKAGE, N_("stale status '%s' located, restoring and clearing\n"), purple_savedstatus_get_title(status_saved)); |
|---|
| 61 | purple_savedstatus_activate(status_saved); |
|---|
| 62 | purple_prefs_set_int(AWAYONLOCK_PREF_OLD_STATUS, 0); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | dbus_conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error); |
|---|
| 66 | |
|---|
| 67 | if(dbus_conn == NULL) { |
|---|
| 68 | purple_debug(PURPLE_DEBUG_ERROR, PACKAGE, N_("failed to get DBus connection\n")); |
|---|
| 69 | purple_notify_error(plugin, "Away-on-lock", _("Failed to get a DBus connection."), g_strdup_printf("DBus error: %s\n",error->message)); |
|---|
| 70 | return FALSE; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | /* |
|---|
| 75 | * Gnome-screensaver 2.26 specific stuff |
|---|
| 76 | */ |
|---|
| 77 | dbus_proxy_gnome = dbus_g_proxy_new_for_name( dbus_conn, |
|---|
| 78 | "org.gnome.ScreenSaver", |
|---|
| 79 | "/org/gnome/ScreenSaver", |
|---|
| 80 | "org.gnome.ScreenSaver" |
|---|
| 81 | ); |
|---|
| 82 | |
|---|
| 83 | if(dbus_proxy_gnome == NULL) { |
|---|
| 84 | purple_debug(PURPLE_DEBUG_ERROR, PACKAGE, N_("failed to get DBus proxy\n")); |
|---|
| 85 | purple_notify_error(plugin, "Away-on-lock", _("Failed to create a DBus Proxy."), NULL); |
|---|
| 86 | dbus_g_connection_unref(dbus_conn); |
|---|
| 87 | return FALSE; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | dbus_g_proxy_add_signal( dbus_proxy_gnome, |
|---|
| 91 | "ActiveChanged", |
|---|
| 92 | G_TYPE_BOOLEAN, |
|---|
| 93 | G_TYPE_INVALID |
|---|
| 94 | ); |
|---|
| 95 | |
|---|
| 96 | dbus_g_proxy_connect_signal( dbus_proxy_gnome, |
|---|
| 97 | "ActiveChanged", |
|---|
| 98 | G_CALLBACK(awayonlock_idle_changed_callback), |
|---|
| 99 | NULL, |
|---|
| 100 | NULL |
|---|
| 101 | ); |
|---|
| 102 | /* |
|---|
| 103 | * END Gnome-screensaver specific stuff |
|---|
| 104 | */ |
|---|
| 105 | |
|---|
| 106 | /* |
|---|
| 107 | * freedesktop stuff |
|---|
| 108 | */ |
|---|
| 109 | dbus_proxy = dbus_g_proxy_new_for_name( dbus_conn, |
|---|
| 110 | "org.freedesktop.ScreenSaver", |
|---|
| 111 | "/ScreenSaver", |
|---|
| 112 | "org.freedesktop.ScreenSaver" |
|---|
| 113 | ); |
|---|
| 114 | |
|---|
| 115 | if(dbus_proxy == NULL) { |
|---|
| 116 | purple_debug(PURPLE_DEBUG_ERROR, PACKAGE, N_("failed to get DBus proxy\n")); |
|---|
| 117 | purple_notify_error(plugin, "Away-on-lock", _("Failed to create a DBus Proxy."), NULL); |
|---|
| 118 | dbus_g_connection_unref(dbus_conn); |
|---|
| 119 | return FALSE; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | dbus_g_proxy_add_signal( dbus_proxy, |
|---|
| 123 | "ActiveChanged", |
|---|
| 124 | G_TYPE_BOOLEAN, |
|---|
| 125 | G_TYPE_INVALID |
|---|
| 126 | ); |
|---|
| 127 | |
|---|
| 128 | dbus_g_proxy_connect_signal( dbus_proxy, |
|---|
| 129 | "ActiveChanged", |
|---|
| 130 | G_CALLBACK(awayonlock_idle_changed_callback), |
|---|
| 131 | NULL, |
|---|
| 132 | NULL |
|---|
| 133 | ); |
|---|
| 134 | /* |
|---|
| 135 | * END freedesktop stuff |
|---|
| 136 | */ |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | /* |
|---|
| 140 | * avoid dangling references to savedstatuses in the config |
|---|
| 141 | */ |
|---|
| 142 | purple_signal_connect(purple_savedstatuses_get_handle(), "savedstatus-deleted", |
|---|
| 143 | awayonlock_get_handle(), |
|---|
| 144 | PURPLE_CALLBACK(prefs_status_deleted_cb), |
|---|
| 145 | NULL |
|---|
| 146 | ); |
|---|
| 147 | |
|---|
| 148 | purple_debug(PURPLE_DEBUG_INFO, PACKAGE, N_("plugin_load finished\n")); |
|---|
| 149 | return TRUE; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | static gboolean plugin_unload(PurplePlugin *plugin) { |
|---|
| 154 | purple_debug(PURPLE_DEBUG_INFO, PACKAGE, N_("plugin_unload called\n")); |
|---|
| 155 | dbus_g_connection_unref(dbus_conn); |
|---|
| 156 | purple_signals_disconnect_by_handle(awayonlock_get_handle()); |
|---|
| 157 | return TRUE; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | static PurplePluginInfo info = { |
|---|
| 161 | PURPLE_PLUGIN_MAGIC, |
|---|
| 162 | PURPLE_MAJOR_VERSION, |
|---|
| 163 | PURPLE_MINOR_VERSION, |
|---|
| 164 | PURPLE_PLUGIN_STANDARD, |
|---|
| 165 | NULL, |
|---|
| 166 | 0, |
|---|
| 167 | NULL, |
|---|
| 168 | PURPLE_PRIORITY_DEFAULT, |
|---|
| 169 | |
|---|
| 170 | AWAYONLOCK_PLUGIN_ID, |
|---|
| 171 | 0, |
|---|
| 172 | AWAYONLOCK_VERSION, |
|---|
| 173 | |
|---|
| 174 | 0, |
|---|
| 175 | 0, |
|---|
| 176 | 0, |
|---|
| 177 | N_("http://costela.net/projects/awayonlock"), |
|---|
| 178 | |
|---|
| 179 | plugin_load, |
|---|
| 180 | plugin_unload, |
|---|
| 181 | NULL, |
|---|
| 182 | |
|---|
| 183 | NULL, |
|---|
| 184 | NULL, |
|---|
| 185 | &prefs, |
|---|
| 186 | NULL, |
|---|
| 187 | NULL, |
|---|
| 188 | NULL, |
|---|
| 189 | NULL, |
|---|
| 190 | NULL |
|---|
| 191 | }; |
|---|
| 192 | |
|---|
| 193 | static void init_plugin(PurplePlugin *plugin) |
|---|
| 194 | { |
|---|
| 195 | bindtextdomain(PACKAGE, LOCALEDIR); |
|---|
| 196 | |
|---|
| 197 | info.name = _("Away-on-lock"); |
|---|
| 198 | info.summary = _("Changes your status when your screensaver gets activated"); |
|---|
| 199 | info.description = _("This plugin changes your status to a preselected saved status or the default away status whenever your screensaver gets activated. It doesn't interfere if you're already marked as auto-away and can also avoid changing your status if you've manually marked yourself as any non-available status."); |
|---|
| 200 | info.author = _("Leo Antunes <leo@costela.net>"); |
|---|
| 201 | |
|---|
| 202 | purple_prefs_add_none(AWAYONLOCK_PREF_ROOT); |
|---|
| 203 | purple_prefs_add_string(AWAYONLOCK_PREF_STATUS, NULL); |
|---|
| 204 | purple_prefs_add_int(AWAYONLOCK_PREF_OLD_STATUS, 0); |
|---|
| 205 | purple_prefs_add_bool(AWAYONLOCK_PREF_AVAILABLE_ONLY, 0); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | PURPLE_INIT_PLUGIN(awayonlock, init_plugin, info); |
|---|