| 1 |
/** |
| 2 |
* This file is a part of the Cairo-Dock project |
| 3 |
* |
| 4 |
* Copyright : (C) see the 'copyright' file. |
| 5 |
* E-mail : see the 'copyright' file. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License |
| 9 |
* as published by the Free Software Foundation; either version 3 |
| 10 |
* of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <unistd.h> // getpid |
| 21 |
#include <glib.h> |
| 22 |
#include <dbus/dbus-glib.h> |
| 23 |
#include <dbus/dbus-glib-bindings.h> |
| 24 |
|
| 25 |
#include "interface-applet-signals.h" |
| 26 |
#include "interface-applet-methods.h" |
| 27 |
#include "dbus-applet-spec.h" |
| 28 |
#include "dbus-sub-applet-spec.h" |
| 29 |
#include "interface-applet-object.h" |
| 30 |
|
| 31 |
static int s_iModuleId = 1; |
| 32 |
static GList *s_pAppletList = NULL; |
| 33 |
|
| 34 |
static void cd_dbus_applet_dispose (GObject *object); |
| 35 |
static void cd_dbus_applet_finalize (GObject *object); |
| 36 |
|
| 37 |
G_DEFINE_TYPE(dbusApplet, cd_dbus_applet, G_TYPE_OBJECT); |
| 38 |
|
| 39 |
G_DEFINE_TYPE(dbusSubApplet, cd_dbus_sub_applet, G_TYPE_OBJECT); |
| 40 |
|
| 41 |
static void cd_dbus_applet_class_init(dbusAppletClass *klass) |
| 42 |
{ |
| 43 |
cd_debug(""); |
| 44 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 45 |
object_class->dispose = cd_dbus_applet_dispose; |
| 46 |
object_class->finalize = cd_dbus_applet_finalize; |
| 47 |
|
| 48 |
cd_dbus_applet_init_signals_once (klass); |
| 49 |
|
| 50 |
dbus_g_object_type_install_info (cd_dbus_applet_get_type(), &dbus_glib_cd_dbus_applet_object_info); |
| 51 |
} |
| 52 |
static void cd_dbus_applet_init (dbusApplet *pDbusApplet) |
| 53 |
{ |
| 54 |
cd_debug(""); |
| 55 |
|
| 56 |
pDbusApplet->connection = cairo_dock_get_session_connection (); |
| 57 |
pDbusApplet->proxy = cairo_dock_get_main_proxy (); |
| 58 |
pDbusApplet->pSubApplet = g_object_new (cd_dbus_sub_applet_get_type(), NULL); |
| 59 |
pDbusApplet->pSubApplet->pApplet = pDbusApplet; |
| 60 |
} |
| 61 |
|
| 62 |
static void cd_dbus_applet_dispose (GObject *object) |
| 63 |
{ |
| 64 |
dbusApplet *pDbusApplet = (dbusApplet*)object; |
| 65 |
if (pDbusApplet->pSubApplet != NULL) |
| 66 |
{ |
| 67 |
g_object_unref (pDbusApplet->pSubApplet); |
| 68 |
pDbusApplet->pSubApplet = NULL; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static void cd_dbus_applet_finalize (GObject *object) |
| 73 |
{ |
| 74 |
dbusApplet *pDbusApplet = (dbusApplet*)object; |
| 75 |
g_free (pDbusApplet->cBusPath); |
| 76 |
pDbusApplet->cBusPath = NULL; |
| 77 |
} |
| 78 |
|
| 79 |
static void cd_dbus_sub_applet_class_init(dbusSubAppletClass *klass) |
| 80 |
{ |
| 81 |
cd_debug(""); |
| 82 |
|
| 83 |
cd_dbus_sub_applet_init_signals_once (klass); |
| 84 |
|
| 85 |
dbus_g_object_type_install_info (cd_dbus_sub_applet_get_type(), &dbus_glib_cd_dbus_sub_applet_object_info); |
| 86 |
} |
| 87 |
static void cd_dbus_sub_applet_init (dbusSubApplet *pDbusSubApplet) |
| 88 |
{ |
| 89 |
cd_debug(""); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
dbusApplet * cd_dbus_get_dbus_applet_from_instance (GldiModuleInstance *pModuleInstance) |
| 94 |
{ |
| 95 |
dbusApplet *pDbusApplet = NULL; |
| 96 |
GList *a; |
| 97 |
for (a = s_pAppletList; a != NULL; a = a->next) |
| 98 |
{ |
| 99 |
pDbusApplet = a->data; |
| 100 |
if (pDbusApplet->pModuleInstance == pModuleInstance) |
| 101 |
return pDbusApplet; |
| 102 |
} |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
#define _applet_list_is_empty() (s_pAppletList == NULL) |
| 108 |
|
| 109 |
dbusApplet *cd_dbus_create_remote_applet_object (GldiModuleInstance *pModuleInstance) |
| 110 |
{ |
| 111 |
g_return_val_if_fail (pModuleInstance != NULL && myData.pMainObject != NULL, NULL); |
| 112 |
const gchar *cModuleName = pModuleInstance->pModule->pVisitCard->cModuleName; |
| 113 |
g_return_val_if_fail (cModuleName != NULL, NULL); |
| 114 |
cd_debug ("%s (%s)", __func__, cModuleName); |
| 115 |
|
| 116 |
//\_____________ unicity check. |
| 117 |
dbusApplet *pDbusApplet = cd_dbus_get_dbus_applet_from_instance (pModuleInstance); |
| 118 |
if (pDbusApplet != NULL) // shouldn't arrive, but let's be cautious. |
| 119 |
{ |
| 120 |
cd_warning ("this applet (%s) already has a remote object on the bus", cModuleName); |
| 121 |
return pDbusApplet; |
| 122 |
} |
| 123 |
|
| 124 |
//\_____________ create a DBus object corresponding to the applet. |
| 125 |
pDbusApplet = g_object_new (cd_dbus_applet_get_type(), NULL); // appelle cd_dbus_applet_class_init() et cd_dbus_applet_init(). |
| 126 |
pDbusApplet->cModuleName = g_strdup (cModuleName); |
| 127 |
pDbusApplet->pModuleInstance = pModuleInstance; |
| 128 |
pDbusApplet->id = s_iModuleId++; |
| 129 |
|
| 130 |
//\_____________ register it under a unique path. |
| 131 |
gchar *cSuffix = NULL; |
| 132 |
if (pModuleInstance->pModule->pInstancesList != NULL) // if this is the only instance of the applet, don't add suffix (it's not needed, and it keeps backward compatibility). |
| 133 |
cSuffix = g_strdup_printf ("_%d", pDbusApplet->id); |
| 134 |
|
| 135 |
gchar *cNameWithoutHyphen = NULL; |
| 136 |
if (strchr (cModuleName, '-') != NULL) |
| 137 |
{ |
| 138 |
cNameWithoutHyphen = g_strdup (cModuleName); |
| 139 |
int i; |
| 140 |
for (i = 0; cNameWithoutHyphen[i] != '\0'; i ++) |
| 141 |
if (cNameWithoutHyphen[i] == '-' || cNameWithoutHyphen[i] == ' ') |
| 142 |
cNameWithoutHyphen[i] = ' '; |
| 143 |
|
| 144 |
} |
| 145 |
pDbusApplet->cBusPath = g_strconcat (myData.cBasePath, "/", cNameWithoutHyphen ? cNameWithoutHyphen : cModuleName, cSuffix, NULL); |
| 146 |
g_free (cNameWithoutHyphen); |
| 147 |
g_free (cSuffix); |
| 148 |
|
| 149 |
dbus_g_connection_register_g_object (pDbusApplet->connection, pDbusApplet->cBusPath, G_OBJECT(pDbusApplet)); |
| 150 |
|
| 151 |
gchar *cSubPath = g_strconcat (pDbusApplet->cBusPath, "/sub_icons", NULL); |
| 152 |
dbus_g_connection_register_g_object (pDbusApplet->connection, cSubPath, G_OBJECT(pDbusApplet->pSubApplet)); |
| 153 |
g_free (cSubPath); |
| 154 |
|
| 155 |
//\_____________ register to the notifications we'll want to propagate on the bus. |
| 156 |
if (pDbusApplet->proxy != NULL && _applet_list_is_empty ()) // 1ere applet Dbus. |
| 157 |
{ |
| 158 |
gldi_object_register_notification (&myContainerObjectMgr, |
| 159 |
NOTIFICATION_CLICK_ICON, |
| 160 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_click_icon, |
| 161 |
GLDI_RUN_AFTER, |
| 162 |
NULL); |
| 163 |
gldi_object_register_notification (&myContainerObjectMgr, |
| 164 |
NOTIFICATION_MIDDLE_CLICK_ICON, |
| 165 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_middle_click_icon, |
| 166 |
GLDI_RUN_AFTER, |
| 167 |
NULL); |
| 168 |
gldi_object_register_notification (&myContainerObjectMgr, |
| 169 |
NOTIFICATION_SCROLL_ICON, |
| 170 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_scroll_icon, |
| 171 |
GLDI_RUN_FIRST, |
| 172 |
NULL); |
| 173 |
gldi_object_register_notification (&myContainerObjectMgr, |
| 174 |
NOTIFICATION_BUILD_ICON_MENU, |
| 175 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_build_menu, |
| 176 |
GLDI_RUN_FIRST, |
| 177 |
NULL); |
| 178 |
gldi_object_register_notification (&myWindowObjectMgr, |
| 179 |
NOTIFICATION_WINDOW_ACTIVATED, |
| 180 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_change_focus, |
| 181 |
GLDI_RUN_AFTER, |
| 182 |
NULL); |
| 183 |
myData.pActiveWindow = gldi_windows_get_active (); |
| 184 |
} |
| 185 |
|
| 186 |
s_pAppletList = g_list_prepend (s_pAppletList, pDbusApplet); |
| 187 |
return pDbusApplet; |
| 188 |
} |
| 189 |
|
| 190 |
void cd_dbus_delete_remote_applet_object (dbusApplet *pDbusApplet) |
| 191 |
{ |
| 192 |
s_pAppletList = g_list_remove (s_pAppletList, pDbusApplet); |
| 193 |
|
| 194 |
if (_applet_list_is_empty ()) // si plus d'applet dbus, inutile de garder les notifications actives. |
| 195 |
{ |
| 196 |
cd_dbus_unregister_notifications (); |
| 197 |
} |
| 198 |
|
| 199 |
if (pDbusApplet != NULL) |
| 200 |
{ |
| 201 |
// on enleve les raccourcis clavier de l'applet. |
| 202 |
GList *kb; |
| 203 |
GldiShortkey *pKeyBinding; |
| 204 |
for (kb = pDbusApplet->pShortkeyList; kb != NULL; kb = kb->next) |
| 205 |
{ |
| 206 |
pKeyBinding = kb->data; |
| 207 |
gldi_object_unref (GLDI_OBJECT(pKeyBinding)); |
| 208 |
} |
| 209 |
g_list_free (pDbusApplet->pShortkeyList); |
| 210 |
pDbusApplet->pShortkeyList = NULL; |
| 211 |
|
| 212 |
// on detruit l'objet. |
| 213 |
g_object_unref (pDbusApplet); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void cd_dbus_unregister_notifications (void) |
| 218 |
{ |
| 219 |
gldi_object_remove_notification (&myContainerObjectMgr, |
| 220 |
NOTIFICATION_CLICK_ICON, |
| 221 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_click_icon, |
| 222 |
NULL); |
| 223 |
gldi_object_remove_notification (&myContainerObjectMgr, |
| 224 |
NOTIFICATION_MIDDLE_CLICK_ICON, |
| 225 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_middle_click_icon, |
| 226 |
NULL); |
| 227 |
gldi_object_remove_notification (&myContainerObjectMgr, |
| 228 |
NOTIFICATION_SCROLL_ICON, |
| 229 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_scroll_icon, |
| 230 |
NULL); |
| 231 |
gldi_object_remove_notification (&myContainerObjectMgr, |
| 232 |
NOTIFICATION_BUILD_ICON_MENU, |
| 233 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_build_menu, |
| 234 |
NULL); |
| 235 |
gldi_object_remove_notification (&myWindowObjectMgr, |
| 236 |
NOTIFICATION_WINDOW_ACTIVATED, |
| 237 |
(GldiNotificationFunc) cd_dbus_applet_emit_on_change_focus, |
| 238 |
NULL); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
void cd_dbus_launch_applet_process (GldiModuleInstance *pModuleInstance, dbusApplet *pDbusApplet) |
| 243 |
{ |
| 244 |
const gchar *cModuleName = pModuleInstance->pModule->pVisitCard->cModuleName; |
| 245 |
const gchar *cDirPath = pModuleInstance->pModule->pVisitCard->cShareDataDir; |
| 246 |
cd_message ("%s (%s)", __func__, cModuleName); |
| 247 |
|
| 248 |
gchar *cCommand = g_strdup_printf ("cd \"%s\" && ./\"%s\" %d \"%s\" \"%s\" \"%s\" %s %d", |
| 249 |
cDirPath, |
| 250 |
cModuleName, |
| 251 |
pDbusApplet->id, |
| 252 |
pDbusApplet->cBusPath, |
| 253 |
pModuleInstance->cConfFilePath, |
| 254 |
g_cCairoDockDataDir, |
| 255 |
myData.cProgName, |
| 256 |
getpid()); |
| 257 |
cd_debug ("launching distant applet with: '%s'", cCommand); |
| 258 |
cairo_dock_launch_command (cCommand); |
| 259 |
g_free (cCommand); |
| 260 |
} |