applied patch from Shlomi Fish that fixes confusion of filenames and

2004-07-06  Sven Neumann  <sven@gimp.org>

	* plug-ins/gimpressionist: applied patch from Shlomi Fish that
	fixes confusion of filenames and user-visible object names (bug
	#132621). Also removed function remove_trailing_whitespace() that
	used to duplicate functionality from GLib and updated
	preset_create_filename().
This commit is contained in:
Sven Neumann 2004-07-06 14:17:17 +00:00 committed by Sven Neumann
parent 3b7fc27b5e
commit cc09735a20
10 changed files with 305 additions and 50 deletions

View File

@ -1,3 +1,11 @@
2004-07-06 Sven Neumann <sven@gimp.org>
* plug-ins/gimpressionist: applied patch from Shlomi Fish that
fixes confusion of filenames and user-visible object names (bug
#132621). Also removed function remove_trailing_whitespace() that
used to duplicate functionality from GLib and updated
preset_create_filename().
2004-07-06 Michael Natterer <mitch@gimp.org>
* app/widgets/gimppreviewrenderer.c

View File

@ -46,8 +46,9 @@ gimpressionist_sources = \
ppmtool.c \
ppmtool.h \
presets.c \
preview.h \
presets.h \
preview.c \
preview.h \
repaint.c \
size.c \
sizemap.c \

View File

@ -19,6 +19,7 @@
#include "gimpressionist.h"
#include "ppmtool.h"
#include "brush.h"
#include "presets.h"
#include <libgimp/stdplugins-intl.h>

View File

@ -10,6 +10,7 @@
#include "gimpressionist.h"
#include "preview.h"
#include "brush.h"
#include "presets.h"
#include "libgimp/stdplugins-intl.h"
@ -177,6 +178,7 @@ run (const gchar *name,
reloadbrush(NULL, NULL);
preview_free_resources();
brush_free();
preset_free();
values[0].data.d_status = status;

View File

@ -13,6 +13,7 @@
#include "placement.h"
#include "preview.h"
#include "size.h"
#include "presets.h"
#include "ppmtool.h"

View File

@ -109,6 +109,12 @@ enum ORIENTATION_ENUM
ORIENTATION_MANUAL = 7,
};
enum PRESETS_LIST_COLUMN_ENUM
{
PRESETS_LIST_COLUMN_FILENAME = 0,
PRESETS_LIST_COLUMN_OBJECT_NAME = 1,
};
/* Globals */
extern char *standalone;
@ -129,15 +135,12 @@ extern GtkObject *colornoiseadjust;
extern GtkWidget *previewbutton;
extern GtkWidget *presetsavebutton;
extern gboolean img_has_alpha;
extern GRand *gr;
/* Prototypes */
void remove_trailing_whitespace(char *buffer);
GList *parsepath(void);
void free_parsepath_cache(void);
@ -145,7 +148,6 @@ void create_paperpage(GtkNotebook *);
void create_brushpage(GtkNotebook *);
void create_orientationpage(GtkNotebook *);
void create_generalpage(GtkNotebook *);
void create_presetpage(GtkNotebook *);
void create_placementpage(GtkNotebook *);
void create_colorpage(GtkNotebook *);
@ -157,6 +159,11 @@ gchar *findfile(const gchar *);
void unselectall(GtkWidget *list);
void reselect(GtkWidget *list, char *fname);
void readdirintolist(char *subdir, GtkWidget *view, char *selected);
void readdirintolist_extended(char *subdir, GtkWidget *view, char *selected,
gboolean with_filename_column,
gchar *(*get_object_name_cb)
(gchar *dir, gchar *filename, void *context),
void * context);
void orientation_restore(void);
void paper_store(void);
void paper_restore(void);
@ -175,14 +182,7 @@ void create_sizemap_dialog(void);
double getsiz_proto (double x, double y, int n, smvector_t *vec,
double smstrexp, int voronoi);
enum SELECT_PRESET_RETURN_VALUES
{
SELECT_PRESET_OK = 0,
SELECT_PRESET_FILE_NOT_FOUND = -1,
SELECT_PRESET_LOAD_FAILED = -2,
};
int select_preset(const gchar *preset);
void set_colorbrushes (const gchar *fn);
int create_gimpressionist (void);

View File

@ -24,7 +24,7 @@ int readline(FILE *f, char *buffer, int len)
}
while (buffer[0] == '#');
remove_trailing_whitespace(buffer);
g_strchomp (buffer);
return 0;
}

View File

@ -15,6 +15,7 @@
#include <libgimp/gimpui.h>
#include "gimpressionist.h"
#include "presets.h"
#include "libgimp/stdplugins-intl.h"
@ -22,10 +23,20 @@
#include "libgimpbase/gimpwin32-io.h"
#endif
#define PRESETMAGIC "Preset"
static GtkWidget *presetnameentry = NULL;
static GtkWidget *presetlist = NULL;
static GtkWidget *presetdesclabel = NULL;
static GtkListStore *store;
static gchar *selected_preset_orig_name = NULL;
static gchar *selected_preset_filename = NULL;
void preset_free(void)
{
g_free (selected_preset_orig_name);
g_free (selected_preset_filename);
}
static void set_preset_description_text (const gchar *text)
{
@ -36,23 +47,128 @@ static char presetdesc[4096] = "";
static char *factory_defaults = "<Factory defaults>";
static gchar *
get_object_name(gchar *dir, gchar *filename, void *context)
{
gchar *ret = NULL;
gchar *full_path = NULL;
FILE *f;
char line[1024] = "";
int line_idx;
/* First try to extract the object's name (= user-friendly description)
* from the preset file
* */
full_path = g_build_filename (dir, filename, NULL);
f = fopen (full_path, "rt");
if (f)
{
/* Skip the preset magic. */
fgets (line, 10, f);
if (!strncmp(line, PRESETMAGIC, 4))
{
for (line_idx = 0; line_idx<5; line_idx++)
{
if (!fgets(line, sizeof(line), f))
break;
g_strchomp (line);
if (!strncmp (line, "name=", 5))
{
ret = g_strcompress (line+5);
break;
}
}
}
fclose (f);
}
/* The object name defaults to a filename-derived description */
if (! ret)
{
ret = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
}
g_free (full_path);
return ret;
}
static void
preset_read_dir_into_list (void)
{
readdirintolist_extended ("Presets", presetlist, NULL, TRUE,
get_object_name, NULL);
}
static gchar *
preset_create_filename (const gchar *basename,
const gchar *dest_dir)
{
gchar *filename;
gchar *fullpath;
gchar *safe_name;
gint i;
gint unum = 1;
g_return_val_if_fail (basename != NULL, NULL);
g_return_val_if_fail (dest_dir != NULL, NULL);
g_return_val_if_fail (g_path_is_absolute (dest_dir), NULL);
safe_name = g_filename_from_utf8 (basename, -1, NULL, NULL, NULL);
if (safe_name[0] == '.')
safe_name[0] = '-';
for (i = 0; safe_name[i]; i++)
if (safe_name[i] == G_DIR_SEPARATOR || g_ascii_isspace (safe_name[i]))
safe_name[i] = '-';
filename = g_strdup (safe_name);
fullpath = g_build_filename (dest_dir, filename, NULL);
g_free (filename);
while (g_file_test (fullpath, G_FILE_TEST_EXISTS))
{
g_free (fullpath);
filename = g_strdup_printf ("%s-%d",
safe_name,
unum++);
fullpath = g_build_filename (dest_dir, filename, NULL);
g_free (filename);
}
g_free (safe_name);
return fullpath;
}
static void addfactorydefaults (void)
{
GtkTreeIter iter;
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, factory_defaults, -1);
/* Set the filename. */
gtk_list_store_set (store, &iter, PRESETS_LIST_COLUMN_FILENAME,
factory_defaults, -1);
/* Set the object name. */
gtk_list_store_set (store, &iter, PRESETS_LIST_COLUMN_OBJECT_NAME,
factory_defaults, -1);
}
static void presetsrefresh(void)
{
gtk_list_store_clear (store);
addfactorydefaults ();
readdirintolist ("Presets", presetlist, NULL);
preset_read_dir_into_list ();
}
#define PRESETMAGIC "Preset"
static int loadoldpreset (const gchar *fname)
{
FILE *f;
@ -271,8 +387,8 @@ static int loadpreset(const gchar *fn)
char *tmps;
if(!fgets(line,1024,f))
break;
remove_trailing_whitespace(line);
tmps = strchr(line, '=');
g_strchomp (line);
tmps = strchr (line, '=');
if(!tmps)
continue;
*tmps = '\0';
@ -331,13 +447,16 @@ static void applypreset(GtkWidget *w, GtkTreeSelection *selection)
{
gchar *preset;
gtk_tree_model_get (model, &iter, 0, &preset, -1);
gtk_tree_model_get (model, &iter, PRESETS_LIST_COLUMN_FILENAME,
&preset, -1);
select_preset(preset);
restorevals ();
g_free (preset);
/* g_free (preset); */
g_free (selected_preset_filename);
selected_preset_filename = preset;
}
}
@ -350,7 +469,8 @@ static void deletepreset(GtkWidget *w, GtkTreeSelection *selection)
{
gchar *preset;
gtk_tree_model_get (model, &iter, 0, &preset, -1);
gtk_tree_model_get (model, &iter, PRESETS_LIST_COLUMN_FILENAME,
&preset, -1);
if (preset)
{
@ -460,7 +580,7 @@ create_savepreset (void)
static void savepreset(void)
{
const gchar *l;
const gchar *preset_name;
gchar *fname, *presets_dir_path;
FILE *f;
GList *thispath;
@ -468,9 +588,9 @@ static void savepreset(void)
gchar vbuf[6][G_ASCII_DTOSTR_BUF_SIZE];
guchar color[3];
gint i;
gchar *desc_escaped;
gchar *desc_escaped, *preset_name_escaped;
l = gtk_entry_get_text (GTK_ENTRY (presetnameentry));
preset_name = gtk_entry_get_text (GTK_ENTRY (presetnameentry));
thispath = parsepath ();
storevals ();
@ -498,9 +618,28 @@ static void savepreset(void)
}
}
fname = g_build_filename (presets_dir_path, l, NULL);
/* Check if the user-friendly name has changed. If so, then save it under
* a new file. If not - use the same file name.
* */
if (!strcmp (preset_name, selected_preset_orig_name))
{
fname = g_build_filename (presets_dir_path,
selected_preset_filename,
NULL);
}
else
{
fname = preset_create_filename(preset_name, presets_dir_path);
}
g_free (presets_dir_path);
if (!fname)
{
g_printerr ("Error building a filename for preset \"%s\"!\n",
preset_name);
return;
}
f = fopen (fname, "wt");
if (!f)
{
@ -513,6 +652,9 @@ static void savepreset(void)
desc_escaped = g_strescape (presetdesc, NULL);
fprintf(f, "desc=%s\n", desc_escaped);
g_free (desc_escaped);
preset_name_escaped = g_strescape (preset_name, NULL);
fprintf(f, "name=%s\n", preset_name_escaped);
g_free (preset_name_escaped);
fprintf(f, "orientnum=%d\n", pcvals.orientnum);
fprintf(f, "orientfirst=%s\n",
g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", pcvals.orientfirst));
@ -657,14 +799,76 @@ static void selectpreset(GtkTreeSelection *selection, gpointer data)
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
gchar *preset;
gchar *preset_name;
gchar *preset_filename;
gtk_tree_model_get (model, &iter, 0, &preset, -1);
if(strcmp(preset, factory_defaults))
gtk_entry_set_text (GTK_ENTRY(presetnameentry), preset);
readdesc (preset);
g_free (preset);
gtk_tree_model_get (model, &iter, PRESETS_LIST_COLUMN_OBJECT_NAME,
&preset_name, -1);
gtk_tree_model_get (model, &iter, PRESETS_LIST_COLUMN_FILENAME,
&preset_filename, -1);
/* TODO : Maybe make the factory defaults behavior in regards
* to the preset's object name and filename more robust?
* */
if(strcmp(preset_filename, factory_defaults))
{
gtk_entry_set_text (GTK_ENTRY(presetnameentry), preset_name);
g_free (selected_preset_orig_name);
g_free (selected_preset_filename);
selected_preset_orig_name = g_strdup (preset_name);
selected_preset_filename = g_strdup(selected_preset_filename);
}
readdesc (preset_filename);
g_free (preset_name);
g_free (preset_filename);
}
}
static GtkWidget *
create_presets_list(GtkWidget *parent,
void (*changed_cb)
(GtkTreeSelection *selection, gpointer data))
{
GtkListStore *store;
GtkTreeSelection *selection;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
GtkWidget *swin, *view;
swin = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(swin),
GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (parent), swin, FALSE, FALSE, 0);
gtk_widget_show (swin);
gtk_widget_set_size_request(swin, 150,-1);
store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);
g_object_unref (store);
gtk_widget_show (view);
renderer = gtk_cell_renderer_text_new ();
column =
gtk_tree_view_column_new_with_attributes ("Preset", renderer,
"text",
PRESETS_LIST_COLUMN_OBJECT_NAME,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);
gtk_container_add (GTK_CONTAINER (swin), view);
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
g_signal_connect (selection, "changed", G_CALLBACK (changed_cb),
NULL);
return view;
}
void create_presetpage(GtkNotebook *notebook)
@ -701,7 +905,7 @@ void create_presetpage(GtkNotebook *notebook)
gtk_box_pack_start(GTK_BOX (thispage), box1, TRUE, TRUE, 0);
gtk_widget_show (box1);
presetlist = view = createonecolumnlist (box1, selectpreset);
presetlist = view = create_presets_list (box1, selectpreset);
store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (view)));
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
addfactorydefaults ();
@ -749,7 +953,7 @@ void create_presetpage(GtkNotebook *notebook)
gtk_box_pack_start(GTK_BOX (vbox), tmpw, TRUE, TRUE, 0);
gtk_widget_show(tmpw);
readdirintolist("Presets", view, NULL);
preset_read_dir_into_list ();
gtk_notebook_append_page_menu (notebook, thispage, label, NULL);
}

View File

@ -0,0 +1,17 @@
#ifndef __PRESETS_H
#define __PRESETS_H
enum SELECT_PRESET_RETURN_VALUES
{
SELECT_PRESET_OK = 0,
SELECT_PRESET_FILE_NOT_FOUND = -1,
SELECT_PRESET_LOAD_FAILED = -2,
};
void create_presetpage (GtkNotebook *);
extern GtkWidget *presetsavebutton;
int select_preset (const gchar *preset);
void preset_free (void);
#endif

View File

@ -91,18 +91,6 @@ double getsiz_proto (double x, double y, int n, smvector_t *vec,
/* String and Path Manipulation Routines */
void remove_trailing_whitespace(char *buffer)
{
char * ptr;
/*
* Note: there is some reliance on the ASCII character code
* characteristics here.
* */
ptr = buffer + strlen(buffer)-1;
while ((ptr > buffer) && ((*ptr) <= ' '))
*(ptr--) = '\0';
}
static GList *parsepath_cached_path = NULL;
@ -230,7 +218,10 @@ reselect (GtkWidget *view,
}
static void readdirintolist_real(char *subdir, GtkWidget *view,
char *selected)
char *selected, gboolean with_filename_column,
gchar *(*get_object_name_cb)
(gchar *dir, gchar * filename, void * context),
void * context)
{
gchar *fpath;
const gchar *de;
@ -282,7 +273,27 @@ static void readdirintolist_real(char *subdir, GtkWidget *view,
while (flist)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter, 0, flist->data, -1);
/* Set the filename */
gtk_list_store_set (store, &iter, PRESETS_LIST_COLUMN_FILENAME,
flist->data, -1);
/* Set the object name */
if (with_filename_column)
{
gchar * object_name;
object_name = get_object_name_cb (subdir, flist->data, context);
if (object_name)
{
gtk_list_store_set (store, &iter,
PRESETS_LIST_COLUMN_OBJECT_NAME,
object_name, -1);
g_free (object_name);
}
else
{
/* Default to the filename */
gtk_list_store_set (store, &iter, 1, flist->data, -1);
}
}
if (selected)
{
@ -302,7 +313,11 @@ static void readdirintolist_real(char *subdir, GtkWidget *view,
}
}
void readdirintolist(char *subdir, GtkWidget *view, char *selected)
void readdirintolist_extended(char *subdir, GtkWidget *view, char *selected,
gboolean with_filename_column,
gchar *(*get_object_name_cb)
(gchar *dir, gchar *filename, void *context),
void * context)
{
char *tmpdir;
GList *thispath = parsepath();
@ -310,12 +325,18 @@ void readdirintolist(char *subdir, GtkWidget *view, char *selected)
while (thispath)
{
tmpdir = g_build_filename ((gchar *) thispath->data, subdir, NULL);
readdirintolist_real (tmpdir, view, selected);
readdirintolist_real (tmpdir, view, selected, with_filename_column,
get_object_name_cb, context);
g_free (tmpdir);
thispath = thispath->next;
}
}
void readdirintolist (char *subdir, GtkWidget *view, char *selected)
{
readdirintolist_extended (subdir, view, selected, FALSE, NULL, NULL);
}
/*
* Creates a radio button.
* box - the containing box.