234 lines
6.5 KiB
C
234 lines
6.5 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 \
|
|
(self->priv->image, "icon-name", img, \
|
|
"icon-size", GTK_ICON_SIZE_MENU, NULL); \
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (self->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_on_error (VnHttpImage * self, const GError * error)
|
|
{
|
|
gchar * msg = error ? error->message : _("Undefined error");
|
|
const gchar * name = g_value_get_string (gvn_param_get_value (GVN_PARAM (self)));
|
|
g_warning ("VnHttpImage (%s%s): %s", self->priv->path, name, msg);
|
|
SET_ICON ("missing-image", msg);
|
|
g_object_unref (self);
|
|
}
|
|
|
|
static void vn_http_image_on_download
|
|
(DbFileLoader * fl, GBytes * bytes, const GError * error, VnHttpImage * self)
|
|
{
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (self->priv->image), NULL);
|
|
|
|
if (error)
|
|
vn_http_image_on_error (self, 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 (self->priv->image, pixbuf);
|
|
self->priv->bytes = g_bytes_ref (bytes);
|
|
}
|
|
else
|
|
{
|
|
vn_http_image_on_error (self, err);
|
|
g_error_free (err);
|
|
}
|
|
|
|
g_object_unref (loader);
|
|
}
|
|
|
|
g_object_unref (self);
|
|
}
|
|
/*
|
|
static void vn_http_image_on_upload
|
|
(DbFileLoader * loader, GBytes * data, GError * error, VnHttpImage * self)
|
|
{}
|
|
*/
|
|
static void vn_http_image_set_value (VnHttpImage * self, const GValue * value)
|
|
{
|
|
gchar * full_path = g_strconcat (self->priv->path, "/",
|
|
g_value_get_string (value), NULL);
|
|
|
|
SET_ICON ("view-refresh", _("Loading"));
|
|
db_file_loader_download (self->priv->loader, full_path,
|
|
(DbFileLoaderCallbackFunc) vn_http_image_on_download, g_object_ref (self));
|
|
g_free (full_path);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_PATH = 1
|
|
,PROP_LOADER
|
|
,PROP_BYTES
|
|
};
|
|
|
|
static void vn_http_image_set_property (VnHttpImage * self, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_PATH:
|
|
g_free (self->priv->path);
|
|
self->priv->path = g_value_dup_string (value);
|
|
break;
|
|
case PROP_LOADER:
|
|
if (self->priv->loader)
|
|
g_warning (_("File loader already set"));
|
|
else
|
|
self->priv->loader = g_value_dup_object (value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void vn_http_image_get_property (VnHttpImage * self, guint id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_PATH:
|
|
g_value_set_string (value, self->priv->path);
|
|
break;
|
|
case PROP_LOADER:
|
|
g_value_set_object (value, self->priv->loader);
|
|
break;
|
|
case PROP_BYTES:
|
|
g_value_set_boxed (value, self->priv->bytes);
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_http_image_init (VnHttpImage * self)
|
|
{
|
|
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
|
|
(self, VN_TYPE_HTTP_IMAGE, VnHttpImagePrivate);
|
|
|
|
self->priv->path = g_strdup ("");
|
|
self->priv->loader = NULL;
|
|
self->priv->image = g_object_ref_sink (GTK_IMAGE (gtk_image_new ()));
|
|
//Se usará para subir imagenes y cambiar nombres, o abrir un menu contextual
|
|
// g_signal_connect (self, "event", G_CALLBACK (vn_http_image_on_event), self);
|
|
SET_ICON ("missing-image", _("No image set"));
|
|
gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->priv->image));
|
|
|
|
VN_FIELD_GET_CLASS (self)->set_widget (VN_FIELD (self),
|
|
GTK_WIDGET (self->priv->image));
|
|
}
|
|
|
|
static void vn_http_image_finalize (VnHttpImage * self)
|
|
{
|
|
g_clear_object (&self->priv->loader);
|
|
g_free (self->priv->path);
|
|
g_bytes_unref (self->priv->bytes);
|
|
g_object_unref (self->priv->image);
|
|
G_OBJECT_CLASS (vn_http_image_parent_class)->finalize (G_OBJECT (self));
|
|
}
|
|
|
|
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
|
|
));
|
|
}
|