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>
12 #include "../vfs/vfs.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 */
28 int column_width
[GMC_COLUMNS
];
34 /* Saves the SM info for a panel */
36 save_panel_info (WPanel
*panel
)
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
);
49 path
= g_strconcat (section
, "/user_format", NULL
);
50 gnome_config_set_string (path
, panel
->user_format
);
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
]);
60 path
= g_strconcat (section
, "/list_type", NULL
);
61 gnome_config_set_int (path
, panel
->list_type
);
65 /* Loads a panel from the information in the specified gnome-config file/section */
67 load_panel_info (char *file
, char *section
)
71 char *cwd
, *user_format
;
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");
83 g_warning ("Could not read panel data for \"%s\"", prefix
);
84 gnome_config_pop_prefix ();
89 pi
= g_new (PanelInfo
, 1);
92 user_format
= gnome_config_get_string ("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 ();
111 /* Frees a panel info structure */
113 free_panel_info (PanelInfo
*pi
)
116 g_free (pi
->user_format
);
121 create_panel_from_info (PanelInfo
*pi
)
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
;
134 load_session_info (char *filename
)
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
);
149 create_panel_from_info (pi
);
150 free_panel_info (pi
);
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
);
165 new_panel_with_geometry_at (dir
, cmdline_geometry
);
168 /* Callback from the master client to save the session */
170 session_save (GnomeClient
*client
, gint phase
, GnomeSaveStyle save_style
, gint shutdown
,
171 GnomeInteractStyle interact_style
, gint fast
, gpointer data
)
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
) {
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]);
199 /* Callback from the master client to terminate the session */
201 session_die (GnomeClient
*client
, gpointer data
)
210 * Initializes session management. Contacts the master client and
211 * connects the appropriate signals.
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
,
223 gtk_signal_connect (GTK_OBJECT (master_client
), "die",
224 (GtkSignalFunc
) session_die
,
227 session_set_restart (TRUE
);
234 * Loads the saved session.
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
253 * Sets the restart style of the session-managed client.
256 session_set_restart (int restart
)
258 gnome_client_set_priority (master_client
, 40);
259 gnome_client_set_restart_style (master_client
,
261 ? GNOME_RESTART_IMMEDIATELY
262 : GNOME_RESTART_NEVER
));