| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
#include <config.h> |
|---|
| 21 |
|
|---|
| 22 |
#include <tny-gtk-account-list-model.h> |
|---|
| 23 |
|
|---|
| 24 |
static GObjectClass *parent_class = NULL; |
|---|
| 25 |
|
|---|
| 26 |
#include "tny-gtk-account-list-model-iterator-priv.h" |
|---|
| 27 |
|
|---|
| 28 |
GType _tny_gtk_account_list_model_iterator_get_type (void); |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
void |
|---|
| 32 |
_tny_gtk_account_list_model_iterator_set_model (TnyGtkAccountListModelIterator *self, TnyGtkAccountListModel *model) |
|---|
| 33 |
{ |
|---|
| 34 |
if (self->model) |
|---|
| 35 |
g_object_unref (self->model); |
|---|
| 36 |
|
|---|
| 37 |
self->model = g_object_ref (model); |
|---|
| 38 |
self->current = model->first; |
|---|
| 39 |
|
|---|
| 40 |
return; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
TnyIterator* |
|---|
| 46 |
_tny_gtk_account_list_model_iterator_new (TnyGtkAccountListModel *model) |
|---|
| 47 |
{ |
|---|
| 48 |
TnyGtkAccountListModelIterator *self = g_object_new (TNY_TYPE_GTK_ACCOUNT_LIST_MODEL_ITERATOR, NULL); |
|---|
| 49 |
|
|---|
| 50 |
_tny_gtk_account_list_model_iterator_set_model (self, model); |
|---|
| 51 |
|
|---|
| 52 |
return TNY_ITERATOR (self); |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
static void |
|---|
| 56 |
tny_gtk_account_list_model_iterator_instance_init (GTypeInstance *instance, gpointer g_class) |
|---|
| 57 |
{ |
|---|
| 58 |
TnyGtkAccountListModelIterator *self = (TnyGtkAccountListModelIterator *)instance; |
|---|
| 59 |
|
|---|
| 60 |
self->model = NULL; |
|---|
| 61 |
self->current = NULL; |
|---|
| 62 |
|
|---|
| 63 |
return; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
static void |
|---|
| 67 |
tny_gtk_account_list_model_iterator_finalize (GObject *object) |
|---|
| 68 |
{ |
|---|
| 69 |
TnyGtkAccountListModelIterator *self = (TnyGtkAccountListModelIterator *) object; |
|---|
| 70 |
|
|---|
| 71 |
if (self->model) |
|---|
| 72 |
g_object_unref (self->model); |
|---|
| 73 |
|
|---|
| 74 |
(*parent_class->finalize) (object); |
|---|
| 75 |
|
|---|
| 76 |
return; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
static void |
|---|
| 81 |
tny_gtk_account_list_model_iterator_next (TnyIterator *self) |
|---|
| 82 |
{ |
|---|
| 83 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 84 |
|
|---|
| 85 |
if (G_UNLIKELY (!me || !me->current || !me->model)) |
|---|
| 86 |
return; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
g_mutex_lock (me->model->iterator_lock); |
|---|
| 91 |
me->current = g_list_next (me->current); |
|---|
| 92 |
g_mutex_unlock (me->model->iterator_lock); |
|---|
| 93 |
|
|---|
| 94 |
return; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
static void |
|---|
| 98 |
tny_gtk_account_list_model_iterator_prev (TnyIterator *self) |
|---|
| 99 |
{ |
|---|
| 100 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 101 |
|
|---|
| 102 |
if (G_UNLIKELY (!me || !me->current || !me->model)) |
|---|
| 103 |
return; |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
g_mutex_lock (me->model->iterator_lock); |
|---|
| 108 |
me->current = g_list_previous (me->current); |
|---|
| 109 |
g_mutex_unlock (me->model->iterator_lock); |
|---|
| 110 |
|
|---|
| 111 |
return; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
static gboolean |
|---|
| 116 |
tny_gtk_account_list_model_iterator_is_done (TnyIterator *self) |
|---|
| 117 |
{ |
|---|
| 118 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 119 |
|
|---|
| 120 |
if (G_UNLIKELY (!me || !me->model)) |
|---|
| 121 |
return TRUE; |
|---|
| 122 |
|
|---|
| 123 |
return me->current == NULL; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
static void |
|---|
| 129 |
tny_gtk_account_list_model_iterator_first (TnyIterator *self) |
|---|
| 130 |
{ |
|---|
| 131 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 132 |
|
|---|
| 133 |
if (G_UNLIKELY (!me || !me->current || !me->model)) |
|---|
| 134 |
return; |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
g_mutex_lock (me->model->iterator_lock); |
|---|
| 141 |
me->current = me->model->first; |
|---|
| 142 |
g_mutex_unlock (me->model->iterator_lock); |
|---|
| 143 |
|
|---|
| 144 |
return; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
static void |
|---|
| 149 |
tny_gtk_account_list_model_iterator_nth (TnyIterator *self, guint nth) |
|---|
| 150 |
{ |
|---|
| 151 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 152 |
|
|---|
| 153 |
if (G_UNLIKELY (!me || !me->current || !me->model)) |
|---|
| 154 |
return; |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
g_mutex_lock (me->model->iterator_lock); |
|---|
| 161 |
me->current = g_list_nth (me->model->first, nth); |
|---|
| 162 |
g_mutex_unlock (me->model->iterator_lock); |
|---|
| 163 |
|
|---|
| 164 |
return; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
static GObject* |
|---|
| 169 |
tny_gtk_account_list_model_iterator_get_current (TnyIterator *self) |
|---|
| 170 |
{ |
|---|
| 171 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 172 |
gpointer retval; |
|---|
| 173 |
|
|---|
| 174 |
if (G_UNLIKELY (!me || !me->model)) |
|---|
| 175 |
return NULL; |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
g_mutex_lock (me->model->iterator_lock); |
|---|
| 180 |
retval = (G_UNLIKELY (me->current)) ? me->current->data : NULL; |
|---|
| 181 |
g_mutex_unlock (me->model->iterator_lock); |
|---|
| 182 |
|
|---|
| 183 |
if (retval) |
|---|
| 184 |
g_object_ref (G_OBJECT(retval)); |
|---|
| 185 |
|
|---|
| 186 |
return (GObject*)retval; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
static TnyList* |
|---|
| 191 |
tny_gtk_account_list_model_iterator_get_list (TnyIterator *self) |
|---|
| 192 |
{ |
|---|
| 193 |
TnyGtkAccountListModelIterator *me = (TnyGtkAccountListModelIterator*) self; |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
if (G_UNLIKELY (!me || !me->model)) |
|---|
| 198 |
return NULL; |
|---|
| 199 |
|
|---|
| 200 |
g_object_ref (G_OBJECT (me->model)); |
|---|
| 201 |
|
|---|
| 202 |
return TNY_LIST (me->model); |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
static void |
|---|
| 206 |
tny_iterator_init (TnyIteratorIface *klass) |
|---|
| 207 |
{ |
|---|
| 208 |
|
|---|
| 209 |
klass->next= tny_gtk_account_list_model_iterator_next; |
|---|
| 210 |
klass->prev= tny_gtk_account_list_model_iterator_prev; |
|---|
| 211 |
klass->first= tny_gtk_account_list_model_iterator_first; |
|---|
| 212 |
klass->nth= tny_gtk_account_list_model_iterator_nth; |
|---|
| 213 |
klass->get_current= tny_gtk_account_list_model_iterator_get_current; |
|---|
| 214 |
klass->get_list= tny_gtk_account_list_model_iterator_get_list; |
|---|
| 215 |
klass->is_done = tny_gtk_account_list_model_iterator_is_done; |
|---|
| 216 |
|
|---|
| 217 |
return; |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
static void |
|---|
| 221 |
tny_gtk_account_list_model_iterator_class_init (TnyGtkAccountListModelIteratorClass *klass) |
|---|
| 222 |
{ |
|---|
| 223 |
GObjectClass *object_class; |
|---|
| 224 |
|
|---|
| 225 |
parent_class = g_type_class_peek_parent (klass); |
|---|
| 226 |
object_class = (GObjectClass*) klass; |
|---|
| 227 |
|
|---|
| 228 |
object_class->finalize = tny_gtk_account_list_model_iterator_finalize; |
|---|
| 229 |
|
|---|
| 230 |
return; |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
static gpointer |
|---|
| 234 |
_tny_gtk_account_list_model_iterator_register_type (gpointer notused) |
|---|
| 235 |
{ |
|---|
| 236 |
GType type = 0; |
|---|
| 237 |
|
|---|
| 238 |
static const GTypeInfo info = |
|---|
| 239 |
{ |
|---|
| 240 |
sizeof (TnyGtkAccountListModelIteratorClass), |
|---|
| 241 |
NULL, |
|---|
| 242 |
NULL, |
|---|
| 243 |
(GClassInitFunc) tny_gtk_account_list_model_iterator_class_init, |
|---|
| 244 |
NULL, |
|---|
| 245 |
NULL, |
|---|
| 246 |
sizeof (TnyGtkAccountListModelIterator), |
|---|
| 247 |
0, |
|---|
| 248 |
tny_gtk_account_list_model_iterator_instance_init |
|---|
| 249 |
}; |
|---|
| 250 |
|
|---|
| 251 |
static const GInterfaceInfo tny_iterator_info = |
|---|
| 252 |
{ |
|---|
| 253 |
(GInterfaceInitFunc) tny_iterator_init, |
|---|
| 254 |
NULL, |
|---|
| 255 |
NULL |
|---|
| 256 |
}; |
|---|
| 257 |
|
|---|
| 258 |
type = g_type_register_static (G_TYPE_OBJECT, |
|---|
| 259 |
"TnyGtkAccountListModelIterator", |
|---|
| 260 |
&info, 0); |
|---|
| 261 |
|
|---|
| 262 |
g_type_add_interface_static (type, TNY_TYPE_ITERATOR, |
|---|
| 263 |
&tny_iterator_info); |
|---|
| 264 |
|
|---|
| 265 |
return GUINT_TO_POINTER (type); |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
GType |
|---|
| 269 |
_tny_gtk_account_list_model_iterator_get_type (void) |
|---|
| 270 |
{ |
|---|
| 271 |
static GOnce once = G_ONCE_INIT; |
|---|
| 272 |
g_once (&once, _tny_gtk_account_list_model_iterator_register_type, NULL); |
|---|
| 273 |
return GPOINTER_TO_UINT (once.retval); |
|---|
| 274 |
} |
|---|