Exemplo n.º 1
0
/**
 * gwy_resource_class_load:
 * @klass: A resource class.
 *
 * Loads resources of a resources class from disk.
 *
 * Resources are loaded from system directory (and marked constant) and from
 * user directory (marked modifiable).
 **/
void
gwy_resource_class_load(GwyResourceClass *klass)
{
    gpointer type;
    gchar *path, *datadir;

    g_return_if_fail(GWY_IS_RESOURCE_CLASS(klass));
    g_return_if_fail(klass->inventory);

    gwy_inventory_forget_order(klass->inventory);

    type = GSIZE_TO_POINTER(G_TYPE_FROM_CLASS(klass));
    if (!g_slist_find(all_resources, type))
        all_resources = g_slist_prepend(all_resources, type);

    datadir = gwy_find_self_dir("data");
    path = g_build_filename(datadir, klass->name, NULL);
    g_free(datadir);
    gwy_resource_class_load_dir(path, klass, TRUE);
    g_free(path);

    path = g_build_filename(gwy_get_user_dir(), klass->name, NULL);
    gwy_resource_class_load_dir(path, klass, FALSE);
    g_free(path);

    gwy_inventory_restore_order(klass->inventory);
}
Exemplo n.º 2
0
/**
 * gwy_inventory_rename_item:
 * @inventory: An inventory.
 * @name: Name of item to rename.
 * @newname: New name of item.
 *
 * Renames an inventory item.
 *
 * If an item of name @newname is already present in @inventory, the rename
 * will fail.
 *
 * Returns: The item, for convenience.
 **/
gpointer
gwy_inventory_rename_item(GwyInventory *inventory,
                          const gchar *name,
                          const gchar *newname)
{
    gpointer mp;
    guint i;

    g_return_val_if_fail(GWY_IS_INVENTORY(inventory), NULL);
    g_return_val_if_fail(!inventory->is_const, NULL);
    g_return_val_if_fail(newname, NULL);
    g_return_val_if_fail(inventory->item_type.rename, NULL);

    if (!(i = gwy_inventory_lookup(inventory, name))) {
        g_warning("Item `%s' does not exist", name);
        return NULL;
    }
    mp = g_ptr_array_index(inventory->items, i-1);
    if (inventory->item_type.is_fixed
        && inventory->item_type.is_fixed(mp)) {
        g_warning("Cannot rename fixed item `%s'", name);
        return NULL;
    }
    if (gwy_strequal(name, newname))
        return mp;

    if (gwy_inventory_lookup(inventory, newname)) {
        g_warning("Item `%s' already exists", newname);
        return NULL;
    }

    g_hash_table_remove(inventory->hash, name);
    inventory->item_type.rename(mp, newname);
    g_hash_table_insert(inventory->hash,
                        (gpointer)inventory->item_type.get_name(mp),
                        GUINT_TO_POINTER(i));

    if (inventory->needs_reindex)
        gwy_inventory_reindex(inventory);
    if (inventory->is_sorted) {
        inventory->is_sorted = FALSE;
        gwy_inventory_restore_order(inventory);
    }

    g_signal_emit(inventory, gwy_inventory_signals[ITEM_UPDATED], 0,
                  g_array_index(inventory->idx, guint, i-1));
    if (inventory->has_default
        && (gwy_strequal(name, inventory->default_key->str)
            || gwy_strequal(newname, inventory->default_key->str)))
        g_signal_emit(inventory, gwy_inventory_signals[DEFAULT_CHANGED], 0);

    return mp;
}
Exemplo n.º 3
0
/**
 * gwy_resource_class_load:
 * @klass: A resource class.
 *
 * Loads resources of a resources class from disk.
 *
 * Resources are loaded from system directory (and marked constant) and from
 * user directory (marked modifiable).
 **/
void
gwy_resource_class_load(GwyResourceClass *klass)
{
    gchar *path, *datadir;

    g_return_if_fail(GWY_IS_RESOURCE_CLASS(klass));
    g_return_if_fail(klass->inventory);

    gwy_inventory_forget_order(klass->inventory);

    datadir = gwy_find_self_dir("data");
    path = g_build_filename(datadir, klass->name, NULL);
    g_free(datadir);
    gwy_resource_class_load_dir(path, klass, TRUE);
    g_free(path);

    path = g_build_filename(gwy_get_user_dir(), klass->name, NULL);
    gwy_resource_class_load_dir(path, klass, FALSE);
    g_free(path);

    gwy_inventory_restore_order(klass->inventory);
}
Exemplo n.º 4
0
void
test_inventory_sorting(void)
{
    GwyInventoryItemType item_type = {
        0,
        NULL,
        item_get_name,
        item_is_modifiable,
        item_compare_tricky,
        item_rename,
        item_destroy,
        item_copy,
        NULL,
        NULL,
        NULL,
    };

    GwyInventory *inventory = gwy_inventory_new();
    g_assert(GWY_IS_INVENTORY(inventory));
    item_type.type = GWY_TYPE_ITEM_TEST;
    gwy_inventory_set_item_type(inventory, &item_type);
    gboolean expect_reorder = FALSE;
    item_destroy_count = 0;
    sort_by_name = TRUE;

    guint64 insert_log = 0, update_log = 0, delete_log = 0;
    g_signal_connect(inventory, "item-inserted",
                     G_CALLBACK(record_item_change), &insert_log);
    g_signal_connect(inventory, "item-updated",
                     G_CALLBACK(record_item_change), &update_log);
    g_signal_connect(inventory, "item-deleted",
                     G_CALLBACK(record_item_change), &delete_log);
    g_signal_connect_swapped(inventory, "items-reordered",
                             G_CALLBACK(check_reordering), &expect_reorder);

    gwy_inventory_insert(inventory, item_new("Bananna", 2));
    gwy_inventory_insert(inventory, item_new("Apple", 8));
    gwy_inventory_insert(inventory, item_new("Date", 4));
    gwy_inventory_insert(inventory, item_new("Citrus", 1));
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 4);
    g_assert_cmphex(insert_log, ==, 0x1133);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);
    insert_log = 0;

    // Break the sorting order so that followig items are just appended.
    gwy_inventory_insert_nth(inventory, item_new("Turd", 0), 2);
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 5);
    g_assert_cmphex(insert_log, ==, 0x3);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);
    insert_log = 0;

    gwy_inventory_insert(inventory, item_new("Bogus", 3));
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 6);
    g_assert_cmphex(insert_log, ==, 0x6);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);
    insert_log = 0;

    g_assert_cmpuint(gwy_inventory_position(inventory, "Apple"), ==, 0);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Bananna"), ==, 1);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Turd"), ==, 2);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Citrus"), ==, 3);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Date"), ==, 4);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Bogus"), ==, 5);

    sort_by_name = FALSE;
    expect_reorder = TRUE;
    gwy_inventory_restore_order(inventory);
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 6);
    g_assert_cmphex(insert_log, ==, 0);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);

    g_assert_cmpuint(gwy_inventory_position(inventory, "Turd"), ==, 0);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Citrus"), ==, 1);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Bananna"), ==, 2);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Bogus"), ==, 3);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Date"), ==, 4);
    g_assert_cmpuint(gwy_inventory_position(inventory, "Apple"), ==, 5);

    expect_reorder = FALSE;
    g_object_unref(inventory);
    g_assert_cmpuint(item_destroy_count, ==, 6);
}
Exemplo n.º 5
0
void
test_inventory_data(void)
{
    GwyInventoryItemType item_type = {
        0,
        NULL,
        item_get_name,
        item_is_modifiable,
        item_compare,
        item_rename,
        item_destroy,
        item_copy,
        NULL,
        NULL,
        NULL,
    };

    GwyInventory *inventory = gwy_inventory_new();
    g_assert(GWY_IS_INVENTORY(inventory));
    item_type.type = GWY_TYPE_ITEM_TEST;
    gwy_inventory_set_item_type(inventory, &item_type);
    item_destroy_count = 0;

    guint64 insert_log = 0, update_log = 0, delete_log = 0;
    g_signal_connect(inventory, "item-inserted",
                     G_CALLBACK(record_item_change), &insert_log);
    g_signal_connect(inventory, "item-updated",
                     G_CALLBACK(record_item_change), &update_log);
    g_signal_connect(inventory, "item-deleted",
                     G_CALLBACK(record_item_change), &delete_log);

    gwy_inventory_insert(inventory, item_new("Fixme", -1));
    gwy_inventory_insert(inventory, item_new("Second", 2));
    gwy_inventory_insert(inventory, item_new("First", 1));
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 3);
    g_assert_cmphex(insert_log, ==, 0x121);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);
    insert_log = 0;

    gwy_inventory_updated(inventory, "Second");
    gwy_inventory_updated(inventory, "First");
    gwy_inventory_updated(inventory, "Second");
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 3);
    g_assert_cmphex(insert_log, ==, 0);
    // Inventory is sorted so "Second" comes after "Fixme".
    g_assert_cmphex(update_log, ==, 0x313);
    g_assert_cmphex(delete_log, ==, 0);
    update_log = 0;

    gwy_inventory_nth_updated(inventory, 0);
    gwy_inventory_nth_updated(inventory, 1);
    gwy_inventory_nth_updated(inventory, 2);
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 3);
    g_assert_cmpuint(gwy_listable_size(GWY_LISTABLE(inventory)), ==, 3);
    g_assert_cmphex(insert_log, ==, 0);
    g_assert_cmphex(update_log, ==, 0x123);
    g_assert_cmphex(delete_log, ==, 0);
    update_log = 0;

    GwyItemTest *itemtest;
    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get(inventory, "Fixme")));
    g_assert_cmpint(itemtest->value, ==, -1);
    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get(inventory, "Second")));
    g_assert_cmpint(itemtest->value, ==, 2);
    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get(inventory, "First")));
    g_assert_cmpint(itemtest->value, ==, 1);

    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get_nth(inventory, 0)));
    g_assert_cmpstr(itemtest->name, ==, "First");
    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get_nth(inventory, 1)));
    g_assert_cmpstr(itemtest->name, ==, "Fixme");
    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get_nth(inventory, 2)));
    g_assert_cmpstr(itemtest->name, ==, "Second");

    for (guint i = 0; i < 3; i++) {
        g_assert(gwy_inventory_get_nth(inventory, i)
                 == gwy_listable_get(GWY_LISTABLE(inventory), i));
    }

    gwy_inventory_forget_order(inventory);
    g_assert_cmphex(insert_log, ==, 0);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);

    gwy_inventory_insert(inventory, item_new("Abel", 0));
    gwy_inventory_insert(inventory, item_new("Kain", 1));
    g_assert_cmphex(insert_log, ==, 0x45);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);
    insert_log = 0;
    gwy_inventory_restore_order(inventory);
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 5);
    g_assert_cmphex(insert_log, ==, 0);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0);

    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get_nth(inventory, 0)));
    g_assert_cmpstr(itemtest->name, ==, "Abel");
    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get_nth(inventory, 3)));
    g_assert_cmpstr(itemtest->name, ==, "Kain");

    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get(inventory, "Fixme")));
    itemtest->value = 3;
    gwy_inventory_rename(inventory, "Fixme", "Third");
    g_assert_cmphex(insert_log, ==, 0x0);
    g_assert_cmphex(update_log, ==, 0x5);
    g_assert_cmphex(delete_log, ==, 0x0);
    insert_log = update_log = delete_log = 0;

    g_assert((itemtest = (GwyItemTest*)gwy_inventory_get_nth(inventory, 4)));
    g_assert_cmpstr(itemtest->name, ==, "Third");

    gwy_inventory_delete_nth(inventory, 0);
    gwy_inventory_delete(inventory, "Kain");
    g_assert_cmpuint(gwy_inventory_size(inventory), ==, 3);
    g_assert_cmphex(insert_log, ==, 0);
    g_assert_cmphex(update_log, ==, 0);
    g_assert_cmphex(delete_log, ==, 0x12);
    delete_log = 0;

    g_object_unref(inventory);
    g_assert_cmpuint(item_destroy_count, ==, 5);
}