~ubuntu-branches/ubuntu/utopic/rhythmbox/utopic-proposed

« back to all changes in this revision

Viewing changes to podcast/rb-podcast-properties-dialog.c

Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 
2
 * 
 
3
 *  arch-tag: Headfile of podcast properties dialog
 
4
 *
 
5
 *  Copyright (C) 2005 Renato Araujo Oliveira Filho <renato.filho@indt.org>
 
6
 *
 
7
 *  This program is free software; you can redistribute it and/or modify
 
8
 *  it under the terms of the GNU General Public License as published by
 
9
 *  the Free Software Foundation; either version 2 of the License, or
 
10
 *  (at your option) any later version.
 
11
 *
 
12
 *  This program is distributed in the hope that it will be useful,
 
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 *  GNU General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU General Public License
 
18
 *  along with this program; if not, write to the Free Software
 
19
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 *
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
 
 
25
#include <string.h>
 
26
#include <time.h>
 
27
 
 
28
#include <glib/gi18n.h>
 
29
#include <gtk/gtk.h>
 
30
#include <glade/glade.h>
 
31
#include <libgnomevfs/gnome-vfs.h>
 
32
 
 
33
#include "rb-podcast-properties-dialog.h"
 
34
#include "rb-file-helpers.h"
 
35
#include "rb-glade-helpers.h"
 
36
#include "rb-dialog.h"
 
37
#include "rb-rating.h"
 
38
#include "rb-cut-and-paste-code.h"
 
39
 
 
40
static void rb_podcast_properties_dialog_class_init (RBPodcastPropertiesDialogClass *klass);
 
41
static void rb_podcast_properties_dialog_init (RBPodcastPropertiesDialog *dialog);
 
42
static void rb_podcast_properties_dialog_finalize (GObject *object);
 
43
static void rb_podcast_properties_dialog_set_property (GObject *object, 
 
44
                                                       guint prop_id,
 
45
                                                       const GValue *value, 
 
46
                                                       GParamSpec *pspec);
 
47
static void rb_podcast_properties_dialog_get_property (GObject *object, 
 
48
                                                       guint prop_id,
 
49
                                                       GValue *value, 
 
50
                                                       GParamSpec *pspec);
 
51
static gboolean rb_podcast_properties_dialog_get_current_entry (RBPodcastPropertiesDialog *dialog);
 
52
static void rb_podcast_properties_dialog_update_title (RBPodcastPropertiesDialog *dialog);
 
53
static void rb_podcast_properties_dialog_update_location (RBPodcastPropertiesDialog *dialog);
 
54
static void rb_podcast_properties_dialog_response_cb (GtkDialog *gtkdialog,
 
55
                                                      int response_id,
 
56
                                                      RBPodcastPropertiesDialog *dialog);
 
57
 
 
58
static void rb_podcast_properties_dialog_update (RBPodcastPropertiesDialog *dialog);
 
59
static void rb_podcast_properties_dialog_update_title_label (RBPodcastPropertiesDialog *dialog);
 
60
static void rb_podcast_properties_dialog_update_feed (RBPodcastPropertiesDialog *dialog);
 
61
static void rb_podcast_properties_dialog_update_play_count (RBPodcastPropertiesDialog *dialog);
 
62
static void rb_podcast_properties_dialog_update_bitrate (RBPodcastPropertiesDialog *dialog);
 
63
static void rb_podcast_properties_dialog_update_last_played (RBPodcastPropertiesDialog *dialog);
 
64
static void rb_podcast_properties_dialog_update_rating (RBPodcastPropertiesDialog *dialog);
 
65
static void rb_podcast_properties_dialog_update_date (RBPodcastPropertiesDialog *dialog);
 
66
static void rb_podcast_properties_dialog_update_description (RBPodcastPropertiesDialog *dialog);
 
67
static gchar* rb_podcast_properties_dialog_parse_time (gulong time);
 
68
static void rb_podcast_properties_dialog_rated_cb (RBRating *rating,
 
69
                                                   double score,
 
70
                                                   RBPodcastPropertiesDialog *dialog);
 
71
static GtkWidget * boldify_label (GtkWidget *label);
 
72
 
 
73
struct RBPodcastPropertiesDialogPrivate
 
74
{
 
75
        RBEntryView *entry_view;
 
76
        RhythmDB *db;
 
77
        RhythmDBEntry *current_entry;
 
78
 
 
79
        GtkWidget   *title;
 
80
        GtkWidget   *feed;
 
81
        GtkWidget   *location;
 
82
        GtkWidget   *lastplayed;
 
83
        GtkWidget   *playcount;
 
84
        GtkWidget   *bitrate;
 
85
        GtkWidget   *rating;
 
86
        GtkWidget   *date;
 
87
        GtkWidget   *description;
 
88
        
 
89
        GtkWidget   *okbutton;
 
90
};
 
91
 
 
92
enum 
 
93
{
 
94
        PROP_0,
 
95
        PROP_ENTRY_VIEW,
 
96
        PROP_BACKEND
 
97
};
 
98
 
 
99
G_DEFINE_TYPE (RBPodcastPropertiesDialog, rb_podcast_properties_dialog, GTK_TYPE_DIALOG)
 
100
 
 
101
 
 
102
static void
 
103
rb_podcast_properties_dialog_class_init (RBPodcastPropertiesDialogClass *klass)
 
104
{
 
105
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
106
 
 
107
        object_class->set_property = rb_podcast_properties_dialog_set_property;
 
108
        object_class->get_property = rb_podcast_properties_dialog_get_property;
 
109
 
 
110
        g_object_class_install_property (object_class,
 
111
                                         PROP_ENTRY_VIEW,
 
112
                                         g_param_spec_object ("entry-view",
 
113
                                                              "RBEntryView",
 
114
                                                              "RBEntryView object",
 
115
                                                              RB_TYPE_ENTRY_VIEW,
 
116
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
117
 
 
118
        object_class->finalize = rb_podcast_properties_dialog_finalize;
 
119
}
 
120
 
 
121
static void
 
122
rb_podcast_properties_dialog_init (RBPodcastPropertiesDialog *dialog)
 
123
{
 
124
        GladeXML *xml;
 
125
        
 
126
        dialog->priv = g_new0 (RBPodcastPropertiesDialogPrivate, 1);
 
127
        
 
128
        g_signal_connect_object (G_OBJECT (dialog),
 
129
                                 "response",
 
130
                                 G_CALLBACK (rb_podcast_properties_dialog_response_cb),
 
131
                                 dialog, 0);
 
132
 
 
133
        gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
 
134
        gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
 
135
        gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
 
136
 
 
137
        gtk_dialog_set_default_response (GTK_DIALOG (dialog),
 
138
                                         GTK_RESPONSE_OK);
 
139
 
 
140
        xml = rb_glade_xml_new ("podcast-properties.glade",
 
141
                                "podcastproperties",
 
142
                                dialog);
 
143
        glade_xml_signal_autoconnect (xml);
 
144
 
 
145
        gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
 
146
                           glade_xml_get_widget (xml, "podcastproperties"));
 
147
        dialog->priv->okbutton = gtk_dialog_add_button (GTK_DIALOG (dialog),
 
148
                                                        GTK_STOCK_OK,
 
149
                                                        GTK_RESPONSE_OK);
 
150
        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
 
151
 
 
152
        /* get the widgets from the XML */
 
153
        dialog->priv->title = glade_xml_get_widget (xml, "titleLabel");
 
154
        dialog->priv->feed = glade_xml_get_widget (xml, "feedLabel");
 
155
        dialog->priv->location = glade_xml_get_widget (xml, "locationLabel");
 
156
        dialog->priv->lastplayed = glade_xml_get_widget (xml, "lastplayedLabel");
 
157
        dialog->priv->playcount = glade_xml_get_widget (xml, "playcountLabel");
 
158
        dialog->priv->bitrate = glade_xml_get_widget (xml, "bitrateLabel");
 
159
        dialog->priv->date = glade_xml_get_widget (xml, "dateLabel");
 
160
        dialog->priv->description = glade_xml_get_widget (xml, "descriptionLabel");
 
161
 
 
162
        boldify_label (glade_xml_get_widget (xml, "titleDescLabel"));
 
163
        boldify_label (glade_xml_get_widget (xml, "feedDescLabel"));
 
164
        boldify_label (glade_xml_get_widget (xml, "locationDescLabel"));
 
165
        boldify_label (glade_xml_get_widget (xml, "ratingDescLabel"));
 
166
        boldify_label (glade_xml_get_widget (xml, "lastplayedDescLabel"));
 
167
        boldify_label (glade_xml_get_widget (xml, "playcountDescLabel"));
 
168
        boldify_label (glade_xml_get_widget (xml, "bitrateDescLabel"));
 
169
        boldify_label (glade_xml_get_widget (xml, "dateDescLabel"));
 
170
        boldify_label (glade_xml_get_widget (xml, "descriptionDescLabel"));
 
171
 
 
172
        dialog->priv->rating = GTK_WIDGET (rb_rating_new ());
 
173
        g_signal_connect_object (dialog->priv->rating, 
 
174
                                 "rated",
 
175
                                 G_CALLBACK (rb_podcast_properties_dialog_rated_cb),
 
176
                                 G_OBJECT (dialog), 0);
 
177
        gtk_container_add (GTK_CONTAINER (glade_xml_get_widget (xml, "ratingVBox")),
 
178
                           dialog->priv->rating);
 
179
        g_object_unref (G_OBJECT (xml));
 
180
}
 
181
 
 
182
static void
 
183
rb_podcast_properties_dialog_finalize (GObject *object)
 
184
{
 
185
        RBPodcastPropertiesDialog *dialog;
 
186
 
 
187
        g_return_if_fail (object != NULL);
 
188
        g_return_if_fail (RB_IS_PODCAST_PROPERTIES_DIALOG (object));
 
189
 
 
190
        dialog = RB_PODCAST_PROPERTIES_DIALOG (object);
 
191
 
 
192
        g_return_if_fail (dialog->priv != NULL);
 
193
 
 
194
        g_free (dialog->priv);
 
195
 
 
196
        G_OBJECT_CLASS (rb_podcast_properties_dialog_parent_class)->finalize (object);
 
197
}
 
198
 
 
199
static void
 
200
rb_podcast_properties_dialog_set_property (GObject *object,
 
201
                                           guint prop_id,
 
202
                                           const GValue *value,
 
203
                                           GParamSpec *pspec)
 
204
{
 
205
        RBPodcastPropertiesDialog *dialog = RB_PODCAST_PROPERTIES_DIALOG (object);
 
206
 
 
207
        switch (prop_id) {
 
208
        case PROP_ENTRY_VIEW:
 
209
                dialog->priv->entry_view = g_value_get_object (value);
 
210
                g_object_get (G_OBJECT (dialog->priv->entry_view), "db",
 
211
                              &dialog->priv->db, NULL);
 
212
                break;
 
213
        default:
 
214
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
215
                break;
 
216
        }
 
217
}
 
218
 
 
219
static void
 
220
rb_podcast_properties_dialog_get_property (GObject *object,
 
221
                                           guint prop_id,
 
222
                                           GValue *value,
 
223
                                           GParamSpec *pspec)
 
224
{
 
225
        RBPodcastPropertiesDialog *dialog = RB_PODCAST_PROPERTIES_DIALOG (object);
 
226
 
 
227
        switch (prop_id) {
 
228
        case PROP_ENTRY_VIEW:
 
229
                g_value_set_object (value, dialog->priv->entry_view);
 
230
                break;
 
231
        default:
 
232
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
233
                break;
 
234
        }
 
235
}
 
236
 
 
237
GtkWidget *
 
238
rb_podcast_properties_dialog_new (RBEntryView *entry_view)
 
239
{
 
240
        RBPodcastPropertiesDialog *dialog;
 
241
 
 
242
        g_return_val_if_fail (RB_IS_ENTRY_VIEW (entry_view), NULL);
 
243
 
 
244
        dialog = g_object_new (RB_TYPE_PODCAST_PROPERTIES_DIALOG,
 
245
                               "entry-view", entry_view, NULL);
 
246
 
 
247
        if (!rb_podcast_properties_dialog_get_current_entry (dialog)) {
 
248
                g_object_unref (G_OBJECT (dialog));
 
249
                return NULL;
 
250
        }
 
251
        rb_podcast_properties_dialog_update (dialog);
 
252
 
 
253
        return GTK_WIDGET (dialog);
 
254
}
 
255
 
 
256
static GtkWidget *
 
257
boldify_label (GtkWidget *label)
 
258
{
 
259
        gchar *str_final;
 
260
        str_final = g_strdup_printf ("<b>%s</b>",
 
261
                                     gtk_label_get_label (GTK_LABEL (label)));
 
262
        gtk_label_set_markup_with_mnemonic (GTK_LABEL (label), str_final);
 
263
        g_free (str_final);
 
264
        return label;
 
265
}
 
266
 
 
267
static void
 
268
rb_podcast_properties_dialog_response_cb (GtkDialog *gtkdialog,
 
269
                                          int response_id,
 
270
                                          RBPodcastPropertiesDialog *dialog)
 
271
{
 
272
        if (response_id != GTK_RESPONSE_OK)
 
273
                goto cleanup;
 
274
cleanup:
 
275
        gtk_widget_destroy (GTK_WIDGET (dialog));
 
276
}
 
277
 
 
278
static gboolean
 
279
rb_podcast_properties_dialog_get_current_entry (RBPodcastPropertiesDialog *dialog)
 
280
{
 
281
        GList *selected_entries;
 
282
        
 
283
        /* get the entry */
 
284
        selected_entries = rb_entry_view_get_selected_entries (dialog->priv->entry_view);
 
285
 
 
286
        if ((selected_entries == NULL) ||
 
287
            (selected_entries->data == NULL)) {
 
288
                dialog->priv->current_entry = NULL;
 
289
                return FALSE;
 
290
        }
 
291
 
 
292
        dialog->priv->current_entry = selected_entries->data;
 
293
        return TRUE;
 
294
}
 
295
 
 
296
static void
 
297
rb_podcast_properties_dialog_update (RBPodcastPropertiesDialog *dialog)
 
298
{
 
299
        g_return_if_fail (dialog->priv->current_entry != NULL);
 
300
        rb_podcast_properties_dialog_update_location (dialog);
 
301
        rb_podcast_properties_dialog_update_title (dialog);
 
302
        rb_podcast_properties_dialog_update_title_label (dialog);
 
303
        rb_podcast_properties_dialog_update_feed (dialog);
 
304
        rb_podcast_properties_dialog_update_play_count (dialog);
 
305
        rb_podcast_properties_dialog_update_bitrate (dialog);
 
306
        rb_podcast_properties_dialog_update_last_played (dialog);
 
307
        rb_podcast_properties_dialog_update_rating (dialog);
 
308
        rb_podcast_properties_dialog_update_date (dialog);
 
309
        rb_podcast_properties_dialog_update_description (dialog);
 
310
}
 
311
 
 
312
static void
 
313
rb_podcast_properties_dialog_update_title (RBPodcastPropertiesDialog *dialog)
 
314
{
 
315
        const char *name;
 
316
        char *tmp;      
 
317
        name = rb_refstring_get (dialog->priv->current_entry->title);
 
318
        tmp = g_strdup_printf (_("Properties for %s"), name);
 
319
        gtk_window_set_title (GTK_WINDOW (dialog), tmp);
 
320
        g_free (tmp);
 
321
}
 
322
 
 
323
static void
 
324
rb_podcast_properties_dialog_update_title_label (RBPodcastPropertiesDialog *dialog)
 
325
{
 
326
        gtk_label_set_text (GTK_LABEL (dialog->priv->title),
 
327
                            rb_refstring_get (dialog->priv->current_entry->title));
 
328
}
 
329
 
 
330
static void
 
331
rb_podcast_properties_dialog_update_feed (RBPodcastPropertiesDialog *dialog)
 
332
{
 
333
        gtk_label_set_text (GTK_LABEL (dialog->priv->feed),
 
334
                            rb_refstring_get (dialog->priv->current_entry->podcast->subtitle));
 
335
}
 
336
 
 
337
static void
 
338
rb_podcast_properties_dialog_update_location (RBPodcastPropertiesDialog *dialog)
 
339
{
 
340
        char *unescaped;
 
341
        unescaped = gnome_vfs_unescape_string_for_display (dialog->priv->current_entry->location);
 
342
        gtk_label_set_text (GTK_LABEL (dialog->priv->location), unescaped);
 
343
        g_free (unescaped);
 
344
}
 
345
 
 
346
static void
 
347
rb_podcast_properties_dialog_rated_cb (RBRating *rating,
 
348
                                       double score,
 
349
                                       RBPodcastPropertiesDialog *dialog)
 
350
{
 
351
        GValue value = { 0, };
 
352
 
 
353
        g_return_if_fail (RB_IS_RATING (rating));
 
354
        g_return_if_fail (RB_IS_PODCAST_PROPERTIES_DIALOG (dialog));
 
355
        g_return_if_fail (score >= 0 && score <= 5 );
 
356
 
 
357
        /* set the new value for the song */
 
358
        g_value_init (&value, G_TYPE_DOUBLE);
 
359
        g_value_set_double (&value, score);
 
360
        rhythmdb_entry_set (dialog->priv->db,
 
361
                            dialog->priv->current_entry,
 
362
                            RHYTHMDB_PROP_RATING,
 
363
                            &value);
 
364
        rhythmdb_commit (dialog->priv->db);
 
365
        g_value_unset (&value);
 
366
 
 
367
        g_object_set (G_OBJECT (dialog->priv->rating),
 
368
                      "rating", score,
 
369
                      NULL);
 
370
}
 
371
 
 
372
static void
 
373
rb_podcast_properties_dialog_update_play_count (RBPodcastPropertiesDialog *dialog)
 
374
{
 
375
        char *text;
 
376
        text = g_strdup_printf ("%ld", dialog->priv->current_entry->play_count);
 
377
        gtk_label_set_text (GTK_LABEL (dialog->priv->playcount), text);
 
378
        g_free (text);
 
379
}
 
380
 
 
381
static void
 
382
rb_podcast_properties_dialog_update_bitrate (RBPodcastPropertiesDialog *dialog)
 
383
{
 
384
        guint val;
 
385
        char *text;
 
386
 
 
387
        val = dialog->priv->current_entry->bitrate;
 
388
        if (val == 0)
 
389
                text = g_strdup (_("Unknown"));
 
390
        else
 
391
                text = g_strdup_printf ("%d", val);
 
392
 
 
393
        gtk_label_set_text (GTK_LABEL (dialog->priv->bitrate), text);
 
394
        g_free (text);
 
395
}
 
396
 
 
397
static void
 
398
rb_podcast_properties_dialog_update_last_played (RBPodcastPropertiesDialog *dialog)
 
399
{
 
400
        gtk_label_set (GTK_LABEL (dialog->priv->lastplayed),
 
401
                       rb_refstring_get (dialog->priv->current_entry->last_played_str));
 
402
}
 
403
 
 
404
static void
 
405
rb_podcast_properties_dialog_update_rating (RBPodcastPropertiesDialog *dialog)
 
406
{
 
407
        g_return_if_fail (RB_IS_PODCAST_PROPERTIES_DIALOG (dialog));
 
408
 
 
409
        g_object_set (G_OBJECT (dialog->priv->rating),
 
410
                      "rating", dialog->priv->current_entry->rating, NULL);
 
411
}
 
412
 
 
413
static void
 
414
rb_podcast_properties_dialog_update_date (RBPodcastPropertiesDialog *dialog)
 
415
{
 
416
        char *time;
 
417
        
 
418
        time = rb_podcast_properties_dialog_parse_time (dialog->priv->current_entry->podcast->post_time);
 
419
 
 
420
        gtk_label_set (GTK_LABEL (dialog->priv->date),
 
421
                       time);
 
422
        g_free (time);
 
423
}
 
424
 
 
425
static void
 
426
rb_podcast_properties_dialog_update_description (RBPodcastPropertiesDialog *dialog)
 
427
{
 
428
        gtk_label_set (GTK_LABEL (dialog->priv->description),
 
429
                       rb_refstring_get (dialog->priv->current_entry->podcast->description));
 
430
}
 
431
 
 
432
static char *
 
433
rb_podcast_properties_dialog_parse_time (gulong value)
 
434
{
 
435
        struct tm then;
 
436
        char *str;
 
437
 
 
438
        if (0 == value) {
 
439
                return NULL;
 
440
        }
 
441
 
 
442
        localtime_r ((time_t*)&value, &then);
 
443
        str = eel_strdup_strftime (_("%Y-%m-%d %H:%M"), &then);
 
444
 
 
445
        return str;
 
446
}