2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1998-06-23 01:30:40 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2001-12-18 20:39:45 +08:00
|
|
|
* Session-managment stuff
|
|
|
|
* Copyright (C) 1998 Sven Neumann <sven@gimp.org>
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1998-06-23 01:30:40 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1998-06-23 01:30:40 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1998-06-23 01:30:40 +08:00
|
|
|
*/
|
|
|
|
|
1999-03-07 20:56:03 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
#include <errno.h>
|
2002-09-07 06:25:19 +08:00
|
|
|
#include <string.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-06-23 01:30:40 +08:00
|
|
|
|
2005-02-07 09:24:22 +08:00
|
|
|
#include <glib/gstdio.h>
|
1998-07-19 20:02:29 +08:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2001-05-22 04:30:16 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2005-01-26 03:11:26 +08:00
|
|
|
#include "libgimpconfig/gimpconfig.h"
|
2001-04-18 00:00:27 +08:00
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include "libgimpbase/gimpwin32-io.h"
|
|
|
|
#endif
|
|
|
|
|
2001-08-17 22:27:31 +08:00
|
|
|
#include "gui-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2005-01-25 06:41:24 +08:00
|
|
|
#include "config/gimpconfig-file.h"
|
2002-11-19 04:50:31 +08:00
|
|
|
#include "config/gimpguiconfig.h"
|
|
|
|
|
2001-10-24 00:23:32 +08:00
|
|
|
#include "core/gimp.h"
|
|
|
|
|
2001-04-17 02:49:29 +08:00
|
|
|
#include "widgets/gimpdialogfactory.h"
|
2003-10-10 23:59:12 +08:00
|
|
|
#include "widgets/gimpsessioninfo.h"
|
2001-04-17 02:49:29 +08:00
|
|
|
|
2008-05-17 00:06:42 +08:00
|
|
|
#include "dialogs/dialogs.h"
|
|
|
|
|
2001-04-18 05:43:29 +08:00
|
|
|
#include "session.h"
|
2009-09-21 01:08:48 +08:00
|
|
|
#include "gimp-log.h"
|
2001-04-18 05:43:29 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2002-06-03 03:48:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SESSION_INFO = 1,
|
2003-10-10 23:59:12 +08:00
|
|
|
LAST_TIP_SHOWN
|
|
|
|
};
|
2002-06-03 03:48:01 +08:00
|
|
|
|
2003-03-11 03:01:07 +08:00
|
|
|
|
2003-11-13 22:14:20 +08:00
|
|
|
static gchar * session_filename (Gimp *gimp);
|
|
|
|
|
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
/* private variables */
|
|
|
|
|
|
|
|
static gboolean sessionrc_deleted = FALSE;
|
|
|
|
|
|
|
|
|
2003-10-10 23:59:12 +08:00
|
|
|
/* public functions */
|
2002-06-03 03:48:01 +08:00
|
|
|
|
2001-04-18 00:00:27 +08:00
|
|
|
void
|
2001-10-24 00:23:32 +08:00
|
|
|
session_init (Gimp *gimp)
|
1998-06-23 01:30:40 +08:00
|
|
|
{
|
2002-06-03 03:48:01 +08:00
|
|
|
gchar *filename;
|
|
|
|
GScanner *scanner;
|
|
|
|
GTokenType token;
|
|
|
|
GError *error = NULL;
|
1998-07-12 06:23:23 +08:00
|
|
|
|
2001-10-24 00:23:32 +08:00
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
2003-11-13 22:14:20 +08:00
|
|
|
filename = session_filename (gimp);
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
scanner = gimp_scanner_new_file (filename, &error);
|
2000-03-25 06:10:28 +08:00
|
|
|
|
2003-11-05 05:58:39 +08:00
|
|
|
if (! scanner && error->code == GIMP_CONFIG_ERROR_OPEN_ENOENT)
|
|
|
|
{
|
|
|
|
g_clear_error (&error);
|
|
|
|
g_free (filename);
|
|
|
|
|
|
|
|
filename = g_build_filename (gimp_sysconf_directory (),
|
|
|
|
"sessionrc", NULL);
|
|
|
|
scanner = gimp_scanner_new_file (filename, NULL);
|
|
|
|
}
|
|
|
|
|
2002-06-03 03:48:01 +08:00
|
|
|
if (! scanner)
|
2000-03-25 06:10:28 +08:00
|
|
|
{
|
2003-11-05 05:58:39 +08:00
|
|
|
g_clear_error (&error);
|
2003-10-22 02:14:58 +08:00
|
|
|
g_free (filename);
|
2002-06-03 03:48:01 +08:00
|
|
|
return;
|
2000-03-25 06:10:28 +08:00
|
|
|
}
|
|
|
|
|
2005-11-07 06:01:25 +08:00
|
|
|
if (gimp->be_verbose)
|
|
|
|
g_print ("Parsing '%s'\n", gimp_filename_to_utf8 (filename));
|
|
|
|
|
2002-06-03 03:48:01 +08:00
|
|
|
g_scanner_scope_add_symbol (scanner, 0, "session-info",
|
|
|
|
GINT_TO_POINTER (SESSION_INFO));
|
|
|
|
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))
|
|
|
|
{
|
2010-01-20 05:23:23 +08:00
|
|
|
GimpDialogFactory *factory = NULL;
|
|
|
|
GimpSessionInfo *info = NULL;
|
|
|
|
gchar *factory_name = NULL;
|
|
|
|
gchar *entry_name = NULL;
|
|
|
|
GimpDialogFactoryEntry *entry = NULL;
|
2002-06-03 03:48:01 +08:00
|
|
|
|
2008-05-17 00:06:42 +08:00
|
|
|
token = G_TOKEN_STRING;
|
|
|
|
|
|
|
|
if (! gimp_scanner_parse_string (scanner, &factory_name))
|
|
|
|
break;
|
|
|
|
|
2010-02-20 16:26:57 +08:00
|
|
|
/* In versions <= GIMP 2.6 there was a "toolbox", a
|
2010-02-20 17:58:29 +08:00
|
|
|
* "dock", a "display" and a "toplevel" factory. These
|
2010-03-01 06:20:57 +08:00
|
|
|
* are now merged to a single gimp_dialog_factory_get_singleton (). We
|
2010-03-01 02:58:37 +08:00
|
|
|
* need the legacy name though, so keep it around.
|
2010-01-21 01:29:54 +08:00
|
|
|
*/
|
2010-03-01 06:20:57 +08:00
|
|
|
factory = gimp_dialog_factory_get_singleton ();
|
2008-05-17 00:06:42 +08:00
|
|
|
|
|
|
|
info = gimp_session_info_new ();
|
|
|
|
|
2010-02-20 19:13:55 +08:00
|
|
|
/* GIMP 2.6 has the entry name as part of the
|
|
|
|
* session-info header, so try to get it
|
2009-09-20 00:00:15 +08:00
|
|
|
*/
|
2010-02-20 19:13:55 +08:00
|
|
|
gimp_scanner_parse_string (scanner, &entry_name);
|
|
|
|
if (entry_name)
|
2010-01-20 05:23:23 +08:00
|
|
|
{
|
2010-02-20 19:13:55 +08:00
|
|
|
/* 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);
|
|
|
|
}
|
2008-05-17 00:06:42 +08:00
|
|
|
}
|
|
|
|
|
2010-01-20 05:23:23 +08:00
|
|
|
/* We're done with these now */
|
|
|
|
g_free (factory_name);
|
2008-07-22 14:51:59 +08:00
|
|
|
g_free (entry_name);
|
|
|
|
|
2010-02-20 19:13:55 +08:00
|
|
|
/* We can get the factory entry either now (the GIMP <=
|
|
|
|
* 2.6 way), or when we deserialize (the GIMP 2.8 way)
|
|
|
|
*/
|
2010-01-20 05:23:23 +08:00
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
gimp_session_info_set_factory_entry (info, entry);
|
|
|
|
}
|
|
|
|
|
2010-02-20 19:13:55 +08:00
|
|
|
/* Always try to deserialize */
|
2009-09-20 00:00:15 +08:00
|
|
|
if (gimp_config_deserialize (GIMP_CONFIG (info), scanner, 1, NULL))
|
2008-05-17 00:06:42 +08:00
|
|
|
{
|
2010-02-20 19:13:55 +08:00
|
|
|
/* Make sure we got a factory entry either the 2.6
|
|
|
|
* or 2.8 way
|
|
|
|
*/
|
|
|
|
if (gimp_session_info_get_factory_entry (info))
|
2008-05-17 00:06:42 +08:00
|
|
|
{
|
2009-09-21 01:08:48 +08:00
|
|
|
GIMP_LOG (DIALOG_FACTORY,
|
|
|
|
"successfully parsed and added session info %p",
|
|
|
|
info);
|
|
|
|
|
2009-12-21 03:19:31 +08:00
|
|
|
gimp_dialog_factory_add_session_info (factory, info);
|
2008-05-17 00:06:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-21 01:08:48 +08:00
|
|
|
GIMP_LOG (DIALOG_FACTORY,
|
|
|
|
"failed to parse session info %p, not adding",
|
|
|
|
info);
|
2008-05-17 00:06:42 +08:00
|
|
|
}
|
2011-05-13 03:12:59 +08:00
|
|
|
|
|
|
|
g_object_unref (info);
|
2008-05-17 00:06:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_object_unref (info);
|
|
|
|
break;
|
|
|
|
}
|
2002-06-03 03:48:01 +08:00
|
|
|
}
|
|
|
|
else if (scanner->value.v_symbol == GINT_TO_POINTER (LAST_TIP_SHOWN))
|
|
|
|
{
|
2006-04-12 20:49:29 +08:00
|
|
|
GimpGuiConfig *config = GIMP_GUI_CONFIG (gimp->config);
|
2002-11-19 04:50:31 +08:00
|
|
|
|
2002-06-03 03:48:01 +08:00
|
|
|
token = G_TOKEN_INT;
|
|
|
|
|
2002-11-19 04:50:31 +08:00
|
|
|
if (! gimp_scanner_parse_int (scanner, &config->last_tip))
|
2002-06-03 03:48:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
token = G_TOKEN_RIGHT_PAREN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case G_TOKEN_RIGHT_PAREN:
|
|
|
|
token = G_TOKEN_LEFT_PAREN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* do nothing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-11-04 20:33:09 +08:00
|
|
|
gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
|
2002-06-03 03:48:01 +08:00
|
|
|
g_clear_error (&error);
|
2003-10-22 02:14:58 +08:00
|
|
|
|
|
|
|
gimp_config_file_backup_on_error (filename, "sessionrc", NULL);
|
2002-06-03 03:48:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gimp_scanner_destroy (scanner);
|
2003-10-22 02:14:58 +08:00
|
|
|
g_free (filename);
|
2008-05-17 00:06:42 +08:00
|
|
|
|
|
|
|
dialogs_load_recent_docks (gimp);
|
2001-04-18 00:00:27 +08:00
|
|
|
}
|
2000-03-25 06:10:28 +08:00
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
void
|
|
|
|
session_exit (Gimp *gimp)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
}
|
|
|
|
|
2001-04-18 00:00:27 +08:00
|
|
|
void
|
2001-10-24 00:23:32 +08:00
|
|
|
session_restore (Gimp *gimp)
|
2001-04-18 00:00:27 +08:00
|
|
|
{
|
2001-10-24 00:23:32 +08:00
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
2010-03-01 06:20:57 +08:00
|
|
|
gimp_dialog_factory_restore (gimp_dialog_factory_get_singleton ());
|
1998-06-23 01:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-07-22 00:11:31 +08:00
|
|
|
session_save (Gimp *gimp,
|
|
|
|
gboolean always_save)
|
1998-06-23 01:30:40 +08:00
|
|
|
{
|
2003-03-11 03:01:07 +08:00
|
|
|
GimpConfigWriter *writer;
|
|
|
|
gchar *filename;
|
2006-10-10 02:49:15 +08:00
|
|
|
GError *error = NULL;
|
1998-06-23 01:30:40 +08:00
|
|
|
|
2001-10-24 00:23:32 +08:00
|
|
|
g_return_if_fail (GIMP_IS_GIMP (gimp));
|
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
if (sessionrc_deleted && ! always_save)
|
|
|
|
return;
|
|
|
|
|
2003-11-13 22:14:20 +08:00
|
|
|
filename = session_filename (gimp);
|
1998-06-23 01:30:40 +08:00
|
|
|
|
2005-11-07 06:01:25 +08:00
|
|
|
if (gimp->be_verbose)
|
|
|
|
g_print ("Writing '%s'\n", gimp_filename_to_utf8 (filename));
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
writer =
|
|
|
|
gimp_config_writer_new_file (filename,
|
2006-04-12 20:49:29 +08:00
|
|
|
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 "
|
2006-02-24 22:57:13 +08:00
|
|
|
"rewritten every time you quit GIMP. "
|
2003-09-12 23:14:02 +08:00
|
|
|
"If this file isn't found, defaults are "
|
|
|
|
"used.",
|
2006-04-12 20:49:29 +08:00
|
|
|
NULL);
|
2003-06-24 21:58:34 +08:00
|
|
|
g_free (filename);
|
2003-03-11 03:01:07 +08:00
|
|
|
|
|
|
|
if (!writer)
|
1999-03-07 20:56:03 +08:00
|
|
|
return;
|
1998-06-24 06:50:16 +08:00
|
|
|
|
2010-03-01 06:20:57 +08:00
|
|
|
gimp_dialog_factory_save (gimp_dialog_factory_get_singleton (), writer);
|
2003-03-11 03:01:07 +08:00
|
|
|
gimp_config_writer_linefeed (writer);
|
2001-04-17 02:49:29 +08:00
|
|
|
|
2009-09-17 06:20:39 +08:00
|
|
|
/* save last tip shown
|
|
|
|
*
|
|
|
|
* FIXME: Make last-tip-shown increment only when used within the
|
|
|
|
* session
|
|
|
|
*/
|
2003-03-11 03:01:07 +08:00
|
|
|
gimp_config_writer_open (writer, "last-tip-shown");
|
|
|
|
gimp_config_writer_printf (writer, "%d",
|
2009-09-17 06:20:39 +08:00
|
|
|
GIMP_GUI_CONFIG (gimp->config)->last_tip);
|
2003-03-11 03:01:07 +08:00
|
|
|
gimp_config_writer_close (writer);
|
2000-02-12 23:01:33 +08:00
|
|
|
|
2006-10-10 02:49:15 +08:00
|
|
|
if (! gimp_config_writer_finish (writer, "end of sessionrc", &error))
|
|
|
|
{
|
2008-11-04 20:33:09 +08:00
|
|
|
gimp_message_literal (gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
|
2006-10-10 02:49:15 +08:00
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
2004-07-22 00:11:31 +08:00
|
|
|
|
2008-05-17 00:06:42 +08:00
|
|
|
dialogs_save_recent_docks (gimp);
|
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
sessionrc_deleted = FALSE;
|
1998-06-23 01:30:40 +08:00
|
|
|
}
|
2002-06-03 03:48:01 +08:00
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
gboolean
|
|
|
|
session_clear (Gimp *gimp,
|
|
|
|
GError **error)
|
2003-04-08 18:02:42 +08:00
|
|
|
{
|
2004-07-22 00:11:31 +08:00
|
|
|
gchar *filename;
|
|
|
|
gboolean success = TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
filename = session_filename (gimp);
|
|
|
|
|
2005-02-07 09:24:22 +08:00
|
|
|
if (g_unlink (filename) != 0 && errno != ENOENT)
|
2004-07-22 00:11:31 +08:00
|
|
|
{
|
2008-11-12 18:56:06 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Deleting \"%s\" failed: %s"),
|
2004-07-22 00:11:31 +08:00
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
|
|
|
success = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sessionrc_deleted = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (filename);
|
2003-04-08 18:02:42 +08:00
|
|
|
|
2004-07-22 00:11:31 +08:00
|
|
|
return success;
|
2003-04-08 18:02:42 +08:00
|
|
|
}
|
2003-11-13 22:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
session_filename (Gimp *gimp)
|
|
|
|
{
|
2011-05-21 01:06:57 +08:00
|
|
|
const gchar *basename;
|
|
|
|
gchar *filename;
|
2011-05-16 13:11:44 +08:00
|
|
|
|
2011-05-21 01:06:57 +08:00
|
|
|
basename = g_getenv ("GIMP_TESTING_SESSIONRC_NAME");
|
|
|
|
if (! basename)
|
|
|
|
basename = "sessionrc";
|
2011-05-16 13:11:44 +08:00
|
|
|
|
2011-05-21 01:06:57 +08:00
|
|
|
filename = gimp_personal_rc_file (basename);
|
2003-11-13 22:14:20 +08:00
|
|
|
|
|
|
|
if (gimp->session_name)
|
|
|
|
{
|
2011-05-21 01:06:57 +08:00
|
|
|
gchar *tmp = g_strconcat (filename, ".", gimp->session_name, NULL);
|
2003-11-13 22:14:20 +08:00
|
|
|
|
2011-05-21 01:06:57 +08:00
|
|
|
g_free (filename);
|
|
|
|
filename = tmp;
|
2003-11-13 22:14:20 +08:00
|
|
|
}
|
|
|
|
|
2011-05-21 01:06:57 +08:00
|
|
|
return filename;
|
2003-11-13 22:14:20 +08:00
|
|
|
}
|