File: notify_proxy.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (182 lines) | stat: -rw-r--r-- 5,275 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains the code for sending notifications to user
 **************************************************************************
 *
 * LADI Session Handler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "notify_proxy.h"

#define NOTIFY_SERVICE "org.freedesktop.Notifications"
#define NOTIFY_OBJECT  "/org/freedesktop/Notifications"
#define NOTIFY_IFACE   "org.freedesktop.Notifications"
#define NOTIFY_METHOD_NOTIFY "Notify"

static char * g_notify_app_name;

bool ladish_notify_init(const char * app_name)
{
  const char * name;
  const char * vendor;
  const char * version;
  const char * spec_version;

  g_notify_app_name = strdup(app_name);
  if (g_notify_app_name == NULL)
  {
    log_error("strdup() failed for app name");
    return false;
  }

  if (cdbus_call(0, NOTIFY_SERVICE, NOTIFY_OBJECT, NOTIFY_IFACE, "GetServerInformation", "", "ssss", &name, &vendor, &version, &spec_version))
  {
    log_info("Sending notifications to '%s' '%s' (%s, %s)", vendor, name, version, spec_version);
  }

  return true;
}

void ladish_notify_uninit(void)
{
  free(g_notify_app_name);
  g_notify_app_name = NULL;
}

void ladish_notify_simple(uint8_t urgency, const char * summary, const char * body)
{
  DBusMessage * request_ptr;
  /* DBusMessage * reply_ptr; */
  DBusMessageIter iter;
  DBusMessageIter array_iter;
  DBusMessageIter dict_iter;
  const char * str_value;
  uint32_t uint32_value;
  int32_t int32_value;

  if (g_notify_app_name == NULL)
  {
    /* notifications are disabled */
    return;
  }

  request_ptr = dbus_message_new_method_call(NOTIFY_SERVICE, NOTIFY_OBJECT, NOTIFY_IFACE, NOTIFY_METHOD_NOTIFY);
  if (request_ptr == NULL)
  {
    log_error("dbus_message_new_method_call() failed.");
    goto exit;
  }

  dbus_message_iter_init_append(request_ptr, &iter);

  /* app_name */
  str_value = g_notify_app_name;
  if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
  {
    log_error("dbus_message_iter_append_basic() failed.");
    goto free_request;
  }

  /* replaces_id */
  uint32_value = 0;
  if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_UINT32, &uint32_value))
  {
    log_error("dbus_message_iter_append_basic() failed.");
    goto free_request;
  }

  /* app_icon */
  str_value = "";
  if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
  {
    log_error("dbus_message_iter_append_basic() failed.");
    goto free_request;
  }

  /* summary */
  str_value = summary;
  if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
  {
    log_error("dbus_message_iter_append_basic() failed.");
    goto free_request;
  }

  /* body */
  str_value = body == NULL ? "" : body;
  if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &str_value))
  {
    log_error("dbus_message_iter_append_basic() failed.");
    goto free_request;
  }

  /* actions */
  if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &array_iter))
  {
    log_error("dbus_message_iter_open_container() failed.");
    goto free_request;
  }

  if (!dbus_message_iter_close_container(&iter, &array_iter))
  {
    log_error("dbus_message_iter_close_container() failed.");
    goto free_request;
  }

  /* hints */
  if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &dict_iter))
  {
    log_error("dbus_message_iter_open_container() failed.");
    goto free_request;
  }

  if (!cdbus_iter_append_dict_entry(&dict_iter, DBUS_TYPE_BYTE, "urgency", &urgency, 0))
  {
    log_error("dbus_iter_append_dict_entry() failed.");
    goto free_request;
  }

  if (!dbus_message_iter_close_container(&iter, &dict_iter))
  {
    log_error("dbus_message_iter_close_container() failed.");
    goto free_request;
  }

  /* expire_timeout */
  int32_value = urgency == LADISH_NOTIFY_URGENCY_HIGH ? 0 : -1;
  if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &int32_value))
  {
    log_error("dbus_message_iter_append_basic() failed.");
    goto free_request;
  }

  if (!cdbus_call(0, NOTIFY_SERVICE, NOTIFY_OBJECT, NOTIFY_IFACE, NOTIFY_METHOD_NOTIFY, NULL, request_ptr, "u", &uint32_value))
  {
    //log_error("Notify() dbus call failed.");
    goto free_request;
  }

  //log_info("notify ID is %"PRIu32, uint32_value);

free_request:
  dbus_message_unref(request_ptr);
exit:
  return;
}