Beispiel #1
0
static void
ditem_read_cb (GObject *source_object,
	       GAsyncResult *res,
	       gpointer user_data)
{
	GnomeDesktopItem *item;
	GtkWidget *box;
	gsize file_size;
	char *file_contents;

	box = GTK_WIDGET (user_data);
	
	if (g_file_load_contents_finish (G_FILE (source_object),
					 res,
					 &file_contents, &file_size,
					 NULL, NULL)) {
		item = gnome_desktop_item_new_from_string (g_object_get_data (G_OBJECT (box),
									      "uri"),
							   file_contents,
							   file_size,
							   0, NULL);
		g_free (file_contents);
		if (item == NULL) {
			return;
		}
		
		/* for some reason, this isn't done automatically */
		gnome_desktop_item_set_location (item, g_object_get_data (G_OBJECT (box), "uri"));
		
		create_page (item, box);
		gnome_desktop_item_unref (item);
	}
	g_object_unref (box);
}
Beispiel #2
0
static void
ditem_read_cb (GObject *source_object,
               GAsyncResult *res,
               gpointer user_data)
{
    GKeyFile *key_file;
    GtkWidget *box;
    gsize file_size;
    char *file_contents;

    box = GTK_WIDGET (user_data);

    if (g_file_load_contents_finish (G_FILE (source_object),
                                     res,
                                     &file_contents, &file_size,
                                     NULL, NULL))
    {
        key_file = g_key_file_new ();
        g_object_set_data_full (G_OBJECT (box), "keyfile", key_file, (GDestroyNotify)g_key_file_free);
        if (g_key_file_load_from_data (key_file, file_contents, file_size, 0, NULL))
        {
            create_page (key_file, box);
        }
        g_free (file_contents);

    }
    g_object_unref (box);
}
static void
file_load_cb (GObject      *source_object,
              GAsyncResult *res,
              gpointer      user_data)
{
  GError *error = NULL;
  DQTask *task = user_data;
  gchar *contents = NULL;
  gsize length = 0;

  g_file_load_contents_finish (task->gio.file,
                               res,
                               &contents,
                               &length,
                               NULL,
                               &error);

  if (!g_cancellable_is_cancelled (task->gio.cancellable))
    {
      if (error)
        {
          task->any.callback (task->any.queue, task->any.uri,
                              NULL, 0,
                              error,
                              task->any.userdata);
        }
      else
        {
          task->any.callback (task->any.queue, task->any.uri,
                              contents, length,
                              NULL,
                              task->any.userdata);

          mex_download_queue_cache_insert (task->any.queue, task->any.uri,
                                           contents, length);
        }
    }

  if (error)
    g_error_free (error);

  g_object_unref (task->gio.cancellable);
  task->gio.cancellable = NULL;

  mex_download_queue_free (task);
}
static void
async_thumbnail_read_image (GObject *source_object,
			    GAsyncResult *res,
			    gpointer callback_data)
{
	NautilusThumbnailAsyncLoadHandle *handle = callback_data;
	GdkPixbuf *pixbuf;
	double scale_x, scale_y;
	gsize file_size;
	char *file_contents;

	pixbuf = NULL;
	scale_x = scale_y = 1.0;

	if (g_file_load_contents_finish (G_FILE (source_object),
					 res,
					 &file_contents, &file_size,
					 NULL, NULL)) {
		pixbuf = get_pixbuf_from_data (file_contents, file_size,
					       handle->file_path,
					       handle->base_size,
					       handle->nominal_size,
					       handle->force_nominal,
					       &scale_x, &scale_y);
		g_free (file_contents);
	}

	handle->load_func (handle,
			   handle->file_path,
			   pixbuf, scale_x, scale_y,
			   handle->load_func_user_data);

	gdk_pixbuf_unref (pixbuf);

	g_object_unref (handle->cancellable);
	g_free (handle->file_path);
	g_free (handle);
}
Beispiel #5
0
static void
load_snapshot_loaded_cb (GFile *snapshot_file,
                         GAsyncResult *result,
                         GSimpleAsyncResult *simple)
{
	EShell *shell;
	GObject *object;
	LoadContext *context;
	EMsgComposer *composer;
	CamelMimeMessage *message;
	CamelStream *camel_stream;
	gchar *contents = NULL;
	gsize length;
	GError *local_error = NULL;

	context = g_simple_async_result_get_op_res_gpointer (simple);

	g_file_load_contents_finish (
		snapshot_file, result, &contents, &length, NULL, &local_error);

	if (local_error != NULL) {
		g_warn_if_fail (contents == NULL);
		g_simple_async_result_take_error (simple, local_error);
		g_simple_async_result_complete (simple);
		return;
	}

	/* Create an in-memory buffer for the MIME parser to read from.
	 * We have to do this because CamelStreams are syncrhonous-only,
	 * and feeding the parser a direct file stream would block. */
	message = camel_mime_message_new ();
	camel_stream = camel_stream_mem_new_with_buffer (contents, length);
	camel_data_wrapper_construct_from_stream_sync (
		CAMEL_DATA_WRAPPER (message), camel_stream, NULL, &local_error);
	g_object_unref (camel_stream);
	g_free (contents);

	if (local_error != NULL) {
		g_simple_async_result_take_error (simple, local_error);
		g_simple_async_result_complete (simple);
		g_object_unref (message);
		return;
	}

	/* g_async_result_get_source_object() returns a new reference. */
	object = g_async_result_get_source_object (G_ASYNC_RESULT (simple));

	/* Create a new composer window from the loaded message and
	 * restore its snapshot file so it continues auto-saving to
	 * the same file. */
	shell = E_SHELL (object);
	g_object_ref (snapshot_file);
	composer = e_msg_composer_new_with_message (shell, message, TRUE, NULL);
	g_object_set_data_full (
		G_OBJECT (composer),
		SNAPSHOT_FILE_KEY, snapshot_file,
		(GDestroyNotify) delete_snapshot_file);
	context->composer = g_object_ref_sink (composer);
	g_object_unref (message);

	g_object_unref (object);

	g_simple_async_result_complete (simple);
	g_object_unref (simple);
}
Beispiel #6
0
static void
on_autorun_loaded (GObject      *source_object,
                   GAsyncResult *res,
                   gpointer      user_data)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
  GFile *autorun_file;
  gchar *content;
  gchar *relative_icon_path;
  gsize content_length;
  GError *error;

  relative_icon_path = NULL;

  autorun_file = G_FILE (source_object);

  error = NULL;
  if (g_file_load_contents_finish (autorun_file,
                                   res,
                                   &content,
                                   &content_length,
                                   NULL,
                                   &error))
    {
      /* Scan through for an "icon=" line. Can't use GKeyFile,
       * because .inf files aren't always valid key files
       **/
      GRegex *icon_regex;
      GMatchInfo *match_info;

      /* [^,] is because sometimes the icon= line
       * has a comma at the end
       **/
      icon_regex = g_regex_new ("icon=([^,\\r\\n]+)",
                                G_REGEX_CASELESS, 0, NULL);
      g_regex_match (icon_regex, content, 0,
                     &match_info);

      /* Even if there are multiple matches, pick only the
       * first.
       **/
      if (g_match_info_matches (match_info))
        {
          gchar *chr;
          gchar *word = g_match_info_fetch (match_info, 1);

          /* Replace '\' with '/' */
          while ((chr = strchr (word, '\\')) != NULL)
            *chr = '/';

          /* If the file name's not valid UTF-8,
           * don't even try to load it
           **/
          if (g_utf8_validate (word, -1, NULL))
            {
              relative_icon_path = word;
            }
          else
            {
              /* TODO: mark for translation. Strictly, this isn't very important; this string
               * will never be displayed since all current users of g_vfs_mount_info_query_autorun_info()
               * passes NULL for the GError**.
               */
              error = g_error_new_literal (G_IO_ERROR,
                                           G_IO_ERROR_FAILED,
                                           "Icon name is not valid UTF-8");
              g_free (word);
            }
        }

      g_match_info_free (match_info);

      g_regex_unref (icon_regex);
      g_free (content);
    }

  /* some autorun.in points to the .exe file for the icon; make sure we avoid using that */
  if (relative_icon_path != NULL)
    {
      if (!g_str_has_suffix (relative_icon_path, ".exe"))
        {
          GFile *root;

          root = g_file_get_parent (autorun_file);

          _g_find_file_insensitive_async (root,
                                          relative_icon_path,
                                          NULL,
                                          on_icon_file_located,
                                          simple);

          g_object_unref (root);
        }
      else
        {
          /* TODO: mark for translation. Strictly, this isn't very important; this string
           * will never be displayed since all current users of g_vfs_mount_info_query_autorun_info()
           * passes NULL for the GError**.
           */
          error = g_error_new_literal (G_IO_ERROR,
                                       G_IO_ERROR_FAILED,
                                       "Icon is an .exe file");
        }
    }

  if (error != NULL)
    {
      g_simple_async_result_set_from_error (simple, error);
      g_simple_async_result_complete_in_idle (simple);
      g_object_unref (simple);
      g_error_free (error);
    }

  g_free (relative_icon_path);
}
Beispiel #7
0
static void
on_xdg_volume_info_loaded (GObject      *source_object,
                           GAsyncResult *res,
                           gpointer      user_data)
{
  GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
  GFile *xdg_volume_info_file;
  gchar *content;
  gsize content_length;
  GError *error;
  GKeyFile *key_file;
  gchar *name;
  gchar *icon_name;
  gchar *icon_file;
  GIcon *icon;

  content = NULL;
  key_file = NULL;
  name = NULL;
  icon_name = NULL;
  icon_file = NULL;

  xdg_volume_info_file = G_FILE (source_object);

  error = NULL;
  if (g_file_load_contents_finish (xdg_volume_info_file,
                                   res,
                                   &content,
                                   &content_length,
                                   NULL,
                                   &error))
    {
      key_file = g_key_file_new ();
      if (!g_key_file_load_from_data (key_file,
                                      content,
                                      content_length,
                                      G_KEY_FILE_NONE,
                                      &error))
        goto out;


      name = g_key_file_get_locale_string (key_file,
                                           VOLUME_INFO_GROUP,
                                           "Name",
                                           NULL,
                                           NULL);

      icon_name = g_key_file_get_string (key_file,
                                         VOLUME_INFO_GROUP,
                                         "Icon",
                                         NULL);
      
      icon_file = g_key_file_get_string (key_file,
                                         VOLUME_INFO_GROUP,
                                         "IconFile",
                                         NULL);

      icon = NULL;
      
      if (icon_file != NULL)
        {
          GFile *dir, *f;

          dir = g_file_get_parent (xdg_volume_info_file);
          if (dir)
            {
              f = g_file_resolve_relative_path (dir, icon_file);
              if (f)
                {
                  icon = g_file_icon_new (f);
                  g_object_unref (f);
                }
              
              g_object_unref (dir);
            }
        }
            
      if (icon == NULL && icon_name != NULL)
        {
          icon = g_themed_icon_new (icon_name);
          g_themed_icon_append_name (G_THEMED_ICON (icon), "drive-removable-media");
          g_themed_icon_append_name (G_THEMED_ICON (icon), "drive-removable");
          g_themed_icon_append_name (G_THEMED_ICON (icon), "drive");
        }

      g_simple_async_result_set_op_res_gpointer (simple, icon, NULL);
      g_object_set_data_full (G_OBJECT (simple), "name", name, g_free);
      name = NULL; /* steals name */
      g_simple_async_result_complete_in_idle (simple);
      g_object_unref (simple);
    }

 out:

  if (key_file != NULL)
    g_key_file_free (key_file);

  if (error != NULL)
    {
      g_simple_async_result_set_from_error (simple, error);
      g_simple_async_result_complete_in_idle (simple);
      g_object_unref (simple);
      g_error_free (error);
    }

  g_free (name);
  g_free (icon_name);
  g_free (icon_file);
  g_free (content);
}
static void
ide_back_forward_list_load_cb (GObject      *object,
                               GAsyncResult *result,
                               gpointer      user_data)
{
  g_autoptr(GTask) task = user_data;
  g_autofree gchar *contents = NULL;
  g_auto(GStrv) lines = NULL;
  IdeBackForwardList *self;
  IdeContext *context;
  GError *error = NULL;
  GFile *file = (GFile *)object;
  gsize length = 0;
  gsize n_lines;
  gint i;

  g_assert (G_IS_FILE (file));
  g_assert (G_IS_TASK (task));

  self = g_task_get_source_object (task);
  g_assert (IDE_IS_BACK_FORWARD_LIST (self));

  context = ide_object_get_context (IDE_OBJECT (self));
  g_assert (IDE_IS_CONTEXT (context));

  if (!g_file_load_contents_finish (file, result, &contents, &length, NULL, &error))
    {
      g_task_return_error (task, error);
      return;
    }

  if (length > (10 * 1024 * 1024))
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_INVALID_DATA,
                               "Implausible file size discovered");
      return;
    }

  if (!g_utf8_validate (contents, length, NULL))
    {
      g_task_return_new_error (task,
                               G_IO_ERROR,
                               G_IO_ERROR_INVALID_DATA,
                               "The content was not UTF-8 formatted");
      return;
    }

  lines = g_strsplit (contents, "\n", 0);
  n_lines = g_strv_length (lines);

  for (i = n_lines; i > 0; i--)
    {
      const gchar *line = lines [i - 1];
      g_autoptr(IdeUri) uri = NULL;
      g_autoptr(IdeBackForwardItem) item = NULL;
      g_autofree gchar *new_style_uri = NULL;
      char *old_style_uri = NULL;
      guint lineno = 0;
      guint line_offset = 0;

      if (ide_str_empty0 (line))
        continue;

      /* Convert from old style "LINE OFFSET URI" to new-style "URI". */
      if (3 == sscanf (line, "%u %u %ms", &lineno, &line_offset, &old_style_uri))
        {
          line = new_style_uri = g_strdup_printf ("%s#L%u_%u", old_style_uri, lineno, line_offset);
          free (old_style_uri);
        }

      uri = ide_uri_new (line, 0, &error);

      if (uri == NULL)
        {
          g_task_return_error (task, error);
          return;
        }

      item = ide_back_forward_item_new (context, uri);
      ide_back_forward_list_push (self, item);
    }

  g_task_return_boolean (task, TRUE);
}
static void
load_desktop_file (GFile        *file,
                   GAsyncResult *result,
                   GAsyncResult *init_result)
{
  HDImagesetBackground *background = HD_IMAGESET_BACKGROUND (g_async_result_get_source_object (init_result));
  HDImagesetBackgroundPrivate *priv = background->priv;
  char *file_contents = NULL;
  gsize file_size;
  char *etag;
  GKeyFile *key_file = NULL;
  guint i;
  GError *error = NULL;
  char *type = NULL;
  g_file_load_contents_finish (file,
                               result,
                               &file_contents,
                               &file_size,
                               &etag,
                               &error);
  if (error)
    {
      g_simple_async_result_set_from_error (G_SIMPLE_ASYNC_RESULT (init_result),
                                            error);
      g_error_free (error);
      goto complete;
    }
  
  key_file = g_key_file_new ();
  g_key_file_load_from_data (key_file,
                             file_contents,
                             file_size,
                             G_KEY_FILE_NONE,
                             &error);
  if (error)
    {
      g_simple_async_result_set_from_error (G_SIMPLE_ASYNC_RESULT (init_result),
                                            error);
      g_error_free (error);
      goto complete;
    }

  type = g_key_file_get_string (key_file,
                                G_KEY_FILE_DESKTOP_GROUP,
                                G_KEY_FILE_DESKTOP_KEY_TYPE,
                                &error);
  if (error)
    {
      g_simple_async_result_set_from_error (G_SIMPLE_ASYNC_RESULT (init_result),
                                            error);
      g_error_free (error);
      goto complete;
    }
  else if (g_strcmp0 (type, KEY_FILE_BACKGROUND_VALUE_TYPE) != 0)
    {
      g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (init_result),
                                       G_IO_ERROR,
                                       G_IO_ERROR_FAILED,
                                       "Not a valid imageset .desktop file. Type needs to be Background Image");
    }

  priv->name = g_key_file_get_string (key_file,
                                      G_KEY_FILE_DESKTOP_GROUP,
                                      G_KEY_FILE_DESKTOP_KEY_NAME,
                                      &error);
  if (error)
    {
      g_simple_async_result_set_from_error (G_SIMPLE_ASYNC_RESULT (init_result),
                                            error);
      g_error_free (error);
      goto complete;
    }

  int max_value = HD_DESKTOP_VIEWS;  

  if(hd_backgrounds_is_portrait_wallpaper_enabled (hd_backgrounds_get ()))
    max_value += HD_DESKTOP_VIEWS;

  for (i = 0; i < max_value; i++)
    {
      gchar *key, *value;
      GFile *image_file;

      if (i >= HD_DESKTOP_VIEWS)
        key = g_strdup_printf ("X-Portrait-File%u", i + 1 - HD_DESKTOP_VIEWS);
      else
        key = g_strdup_printf ("X-File%u", i + 1);

      value = g_key_file_get_string  (key_file,
                                      G_KEY_FILE_DESKTOP_GROUP,
                                      key,
                                      &error);
      g_free (key);

      if (error)
        {
          g_error_free (error);
          error = NULL;          
          g_free (value);
          continue;
        }

      g_strstrip (value);
      if (g_path_is_absolute (value))
        image_file = g_file_new_for_path (value);
      else
        {
          GFile *desktop_parent = g_file_get_parent (file);
          image_file = g_file_get_child (desktop_parent,
                                         value);
          g_object_unref (desktop_parent);
        }

      g_free (value);

      if (g_file_query_exists (image_file,
                               NULL))
        {
          hd_object_vector_push_back (priv->image_files,
                                      image_file);
          g_object_unref (image_file);
        }
      else
        {
          char *path = g_file_get_path (image_file);
          g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (init_result),
                                           G_IO_ERROR,
                                           G_IO_ERROR_NOT_FOUND,
                                           "Could not find file %s",
                                           path);
          g_object_unref (image_file);
          g_free (path);
          goto complete;
        }
    }

  g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (init_result),
                                             TRUE);

complete:
  g_free (file_contents);
  g_free (type);
  if (key_file)
    g_key_file_free (key_file);

  g_simple_async_result_complete (G_SIMPLE_ASYNC_RESULT (init_result));
}