Commit 73446415 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann

added new convenience function gimp_xml_parser_parse_fd().

2004-09-28  Sven Neumann  <sven@gimp.org>

	* app/config/gimpxmlparser.[ch]: added new convenience function
	gimp_xml_parser_parse_fd().

	* app/file/Makefile.am
	* app/file/gimprecentitem.[ch]
	* app/file/gimprecentlist.[ch]: added an implementation of the
	recent-files spec as found on freedesktop.org. This code is taken
	from libegg and has been edited to fit the GIMP needs.

	* app/file/file-open.c
	* app/file/file-save.c: update the ~/.recently-used file. Fixes
	bug #131206.
parent 2b45c7a3
2004-09-28 Sven Neumann <sven@gimp.org>
* app/config/gimpxmlparser.[ch]: added new convenience function
gimp_xml_parser_parse_fd().
* app/file/Makefile.am
* app/file/gimprecentitem.[ch]
* app/file/gimprecentlist.[ch]: added an implementation of the
recent-files spec as found on freedesktop.org. This code is taken
from libegg and has been edited to fit the GIMP needs.
* app/file/file-open.c
* app/file/file-save.c: update the ~/.recently-used file. Fixes
bug #131206.
2004-09-28 Michael Natterer <mitch@gimp.org>
* app/widgets/gimpcontainerbox.c (gimp_container_box_get_preview):
......
......@@ -101,6 +101,37 @@ gimp_xml_parser_parse_file (GimpXmlParser *parser,
return success;
}
/**
* gimp_xml_parser_parse_fd:
* @parser: a #GimpXmlParser
* @fd: a file descriptor
* @error: return location for possible errors
*
* This function creates a GIOChannel for @fd and calls
* gimp_xml_parser_parse_io_channel() for you.
*
* Return value: %TRUE on success, %FALSE otherwise
**/
gboolean
gimp_xml_parser_parse_fd (GimpXmlParser *parser,
gint fd,
GError **error)
{
GIOChannel *io;
gboolean success;
g_return_val_if_fail (parser != NULL, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
io = g_io_channel_unix_new (fd);
success = gimp_xml_parser_parse_io_channel (parser, io, error);
g_io_channel_unref (io);
return success;
}
/**
* gimp_xml_parser_parse_io_channel:
* @parser: a #GimpXmlParser
......
......@@ -28,6 +28,9 @@ GimpXmlParser * gimp_xml_parser_new (const GMarkupParser *markup_pa
gboolean gimp_xml_parser_parse_file (GimpXmlParser *parser,
const gchar *filename,
GError **error);
gboolean gimp_xml_parser_parse_fd (GimpXmlParser *parser,
gint fd,
GError **error);
gboolean gimp_xml_parser_parse_io_channel (GimpXmlParser *parser,
GIOChannel *io,
GError **error);
......
......@@ -3,12 +3,16 @@
noinst_LIBRARIES = libappfile.a
libappfile_a_SOURCES = \
file-open.c \
file-open.h \
file-save.c \
file-save.h \
file-utils.c \
file-utils.h
file-open.c \
file-open.h \
file-save.c \
file-save.h \
file-utils.c \
file-utils.h \
gimprecentitem.c \
gimprecentitem.h \
gimprecentlist.c \
gimprecentlist.h
AM_CPPFLAGS = \
-DG_LOG_DOMAIN=\"Gimp-File\" \
......
......@@ -59,6 +59,7 @@
#include "file-open.h"
#include "file-utils.h"
#include "gimprecentlist.h"
#include "gimp-intl.h"
......@@ -250,6 +251,8 @@ file_open_with_proc_and_display (Gimp *gimp,
gimp_imagefile_save_thumbnail (imagefile, gimage);
}
gimp_recent_list_add_uri (uri, mime_type);
/* the display owns the image now */
g_object_unref (gimage);
}
......
......@@ -59,6 +59,7 @@
#include "file-save.h"
#include "file-utils.h"
#include "gimprecentlist.h"
#include "gimp-intl.h"
......@@ -220,6 +221,8 @@ file_save_as (GimpImage *gimage,
GIMP_OBJECT (gimage)->name = saved_uri;
}
gimp_recent_list_add_uri (uri, file_proc->mime_type);
}
else if (status != GIMP_PDB_CANCEL)
{
......
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Recent File Storage,
* see http://freedesktop.org/Standards/recent-file-spec/
*
* This code is taken from libegg and has been adapted to the GIMP needs.
* The original author is James Willcox <jwillcox@cs.indiana.edu>,
* responsible for bugs in this version is 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <time.h>
#include <glib-object.h>
#include "gimprecentitem.h"
struct _GimpRecentItem
{
gchar *uri;
gchar *mime_type;
time_t timestamp;
gboolean private_data;
GList *groups;
gint refcount;
};
GimpRecentItem *
gimp_recent_item_new (void)
{
GimpRecentItem *item;
item = g_new (GimpRecentItem, 1);
item->groups = NULL;
item->private_data = FALSE;
item->uri = NULL;
item->mime_type = NULL;
item->refcount = 1;
return item;
}
static void
gimp_recent_item_free (GimpRecentItem *item)
{
if (item->uri)
g_free (item->uri);
if (item->mime_type)
g_free (item->mime_type);
if (item->groups)
{
g_list_foreach (item->groups, (GFunc) g_free, NULL);
g_list_free (item->groups);
item->groups = NULL;
}
g_free (item);
}
GimpRecentItem *
gimp_recent_item_ref (GimpRecentItem *item)
{
item->refcount++;
return item;
}
GimpRecentItem *
gimp_recent_item_unref (GimpRecentItem *item)
{
item->refcount--;
if (item->refcount == 0)
gimp_recent_item_free (item);
return item;
}
GimpRecentItem *
gimp_recent_item_new_from_uri (const gchar *uri)
{
GimpRecentItem *item;
g_return_val_if_fail (uri != NULL, NULL);
item = gimp_recent_item_new ();
if (!gimp_recent_item_set_uri (item ,uri))
{
gimp_recent_item_free (item);
return NULL;
}
return item;
}
gboolean
gimp_recent_item_set_uri (GimpRecentItem *item,
const gchar *uri)
{
gchar *utf8_uri = g_filename_to_utf8 (uri, -1, NULL, NULL, NULL);
if (! utf8_uri)
{
g_warning ("%s: URI can't be converted to UTF-8", G_STRFUNC);
return FALSE;
}
g_free (utf8_uri);
if (item->uri)
g_free (item->uri);
item->uri = g_strdup (uri);
return TRUE;
}
const gchar *
gimp_recent_item_get_uri (const GimpRecentItem *item)
{
return item->uri;
}
gchar *
gimp_recent_item_get_uri_utf8 (const GimpRecentItem *item)
{
/* this could fail, but it's not likely, since we've already done it
* once in set_uri()
*/
return g_filename_to_utf8 (item->uri, -1, NULL, NULL, NULL);
}
void
gimp_recent_item_set_mime_type (GimpRecentItem *item,
const gchar *mime)
{
if (item->mime_type)
g_free (item->mime_type);
item->mime_type = g_strdup (mime);
}
const gchar *
gimp_recent_item_get_mime_type (const GimpRecentItem *item)
{
return item->mime_type;
}
void
gimp_recent_item_set_timestamp (GimpRecentItem *item,
time_t timestamp)
{
if (timestamp == (time_t) -1)
time (&timestamp);
item->timestamp = timestamp;
}
time_t
gimp_recent_item_get_timestamp (const GimpRecentItem *item)
{
return item->timestamp;
}
const GList *
gimp_recent_item_get_groups (const GimpRecentItem *item)
{
return item->groups;
}
gboolean
gimp_recent_item_in_group (const GimpRecentItem *item,
const gchar *group_name)
{
GList *tmp = item->groups;
for (tmp = item->groups; tmp; tmp = tmp->next)
{
const gchar *value = tmp->data;
if (strcmp (group_name, value) == 0)
return TRUE;
}
return FALSE;
}
void
gimp_recent_item_add_group (GimpRecentItem *item,
const gchar *group_name)
{
g_return_if_fail (group_name != NULL);
if (!gimp_recent_item_in_group (item, group_name))
item->groups = g_list_append (item->groups, g_strdup (group_name));
}
void
gimp_recent_item_remove_group (GimpRecentItem *item,
const gchar *group_name)
{
GList *tmp;
g_return_if_fail (group_name != NULL);
for (tmp = item->groups; tmp; tmp = tmp->next)
{
gchar *value = tmp->data;
if (strcmp (group_name, value) == 0)
{
item->groups = g_list_remove (item->groups, value);
g_free (value);
break;
}
}
}
void
gimp_recent_item_set_private (GimpRecentItem *item,
gboolean priv)
{
item->private_data = priv;
}
gboolean
gimp_recent_item_get_private (const GimpRecentItem *item)
{
return item->private_data;
}
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Recent File Storage,
* see http://freedesktop.org/Standards/recent-file-spec/
*
* This code is taken from libegg and has been adapted to the GIMP needs.
* The original author is James Willcox <jwillcox@cs.indiana.edu>,
* responsible for bugs in this version is 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __GIMP_RECENT_ITEM_H__
#define __GIMP_RECENT_ITEM_H__
typedef struct _GimpRecentItem GimpRecentItem;
GimpRecentItem * gimp_recent_item_new (void);
GimpRecentItem * gimp_recent_item_ref (GimpRecentItem *item);
GimpRecentItem * gimp_recent_item_unref (GimpRecentItem *item);
GimpRecentItem * gimp_recent_item_new_from_uri (const gchar *uri);
gboolean gimp_recent_item_set_uri (GimpRecentItem *item,
const gchar *uri);
const gchar * gimp_recent_item_get_uri (const GimpRecentItem *item);
gchar * gimp_recent_item_get_uri_utf8 (const GimpRecentItem *item);
void gimp_recent_item_set_mime_type (GimpRecentItem *item,
const gchar *mime);
const gchar * gimp_recent_item_get_mime_type (const GimpRecentItem *item);
void gimp_recent_item_set_timestamp (GimpRecentItem *item,
time_t timestamp);
time_t gimp_recent_item_get_timestamp (const GimpRecentItem *item);
/* groups */
const GList * gimp_recent_item_get_groups (const GimpRecentItem *item);
gboolean gimp_recent_item_in_group (const GimpRecentItem *item,
const gchar *group_name);
void gimp_recent_item_add_group (GimpRecentItem *item,
const gchar *group_name);
void gimp_recent_item_remove_group (GimpRecentItem *item,
const gchar *group_name);
void gimp_recent_item_set_private (GimpRecentItem *item,
gboolean priv);
gboolean gimp_recent_item_get_private (const GimpRecentItem *item);
#endif /* __GIMP_RECENT_ITEM_H__ */
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* Recent File Storage,
* see http://freedesktop.org/Standards/recent-file-spec/
*
* This code is taken from libegg and has been adapted to the GIMP needs.
* The original author is James Willcox <jwillcox@cs.indiana.edu>,
* responsible for bugs in this version is 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib-object.h>
#ifdef G_OS_WIN32
#include "libgimpbase/gimpwin32-io.h"
#endif
#include "config/config-types.h"
#include "config/gimpxmlparser.h"
#include "gimprecentitem.h"
#include "gimprecentlist.h"
#define GIMP_RECENT_LIST_FILE_NAME ".recently-used"
#define GIMP_RECENT_LIST_MAX_ITEMS 500
#define GIMP_RECENT_LIST_GROUP_GIMP "gimp"
#define GIMP_RECENT_ITEM_LIST_UNREF(list) \
g_list_foreach (list, (GFunc) gimp_recent_item_unref, NULL); \
g_list_free (list);
typedef struct
{
GSList *states;
GList *items;
GimpRecentItem *current_item;
} ParseInfo;
typedef enum
{
STATE_START,
STATE_RECENT_FILES,
STATE_RECENT_ITEM,
STATE_URI,
STATE_MIME_TYPE,
STATE_TIMESTAMP,
STATE_PRIVATE,
STATE_GROUPS,
STATE_GROUP
} ParseState;
#define TAG_RECENT_FILES "RecentFiles"
#define TAG_RECENT_ITEM "RecentItem"
#define TAG_URI "URI"
#define TAG_MIME_TYPE "Mime-Type"
#define TAG_TIMESTAMP "Timestamp"
#define TAG_PRIVATE "Private"
#define TAG_GROUPS "Groups"
#define TAG_GROUP "Group"
static void start_element_handler (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error);
static void end_element_handler (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error);
static void text_handler (GMarkupParseContext *context,
const gchar *text,
gsize text_len,
gpointer user_data,
GError **error);
static const GMarkupParser markup_parser =
{
start_element_handler,
end_element_handler,
text_handler,
NULL,
NULL
};
static void
gimp_recent_list_add_new_groups (GimpRecentItem *item,
GimpRecentItem *upd_item)
{
const GList *tmp;
for (tmp = gimp_recent_item_get_groups (upd_item); tmp; tmp = tmp->next)
{
const gchar *group = tmp->data;
if (! gimp_recent_item_in_group (item, group))
gimp_recent_item_add_group (item, group);
}
}
static gboolean
gimp_recent_list_update_item (GList *items,
GimpRecentItem *upd_item)
{
const char *uri = gimp_recent_item_get_uri (upd_item);
GList *tmp;
for (tmp = items; tmp; tmp = tmp->next)
{
GimpRecentItem *item = tmp->data;
/* gnome_vfs_uris_match (gimp_recent_item_get_uri (item), uri) */
if (strcmp (gimp_recent_item_get_uri (item), uri) == 0)
{
gimp_recent_item_set_timestamp (item, (time_t) -1);
gimp_recent_list_add_new_groups (item, upd_item);
return TRUE;
}
}
return FALSE;
}
static void
parse_info_init (ParseInfo *info)
{
info->states = g_slist_prepend (NULL, STATE_START);
info->items = NULL;
}
static void
parse_info_free (ParseInfo *info)
{
g_slist_free (info->states);
}
static void
push_state (ParseInfo *info,
ParseState state)
{
info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
}
static void
pop_state (ParseInfo *info)
{
g_return_if_fail (info->states != NULL);
info->states = g_slist_remove (info->states, info->states->data);
}
static ParseState
peek_state (ParseInfo *info)
{
g_return_val_if_fail (info->states != NULL, STATE_START);
return GPOINTER_TO_INT (info->states->data);
}
#define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
static void
start_element_handler (GMarkupParseContext *context,
const gchar *element_name,
const gchar **attribute_names,
const gchar **attribute_values,
gpointer user_data,
GError **error)
{
ParseInfo *info = user_data;
if (ELEMENT_IS (TAG_RECENT_FILES))
{
push_state (info, STATE_RECENT_FILES);
}
else if (ELEMENT_IS (TAG_RECENT_ITEM))
{
info->current_item = gimp_recent_item_new ();
push_state (info, STATE_RECENT_ITEM);
}
else if (ELEMENT_IS (TAG_URI))
{
push_state (info, STATE_URI);
}
else if (ELEMENT_IS (TAG_MIME_TYPE))
{
push_state (info, STATE_MIME_TYPE);
}
else if (ELEMENT_IS (TAG_TIMESTAMP))
{
push_state (info, STATE_TIMESTAMP);
}
else if (ELEMENT_IS (TAG_PRIVATE))
{
push_state (info, STATE_PRIVATE);
gimp_recent_item_set_private (info->current_item, TRUE);
}
else if (ELEMENT_IS (TAG_GROUPS))
{
push_state (info, STATE_GROUPS);
}
else if (ELEMENT_IS (TAG_GROUP))
{
push_state (info, STATE_GROUP);
}
}
static void
end_element_handler (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
ParseInfo *info