~cordia-team/+junk/hildon-home

« back to all changes in this revision

Viewing changes to src/hd-change-background-dialog.c

  • Committer: Thomas-Karl Pietrowski
  • Date: 2011-10-09 16:56:50 UTC
  • Revision ID: thopiekar@googlemail.com-20111009165650-4c3oct3pk33c2fb3
first release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of hildon-home
 
3
 *
 
4
 * Copyright (C) 2008, 2009, 2010 Nokia Corporation.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public License
 
8
 * as published by the Free Software Foundation; either version 2.1 of
 
9
 * the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
19
 * 02110-1301 USA
 
20
 *
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include <glib/gi18n.h>
 
28
 
 
29
#ifdef HAVE_HILDON_FM
 
30
#include <hildon/hildon-file-chooser-dialog.h>
 
31
#include <hildon/hildon-file-selection.h>
 
32
#endif
 
33
 
 
34
#include "hd-available-backgrounds.h"
 
35
#include "hd-backgrounds.h"
 
36
 
 
37
#include "hd-change-background-dialog.h"
 
38
#include "hd-dbus-utils.h"
 
39
 
 
40
/* Add Image dialog */
 
41
#define RESPONSE_ADD 1
 
42
 
 
43
/* Images folder */
 
44
#define USER_IMAGES_FOLDER "MyDocs", ".images"
 
45
 
 
46
#define HD_CHANGE_BACKGROUND_DIALOG_GET_PRIVATE(object) \
 
47
  (G_TYPE_INSTANCE_GET_PRIVATE ((object), HD_TYPE_CHANGE_BACKGROUND_DIALOG, HDChangeBackgroundDialogPrivate))
 
48
 
 
49
enum
 
50
{
 
51
  PROP_0,
 
52
  PROP_CURRENT_VIEW
 
53
};
 
54
 
 
55
struct _HDChangeBackgroundDialogPrivate
 
56
{
 
57
  HDAvailableBackgrounds *backgrounds;
 
58
 
 
59
  GtkWidget    *selector;
 
60
 
 
61
  GtkTreePath  *custom_image;
 
62
 
 
63
  guint  current_view;
 
64
  GFile *current_background;
 
65
 
 
66
  guint scroll_to_selected_id;
 
67
 
 
68
  GCancellable *cancellable;
 
69
};
 
70
 
 
71
G_DEFINE_TYPE (HDChangeBackgroundDialog, hd_change_background_dialog, GTK_TYPE_DIALOG);
 
72
 
 
73
static gboolean
 
74
scroll_to_selected (gpointer data)
 
75
{
 
76
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG (data)->priv;
 
77
  GtkTreeIter iter;
 
78
 
 
79
  priv->scroll_to_selected_id = 0;
 
80
 
 
81
  if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (priv->selector),
 
82
                                          0,
 
83
                                          &iter))
 
84
    {
 
85
      hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (priv->selector),
 
86
                                         0,
 
87
                                         &iter,
 
88
                                         TRUE);
 
89
    }
 
90
 
 
91
  return FALSE;
 
92
}
 
93
 
 
94
static void
 
95
backgrounds_selection_changed (HDAvailableBackgrounds   *backgrounds,
 
96
                               GtkTreeIter              *iter,
 
97
                               HDChangeBackgroundDialog *dialog)
 
98
{
 
99
  HDChangeBackgroundDialogPrivate *priv = dialog->priv;
 
100
 
 
101
  hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (priv->selector),
 
102
                                     0,
 
103
                                     iter,
 
104
                                     FALSE);
 
105
 
 
106
  priv->scroll_to_selected_id = gdk_threads_add_idle (scroll_to_selected,
 
107
                                                      dialog);
 
108
}
 
109
 
 
110
static void
 
111
_hd_background_selector_changed (HildonTouchSelector* widget,
 
112
                                 gint column,
 
113
                                 HDChangeBackgroundDialog *dialog)
 
114
{
 
115
  GtkTreeModel *model = NULL;
 
116
  GtkTreeIter iter;
 
117
  gboolean is_ovi = FALSE;
 
118
 
 
119
  model = hildon_touch_selector_get_model (widget, column);
 
120
  if (!model ||
 
121
      !hildon_touch_selector_get_selected (widget, column, &iter))
 
122
      {
 
123
        g_debug ("%s: Couldn't get selected item", __FUNCTION__);
 
124
        return;
 
125
      }
 
126
  gtk_tree_model_get (model,
 
127
                      &iter,
 
128
                      HD_BACKGROUND_COL_OVI, &is_ovi,
 
129
                      -1);
 
130
  if (is_ovi)
 
131
    {
 
132
      hd_utils_open_link (HD_OVI_LINK_BACKGROUNDS);
 
133
    }
 
134
}
 
135
 
 
136
static void
 
137
hd_change_background_dialog_constructed (GObject *object)
 
138
{
 
139
  HDChangeBackgroundDialog *dialog = HD_CHANGE_BACKGROUND_DIALOG (object);
 
140
  HDChangeBackgroundDialogPrivate *priv = dialog->priv;
 
141
 
 
142
  if (G_OBJECT_CLASS (hd_change_background_dialog_parent_class)->constructed)
 
143
    G_OBJECT_CLASS (hd_change_background_dialog_parent_class)->constructed (object);
 
144
 
 
145
  hd_available_backgrounds_run (priv->backgrounds,
 
146
                                priv->current_view);
 
147
 
 
148
  g_signal_connect (priv->backgrounds, "selection-changed",
 
149
                    G_CALLBACK (backgrounds_selection_changed),
 
150
                    object);
 
151
 
 
152
  g_signal_connect (priv->selector, "changed",
 
153
                    G_CALLBACK (_hd_background_selector_changed),
 
154
                                object);
 
155
}
 
156
 
 
157
static void
 
158
hd_change_background_dialog_dispose (GObject *object)
 
159
{
 
160
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG (object)->priv;
 
161
 
 
162
  if (priv->backgrounds)
 
163
    priv->backgrounds = (g_object_unref (priv->backgrounds), NULL);
 
164
 
 
165
  if (priv->scroll_to_selected_id)
 
166
    priv->scroll_to_selected_id = (g_source_remove (priv->scroll_to_selected_id), 0);
 
167
 
 
168
  if (priv->cancellable)
 
169
    priv->cancellable = (g_object_unref (priv->cancellable), NULL);
 
170
 
 
171
  G_OBJECT_CLASS (hd_change_background_dialog_parent_class)->dispose (object);
 
172
}
 
173
 
 
174
static void
 
175
hd_change_background_dialog_finalize (GObject *object)
 
176
{
 
177
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG (object)->priv;
 
178
 
 
179
  if (priv->custom_image)
 
180
    priv->custom_image = (gtk_tree_path_free (priv->custom_image), NULL);
 
181
 
 
182
  G_OBJECT_CLASS (hd_change_background_dialog_parent_class)->finalize (object);
 
183
}
 
184
 
 
185
static void
 
186
hd_change_background_dialog_set_property (GObject      *object,
 
187
                                          guint         prop_id,
 
188
                                          const GValue *value,
 
189
                                          GParamSpec   *pspec)
 
190
{
 
191
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG (object)->priv;
 
192
 
 
193
  switch (prop_id)
 
194
    {
 
195
    case PROP_CURRENT_VIEW:
 
196
      priv->current_view = g_value_get_uint (value);
 
197
      break;
 
198
 
 
199
    default:
 
200
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
201
    }
 
202
}
 
203
 
 
204
#ifdef HAVE_HILDON_FM
 
205
static void
 
206
add_image_dialog_response (GtkDialog                *dialog,
 
207
                           gint                      response_id,
 
208
                           HDChangeBackgroundDialog *cb_dialog)
 
209
{
 
210
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG (cb_dialog)->priv;
 
211
 
 
212
  if (response_id == GTK_RESPONSE_OK)
 
213
    {
 
214
      /* An image was selected */
 
215
      gchar *uri;
 
216
 
 
217
      uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
 
218
 
 
219
      if (uri)
 
220
        {
 
221
          GFile *image_file;
 
222
 
 
223
          image_file = g_file_new_for_uri (uri);
 
224
 
 
225
          hd_available_backgrounds_set_user_selected (priv->backgrounds,
 
226
                                                      image_file);
 
227
 
 
228
          g_free (uri);
 
229
        }
 
230
    }
 
231
 
 
232
  gtk_widget_destroy (GTK_WIDGET (dialog));
 
233
}
 
234
#endif
 
235
 
 
236
static gboolean
 
237
background_set_cb (gpointer data)
 
238
{
 
239
  if (GTK_IS_WIDGET (data))
 
240
    gtk_widget_destroy (GTK_WIDGET (data));
 
241
 
 
242
  return FALSE;
 
243
}
 
244
 
 
245
static void
 
246
hd_change_background_dialog_response (GtkDialog *dialog,
 
247
                                      gint       response_id)
 
248
{
 
249
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG (dialog)->priv;
 
250
 
 
251
  if (response_id == RESPONSE_ADD)
 
252
    {
 
253
#ifdef HAVE_HILDON_FM
 
254
      GtkWidget *add_image;
 
255
      GtkFileFilter *filter;
 
256
      gchar *images_folder;
 
257
 
 
258
      /* Create the Add Image dialog */
 
259
      add_image = hildon_file_chooser_dialog_new_with_properties (GTK_WINDOW (dialog),
 
260
                                                                  "action", GTK_FILE_CHOOSER_ACTION_OPEN,
 
261
                                                                  "title", dgettext (GETTEXT_PACKAGE, "home_ti_add_image"),
 
262
                                                                  "empty-text", dgettext (GETTEXT_PACKAGE, "home_li_no_images"),
 
263
                                                                  "open-button-text", dgettext ("hildon-libs", "wdgt_bd_done"),
 
264
                                                                  "select-multiple", FALSE,
 
265
                                                                  "selection-mode", HILDON_FILE_SELECTION_MODE_THUMBNAILS,
 
266
                                                                  "modal", FALSE,
 
267
                                                                  NULL);
 
268
 
 
269
      /* Filter for shown mime-types: JPG, GIF, PNG, BMP, TIFF, sketch.png */
 
270
      filter = gtk_file_filter_new ();
 
271
      gtk_file_filter_add_mime_type (filter, "image/jpeg");
 
272
      gtk_file_filter_add_mime_type (filter, "image/gif");
 
273
      gtk_file_filter_add_mime_type (filter, "image/png");
 
274
      gtk_file_filter_add_mime_type (filter, "image/bmp");
 
275
      gtk_file_filter_add_mime_type (filter, "image/tiff");
 
276
      gtk_file_filter_add_mime_type (filter, "sketch/png");
 
277
      gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (add_image),
 
278
                                   filter);
 
279
 
 
280
      /* Current folder by default: Images folder */
 
281
      images_folder = g_build_filename (g_get_home_dir (),
 
282
                                        USER_IMAGES_FOLDER,
 
283
                                        NULL);
 
284
      gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (add_image),
 
285
                                           images_folder);
 
286
      g_free (images_folder);
 
287
 
 
288
      /* Connect signal handler */
 
289
      g_signal_connect (add_image, "response",
 
290
                        G_CALLBACK (add_image_dialog_response),
 
291
                        dialog);
 
292
 
 
293
      /* Show Add Image dialog */
 
294
      gtk_dialog_run (GTK_DIALOG (add_image));
 
295
#endif
 
296
        }
 
297
  else if (response_id == GTK_RESPONSE_ACCEPT)
 
298
    {
 
299
      GtkTreeIter iter;
 
300
 
 
301
      if (hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (priv->selector),
 
302
                                              0,
 
303
                                              &iter))
 
304
        {
 
305
          GtkTreeModel *model;
 
306
          HDBackground *background;
 
307
          gboolean is_ovi;
 
308
 
 
309
          model = hd_available_backgrounds_get_model (priv->backgrounds);
 
310
 
 
311
          gtk_tree_model_get (model, &iter,
 
312
                              HD_BACKGROUND_COL_OVI, &is_ovi,
 
313
                              -1);
 
314
          if (is_ovi)
 
315
            {
 
316
              /* We shouldn't really get here, but just in case. */
 
317
              gtk_widget_destroy (GTK_WIDGET (dialog));
 
318
              return;
 
319
            }
 
320
 
 
321
          /* Get selected background image */
 
322
          gtk_tree_model_get (model, &iter,
 
323
                              HD_BACKGROUND_COL_OBJECT, &background,
 
324
                              -1);
 
325
 
 
326
          hildon_gtk_window_set_progress_indicator (GTK_WINDOW (dialog),
 
327
                                                    TRUE);
 
328
          gtk_widget_set_sensitive (GTK_WIDGET (dialog), FALSE);
 
329
 
 
330
          hd_background_set_for_current_view (background,
 
331
                                              priv->current_view,
 
332
                                              priv->cancellable);
 
333
 
 
334
          hd_backgrounds_add_done_cb (hd_backgrounds_get (),
 
335
                                      background_set_cb,
 
336
                                      dialog,
 
337
                                      NULL);
 
338
        }
 
339
      else
 
340
        {
 
341
          gtk_widget_destroy (GTK_WIDGET (dialog));
 
342
        }
 
343
    }
 
344
  else
 
345
    {
 
346
      if (priv->cancellable)
 
347
        g_cancellable_cancel (priv->cancellable);
 
348
 
 
349
      gtk_widget_destroy (GTK_WIDGET (dialog));
 
350
    }
 
351
}
 
352
 
 
353
static void
 
354
hd_change_background_dialog_class_init (HDChangeBackgroundDialogClass *klass)
 
355
{
 
356
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
357
  GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
 
358
 
 
359
  object_class->constructed = hd_change_background_dialog_constructed;
 
360
  object_class->dispose = hd_change_background_dialog_dispose;
 
361
  object_class->finalize = hd_change_background_dialog_finalize;
 
362
 
 
363
  object_class->set_property = hd_change_background_dialog_set_property;
 
364
 
 
365
  dialog_class->response = hd_change_background_dialog_response;
 
366
 
 
367
  g_object_class_install_property (object_class,
 
368
                                   PROP_CURRENT_VIEW,
 
369
                                   g_param_spec_uint ("current-view",
 
370
                                                      "Current view",
 
371
                                                      "ID of the currently shown view",
 
372
                                                      0,
 
373
                                                      3,
 
374
                                                      0,
 
375
                                                      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
 
376
 
 
377
  g_type_class_add_private (klass, sizeof (HDChangeBackgroundDialogPrivate));
 
378
}
 
379
 
 
380
static void
 
381
hd_change_background_dialog_init (HDChangeBackgroundDialog *dialog)
 
382
{
 
383
  HDChangeBackgroundDialogPrivate *priv = HD_CHANGE_BACKGROUND_DIALOG_GET_PRIVATE (dialog);
 
384
  HildonTouchSelectorColumn *column;
 
385
  GtkCellRenderer *renderer;
 
386
 
 
387
  dialog->priv = priv;
 
388
 
 
389
  priv->backgrounds = hd_available_backgrounds_new ();
 
390
  priv->cancellable = g_cancellable_new ();
 
391
 
 
392
  /* Set dialog title */
 
393
  gtk_window_set_title (GTK_WINDOW (dialog), dgettext ("maemo-af-desktop", "home_ti_change_backgr"));
 
394
 
 
395
  /* Add buttons */
 
396
  gtk_dialog_add_button (GTK_DIALOG (dialog), dgettext ("hildon-libs", "wdgt_bd_add"), RESPONSE_ADD);
 
397
  gtk_dialog_add_button (GTK_DIALOG (dialog), dgettext ("hildon-libs", "wdgt_bd_done"), GTK_RESPONSE_ACCEPT);
 
398
 
 
399
  /* Create the touch selector */
 
400
  priv->selector = hildon_touch_selector_new ();
 
401
 
 
402
  /* Create empty column */
 
403
  column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (priv->selector),
 
404
                                                hd_available_backgrounds_get_model (priv->backgrounds),
 
405
                                                NULL);
 
406
 
 
407
  /* Add the thumbnail renderer */
 
408
  renderer = gtk_cell_renderer_pixbuf_new ();
 
409
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column),
 
410
                              renderer,
 
411
                              FALSE);
 
412
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column),
 
413
                                 renderer,
 
414
                                 "pixbuf", HD_BACKGROUND_COL_THUMBNAIL);
 
415
 
 
416
  /* Add the label renderer */
 
417
  renderer = gtk_cell_renderer_text_new ();
 
418
  g_object_set (renderer, "xpad", HILDON_MARGIN_DOUBLE, NULL);
 
419
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (column),
 
420
                              renderer,
 
421
                              FALSE);
 
422
  gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column),
 
423
                                 renderer,
 
424
                                 "text", HD_BACKGROUND_COL_LABEL);
 
425
 
 
426
  g_object_set (column,
 
427
                "text-column", HD_BACKGROUND_COL_LABEL,
 
428
                NULL);
 
429
 
 
430
  gtk_widget_show (priv->selector);
 
431
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), priv->selector);
 
432
 
 
433
  gtk_widget_set_size_request (GTK_WIDGET (dialog), -1, 358);
 
434
}
 
435
 
 
436
GtkWidget *
 
437
hd_change_background_dialog_new (guint current_view)
 
438
{
 
439
  GtkWidget *window;
 
440
 
 
441
  window = g_object_new (HD_TYPE_CHANGE_BACKGROUND_DIALOG,
 
442
                         "current-view", current_view,
 
443
                         NULL);
 
444
 
 
445
  return window;
 
446
}
 
447