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
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Session-managment stuff
* Copyright (C) 1998 Sven Neumann <sven@gimp.org>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpconfig/gimpconfig.h"
#include "gui-types.h"
#include "config/gimpconfig-file.h"
#include "config/gimpguiconfig.h"
#include "core/gimp.h"
#include "core/gimperror.h"
#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpsessioninfo.h"
#include "widgets/gimpwidgets-utils.h"
#include "dialogs/dialogs.h"
#include "session.h"
#include "gimp-log.h"
#include "gimp-intl.h"
enum
{
SESSION_INFO = 1,
HIDE_DOCKS,
SINGLE_WINDOW_MODE,
SHOW_TABS,
TABS_POSITION,
LAST_TIP_SHOWN
};
static GFile * session_file (Gimp *gimp);
/* private variables */
static gboolean sessionrc_deleted = FALSE;
/* public functions */
void
session_init (Gimp *gimp)
{
GFile *file;
GScanner *scanner;
GTokenType token;
GError *error = NULL;
g_return_if_fail (GIMP_IS_GIMP (gimp));
file = session_file (gimp);
scanner = gimp_scanner_new_gfile (file, &error);
if (! scanner && error->code == GIMP_CONFIG_ERROR_OPEN_ENOENT)
{
g_clear_error (&error);
g_object_unref (file);
file = gimp_sysconf_directory_file ("sessionrc", NULL);
scanner = gimp_scanner_new_gfile (file, NULL);
}
if (! scanner)
{
g_clear_error (&error);
g_object_unref (file);
return;
}
if (gimp->be_verbose)
g_print ("Parsing '%s'\n", gimp_file_get_utf8_name (file));
g_scanner_scope_add_symbol (scanner, 0, "session-info",
GINT_TO_POINTER (SESSION_INFO));
g_scanner_scope_add_symbol (scanner, 0, "hide-docks",
GINT_TO_POINTER (HIDE_DOCKS));
g_scanner_scope_add_symbol (scanner, 0, "single-window-mode",
GINT_TO_POINTER (SINGLE_WINDOW_MODE));
g_scanner_scope_add_symbol (scanner, 0, "show-tabs",
GINT_TO_POINTER (SHOW_TABS));
g_scanner_scope_add_symbol (scanner, 0, "tabs-position",
GINT_TO_POINTER (TABS_POSITION));
g_scanner_scope_add_symbol (scanner, 0, "last-tip-shown",
GINT_TO_POINTER (LAST_TIP_SHOWN));
token = G_TOKEN_LEFT_PAREN;
while (g_scanner_peek_next_token (scanner) == token)
{
token = g_scanner_get_next_token (scanner);
switch (token)
{
case G_TOKEN_LEFT_PAREN:
token = G_TOKEN_SYMBOL;
break;
case G_TOKEN_SYMBOL:
if (scanner->value.v_symbol == GINT_TO_POINTER (SESSION_INFO))
{
GimpDialogFactory *factory = NULL;
GimpSessionInfo *info = NULL;
gchar *factory_name = NULL;
gchar *entry_name = NULL;
GimpDialogFactoryEntry *entry = NULL;
token = G_TOKEN_STRING;
if (! gimp_scanner_parse_string (scanner, &factory_name))
break;
/* In versions <= GIMP 2.6 there was a "toolbox", a
* "dock", a "display" and a "toplevel" factory. These
* are now merged to a single gimp_dialog_factory_get_singleton (). We
* need the legacy name though, so keep it around.
*/
factory = gimp_dialog_factory_get_singleton ();
info = gimp_session_info_new ();
/* GIMP 2.6 has the entry name as part of the
* session-info header, so try to get it
*/
gimp_scanner_parse_string (scanner, &entry_name);
if (entry_name)
{
/* Previously, GimpDock was a toplevel. That is why
* versions <= GIMP 2.6 has "dock" as the entry name. We
* want "dock" to be interpreted as 'dock window'
* however so have some special-casing for that. When
* the entry name is "dock" the factory name is either
* "dock" or "toolbox".
*/
if (strcmp (entry_name, "dock") == 0)
{
entry =
gimp_dialog_factory_find_entry (factory,
(strcmp (factory_name, "toolbox") == 0 ?
"gimp-toolbox-window" :
"gimp-dock-window"));
}
else
{
entry = gimp_dialog_factory_find_entry (factory,
entry_name);
}
}
/* We're done with these now */
g_free (factory_name);
g_free (entry_name);
/* We can get the factory entry either now (the GIMP <=
* 2.6 way), or when we deserialize (the GIMP 2.8 way)
*/
if (entry)
{
gimp_session_info_set_factory_entry (info, entry);
}
/* Always try to deserialize */
if (gimp_config_deserialize (GIMP_CONFIG (info), scanner, 1, NULL))
{
/* Make sure we got a factory entry either the 2.6
* or 2.8 way
*/
if (gimp_session_info_get_factory_entry (info))
{
GIMP_LOG (DIALOG_FACTORY,
"successfully parsed and added session info %p",
info);
gimp_dialog_factory_add_session_info (factory, info);
}
else
{
GIMP_LOG (DIALOG_FACTORY,
"failed to parse session info %p, not adding",
info);
}
g_object_unref (info);
}
else
{
g_object_unref (info);
/* set token to left paren to we won't set another
* error below, gimp_config_deserialize() already did
*/
token = G_TOKEN_LEFT_PAREN;
goto error;
}
}
else if (scanner->value.v_symbol == GINT_TO_POINTER (HIDE_DOCKS))
{
gboolean hide_docks;
token = G_TOKEN_IDENTIFIER;
if (! gimp_scanner_parse_boolean (scanner, &hide_docks))
break;
g_object_set (gimp->config,
"hide-docks", hide_docks,
NULL);
}
else if (scanner->value.v_symbol == GINT_TO_POINTER (SINGLE_WINDOW_MODE))
{
gboolean single_window_mode;
token = G_TOKEN_IDENTIFIER;
if (! gimp_scanner_parse_boolean (scanner, &single_window_mode))
break;
g_object_set (gimp->config,
"single-window-mode", single_window_mode,
NULL);
}
else if (scanner->value.v_symbol == GINT_TO_POINTER (SHOW_TABS))
{
gboolean show_tabs;
token = G_TOKEN_IDENTIFIER;
if (! gimp_scanner_parse_boolean (scanner, &show_tabs))
break;
g_object_set (gimp->config,
"show-tabs", show_tabs,
NULL);
}
else if (scanner->value.v_symbol == GINT_TO_POINTER (TABS_POSITION))
{
gint tabs_position;
token = G_TOKEN_INT;
if (! gimp_scanner_parse_int (scanner, &tabs_position))
break;
g_object_set (gimp->config,
"tabs-position", tabs_position,
NULL);
}
else if (scanner->value.v_symbol == GINT_TO_POINTER (LAST_TIP_SHOWN))
{
gint last_tip_shown;
token = G_TOKEN_INT;
if (! gimp_scanner_parse_int (scanner, &last_tip_shown))
break;
g_object_set (gimp->config,
"last-tip-shown", last_tip_shown,
NULL);
}
token = G_TOKEN_RIGHT_PAREN;
break;
case G_TOKEN_RIGHT_PAREN:
token = G_TOKEN_LEFT_PAREN;
break;
default: /* do nothing */
break;
}
}
error:
if (token != G_TOKEN_LEFT_PAREN)
{
g_scanner_get_next_token (scanner);
g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
_("fatal parse error"), TRUE);
}
if (error)
{
gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
g_clear_error (&error);
gimp_config_file_backup_on_error (file, "sessionrc", NULL);
}
gimp_scanner_destroy (scanner);
g_object_unref (file);
dialogs_load_recent_docks (gimp);
}
void
session_exit (Gimp *gimp)
{
g_return_if_fail (GIMP_IS_GIMP (gimp));
}
void
session_restore (Gimp *gimp,
GdkScreen *screen,
gint monitor)
{
g_return_if_fail (GIMP_IS_GIMP (gimp));
g_return_if_fail (GDK_IS_SCREEN (screen));
gimp_dialog_factory_restore (gimp_dialog_factory_get_singleton (),
screen, monitor);
/* make sure GimpImageWindow acts upon hide-docks at the right time,
* see bug #678043.
*/
if (GIMP_GUI_CONFIG (gimp->config)->single_window_mode &&
GIMP_GUI_CONFIG (gimp->config)->hide_docks)
{
g_object_notify (G_OBJECT (gimp->config), "hide-docks");
}
}
void
session_save (Gimp *gimp,
gboolean always_save)
{
GimpConfigWriter *writer;
GFile *file;
GError *error = NULL;
g_return_if_fail (GIMP_IS_GIMP (gimp));
if (sessionrc_deleted && ! always_save)
return;
file = session_file (gimp);
if (gimp->be_verbose)
g_print ("Writing '%s'\n", gimp_file_get_utf8_name (file));
writer =
gimp_config_writer_new_gfile (file,
TRUE,
"GIMP sessionrc\n\n"
"This file takes session-specific info "
"(that is info, you want to keep between "
"two GIMP sessions). You are not supposed "
"to edit it manually, but of course you "
"can do. The sessionrc will be entirely "
"rewritten every time you quit GIMP. "
"If this file isn't found, defaults are "
"used.",
NULL);
g_object_unref (file);
if (!writer)
return;
gimp_dialog_factory_save (gimp_dialog_factory_get_singleton (), writer);
gimp_config_writer_linefeed (writer);
gimp_config_writer_open (writer, "hide-docks");
gimp_config_writer_identifier (writer,
GIMP_GUI_CONFIG (gimp->config)->hide_docks ?
"yes" : "no");
gimp_config_writer_close (writer);
gimp_config_writer_open (writer, "single-window-mode");
gimp_config_writer_identifier (writer,
GIMP_GUI_CONFIG (gimp->config)->single_window_mode ?
"yes" : "no");
gimp_config_writer_close (writer);
gimp_config_writer_open (writer, "show-tabs");
gimp_config_writer_printf (writer,
GIMP_GUI_CONFIG (gimp->config)->show_tabs ?
"yes" : "no");
gimp_config_writer_close (writer);
gimp_config_writer_open (writer, "tabs-position");
gimp_config_writer_printf (writer, "%d",
GIMP_GUI_CONFIG (gimp->config)->tabs_position);
gimp_config_writer_close (writer);
gimp_config_writer_open (writer, "last-tip-shown");
gimp_config_writer_printf (writer, "%d",
GIMP_GUI_CONFIG (gimp->config)->last_tip_shown);
gimp_config_writer_close (writer);
if (! gimp_config_writer_finish (writer, "end of sessionrc", &error))
{
gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
g_clear_error (&error);
}
dialogs_save_recent_docks (gimp);
sessionrc_deleted = FALSE;
}
gboolean
session_clear (Gimp *gimp,
GError **error)
{
GFile *file;
GError *my_error = NULL;
gboolean success = TRUE;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
file = session_file (gimp);
if (! g_file_delete (file, NULL, &my_error) &&
my_error->code != G_IO_ERROR_NOT_FOUND)
{
success = FALSE;
g_set_error (error, GIMP_ERROR, GIMP_FAILED,
_("Deleting \"%s\" failed: %s"),
gimp_file_get_utf8_name (file), my_error->message);
}
else
{
sessionrc_deleted = TRUE;
}
g_clear_error (&my_error);
g_object_unref (file);
return success;
}
static GFile *
session_file (Gimp *gimp)
{
const gchar *basename;
gchar *filename;
GFile *file;
basename = g_getenv ("GIMP_TESTING_SESSIONRC_NAME");
if (! basename)
basename = "sessionrc";
if (gimp->session_name)
filename = g_strconcat (basename, ".", gimp->session_name, NULL);
else
filename = g_strdup (basename);
file = gimp_directory_file (filename, NULL);
g_free (filename);
return file;
}
|