gfileicon.c 8.62 KB
Newer Older
1 2 3 4 5 6 7
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
16
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 18 19 20
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

21
#include "config.h"
22 23

#include "gfileicon.h"
24 25
#include "gfile.h"
#include "gicon.h"
26
#include "glibintl.h"
27 28
#include "gloadableicon.h"
#include "ginputstream.h"
29
#include "gtask.h"
30
#include "gioerror.h"
31

32

33 34
/**
 * SECTION:gfileicon
35
 * @short_description: Icons pointing to an image file
Matthias Clasen's avatar
Matthias Clasen committed
36
 * @include: gio/gio.h
37
 * @see_also: #GIcon, #GLoadableIcon
38
 * 
39 40
 * #GFileIcon specifies an icon by pointing to an image file
 * to be used as icon.
41 42 43
 * 
 **/

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
static void g_file_icon_icon_iface_init          (GIconIface          *iface);
static void g_file_icon_loadable_icon_iface_init (GLoadableIconIface  *iface);
static void g_file_icon_load_async               (GLoadableIcon       *icon,
						  int                  size,
						  GCancellable        *cancellable,
						  GAsyncReadyCallback  callback,
						  gpointer             user_data);

struct _GFileIcon
{
  GObject parent_instance;

  GFile *file;
};

struct _GFileIconClass
{
  GObjectClass parent_class;
};

64 65 66 67 68 69
enum
{
  PROP_0,
  PROP_FILE
};

70
G_DEFINE_TYPE_WITH_CODE (GFileIcon, g_file_icon, G_TYPE_OBJECT,
71 72 73 74 75
                         G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
                                                g_file_icon_icon_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_LOADABLE_ICON,
                                                g_file_icon_loadable_icon_iface_init))

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static void
g_file_icon_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GFileIcon *icon = G_FILE_ICON (object);

  switch (prop_id)
    {
      case PROP_FILE:
        g_value_set_object (value, icon->file);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
g_file_icon_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
  GFileIcon *icon = G_FILE_ICON (object);

  switch (prop_id)
    {
      case PROP_FILE:
        icon->file = G_FILE (g_value_dup_object (value));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

114 115 116 117 118 119 120
static void
g_file_icon_finalize (GObject *object)
{
  GFileIcon *icon;

  icon = G_FILE_ICON (object);

121 122
  if (icon->file)
    g_object_unref (icon->file);
123 124

  G_OBJECT_CLASS (g_file_icon_parent_class)->finalize (object);
125 126 127 128 129 130 131
}

static void
g_file_icon_class_init (GFileIconClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  
132 133
  gobject_class->get_property = g_file_icon_get_property;
  gobject_class->set_property = g_file_icon_set_property;
134
  gobject_class->finalize = g_file_icon_finalize;
135 136 137 138 139 140 141 142

  /**
   * GFileIcon:file:
   *
   * The file containing the icon.
   */
  g_object_class_install_property (gobject_class, PROP_FILE,
                                   g_param_spec_object ("file",
143 144
                                                        P_("file"),
                                                        P_("The file containing the icon"),
145 146
                                                        G_TYPE_FILE,
                                                        G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
147 148 149 150 151 152 153 154 155
}

static void
g_file_icon_init (GFileIcon *file)
{
}

/**
 * g_file_icon_new:
156 157 158
 * @file: a #GFile.
 * 
 * Creates a new icon for a file.
159
 * 
160 161
 * Returns: (transfer full) (type GFileIcon): a #GIcon for the given
 *   @file, or %NULL on error.
162 163 164 165 166 167
 **/
GIcon *
g_file_icon_new (GFile *file)
{
  g_return_val_if_fail (G_IS_FILE (file), NULL);

168
  return G_ICON (g_object_new (G_TYPE_FILE_ICON, "file", file, NULL));
169 170 171 172
}

/**
 * g_file_icon_get_file:
173 174 175
 * @icon: a #GIcon.
 * 
 * Gets the #GFile associated with the given @icon.
176
 * 
177
 * Returns: (transfer none): a #GFile, or %NULL.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
 **/
GFile *
g_file_icon_get_file (GFileIcon *icon)
{
  g_return_val_if_fail (G_IS_FILE_ICON (icon), NULL);

  return icon->file;
}

static guint
g_file_icon_hash (GIcon *icon)
{
  GFileIcon *file_icon = G_FILE_ICON (icon);

  return g_file_hash (file_icon->file);
}

static gboolean
g_file_icon_equal (GIcon *icon1,
		   GIcon *icon2)
{
  GFileIcon *file1 = G_FILE_ICON (icon1);
  GFileIcon *file2 = G_FILE_ICON (icon2);
  
  return g_file_equal (file1->file, file2->file);
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
static gboolean
g_file_icon_to_tokens (GIcon *icon,
		       GPtrArray *tokens,
                       gint  *out_version)
{
  GFileIcon *file_icon = G_FILE_ICON (icon);

  g_return_val_if_fail (out_version != NULL, FALSE);

  *out_version = 0;

  g_ptr_array_add (tokens, g_file_get_uri (file_icon->file));
  return TRUE;
}

static GIcon *
g_file_icon_from_tokens (gchar  **tokens,
                         gint     num_tokens,
                         gint     version,
                         GError **error)
{
  GIcon *icon;
  GFile *file;

  icon = NULL;

  if (version != 0)
    {
      g_set_error (error,
                   G_IO_ERROR,
                   G_IO_ERROR_INVALID_ARGUMENT,
236
                   _("Can’t handle version %d of GFileIcon encoding"),
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
                   version);
      goto out;
    }

  if (num_tokens != 1)
    {
      g_set_error_literal (error,
                           G_IO_ERROR,
                           G_IO_ERROR_INVALID_ARGUMENT,
                           _("Malformed input data for GFileIcon"));
      goto out;
    }

  file = g_file_new_for_uri (tokens[0]);
  icon = g_file_icon_new (file);
  g_object_unref (file);

 out:
  return icon;
}
257

258 259 260 261 262 263 264 265
static GVariant *
g_file_icon_serialize (GIcon *icon)
{
  GFileIcon *file_icon = G_FILE_ICON (icon);

  return g_variant_new ("(sv)", "file", g_variant_new_take_string (g_file_get_uri (file_icon->file)));
}

266 267 268 269 270
static void
g_file_icon_icon_iface_init (GIconIface *iface)
{
  iface->hash = g_file_icon_hash;
  iface->equal = g_file_icon_equal;
271 272
  iface->to_tokens = g_file_icon_to_tokens;
  iface->from_tokens = g_file_icon_from_tokens;
273
  iface->serialize = g_file_icon_serialize;
274 275 276 277
}


static GInputStream *
Matthias Clasen's avatar
Matthias Clasen committed
278 279 280 281 282
g_file_icon_load (GLoadableIcon  *icon,
		  int            size,
		  char          **type,
		  GCancellable   *cancellable,
		  GError        **error)
283 284 285 286 287 288 289
{
  GFileInputStream *stream;
  GFileIcon *file_icon = G_FILE_ICON (icon);

  stream = g_file_read (file_icon->file,
			cancellable,
			error);
290 291 292

  if (stream && type)
    *type = NULL;
293 294 295 296 297
  
  return G_INPUT_STREAM (stream);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
298
load_async_callback (GObject      *source_object,
299
		     GAsyncResult *res,
Matthias Clasen's avatar
Matthias Clasen committed
300
		     gpointer      user_data)
301 302 303
{
  GFileInputStream *stream;
  GError *error = NULL;
304
  GTask *task = user_data;
305 306 307

  stream = g_file_read_finish (G_FILE (source_object), res, &error);
  if (stream == NULL)
308
    g_task_return_error (task, error);
309
  else
310 311
    g_task_return_pointer (task, stream, g_object_unref);
  g_object_unref (task);
312 313 314
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
315 316 317 318 319
g_file_icon_load_async (GLoadableIcon       *icon,
                        int                  size,
                        GCancellable        *cancellable,
                        GAsyncReadyCallback  callback,
                        gpointer             user_data)
320 321
{
  GFileIcon *file_icon = G_FILE_ICON (icon);
322
  GTask *task;
323

324
  task = g_task_new (icon, cancellable, callback, user_data);
325
  g_task_set_source_tag (task, g_file_icon_load_async);
326 327
  
  g_file_read_async (file_icon->file, 0,
328 329
                     cancellable,
                     load_async_callback, task);
330 331 332
}

static GInputStream *
Matthias Clasen's avatar
Matthias Clasen committed
333 334 335 336
g_file_icon_load_finish (GLoadableIcon  *icon,
			 GAsyncResult   *res,
			 char          **type,
			 GError        **error)
337
{
338
  g_return_val_if_fail (g_task_is_valid (res, icon), NULL);
339 340 341 342

  if (type)
    *type = NULL;
  
343
  return g_task_propagate_pointer (G_TASK (res), error);
344 345 346 347 348 349 350 351 352
}

static void
g_file_icon_loadable_icon_iface_init (GLoadableIconIface *iface)
{
  iface->load = g_file_icon_load;
  iface->load_async = g_file_icon_load_async;
  iface->load_finish = g_file_icon_load_finish;
}