Debian Codegraph @

logout/src/applet-logout.c

raw
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 <stdlib.h>
21 #include <string.h>
22
23 #ifdef CD_UPOWER_AVAILABLE
24 /* to access suspend/resume functionality on Upower 0.9
25 * even if we use first logind, we only use logind via DBus
26 */
27 #define UPOWER_ENABLE_DEPRECATED
28 #include <upower.h>
29 #endif
30
31 #include "applet-struct.h"
32 #include "applet-reboot-required.h"
33 #include "applet-timer.h"
34 #include "applet-logout.h"
35
36
37 typedef struct {
38 gchar *cUserName;
39 gchar *cIconFile;
40 gchar *cRealName;
41 } CDUser;
42
43 static void cd_logout_shut_down (void);
44 static void cd_logout_restart (void);
45 static void cd_logout_suspend (void);
46 static void cd_logout_hibernate (void);
47 static void cd_logout_hybridSleep (void);
48 static void cd_logout_close_session (void);
49 static void cd_logout_switch_to_user (const gchar *cUser);
50 static void cd_logout_switch_to_guest (void);
51 //static gboolean cd_logout_switch_to_greeter (void);
52
53 static void _free_user (CDUser *pUser);
54 static GList *cd_logout_get_users_list (void);
55
56 static void _display_menu (void);
57
58
59 ////////////////////
60 /// CAPABILITIES ///
61 ////////////////////
62
63 /*
64 * Check if the method (cMethod) exists and returns 'yes' or 'challenge':
65 * http://www.freedesktop.org/wiki/Software/systemd/logind/
66 * If "yes" is returned the operation is supported and the user may execute the
67 * operation without further authentication.
68 * If "challenge" is returned the operation is available, but only after
69 * authorization.
70 * If it exists, *bIsAble is modified and TRUE is returned.
71 */
72 static gboolean _cd_logout_check_capabilities_logind (DBusGProxy *pProxy, const gchar *cMethod, gboolean *bIsAble)
73 {
74 GError *error = NULL;
75 gchar *cResult = NULL;
76 dbus_g_proxy_call (pProxy, cMethod, &error,
77 G_TYPE_INVALID,
78 G_TYPE_STRING, &cResult,
79 G_TYPE_INVALID);
80 if (!error)
81 {
82 *bIsAble = (cResult && (strcmp (cResult, "yes") == 0
83 || strcmp (cResult, "challenge") == 0));
84 g_free (cResult);
85 }
86 else
87 {
88 cd_debug ("Logind error: %s", error->message);
89 g_error_free (error);
90 return FALSE;
91 }
92 return TRUE;
93 }
94
95 static void _cd_logout_check_capabilities_async (CDSharedMemory *pSharedMemory)
96 {
97 // test first with LoginD
98 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
99 "org.freedesktop.login1",
100 "/org/freedesktop/login1",
101 "org.freedesktop.login1.Manager");
102
103 const gchar *cLogindMethods[] = {"CanPowerOff", "CanReboot", "CanSuspend", "CanHibernate", "CanHybridSleep", NULL};
104 gboolean *bCapabilities[] = {&pSharedMemory->bCanStop,
105 &pSharedMemory->bCanRestart, &pSharedMemory->bCanSuspend,
106 &pSharedMemory->bCanHibernate, &pSharedMemory->bCanHybridSleep};
107
108 if (pProxy && _cd_logout_check_capabilities_logind (pProxy, cLogindMethods[0], bCapabilities[0]))
109 {
110 pSharedMemory->iLoginManager = CD_LOGIND;
111 for (int i = 1; cLogindMethods[i] != NULL; i++)
112 _cd_logout_check_capabilities_logind (pProxy, cLogindMethods[i], bCapabilities[i]);
113
114 g_object_unref (pProxy);
115 }
116 else // then check with ConsoleKit and UPower
117 {
118 GError *error = NULL;
119
120 // get capabilities from UPower: hibernate and suspend
121 #ifdef CD_UPOWER_AVAILABLE
122 UpClient *pUPowerClient = up_client_new ();
123 if (pUPowerClient)
124 {
125 up_client_get_properties_sync (pUPowerClient, NULL, &error); // this function always returns false ... and it crashes the dock (Debian 6) ! :-O
126 if (error)
127 {
128 cd_warning ("UPower error: %s", error->message);
129 g_error_free (error);
130 error = NULL;
131 }
132 else
133 {
134 pSharedMemory->bCanHibernate = up_client_get_can_hibernate (pUPowerClient);
135 pSharedMemory->bCanSuspend = up_client_get_can_suspend (pUPowerClient);
136 g_object_unref (pUPowerClient); // not do that if there is an error to avoid: double free or corruption
137 }
138 }
139 #endif
140
141 // get capabilities from ConsoleKit.: reboot and poweroff
142 pProxy = cairo_dock_create_new_system_proxy (
143 "org.freedesktop.ConsoleKit",
144 "/org/freedesktop/ConsoleKit/Manager",
145 "org.freedesktop.ConsoleKit.Manager");
146
147 dbus_g_proxy_call (pProxy, "CanRestart", &error,
148 G_TYPE_INVALID,
149 G_TYPE_BOOLEAN, &pSharedMemory->bCanRestart,
150 G_TYPE_INVALID);
151 if (!error)
152 {
153 pSharedMemory->iLoginManager = CD_CONSOLE_KIT;
154
155 dbus_g_proxy_call (pProxy, "CanStop", &error,
156 G_TYPE_INVALID,
157 G_TYPE_BOOLEAN, &pSharedMemory->bCanStop,
158 G_TYPE_INVALID);
159 if (error)
160 {
161 cd_warning ("ConsoleKit error: %s", error->message);
162 g_error_free (error);
163 /*g_object_unref (pProxy); // should not happen but lets check guest account
164 return;*/
165 }
166 }
167 else
168 {
169 cd_debug ("ConsoleKit error: %s", error->message);
170 g_error_free (error);
171 }
172 g_object_unref (pProxy);
173 }
174
175 // get capabilities from DisplayManager
176 const gchar *seat = g_getenv ("XDG_SEAT_PATH");
177 if (seat) // else, we could possibly get it by: ck -> GetCurrentSession -> session -> GetSeatId
178 {
179 pProxy = cairo_dock_create_new_system_proxy (
180 "org.freedesktop.DisplayManager",
181 seat,
182 DBUS_INTERFACE_PROPERTIES);
183 pSharedMemory->bHasGuestAccount = cairo_dock_dbus_get_property_as_boolean (pProxy, "org.freedesktop.DisplayManager.Seat", "HasGuestAccount");
184 g_object_unref (pProxy);
185 }
186 else
187 {
188 pSharedMemory->bHasGuestAccount = cairo_dock_dbus_detect_system_application ("org.gnome.DisplayManager");
189 }
190 }
191
192 static gboolean _cd_logout_got_capabilities (CDSharedMemory *pSharedMemory)
193 {
194 CD_APPLET_ENTER;
195
196 // fetch the capabilities.
197 myData.bCapabilitiesChecked = TRUE;
198 myData.bCanHibernate = pSharedMemory->bCanHibernate;
199 myData.bCanHybridSleep = pSharedMemory->bCanHybridSleep;
200 myData.bCanSuspend = pSharedMemory->bCanSuspend;
201 myData.bCanRestart = pSharedMemory->bCanRestart;
202 myData.bCanStop = pSharedMemory->bCanStop;
203 myData.bHasGuestAccount = pSharedMemory->bHasGuestAccount;
204 myData.iLoginManager = pSharedMemory->iLoginManager;
205 cd_debug ("capabilities: %d; %d; %d; %d; %d; %d", myData.bCanHibernate,
206 myData.bCanHybridSleep, myData.bCanSuspend, myData.bCanRestart,
207 myData.bCanStop, myData.bHasGuestAccount);
208
209 // display the menu that has been asked beforehand.
210 _display_menu ();
211
212 // sayonara task-san ^^
213 gldi_task_discard (myData.pTask);
214 myData.pTask = NULL;
215
216 CD_APPLET_LEAVE (FALSE);
217 }
218
219
220 void cd_logout_display_actions (void)
221 {
222 if (myData.pTask != NULL)
223 return;
224 if (! myData.bCapabilitiesChecked)
225 {
226 CDSharedMemory *pSharedMemory = g_new0 (CDSharedMemory, 1);
227 myData.pTask = gldi_task_new_full (0,
228 (GldiGetDataAsyncFunc) _cd_logout_check_capabilities_async,
229 (GldiUpdateSyncFunc) _cd_logout_got_capabilities,
230 (GFreeFunc) g_free,
231 pSharedMemory);
232 gldi_task_launch (myData.pTask);
233 }
234 else
235 {
236 _display_menu ();
237 }
238 }
239
240
241 ////////////
242 /// MENU ///
243 ////////////
244
245 gchar *cd_logout_check_icon (const gchar *cIconStock, gint iIconSize)
246 {
247 gchar *cImagePath = cairo_dock_search_icon_s_path (cIconStock, iIconSize);
248 if (cImagePath != NULL && g_file_test (cImagePath, G_FILE_TEST_EXISTS))
249 return cImagePath;
250 else
251 return NULL;
252 }
253
254 static void _switch_to_user (GtkMenuItem *menu_item, gchar *cUserName)
255 {
256 if (cUserName != NULL)
257 {
258 cd_logout_switch_to_user (cUserName);
259 }
260 else // guest
261 {
262 cd_logout_switch_to_guest ();
263 }
264 }
265
266 static GtkWidget *_build_menu (GtkWidget **pShutdownMenuItem)
267 {
268 GtkWidget *pMenu = gldi_menu_new (myIcon);
269
270 GtkWidget *pMenuItem;
271
272 gchar *cImagePath;
273 cImagePath = cd_logout_check_icon ("system-shutdown", myData.iDesiredIconSize);
274 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Shut down"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/system-shutdown.svg", cd_logout_shut_down, pMenu);
275 g_free (cImagePath);
276 if (!myData.bCanStop && ! myConfig.cUserActionShutdown)
277 gtk_widget_set_sensitive (pMenuItem, FALSE);
278 *pShutdownMenuItem = pMenuItem;
279
280 cImagePath = cd_logout_check_icon (GLDI_ICON_NAME_REFRESH, myData.iDesiredIconSize);
281 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Restart"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/system-restart.svg", cd_logout_restart, pMenu);
282 g_free (cImagePath);
283 if (!myData.bCanRestart)
284 gtk_widget_set_sensitive (pMenuItem, FALSE);
285
286 cImagePath = cd_logout_check_icon ("sleep", myData.iDesiredIconSize);
287 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Hibernate"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/system-hibernate.svg", cd_logout_hibernate, pMenu);
288 gtk_widget_set_tooltip_text (pMenuItem, D_("Your computer will not consume any energy."));
289 if (!myData.bCanHibernate)
290 gtk_widget_set_sensitive (pMenuItem, FALSE);
291
292 if (myData.bCanHybridSleep)
293 {
294 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Hybrid Sleep"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/system-hibernate.svg", cd_logout_hybridSleep, pMenu);
295 gtk_widget_set_tooltip_text (pMenuItem, D_("Your computer will still consume a small amount of energy but after some time, the computer will suspend to disk and not consume any energy."));
296 }
297 g_free (cImagePath);
298
299 cImagePath = cd_logout_check_icon ("clock", myData.iDesiredIconSize);
300 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Suspend"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/system-suspend.svg", cd_logout_suspend, pMenu);
301 g_free (cImagePath);
302 gtk_widget_set_tooltip_text (pMenuItem, D_("Your computer will still consume a small amount of energy."));
303 if (!myData.bCanSuspend)
304 gtk_widget_set_sensitive (pMenuItem, FALSE);
305
306 if (g_getenv ("SESSION_MANAGER") != NULL) // needs a session manager for this.
307 {
308 cImagePath = cd_logout_check_icon ("system-log-out", myData.iDesiredIconSize);
309 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Log out"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/system-log-out.svg", cd_logout_close_session, pMenu);
310 g_free (cImagePath);
311 gtk_widget_set_tooltip_text (pMenuItem, D_("Close your session and allow to open a new one."));
312 }
313
314 if (myData.pUserList != NULL) // refresh the users list (we could listen for the UserAdded,UserDeleted, UserChanged signals too).
315 {
316 g_list_foreach (myData.pUserList, (GFunc)_free_user, NULL);
317 g_list_free (myData.pUserList);
318 }
319 myData.pUserList = cd_logout_get_users_list ();
320 if (myData.pUserList != NULL && (myData.bHasGuestAccount || myData.pUserList->next != NULL)) // at least 2 users
321 {
322 GtkWidget *pUsersSubMenu = CD_APPLET_ADD_SUB_MENU_WITH_IMAGE (D_("Switch user"), pMenu, GLDI_ICON_NAME_JUMP_TO);
323
324 gboolean bFoundUser = FALSE;
325 const gchar *cCurrentUser = g_getenv ("USER");
326 CDUser *pUser;
327 GList *u;
328 for (u = myData.pUserList; u != NULL; u = u->next)
329 {
330 pUser = u->data;
331 pMenuItem = CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (
332 (pUser->cRealName && *(pUser->cRealName) != '\0') ? pUser->cRealName : pUser->cUserName,
333 pUser->cIconFile, _switch_to_user, pUsersSubMenu, pUser->cUserName);
334 if (! bFoundUser && cCurrentUser && strcmp (cCurrentUser, pUser->cUserName) == 0)
335 {
336 bFoundUser = TRUE;
337 gtk_widget_set_sensitive (pMenuItem, FALSE);
338 }
339 }
340
341 if (myData.bHasGuestAccount && bFoundUser) // if we didn't find the user yet, it means we are the guest, so don't show this entry.
342 {
343 CD_APPLET_ADD_IN_MENU_WITH_STOCK_AND_DATA (D_("Guest session"), NULL, _switch_to_user, pUsersSubMenu, NULL); // NULL will mean "guest"
344 }
345 }
346
347 CD_APPLET_ADD_SEPARATOR_IN_MENU (pMenu);
348
349 cImagePath = cd_logout_check_icon ("system-lock-screen", myData.iDesiredIconSize);
350 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Lock screen"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/locked.svg", cairo_dock_fm_lock_screen, pMenu); /// TODO: same question...
351 g_free (cImagePath);
352 if (myData.bCanStop)
353 {
354 cImagePath = cd_logout_check_icon ("document-open-recent", myData.iDesiredIconSize);
355 CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Program an automatic shut-down"), cImagePath ? cImagePath : MY_APPLET_SHARE_DATA_DIR"/icon-scheduling.svg", cd_logout_program_shutdown, pMenu);
356 g_free (cImagePath);
357 }
358
359 if ((myDock && myDock->container.bIsHorizontal && ! myDock->container.bDirectionUp) // on the top, we inverse the menu (mainly to be close to what others do).
360 || (myDesklet && myDesklet->container.iWindowPositionY < (g_desktopGeometry.Xscreen.height / 2)))
361 {
362 GList *children = gtk_container_get_children (GTK_CONTAINER (pMenu));
363 GList *pMenuList;
364 for (pMenuList = children; pMenuList != NULL; pMenuList = pMenuList->next)
365 {
366 pMenuItem = pMenuList->data;
367 gtk_menu_reorder_child (GTK_MENU (pMenu), pMenuItem, 0);
368 }
369 g_list_free (children);
370 }
371
372 return pMenu;
373 }
374
375 static void _display_menu (void)
376 {
377 // build and show the menu
378 GtkWidget *pShutdownMenuItem = NULL;
379 GtkWidget *pMenu = _build_menu (&pShutdownMenuItem);
380 CD_APPLET_POPUP_MENU_ON_MY_ICON (pMenu);
381
382 // select the first (or last) item, which corresponds to the 'shutdown' action.
383 gtk_menu_shell_select_item (GTK_MENU_SHELL (pMenu), pShutdownMenuItem); // must be done here, after the menu has been realized.
384 }
385
386
387 ///////////////
388 /// ACTIONS ///
389 ///////////////
390
391 static void _console_kit_action (const gchar *cAction)
392 {
393 GError *error = NULL;
394 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
395 "org.freedesktop.ConsoleKit",
396 "/org/freedesktop/ConsoleKit/Manager",
397 "org.freedesktop.ConsoleKit.Manager");
398
399 dbus_g_proxy_call (pProxy, cAction, &error,
400 G_TYPE_INVALID,
401 G_TYPE_INVALID);
402 if (error)
403 {
404 cd_warning ("ConsoleKit error: %s", error->message);
405 g_error_free (error);
406 }
407 g_object_unref (pProxy);
408 }
409
410 static void _logind_action (const gchar *cAction)
411 {
412 GError *error = NULL;
413 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
414 "org.freedesktop.login1",
415 "/org/freedesktop/login1",
416 "org.freedesktop.login1.Manager");
417
418 dbus_g_proxy_call (pProxy, cAction, &error,
419 G_TYPE_BOOLEAN, FALSE, // non-interactive
420 G_TYPE_INVALID,
421 G_TYPE_INVALID);
422 if (error)
423 {
424 cd_warning ("Logind error: %s", error->message);
425 gchar *cMessage = g_strdup_printf ("%s %s\n%s",
426 D_("Logind has returned this error:"),
427 error->message,
428 D_("Please check that you can do this action\n"
429 "(e.g. you can't power the computer off if another session is active)"));
430 gldi_dialog_show_temporary_with_icon (cMessage, myIcon, myContainer,
431 15e3, "same icon");
432 g_free (cMessage);
433 g_error_free (error);
434 }
435 g_object_unref (pProxy);
436 }
437
438 static void _upower_action (gboolean bSuspend)
439 {
440 #ifdef CD_UPOWER_AVAILABLE
441 UpClient *pUPowerClient = up_client_new ();
442 if (bSuspend)
443 up_client_suspend_sync (pUPowerClient, NULL, NULL);
444 else
445 up_client_hibernate_sync (pUPowerClient, NULL, NULL);
446 g_object_unref (pUPowerClient);
447 #endif
448 }
449
450
451 static void _exec_action (int iClickedButton, GtkWidget *pInteractiveWidget, void (*callback) (void), CairoDialog *pDialog)
452 {
453 if (iClickedButton == 0 || iClickedButton == -1) // 'OK' button or 'Enter', execute the action.
454 callback ();
455 else if (myData.iSidShutDown != 0) // 'Cancel' or 'Escap', if a countdown was scheduled, remove it.
456 {
457 g_source_remove (myData.iSidShutDown);
458 myData.iSidShutDown = 0;
459 }
460 myData.pConfirmationDialog = NULL;
461 }
462 static void _demand_confirmation (const gchar *cMessage, const gchar *cIconStock, const gchar *cIconImage, void (*callback) (void))
463 {
464 gchar *cImagePath = cd_logout_check_icon (cIconStock, 32); // dialog
465 myData.pConfirmationDialog = gldi_dialog_show (cMessage, myIcon, myContainer, 0, cImagePath ? cImagePath : cIconImage, NULL, (CairoDockActionOnAnswerFunc) _exec_action, callback, NULL);
466 g_free (cImagePath);
467 }
468
469 static void _shut_down (void)
470 {
471 gldi_object_notify (&myModuleObjectMgr, NOTIFICATION_LOGOUT);
472 if (myData.bCanStop)
473 {
474 switch (myData.iLoginManager)
475 {
476 case CD_CONSOLE_KIT:
477 _console_kit_action ("Stop"); // could use org.gnome.SessionManager.RequestShutdown, but it's not standard.
478 break;
479 case CD_LOGIND:
480 _logind_action ("PowerOff");
481 break;
482 default:
483 break;
484 }
485 }
486 else if (myConfig.cUserActionShutdown)
487 {
488 cairo_dock_launch_command (myConfig.cUserActionShutdown);
489 }
490 }
491 static inline gchar *_info_msg (void)
492 {
493 gchar *cInfo = g_strdup_printf (D_("It will automatically shut-down in %ds"), myData.iCountDown);
494 gchar *cMessage = g_strdup_printf ("%s\n\n (%s)", D_("Shut down the computer?"), cInfo);
495 g_free (cInfo);
496 if (! cd_logout_can_safety_shutdown ())
497 {
498 cInfo = g_strdup_printf ("%s\n\n%s", cMessage,
499 D_("It seems your system is being updated!\n"
500 "Please be sure that you can shut down your computer right now."));
501 g_free (cMessage);
502 return cInfo;
503 }
504 return cMessage;
505 }
506 static gboolean _auto_shot_down (gpointer data)
507 {
508 myData.iCountDown --;
509 if (myData.iCountDown <= 0)
510 {
511 myData.iSidShutDown = 0;
512 gldi_object_unref (GLDI_OBJECT(myData.pConfirmationDialog));
513 myData.pConfirmationDialog = NULL;
514 _shut_down ();
515 return FALSE;
516 }
517 else
518 {
519 if (myData.pConfirmationDialog) // paranoia
520 {
521 gchar *cMessage = _info_msg ();
522 gldi_dialog_set_message (myData.pConfirmationDialog, cMessage);
523 g_free (cMessage);
524 }
525 return TRUE;
526 }
527 }
528 void cd_logout_shut_down (void)
529 {
530 if (myConfig.bConfirmAction)
531 {
532 myData.iCountDown = 60;
533 gchar *cMessage = _info_msg ();
534 _demand_confirmation (cMessage, "system-shutdown", MY_APPLET_SHARE_DATA_DIR"/system-shutdown.svg", _shut_down);
535 g_free (cMessage);
536 if (myData.iSidShutDown == 0)
537 {
538 myData.iSidShutDown = g_timeout_add_seconds (1, _auto_shot_down, NULL);
539 }
540 }
541 else
542 {
543 _shut_down ();
544 }
545 }
546
547 static void _restart (void)
548 {
549 gldi_object_notify (&myModuleObjectMgr, NOTIFICATION_LOGOUT);
550 if (myData.bCanRestart)
551 {
552 switch (myData.iLoginManager)
553 {
554 case CD_CONSOLE_KIT:
555 _console_kit_action ("Restart"); // could use org.gnome.SessionManager.RequestShutdown, but it's not standard.
556 break;
557 case CD_LOGIND:
558 _logind_action ("Reboot");
559 break;
560 default:
561 break;
562 }
563 }
564 else if (myConfig.cUserActionShutdown)
565 {
566 cairo_dock_launch_command (myConfig.cUserActionShutdown);
567 }
568 }
569
570 static void cd_logout_restart (void)
571 {
572 if (myConfig.bConfirmAction)
573 {
574 _demand_confirmation (D_("Restart the computer?"), GLDI_ICON_NAME_REFRESH, MY_APPLET_SHARE_DATA_DIR"/system-restart.svg", _restart);
575 }
576 else
577 {
578 _restart ();
579 }
580 }
581
582 static void cd_logout_suspend (void)
583 {
584 if (myData.iLoginManager == CD_LOGIND)
585 _logind_action ("Suspend");
586 else
587 _upower_action (TRUE);
588 }
589
590 static void cd_logout_hibernate (void)
591 {
592 if (myData.iLoginManager == CD_LOGIND)
593 _logind_action ("Hibernate");
594 else
595 _upower_action (FALSE);
596 }
597
598 static void cd_logout_hybridSleep (void)
599 {
600 if (myData.iLoginManager == CD_LOGIND)
601 _logind_action ("HybridSleep");
602 else
603 _upower_action (FALSE);
604 }
605
606 static void _logout (void)
607 {
608 gldi_object_notify (&myModuleObjectMgr, NOTIFICATION_LOGOUT);
609 if (myConfig.cUserAction != NULL)
610 cairo_dock_launch_command (myConfig.cUserAction);
611 else // SwitchToGreeter will only show the greeter, we want to close the session
612 cairo_dock_launch_command (MY_APPLET_SHARE_DATA_DIR"/logout.sh");
613 }
614
615 static void cd_logout_close_session (void) // could use org.gnome.SessionManager.Logout
616 {
617 /* Currently, cairo_dock_fm_logout displays to us a window from the DE
618 * to confirm if we want to close the session or not. So there is a
619 * confirmation box if cairo_dock_fm_logout returns TRUE.
620 * Now, if it returns FALSE, we will use logout.sh script and display
621 * a confirmation box if the user wants to have this dialogue.
622 */
623 if (! cairo_dock_fm_logout ()) // it seems it's better to use tools from the DE for the logout action
624 {
625 if (myConfig.bConfirmAction)
626 {
627 _demand_confirmation (D_("Close the current session?"), "system-log-out", MY_APPLET_SHARE_DATA_DIR"/system-log-out.svg", _logout); /// same question, see above...
628 }
629 else
630 {
631 _logout ();
632 }
633 }
634 else
635 gldi_object_notify (&myModuleObjectMgr, NOTIFICATION_LOGOUT);
636 }
637
638 static void cd_logout_switch_to_user (const gchar *cUser)
639 {
640 const gchar *seat = g_getenv ("XDG_SEAT_PATH");
641 GError *error = NULL;
642 if (seat) // else, we could possibly get it by: ck -> GetCurrentSession -> session -> GetSeatId
643 {
644 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
645 "org.freedesktop.DisplayManager",
646 seat,
647 "org.freedesktop.DisplayManager.Seat");
648 dbus_g_proxy_call (pProxy, "SwitchToUser", &error,
649 G_TYPE_STRING, cUser,
650 G_TYPE_STRING, "", // session, but actually it can be NULL so why bother?
651 G_TYPE_INVALID,
652 G_TYPE_INVALID);
653 if (error)
654 {
655 cd_warning ("DisplayManager (LocalDisplay) error: %s", error->message);
656 g_error_free (error);
657 if (myConfig.cUserActionSwitchUser != NULL)
658 cairo_dock_launch_command_printf ("%s %s", NULL,
659 myConfig.cUserActionSwitchUser, cUser);
660 }
661 g_object_unref (pProxy);
662 }
663 else // try with gdm
664 {
665 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
666 "org.gnome.DisplayManager",
667 "/org/gnome/DisplayManager/LocalDisplayFactory",
668 "org.gnome.DisplayManager.LocalDisplayFactory");
669 dbus_g_proxy_call (pProxy, "SwitchToUser", &error,
670 G_TYPE_STRING, cUser,
671 G_TYPE_INVALID,
672 G_TYPE_INVALID); // we don't care the 'id' object path returned
673 if (error)
674 {
675 cd_warning ("DisplayManager error: %s", error->message);
676 g_error_free (error);
677 if (myConfig.cUserActionSwitchUser != NULL)
678 cairo_dock_launch_command_printf ("%s %s", NULL,
679 myConfig.cUserActionSwitchUser, cUser);
680 }
681 g_object_unref (pProxy);
682 }
683 }
684
685 static void cd_logout_switch_to_guest (void)
686 {
687 const gchar *seat = g_getenv ("XDG_SEAT_PATH");
688 GError *error = NULL;
689 if (seat) // else, we could possibly get it by: ck -> GetCurrentSession -> session -> GetSeatId
690 {
691 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
692 "org.freedesktop.DisplayManager",
693 seat,
694 "org.freedesktop.DisplayManager.Seat");
695 dbus_g_proxy_call (pProxy, "SwitchToGuest", &error,
696 G_TYPE_STRING, "", // session, but actually it can be NULL so why bother?
697 G_TYPE_INVALID,
698 G_TYPE_INVALID);
699 if (error)
700 {
701 cd_warning ("DisplayManager error: %s", error->message);
702 g_error_free (error);
703 if (myConfig.cUserActionSwitchUser != NULL)
704 cairo_dock_launch_command (myConfig.cUserActionSwitchUser);
705 }
706 g_object_unref (pProxy);
707 }
708 else // try with gdm
709 {
710 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
711 "org.gnome.DisplayManager",
712 "/org/gnome/DisplayManager/LocalDisplayFactory",
713 "org.gnome.DisplayManager.LocalDisplayFactory");
714 dbus_g_proxy_call (pProxy, "StartGuestSession", &error,
715 G_TYPE_STRING, "", // current user session, but actually it can be NULL so why bother?
716 G_TYPE_INVALID,
717 G_TYPE_INVALID); // we don't care the 'id' object path returned
718 if (error)
719 {
720 cd_warning ("DisplayManager (LocalDisplay) error: %s", error->message);
721 g_error_free (error);
722 if (myConfig.cUserActionSwitchUser != NULL)
723 cairo_dock_launch_command (myConfig.cUserActionSwitchUser);
724 }
725 g_object_unref (pProxy);
726 }
727 }
728 /*
729 static gboolean cd_logout_switch_to_greeter (void) // not really sure how to use it.
730 {
731 const gchar *seat = g_getenv ("XDG_SEAT_PATH");
732 if (!seat)
733 return FALSE;
734
735 GError *error = NULL;
736 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy (
737 "org.freedesktop.DisplayManager",
738 seat,
739 "org.freedesktop.DisplayManager.Seat");
740 dbus_g_proxy_call (pProxy, "SwitchToGreeter", &error,
741 G_TYPE_INVALID,
742 G_TYPE_INVALID);
743 if (error)
744 {
745 cd_warning ("DisplayManager error: %s", error->message);
746 g_error_free (error);
747 g_object_unref (pProxy);
748 return FALSE;
749 }
750 g_object_unref (pProxy);
751 return TRUE;
752 }
753 */
754
755 //////////////////
756 /// USERS LIST ///
757 //////////////////
758
759 static void _free_user (CDUser *pUser)
760 {
761 g_free (pUser->cUserName);
762 g_free (pUser->cIconFile);
763 g_free (pUser->cRealName);
764 g_free (pUser);
765 }
766
767 static int _compare_user_name (CDUser *pUser1, CDUser *pUser2)
768 {
769 return strcmp (pUser1->cUserName, pUser2->cUserName);
770 }
771
772 static GList* _get_users_list_fallback (void)
773 {
774 // read from /etc/passwd
775 gchar *cContent=NULL;
776 gsize length = 0;
777 g_file_get_contents ("/etc/passwd",
778 &cContent,
779 &length,
780 NULL);
781 g_return_val_if_fail (cContent != NULL, NULL);
782
783 // parse eash user
784 gchar **cUsers = g_strsplit (cContent, "\n", 0);
785 GList *pUserList = NULL;
786 gchar **cUserProps;
787 CDUser *pUser;
788 char *str;
789 int i;
790 for (i = 0; cUsers[i] != NULL; i ++)
791 {
792 cUserProps = g_strsplit (cUsers[i], ":", 0);
793 // add the user if it fits
794 if (cUserProps && cUserProps[0] && cUserProps[1] && cUserProps[2]
795 && atoi (cUserProps[2]) >= 1000 && atoi (cUserProps[2]) < 65530) // heuristic: first user has an udi of 1000, and other users an uid >= 1000; remove the 'nobody' user (uid=65534)
796 {
797 pUser = g_new0 (CDUser, 1);
798 pUser->cUserName = g_strdup (cUserProps[0]);
799 pUser->cIconFile = NULL;
800 pUser->cRealName = g_strdup (cUserProps[4]);
801 if (pUser->cRealName)
802 {
803 str = strchr (pUser->cRealName, ',');
804 if (str) // remove the comments, separated by ','
805 *str = '\0';
806 }
807 pUserList = g_list_insert_sorted (pUserList, pUser, (GCompareFunc)_compare_user_name);
808 }
809 }
810
811 free (cContent);
812 g_strfreev (cUsers);
813 return pUserList;
814 }
815
816 static GList* _get_users_list_gdm (void)
817 {
818 GError *error = NULL;
819 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy ("org.gnome.DisplayManager",
820 "/org/gnome/DisplayManager/UserManager",
821 "org.gnome.DisplayManager.UserManager");
822 GArray *users = NULL;
823 dbus_g_proxy_call (pProxy, "GetUserList", &error,
824 G_TYPE_INVALID,
825 dbus_g_type_get_collection ("GArray", G_TYPE_INT64), &users,
826 G_TYPE_INVALID);
827 if (error)
828 {
829 cd_warning ("Couldn't get users on the bus from org.gnome.DisplayManager (%s)\n-> Using a fallback method.", error->message);
830 g_error_free (error);
831 users = NULL;
832 }
833 if (users == NULL)
834 return _get_users_list_fallback ();
835
836 GType g_type_ptrarray = dbus_g_type_get_collection ("GPtrArray",
837 dbus_g_type_get_struct("GValueArray",
838 G_TYPE_INT64, // uid
839 G_TYPE_STRING, // user name
840 G_TYPE_STRING, // real name
841 G_TYPE_STRING, // shell
842 G_TYPE_INT, // login frequency
843 G_TYPE_STRING, // icon URL
844 G_TYPE_INVALID));
845 GPtrArray *info = NULL;
846 dbus_g_proxy_call (pProxy, "GetUsersInfo", &error,
847 dbus_g_type_get_collection ("GArray", G_TYPE_INT64), users,
848 G_TYPE_INVALID,
849 g_type_ptrarray, &info,
850 G_TYPE_INVALID);
851 if (error)
852 {
853 cd_warning ("Couldn't get info on the bus from org.gnome.DisplayManager (%s)\n-> Using a fallback method.", error->message);
854 g_error_free (error);
855 info = NULL;
856 }
857 if (info == NULL)
858 return _get_users_list_fallback ();
859
860 CDUser *pUser;
861 GList *pUserList = NULL;
862 GValueArray *va;
863 GValue *v;
864 guint i;
865 for (i = 0; i < info->len; i ++)
866 {
867 va = info->pdata[i];
868 if (! va)
869 continue;
870
871 pUser = g_new0 (CDUser, 1);
872 v = g_value_array_get_nth (va, 1); // GValueArray is deprecated from 2.32, yet it's so convenient to map the g_type_ptrarray type ...
873 if (v && G_VALUE_HOLDS_STRING (v))
874 pUser->cUserName = g_strdup (g_value_get_string (v));
875 if (pUser->cUserName == NULL) // shouldn't happen
876 continue;
877
878 v = g_value_array_get_nth (va, 2);
879 if (v && G_VALUE_HOLDS_STRING (v))
880 pUser->cRealName = g_strdup (g_value_get_string (v));
881
882 v = g_value_array_get_nth (va, 5);
883 if (v && G_VALUE_HOLDS_STRING (v))
884 pUser->cIconFile = g_strdup (g_value_get_string (v));
885
886 pUserList = g_list_insert_sorted (pUserList, pUser, (GCompareFunc)_compare_user_name);
887 }
888 g_ptr_array_free (info, TRUE);
889 g_array_free (users, TRUE);
890
891 g_object_unref (pProxy);
892 return pUserList;
893 }
894
895 static GList *cd_logout_get_users_list (void)
896 {
897 // get the list of users
898 GError *error = NULL;
899 DBusGProxy *pProxy = cairo_dock_create_new_system_proxy ("org.freedesktop.Accounts",
900 "/org/freedesktop/Accounts",
901 "org.freedesktop.Accounts");
902
903 GPtrArray *users = NULL;
904 dbus_g_proxy_call (pProxy, "ListCachedUsers", &error,
905 G_TYPE_INVALID,
906 dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &users,
907 G_TYPE_INVALID);
908 g_object_unref (pProxy);
909
910 if (error)
911 {
912 cd_warning ("Couldn't get info on the bus from org.freedesktop.Accounts (%s)\n-> Trying from GnomeDisplayManager.", error->message);
913 g_error_free (error);
914 users = NULL;
915 }
916 if (users == NULL)
917 return _get_users_list_gdm ();
918
919 // foreach user, get its properties (name & icon).
920 CDUser *pUser;
921 GList *pUserList = NULL;
922 gchar *cUserObjectPath;
923 guint i;
924 for (i = 0; i < users->len; i++)
925 {
926 cUserObjectPath = g_ptr_array_index (users, i);
927 pProxy = cairo_dock_create_new_system_proxy ("org.freedesktop.Accounts",
928 cUserObjectPath,
929 DBUS_INTERFACE_PROPERTIES);
930
931 pUser = g_new0 (CDUser, 1);
932 pUser->cUserName = cairo_dock_dbus_get_property_as_string (pProxy, "org.freedesktop.Accounts.User", "UserName"); // used to identify the user in SwitchToUser()
933 if (pUser->cUserName == NULL) // shouldn't happen
934 continue;
935 pUser->cIconFile = cairo_dock_dbus_get_property_as_string (pProxy, "org.freedesktop.Accounts.User", "IconFile");
936 pUser->cRealName = cairo_dock_dbus_get_property_as_string (pProxy, "org.freedesktop.Accounts.User", "RealName");
937 pUserList = g_list_insert_sorted (pUserList, pUser, (GCompareFunc)_compare_user_name);
938
939 g_object_unref (pProxy);
940 }
941 return pUserList;
942 }