~midori/midori/fix-1094858

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
 Copyright (C) 2008-2011 Christian Dywan <christian@twotoasts.de>

 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
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#include "katze-array.h"

#include "katze-utils.h"
#include "marshal.h"

#include <glib/gi18n.h>
#include <string.h>

/**
 * SECTION:katze-array
 * @short_description: A type aware item container
 * @see_also: #KatzeList
 *
 * #KatzeArray is a type aware container for items.
 */

struct _KatzeArray
{
    KatzeItem parent_instance;

    GType type;
    GList* items;
};

struct _KatzeArrayClass
{
    KatzeItemClass parent_class;

    /* Signals */
    void
    (*add_item)               (KatzeArray* array,
                               gpointer    item);
    void
    (*remove_item)            (KatzeArray* array,
                               gpointer    item);
    void
    (*move_item)              (KatzeArray* array,
                               gpointer    item,
                               gint        index);
    void
    (*clear)                  (KatzeArray* array);

    void
    (*update)                 (KatzeArray* array);
};

G_DEFINE_TYPE (KatzeArray, katze_array, KATZE_TYPE_ITEM);

enum {
    ADD_ITEM,
    REMOVE_ITEM,
    MOVE_ITEM,
    CLEAR,
    UPDATE,

    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

GList* kalistglobal;

static void
katze_array_finalize (GObject* object);

static void
_katze_array_update (KatzeArray* array)
{
    g_object_set_data (G_OBJECT (array), "last-update",
                       GINT_TO_POINTER (time (NULL)));
}

static void
_katze_array_add_item (KatzeArray* array,
                       gpointer    item)
{
    GType type = G_OBJECT_TYPE (item);
    g_object_ref (item);
    if (g_type_is_a (type, KATZE_TYPE_ITEM))
        katze_item_set_parent (item, array);

    array->items = g_list_append (array->items, item);
    _katze_array_update (array);
}

static void
_katze_array_remove_item (KatzeArray* array,
                          gpointer   item)
{
    array->items = g_list_remove (array->items, item);

    if (KATZE_IS_ITEM (item))
        katze_item_set_parent (item, NULL);
    g_object_unref (item);
    _katze_array_update (array);
}

static void
_katze_array_move_item (KatzeArray* array,
                        gpointer    item,
                        gint        position)
{
    array->items = g_list_remove (array->items, item);
    array->items = g_list_insert (array->items, item, position);
    _katze_array_update (array);
}

static void
_katze_array_clear (KatzeArray* array)
{
    GObject* item;

    while ((item = g_list_nth_data (array->items, 0)))
        g_signal_emit (array, signals[REMOVE_ITEM], 0, item);
    g_list_free (array->items);
    array->items = NULL;
    _katze_array_update (array);
}

static void
katze_array_class_init (KatzeArrayClass* class)
{
    GObjectClass* gobject_class;

    gobject_class = G_OBJECT_CLASS (class);
    gobject_class->finalize = katze_array_finalize;

    signals[ADD_ITEM] = g_signal_new (
        "add-item",
        G_TYPE_FROM_CLASS (class),
        (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
        G_STRUCT_OFFSET (KatzeArrayClass, add_item),
        0,
        NULL,
        g_cclosure_marshal_VOID__POINTER,
        G_TYPE_NONE, 1,
        G_TYPE_POINTER);

    signals[REMOVE_ITEM] = g_signal_new (
        "remove-item",
        G_TYPE_FROM_CLASS (class),
        (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
        G_STRUCT_OFFSET (KatzeArrayClass, remove_item),
        0,
        NULL,
        g_cclosure_marshal_VOID__POINTER,
        G_TYPE_NONE, 1,
        G_TYPE_POINTER);

    /**
     * KatzeArray::move-item:
     * @array: the object on which the signal is emitted
     * @item: the item being moved
     * @position: the new position of the item
     *
     * An item is moved to a new position.
     *
     * Since: 0.1.6
     **/
    signals[MOVE_ITEM] = g_signal_new (
        "move-item",
        G_TYPE_FROM_CLASS (class),
        (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
        G_STRUCT_OFFSET (KatzeArrayClass, move_item),
        0,
        NULL,
        midori_cclosure_marshal_VOID__POINTER_INT,
        G_TYPE_NONE, 2,
        G_TYPE_POINTER,
        G_TYPE_INT);

    signals[CLEAR] = g_signal_new (
        "clear",
        G_TYPE_FROM_CLASS (class),
        (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
        G_STRUCT_OFFSET (KatzeArrayClass, clear),
        0,
        NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    /**
     * KatzeArray::update:
     * @array: the object on which the signal is emitted
     *
     * The array changed and any display widgets should
     * be updated.
     *
     * Since: 0.3.0
     **/
    signals[UPDATE] = g_signal_new (
        "update",
        G_TYPE_FROM_CLASS (class),
        (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
        G_STRUCT_OFFSET (KatzeArrayClass, update),
        0,
        NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    gobject_class = G_OBJECT_CLASS (class);
    gobject_class->finalize = katze_array_finalize;

    class->add_item = _katze_array_add_item;
    class->remove_item = _katze_array_remove_item;
    class->move_item = _katze_array_move_item;
    class->clear = _katze_array_clear;
    class->update = _katze_array_update;
}

static void
katze_array_init (KatzeArray* array)
{
    array->type = G_TYPE_OBJECT;
    array->items = NULL;
}

static void
katze_array_finalize (GObject* object)
{
    KatzeArray* array = KATZE_ARRAY (object);
    GList* items;

    for (items = array->items; items; items = g_list_next (items))
        g_object_unref (items->data);
    g_list_free (array->items);

    G_OBJECT_CLASS (katze_array_parent_class)->finalize (object);
}

/**
 * katze_array_new:
 * @type: the expected item type
 *
 * Creates a new #KatzeArray for @type items.
 *
 * The array will keep a reference on each object until
 * it is removed from the array.
 *
 * Return value: a new #KatzeArray
 **/
KatzeArray*
katze_array_new (GType type)
{
    KatzeArray* array;

    g_return_val_if_fail (g_type_is_a (type, G_TYPE_OBJECT), NULL);

    array = g_object_new (KATZE_TYPE_ARRAY, NULL);
    array->type = type;

    return array;
}

/**
 * katze_array_is_a:
 * @array: a #KatzeArray
 * @is_a_type: type to compare with
 *
 * Checks whether the array is compatible
 * with items of the specified type.
 *
 * Retur value: %TRUE if @array is compatible with @is_a_type
 **/
gboolean
katze_array_is_a (KatzeArray* array,
                  GType       is_a_type)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), FALSE);

    return g_type_is_a (array->type, is_a_type);
}

/**
 * katze_array_add_item:
 * @array: a #KatzeArray
 * @item: an item
 *
 * Adds an item to the array.
 *
 * If @item is a #KatzeItem its parent is set accordingly.
 **/
void
katze_array_add_item (KatzeArray* array,
                      gpointer    item)
{
    g_return_if_fail (KATZE_IS_ARRAY (array));

    g_signal_emit (array, signals[ADD_ITEM], 0, item);
}

/**
 * katze_array_remove_item:
 * @array: a #KatzeArray
 * @item: an item
 *
 * Removes an item from the array.
 *
 * If @item is a #KatzeItem its parent is unset accordingly.
 **/
void
katze_array_remove_item (KatzeArray* array,
                         gpointer    item)
{
    g_return_if_fail (KATZE_IS_ARRAY (array));

    g_signal_emit (array, signals[REMOVE_ITEM], 0, item);
}

/**
 * katze_array_get_nth_item:
 * @array: a #KatzeArray
 * @n: an index in the array
 *
 * Retrieves the item in @array at the position @n.
 *
 * Return value: an item, or %NULL
 **/
gpointer
katze_array_get_nth_item (KatzeArray* array,
                          guint       n)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);

    return g_list_nth_data (array->items, n);
}

/**
 * katze_array_is_empty:
 * @array: a #KatzeArray
 *
 * Determines whether @array is empty.
 *
 * Return value: an item, or %NULL
 **/
gboolean
katze_array_is_empty (KatzeArray* array)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), TRUE);

    return !g_list_nth_data (array->items, 0);
}

/**
 * katze_array_get_item_index:
 * @array: a #KatzeArray
 * @item: an item in the array
 *
 * Retrieves the index of the item in @array.
 *
 * Return value: an item, or -1
 **/
gint
katze_array_get_item_index (KatzeArray* array,
                            gpointer    item)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), -1);

    return g_list_index (array->items, item);
}

/**
 * katze_array_find_token:
 * @array: a #KatzeArray
 * @token: a token string, or "token keywords" string
 *
 * Looks up an item in the array which has the specified token.
 *
 * This function will fail if the type of the list
 * is not based on #KatzeItem children.
 *
 * Note that @token is by definition unique to one item.
 *
 * Since 0.4.4 @token can be a "token keywords" string.
 *
 * Return value: an item, or %NULL
 **/
gpointer
katze_array_find_token (KatzeArray*  array,
                        const gchar* token)
{
    goffset token_length;
    GList* items;

    g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
    g_return_val_if_fail (katze_array_is_a (array, KATZE_TYPE_ITEM), NULL);
    g_return_val_if_fail (token != NULL, NULL);

    token_length = strchr (token, ' ') - token;
    if (token_length < 1)
        token_length = strlen (token);

    for (items = array->items; items; items = g_list_next (items))
    {
        const gchar* found_token = ((KatzeItem*)items->data)->token;
        if (found_token != NULL)
        {
            guint bigger_item = strlen (found_token) > token_length ? strlen (found_token) : token_length;

            if (strncmp (token, found_token, bigger_item) == 0)
                return items->data;
        }
    }
    return NULL;
}

/**
 * katze_array_find_uri:
 * @array: a #KatzeArray
 * @uri: an URI
 *
 * Looks up an item in the array which has the specified URI.
 *
 * This function will fail if the type of the list
 * is not based on #KatzeItem children.
 *
 * Return value: an item, or %NULL
 *
 * Since: 0.2.0
 **/
gpointer
katze_array_find_uri (KatzeArray*  array,
                      const gchar* uri)
{
    GList* items;

    g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
    g_return_val_if_fail (katze_array_is_a (array, KATZE_TYPE_ITEM), NULL);
    g_return_val_if_fail (uri != NULL, NULL);

    for (items = array->items; items; items = g_list_next (items))
    {
        const gchar* found_uri = ((KatzeItem*)items->data)->uri;
        if (found_uri != NULL && !strcmp (found_uri, uri))
            return items->data;
    }
    return NULL;
}

/**
 * katze_array_get_length:
 * @array: a #KatzeArray
 *
 * Retrieves the number of items in @array.
 *
 * Return value: the length of the list
 **/
guint
katze_array_get_length (KatzeArray* array)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), 0);

    return g_list_length (array->items);
}

/**
 * katze_array_move_item:
 * @array: a #KatzeArray
 * @item: the item being moved
 * @position: the new position of the item
 *
 * Moves @item to the position @position.
 *
 * Since: 0.1.6
 **/
void
katze_array_move_item (KatzeArray* array,
                       gpointer    item,
                       gint        position)
{
    g_return_if_fail (KATZE_IS_ARRAY (array));

    g_signal_emit (array, signals[MOVE_ITEM], 0, item, position);
}

/**
 * katze_array_get_items:
 * @array: a #KatzeArray
 *
 * Retrieves the items as a list.
 *
 * Return value: a newly allocated #GList of items
 *
 * Since: 0.2.5
 **/
GList*
katze_array_get_items (KatzeArray* array)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);

    return g_list_copy (array->items);
}

GList*
katze_array_peek_items (KatzeArray* array)
{
    g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);

    return array->items;
}

/**
 * katze_array_clear:
 * @array: a #KatzeArray
 *
 * Deletes all items currently contained in @array.
 **/
void
katze_array_clear (KatzeArray* array)
{
    g_return_if_fail (KATZE_IS_ARRAY (array));

    g_signal_emit (array, signals[CLEAR], 0);
}

/**
 * katze_array_update:
 * @array: a #KatzeArray
 *
 * Indicates that the array changed and any display
 * widgets should be updated.
 *
 * Since: 0.3.0
 **/
void
katze_array_update (KatzeArray* array)
{
    g_return_if_fail (KATZE_IS_ARRAY (array));

    g_signal_emit (array, signals[UPDATE], 0);
}