~midori/midori/cmake-make-dist

« back to all changes in this revision

Viewing changes to src/midori-trash.c

  • Committer: Christian Dywan
  • Date: 2008-06-01 21:47:27 UTC
  • Revision ID: git-v1:b511f12b9b4b063610161f2229b94a24a86be0fc
Rename folder 'src' to 'midori'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
3
 
 
4
 
 This library is free software; you can redistribute it and/or
5
 
 modify it under the terms of the GNU Lesser General Public
6
 
 License as published by the Free Software Foundation; either
7
 
 version 2.1 of the License, or (at your option) any later version.
8
 
 
9
 
 See the file COPYING for the full license text.
10
 
*/
11
 
 
12
 
#include "midori-trash.h"
13
 
 
14
 
#include "sokoke.h"
15
 
#include <glib/gi18n.h>
16
 
 
17
 
G_DEFINE_TYPE (MidoriTrash, midori_trash, G_TYPE_OBJECT)
18
 
 
19
 
struct _MidoriTrashPrivate
20
 
{
21
 
    guint limit;
22
 
    KatzeXbelItem* xbel_folder;
23
 
};
24
 
 
25
 
#define MIDORI_TRASH_GET_PRIVATE(obj) \
26
 
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
27
 
     MIDORI_TYPE_TRASH, MidoriTrashPrivate))
28
 
 
29
 
enum
30
 
{
31
 
    PROP_0,
32
 
 
33
 
    PROP_LIMIT
34
 
};
35
 
 
36
 
enum {
37
 
    INSERTED,
38
 
    REMOVED,
39
 
 
40
 
    LAST_SIGNAL
41
 
};
42
 
 
43
 
static guint signals[LAST_SIGNAL];
44
 
 
45
 
static void
46
 
midori_trash_finalize (GObject* object);
47
 
 
48
 
static void
49
 
midori_trash_set_property (GObject*      object,
50
 
                           guint         prop_id,
51
 
                           const GValue* value,
52
 
                           GParamSpec*   pspec);
53
 
 
54
 
static void
55
 
midori_trash_get_property (GObject*    object,
56
 
                           guint       prop_id,
57
 
                           GValue*     value,
58
 
                           GParamSpec* pspec);
59
 
 
60
 
static void
61
 
midori_trash_class_init (MidoriTrashClass* class)
62
 
{
63
 
    signals[INSERTED] = g_signal_new (
64
 
        "inserted",
65
 
        G_TYPE_FROM_CLASS(class),
66
 
        (GSignalFlags)(G_SIGNAL_RUN_LAST),
67
 
        G_STRUCT_OFFSET (MidoriTrashClass, inserted),
68
 
        0,
69
 
        NULL,
70
 
        g_cclosure_marshal_VOID__UINT,
71
 
        G_TYPE_NONE, 1,
72
 
        G_TYPE_UINT);
73
 
 
74
 
    signals[REMOVED] = g_signal_new (
75
 
        "removed",
76
 
        G_TYPE_FROM_CLASS(class),
77
 
        (GSignalFlags)(G_SIGNAL_RUN_LAST),
78
 
        G_STRUCT_OFFSET (MidoriTrashClass, removed),
79
 
        0,
80
 
        NULL,
81
 
        g_cclosure_marshal_VOID__UINT,
82
 
        G_TYPE_NONE, 1,
83
 
        G_TYPE_UINT);
84
 
 
85
 
    GObjectClass* gobject_class = G_OBJECT_CLASS (class);
86
 
    gobject_class->finalize = midori_trash_finalize;
87
 
    gobject_class->set_property = midori_trash_set_property;
88
 
    gobject_class->get_property = midori_trash_get_property;
89
 
 
90
 
    GParamFlags flags = G_PARAM_READWRITE | G_PARAM_CONSTRUCT;
91
 
 
92
 
    g_object_class_install_property (gobject_class,
93
 
                                     PROP_LIMIT,
94
 
                                     g_param_spec_uint (
95
 
                                     "limit",
96
 
                                     "Limit",
97
 
                                     _("The maximum number of items"),
98
 
                                     0, G_MAXUINT, 10,
99
 
                                     flags));
100
 
 
101
 
    g_type_class_add_private (class, sizeof (MidoriTrashPrivate));
102
 
}
103
 
 
104
 
 
105
 
 
106
 
static void
107
 
midori_trash_init (MidoriTrash* trash)
108
 
{
109
 
    trash->priv = MIDORI_TRASH_GET_PRIVATE (trash);
110
 
 
111
 
    MidoriTrashPrivate* priv = trash->priv;
112
 
 
113
 
    priv->xbel_folder = katze_xbel_folder_new ();
114
 
}
115
 
 
116
 
static void
117
 
midori_trash_finalize (GObject* object)
118
 
{
119
 
    MidoriTrash* trash = MIDORI_TRASH (object);
120
 
    MidoriTrashPrivate* priv = trash->priv;
121
 
 
122
 
    katze_xbel_item_unref (priv->xbel_folder);
123
 
 
124
 
    G_OBJECT_CLASS (midori_trash_parent_class)->finalize (object);
125
 
}
126
 
 
127
 
static void
128
 
midori_trash_set_property (GObject*      object,
129
 
                           guint         prop_id,
130
 
                           const GValue* value,
131
 
                           GParamSpec*   pspec)
132
 
{
133
 
    MidoriTrash* trash = MIDORI_TRASH (object);
134
 
    MidoriTrashPrivate* priv = trash->priv;
135
 
 
136
 
    switch (prop_id)
137
 
    {
138
 
    case PROP_LIMIT:
139
 
        priv->limit = g_value_get_uint (value);
140
 
        break;
141
 
    default:
142
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143
 
        break;
144
 
    }
145
 
}
146
 
 
147
 
static void
148
 
midori_trash_get_property (GObject*    object,
149
 
                           guint       prop_id,
150
 
                           GValue*     value,
151
 
                           GParamSpec* pspec)
152
 
{
153
 
    MidoriTrash* trash = MIDORI_TRASH (object);
154
 
    MidoriTrashPrivate* priv = trash->priv;
155
 
 
156
 
    switch (prop_id)
157
 
    {
158
 
    case PROP_LIMIT:
159
 
        g_value_set_uint (value, priv->limit);
160
 
        break;
161
 
    default:
162
 
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
163
 
        break;
164
 
    }
165
 
}
166
 
 
167
 
/**
168
 
 * midori_trash_new:
169
 
 * @limit: the maximum number of items
170
 
 *
171
 
 * Creates a new #MidoriTrash that can contain a specified number of items,
172
 
 * meaning that each additional item will replace the oldest existing item.
173
 
 *
174
 
 * The value 0 for @limit actually means that there is no limit.
175
 
 *
176
 
 * You will typically want to assign this to a #MidoriBrowser.
177
 
 *
178
 
 * Return value: a new #MidoriTrash
179
 
 **/
180
 
MidoriTrash*
181
 
midori_trash_new (guint limit)
182
 
{
183
 
    MidoriTrash* trash = g_object_new (MIDORI_TYPE_TRASH,
184
 
                                       "limit", limit,
185
 
                                       NULL);
186
 
 
187
 
    return trash;
188
 
}
189
 
 
190
 
/**
191
 
 * midori_trash_is_empty:
192
 
 * @trash: a #MidoriTrash
193
 
 *
194
 
 * Determines whether the @trash contains no items.
195
 
 *
196
 
 * Return value: %TRUE if there are no items, %FALSE otherwise
197
 
 **/
198
 
gboolean
199
 
midori_trash_is_empty (MidoriTrash* trash)
200
 
{
201
 
    g_return_val_if_fail (MIDORI_IS_TRASH (trash), FALSE);
202
 
 
203
 
    MidoriTrashPrivate* priv = trash->priv;
204
 
 
205
 
    return katze_xbel_folder_is_empty (priv->xbel_folder);
206
 
}
207
 
 
208
 
/**
209
 
 * midori_trash_get_n_items:
210
 
 * @trash: a #MidoriTrash
211
 
 *
212
 
 * Determines the number of items in @trash.
213
 
 *
214
 
 * Return value: the current number of items
215
 
 **/
216
 
guint
217
 
midori_trash_get_n_items (MidoriTrash* trash)
218
 
{
219
 
    g_return_val_if_fail (MIDORI_IS_TRASH (trash), 0);
220
 
 
221
 
    MidoriTrashPrivate* priv = trash->priv;
222
 
 
223
 
    return katze_xbel_folder_get_n_items (priv->xbel_folder);
224
 
}
225
 
 
226
 
/**
227
 
 * midori_trash_get_nth_xbel_item:
228
 
 * @trash: a #MidoriTrash
229
 
 * @n: the index of an item
230
 
 *
231
 
 * Retrieve an item contained in @trash by its index.
232
 
 *
233
 
 * Note that you mustn't unref this item.
234
 
 *
235
 
 * Return value: the index at the given index or %NULL
236
 
 **/
237
 
KatzeXbelItem*
238
 
midori_trash_get_nth_xbel_item (MidoriTrash* trash,
239
 
                                guint        n)
240
 
{
241
 
    g_return_val_if_fail (MIDORI_IS_TRASH (trash), 0);
242
 
 
243
 
    MidoriTrashPrivate* priv = trash->priv;
244
 
 
245
 
    return katze_xbel_folder_get_nth_item (priv->xbel_folder, n);
246
 
}
247
 
 
248
 
/**
249
 
 * midori_trash_prepend_xbel_item:
250
 
 * @trash: a #MidoriTrash
251
 
 * @xbel_item: a #KatzeXbelItem
252
 
 *
253
 
 * Prepends a #KatzeXbelItem to @trash.
254
 
 *
255
 
 * The item is copied. If there is a limit set, the oldest item is
256
 
 * removed automatically.
257
 
 *
258
 
 * Return value: %TRUE if there are no items, %FALSE otherwise
259
 
 **/
260
 
void
261
 
midori_trash_prepend_xbel_item (MidoriTrash*   trash,
262
 
                                KatzeXbelItem* xbel_item)
263
 
{
264
 
    g_return_if_fail (MIDORI_IS_TRASH (trash));
265
 
 
266
 
    MidoriTrashPrivate* priv = trash->priv;
267
 
 
268
 
    KatzeXbelItem* copy = katze_xbel_item_copy (xbel_item);
269
 
    katze_xbel_folder_prepend_item (priv->xbel_folder, copy);
270
 
    g_signal_emit (trash, signals[INSERTED], 0, 0);
271
 
    guint n = katze_xbel_folder_get_n_items (priv->xbel_folder);
272
 
    if (n > 10)
273
 
    {
274
 
        KatzeXbelItem* item = katze_xbel_folder_get_nth_item (priv->xbel_folder,
275
 
                                                              n - 1);
276
 
        g_signal_emit (trash, signals[REMOVED], 0, n - 1);
277
 
        katze_xbel_item_unref (item);
278
 
    }
279
 
}
280
 
 
281
 
/**
282
 
 * midori_trash_remove_nth_item:
283
 
 * @trash: a #MidoriTrash
284
 
 * @n: the index of an item
285
 
 *
286
 
 * Removes the item at the specified position from @trash.
287
 
 *
288
 
 * Nothing happens if the function fails.
289
 
 **/
290
 
void
291
 
midori_trash_remove_nth_item (MidoriTrash* trash,
292
 
                              guint        n)
293
 
{
294
 
    g_return_if_fail (MIDORI_IS_TRASH (trash));
295
 
 
296
 
    MidoriTrashPrivate* priv = trash->priv;
297
 
 
298
 
    KatzeXbelItem* item = katze_xbel_folder_get_nth_item (priv->xbel_folder, n);
299
 
    if (!n)
300
 
        return;
301
 
    katze_xbel_folder_remove_item (priv->xbel_folder, item);
302
 
    g_signal_emit (trash, signals[REMOVED], 0, n);
303
 
    katze_xbel_item_unref (item);
304
 
}
305
 
 
306
 
/**
307
 
 * midori_trash_empty:
308
 
 * @trash: a #MidoriTrash
309
 
 *
310
 
 * Deletes all items currently contained in @trash.
311
 
 **/
312
 
void
313
 
midori_trash_empty (MidoriTrash* trash)
314
 
{
315
 
    g_return_if_fail (MIDORI_IS_TRASH (trash));
316
 
 
317
 
    MidoriTrashPrivate* priv = trash->priv;
318
 
 
319
 
    guint n = katze_xbel_folder_get_n_items (priv->xbel_folder);
320
 
    guint i;
321
 
    for (i = 0; i < n; i++)
322
 
    {
323
 
        KatzeXbelItem* item = katze_xbel_folder_get_nth_item (priv->xbel_folder,
324
 
                                                              i);
325
 
        g_signal_emit (trash, signals[REMOVED], 0, i);
326
 
        katze_xbel_item_unref (item);
327
 
    }
328
 
}