229 lines
6.3 KiB
C
229 lines
6.3 KiB
C
/*
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "vn-http-image.h"
|
|
#include <db/db.h>
|
|
|
|
#define LOAD_IMAGE _IMAGE_DIR"/load.gif"
|
|
/*
|
|
* img: an icon name
|
|
* msg: text message or NULL
|
|
*/
|
|
#define SET_ICON(img, msg) \
|
|
g_object_set \
|
|
(obj->priv->image, "icon-name", img, \
|
|
"icon-size", GTK_ICON_SIZE_MENU, NULL); \
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (obj->priv->image), msg)
|
|
|
|
/**
|
|
* SECTION: vn-http-image
|
|
* @Short_description: an image from an http server
|
|
* @Title: VnHttpImage
|
|
*
|
|
* The image is loaded using a, previously created, #DbFileLoader.
|
|
*/
|
|
|
|
struct _VnHttpImagePrivate
|
|
{
|
|
DbFileLoader * loader;
|
|
gchar * path;
|
|
GtkImage * image;
|
|
GBytes * bytes;
|
|
};
|
|
|
|
G_DEFINE_TYPE (VnHttpImage, vn_http_image, VN_TYPE_FIELD);
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Methods
|
|
|
|
/**
|
|
* vn_http_image_new:
|
|
* @loader: a #DbFileLoader
|
|
* @name: (allow-none): the name of the file, with or without type extension
|
|
*
|
|
* Create an image named after @name located in an HTTP server.
|
|
*
|
|
* Return value: a #VnHttpImage
|
|
**/
|
|
VnField * vn_http_image_new (DbFileLoader * loader, const gchar * name)
|
|
{
|
|
g_return_val_if_fail (DB_IS_FILE_LOADER (loader), NULL);
|
|
|
|
return g_object_new (VN_TYPE_HTTP_IMAGE, "name", name, "loader", loader, NULL);
|
|
}
|
|
|
|
static void vn_http_image_cb_error (VnHttpImage * obj, const GError * error)
|
|
{
|
|
gchar * e = error ? error->message : _("Undefined error");
|
|
const gchar * name = g_value_get_string (vn_field_get_value (VN_FIELD (obj)));
|
|
g_warning ("VnHttpImage (%s%s): %s", obj->priv->path, name, e);
|
|
SET_ICON ("missing-image", e);
|
|
}
|
|
|
|
static void vn_http_image_cb_download
|
|
(DbFileLoader * fl, GBytes * bytes, const GError * error, VnHttpImage * obj)
|
|
{
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (obj->priv->image), NULL);
|
|
|
|
if (error)
|
|
vn_http_image_cb_error (obj, error);
|
|
else if (bytes)
|
|
{
|
|
gsize len;
|
|
const gchar * data = g_bytes_get_data (bytes, &len);
|
|
GError * err = NULL;
|
|
GdkPixbufLoader * loader = gdk_pixbuf_loader_new ();
|
|
|
|
if (gdk_pixbuf_loader_write (loader, (guchar *) data, len, &err)
|
|
&& gdk_pixbuf_loader_close (loader, &err))
|
|
{
|
|
GdkPixbuf * pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
|
|
gtk_image_set_from_pixbuf (obj->priv->image, pixbuf);
|
|
obj->priv->bytes = g_bytes_ref (bytes);
|
|
}
|
|
else
|
|
{
|
|
vn_http_image_cb_error (obj, err);
|
|
g_error_free (err);
|
|
}
|
|
|
|
g_object_unref (loader);
|
|
}
|
|
}
|
|
/*
|
|
static void vn_http_image_cb_upload
|
|
(DbFileLoader * loader, GBytes * data, GError * error, VnHttpImage * obj)
|
|
{}
|
|
*/
|
|
static void vn_http_image_set_value (VnHttpImage * obj, const GValue * value)
|
|
{
|
|
gchar * full_path = g_strconcat (obj->priv->path, "/",
|
|
g_value_get_string (value), NULL);
|
|
|
|
SET_ICON ("view-refresh", _("Loading"));
|
|
db_file_loader_download (obj->priv->loader, full_path,
|
|
(DbFileLoaderCallbackFunc) vn_http_image_cb_download, obj);
|
|
g_free (full_path);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_PATH = 1
|
|
,PROP_LOADER
|
|
,PROP_BYTES
|
|
};
|
|
|
|
static void vn_http_image_set_property (VnHttpImage * obj, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_PATH:
|
|
g_free (obj->priv->path);
|
|
obj->priv->path = g_value_dup_string (value);
|
|
break;
|
|
case PROP_LOADER:
|
|
if (obj->priv->loader)
|
|
g_warning (_("File loader already set"));
|
|
else
|
|
obj->priv->loader = g_value_dup_object (value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void vn_http_image_get_property (VnHttpImage * obj, guint id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_PATH:
|
|
g_value_set_string (value, obj->priv->path);
|
|
break;
|
|
case PROP_LOADER:
|
|
g_value_set_object (value, obj->priv->loader);
|
|
break;
|
|
case PROP_BYTES:
|
|
g_value_set_boxed (value, obj->priv->bytes);
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_http_image_init (VnHttpImage * obj)
|
|
{
|
|
obj->priv = G_TYPE_INSTANCE_GET_PRIVATE
|
|
(obj, VN_TYPE_HTTP_IMAGE, VnHttpImagePrivate);
|
|
|
|
obj->priv->path = g_strdup ("");
|
|
obj->priv->loader = NULL;
|
|
obj->priv->image = GTK_IMAGE (gtk_image_new ());
|
|
//Se usará para subir imagenes y cambiar nombres, o abrir un menu contextual
|
|
// g_signal_connect (obj, "event", G_CALLBACK (vn_http_image_cb_event), obj);
|
|
SET_ICON ("missing-image", _("No image set"));
|
|
gtk_container_add (GTK_CONTAINER (obj), GTK_WIDGET (obj->priv->image));
|
|
|
|
VN_FIELD (obj)->field = GTK_WIDGET (obj->priv->image);
|
|
}
|
|
|
|
static void vn_http_image_finalize (VnHttpImage * obj)
|
|
{
|
|
g_clear_object (&obj->priv->loader);
|
|
g_free (obj->priv->path);
|
|
g_bytes_unref (obj->priv->bytes);
|
|
G_OBJECT_CLASS (vn_http_image_parent_class)->finalize (G_OBJECT (obj));
|
|
}
|
|
|
|
static void vn_http_image_class_init (VnHttpImageClass * klass)
|
|
{
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
k->set_property = (GObjectSetPropertyFunc) vn_http_image_set_property;
|
|
k->get_property = (GObjectGetPropertyFunc) vn_http_image_get_property;
|
|
k->finalize = (GObjectFinalizeFunc) vn_http_image_finalize;
|
|
VN_FIELD_CLASS (klass)->set_value = (VnFieldSetValueFunc) vn_http_image_set_value;
|
|
|
|
g_type_class_add_private (klass, sizeof (VnHttpImagePrivate));
|
|
|
|
g_object_class_install_property (k, PROP_LOADER,
|
|
g_param_spec_object ("loader"
|
|
,_("File loader")
|
|
,_("A DbFileLoader, used to download the files")
|
|
,DB_TYPE_FILE_LOADER
|
|
,G_PARAM_READWRITE
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_PATH,
|
|
g_param_spec_string ("path"
|
|
,_("File path")
|
|
,_("The relative path to the image from file loader path")
|
|
,NULL
|
|
,G_PARAM_READWRITE
|
|
));
|
|
|
|
g_object_class_install_property (k, PROP_BYTES,
|
|
g_param_spec_boxed ("bytes"
|
|
,_("Image bytes")
|
|
,_("A GBytes structure with the image data")
|
|
,G_TYPE_BYTES
|
|
,G_PARAM_READABLE
|
|
));
|
|
}
|