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

« back to all changes in this revision

Viewing changes to iradio/rb-new-station-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
 
/*
2
 
 *  arch-tag: Implementation of new internet radio station dialog
3
 
 *
4
 
 *  Copyright (C) 2002,2003 Colin Walters <walters@gnu.org>
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; either version 2 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  This program is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with this program; if not, write to the Free Software
18
 
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 
 *
20
 
 */
21
 
 
22
 
#include <config.h>
23
 
#include <libgnomevfs/gnome-vfs.h>
24
 
#include <libgnome/gnome-i18n.h>
25
 
#include <gtk/gtkentry.h>
26
 
#include <gtk/gtklabel.h>
27
 
#include <gtk/gtkcombo.h>
28
 
#include <gtk/gtkbox.h>
29
 
#include <gtk/gtktable.h>
30
 
#include <gtk/gtkdialog.h>
31
 
#include <gtk/gtkstock.h>
32
 
#include <glade/glade.h>
33
 
#include <string.h>
34
 
#include <time.h>
35
 
 
36
 
#include "rb-new-station-dialog.h"
37
 
#include "rb-glade-helpers.h"
38
 
#include "rb-dialog.h"
39
 
 
40
 
static void rb_new_station_dialog_class_init (RBNewStationDialogClass *klass);
41
 
static void rb_new_station_dialog_init (RBNewStationDialog *dialog);
42
 
static void rb_new_station_dialog_finalize (GObject *object);
43
 
static void rb_new_station_dialog_set_property (GObject *object,
44
 
                                                guint prop_id,
45
 
                                                const GValue *value,
46
 
                                                GParamSpec *pspec);
47
 
static void rb_new_station_dialog_get_property (GObject *object,
48
 
                                                guint prop_id,
49
 
                                                GValue *value,
50
 
                                                GParamSpec *pspec);
51
 
static void rb_new_station_dialog_response_cb (GtkDialog *gtkdialog,
52
 
                                               int response_id,
53
 
                                               RBNewStationDialog *dialog);
54
 
static void rb_new_station_dialog_entry_changed_cb (GtkEntry *entry,
55
 
                                                    RBNewStationDialog *dialog);
56
 
 
57
 
struct RBNewStationDialogPrivate
58
 
{
59
 
        RhythmDB *db;
60
 
 
61
 
        GtkWidget   *title;
62
 
        GtkWidget   *genre;
63
 
        GtkWidget   *location;
64
 
        GtkWidget   *okbutton;
65
 
        GtkWidget   *cancelbutton;
66
 
};
67
 
 
68
 
enum 
69
 
{
70
 
        PROP_0,
71
 
        PROP_DB
72
 
};
73
 
 
74
 
static GObjectClass *parent_class = NULL;
75
 
 
76
 
GType
77
 
rb_new_station_dialog_get_type (void)
78
 
{
79
 
        static GType rb_new_station_dialog_type = 0;
80
 
 
81
 
        if (rb_new_station_dialog_type == 0)
82
 
        {
83
 
                static const GTypeInfo our_info =
84
 
                {
85
 
                        sizeof (RBNewStationDialogClass),
86
 
                        NULL,
87
 
                        NULL,
88
 
                        (GClassInitFunc) rb_new_station_dialog_class_init,
89
 
                        NULL,
90
 
                        NULL,
91
 
                        sizeof (RBNewStationDialog),
92
 
                        0,
93
 
                        (GInstanceInitFunc) rb_new_station_dialog_init
94
 
                };
95
 
 
96
 
                rb_new_station_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
97
 
                                                                     "RBNewStationDialog",
98
 
                                                                     &our_info, 0);
99
 
        }
100
 
 
101
 
        return rb_new_station_dialog_type;
102
 
}
103
 
 
104
 
static void
105
 
rb_new_station_dialog_class_init (RBNewStationDialogClass *klass)
106
 
{
107
 
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
108
 
 
109
 
        parent_class = g_type_class_peek_parent (klass);
110
 
 
111
 
        object_class->set_property = rb_new_station_dialog_set_property;
112
 
        object_class->get_property = rb_new_station_dialog_get_property;
113
 
 
114
 
        g_object_class_install_property (object_class,
115
 
                                         PROP_DB,
116
 
                                         g_param_spec_object ("db",
117
 
                                                              "RhythmDB",
118
 
                                                              "RhythmDB object",
119
 
                                                              RHYTHMDB_TYPE,
120
 
                                                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
121
 
 
122
 
        object_class->finalize = rb_new_station_dialog_finalize;
123
 
}
124
 
 
125
 
static void
126
 
rb_new_station_dialog_init (RBNewStationDialog *dialog)
127
 
{
128
 
        GladeXML *xml;
129
 
 
130
 
        /* create the dialog and some buttons forward - close */
131
 
        dialog->priv = g_new0 (RBNewStationDialogPrivate, 1);
132
 
 
133
 
        g_signal_connect_object (G_OBJECT (dialog),
134
 
                                 "response",
135
 
                                 G_CALLBACK (rb_new_station_dialog_response_cb),
136
 
                                 dialog, 0);
137
 
 
138
 
        gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
139
 
        gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
140
 
        gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 2);
141
 
 
142
 
        gtk_dialog_set_default_response (GTK_DIALOG (dialog),
143
 
                                         GTK_RESPONSE_OK);
144
 
        gtk_window_set_title (GTK_WINDOW (dialog), _("New Internet Radio Station"));
145
 
 
146
 
        dialog->priv->cancelbutton = gtk_dialog_add_button (GTK_DIALOG (dialog),
147
 
                                                            GTK_STOCK_CANCEL,
148
 
                                                            GTK_RESPONSE_CANCEL);
149
 
        dialog->priv->okbutton = gtk_dialog_add_button (GTK_DIALOG (dialog),
150
 
                                                        GTK_STOCK_ADD,
151
 
                                                        GTK_RESPONSE_OK);
152
 
        gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
153
 
 
154
 
        xml = rb_glade_xml_new ("station-new.glade",
155
 
                                "newstation",
156
 
                                dialog);
157
 
        glade_xml_signal_autoconnect (xml);
158
 
 
159
 
        gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
160
 
                           glade_xml_get_widget (xml, "newstation"));
161
 
 
162
 
        /* get the widgets from the XML */
163
 
        dialog->priv->title = glade_xml_get_widget (xml, "titleEntry");
164
 
        dialog->priv->genre = glade_xml_get_widget (xml, "genreCombo");
165
 
        dialog->priv->location = glade_xml_get_widget (xml, "locationEntry");
166
 
        g_signal_connect_object (G_OBJECT (dialog->priv->title),
167
 
                                 "changed",
168
 
                                 G_CALLBACK (rb_new_station_dialog_entry_changed_cb),
169
 
                                 dialog, 0);
170
 
 
171
 
        g_signal_connect_object (G_OBJECT (GTK_COMBO (dialog->priv->genre)->entry),
172
 
                                 "changed",
173
 
                                 G_CALLBACK (rb_new_station_dialog_entry_changed_cb),
174
 
                                 dialog, 0);
175
 
 
176
 
        g_signal_connect_object (G_OBJECT (dialog->priv->location),
177
 
                                 "changed",
178
 
                                 G_CALLBACK (rb_new_station_dialog_entry_changed_cb),
179
 
                                 dialog, 0);
180
 
 
181
 
        gtk_combo_set_popdown_strings (GTK_COMBO (dialog->priv->genre),
182
 
                                       g_list_append (NULL, _("Unknown")));
183
 
 
184
 
        /* default focus */
185
 
        gtk_widget_grab_focus (dialog->priv->title);
186
 
        /* FIXME */
187
 
        gtk_widget_set_sensitive (dialog->priv->okbutton, FALSE);
188
 
 
189
 
        g_object_unref (G_OBJECT (xml));
190
 
}
191
 
 
192
 
static void
193
 
rb_new_station_dialog_finalize (GObject *object)
194
 
{
195
 
        RBNewStationDialog *dialog;
196
 
 
197
 
        g_return_if_fail (object != NULL);
198
 
        g_return_if_fail (RB_IS_NEW_STATION_DIALOG (object));
199
 
 
200
 
        dialog = RB_NEW_STATION_DIALOG (object);
201
 
 
202
 
        g_return_if_fail (dialog->priv != NULL);
203
 
 
204
 
        g_free (dialog->priv);
205
 
 
206
 
        G_OBJECT_CLASS (parent_class)->finalize (object);
207
 
}
208
 
 
209
 
static void
210
 
rb_new_station_dialog_set_property (GObject *object,
211
 
                           guint prop_id,
212
 
                           const GValue *value,
213
 
                           GParamSpec *pspec)
214
 
{
215
 
        RBNewStationDialog *dialog = RB_NEW_STATION_DIALOG (object);
216
 
 
217
 
        switch (prop_id)
218
 
        {
219
 
        case PROP_DB:
220
 
                dialog->priv->db = g_value_get_object (value);
221
 
                break;
222
 
        default:
223
 
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224
 
                break;
225
 
        }
226
 
}
227
 
 
228
 
static void
229
 
rb_new_station_dialog_get_property (GObject *object,
230
 
                              guint prop_id,
231
 
                              GValue *value,
232
 
                              GParamSpec *pspec)
233
 
{
234
 
        RBNewStationDialog *dialog = RB_NEW_STATION_DIALOG (object);
235
 
 
236
 
        switch (prop_id)
237
 
        {
238
 
        case PROP_DB:
239
 
                g_value_set_object (value, dialog->priv->db);
240
 
                break;
241
 
        default:
242
 
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243
 
                break;
244
 
        }
245
 
}
246
 
 
247
 
GtkWidget *
248
 
rb_new_station_dialog_new (RhythmDB *db)
249
 
{
250
 
        RBNewStationDialog *dialog;
251
 
        GList *genrenames;
252
 
 
253
 
        g_return_val_if_fail (RHYTHMDB_IS (db), NULL);
254
 
 
255
 
        dialog = g_object_new (RB_TYPE_NEW_STATION_DIALOG, "db", db, NULL);
256
 
 
257
 
/*      genrenames = rb_iradio_backend_get_genre_names (backend); */
258
 
        genrenames = NULL;
259
 
        if (genrenames != NULL) {
260
 
                gtk_combo_set_popdown_strings (GTK_COMBO (dialog->priv->genre),
261
 
                                               genrenames);
262
 
                g_list_free (genrenames);
263
 
        }
264
 
 
265
 
        g_return_val_if_fail (dialog->priv != NULL, NULL);
266
 
 
267
 
        return GTK_WIDGET (dialog);
268
 
}
269
 
 
270
 
static void
271
 
rb_new_station_dialog_response_cb (GtkDialog *gtkdialog,
272
 
                                   int response_id,
273
 
                                   RBNewStationDialog *dialog)
274
 
{
275
 
        const char *location;
276
 
        GValue title_val = { 0, };
277
 
        GValue genre_val = { 0, };
278
 
        RhythmDBEntry *entry;
279
 
        if (response_id != GTK_RESPONSE_OK)
280
 
                return;
281
 
 
282
 
        location = gtk_entry_get_text (GTK_ENTRY (dialog->priv->location));
283
 
 
284
 
        g_value_init (&title_val, G_TYPE_STRING);
285
 
        g_value_set_string (&title_val, gtk_entry_get_text (GTK_ENTRY (dialog->priv->title)));
286
 
        g_value_init (&genre_val, G_TYPE_STRING);
287
 
        g_value_set_string (&genre_val,
288
 
                            gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (dialog->priv->genre)->entry)));
289
 
        rhythmdb_write_lock (dialog->priv->db);
290
 
        entry = rhythmdb_entry_lookup_by_location (dialog->priv->db, location);
291
 
        if (!entry)
292
 
                entry = rhythmdb_entry_new (dialog->priv->db,
293
 
                                            RHYTHMDB_ENTRY_TYPE_IRADIO_STATION,
294
 
                                            location);
295
 
        rhythmdb_entry_set (dialog->priv->db, entry, RHYTHMDB_PROP_TITLE, &title_val);
296
 
        rhythmdb_entry_set (dialog->priv->db, entry, RHYTHMDB_PROP_GENRE, &genre_val);
297
 
        rhythmdb_write_unlock (dialog->priv->db);
298
 
        g_value_unset (&title_val);
299
 
        g_value_unset (&genre_val);
300
 
}
301
 
 
302
 
static void
303
 
rb_new_station_dialog_entry_changed_cb (GtkEntry *entry,
304
 
                                        RBNewStationDialog *dialog)
305
 
{
306
 
        gtk_widget_set_sensitive (dialog->priv->okbutton,
307
 
                                  g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (dialog->priv->title)), -1) > 0
308
 
                                  && g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (dialog->priv->genre)->entry)), -1) > 0
309
 
                                  && g_utf8_collate (gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (dialog->priv->genre)->entry)), _("All"))
310
 
                                  && g_utf8_strlen (gtk_entry_get_text (GTK_ENTRY (dialog->priv->location)), -1) > 0);
311
 
}