Defuzzied one entry
[midnight-commander.git] / gnome / gsession.c
blob1e8c3b8ecfe177ebaff2cc607d9e2cf964423595
1 /* Session management for the Midnight Commander
3 * Copyright (C) 1999 the Free Software Foundation
5 * Authors: Federico Mena <federico@nuclecu.unam.mx>
6 * Miguel de Icaza <miguel@nuclecu.unam.mx>
7 */
9 #include <config.h>
10 #include <gnome.h>
11 #include "main.h"
12 #include "../vfs/vfs.h"
13 #include "gcmd.h"
14 #include "gmain.h"
15 #include "gscreen.h"
16 #include "gsession.h"
19 /* Whether we are session managed or not */
20 int session_have_sm = FALSE;
22 /* The master SM client */
23 static GnomeClient *master_client;
25 /* Structure to hold information for a panel to be created */
26 typedef struct {
27 char *cwd;
28 int column_width[GMC_COLUMNS];
29 char *user_format;
30 int list_type;
31 } PanelInfo;
34 /* Saves the SM info for a panel */
35 static void
36 save_panel_info (WPanel *panel)
38 char section[50];
39 char key[50];
40 char *path;
41 int i;
43 g_snprintf (section, sizeof (section), "panel %d", panel->id);
45 path = g_strconcat (section, "/cwd", NULL);
46 gnome_config_set_string (path, panel->cwd);
47 g_free (path);
49 path = g_strconcat (section, "/user_format", NULL);
50 gnome_config_set_string (path, panel->user_format);
51 g_free (path);
53 for (i = 0; i < GMC_COLUMNS; i++) {
54 g_snprintf (key, sizeof (key), "/column_width_%i", i);
55 path = g_strconcat (section, key, NULL);
56 gnome_config_set_int (path, panel->column_width[i]);
57 g_free (path);
60 path = g_strconcat (section, "/list_type", NULL);
61 gnome_config_set_int (path, panel->list_type);
62 g_free (path);
65 /* Loads a panel from the information in the specified gnome-config file/section */
66 static PanelInfo *
67 load_panel_info (char *file, char *section)
69 PanelInfo *pi;
70 char *prefix;
71 char *cwd, *user_format;
72 char key[50];
73 int i;
75 pi = NULL;
77 prefix = g_strconcat (file, section, "/", NULL);
78 printf ("Prefix is \"%s\"\n", prefix);
79 gnome_config_push_prefix (prefix);
81 cwd = gnome_config_get_string ("cwd");
82 if (!cwd) {
83 g_warning ("Could not read panel data for \"%s\"", prefix);
84 gnome_config_pop_prefix ();
85 g_free (prefix);
86 return NULL;
89 pi = g_new (PanelInfo, 1);
90 pi->cwd = cwd;
92 user_format = gnome_config_get_string ("user_format");
93 if (!user_format)
94 user_format = g_strdup (DEFAULT_USER_FORMAT);
96 pi->user_format = user_format;
98 for (i = 0; i < GMC_COLUMNS; i++) {
99 g_snprintf (key, sizeof (key), "column_width_%i=0", i);
100 pi->column_width[i] = gnome_config_get_int_with_default (key, NULL);
103 pi->list_type = gnome_config_get_int_with_default ("list_type=0", NULL);
105 gnome_config_pop_prefix ();
106 g_free (prefix);
108 return pi;
111 /* Frees a panel info structure */
112 static void
113 free_panel_info (PanelInfo *pi)
115 g_free (pi->cwd);
116 g_free (pi->user_format);
117 g_free (pi);
120 static void
121 create_panel_from_info (PanelInfo *pi)
123 WPanel *panel;
125 panel = new_panel_at (pi->cwd);
127 g_free (panel->user_format);
128 panel->user_format = g_strdup (pi->user_format);
129 memcpy (panel->column_width, pi->column_width, sizeof (pi->column_width));
130 panel->list_type = pi->list_type;
133 static void
134 load_session_info (char *filename)
136 void *iterator;
137 char *key, *value;
138 PanelInfo *pi;
140 iterator = gnome_config_init_iterator_sections (filename);
142 while ((iterator = gnome_config_iterator_next (iterator, &key, &value)) != NULL)
143 if (key && strncmp (key, "panel ", 6) == 0) {
144 pi = load_panel_info (filename, key);
145 g_free (key);
146 if (!pi)
147 continue;
149 create_panel_from_info (pi);
150 free_panel_info (pi);
154 static void
155 create_default_panel (const char *startup_dir)
157 char buf[MC_MAXPATHLEN];
158 const char *dir = buf;
160 if (startup_dir == NULL)
161 mc_get_current_wd (buf, MC_MAXPATHLEN);
162 else
163 dir = startup_dir;
165 new_panel_with_geometry_at (dir, cmdline_geometry);
168 /* Callback from the master client to save the session */
169 static gint
170 session_save (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
171 GnomeInteractStyle interact_style, gint fast, gpointer data)
173 char *prefix;
174 GList *l;
175 PanelContainer *pc;
176 gchar *discard_args[] = { "rm", NULL };
178 prefix = gnome_client_get_config_prefix (client);
179 gnome_config_push_prefix (prefix);
181 /* Save the panels */
183 for (l = containers; l; l = l->next) {
184 pc = l->data;
185 if (!is_a_desktop_panel (pc->panel))
186 save_panel_info (pc->panel);
189 gnome_config_pop_prefix ();
190 gnome_config_sync ();
192 discard_args[1] = gnome_config_get_real_path (prefix);
193 gnome_client_set_discard_command (client, 2, discard_args);
194 g_free (discard_args[1]);
196 return TRUE;
199 /* Callback from the master client to terminate the session */
200 static void
201 session_die (GnomeClient *client, gpointer data)
203 gmc_do_quit ();
207 * session_init:
208 * @void:
210 * Initializes session management. Contacts the master client and
211 * connects the appropriate signals.
213 void
214 session_init ()
216 master_client = gnome_master_client ();
218 session_have_sm = (gnome_client_get_flags (master_client) & GNOME_CLIENT_IS_CONNECTED) != 0;
220 gtk_signal_connect (GTK_OBJECT (master_client), "save_yourself",
221 (GtkSignalFunc) session_save,
222 NULL);
223 gtk_signal_connect (GTK_OBJECT (master_client), "die",
224 (GtkSignalFunc) session_die,
225 NULL);
227 session_set_restart (TRUE);
231 * session_load:
232 * @void:
234 * Loads the saved session.
236 void
237 session_load (void)
239 char *filename;
241 if (gnome_client_get_flags (master_client) & GNOME_CLIENT_RESTORED) {
242 filename = gnome_client_get_config_prefix (master_client);
243 load_session_info (filename);
244 } else if (!nowindows)
245 create_default_panel (this_dir);
249 * session_set_restart:
250 * @restart: TRUE if it should restart immediately, FALSE if it should restart
251 * never.
253 * Sets the restart style of the session-managed client.
255 void
256 session_set_restart (int restart)
258 gnome_client_set_priority (master_client, 40);
259 gnome_client_set_restart_style (master_client,
260 (restart
261 ? GNOME_RESTART_IMMEDIATELY
262 : GNOME_RESTART_NEVER));