2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
#define LOAD_IMAGE _IMAGE_DIR"/load.gif"
|
2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* img: an icon name
|
|
|
|
* msg: text message or NULL
|
|
|
|
*/
|
2013-10-14 12:08:06 +00:00
|
|
|
#define SET_ICON(img, msg) \
|
2013-10-11 23:07:35 +00:00
|
|
|
g_object_set \
|
2014-07-16 14:12:36 +00:00
|
|
|
(self->priv->image, "icon-name", img, \
|
2013-10-11 23:07:35 +00:00
|
|
|
"icon-size", GTK_ICON_SIZE_MENU, NULL); \
|
2014-07-16 14:12:36 +00:00
|
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (self->priv->image), msg)
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_on_error (VnHttpImage * self, const GError * error)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-07 15:00:21 +00:00
|
|
|
gchar * msg = error ? error->message : _("Undefined error");
|
2014-07-16 14:12:36 +00:00
|
|
|
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);
|
2014-07-07 15:00:21 +00:00
|
|
|
SET_ICON ("missing-image", msg);
|
2014-07-16 14:12:36 +00:00
|
|
|
g_object_unref (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_on_download
|
|
|
|
(DbFileLoader * fl, GBytes * bytes, const GError * error, VnHttpImage * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
gtk_widget_set_tooltip_text (GTK_WIDGET (self->priv->image), NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
if (error)
|
2014-07-16 14:12:36 +00:00
|
|
|
vn_http_image_on_error (self, error);
|
2013-10-14 12:08:06 +00:00
|
|
|
else if (bytes)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
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);
|
2014-07-16 14:12:36 +00:00
|
|
|
gtk_image_set_from_pixbuf (self->priv->image, pixbuf);
|
|
|
|
self->priv->bytes = g_bytes_ref (bytes);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
vn_http_image_on_error (self, err);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_error_free (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (loader);
|
|
|
|
}
|
2014-07-07 15:00:21 +00:00
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
g_object_unref (self);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
/*
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_on_upload
|
|
|
|
(DbFileLoader * loader, GBytes * data, GError * error, VnHttpImage * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{}
|
|
|
|
*/
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_set_value (VnHttpImage * self, const GValue * value)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
gchar * full_path = g_strconcat (self->priv->path, "/",
|
2013-10-11 23:07:35 +00:00
|
|
|
g_value_get_string (value), NULL);
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
SET_ICON ("view-refresh", _("Loading"));
|
2014-07-16 14:12:36 +00:00
|
|
|
db_file_loader_download (self->priv->loader, full_path,
|
|
|
|
(DbFileLoaderCallbackFunc) vn_http_image_on_download, g_object_ref (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
g_free (full_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_PATH = 1
|
|
|
|
,PROP_LOADER
|
|
|
|
,PROP_BYTES
|
|
|
|
};
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_set_property (VnHttpImage * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_PATH:
|
2014-07-16 14:12:36 +00:00
|
|
|
g_free (self->priv->path);
|
|
|
|
self->priv->path = g_value_dup_string (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_LOADER:
|
2014-07-16 14:12:36 +00:00
|
|
|
if (self->priv->loader)
|
2013-10-11 23:07:35 +00:00
|
|
|
g_warning (_("File loader already set"));
|
|
|
|
else
|
2014-07-16 14:12:36 +00:00
|
|
|
self->priv->loader = g_value_dup_object (value);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-07-16 14:12:36 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_get_property (VnHttpImage * self, guint id,
|
2013-10-11 23:07:35 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case PROP_PATH:
|
2014-07-16 14:12:36 +00:00
|
|
|
g_value_set_string (value, self->priv->path);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_LOADER:
|
2014-07-16 14:12:36 +00:00
|
|
|
g_value_set_object (value, self->priv->loader);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_BYTES:
|
2014-07-16 14:12:36 +00:00
|
|
|
g_value_set_boxed (value, self->priv->bytes);
|
2013-10-11 23:07:35 +00:00
|
|
|
default:
|
2014-07-16 14:12:36 +00:00
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_init (VnHttpImage * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
|
|
|
|
(self, VN_TYPE_HTTP_IMAGE, VnHttpImagePrivate);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
self->priv->path = g_strdup ("");
|
|
|
|
self->priv->loader = NULL;
|
|
|
|
self->priv->image = g_object_ref_sink (GTK_IMAGE (gtk_image_new ()));
|
2013-10-11 23:07:35 +00:00
|
|
|
//Se usará para subir imagenes y cambiar nombres, o abrir un menu contextual
|
2014-07-16 14:12:36 +00:00
|
|
|
// g_signal_connect (self, "event", G_CALLBACK (vn_http_image_on_event), self);
|
2013-10-14 12:08:06 +00:00
|
|
|
SET_ICON ("missing-image", _("No image set"));
|
2014-07-16 14:12:36 +00:00
|
|
|
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));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:36 +00:00
|
|
|
static void vn_http_image_finalize (VnHttpImage * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:36 +00:00
|
|
|
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));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
));
|
|
|
|
}
|