~cjcurran/indicator-sound/graceful-crashing

« back to all changes in this revision

Viewing changes to src/indicator-sound.c

  • Committer: Conor Curran
  • Date: 2010-01-27 13:21:02 UTC
  • Revision ID: conor.curran@canonical.com-20100127132102-xc5w5ob5kei1iblb
copied contents from lp:~cjcurran/wasilla/soundmenu without the noise

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
A small wrapper utility to load indicators and put them as menu items
 
3
into the gnome-panel using it's applet interface.
 
4
 
 
5
Copyright 2010 Canonical Ltd.
 
6
 
 
7
Authors:
 
8
    Conor Curran <conor.curra@canonical.com>
 
9
    Ted Gould <ted@canonical.com>
 
10
 
 
11
This program is free software: you can redistribute it and/or modify it 
 
12
under the terms of the GNU General Public License version 3, as published 
 
13
by the Free Software Foundation.
 
14
 
 
15
This program is distributed in the hope that it will be useful, but 
 
16
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
17
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
18
PURPOSE.  See the GNU General Public License for more details.
 
19
 
 
20
You should have received a copy of the GNU General Public License along 
 
21
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
22
*/
 
23
 
 
24
#include <glib.h>
 
25
#include <glib-object.h>
 
26
#include <gtk/gtk.h>
 
27
#include <libdbusmenu-gtk/menu.h>
 
28
/*#include <idoscalemenuitem.h>*/
 
29
 
 
30
#include <dbus/dbus-glib.h>
 
31
#include <dbus/dbus-glib-bindings.h>
 
32
 
 
33
#include <libindicator/indicator.h>
 
34
#include <libindicator/indicator-object.h>
 
35
#include <libindicator/indicator-service-manager.h>
 
36
 
 
37
 
 
38
#include "dbus-shared-names.h"
 
39
#include "sound-service-client.h"
 
40
#include "common-defs.h"
 
41
#include "sound-service-marshal.h"
 
42
 
 
43
 
 
44
#define INDICATOR_SOUND_TYPE            (indicator_sound_get_type ())
 
45
#define INDICATOR_SOUND(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), INDICATOR_SOUND_TYPE, IndicatorSound))
 
46
#define INDICATOR_SOUND_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), INDICATOR_SOUND_TYPE, IndicatorSoundClass))
 
47
#define IS_INDICATOR_SOUND(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), INDICATOR_SOUND_TYPE))
 
48
#define IS_INDICATOR_SOUND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), INDICATOR_SOUND_TYPE))
 
49
#define INDICATOR_SOUND_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), INDICATOR_SOUND_TYPE, IndicatorSoundClass))
 
50
 
 
51
typedef struct _IndicatorSound      IndicatorSound;
 
52
typedef struct _IndicatorSoundClass IndicatorSoundClass;
 
53
 
 
54
struct _IndicatorSoundClass {
 
55
        IndicatorObjectClass parent_class;
 
56
};
 
57
 
 
58
struct _IndicatorSound {
 
59
        IndicatorObject parent;
 
60
        IndicatorServiceManager * service;
 
61
};
 
62
 
 
63
GType indicator_sound_get_type (void);
 
64
 
 
65
 
 
66
/* Indicator stuff */
 
67
INDICATOR_SET_VERSION
 
68
INDICATOR_SET_TYPE(INDICATOR_SOUND_TYPE)
 
69
 
 
70
/* Prototypes */
 
71
static GtkLabel * get_label (IndicatorObject * io);
 
72
static GtkImage * get_icon (IndicatorObject * io);
 
73
static GtkMenu * get_menu (IndicatorObject * io);
 
74
//static GtkWidget *volume_item;
 
75
static DBusGProxy * sound_dbus_proxy = NULL;
 
76
 
 
77
 
 
78
static void indicator_sound_class_init (IndicatorSoundClass *klass);
 
79
static void indicator_sound_init       (IndicatorSound *self);
 
80
static void indicator_sound_dispose    (GObject *object);
 
81
static void indicator_sound_finalize   (GObject *object);
 
82
 
 
83
G_DEFINE_TYPE (IndicatorSound, indicator_sound, INDICATOR_OBJECT_TYPE);
 
84
 
 
85
static void connection_changed (IndicatorServiceManager * sm, gboolean connected, gpointer userdata);
 
86
static void catch_signal(DBusGProxy * proxy, gint sink_index, gboolean value, gpointer userdata);
 
87
 
 
88
static void
 
89
indicator_sound_class_init (IndicatorSoundClass *klass)
 
90
{
 
91
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
92
 
 
93
        object_class->dispose = indicator_sound_dispose;
 
94
        object_class->finalize = indicator_sound_finalize;
 
95
 
 
96
        IndicatorObjectClass * io_class = INDICATOR_OBJECT_CLASS(klass);
 
97
        io_class->get_label = get_label;
 
98
        io_class->get_image = get_icon;
 
99
        io_class->get_menu = get_menu;
 
100
 
 
101
    dbus_g_object_register_marshaller (_sound_service_marshal_VOID__INT_BOOLEAN,
 
102
                                     G_TYPE_NONE,
 
103
                                     G_TYPE_INT,
 
104
                                     G_TYPE_BOOLEAN,
 
105
                                     G_TYPE_INVALID);
 
106
 
 
107
        return;
 
108
}
 
109
 
 
110
static void indicator_sound_init (IndicatorSound *self)
 
111
{
 
112
        /* Set good defaults */
 
113
        self->service = NULL;
 
114
 
 
115
        /* Now let's fire these guys up. */
 
116
        self->service = indicator_service_manager_new_version(INDICATOR_SOUND_DBUS_NAME, INDICATOR_SOUND_DBUS_VERSION);
 
117
 
 
118
        g_signal_connect(G_OBJECT(self->service), INDICATOR_SERVICE_MANAGER_SIGNAL_CONNECTION_CHANGE, G_CALLBACK(connection_changed), self);
 
119
 
 
120
 
 
121
    return;
 
122
}
 
123
 
 
124
static void
 
125
connection_changed (IndicatorServiceManager * sm, gboolean connected, gpointer userdata)
 
126
{
 
127
        if (connected) {
 
128
                if (sound_dbus_proxy == NULL) {
 
129
                        GError * error = NULL;
 
130
 
 
131
                        DBusGConnection * sbus = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
 
132
 
 
133
                        sound_dbus_proxy = dbus_g_proxy_new_for_name_owner(sbus,
 
134
                                                                                                                   INDICATOR_SOUND_DBUS_NAME,
 
135
                                                                                                                   INDICATOR_SOUND_SERVICE_DBUS_OBJECT,
 
136
                                                                                                                   INDICATOR_SOUND_SERVICE_DBUS_INTERFACE,
 
137
                                                                                                                   &error);
 
138
 
 
139
                        if (error != NULL) {
 
140
                                g_warning("Unable to get status proxy: %s", error->message);
 
141
                                g_error_free(error);
 
142
                        }
 
143
            g_debug("about to connect to the signals");
 
144
                        dbus_g_proxy_add_signal(sound_dbus_proxy, SIGNAL_SINK_INPUT_WHILE_MUTED, G_TYPE_INT, G_TYPE_BOOLEAN, G_TYPE_INVALID);
 
145
                        dbus_g_proxy_connect_signal(sound_dbus_proxy, SIGNAL_SINK_INPUT_WHILE_MUTED, G_CALLBACK(catch_signal), NULL, NULL);
 
146
                }
 
147
 
 
148
        } else {
 
149
        //TODO : will need to handle this scenario
 
150
        }
 
151
 
 
152
        return;
 
153
}
 
154
 
 
155
static void catch_signal (DBusGProxy * proxy, gint sink_index, gboolean value, gpointer userdata)
 
156
{
 
157
    g_debug("signal caught - i don't believe it !");
 
158
}
 
159
 
 
160
 
 
161
static void
 
162
indicator_sound_dispose (GObject *object)
 
163
{
 
164
        IndicatorSound * self = INDICATOR_SOUND(object);
 
165
 
 
166
        if (self->service != NULL) {
 
167
                g_object_unref(G_OBJECT(self->service));
 
168
                self->service = NULL;
 
169
        }
 
170
    
 
171
 
 
172
        G_OBJECT_CLASS (indicator_sound_parent_class)->dispose (object);
 
173
        return;
 
174
}
 
175
 
 
176
static void
 
177
indicator_sound_finalize (GObject *object)
 
178
{
 
179
 
 
180
        G_OBJECT_CLASS (indicator_sound_parent_class)->finalize (object);
 
181
        return;
 
182
}
 
183
 
 
184
static GtkLabel *
 
185
get_label (IndicatorObject * io)
 
186
{
 
187
        return NULL;
 
188
}
 
189
 
 
190
static GtkImage *
 
191
get_icon (IndicatorObject * io)
 
192
{
 
193
        GtkImage * status_image = GTK_IMAGE(gtk_image_new_from_icon_name("audio-volume-high", GTK_ICON_SIZE_MENU));
 
194
        gtk_widget_show(GTK_WIDGET(status_image));
 
195
        return status_image;
 
196
}
 
197
 
 
198
/* Indicator based function to get the menu for the whole
 
199
   applet.  This starts up asking for the parts of the menu
 
200
   from the various services. */
 
201
static GtkMenu *
 
202
get_menu (IndicatorObject * io)
 
203
{
 
204
    //volume_item = ido_scale_menu_item_new_with_range ("Volume", 0, 100, 1);
 
205
    //gtk_menu_shell_append (GTK_MENU_SHELL (menu), volume_item);
 
206
    return GTK_MENU(dbusmenu_gtkmenu_new(INDICATOR_SOUND_DBUS_NAME, INDICATOR_SOUND_DBUS_OBJECT));
 
207
}
 
208
 
 
209