~ubuntu-branches/ubuntu/oneiric/cairo-dock-plug-ins/oneiric-updates

« back to all changes in this revision

Viewing changes to logout/src/applet-notifications.c

  • Committer: Kees Cook
  • Date: 2011-08-11 23:17:39 UTC
  • mfrom: (20.1.1 cairo-dock-plug-ins)
  • Revision ID: kees@outflux.net-20110811231739-cteedan51tmdg77v
Tags: 2.4.0~0beta2-0ubuntu1
releasing version 2.4.0~0beta2-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include <stdlib.h>
21
21
#include <string.h>
22
 
#include <signal.h>
23
 
#include <math.h>
24
 
#include <glib/gi18n.h>
25
22
 
26
23
#include "applet-struct.h"
 
24
#include "applet-logout.h"
27
25
#include "applet-notifications.h"
28
26
 
29
 
#define GUEST_SESSION_LAUNCHER "/usr/share/gdm/guest-session/guest-session-launch"
30
 
 
31
27
 
32
28
static void _logout (void)
33
29
{
40
36
                gboolean bLoggedOut = cairo_dock_fm_logout ();
41
37
                if (! bLoggedOut)
42
38
                {
43
 
                        cd_warning ("couldn't guess what to do to log out, you may try to specify the command in the config.");
 
39
                        cd_logout_display_actions ();
44
40
                }
45
41
        }
46
42
}
55
51
                gboolean bShutdowned = cairo_dock_fm_shutdown ();
56
52
                if (! bShutdowned)
57
53
                {
58
 
                        cd_warning ("couldn't guess what to do to shutdown, you may try to specify the command in the config.");
 
54
                        cd_logout_display_actions ();
59
55
                }
60
56
        }
61
57
}
123
119
static void _cd_logout_guest_session (GtkMenuItem *menu_item, gpointer data)
124
120
{
125
121
        CD_APPLET_ENTER;
126
 
        gchar *cResult = cairo_dock_launch_command_sync ("which guest-session");
127
 
        if (cResult != NULL && *cResult == '/')
128
 
                cairo_dock_launch_command ("guest-session");
129
 
        else if (g_file_test (GUEST_SESSION_LAUNCHER, G_FILE_TEST_EXISTS))
130
 
                cairo_dock_launch_command (GUEST_SESSION_LAUNCHER);
131
 
        g_free (cResult);
 
122
        cd_logout_launch_guest_session ();
132
123
        CD_APPLET_LEAVE ();
133
124
}
134
125
 
135
126
static void _cd_logout_program_shutdown (GtkMenuItem *menu_item, gpointer data)
136
127
{
137
128
        CD_APPLET_ENTER;
138
 
        int iDeltaT = (int) (cairo_dock_show_value_and_wait (D_("Choose in how many minutes your PC will stop:"), myIcon, myContainer, 30, 150) * 60);
139
 
        if (iDeltaT == -1)  // cancel
140
 
                CD_APPLET_LEAVE ();
141
 
                //return ;
142
 
        
143
 
        time_t t_cur = (time_t) time (NULL);
144
 
        if (iDeltaT > 0)
145
 
        {
146
 
                //g_print ("iShutdownTime <- %ld + %d\n", t_cur, iDeltaT);
147
 
                myConfig.iShutdownTime = (int) (t_cur + iDeltaT);
148
 
        }
149
 
        else if (iDeltaT == 0)  // on annule l'eventuel precedent.
150
 
        {
151
 
                myConfig.iShutdownTime = 0;
152
 
        }
153
 
        cairo_dock_update_conf_file (CD_APPLET_MY_CONF_FILE,
154
 
                G_TYPE_INT, "Configuration", "shutdown time", myConfig.iShutdownTime,
155
 
                G_TYPE_INVALID);
156
 
        cd_logout_set_timer ();
 
129
        cd_logout_program_shutdown ();
157
130
        CD_APPLET_LEAVE ();
158
131
}
159
132
CD_APPLET_ON_BUILD_MENU_BEGIN
161
134
        GtkWidget *pSubMenu = CD_APPLET_CREATE_MY_SUB_MENU ();
162
135
        
163
136
        gchar *cLabel;
164
 
        if (myConfig.iActionOnClick != CD_LOGOUT)  // logout action not on click => put it in the menu
165
 
        {
166
 
                if (myConfig.iActionOnMiddleClick == CD_LOGOUT)  // logout action on middle-click
167
 
                        cLabel = g_strdup_printf ("%s (%s)", D_("Log out"), D_("middle-click"));
168
 
                else
169
 
                        cLabel = g_strdup (D_("Log out"));
170
 
                CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE, _cd_logout, CD_APPLET_MY_MENU);
171
 
                g_free (cLabel);
172
 
        }
173
 
        if (myConfig.iActionOnClick != CD_SHUTDOWN)  // shutdown action not on click => put it in the menu
174
 
        {
175
 
                if (myConfig.iActionOnMiddleClick == CD_SHUTDOWN)  // logout action on middle-click
176
 
                        cLabel = g_strdup_printf ("%s (%s)", D_("Shut down"), D_("middle-click"));
177
 
                else
178
 
                        cLabel = g_strdup (D_("Shut down"));
179
 
                CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE, _cd_shutdown, CD_APPLET_MY_MENU);
180
 
                g_free (cLabel);
181
 
        }
182
 
        if (myConfig.iActionOnClick != CD_LOCK_SCREEN)  // lockscreen action not on click => put it in the menu
183
 
        {
184
 
                if (myConfig.iActionOnMiddleClick == CD_LOCK_SCREEN)  // lockscreen action on middle-click
185
 
                        cLabel = g_strdup_printf ("%s (%s)", D_("Lock screen"), D_("middle-click"));
186
 
                else
187
 
                        cLabel = g_strdup (D_("Lock screen"));
188
 
                CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, MY_APPLET_SHARE_DATA_DIR"/icon-lock.png", _cd_lock_screen, CD_APPLET_MY_MENU);
189
 
                g_free (cLabel);
190
 
        }
191
 
        if (g_file_test (GUEST_SESSION_LAUNCHER, G_FILE_TEST_EXISTS)) // Guest Session
192
 
        {
193
 
                CD_APPLET_ADD_IN_MENU (D_("Guest session"), _cd_logout_guest_session, CD_APPLET_MY_MENU);
194
 
        }
195
 
        else
196
 
        {
197
 
                gchar *cResult = cairo_dock_launch_command_sync ("which guest-session");
198
 
                if (cResult != NULL && *cResult == '/')
199
 
                        CD_APPLET_ADD_IN_MENU (D_("Guest session"), _cd_logout_guest_session, CD_APPLET_MY_MENU);
200
 
                g_free (cResult);
 
137
        if (! myData.bCapabilitiesChecked)  // if we're using our own logout methods, they are all accessible from the left-click, so no need to add the following actions in the right-click menu.
 
138
        {
 
139
                if (myConfig.iActionOnClick != CD_LOGOUT)  // logout action not on click => put it in the menu
 
140
                {
 
141
                        if (myConfig.iActionOnMiddleClick == CD_LOGOUT)  // logout action on middle-click
 
142
                                cLabel = g_strdup_printf ("%s (%s)", D_("Log out"), D_("middle-click"));
 
143
                        else
 
144
                                cLabel = g_strdup (D_("Log out"));
 
145
                        CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, MY_APPLET_SHARE_DATA_DIR"/system-log-out.svg", _cd_logout, CD_APPLET_MY_MENU);
 
146
                        g_free (cLabel);
 
147
                }
 
148
                if (myConfig.iActionOnClick != CD_SHUTDOWN)  // shutdown action not on click => put it in the menu
 
149
                {
 
150
                        if (myConfig.iActionOnMiddleClick == CD_SHUTDOWN)  // logout action on middle-click
 
151
                                cLabel = g_strdup_printf ("%s (%s)", D_("Shut down"), D_("middle-click"));
 
152
                        else
 
153
                                cLabel = g_strdup (D_("Shut down"));
 
154
                        CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, MY_APPLET_SHARE_DATA_DIR"/system-shutdown.svg", _cd_shutdown, CD_APPLET_MY_MENU);
 
155
                        g_free (cLabel);
 
156
                }
 
157
                if (myConfig.iActionOnClick != CD_LOCK_SCREEN)  // lockscreen action not on click => put it in the menu
 
158
                {
 
159
                        if (myConfig.iActionOnMiddleClick == CD_LOCK_SCREEN)  // lockscreen action on middle-click
 
160
                                cLabel = g_strdup_printf ("%s (%s)", D_("Lock screen"), D_("middle-click"));
 
161
                        else
 
162
                                cLabel = g_strdup (D_("Lock screen"));
 
163
                        CD_APPLET_ADD_IN_MENU_WITH_STOCK (cLabel, MY_APPLET_SHARE_DATA_DIR"/locked.svg", _cd_lock_screen, CD_APPLET_MY_MENU);
 
164
                        g_free (cLabel);
 
165
                }
 
166
        }
 
167
        if (cd_logout_have_guest_session ()) // Guest Session
 
168
        {
 
169
                CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Guest session"), MY_APPLET_SHARE_DATA_DIR"/system-guest.svg", _cd_logout_guest_session, CD_APPLET_MY_MENU);
201
170
        }
202
171
        
203
 
        CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Program an automatic shut-down"), MY_APPLET_SHARE_DATA_DIR"/icon-scheduling.png", _cd_logout_program_shutdown, CD_APPLET_MY_MENU);  // pas beaucoup d'entrees => on le met dans le menu global.
 
172
        CD_APPLET_ADD_IN_MENU_WITH_STOCK (D_("Program an automatic shut-down"), MY_APPLET_SHARE_DATA_DIR"/icon-scheduling.svg", _cd_logout_program_shutdown, CD_APPLET_MY_MENU);  // pas beaucoup d'entrees => on le met dans le menu global.
204
173
        
205
174
        CD_APPLET_ADD_ABOUT_IN_MENU (pSubMenu);
206
175
}
207
176
CD_APPLET_ON_BUILD_MENU_END
208
 
 
209
 
 
210
 
 
211
 
static gboolean _timer (gpointer data)
212
 
{
213
 
        CD_APPLET_ENTER;
214
 
        time_t t_cur = (time_t) time (NULL);
215
 
        if (t_cur >= myConfig.iShutdownTime)
216
 
        {
217
 
                cd_debug ("shutdown !\n");
218
 
                if (g_iDesktopEnv == CAIRO_DOCK_KDE)
219
 
                        cairo_dock_launch_command ("dbus-send --session --type=method_call --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout int32:0 int32:2 int32:2");
220
 
                else
221
 
                        ///cairo_dock_launch_command ("dbus-send --session --type=method_call --dest=org.freedesktop.PowerManagement /org/freedesktop/PowerManagement org.freedesktop.PowerManagement.Shutdown");  // --print-reply --reply-timeout=2000
222
 
                        cairo_dock_launch_command ("dbus-send --system --print-reply --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager org.freedesktop.ConsoleKit.Manager.Stop");  // Suspend est aussi possible
223
 
                
224
 
                myData.iSidTimer = 0;
225
 
                CD_APPLET_LEAVE (FALSE);  // inutile de faire quoique ce soit d'autre, puisque l'ordi s'eteint.
226
 
        }
227
 
        else
228
 
        {
229
 
                cd_debug ("shutdown in %d minutes\n", (int) (myConfig.iShutdownTime - t_cur) / 60);
230
 
                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON_PRINTF ("%dmn", (int) ceil ((double)(myConfig.iShutdownTime - t_cur) / 60.));
231
 
                CD_APPLET_REDRAW_MY_ICON;
232
 
                if (t_cur >= myConfig.iShutdownTime - 60)
233
 
                        cairo_dock_show_temporary_dialog_with_icon (D_("Your computer will shut-down in 1 minute."), myIcon, myContainer, 8000, "same icon");
234
 
        }
235
 
        CD_APPLET_LEAVE (TRUE);
236
 
        
237
 
}
238
 
void cd_logout_set_timer (void)
239
 
{
240
 
        time_t t_cur = (time_t) time (NULL);
241
 
        if (myConfig.iShutdownTime > t_cur)
242
 
        {
243
 
                if (myData.iSidTimer == 0)
244
 
                        myData.iSidTimer = g_timeout_add_seconds (60, _timer, NULL);
245
 
                _timer (NULL);
246
 
        }
247
 
        else if (myData.iSidTimer != 0)
248
 
        {
249
 
                g_source_remove (myData.iSidTimer);
250
 
                myData.iSidTimer = 0;
251
 
                CD_APPLET_SET_QUICK_INFO_ON_MY_ICON (NULL);
252
 
        }
253
 
}
254
 
 
255
 
static void _set_reboot_message (void)
256
 
{
257
 
        gchar *cMessage = NULL;
258
 
        gsize length = 0;
259
 
        g_file_get_contents (CD_REBOOT_NEEDED_FILE,
260
 
                &cMessage,
261
 
                &length,
262
 
                NULL);
263
 
        if (cMessage != NULL)
264
 
        {
265
 
                int len = strlen (cMessage);
266
 
                if (cMessage[len-1] == '\n')
267
 
                        cMessage[len-1] = '\0';
268
 
                CD_APPLET_SET_NAME_FOR_MY_ICON (cMessage);
269
 
        }
270
 
        else
271
 
                CD_APPLET_SET_NAME_FOR_MY_ICON (myConfig.cDefaultLabel);
272
 
        g_free (cMessage);
273
 
}
274
 
void cd_logout_check_reboot_required (CairoDockFMEventType iEventType, const gchar *cURI, gpointer data)
275
 
{
276
 
        switch (iEventType)
277
 
        {
278
 
                case CAIRO_DOCK_FILE_MODIFIED:  // new message
279
 
                        _set_reboot_message ();
280
 
                break;
281
 
                
282
 
                case CAIRO_DOCK_FILE_DELETED:  // reboot no more required (shouldn't happen)
283
 
                        myData.bRebootNeeded = FALSE;
284
 
                        CD_APPLET_SET_NAME_FOR_MY_ICON (myConfig.cDefaultLabel);
285
 
                        CD_APPLET_STOP_DEMANDING_ATTENTION;
286
 
                break;
287
 
                
288
 
                case CAIRO_DOCK_FILE_CREATED:  // reboot required
289
 
                        myData.bRebootNeeded = TRUE;
290
 
                        _set_reboot_message ();
291
 
                        CD_APPLET_DEMANDS_ATTENTION ("pulse", 20);
292
 
                        cairo_dock_show_temporary_dialog_with_icon (myIcon->cName, myIcon, myContainer, 5e3, "same icon");
293
 
                        if (myConfig.cEmblemPath != NULL && *myConfig.cEmblemPath != '\0' && g_file_test (myConfig.cEmblemPath, G_FILE_TEST_EXISTS))
294
 
                                CD_APPLET_SET_EMBLEM_ON_MY_ICON (myConfig.cEmblemPath, CAIRO_DOCK_EMBLEM_UPPER_RIGHT);
295
 
                        else
296
 
                                CD_APPLET_SET_EMBLEM_ON_MY_ICON (MY_APPLET_SHARE_DATA_DIR"/emblem-reboot.png", CAIRO_DOCK_EMBLEM_UPPER_RIGHT);
297
 
                break;
298
 
                default:
299
 
                break;
300
 
        }
301
 
}
302
 
 
303
 
void cd_logout_check_reboot_required_init (void)
304
 
{
305
 
        if (g_file_test (CD_REBOOT_NEEDED_FILE, G_FILE_TEST_EXISTS))
306
 
        {
307
 
                cd_logout_check_reboot_required (CAIRO_DOCK_FILE_CREATED, CD_REBOOT_NEEDED_FILE, NULL);
308
 
        }
309
 
}