351 lines
9.5 KiB
C
351 lines
9.5 KiB
C
|
/* PIKA - Photo and Image Kooker Application
|
||
|
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
|
||
|
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
|
||
|
*
|
||
|
* Original copyright, applying to most contents (license remains unchanged):
|
||
|
* Copyright (C) 1995-2002 Spencer Kimball, Peter Mattis, and others
|
||
|
*
|
||
|
* pika-internal-data.c
|
||
|
* Copyright (C) 2017 Ell
|
||
|
*
|
||
|
* 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 <gdk-pixbuf/gdk-pixbuf.h>
|
||
|
#include <gegl.h>
|
||
|
|
||
|
#include "libpikabase/pikabase.h"
|
||
|
|
||
|
#include "core-types.h"
|
||
|
|
||
|
#include "pika.h"
|
||
|
#include "pika-gradients.h"
|
||
|
#include "pika-internal-data.h"
|
||
|
#include "pikadata.h"
|
||
|
#include "pikadataloaderfactory.h"
|
||
|
#include "pikaerror.h"
|
||
|
#include "pikagradient-load.h"
|
||
|
|
||
|
#include "pika-intl.h"
|
||
|
|
||
|
|
||
|
#define PIKA_INTERNAL_DATA_DIRECTORY "internal-data"
|
||
|
|
||
|
|
||
|
typedef PikaData * (* PikaDataGetFunc) (Pika *pika);
|
||
|
|
||
|
|
||
|
typedef struct _PikaInternalDataFile PikaInternalDataFile;
|
||
|
|
||
|
struct _PikaInternalDataFile
|
||
|
{
|
||
|
const gchar *name;
|
||
|
PikaDataGetFunc get_func;
|
||
|
PikaDataLoadFunc load_func;
|
||
|
};
|
||
|
|
||
|
|
||
|
/* local function prototypes */
|
||
|
|
||
|
static gboolean pika_internal_data_create_directory (GError **error);
|
||
|
|
||
|
static GFile * pika_internal_data_get_file (const PikaInternalDataFile *data_file);
|
||
|
|
||
|
static gboolean pika_internal_data_load_data_file (Pika *pika,
|
||
|
const PikaInternalDataFile *data_file,
|
||
|
GError **error);
|
||
|
static gboolean pika_internal_data_save_data_file (Pika *pika,
|
||
|
const PikaInternalDataFile *data_file,
|
||
|
GError **error);
|
||
|
static gboolean pika_internal_data_delete_data_file (Pika *pika,
|
||
|
const PikaInternalDataFile *data_file,
|
||
|
GError **error);
|
||
|
|
||
|
/* static variables */
|
||
|
|
||
|
static const PikaInternalDataFile internal_data_files[] =
|
||
|
{
|
||
|
/* Custom gradient */
|
||
|
{
|
||
|
.name = "custom" PIKA_GRADIENT_FILE_EXTENSION,
|
||
|
.get_func = (PikaDataGetFunc) pika_gradients_get_custom,
|
||
|
.load_func = pika_gradient_load
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
/* public functions */
|
||
|
|
||
|
gboolean
|
||
|
pika_internal_data_load (Pika *pika,
|
||
|
GError **error)
|
||
|
{
|
||
|
gint i;
|
||
|
|
||
|
g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
|
||
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
||
|
|
||
|
for (i = 0; i < G_N_ELEMENTS (internal_data_files); i++)
|
||
|
{
|
||
|
const PikaInternalDataFile *data_file = &internal_data_files[i];
|
||
|
|
||
|
if (! pika_internal_data_load_data_file (pika, data_file, error))
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_internal_data_save (Pika *pika,
|
||
|
GError **error)
|
||
|
{
|
||
|
gint i;
|
||
|
|
||
|
g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
|
||
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
||
|
|
||
|
if (! pika_internal_data_create_directory (error))
|
||
|
return FALSE;
|
||
|
|
||
|
for (i = 0; i < G_N_ELEMENTS (internal_data_files); i++)
|
||
|
{
|
||
|
const PikaInternalDataFile *data_file = &internal_data_files[i];
|
||
|
|
||
|
if (! pika_internal_data_save_data_file (pika, data_file, error))
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_internal_data_clear (Pika *pika,
|
||
|
GError **error)
|
||
|
{
|
||
|
gint i;
|
||
|
|
||
|
g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
|
||
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
||
|
|
||
|
for (i = 0; i < G_N_ELEMENTS (internal_data_files); i++)
|
||
|
{
|
||
|
const PikaInternalDataFile *data_file = &internal_data_files[i];
|
||
|
|
||
|
if (! pika_internal_data_delete_data_file (pika, data_file, error))
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* private functions */
|
||
|
|
||
|
static gboolean
|
||
|
pika_internal_data_create_directory (GError **error)
|
||
|
{
|
||
|
GFile *directory;
|
||
|
GError *my_error = NULL;
|
||
|
gboolean success;
|
||
|
|
||
|
directory = pika_directory_file (PIKA_INTERNAL_DATA_DIRECTORY, NULL);
|
||
|
|
||
|
success = g_file_make_directory (directory, NULL, &my_error);
|
||
|
|
||
|
g_object_unref (directory);
|
||
|
|
||
|
if (! success)
|
||
|
{
|
||
|
if (my_error->code == G_IO_ERROR_EXISTS)
|
||
|
{
|
||
|
g_clear_error (&my_error);
|
||
|
success = TRUE;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
g_propagate_error (error, my_error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
static GFile *
|
||
|
pika_internal_data_get_file (const PikaInternalDataFile *data_file)
|
||
|
{
|
||
|
return pika_directory_file (PIKA_INTERNAL_DATA_DIRECTORY,
|
||
|
data_file->name,
|
||
|
NULL);
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
pika_internal_data_load_data_file (Pika *pika,
|
||
|
const PikaInternalDataFile *data_file,
|
||
|
GError **error)
|
||
|
{
|
||
|
GFile *file;
|
||
|
GInputStream *input;
|
||
|
PikaData *data;
|
||
|
GList *list;
|
||
|
GError *my_error = NULL;
|
||
|
|
||
|
file = pika_internal_data_get_file (data_file);
|
||
|
|
||
|
if (pika->be_verbose)
|
||
|
g_print ("Parsing '%s'\n", pika_file_get_utf8_name (file));
|
||
|
|
||
|
input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));
|
||
|
|
||
|
if (! input)
|
||
|
{
|
||
|
g_object_unref (file);
|
||
|
|
||
|
if (my_error->code == G_IO_ERROR_NOT_FOUND)
|
||
|
{
|
||
|
g_clear_error (&my_error);
|
||
|
return TRUE;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
g_propagate_error (error, my_error);
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
list = data_file->load_func (pika->user_context, file, input, error);
|
||
|
|
||
|
g_object_unref (input);
|
||
|
g_object_unref (file);
|
||
|
|
||
|
if (! list)
|
||
|
return FALSE;
|
||
|
|
||
|
data = data_file->get_func (pika);
|
||
|
|
||
|
pika_data_copy (data, PIKA_DATA (list->data));
|
||
|
|
||
|
g_list_free_full (list, g_object_unref);
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
pika_internal_data_save_data_file (Pika *pika,
|
||
|
const PikaInternalDataFile *data_file,
|
||
|
GError **error)
|
||
|
{
|
||
|
GFile *file;
|
||
|
GOutputStream *output;
|
||
|
PikaData *data;
|
||
|
gboolean success;
|
||
|
|
||
|
file = pika_internal_data_get_file (data_file);
|
||
|
|
||
|
if (pika->be_verbose)
|
||
|
g_print ("Writing '%s'\n", pika_file_get_utf8_name (file));
|
||
|
|
||
|
output = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
|
||
|
G_FILE_CREATE_NONE,
|
||
|
NULL, error));
|
||
|
|
||
|
if (! output)
|
||
|
{
|
||
|
g_object_unref (file);
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
data = data_file->get_func (pika);
|
||
|
|
||
|
/* we bypass pika_data_save() and call the data's save() virtual function
|
||
|
* directly, since pika_data_save() is a nop for internal data.
|
||
|
*
|
||
|
* FIXME: we save the data whether it's dirty or not, since it might not
|
||
|
* exist on disk. currently, we only use this for cheap data, such as
|
||
|
* gradients, so this is not a big concern, but if we save more expensive
|
||
|
* data in the future, we should fix this.
|
||
|
*/
|
||
|
pika_assert (PIKA_DATA_GET_CLASS (data)->save);
|
||
|
success = PIKA_DATA_GET_CLASS (data)->save (data, output, error);
|
||
|
|
||
|
if (success)
|
||
|
{
|
||
|
if (! g_output_stream_close (output, NULL, error))
|
||
|
{
|
||
|
g_prefix_error (error,
|
||
|
_("Error saving '%s': "),
|
||
|
pika_file_get_utf8_name (file));
|
||
|
success = FALSE;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
GCancellable *cancellable = g_cancellable_new ();
|
||
|
|
||
|
g_cancellable_cancel (cancellable);
|
||
|
if (error && *error)
|
||
|
{
|
||
|
g_prefix_error (error,
|
||
|
_("Error saving '%s': "),
|
||
|
pika_file_get_utf8_name (file));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
g_set_error (error, PIKA_DATA_ERROR, PIKA_DATA_ERROR_WRITE,
|
||
|
_("Error saving '%s'"),
|
||
|
pika_file_get_utf8_name (file));
|
||
|
}
|
||
|
g_output_stream_close (output, cancellable, NULL);
|
||
|
g_object_unref (cancellable);
|
||
|
}
|
||
|
|
||
|
g_object_unref (output);
|
||
|
g_object_unref (file);
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
pika_internal_data_delete_data_file (Pika *pika,
|
||
|
const PikaInternalDataFile *data_file,
|
||
|
GError **error)
|
||
|
{
|
||
|
GFile *file;
|
||
|
GError *my_error = NULL;
|
||
|
gboolean success = TRUE;
|
||
|
|
||
|
file = pika_internal_data_get_file (data_file);
|
||
|
|
||
|
if (pika->be_verbose)
|
||
|
g_print ("Deleting '%s'\n", pika_file_get_utf8_name (file));
|
||
|
|
||
|
if (! g_file_delete (file, NULL, &my_error) &&
|
||
|
my_error->code != G_IO_ERROR_NOT_FOUND)
|
||
|
{
|
||
|
success = FALSE;
|
||
|
|
||
|
g_set_error (error, PIKA_ERROR, PIKA_FAILED,
|
||
|
_("Deleting \"%s\" failed: %s"),
|
||
|
pika_file_get_utf8_name (file), my_error->message);
|
||
|
}
|
||
|
|
||
|
g_clear_error (&my_error);
|
||
|
g_object_unref (file);
|
||
|
|
||
|
return success;
|
||
|
}
|