~elementary-os/elementaryos/os-patch-notify-osd-precise

« back to all changes in this revision

Viewing changes to src/dbus.c

  • Committer: Sergey "Shnatsel" Davidoff
  • Date: 2012-06-18 21:08:31 UTC
  • Revision ID: shnatsel@gmail.com-20120618210831-g6k5y7vecjdylgic
Initial import, version 0.9.34-0ubuntu2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
**3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
 
3
**      10        20        30        40        50        60        70        80
 
4
**
 
5
** notify-osd
 
6
**
 
7
** dbus.c - dbus boiler-plate code for talking with libnotify
 
8
**
 
9
** Copyright 2009 Canonical Ltd.
 
10
**
 
11
** Authors:
 
12
**    Mirco "MacSlow" Mueller <mirco.mueller@canonical.com>
 
13
**    David Barth <david.barth@canonical.com>
 
14
**
 
15
** This program is free software: you can redistribute it and/or modify it
 
16
** under the terms of the GNU General Public License version 3, as published
 
17
** by the Free Software Foundation.
 
18
**
 
19
** This program is distributed in the hope that it will be useful, but
 
20
** WITHOUT ANY WARRANTY; without even the implied warranties of
 
21
** MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
22
** PURPOSE.  See the GNU General Public License for more details.
 
23
**
 
24
** You should have received a copy of the GNU General Public License along
 
25
** with this program.  If not, see <http://www.gnu.org/licenses/>.
 
26
**
 
27
*******************************************************************************/
 
28
 
 
29
#include <string.h>
 
30
#include <stdlib.h>
 
31
#include <dbus/dbus.h>
 
32
#include <dbus/dbus-glib.h>
 
33
#include <dbus/dbus-glib-bindings.h>
 
34
#include <dbus/dbus-glib-lowlevel.h>
 
35
 
 
36
#include "dbus.h"
 
37
 
 
38
static DBusGConnection* connection = NULL;
 
39
 
 
40
DBusGConnection*
 
41
dbus_get_connection (void)
 
42
{
 
43
        /* usefull mostly for unit tests */
 
44
        if (connection == NULL)
 
45
                connection = dbus_create_service_instance (DBUS_NAME);
 
46
 
 
47
        return connection;
 
48
}
 
49
 
 
50
DBusGConnection*
 
51
dbus_create_service_instance (const char *service_name)
 
52
{
 
53
        DBusGProxy*      proxy      = NULL;
 
54
        guint            request_name_result;
 
55
        GError*          error      = NULL;
 
56
 
 
57
        error = NULL;
 
58
        connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
 
59
        if (error)
 
60
        {
 
61
                g_warning ("dbus_create_service_instance(): "
 
62
                           "Got error \"%s\"\n",
 
63
                           error->message);
 
64
                g_error_free (error);
 
65
                return NULL;
 
66
        }
 
67
 
 
68
        proxy = dbus_g_proxy_new_for_name (connection,
 
69
                                           "org.freedesktop.DBus",
 
70
                                           "/org/freedesktop/Dbus",
 
71
                                           "org.freedesktop.DBus");
 
72
        error = NULL;
 
73
        if (!dbus_g_proxy_call (proxy,
 
74
                                "RequestName",
 
75
                                &error,
 
76
                                G_TYPE_STRING, service_name,
 
77
                                G_TYPE_UINT, 0,
 
78
                                G_TYPE_INVALID,
 
79
                                G_TYPE_UINT, &request_name_result,
 
80
                                G_TYPE_INVALID))
 
81
        {
 
82
                g_warning ("dbus_create_service_instance(): "
 
83
                           "Got error \"%s\"\n",
 
84
                           error->message);
 
85
                g_error_free (error);
 
86
                return NULL;
 
87
        }
 
88
 
 
89
        if (request_name_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
 
90
        {
 
91
                g_warning ("Another instance has already registered %s", service_name);
 
92
                return NULL;
 
93
        }
 
94
 
 
95
        return connection;
 
96
}
 
97
 
 
98
 
 
99
void
 
100
dbus_send_close_signal (gchar *dest,
 
101
                        guint id, 
 
102
                        guint reason)
 
103
{
 
104
        DBusMessage *msg;
 
105
 
 
106
        msg = dbus_message_new_signal ("/org/freedesktop/Notifications",
 
107
                                       "org.freedesktop.Notifications",
 
108
                                       "NotificationClosed");
 
109
        dbus_message_set_destination (msg, dest);
 
110
        dbus_message_append_args (msg, DBUS_TYPE_UINT32, &id,
 
111
                                  DBUS_TYPE_INVALID);
 
112
        dbus_message_append_args (msg, DBUS_TYPE_UINT32, &reason,
 
113
                                  DBUS_TYPE_INVALID);
 
114
 
 
115
        dbus_connection_send (dbus_g_connection_get_connection (connection),
 
116
                              msg,
 
117
                              NULL);
 
118
 
 
119
        dbus_message_unref (msg);
 
120
}
 
121
 
 
122
void
 
123
dbus_send_action_signal (gchar *dest,
 
124
                         guint id, 
 
125
                         const char *action_key)
 
126
{
 
127
        DBusMessage *msg;
 
128
 
 
129
        msg = dbus_message_new_signal ("/org/freedesktop/Notifications",
 
130
                                       "org.freedesktop.Notifications",
 
131
                                       "ActionInvoked");
 
132
        dbus_message_set_destination (msg, dest);
 
133
        dbus_message_append_args (msg, DBUS_TYPE_UINT32, &id,
 
134
                                  DBUS_TYPE_INVALID);
 
135
        dbus_message_append_args (msg, DBUS_TYPE_STRING, &action_key,
 
136
                                  DBUS_TYPE_INVALID);
 
137
 
 
138
        dbus_connection_send (dbus_g_connection_get_connection (connection),
 
139
                              msg,
 
140
                              NULL);
 
141
 
 
142
        dbus_message_unref (msg);
 
143
}