background: use GFiles instead of filenames

We want to use GResources for system backgrounds, so move this to a
GFile.

https://bugzilla.gnome.org/show_bug.cgi?id=736936
This commit is contained in:
Cosimo Cecchi 2014-09-18 18:22:39 -07:00 committed by Jasper St. Pierre
parent 8a6542c242
commit a37f632b1b
4 changed files with 71 additions and 47 deletions

View File

@ -53,7 +53,7 @@ struct _MetaBackgroundImageCacheClass
struct _MetaBackgroundImage
{
GObject parent_instance;
char *filename;
GFile *file;
MetaBackgroundImageCache *cache;
gboolean in_cache;
gboolean loaded;
@ -70,7 +70,7 @@ G_DEFINE_TYPE (MetaBackgroundImageCache, meta_background_image_cache, G_TYPE_OBJ
static void
meta_background_image_cache_init (MetaBackgroundImageCache *cache)
{
cache->images = g_hash_table_new (g_str_hash, g_str_equal);
cache->images = g_hash_table_new (g_file_hash, (GEqualFunc) g_file_equal);
}
static void
@ -124,9 +124,17 @@ load_file (GTask *task,
{
GError *error = NULL;
GdkPixbuf *pixbuf;
GFileInputStream *stream;
pixbuf = gdk_pixbuf_new_from_file (image->filename,
&error);
stream = g_file_read (image->file, NULL, &error);
if (stream == NULL)
{
g_task_return_error (task, error);
return;
}
pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (stream), NULL, &error);
g_object_unref (stream);
if (pixbuf == NULL)
{
@ -156,9 +164,11 @@ file_loaded (GObject *source_object,
if (pixbuf == NULL)
{
char *uri = g_file_get_uri (image->file);
g_warning ("Failed to load background '%s': %s",
image->filename, error->message);
uri, error->message);
g_clear_error (&error);
g_free (uri);
goto out;
}
@ -195,7 +205,7 @@ out:
/**
* meta_background_image_cache_load:
* @cache: a #MetaBackgroundImageCache
* @filename: filename to load
* @file: #GFile to load
*
* Loads an image to use as a background, or returns a reference to an
* image that is already in the process of loading or loaded. In either
@ -209,23 +219,23 @@ out:
*/
MetaBackgroundImage *
meta_background_image_cache_load (MetaBackgroundImageCache *cache,
const char *filename)
GFile *file)
{
MetaBackgroundImage *image;
GTask *task;
g_return_val_if_fail (META_IS_BACKGROUND_IMAGE_CACHE (cache), NULL);
g_return_val_if_fail (filename != NULL, NULL);
g_return_val_if_fail (file != NULL, NULL);
image = g_hash_table_lookup (cache->images, filename);
image = g_hash_table_lookup (cache->images, file);
if (image != NULL)
return g_object_ref (image);
image = g_object_new (META_TYPE_BACKGROUND_IMAGE, NULL);
image->cache = cache;
image->in_cache = TRUE;
image->filename = g_strdup (filename);
g_hash_table_insert (cache->images, image->filename, image);
image->file = g_object_ref (file);
g_hash_table_insert (cache->images, image->file, image);
task = g_task_new (image, NULL, file_loaded, NULL);
@ -238,25 +248,25 @@ meta_background_image_cache_load (MetaBackgroundImageCache *cache,
/**
* meta_background_image_cache_purge:
* @cache: a #MetaBackgroundImageCache
* @filename: filename to remove from the cache
* @file: file to remove from the cache
*
* Remove an entry from the cache; this would be used if monitoring
* showed that the file changed.
*/
void
meta_background_image_cache_purge (MetaBackgroundImageCache *cache,
const char *filename)
GFile *file)
{
MetaBackgroundImage *image;
g_return_if_fail (META_IS_BACKGROUND_IMAGE_CACHE (cache));
g_return_if_fail (filename != NULL);
g_return_if_fail (file != NULL);
image = g_hash_table_lookup (cache->images, filename);
image = g_hash_table_lookup (cache->images, file);
if (image == NULL)
return;
g_hash_table_remove (cache->images, image->filename);
g_hash_table_remove (cache->images, image->file);
image->in_cache = FALSE;
}
@ -273,12 +283,12 @@ meta_background_image_finalize (GObject *object)
MetaBackgroundImage *image = META_BACKGROUND_IMAGE (object);
if (image->in_cache)
g_hash_table_remove (image->cache->images, image->filename);
g_hash_table_remove (image->cache->images, image->file);
if (image->texture)
cogl_object_unref (image->texture);
if (image->filename)
g_free (image->filename);
if (image->file)
g_object_unref (image->file);
G_OBJECT_CLASS (meta_background_image_parent_class)->finalize (object);
}

View File

@ -50,9 +50,9 @@ struct _MetaBackgroundPrivate
ClutterColor color;
ClutterColor second_color;
char *filename1;
GFile *file1;
MetaBackgroundImage *background_image1;
char *filename2;
GFile *file2;
MetaBackgroundImage *background_image2;
CoglTexture *color_texture;
@ -241,16 +241,28 @@ on_background_loaded (MetaBackgroundImage *image,
mark_changed (self);
}
static void
set_filename (MetaBackground *self,
char **filenamep,
MetaBackgroundImage **imagep,
const char *filename)
static gboolean
file_equal0 (GFile *file1,
GFile *file2)
{
if (g_strcmp0 (filename, *filenamep) != 0)
if (file1 == file2)
return TRUE;
if ((file1 == NULL) || (file2 == NULL))
return FALSE;
return g_file_equal (file1, file2);
}
static void
set_file (MetaBackground *self,
GFile **filep,
MetaBackgroundImage **imagep,
GFile *file)
{
if (!file_equal0 (*filep, file))
{
g_free (*filenamep);
*filenamep = g_strdup (filename);
g_clear_object (filep);
if (*imagep)
{
@ -261,10 +273,12 @@ set_filename (MetaBackground *self,
*imagep = NULL;
}
if (filename)
if (file)
{
MetaBackgroundImageCache *cache = meta_background_image_cache_get_default ();
*imagep = meta_background_image_cache_load (cache, filename);
*filep = g_object_ref (file);
*imagep = meta_background_image_cache_load (cache, file);
g_signal_connect (*imagep, "loaded",
G_CALLBACK (on_background_loaded), self);
}
@ -280,8 +294,8 @@ meta_background_dispose (GObject *object)
free_color_texture (self);
free_wallpaper_texture (self);
set_filename (self, &priv->filename1, &priv->background_image1, NULL);
set_filename (self, &priv->filename2, &priv->background_image2, NULL);
set_file (self, &priv->file1, &priv->background_image1, NULL);
set_file (self, &priv->file2, &priv->background_image2, NULL);
set_screen (self, NULL);
@ -867,19 +881,19 @@ meta_background_set_gradient (MetaBackground *self,
}
void
meta_background_set_filename (MetaBackground *self,
const char *filename,
meta_background_set_file (MetaBackground *self,
GFile *file,
GDesktopBackgroundStyle style)
{
g_return_if_fail (META_IS_BACKGROUND (self));
meta_background_set_blend (self, filename, NULL, 0.0, style);
meta_background_set_blend (self, file, NULL, 0.0, style);
}
void
meta_background_set_blend (MetaBackground *self,
const char *filename1,
const char *filename2,
GFile *file1,
GFile *file2,
double blend_factor,
GDesktopBackgroundStyle style)
{
@ -890,8 +904,8 @@ meta_background_set_blend (MetaBackground *self,
priv = self->priv;
set_filename (self, &priv->filename1, &priv->background_image1, filename1);
set_filename (self, &priv->filename2, &priv->background_image2, filename2);
set_file (self, &priv->file1, &priv->background_image1, file1);
set_file (self, &priv->file2, &priv->background_image2, file2);
priv->blend_factor = blend_factor;
priv->style = style;

View File

@ -69,8 +69,8 @@ MetaBackgroundImageCache *meta_background_image_cache_get_default (void);
GType meta_background_image_cache_get_type (void);
MetaBackgroundImage *meta_background_image_cache_load (MetaBackgroundImageCache *cache,
const char *filename);
GFile *file);
void meta_background_image_cache_purge (MetaBackgroundImageCache *cache,
const char *filename);
GFile *file);
#endif /* __META_BACKGROUND_IMAGE_H__ */

View File

@ -67,12 +67,12 @@ void meta_background_set_gradient (MetaBackground *self,
GDesktopBackgroundShading shading_direction,
ClutterColor *color,
ClutterColor *second_color);
void meta_background_set_filename (MetaBackground *self,
const char *filename,
void meta_background_set_file (MetaBackground *self,
GFile *file,
GDesktopBackgroundStyle style);
void meta_background_set_blend (MetaBackground *self,
const char *filename1,
const char *filename2,
GFile *file1,
GFile *file2,
double blend_factor,
GDesktopBackgroundStyle style);