Ejemplo n.º 1
0
void
nautilus_keyfile_metadata_set_stringv (NautilusFile       *file,
                                       const char         *keyfile_filename,
                                       const char         *name,
                                       const char         *key,
                                       const char * const *stringv)
{
    GKeyFile *keyfile;
    guint length;
    gchar **actual_stringv = NULL;
    gboolean free_strv = FALSE;

    keyfile = get_keyfile (keyfile_filename);

    /* if we would be setting a single-length strv, append a fake
     * terminator to the array, to be able to differentiate it later from
     * the single string case
     */
    length = g_strv_length ((gchar **) stringv);

    if (length == 1)
    {
        actual_stringv = g_malloc0 (3 * sizeof (gchar *));
        actual_stringv[0] = (gchar *) stringv[0];
        actual_stringv[1] = STRV_TERMINATOR;
        actual_stringv[2] = NULL;

        length = 2;
        free_strv = TRUE;
    }
    else
    {
        actual_stringv = (gchar **) stringv;
    }

    g_key_file_set_string_list (keyfile,
                                name,
                                key,
                                (const gchar **) actual_stringv,
                                length);

    save_in_idle (keyfile_filename);

    if (nautilus_keyfile_metadata_update_from_keyfile (file, keyfile_filename, name))
    {
        nautilus_file_changed (file);
    }

    if (free_strv)
    {
        g_free (actual_stringv);
    }
}
Ejemplo n.º 2
0
void
nautilus_keyfile_metadata_set_string (NautilusFile *file,
                                      const char   *keyfile_filename,
                                      const gchar  *name,
                                      const gchar  *key,
                                      const gchar  *string)
{
    GKeyFile *keyfile;

    keyfile = get_keyfile (keyfile_filename);

    g_key_file_set_string (keyfile,
                           name,
                           key,
                           string);

    save_in_idle (keyfile_filename);

    if (nautilus_keyfile_metadata_update_from_keyfile (file, keyfile_filename, name))
    {
        nautilus_file_changed (file);
    }
}
Ejemplo n.º 3
0
gboolean
imgur_forget_record (const gchar *record_name)
{
	gboolean result = TRUE;
	gchar *path = get_path ();
	gchar *filename = get_filename (path);
	GKeyFile *keyfile = get_keyfile (path);
	gchar *local_thumbnail = get_local_thumbnail (path,
		keyfile,
		record_name);
	gchar *temp;

	if (g_file_test (local_thumbnail,
		G_FILE_TEST_IS_REGULAR))
	{
		g_unlink (local_thumbnail);
	}

	result = g_key_file_remove_group (keyfile,
		record_name,
		NULL);

	temp = g_key_file_to_data (keyfile,
		NULL, NULL);

	g_file_set_contents (filename,
		temp,
		-1, NULL);

	g_free (temp);
	g_free (filename);
	g_free (path);

	/* FIXME: free the keyfile? */
	return result;
}
Ejemplo n.º 4
0
gboolean
nautilus_keyfile_metadata_update_from_keyfile (NautilusFile *file,
                                               const char   *keyfile_filename,
                                               const gchar  *name)
{
    gchar **keys, **values;
    const gchar *actual_values[2];
    const gchar *key, *value;
    gchar *gio_key;
    gsize length, values_length;
    GKeyFile *keyfile;
    GFileInfo *info;
    gint idx;
    gboolean res;

    keyfile = get_keyfile (keyfile_filename);

    keys = g_key_file_get_keys (keyfile,
                                name,
                                &length,
                                NULL);

    if (keys == NULL)
    {
        return FALSE;
    }

    info = g_file_info_new ();

    for (idx = 0; idx < length; idx++)
    {
        key = keys[idx];
        values = g_key_file_get_string_list (keyfile,
                                             name,
                                             key,
                                             &values_length,
                                             NULL);

        gio_key = g_strconcat ("metadata::", key, NULL);

        if (values_length < 1)
        {
            continue;
        }
        else if (values_length == 1)
        {
            g_file_info_set_attribute_string (info,
                                              gio_key,
                                              values[0]);
        }
        else if (values_length == 2)
        {
            /* deal with the fact that single-length strv are stored
             * with an additional terminator in the keyfile string, to differentiate
             * them from the regular string case.
             */
            value = values[1];

            if (g_strcmp0 (value, STRV_TERMINATOR) == 0)
            {
                /* if the 2nd value is the terminator, remove it */
                actual_values[0] = values[0];
                actual_values[1] = NULL;

                g_file_info_set_attribute_stringv (info,
                                                   gio_key,
                                                   (gchar **) actual_values);
            }
            else
            {
                /* otherwise, set it as a regular strv */
                g_file_info_set_attribute_stringv (info,
                                                   gio_key,
                                                   values);
            }
        }
        else
        {
            g_file_info_set_attribute_stringv (info,
                                               gio_key,
                                               values);
        }

        g_free (gio_key);
        g_strfreev (values);
    }

    res = nautilus_file_update_metadata_from_info (file, info);

    g_strfreev (keys);
    g_object_unref (info);

    return res;
}
Ejemplo n.º 5
0
GPtrArray*
imgur_list_records (void)
{
	GPtrArray *result;
	GKeyFile *keyfile;
	gchar **entries, **cursor;
	gchar *path;
	GList *candidates = NULL, *candidate_cursor;
	long count = 0;

	path = get_path ();
	keyfile = get_keyfile (path);

	if (!keyfile)
	{
		g_free (path);
		return g_ptr_array_new ();
	}

	entries = g_key_file_get_groups (keyfile, NULL);

	for (cursor=entries; *cursor; cursor++)
	{
		gchar *thumbnail,
			*thumbnail_extension,
			*time,
			*our_file_uri,
			*our_thumbnail;
		long timestamp;
		ListEntry *entry;
		GValue *value;

		thumbnail = g_key_file_get_string (keyfile,
			*cursor,
			"small_thumbnail",
			NULL);

		if (!thumbnail)
		{
			g_warning ("Group %s has no thumbnail",
				*cursor);
			continue;
		}

		thumbnail_extension = strrchr (thumbnail, '.');

		if (!thumbnail_extension)
		{
			g_warning ("Group %s has an invalid thumbnail",
				*cursor);
			continue;
		}

		time = g_key_file_get_string (keyfile,
			*cursor,
			"time",
			NULL);

		if (time)
		{
			timestamp = atol (time);
		}
		else
		{
			/* well, it won't kill us */
			timestamp = 0;
		}

		our_thumbnail = g_strdup_printf (
			"%s/%s%s",
			path,
			*cursor,
			thumbnail_extension);

		if (!g_file_test (our_thumbnail,
			G_FILE_TEST_IS_REGULAR))
		{
			g_free (time);
			g_free (thumbnail);
			g_free (our_thumbnail);
			continue;
		}

		our_file_uri = g_filename_to_uri (our_thumbnail,
				NULL,
				/* FIXME: Should we really be ignoring errors? */
				NULL);

		entry = g_malloc (sizeof (ListEntry));
		entry->time = timestamp;
		entry->details = g_value_array_new (2);

		/* FIXME Check this carefully for memory leaks */
		value = g_malloc0 (sizeof (GValue));
		g_value_init (value, G_TYPE_STRING);
		g_value_set_string (value, *cursor);
		g_value_array_append (entry->details, value);

		value = g_malloc0 (sizeof (GValue));
		g_value_init (value, G_TYPE_STRING);
		g_value_set_string (value, our_file_uri);
		g_value_array_append (entry->details, value);

		count++;
		candidates = g_list_prepend (candidates,
			entry);

		g_free (time);
		g_free (thumbnail);
		g_free (our_file_uri);
	}

	g_strfreev (entries);
	g_free (path);

	candidates = g_list_sort (candidates,
		compare_entries);

	result = g_ptr_array_sized_new (count);

	/* Now go through and pick out the details */

	for (candidate_cursor = candidates;
		candidate_cursor;
		candidate_cursor = candidate_cursor->next)
	{
		ListEntry *entry = (ListEntry*) candidate_cursor->data;

		g_ptr_array_add (result, entry->details);
		g_free (entry);
	}

	g_list_free (candidates);

	return result;
}
Ejemplo n.º 6
0
GHashTable*
imgur_get_record (const gchar* record_name)
{
	gchar *path = get_path ();
	GKeyFile *keyfile = get_keyfile (path);
	GHashTable *result = g_hash_table_new (g_str_hash, g_str_equal);
	gchar **keys, **cursor;
	gchar *local_thumbnail = NULL;
	if (!keyfile)
	{
		g_free (path);
		return result;
	}

	keys = g_key_file_get_keys (keyfile,
		record_name,
		NULL, NULL);

	if (!keys)
	{
		g_free (path);
		g_key_file_free (keyfile);
		return result;
	}

	for (cursor = keys; *cursor; cursor++)
	{
		gchar *value = g_key_file_get_string (keyfile,
				record_name,
				*cursor,
				NULL);

		add_hash_entry (result,
			*cursor,
			value);
	}

	local_thumbnail = get_local_thumbnail (path,
		keyfile,
		record_name);

	if (g_file_test (local_thumbnail,
		G_FILE_TEST_IS_REGULAR))
	{
		add_hash_entry (result,
			"local_thumbnail",
			local_thumbnail);
	}
	g_free (local_thumbnail);

	/* Since we took it out before storage: */
	add_hash_entry (result,
		"image_hash",
		record_name);

	/* FIXME: free the keyfile? */
	g_strfreev (keys);
	g_free (path);

	return result;
}