1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
 * lapiz-session.c - Basic session management for lapiz
 * This file is part of lapiz
 *
 * Copyright (C) 2002 Ximian, Inc.
 * Copyright (C) 2005 - Paolo Maggi
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/*
 * Modified by the lapiz Team, 2002-2005. See the AUTHORS file for a
 * list of people on the lapiz Team.
 * See the ChangeLog files for a list of changes.
 *
 * $Id$
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <string.h>

#include <libxml/tree.h>
#include <libxml/xmlwriter.h>

#include "lapiz-session.h"

#include "lapiz-debug.h"
#include "lapiz-plugins-engine.h"
#include "lapiz-prefs-manager-app.h"
#include "lapiz-metadata-manager.h"
#include "lapiz-window.h"
#include "lapiz-app.h"
#include "lapiz-commands.h"
#include "dialogs/lapiz-close-confirmation-dialog.h"
#include "smclient/eggsmclient.h"

/* The master client we use for SM */
static EggSMClient *master_client = NULL;

/* global var used during quit_requested */
static GSList *window_dirty_list;

static void	ask_next_confirmation	(void);

#define LAPIZ_SESSION_LIST_OF_DOCS_TO_SAVE "lapiz-session-list-of-docs-to-save-key"

static void
save_window_session (GKeyFile    *state_file,
		     const gchar *group_name,
		     LapizWindow      *window)
{
	const gchar *role;
	int width, height;
	LapizPanel *panel;
	GList *docs, *l;
	GPtrArray *doc_array;
	LapizDocument *active_document;
	gchar *uri;

	lapiz_debug (DEBUG_SESSION);

	role = ctk_window_get_role (CTK_WINDOW (window));
	g_key_file_set_string (state_file, group_name, "role", role);
	ctk_window_get_size (CTK_WINDOW (window), &width, &height);
	g_key_file_set_integer (state_file, group_name, "width", width);
	g_key_file_set_integer (state_file, group_name, "height", height);

	panel = lapiz_window_get_side_panel (window);
	g_key_file_set_boolean (state_file, group_name, "side-panel-visible",
				ctk_widget_get_visible (CTK_WIDGET (panel)));

	panel = lapiz_window_get_bottom_panel (window);
	g_key_file_set_boolean (state_file, group_name, "bottom-panel-visible",
				ctk_widget_get_visible (CTK_WIDGET (panel)));

	active_document = lapiz_window_get_active_document (window);
	if (active_document)
	{
	        uri = lapiz_document_get_uri (active_document);
	        g_key_file_set_string (state_file, group_name,
				       "active-document", uri);
	}

	docs = lapiz_window_get_documents (window);

	doc_array = g_ptr_array_new ();
	for (l = docs; l != NULL; l = g_list_next (l))
	{
		uri = lapiz_document_get_uri (LAPIZ_DOCUMENT (l->data));

		if (uri != NULL)
		        g_ptr_array_add (doc_array, uri);

	}
	g_list_free (docs);

	if (doc_array->len)
	{
	        guint i;

		g_key_file_set_string_list (state_file, group_name,
					    "documents",
					    (const char **)doc_array->pdata,
					    doc_array->len);
		for (i = 0; i < doc_array->len; i++)
		        g_free (doc_array->pdata[i]);
	}
	g_ptr_array_free (doc_array, TRUE);
}

static void
client_save_state_cb (EggSMClient *client G_GNUC_UNUSED,
		      GKeyFile    *state_file,
		      gpointer     user_data G_GNUC_UNUSED)
{
        const GList *windows;
	int n;

	windows = lapiz_app_get_windows (lapiz_app_get_default ());
	n = 1;

	while (windows != NULL)
	{
		gchar *group_name;

		group_name = g_strdup_printf ("lapiz window %d", n);
		save_window_session (state_file,
				     group_name,
				     LAPIZ_WINDOW (windows->data));
		g_free (group_name);

		windows = g_list_next (windows);
		n++;
	}
}

static void
window_handled (LapizWindow *window)
{
	window_dirty_list = g_slist_remove (window_dirty_list, window);

	/* whee... we made it! */
	if (window_dirty_list == NULL)
	        egg_sm_client_will_quit (master_client, TRUE);
	else
		ask_next_confirmation ();
}

static void
window_state_change (LapizWindow *window,
		     GParamSpec  *pspec G_GNUC_UNUSED,
		     gpointer     data)
{
	LapizWindowState state;
	GList *unsaved_docs;
	GList *docs_to_save;
	GList *l;
	gboolean done = TRUE;

	state = lapiz_window_get_state (window);

	/* we are still saving */
	if (state & LAPIZ_WINDOW_STATE_SAVING)
		return;

	unsaved_docs = lapiz_window_get_unsaved_documents (window);

	docs_to_save =	g_object_get_data (G_OBJECT (window),
					   LAPIZ_SESSION_LIST_OF_DOCS_TO_SAVE);


	for (l = docs_to_save; l != NULL; l = l->next)
	{
		if (g_list_find (unsaved_docs, l->data))
		{
			done = FALSE;
			break;
		}
	}

	if (done)
	{
		g_signal_handlers_disconnect_by_func (window, window_state_change, data);
		g_list_free (docs_to_save);
		g_object_set_data (G_OBJECT (window),
				   LAPIZ_SESSION_LIST_OF_DOCS_TO_SAVE,
				   NULL);

		window_handled (window);
	}

	g_list_free (unsaved_docs);
}

static void
close_confirmation_dialog_response_handler (LapizCloseConfirmationDialog *dlg,
					    gint                          response_id,
					    LapizWindow                  *window)
{
	GList *selected_documents;
	GSList *l;<--- Variable 'l' can be declared as pointer to const

	lapiz_debug (DEBUG_COMMANDS);

	switch (response_id)
	{
		case CTK_RESPONSE_YES:
			/* save selected docs */

			g_signal_connect (window,
					  "notify::state",
					  G_CALLBACK (window_state_change),
					  NULL);

			selected_documents = lapiz_close_confirmation_dialog_get_selected_documents (dlg);

			g_return_if_fail (g_object_get_data (G_OBJECT (window),
							     LAPIZ_SESSION_LIST_OF_DOCS_TO_SAVE) == NULL);

			g_object_set_data (G_OBJECT (window),
					   LAPIZ_SESSION_LIST_OF_DOCS_TO_SAVE,
					   selected_documents);

			_lapiz_cmd_file_save_documents_list (window, selected_documents);

			/* FIXME: also need to lock the window to prevent further changes... */

			break;

		case CTK_RESPONSE_NO:
			/* dont save */
			window_handled (window);
			break;

		default:
			/* disconnect window_state_changed where needed */
			for (l = window_dirty_list; l != NULL; l = l->next)
				g_signal_handlers_disconnect_by_func (window,
						window_state_change, NULL);
			g_slist_free (window_dirty_list);
			window_dirty_list = NULL;

			/* cancel shutdown */
			egg_sm_client_will_quit (master_client, FALSE);

			break;
	}

	ctk_widget_destroy (CTK_WIDGET (dlg));
}

static void
show_confirmation_dialog (LapizWindow *window)
{
	GList *unsaved_docs;
	CtkWidget *dlg;

	lapiz_debug (DEBUG_SESSION);

	unsaved_docs = lapiz_window_get_unsaved_documents (window);

	g_return_if_fail (unsaved_docs != NULL);

	if (unsaved_docs->next == NULL)
	{
		/* There is only one unsaved document */
		LapizTab *tab;
		LapizDocument *doc;

		doc = LAPIZ_DOCUMENT (unsaved_docs->data);

		tab = lapiz_tab_get_from_document (doc);
		g_return_if_fail (tab != NULL);

		lapiz_window_set_active_tab (window, tab);

		dlg = lapiz_close_confirmation_dialog_new_single (
						CTK_WINDOW (window),
						doc,
						TRUE);
	}
	else
	{
		dlg = lapiz_close_confirmation_dialog_new (CTK_WINDOW (window),
							   unsaved_docs,
							   TRUE);
	}

	g_list_free (unsaved_docs);

	g_signal_connect (dlg,
			  "response",
			  G_CALLBACK (close_confirmation_dialog_response_handler),
			  window);

	ctk_widget_show (dlg);
}

static void
ask_next_confirmation (void)
{
	g_return_if_fail (window_dirty_list != NULL);

	/* pop up the confirmation dialog for the first window
	 * in the dirty list. The next confirmation is asked once
	 * this one has been handled.
	 */
	show_confirmation_dialog (LAPIZ_WINDOW (window_dirty_list->data));
}

/* quit_requested handler for the master client */
static void
client_quit_requested_cb (EggSMClient *client,
			  gpointer     data G_GNUC_UNUSED)
{
	LapizApp *app;
	const GList *l;

	lapiz_debug (DEBUG_SESSION);

	app = lapiz_app_get_default ();

	if (window_dirty_list != NULL)
	{
		g_critical ("global variable window_dirty_list not NULL");
		window_dirty_list = NULL;
	}

	for (l = lapiz_app_get_windows (app); l != NULL; l = l->next)
	{
		if (lapiz_window_get_unsaved_documents (LAPIZ_WINDOW (l->data)) != NULL)
		{
			window_dirty_list = g_slist_prepend (window_dirty_list, l->data);
		}
	}

	/* no modified docs */
	if (window_dirty_list == NULL)
	{
		egg_sm_client_will_quit (client, TRUE);

		return;
	}

	ask_next_confirmation ();

	lapiz_debug_message (DEBUG_SESSION, "END");
}

/* quit handler for the master client */
static void
client_quit_cb (EggSMClient *client G_GNUC_UNUSED,
		gpointer     data G_GNUC_UNUSED)
{
#if 0
	lapiz_debug (DEBUG_SESSION);

	if (!client->save_yourself_emitted)
		lapiz_file_close_all ();

	lapiz_debug_message (DEBUG_FILE, "All files closed.");

	cafecomponent_mdi_destroy (CAFECOMPONENT_MDI (lapiz_mdi));

	lapiz_debug_message (DEBUG_FILE, "Unref lapiz_mdi.");

	g_object_unref (G_OBJECT (lapiz_mdi));

	lapiz_debug_message (DEBUG_FILE, "Unref lapiz_mdi: DONE");

	lapiz_debug_message (DEBUG_FILE, "Unref lapiz_app_server.");

	cafecomponent_object_unref (lapiz_app_server);

	lapiz_debug_message (DEBUG_FILE, "Unref lapiz_app_server: DONE");
#endif

	ctk_main_quit ();
}

/**
 * lapiz_session_init:
 *
 * Initializes session management support.  This function should be called near
 * the beginning of the program.
 **/
void
lapiz_session_init (void)
{
	lapiz_debug (DEBUG_SESSION);

	if (master_client)
	  return;

	master_client = egg_sm_client_get ();
	g_signal_connect (master_client,
			  "save_state",
			  G_CALLBACK (client_save_state_cb),
			  NULL);
	g_signal_connect (master_client,
			  "quit_requested",
			  G_CALLBACK (client_quit_requested_cb),
			  NULL);
	g_signal_connect (master_client,
			  "quit",
			  G_CALLBACK (client_quit_cb),
			  NULL);
}

/**
 * lapiz_session_is_restored:
 *
 * Returns whether this lapiz is running from a restarted session.
 *
 * Return value: TRUE if the session manager restarted us, FALSE otherwise.
 * This should be used to determine whether to pay attention to command line
 * arguments in case the session was not restored.
 **/
gboolean
lapiz_session_is_restored (void)
{
	gboolean restored;

	lapiz_debug (DEBUG_SESSION);

	if (!master_client)
		return FALSE;

	restored = egg_sm_client_is_resumed (master_client);

	lapiz_debug_message (DEBUG_SESSION, restored ? "RESTORED" : "NOT RESTORED");

	return restored;
}

static void
parse_window (GKeyFile *state_file, const char *group_name)
{
	LapizWindow *window;
	gchar *role, *active_document, **documents;
	int width, height;
	gboolean visible;
	LapizPanel *panel;
	GError *error = NULL;

	role = g_key_file_get_string (state_file, group_name, "role", NULL);

	lapiz_debug_message (DEBUG_SESSION, "Window role: %s", role);

	window = _lapiz_app_restore_window (lapiz_app_get_default (), (gchar *) role);
	g_free (role);

	if (window == NULL)
	{
		g_warning ("Couldn't restore window");
		return;
	}

	width = g_key_file_get_integer (state_file, group_name,
					"width", &error);
	if (error)
	{
	        g_clear_error (&error);
		width = -1;
	}
	height = g_key_file_get_integer (state_file, group_name,
					 "height", &error);
	if (error)
	{
	        g_clear_error (&error);
		height = -1;
	}
	ctk_window_set_default_size (CTK_WINDOW (window), width, height);


	visible = g_key_file_get_boolean (state_file, group_name,
					  "side-panel-visible", &error);
	if (error)
	{
	        g_clear_error (&error);
		visible = FALSE;
	}

	panel = lapiz_window_get_side_panel (window);

	if (visible)
	{
	        lapiz_debug_message (DEBUG_SESSION, "Side panel visible");
		ctk_widget_show (CTK_WIDGET (panel));
	}
	else
	{
	      lapiz_debug_message (DEBUG_SESSION, "Side panel _NOT_ visible");
	      ctk_widget_hide (CTK_WIDGET (panel));
	}

	visible = g_key_file_get_boolean (state_file, group_name,
					  "bottom-panel-visible", &error);
	if (error)
	{
	        g_clear_error (&error);
		visible = FALSE;
	}

	panel = lapiz_window_get_bottom_panel (window);
	if (visible)
	{
	        lapiz_debug_message (DEBUG_SESSION, "Bottom panel visible");
		ctk_widget_show (CTK_WIDGET (panel));
	}
	else
	{
	        lapiz_debug_message (DEBUG_SESSION, "Bottom panel _NOT_ visible");
		ctk_widget_hide (CTK_WIDGET (panel));
	}

	active_document = g_key_file_get_string (state_file, group_name,
						 "active-document", NULL);
	documents = g_key_file_get_string_list (state_file, group_name,
						"documents", NULL, NULL);
	if (documents)
	{
	        int i;
		gboolean jump_to = FALSE;

		for (i = 0; documents[i]; i++)
		{
		        if (active_document != NULL)
			        jump_to = strcmp (active_document,
						  documents[i]) == 0;

			lapiz_debug_message (DEBUG_SESSION,
					     "URI: %s (%s)",
					     documents[i],
					     jump_to ? "active" : "not active");
			lapiz_window_create_tab_from_uri (window,
							  documents[i],
							  NULL,
							  0,
							  FALSE,
							  jump_to);
		}
		g_strfreev (documents);
	}

	g_free (active_document);

	ctk_widget_show (CTK_WIDGET (window));
}

/**
 * lapiz_session_load:
 *
 * Loads the session by fetching the necessary information from the session
 * manager and opening files.
 *
 * Return value: TRUE if the session was loaded successfully, FALSE otherwise.
 **/
gboolean
lapiz_session_load (void)
{
	GKeyFile *state_file;
	gchar **groups;
	int i;

	lapiz_debug (DEBUG_SESSION);

	state_file = egg_sm_client_get_state_file (master_client);
	if (state_file == NULL)
	       return FALSE;

	groups = g_key_file_get_groups (state_file, NULL);

	for (i = 0; groups[i] != NULL; i++)
	{
		if (g_str_has_prefix (groups[i], "lapiz window "))
		        parse_window (state_file, groups[i]);
	}

	g_strfreev (groups);
	g_key_file_free (state_file);

	return TRUE;
}