1596 lines
41 KiB
C
1596 lines
41 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 Spencer Kimball and Peter Mattis
|
||
|
*
|
||
|
* 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 <errno.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <locale.h>
|
||
|
|
||
|
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
|
||
|
#include <langinfo.h>
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_UNISTD_H
|
||
|
#include <unistd.h>
|
||
|
#endif
|
||
|
|
||
|
#include <glib.h>
|
||
|
|
||
|
#ifdef G_OS_WIN32
|
||
|
#define _WIN32_WINNT 0x0500
|
||
|
#include <windows.h>
|
||
|
#include <process.h>
|
||
|
#endif
|
||
|
|
||
|
#include <cairo.h>
|
||
|
#include <gegl.h>
|
||
|
#include <gobject/gvaluecollector.h>
|
||
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
||
|
|
||
|
#include "libpikabase/pikabase.h"
|
||
|
#include "libpikamath/pikamath.h"
|
||
|
#include "libpikacolor/pikacolor.h"
|
||
|
|
||
|
#include "core-types.h"
|
||
|
|
||
|
#include "config/pikaxmlparser.h"
|
||
|
|
||
|
#include "pika.h"
|
||
|
#include "pika-utils.h"
|
||
|
#include "pikaasync.h"
|
||
|
#include "pikacontext.h"
|
||
|
#include "pikaerror.h"
|
||
|
#include "pikaimage.h"
|
||
|
|
||
|
#include "pika-intl.h"
|
||
|
|
||
|
|
||
|
#define MAX_FUNC 100
|
||
|
|
||
|
|
||
|
typedef struct
|
||
|
{
|
||
|
GString *text;
|
||
|
gint level;
|
||
|
|
||
|
gboolean numbered_list;
|
||
|
gint list_num;
|
||
|
gboolean unnumbered_list;
|
||
|
|
||
|
const gchar *lang;
|
||
|
GString *original;
|
||
|
gint foreign_level;
|
||
|
|
||
|
gchar **introduction;
|
||
|
GList **release_items;
|
||
|
} ParseState;
|
||
|
|
||
|
|
||
|
static gchar * pika_appstream_parse (const gchar *as_text,
|
||
|
gchar **introduction,
|
||
|
GList **release_items);
|
||
|
static void appstream_text_start_element (GMarkupParseContext *context,
|
||
|
const gchar *element_name,
|
||
|
const gchar **attribute_names,
|
||
|
const gchar **attribute_values,
|
||
|
gpointer user_data,
|
||
|
GError **error);
|
||
|
static void appstream_text_end_element (GMarkupParseContext *context,
|
||
|
const gchar *element_name,
|
||
|
gpointer user_data,
|
||
|
GError **error);
|
||
|
static void appstream_text_characters (GMarkupParseContext *context,
|
||
|
const gchar *text,
|
||
|
gsize text_len,
|
||
|
gpointer user_data,
|
||
|
GError **error);
|
||
|
static const gchar* pika_extension_get_tag_lang (const gchar **attribute_names,
|
||
|
const gchar **attribute_values);
|
||
|
|
||
|
gint
|
||
|
pika_get_pid (void)
|
||
|
{
|
||
|
return (gint) getpid ();
|
||
|
}
|
||
|
|
||
|
guint64
|
||
|
pika_get_physical_memory_size (void)
|
||
|
{
|
||
|
#ifdef G_OS_UNIX
|
||
|
#if defined(HAVE_UNISTD_H) && defined(_SC_PHYS_PAGES) && defined (_SC_PAGE_SIZE)
|
||
|
return (guint64) sysconf (_SC_PHYS_PAGES) * sysconf (_SC_PAGE_SIZE);
|
||
|
#endif
|
||
|
#endif
|
||
|
|
||
|
#ifdef G_OS_WIN32
|
||
|
# if defined(_MSC_VER) && (_MSC_VER <= 1200)
|
||
|
MEMORYSTATUS memory_status;
|
||
|
memory_status.dwLength = sizeof (memory_status);
|
||
|
|
||
|
GlobalMemoryStatus (&memory_status);
|
||
|
return memory_status.dwTotalPhys;
|
||
|
# else
|
||
|
/* requires w2k and newer SDK than provided with msvc6 */
|
||
|
MEMORYSTATUSEX memory_status;
|
||
|
|
||
|
memory_status.dwLength = sizeof (memory_status);
|
||
|
|
||
|
if (GlobalMemoryStatusEx (&memory_status))
|
||
|
return memory_status.ullTotalPhys;
|
||
|
# endif
|
||
|
#endif
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* basically copied from gtk_get_default_language()
|
||
|
*/
|
||
|
gchar *
|
||
|
pika_get_default_language (const gchar *category)
|
||
|
{
|
||
|
gchar *lang;
|
||
|
gchar *p;
|
||
|
gint cat = LC_CTYPE;
|
||
|
|
||
|
if (! category)
|
||
|
category = "LC_CTYPE";
|
||
|
|
||
|
#ifdef G_OS_WIN32
|
||
|
|
||
|
p = getenv ("LC_ALL");
|
||
|
if (p != NULL)
|
||
|
lang = g_strdup (p);
|
||
|
else
|
||
|
{
|
||
|
p = getenv ("LANG");
|
||
|
if (p != NULL)
|
||
|
lang = g_strdup (p);
|
||
|
else
|
||
|
{
|
||
|
p = getenv (category);
|
||
|
if (p != NULL)
|
||
|
lang = g_strdup (p);
|
||
|
else
|
||
|
lang = g_win32_getlocale ();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
|
if (strcmp (category, "LC_CTYPE") == 0)
|
||
|
cat = LC_CTYPE;
|
||
|
else if (strcmp (category, "LC_MESSAGES") == 0)
|
||
|
cat = LC_MESSAGES;
|
||
|
else
|
||
|
g_warning ("unsupported category used with pika_get_default_language()");
|
||
|
|
||
|
lang = g_strdup (setlocale (cat, NULL));
|
||
|
|
||
|
#endif
|
||
|
|
||
|
p = strchr (lang, '.');
|
||
|
if (p)
|
||
|
*p = '\0';
|
||
|
p = strchr (lang, '@');
|
||
|
if (p)
|
||
|
*p = '\0';
|
||
|
|
||
|
return lang;
|
||
|
}
|
||
|
|
||
|
PikaUnit
|
||
|
pika_get_default_unit (void)
|
||
|
{
|
||
|
#if defined (HAVE__NL_MEASUREMENT_MEASUREMENT)
|
||
|
const gchar *measurement = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
|
||
|
|
||
|
switch (*((guchar *) measurement))
|
||
|
{
|
||
|
case 1: /* metric */
|
||
|
return PIKA_UNIT_MM;
|
||
|
|
||
|
case 2: /* imperial */
|
||
|
return PIKA_UNIT_INCH;
|
||
|
}
|
||
|
|
||
|
#elif defined (G_OS_WIN32)
|
||
|
DWORD measurement;
|
||
|
int ret;
|
||
|
|
||
|
ret = GetLocaleInfoW (LOCALE_USER_DEFAULT,
|
||
|
LOCALE_IMEASURE | LOCALE_RETURN_NUMBER,
|
||
|
(LPWSTR) &measurement,
|
||
|
sizeof (measurement) / sizeof (wchar_t));
|
||
|
|
||
|
if (ret != 0) /* GetLocaleInfo succeeded */
|
||
|
{
|
||
|
switch ((guint) measurement)
|
||
|
{
|
||
|
case 0: /* metric */
|
||
|
return PIKA_UNIT_MM;
|
||
|
|
||
|
case 1: /* imperial */
|
||
|
return PIKA_UNIT_INCH;
|
||
|
}
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
return PIKA_UNIT_MM;
|
||
|
}
|
||
|
|
||
|
gchar **
|
||
|
pika_properties_append (GType object_type,
|
||
|
gint *n_properties,
|
||
|
gchar **names,
|
||
|
GValue **values,
|
||
|
...)
|
||
|
{
|
||
|
va_list args;
|
||
|
|
||
|
g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_OBJECT), NULL);
|
||
|
g_return_val_if_fail (n_properties != NULL, NULL);
|
||
|
g_return_val_if_fail (names != NULL || *n_properties == 0, NULL);
|
||
|
g_return_val_if_fail (values != NULL || *n_properties == 0, NULL);
|
||
|
|
||
|
va_start (args, values);
|
||
|
names = pika_properties_append_valist (object_type, n_properties,
|
||
|
names, values, args);
|
||
|
va_end (args);
|
||
|
|
||
|
return names;
|
||
|
}
|
||
|
|
||
|
gchar **
|
||
|
pika_properties_append_valist (GType object_type,
|
||
|
gint *n_properties,
|
||
|
gchar **names,
|
||
|
GValue **values,
|
||
|
va_list args)
|
||
|
{
|
||
|
GObjectClass *object_class;
|
||
|
gchar *param_name;
|
||
|
|
||
|
g_return_val_if_fail (g_type_is_a (object_type, G_TYPE_OBJECT), NULL);
|
||
|
g_return_val_if_fail (n_properties != NULL, NULL);
|
||
|
g_return_val_if_fail (names != NULL || *n_properties == 0, NULL);
|
||
|
g_return_val_if_fail (values != NULL || *n_properties == 0, NULL);
|
||
|
|
||
|
object_class = g_type_class_ref (object_type);
|
||
|
|
||
|
param_name = va_arg (args, gchar *);
|
||
|
|
||
|
while (param_name)
|
||
|
{
|
||
|
GValue *value;
|
||
|
gchar *error = NULL;
|
||
|
GParamSpec *pspec = g_object_class_find_property (object_class,
|
||
|
param_name);
|
||
|
|
||
|
if (! pspec)
|
||
|
{
|
||
|
g_warning ("%s: object class `%s' has no property named `%s'",
|
||
|
G_STRFUNC, g_type_name (object_type), param_name);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
names = g_renew (gchar *, names, *n_properties + 1);
|
||
|
*values = g_renew (GValue, *values, *n_properties + 1);
|
||
|
|
||
|
value = &((*values)[*n_properties]);
|
||
|
|
||
|
names[*n_properties] = g_strdup (param_name);
|
||
|
value->g_type = 0;
|
||
|
|
||
|
g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
|
||
|
|
||
|
G_VALUE_COLLECT (value, args, 0, &error);
|
||
|
|
||
|
if (error)
|
||
|
{
|
||
|
g_warning ("%s: %s", G_STRFUNC, error);
|
||
|
g_free (error);
|
||
|
g_free (names[*n_properties]);
|
||
|
g_value_unset (value);
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
*n_properties = *n_properties + 1;
|
||
|
|
||
|
param_name = va_arg (args, gchar *);
|
||
|
}
|
||
|
|
||
|
g_type_class_unref (object_class);
|
||
|
|
||
|
return names;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
pika_properties_free (gint n_properties,
|
||
|
gchar **names,
|
||
|
GValue *values)
|
||
|
{
|
||
|
g_return_if_fail (names != NULL || n_properties == 0);
|
||
|
g_return_if_fail (values != NULL || n_properties == 0);
|
||
|
|
||
|
if (names && values)
|
||
|
{
|
||
|
gint i;
|
||
|
|
||
|
for (i = 0; i < n_properties; i++)
|
||
|
{
|
||
|
g_free (names[i]);
|
||
|
g_value_unset (&values[i]);
|
||
|
}
|
||
|
|
||
|
g_free (names);
|
||
|
g_free (values);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* markup unescape code stolen and adapted from gmarkup.c
|
||
|
*/
|
||
|
static gchar *
|
||
|
char_str (gunichar c,
|
||
|
gchar *buf)
|
||
|
{
|
||
|
memset (buf, 0, 8);
|
||
|
g_unichar_to_utf8 (c, buf);
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
unescape_gstring (GString *string)
|
||
|
{
|
||
|
const gchar *from;
|
||
|
gchar *to;
|
||
|
|
||
|
/*
|
||
|
* Meeks' theorum: unescaping can only shrink text.
|
||
|
* for < etc. this is obvious, for  more
|
||
|
* thought is required, but this is patently so.
|
||
|
*/
|
||
|
for (from = to = string->str; *from != '\0'; from++, to++)
|
||
|
{
|
||
|
*to = *from;
|
||
|
|
||
|
if (*to == '\r')
|
||
|
{
|
||
|
*to = '\n';
|
||
|
if (from[1] == '\n')
|
||
|
from++;
|
||
|
}
|
||
|
if (*from == '&')
|
||
|
{
|
||
|
from++;
|
||
|
if (*from == '#')
|
||
|
{
|
||
|
gboolean is_hex = FALSE;
|
||
|
gulong l;
|
||
|
gchar *end = NULL;
|
||
|
|
||
|
from++;
|
||
|
|
||
|
if (*from == 'x')
|
||
|
{
|
||
|
is_hex = TRUE;
|
||
|
from++;
|
||
|
}
|
||
|
|
||
|
/* digit is between start and p */
|
||
|
errno = 0;
|
||
|
if (is_hex)
|
||
|
l = strtoul (from, &end, 16);
|
||
|
else
|
||
|
l = strtoul (from, &end, 10);
|
||
|
|
||
|
if (end == from || errno != 0)
|
||
|
{
|
||
|
return FALSE;
|
||
|
}
|
||
|
else if (*end != ';')
|
||
|
{
|
||
|
return FALSE;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
/* characters XML 1.1 permits */
|
||
|
if ((0 < l && l <= 0xD7FF) ||
|
||
|
(0xE000 <= l && l <= 0xFFFD) ||
|
||
|
(0x10000 <= l && l <= 0x10FFFF))
|
||
|
{
|
||
|
gchar buf[8];
|
||
|
char_str (l, buf);
|
||
|
strcpy (to, buf);
|
||
|
to += strlen (buf) - 1;
|
||
|
from = end;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
else if (strncmp (from, "lt;", 3) == 0)
|
||
|
{
|
||
|
*to = '<';
|
||
|
from += 2;
|
||
|
}
|
||
|
else if (strncmp (from, "gt;", 3) == 0)
|
||
|
{
|
||
|
*to = '>';
|
||
|
from += 2;
|
||
|
}
|
||
|
else if (strncmp (from, "amp;", 4) == 0)
|
||
|
{
|
||
|
*to = '&';
|
||
|
from += 3;
|
||
|
}
|
||
|
else if (strncmp (from, "quot;", 5) == 0)
|
||
|
{
|
||
|
*to = '"';
|
||
|
from += 4;
|
||
|
}
|
||
|
else if (strncmp (from, "apos;", 5) == 0)
|
||
|
{
|
||
|
*to = '\'';
|
||
|
from += 4;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pika_assert (to - string->str <= string->len);
|
||
|
if (to - string->str != string->len)
|
||
|
g_string_truncate (string, to - string->str);
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
gchar *
|
||
|
pika_markup_extract_text (const gchar *markup)
|
||
|
{
|
||
|
GString *string;
|
||
|
const gchar *p;
|
||
|
gboolean in_tag = FALSE;
|
||
|
|
||
|
if (! markup)
|
||
|
return NULL;
|
||
|
|
||
|
string = g_string_new (NULL);
|
||
|
|
||
|
for (p = markup; *p; p++)
|
||
|
{
|
||
|
if (in_tag)
|
||
|
{
|
||
|
if (*p == '>')
|
||
|
in_tag = FALSE;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (*p == '<')
|
||
|
in_tag = TRUE;
|
||
|
else
|
||
|
g_string_append_c (string, *p);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
unescape_gstring (string);
|
||
|
|
||
|
return g_string_free (string, FALSE);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* pika_enum_get_value_name:
|
||
|
* @enum_type: Enum type
|
||
|
* @value: Enum value
|
||
|
*
|
||
|
* Returns the value name for a given value of a given enum
|
||
|
* type. Useful to have inline in PIKA_LOG() messages for example.
|
||
|
*
|
||
|
* Returns: The value name.
|
||
|
**/
|
||
|
const gchar *
|
||
|
pika_enum_get_value_name (GType enum_type,
|
||
|
gint value)
|
||
|
{
|
||
|
const gchar *value_name = NULL;
|
||
|
|
||
|
pika_enum_get_value (enum_type,
|
||
|
value,
|
||
|
&value_name,
|
||
|
NULL /*value_nick*/,
|
||
|
NULL /*value_desc*/,
|
||
|
NULL /*value_help*/);
|
||
|
|
||
|
return value_name;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_get_fill_params (PikaContext *context,
|
||
|
PikaFillType fill_type,
|
||
|
PikaRGB *color,
|
||
|
PikaPattern **pattern,
|
||
|
GError **error)
|
||
|
|
||
|
{
|
||
|
g_return_val_if_fail (PIKA_IS_CONTEXT (context), FALSE);
|
||
|
g_return_val_if_fail (color != NULL, FALSE);
|
||
|
g_return_val_if_fail (pattern != NULL, FALSE);
|
||
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
||
|
|
||
|
*pattern = NULL;
|
||
|
|
||
|
switch (fill_type)
|
||
|
{
|
||
|
case PIKA_FILL_FOREGROUND:
|
||
|
pika_context_get_foreground (context, color);
|
||
|
break;
|
||
|
|
||
|
case PIKA_FILL_BACKGROUND:
|
||
|
pika_context_get_background (context, color);
|
||
|
break;
|
||
|
|
||
|
case PIKA_FILL_CIELAB_MIDDLE_GRAY:
|
||
|
{
|
||
|
const float cielab_pixel[3] = {50, 0, 0};
|
||
|
float pixel[3] = {0, 0, 0};
|
||
|
PikaImage *image = pika_context_get_image (context);
|
||
|
PikaImageBaseType base_type;
|
||
|
const Babl *format;
|
||
|
|
||
|
base_type = pika_image_get_base_type (image);
|
||
|
if (base_type == PIKA_INDEXED)
|
||
|
base_type = PIKA_RGB;
|
||
|
|
||
|
format = pika_image_get_format (image, base_type,
|
||
|
PIKA_PRECISION_FLOAT_NON_LINEAR, FALSE,
|
||
|
pika_image_get_layer_space (image));
|
||
|
|
||
|
babl_process (babl_fish (babl_format ("CIE Lab float"), format),
|
||
|
cielab_pixel, pixel, 1);
|
||
|
|
||
|
pika_rgba_set (color, pixel[0], pixel[1], pixel[2], PIKA_OPACITY_OPAQUE);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case PIKA_FILL_WHITE:
|
||
|
pika_rgba_set (color, 1.0, 1.0, 1.0, PIKA_OPACITY_OPAQUE);
|
||
|
break;
|
||
|
|
||
|
case PIKA_FILL_TRANSPARENT:
|
||
|
pika_rgba_set (color, 0.0, 0.0, 0.0, PIKA_OPACITY_TRANSPARENT);
|
||
|
break;
|
||
|
|
||
|
case PIKA_FILL_PATTERN:
|
||
|
*pattern = pika_context_get_pattern (context);
|
||
|
|
||
|
if (! *pattern)
|
||
|
{
|
||
|
g_set_error_literal (error, PIKA_ERROR, PIKA_FAILED,
|
||
|
_("No patterns available for this operation."));
|
||
|
|
||
|
/* fall back to BG fill */
|
||
|
pika_context_get_background (context, color);
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
g_warning ("%s: invalid fill_type %d", G_STRFUNC, fill_type);
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* pika_constrain_line:
|
||
|
* @start_x:
|
||
|
* @start_y:
|
||
|
* @end_x:
|
||
|
* @end_y:
|
||
|
* @n_snap_lines: Number evenly disributed lines to snap to.
|
||
|
* @offset_angle: The angle by which to offset the lines, in degrees.
|
||
|
* @xres: The horizontal resolution.
|
||
|
* @yres: The vertical resolution.
|
||
|
*
|
||
|
* Projects a line onto the specified subset of evenly radially
|
||
|
* distributed lines. @n_lines of 2 makes the line snap horizontally
|
||
|
* or vertically. @n_lines of 4 snaps on 45 degree steps. @n_lines of
|
||
|
* 12 on 15 degree steps. etc.
|
||
|
**/
|
||
|
void
|
||
|
pika_constrain_line (gdouble start_x,
|
||
|
gdouble start_y,
|
||
|
gdouble *end_x,
|
||
|
gdouble *end_y,
|
||
|
gint n_snap_lines,
|
||
|
gdouble offset_angle,
|
||
|
gdouble xres,
|
||
|
gdouble yres)
|
||
|
{
|
||
|
PikaVector2 diff;
|
||
|
PikaVector2 dir;
|
||
|
gdouble angle;
|
||
|
|
||
|
offset_angle *= G_PI / 180.0;
|
||
|
|
||
|
diff.x = (*end_x - start_x) / xres;
|
||
|
diff.y = (*end_y - start_y) / yres;
|
||
|
|
||
|
angle = (atan2 (diff.y, diff.x) - offset_angle) * n_snap_lines / G_PI;
|
||
|
angle = RINT (angle) * G_PI / n_snap_lines + offset_angle;
|
||
|
|
||
|
dir.x = cos (angle);
|
||
|
dir.y = sin (angle);
|
||
|
|
||
|
pika_vector2_mul (&dir, pika_vector2_inner_product (&dir, &diff));
|
||
|
|
||
|
*end_x = start_x + dir.x * xres;
|
||
|
*end_y = start_y + dir.y * yres;
|
||
|
}
|
||
|
|
||
|
gint
|
||
|
pika_file_compare (GFile *file1,
|
||
|
GFile *file2)
|
||
|
{
|
||
|
if (g_file_equal (file1, file2))
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
gchar *uri1 = g_file_get_uri (file1);
|
||
|
gchar *uri2 = g_file_get_uri (file2);
|
||
|
gint result = strcmp (uri1, uri2);
|
||
|
|
||
|
g_free (uri1);
|
||
|
g_free (uri2);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static inline gboolean
|
||
|
is_script (const gchar *filename)
|
||
|
{
|
||
|
#ifdef G_OS_WIN32
|
||
|
/* On Windows there is no concept like the Unix executable flag.
|
||
|
* There is a weak emulation provided by the MS C Runtime using file
|
||
|
* extensions (com, exe, cmd, bat). This needs to be extended to
|
||
|
* treat scripts (Python, Perl, ...) as executables, too. We use the
|
||
|
* PATHEXT variable, which is also used by cmd.exe.
|
||
|
*/
|
||
|
static gchar **exts = NULL;
|
||
|
|
||
|
const gchar *ext = strrchr (filename, '.');
|
||
|
const gchar *pathext;
|
||
|
gint i;
|
||
|
|
||
|
if (exts == NULL)
|
||
|
{
|
||
|
pathext = g_getenv ("PATHEXT");
|
||
|
if (pathext != NULL)
|
||
|
{
|
||
|
exts = g_strsplit (pathext, G_SEARCHPATH_SEPARATOR_S, 100);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
exts = g_new (gchar *, 1);
|
||
|
exts[0] = NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (i = 0; exts[i]; i++)
|
||
|
{
|
||
|
if (g_ascii_strcasecmp (ext, exts[i]) == 0)
|
||
|
return TRUE;
|
||
|
}
|
||
|
#endif /* G_OS_WIN32 */
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_file_is_executable (GFile *file)
|
||
|
{
|
||
|
GFileInfo *info;
|
||
|
gboolean executable = FALSE;
|
||
|
|
||
|
g_return_val_if_fail (G_IS_FILE (file), FALSE);
|
||
|
|
||
|
info = g_file_query_info (file,
|
||
|
G_FILE_ATTRIBUTE_STANDARD_NAME ","
|
||
|
G_FILE_ATTRIBUTE_STANDARD_TYPE ","
|
||
|
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ",",
|
||
|
G_FILE_QUERY_INFO_NONE,
|
||
|
NULL, NULL);
|
||
|
|
||
|
if (info)
|
||
|
{
|
||
|
GFileType file_type = g_file_info_get_file_type (info);
|
||
|
const gchar *filename = g_file_info_get_name (info);
|
||
|
|
||
|
if (file_type == G_FILE_TYPE_REGULAR &&
|
||
|
(g_file_info_get_attribute_boolean (info,
|
||
|
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) ||
|
||
|
is_script (filename)))
|
||
|
{
|
||
|
executable = TRUE;
|
||
|
}
|
||
|
|
||
|
g_object_unref (info);
|
||
|
}
|
||
|
|
||
|
return executable;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* pika_file_get_extension:
|
||
|
* @file: A #GFile
|
||
|
*
|
||
|
* Returns @file's extension (including the .), or NULL if there is no
|
||
|
* extension. Note that this function handles compressed files too,
|
||
|
* e.g. for "file.png.gz" it will return ".png.gz".
|
||
|
*
|
||
|
* Returns: The @file's extension. Free with g_free() when no longer needed.
|
||
|
**/
|
||
|
gchar *
|
||
|
pika_file_get_extension (GFile *file)
|
||
|
{
|
||
|
gchar *uri;
|
||
|
gint uri_len;
|
||
|
gchar *ext = NULL;
|
||
|
gint search_len;
|
||
|
|
||
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
||
|
|
||
|
uri = g_file_get_uri (file);
|
||
|
uri_len = strlen (uri);
|
||
|
|
||
|
if (g_str_has_suffix (uri, ".gz"))
|
||
|
search_len = uri_len - 3;
|
||
|
else if (g_str_has_suffix (uri, ".bz2"))
|
||
|
search_len = uri_len - 4;
|
||
|
else if (g_str_has_suffix (uri, ".xz"))
|
||
|
search_len = uri_len - 3;
|
||
|
else
|
||
|
search_len = uri_len;
|
||
|
|
||
|
ext = g_strrstr_len (uri, search_len, ".");
|
||
|
|
||
|
if (ext)
|
||
|
ext = g_strdup (ext);
|
||
|
|
||
|
g_free (uri);
|
||
|
|
||
|
return ext;
|
||
|
}
|
||
|
|
||
|
GFile *
|
||
|
pika_file_with_new_extension (GFile *file,
|
||
|
GFile *ext_file)
|
||
|
{
|
||
|
gchar *uri;
|
||
|
gchar *file_ext;
|
||
|
gint file_ext_len = 0;
|
||
|
gchar *ext_file_ext = NULL;
|
||
|
gchar *uri_without_ext;
|
||
|
gchar *new_uri;
|
||
|
GFile *ret;
|
||
|
|
||
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
||
|
g_return_val_if_fail (ext_file == NULL || G_IS_FILE (ext_file), NULL);
|
||
|
|
||
|
uri = g_file_get_uri (file);
|
||
|
file_ext = pika_file_get_extension (file);
|
||
|
|
||
|
if (file_ext)
|
||
|
{
|
||
|
file_ext_len = strlen (file_ext);
|
||
|
g_free (file_ext);
|
||
|
}
|
||
|
|
||
|
if (ext_file)
|
||
|
ext_file_ext = pika_file_get_extension (ext_file);
|
||
|
|
||
|
uri_without_ext = g_strndup (uri, strlen (uri) - file_ext_len);
|
||
|
|
||
|
g_free (uri);
|
||
|
|
||
|
new_uri = g_strconcat (uri_without_ext, ext_file_ext, NULL);
|
||
|
|
||
|
ret = g_file_new_for_uri (new_uri);
|
||
|
|
||
|
g_free (ext_file_ext);
|
||
|
g_free (uri_without_ext);
|
||
|
g_free (new_uri);
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* pika_file_delete_recursive:
|
||
|
* @file: #GFile to delete from file system.
|
||
|
* @error:
|
||
|
*
|
||
|
* Delete @file. If file is a directory, it will delete its children as
|
||
|
* well recursively. It will not follow symlinks so you won't end up in
|
||
|
* infinite loops, not will you be at risk of deleting your whole file
|
||
|
* system (unless you pass the root of course!).
|
||
|
* Such function unfortunately does not exist in glib, which only allows
|
||
|
* to delete single files or empty directories by default.
|
||
|
*
|
||
|
* Returns: %TRUE if @file was successfully deleted and all its
|
||
|
* children, %FALSE otherwise with @error filled.
|
||
|
*/
|
||
|
gboolean
|
||
|
pika_file_delete_recursive (GFile *file,
|
||
|
GError **error)
|
||
|
{
|
||
|
gboolean success = TRUE;
|
||
|
|
||
|
if (g_file_query_exists (file, NULL))
|
||
|
{
|
||
|
if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
|
||
|
NULL) == G_FILE_TYPE_DIRECTORY)
|
||
|
{
|
||
|
GFileEnumerator *enumerator;
|
||
|
|
||
|
enumerator = g_file_enumerate_children (file,
|
||
|
G_FILE_ATTRIBUTE_STANDARD_NAME ","
|
||
|
G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
|
||
|
G_FILE_ATTRIBUTE_TIME_MODIFIED,
|
||
|
G_FILE_QUERY_INFO_NONE,
|
||
|
NULL, NULL);
|
||
|
if (enumerator)
|
||
|
{
|
||
|
GFileInfo *info;
|
||
|
|
||
|
while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)))
|
||
|
{
|
||
|
GFile *child;
|
||
|
|
||
|
child = g_file_enumerator_get_child (enumerator, info);
|
||
|
g_object_unref (info);
|
||
|
|
||
|
if (! pika_file_delete_recursive (child, error))
|
||
|
success = FALSE;
|
||
|
|
||
|
g_object_unref (child);
|
||
|
if (! success)
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
g_object_unref (enumerator);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (success)
|
||
|
/* Non-directory or empty directory. */
|
||
|
success = g_file_delete (file, NULL, error);
|
||
|
}
|
||
|
|
||
|
return success;
|
||
|
}
|
||
|
|
||
|
gchar *
|
||
|
pika_data_input_stream_read_line_always (GDataInputStream *stream,
|
||
|
gsize *length,
|
||
|
GCancellable *cancellable,
|
||
|
GError **error)
|
||
|
{
|
||
|
GError *temp_error = NULL;
|
||
|
gchar *result;
|
||
|
|
||
|
g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), NULL);
|
||
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
||
|
|
||
|
if (! error)
|
||
|
error = &temp_error;
|
||
|
|
||
|
result = g_data_input_stream_read_line (stream, length, cancellable, error);
|
||
|
|
||
|
if (! result && ! *error)
|
||
|
{
|
||
|
result = g_strdup ("");
|
||
|
|
||
|
if (length) *length = 0;
|
||
|
}
|
||
|
|
||
|
g_clear_error (&temp_error);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_ascii_strtoi (const gchar *nptr,
|
||
|
gchar **endptr,
|
||
|
gint base,
|
||
|
gint *result)
|
||
|
{
|
||
|
gchar *temp_endptr;
|
||
|
gint64 temp_result;
|
||
|
|
||
|
g_return_val_if_fail (nptr != NULL, FALSE);
|
||
|
g_return_val_if_fail (base == 0 || (base >= 2 && base <= 36), FALSE);
|
||
|
|
||
|
if (! endptr)
|
||
|
endptr = &temp_endptr;
|
||
|
|
||
|
temp_result = g_ascii_strtoll (nptr, endptr, base);
|
||
|
|
||
|
if (*endptr == nptr || errno == ERANGE ||
|
||
|
temp_result < G_MININT || temp_result > G_MAXINT)
|
||
|
{
|
||
|
errno = 0;
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
if (result) *result = temp_result;
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_ascii_strtod (const gchar *nptr,
|
||
|
gchar **endptr,
|
||
|
gdouble *result)
|
||
|
{
|
||
|
gchar *temp_endptr;
|
||
|
gdouble temp_result;
|
||
|
|
||
|
g_return_val_if_fail (nptr != NULL, FALSE);
|
||
|
|
||
|
if (! endptr)
|
||
|
endptr = &temp_endptr;
|
||
|
|
||
|
temp_result = g_ascii_strtod (nptr, endptr);
|
||
|
|
||
|
if (*endptr == nptr || errno == ERANGE)
|
||
|
{
|
||
|
errno = 0;
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
if (result) *result = temp_result;
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
gchar *
|
||
|
pika_appstream_to_pango_markup (const gchar *as_text)
|
||
|
{
|
||
|
return pika_appstream_parse (as_text, NULL, NULL);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
pika_appstream_to_pango_markups (const gchar *as_text,
|
||
|
gchar **introduction,
|
||
|
GList **release_items)
|
||
|
{
|
||
|
gchar * markup;
|
||
|
|
||
|
markup = pika_appstream_parse (as_text, introduction, release_items);
|
||
|
g_free (markup);
|
||
|
}
|
||
|
|
||
|
gint
|
||
|
pika_g_list_compare (GList *list1,
|
||
|
GList *list2)
|
||
|
{
|
||
|
while (list1 && list2)
|
||
|
{
|
||
|
if (list1->data < list2->data)
|
||
|
return -1;
|
||
|
else if (list1->data > list2->data)
|
||
|
return +1;
|
||
|
|
||
|
list1 = g_list_next (list1);
|
||
|
list2 = g_list_next (list2);
|
||
|
}
|
||
|
|
||
|
if (! list1)
|
||
|
return -1;
|
||
|
else if (! list2)
|
||
|
return +1;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
PikaTRCType
|
||
|
pika_suggest_trc_for_component_type (PikaComponentType component_type,
|
||
|
PikaTRCType old_trc)
|
||
|
{
|
||
|
PikaTRCType new_trc = old_trc;
|
||
|
|
||
|
switch (component_type)
|
||
|
{
|
||
|
case PIKA_COMPONENT_TYPE_U8:
|
||
|
/* default to non-linear when converting 8 bit */
|
||
|
new_trc = PIKA_TRC_NON_LINEAR;
|
||
|
break;
|
||
|
|
||
|
case PIKA_COMPONENT_TYPE_U16:
|
||
|
case PIKA_COMPONENT_TYPE_U32:
|
||
|
default:
|
||
|
/* leave TRC alone by default when converting to 16/32 bit int */
|
||
|
break;
|
||
|
|
||
|
case PIKA_COMPONENT_TYPE_HALF:
|
||
|
case PIKA_COMPONENT_TYPE_FLOAT:
|
||
|
case PIKA_COMPONENT_TYPE_DOUBLE:
|
||
|
/* default to linear when converting to floating point */
|
||
|
new_trc = PIKA_TRC_LINEAR;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
return new_trc;
|
||
|
}
|
||
|
|
||
|
typedef struct
|
||
|
{
|
||
|
gint ref_count;
|
||
|
|
||
|
PikaAsync *async;
|
||
|
gint idle_id;
|
||
|
|
||
|
PikaRunAsyncFunc func;
|
||
|
gpointer user_data;
|
||
|
GDestroyNotify user_data_destroy_func;
|
||
|
} PikaIdleRunAsyncData;
|
||
|
|
||
|
static PikaIdleRunAsyncData *
|
||
|
pika_idle_run_async_data_new (void)
|
||
|
{
|
||
|
PikaIdleRunAsyncData *data;
|
||
|
|
||
|
data = g_slice_new0 (PikaIdleRunAsyncData);
|
||
|
|
||
|
data->ref_count = 1;
|
||
|
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
pika_idle_run_async_data_inc_ref (PikaIdleRunAsyncData *data)
|
||
|
{
|
||
|
data->ref_count++;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
pika_idle_run_async_data_dec_ref (PikaIdleRunAsyncData *data)
|
||
|
{
|
||
|
data->ref_count--;
|
||
|
|
||
|
if (data->ref_count == 0)
|
||
|
{
|
||
|
g_signal_handlers_disconnect_by_data (data->async, data);
|
||
|
|
||
|
if (! pika_async_is_stopped (data->async))
|
||
|
pika_async_abort (data->async);
|
||
|
|
||
|
g_object_unref (data->async);
|
||
|
|
||
|
if (data->user_data && data->user_data_destroy_func)
|
||
|
data->user_data_destroy_func (data->user_data);
|
||
|
|
||
|
g_slice_free (PikaIdleRunAsyncData, data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
pika_idle_run_async_cancel (PikaAsync *async,
|
||
|
PikaIdleRunAsyncData *data)
|
||
|
{
|
||
|
pika_idle_run_async_data_inc_ref (data);
|
||
|
|
||
|
if (data->idle_id)
|
||
|
{
|
||
|
g_source_remove (data->idle_id);
|
||
|
|
||
|
data->idle_id = 0;
|
||
|
}
|
||
|
|
||
|
pika_idle_run_async_data_dec_ref (data);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
pika_idle_run_async_waiting (PikaAsync *async,
|
||
|
PikaIdleRunAsyncData *data)
|
||
|
{
|
||
|
pika_idle_run_async_data_inc_ref (data);
|
||
|
|
||
|
if (data->idle_id)
|
||
|
{
|
||
|
g_source_remove (data->idle_id);
|
||
|
|
||
|
data->idle_id = 0;
|
||
|
}
|
||
|
|
||
|
g_signal_handlers_block_by_func (data->async,
|
||
|
pika_idle_run_async_cancel,
|
||
|
data);
|
||
|
|
||
|
while (! pika_async_is_stopped (data->async))
|
||
|
data->func (data->async, data->user_data);
|
||
|
|
||
|
g_signal_handlers_unblock_by_func (data->async,
|
||
|
pika_idle_run_async_cancel,
|
||
|
data);
|
||
|
|
||
|
data->user_data = NULL;
|
||
|
|
||
|
pika_idle_run_async_data_dec_ref (data);
|
||
|
}
|
||
|
|
||
|
static gboolean
|
||
|
pika_idle_run_async_idle (PikaIdleRunAsyncData *data)
|
||
|
{
|
||
|
pika_idle_run_async_data_inc_ref (data);
|
||
|
|
||
|
g_signal_handlers_block_by_func (data->async,
|
||
|
pika_idle_run_async_cancel,
|
||
|
data);
|
||
|
|
||
|
data->func (data->async, data->user_data);
|
||
|
|
||
|
g_signal_handlers_unblock_by_func (data->async,
|
||
|
pika_idle_run_async_cancel,
|
||
|
data);
|
||
|
|
||
|
if (pika_async_is_stopped (data->async))
|
||
|
{
|
||
|
data->user_data = NULL;
|
||
|
|
||
|
pika_idle_run_async_data_dec_ref (data);
|
||
|
|
||
|
return G_SOURCE_REMOVE;
|
||
|
}
|
||
|
|
||
|
pika_idle_run_async_data_dec_ref (data);
|
||
|
|
||
|
return G_SOURCE_CONTINUE;
|
||
|
}
|
||
|
|
||
|
PikaAsync *
|
||
|
pika_idle_run_async (PikaRunAsyncFunc func,
|
||
|
gpointer user_data)
|
||
|
{
|
||
|
return pika_idle_run_async_full (G_PRIORITY_DEFAULT_IDLE, func,
|
||
|
user_data, NULL);
|
||
|
}
|
||
|
|
||
|
PikaAsync *
|
||
|
pika_idle_run_async_full (gint priority,
|
||
|
PikaRunAsyncFunc func,
|
||
|
gpointer user_data,
|
||
|
GDestroyNotify user_data_destroy_func)
|
||
|
{
|
||
|
PikaIdleRunAsyncData *data;
|
||
|
|
||
|
g_return_val_if_fail (func != NULL, NULL);
|
||
|
|
||
|
data = pika_idle_run_async_data_new ();
|
||
|
|
||
|
data->func = func;
|
||
|
data->user_data = user_data;
|
||
|
data->user_data_destroy_func = user_data_destroy_func;
|
||
|
|
||
|
data->async = pika_async_new ();
|
||
|
|
||
|
g_signal_connect (data->async, "cancel",
|
||
|
G_CALLBACK (pika_idle_run_async_cancel),
|
||
|
data);
|
||
|
g_signal_connect (data->async, "waiting",
|
||
|
G_CALLBACK (pika_idle_run_async_waiting),
|
||
|
data);
|
||
|
|
||
|
data->idle_id = g_idle_add_full (
|
||
|
priority,
|
||
|
(GSourceFunc) pika_idle_run_async_idle,
|
||
|
data,
|
||
|
(GDestroyNotify) pika_idle_run_async_data_dec_ref);
|
||
|
|
||
|
return g_object_ref (data->async);
|
||
|
}
|
||
|
|
||
|
#ifdef G_OS_WIN32
|
||
|
|
||
|
gboolean
|
||
|
pika_win32_have_wintab (void)
|
||
|
{
|
||
|
gunichar2 wchars_buffer[MAX_PATH + 1];
|
||
|
UINT wchars_count = 0;
|
||
|
|
||
|
memset (wchars_buffer, 0, sizeof (wchars_buffer));
|
||
|
wchars_count = GetSystemDirectoryW (wchars_buffer, MAX_PATH);
|
||
|
if (wchars_count > 0 && wchars_count < MAX_PATH)
|
||
|
{
|
||
|
char *system32_directory = g_utf16_to_utf8 (wchars_buffer, -1, NULL, NULL, NULL);
|
||
|
|
||
|
if (system32_directory)
|
||
|
{
|
||
|
GFile *file = g_file_new_build_filename (system32_directory, "Wintab32.dll", NULL);
|
||
|
gboolean exists = g_file_query_exists (file, NULL);
|
||
|
|
||
|
g_object_unref (file);
|
||
|
g_free (system32_directory);
|
||
|
|
||
|
return exists;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
pika_win32_have_windows_ink (void)
|
||
|
{
|
||
|
/* Check for Windows 8 or later */
|
||
|
return g_win32_check_windows_version (6, 2, 0, G_WIN32_OS_ANY);
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
/* debug stuff */
|
||
|
|
||
|
#include "gegl/pika-babl.h"
|
||
|
#include "pikaimage.h"
|
||
|
#include "pikalayer.h"
|
||
|
#include "pikalayer-new.h"
|
||
|
|
||
|
PikaImage *
|
||
|
pika_create_image_from_buffer (Pika *pika,
|
||
|
GeglBuffer *buffer,
|
||
|
const gchar *image_name)
|
||
|
{
|
||
|
PikaImage *image;
|
||
|
PikaLayer *layer;
|
||
|
const Babl *format;
|
||
|
|
||
|
g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
|
||
|
g_return_val_if_fail (GEGL_IS_BUFFER (buffer), NULL);
|
||
|
|
||
|
if (! image_name)
|
||
|
image_name = "Debug Image";
|
||
|
|
||
|
format = gegl_buffer_get_format (buffer);
|
||
|
|
||
|
image = pika_create_image (pika,
|
||
|
gegl_buffer_get_width (buffer),
|
||
|
gegl_buffer_get_height (buffer),
|
||
|
pika_babl_format_get_base_type (format),
|
||
|
pika_babl_format_get_precision (format),
|
||
|
FALSE);
|
||
|
|
||
|
layer = pika_layer_new_from_gegl_buffer (buffer, image, format,
|
||
|
image_name,
|
||
|
PIKA_OPACITY_OPAQUE,
|
||
|
PIKA_LAYER_MODE_NORMAL,
|
||
|
NULL /* same image */);
|
||
|
pika_image_add_layer (image, layer, NULL, -1, FALSE);
|
||
|
|
||
|
pika_create_display (pika, image, PIKA_UNIT_PIXEL, 1.0, NULL);
|
||
|
|
||
|
/* unref the image unconditionally, even when no display was created */
|
||
|
g_object_add_weak_pointer (G_OBJECT (image), (gpointer) &image);
|
||
|
|
||
|
g_object_unref (image);
|
||
|
|
||
|
if (image)
|
||
|
g_object_remove_weak_pointer (G_OBJECT (image), (gpointer) &image);
|
||
|
|
||
|
return image;
|
||
|
}
|
||
|
|
||
|
gint
|
||
|
pika_view_size_get_larger (gint view_size)
|
||
|
{
|
||
|
if (view_size < PIKA_VIEW_SIZE_TINY)
|
||
|
return PIKA_VIEW_SIZE_TINY;
|
||
|
if (view_size < PIKA_VIEW_SIZE_EXTRA_SMALL)
|
||
|
return PIKA_VIEW_SIZE_EXTRA_SMALL;
|
||
|
if (view_size < PIKA_VIEW_SIZE_SMALL)
|
||
|
return PIKA_VIEW_SIZE_SMALL;
|
||
|
if (view_size < PIKA_VIEW_SIZE_MEDIUM)
|
||
|
return PIKA_VIEW_SIZE_MEDIUM;
|
||
|
if (view_size < PIKA_VIEW_SIZE_LARGE)
|
||
|
return PIKA_VIEW_SIZE_LARGE;
|
||
|
if (view_size < PIKA_VIEW_SIZE_EXTRA_LARGE)
|
||
|
return PIKA_VIEW_SIZE_EXTRA_LARGE;
|
||
|
if (view_size < PIKA_VIEW_SIZE_HUGE)
|
||
|
return PIKA_VIEW_SIZE_HUGE;
|
||
|
if (view_size < PIKA_VIEW_SIZE_ENORMOUS)
|
||
|
return PIKA_VIEW_SIZE_ENORMOUS;
|
||
|
return PIKA_VIEW_SIZE_GIGANTIC;
|
||
|
}
|
||
|
|
||
|
gint
|
||
|
pika_view_size_get_smaller (gint view_size)
|
||
|
{
|
||
|
if (view_size > PIKA_VIEW_SIZE_GIGANTIC)
|
||
|
return PIKA_VIEW_SIZE_GIGANTIC;
|
||
|
if (view_size > PIKA_VIEW_SIZE_ENORMOUS)
|
||
|
return PIKA_VIEW_SIZE_ENORMOUS;
|
||
|
if (view_size > PIKA_VIEW_SIZE_HUGE)
|
||
|
return PIKA_VIEW_SIZE_HUGE;
|
||
|
if (view_size > PIKA_VIEW_SIZE_EXTRA_LARGE)
|
||
|
return PIKA_VIEW_SIZE_EXTRA_LARGE;
|
||
|
if (view_size > PIKA_VIEW_SIZE_LARGE)
|
||
|
return PIKA_VIEW_SIZE_LARGE;
|
||
|
if (view_size > PIKA_VIEW_SIZE_MEDIUM)
|
||
|
return PIKA_VIEW_SIZE_MEDIUM;
|
||
|
if (view_size > PIKA_VIEW_SIZE_SMALL)
|
||
|
return PIKA_VIEW_SIZE_SMALL;
|
||
|
if (view_size > PIKA_VIEW_SIZE_EXTRA_SMALL)
|
||
|
return PIKA_VIEW_SIZE_EXTRA_SMALL;
|
||
|
return PIKA_VIEW_SIZE_TINY;
|
||
|
}
|
||
|
|
||
|
/* Private functions */
|
||
|
|
||
|
|
||
|
static gchar *
|
||
|
pika_appstream_parse (const gchar *as_text,
|
||
|
gchar **introduction,
|
||
|
GList **release_items)
|
||
|
{
|
||
|
static const GMarkupParser appstream_text_parser =
|
||
|
{
|
||
|
appstream_text_start_element,
|
||
|
appstream_text_end_element,
|
||
|
appstream_text_characters,
|
||
|
NULL, /* passthrough */
|
||
|
NULL /* error */
|
||
|
};
|
||
|
|
||
|
PikaXmlParser *xml_parser;
|
||
|
gchar *markup = NULL;
|
||
|
GError *error = NULL;
|
||
|
ParseState state;
|
||
|
|
||
|
state.level = 0;
|
||
|
state.foreign_level = -1;
|
||
|
state.text = g_string_new (NULL);
|
||
|
state.list_num = 0;
|
||
|
state.numbered_list = FALSE;
|
||
|
state.unnumbered_list = FALSE;
|
||
|
state.lang = g_getenv ("LANGUAGE");
|
||
|
state.original = NULL;
|
||
|
state.introduction = introduction;
|
||
|
state.release_items = release_items;
|
||
|
|
||
|
xml_parser = pika_xml_parser_new (&appstream_text_parser, &state);
|
||
|
if (as_text &&
|
||
|
! pika_xml_parser_parse_buffer (xml_parser, as_text, -1, &error))
|
||
|
{
|
||
|
g_printerr ("%s: %s\n", G_STRFUNC, error->message);
|
||
|
g_error_free (error);
|
||
|
}
|
||
|
|
||
|
/* Append possibly last original text without proper localization. */
|
||
|
if (state.original)
|
||
|
{
|
||
|
g_string_append (state.text, state.original->str);
|
||
|
g_string_free (state.original, TRUE);
|
||
|
}
|
||
|
|
||
|
if (release_items)
|
||
|
*release_items = g_list_reverse (*release_items);
|
||
|
|
||
|
markup = g_string_free (state.text, FALSE);
|
||
|
pika_xml_parser_free (xml_parser);
|
||
|
|
||
|
return markup;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
appstream_text_start_element (GMarkupParseContext *context,
|
||
|
const gchar *element_name,
|
||
|
const gchar **attribute_names,
|
||
|
const gchar **attribute_values,
|
||
|
gpointer user_data,
|
||
|
GError **error)
|
||
|
{
|
||
|
ParseState *state = user_data;
|
||
|
GString *output = state->text;
|
||
|
const gchar *tag_lang;
|
||
|
|
||
|
state->level++;
|
||
|
|
||
|
if (state->foreign_level >= 0)
|
||
|
return;
|
||
|
|
||
|
tag_lang = pika_extension_get_tag_lang (attribute_names, attribute_values);
|
||
|
if ((state->lang == NULL && tag_lang == NULL) ||
|
||
|
g_strcmp0 (tag_lang, state->lang) == 0)
|
||
|
{
|
||
|
/* Current tag is our current language. */
|
||
|
if (state->original)
|
||
|
g_string_free (state->original, TRUE);
|
||
|
state->original = NULL;
|
||
|
|
||
|
output = state->text;
|
||
|
}
|
||
|
else if (tag_lang == NULL)
|
||
|
{
|
||
|
/* Current tag is the original language (and we want a
|
||
|
* localization).
|
||
|
*/
|
||
|
if (state->original)
|
||
|
{
|
||
|
g_string_append (state->text, state->original->str);
|
||
|
g_string_free (state->original, TRUE);
|
||
|
}
|
||
|
state->original = g_string_new (NULL);
|
||
|
|
||
|
output = state->original;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
/* Current tag is an unrelated language */
|
||
|
state->foreign_level = state->level;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if ((state->numbered_list || state->unnumbered_list) &&
|
||
|
(g_strcmp0 (element_name, "ul") == 0 ||
|
||
|
g_strcmp0 (element_name, "ol") == 0))
|
||
|
{
|
||
|
g_set_error (error, PIKA_ERROR, PIKA_FAILED,
|
||
|
_("This parser does not support imbricated lists."));
|
||
|
}
|
||
|
else if (g_strcmp0 (element_name, "ul") == 0)
|
||
|
{
|
||
|
state->unnumbered_list = TRUE;
|
||
|
state->list_num = 0;
|
||
|
}
|
||
|
else if (g_strcmp0 (element_name, "ol") == 0)
|
||
|
{
|
||
|
state->numbered_list = TRUE;
|
||
|
state->list_num = 0;
|
||
|
}
|
||
|
else if (g_strcmp0 (element_name, "li") == 0)
|
||
|
{
|
||
|
state->list_num++;
|
||
|
if (state->numbered_list)
|
||
|
g_string_append_printf (output,
|
||
|
"\n<span weight='ultrabold' >\xe2\x9d\xa8%d\xe2\x9d\xa9</span> ",
|
||
|
state->list_num);
|
||
|
else if (state->unnumbered_list)
|
||
|
g_string_append (output, "\n<span weight='ultrabold' >\xe2\x80\xa2</span> ");
|
||
|
else
|
||
|
g_set_error (error, PIKA_ERROR, PIKA_FAILED,
|
||
|
_("<li> must be inside <ol> or <ul> tags."));
|
||
|
}
|
||
|
else if (g_strcmp0 (element_name, "p") != 0)
|
||
|
{
|
||
|
g_set_error (error, PIKA_ERROR, PIKA_FAILED,
|
||
|
_("Unknown tag <%s>."), element_name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
appstream_text_end_element (GMarkupParseContext *context,
|
||
|
const gchar *element_name,
|
||
|
gpointer user_data,
|
||
|
GError **error)
|
||
|
{
|
||
|
ParseState *state = user_data;
|
||
|
|
||
|
state->level--;
|
||
|
|
||
|
if (g_strcmp0 (element_name, "p") == 0)
|
||
|
{
|
||
|
if (state->foreign_level < 0)
|
||
|
{
|
||
|
if (state->introduction &&
|
||
|
*state->introduction == NULL)
|
||
|
*state->introduction = g_strdup (state->original ? state->original->str : state->text->str);
|
||
|
|
||
|
if (state->original)
|
||
|
g_string_append (state->original, "\n\n");
|
||
|
else
|
||
|
g_string_append (state->text, "\n\n");
|
||
|
}
|
||
|
}
|
||
|
else if (g_strcmp0 (element_name, "ul") == 0 ||
|
||
|
g_strcmp0 (element_name, "ol") == 0)
|
||
|
{
|
||
|
state->numbered_list = FALSE;
|
||
|
state->unnumbered_list = FALSE;
|
||
|
}
|
||
|
else if (g_strcmp0 (element_name, "li") == 0)
|
||
|
{
|
||
|
if (state->original)
|
||
|
g_string_append (state->original, "\n");
|
||
|
else
|
||
|
g_string_append (state->text, "\n");
|
||
|
}
|
||
|
|
||
|
if (state->foreign_level > state->level)
|
||
|
state->foreign_level = -1;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
appstream_text_characters (GMarkupParseContext *context,
|
||
|
const gchar *text,
|
||
|
gsize text_len,
|
||
|
gpointer user_data,
|
||
|
GError **error)
|
||
|
{
|
||
|
ParseState *state = user_data;
|
||
|
|
||
|
if (state->foreign_level < 0 && text_len > 0)
|
||
|
{
|
||
|
if (state->list_num > 0 && state->release_items)
|
||
|
{
|
||
|
GList **items = state->release_items;
|
||
|
|
||
|
if (state->list_num == g_list_length (*(state->release_items)))
|
||
|
{
|
||
|
gchar *tmp = (*items)->data;
|
||
|
|
||
|
(*items)->data = g_strconcat (tmp, text, NULL);
|
||
|
g_free (tmp);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
*items = g_list_prepend (*items, g_strdup (text));
|
||
|
}
|
||
|
}
|
||
|
if (state->original)
|
||
|
g_string_append (state->original, text);
|
||
|
else
|
||
|
g_string_append (state->text, text);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static const gchar *
|
||
|
pika_extension_get_tag_lang (const gchar **attribute_names,
|
||
|
const gchar **attribute_values)
|
||
|
{
|
||
|
while (*attribute_names)
|
||
|
{
|
||
|
if (! strcmp (*attribute_names, "xml:lang"))
|
||
|
{
|
||
|
return *attribute_values;
|
||
|
}
|
||
|
|
||
|
attribute_names++;
|
||
|
attribute_values++;
|
||
|
}
|
||
|
|
||
|
return NULL;
|
||
|
}
|