Converted README to markdown
[rox-filer.git] / ROX-Filer / src / session.c
blob3f5a9af2e7bb4b66934ec573ad6e0fdeb469a528
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* session.c - XSMP client support */
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gtk/gtk.h>
27 #include <X11/SM/SMlib.h>
28 #include <pwd.h>
29 #include "global.h"
30 #include "filer.h"
31 #include "main.h"
32 #include "pinboard.h"
33 #include "panel.h"
34 #include "sc.h"
35 #include "session.h"
37 #define ROX_FILER_URI "http://rox.sourceforge.net/2005/interfaces/ROX-Filer"
39 static gboolean use_0launch;
40 gboolean session_auto_respawn = FALSE; /* If we were started as 'rox -S' */
42 static void save_state(SmClient *client)
44 FilerWindow *filer_window;
45 Panel *panel;
46 Pinboard *pinboard = current_pinboard;
47 GList *list;
48 GPtrArray *restart_cmd = g_ptr_array_new();
49 SmPropValue *program;
50 gchar *types[] = { "-t", "-B", "-l", "-r" };
51 gint i, nvals;
53 if (use_0launch)
55 g_ptr_array_add(restart_cmd, "0launch");
56 g_ptr_array_add(restart_cmd, ROX_FILER_URI);
58 else
60 sc_get_prop_value(client, SmProgram, &program, &nvals);
61 g_ptr_array_add(restart_cmd, program->value);
64 g_ptr_array_add(restart_cmd, "-c");
65 g_ptr_array_add(restart_cmd, client->id);
67 for (list = all_filer_windows; list; list = list->next)
69 filer_window = (FilerWindow *)list->data;
70 gdk_window_set_role(filer_window->window->window,
71 filer_window->sym_path);
72 g_ptr_array_add(restart_cmd, "-d");
73 g_ptr_array_add(restart_cmd, filer_window->sym_path);
76 if (session_auto_respawn)
78 for(i = 0; i < PANEL_NUMBER_OF_SIDES; i++)
80 panel = current_panel[i];
81 if(!panel)
82 continue;
83 g_ptr_array_add(restart_cmd, types[panel->side]);
84 g_ptr_array_add(restart_cmd, panel->name);
87 if (pinboard)
89 g_ptr_array_add(restart_cmd, "-p");
90 g_ptr_array_add(restart_cmd, (gchar *) pinboard_get_name());
93 else
95 g_ptr_array_add(restart_cmd, "-S");
98 sc_set_list_of_array_prop(client, SmRestartCommand,
99 (const gchar **) restart_cmd->pdata, restart_cmd->len);
101 g_ptr_array_free(restart_cmd, TRUE);
104 /* Callbacks for various SM messages */
106 static gboolean save_yourself(SmClient *client)
108 save_state(client);
109 return TRUE;
112 static void die(SmClient *client)
114 gtk_main_quit();
117 void session_init(const gchar *client_id)
119 SmClient *client;
120 struct passwd *pw;
121 gchar *bin_path;
122 gchar *clone_cmd[3];
123 gchar *zerolaunch;
125 if (!sc_session_up())
126 return;
128 pw = getpwuid(euid);
130 zerolaunch = g_find_program_in_path("0launch");
131 use_0launch = (zerolaunch != NULL);
132 g_free(zerolaunch);
134 if (use_0launch)
136 bin_path = "0launch";
137 clone_cmd[0] = bin_path;
138 clone_cmd[1] = ROX_FILER_URI,
139 clone_cmd[2] = "-n";
141 else
143 bin_path = g_strconcat(app_dir, "/AppRun", NULL);
144 clone_cmd[0] = bin_path;
145 clone_cmd[1] = "-n";
146 clone_cmd[2] = NULL;
149 client = sc_new(client_id);
151 if (!sc_connect(client))
153 sc_destroy(client);
154 return;
157 sc_set_array_prop(client, SmProgram, bin_path);
158 sc_set_array_prop(client, SmUserID, pw->pw_name);
159 sc_set_list_of_array_prop(client, SmCloneCommand,
160 (const gchar **) clone_cmd,
161 clone_cmd[2] == NULL ? 2 : 3);
162 sc_set_card_prop(client, SmRestartStyleHint,
163 session_auto_respawn ? SmRestartImmediately : SmRestartIfRunning);
165 client->save_yourself_fn = &save_yourself;
166 client->shutdown_cancelled_fn = NULL;
167 client->save_complete_fn = NULL;
168 client->die_fn = &die;