~ubuntu-branches/ubuntu/trusty/gnome-photos/trusty

« back to all changes in this revision

Viewing changes to src/photos-view-model.c

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2014-02-12 06:40:47 UTC
  • Revision ID: package-import@ubuntu.com-20140212064047-33l9kizb5e74s7db
Tags: upstream-3.10.2
ImportĀ upstreamĀ versionĀ 3.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Photos - access, organize and share your photos on GNOME
 
3
 * Copyright Ā© 2012, 2013 Red Hat, Inc.
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
18
 * 02110-1301, USA.
 
19
 */
 
20
 
 
21
/* Based on code from:
 
22
 *   + Documents
 
23
 */
 
24
 
 
25
 
 
26
#include "config.h"
 
27
 
 
28
#include "photos-collection-manager.h"
 
29
#include "photos-enums.h"
 
30
#include "photos-item-manager.h"
 
31
#include "photos-view-model.h"
 
32
 
 
33
 
 
34
struct _PhotosViewModelPrivate
 
35
{
 
36
  PhotosBaseManager *col_mngr;
 
37
  PhotosBaseManager *item_mngr;
 
38
  PhotosWindowMode mode;
 
39
  gchar *row_ref_key;
 
40
};
 
41
 
 
42
enum
 
43
{
 
44
  PROP_0,
 
45
  PROP_MODE
 
46
};
 
47
 
 
48
 
 
49
G_DEFINE_TYPE_WITH_PRIVATE (PhotosViewModel, photos_view_model, GTK_TYPE_LIST_STORE);
 
50
 
 
51
 
 
52
static void
 
53
photos_view_model_info_set (PhotosViewModel *self, PhotosBaseItem *item, GtkTreeIter *iter)
 
54
{
 
55
  gtk_list_store_set (GTK_LIST_STORE (self),
 
56
                      iter,
 
57
                      PHOTOS_VIEW_MODEL_URN, photos_base_item_get_id (item),
 
58
                      PHOTOS_VIEW_MODEL_URI, photos_base_item_get_uri (item),
 
59
                      PHOTOS_VIEW_MODEL_NAME, photos_base_item_get_name (item),
 
60
                      PHOTOS_VIEW_MODEL_AUTHOR, photos_base_item_get_author (item),
 
61
                      PHOTOS_VIEW_MODEL_ICON, photos_base_item_get_icon (item),
 
62
                      PHOTOS_VIEW_MODEL_MTIME, photos_base_item_get_mtime (item),
 
63
                      -1);
 
64
}
 
65
 
 
66
 
 
67
static void
 
68
photos_view_model_add_item (PhotosViewModel *self, PhotosBaseItem *item)
 
69
{
 
70
  GtkTreeIter iter;
 
71
  GtkTreePath *path;
 
72
  GtkTreeRowReference *row_ref;
 
73
 
 
74
  gtk_list_store_append (GTK_LIST_STORE (self), &iter);
 
75
  photos_view_model_info_set (self, item, &iter);
 
76
 
 
77
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (self), &iter);
 
78
  row_ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (self), path);
 
79
  gtk_tree_path_free (path);
 
80
 
 
81
  g_object_set_data_full (G_OBJECT (item),
 
82
                          self->priv->row_ref_key,
 
83
                          row_ref,
 
84
                          (GDestroyNotify) gtk_tree_row_reference_free);
 
85
}
 
86
 
 
87
 
 
88
static gboolean
 
89
photos_view_model_item_removed_foreach (GtkTreeModel *model,
 
90
                                        GtkTreePath *path,
 
91
                                        GtkTreeIter *iter,
 
92
                                        gpointer user_data)
 
93
{
 
94
  PhotosBaseItem *item = PHOTOS_BASE_ITEM (user_data);
 
95
  gboolean ret_val = FALSE;
 
96
  const gchar *id;
 
97
  gchar *value;
 
98
 
 
99
  id = photos_base_item_get_id (item);
 
100
  gtk_tree_model_get (model, iter, PHOTOS_VIEW_MODEL_URN, &value, -1);
 
101
 
 
102
  if (g_strcmp0 (id, value) == 0)
 
103
    {
 
104
      gtk_list_store_remove (GTK_LIST_STORE (model), iter);
 
105
      ret_val = TRUE;
 
106
    }
 
107
 
 
108
  g_free (value);
 
109
  return ret_val;
 
110
}
 
111
 
 
112
 
 
113
static void
 
114
photos_view_model_object_removed (PhotosViewModel *self, GObject *object)
 
115
{
 
116
  PhotosBaseItem *item = PHOTOS_BASE_ITEM (object);
 
117
 
 
118
  gtk_tree_model_foreach (GTK_TREE_MODEL (self), photos_view_model_item_removed_foreach, item);
 
119
  g_object_set_data (object, self->priv->row_ref_key, NULL);
 
120
}
 
121
 
 
122
 
 
123
static void
 
124
photos_view_model_info_updated (PhotosBaseItem *item, gpointer user_data)
 
125
{
 
126
  PhotosViewModel *self = PHOTOS_VIEW_MODEL (user_data);
 
127
  PhotosViewModelPrivate *priv = self->priv;
 
128
  GObject *collection;
 
129
  GtkTreeIter iter;
 
130
  GtkTreePath *path;
 
131
  GtkTreeRowReference *row_ref;
 
132
 
 
133
  collection = photos_base_manager_get_active_object (priv->col_mngr);
 
134
  row_ref = (GtkTreeRowReference *) g_object_get_data (G_OBJECT (item), priv->row_ref_key);
 
135
 
 
136
  if (priv->mode == PHOTOS_WINDOW_MODE_COLLECTIONS)
 
137
    {
 
138
      gboolean is_collection;
 
139
 
 
140
      is_collection = photos_base_item_is_collection (item);
 
141
      if (!is_collection && row_ref != NULL && collection == NULL)
 
142
        photos_view_model_object_removed (self, G_OBJECT (item));
 
143
      else if (is_collection  && row_ref == NULL)
 
144
        photos_view_model_add_item (self, item);
 
145
    }
 
146
  else if (priv->mode == PHOTOS_WINDOW_MODE_FAVORITES)
 
147
    {
 
148
      gboolean is_favorite;
 
149
 
 
150
      is_favorite = photos_base_item_is_favorite (item);
 
151
      if (!is_favorite && row_ref != NULL && collection == NULL)
 
152
        photos_view_model_object_removed (self, G_OBJECT (item));
 
153
      else if (is_favorite  && row_ref == NULL && collection == NULL)
 
154
        photos_view_model_add_item (self, item);
 
155
    }
 
156
 
 
157
  row_ref = (GtkTreeRowReference *) g_object_get_data (G_OBJECT (item), priv->row_ref_key);
 
158
  if (row_ref != NULL)
 
159
    {
 
160
      path = gtk_tree_row_reference_get_path (row_ref);
 
161
      if (path == NULL)
 
162
        return;
 
163
 
 
164
      gtk_tree_model_get_iter (GTK_TREE_MODEL (self), &iter, path);
 
165
      photos_view_model_info_set (self, item, &iter);
 
166
    }
 
167
}
 
168
 
 
169
 
 
170
static void
 
171
photos_view_model_object_added (PhotosViewModel *self, GObject *object)
 
172
{
 
173
  PhotosBaseItem *item = PHOTOS_BASE_ITEM (object);
 
174
  PhotosViewModelPrivate *priv = self->priv;
 
175
  GObject *collection;
 
176
 
 
177
  collection = photos_base_manager_get_active_object (priv->col_mngr);
 
178
 
 
179
  if (collection == NULL)
 
180
    {
 
181
      if ((priv->mode == PHOTOS_WINDOW_MODE_COLLECTIONS && !photos_base_item_is_collection (item))
 
182
          || (priv->mode == PHOTOS_WINDOW_MODE_FAVORITES && !photos_base_item_is_favorite (item)))
 
183
        goto out;
 
184
    }
 
185
 
 
186
  photos_view_model_add_item (self, item);
 
187
 
 
188
 out:
 
189
  g_signal_connect (item, "info-updated", G_CALLBACK (photos_view_model_info_updated), self);
 
190
}
 
191
 
 
192
 
 
193
static void
 
194
photos_view_model_dispose (GObject *object)
 
195
{
 
196
  PhotosViewModel *self = PHOTOS_VIEW_MODEL (object);
 
197
  PhotosViewModelPrivate *priv = self->priv;
 
198
 
 
199
  g_clear_object (&priv->col_mngr);
 
200
  g_clear_object (&priv->item_mngr);
 
201
 
 
202
  G_OBJECT_CLASS (photos_view_model_parent_class)->dispose (object);
 
203
}
 
204
 
 
205
 
 
206
static void
 
207
photos_view_model_finalize (GObject *object)
 
208
{
 
209
  PhotosViewModel *self = PHOTOS_VIEW_MODEL (object);
 
210
 
 
211
  g_free (self->priv->row_ref_key);
 
212
 
 
213
  G_OBJECT_CLASS (photos_view_model_parent_class)->finalize (object);
 
214
}
 
215
 
 
216
 
 
217
static void
 
218
photos_view_model_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
 
219
{
 
220
  PhotosViewModel *self = PHOTOS_VIEW_MODEL (object);
 
221
  PhotosViewModelPrivate *priv = self->priv;
 
222
 
 
223
  switch (prop_id)
 
224
    {
 
225
    case PROP_MODE:
 
226
      priv->mode = (PhotosWindowMode) g_value_get_enum (value);
 
227
      priv->row_ref_key = g_strdup_printf ("row-ref-%d", priv->mode);
 
228
      break;
 
229
 
 
230
    default:
 
231
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
232
      break;
 
233
    }
 
234
}
 
235
 
 
236
 
 
237
static void
 
238
photos_view_model_init (PhotosViewModel *self)
 
239
{
 
240
  PhotosViewModelPrivate *priv;
 
241
  GType columns[] = {G_TYPE_STRING,    /* URN */
 
242
                     G_TYPE_STRING,    /* URI */
 
243
                     G_TYPE_STRING,    /* NAME */
 
244
                     G_TYPE_STRING,    /* AUTHOR */
 
245
                     GDK_TYPE_PIXBUF,  /* ICON */
 
246
                     G_TYPE_INT64,     /* MTIME */
 
247
                     G_TYPE_BOOLEAN};  /* STATE */
 
248
 
 
249
  self->priv = photos_view_model_get_instance_private (self);
 
250
  priv = self->priv;
 
251
 
 
252
  gtk_list_store_set_column_types (GTK_LIST_STORE (self), sizeof (columns) / sizeof (columns[0]), columns);
 
253
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self), PHOTOS_VIEW_MODEL_MTIME, GTK_SORT_DESCENDING);
 
254
 
 
255
  priv->col_mngr = photos_collection_manager_dup_singleton ();
 
256
 
 
257
  priv->item_mngr = photos_item_manager_dup_singleton ();
 
258
  g_signal_connect_swapped (priv->item_mngr, "object-added", G_CALLBACK (photos_view_model_object_added), self);
 
259
  g_signal_connect_swapped (priv->item_mngr, "object-removed", G_CALLBACK (photos_view_model_object_removed), self);
 
260
  g_signal_connect_swapped (priv->item_mngr, "clear", G_CALLBACK (gtk_list_store_clear), self);
 
261
}
 
262
 
 
263
 
 
264
static void
 
265
photos_view_model_class_init (PhotosViewModelClass *class)
 
266
{
 
267
  GObjectClass *object_class = G_OBJECT_CLASS (class);
 
268
 
 
269
  object_class->dispose = photos_view_model_dispose;
 
270
  object_class->finalize = photos_view_model_finalize;
 
271
  object_class->set_property = photos_view_model_set_property;
 
272
 
 
273
  g_object_class_install_property (object_class,
 
274
                                   PROP_MODE,
 
275
                                   g_param_spec_enum ("mode",
 
276
                                                      "PhotosWindowMode enum",
 
277
                                                      "The mode for which the model holds the data",
 
278
                                                      PHOTOS_TYPE_WINDOW_MODE,
 
279
                                                      PHOTOS_WINDOW_MODE_NONE,
 
280
                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE));
 
281
}
 
282
 
 
283
 
 
284
GtkListStore *
 
285
photos_view_model_new (PhotosWindowMode mode)
 
286
{
 
287
  return g_object_new (PHOTOS_TYPE_VIEW_MODEL, "mode", mode, NULL);
 
288
}