summaryrefslogtreecommitdiffstats
path: root/src/daemon/daemon.cpp
blob: 04f0b84e0b0c7629815f0a86383ed4c2df2331fb (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* $Id: daemon.c 2337 2007-01-11 19:17:30Z nick $
 *
 * Copyright (C) 2006 Christian Hammond <chipx86@chipx86.com>
 * Copyright (C) 2005 John (J5) Palmieri <johnp@redhat.com>
 * Copyright (C) 2006 Nick Schermer <nick@xfce.org>
 *
 * This program 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, or (at your option)
 * any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "config.h"

#include <tqsignalmapper.h>
#include <tqevent.h>
#include <tqsize.h>
#include <tqcursor.h>
#include <tqpixmap.h>
#include <tqtimer.h>
#include <knotifyclient.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kglobalsettings.h>

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#include "daemon.h"

NotifierContainer* GTKNotifierContainer = NULL;
void real_handleGTKMain();

NotifierContainer::NotifierContainer() : TQWidget() {
	mPopupList.clear();

	// Determine bottom of desktop
	TQPoint cursorPos = TQCursor::pos();
	TQRect r = KGlobalSettings::desktopGeometry(cursorPos);
	mTopOfStack = r.height();
	mRightOfStack = r.width();
}

NotifierContainer::~NotifierContainer() {
}

void NotifierContainer::handleGTKMain() {
	real_handleGTKMain();
}

void NotifierContainer::displayMessage(TQString title, TQString message, TQString icon, int x, int y) {
	TQPixmap px;
	KIconLoader* il = KGlobal::iconLoader();
	px = il->loadIcon( icon, KIcon::NoGroup );
// 	if (px.isNull()) {
// 		px = il->loadIcon( "gnome_apps", KIcon::NoGroup );
// 	}

	KPassivePopup *pop = new KPassivePopup( KPassivePopup::Boxed, this, "" );
	pop->setAutoDelete( true );
	pop->setView( title, message, icon );
	pop->setTimeout( -1 );
	TQPoint leftCorner( x, y);
	if (leftCorner.isNull()) {
		if (mPopupList.isEmpty()) {
			// Determine bottom of desktop
			TQPoint cursorPos = TQCursor::pos();
			TQRect r = KGlobalSettings::desktopGeometry(cursorPos);
			mTopOfStack = r.height();
			mRightOfStack = r.width();
		}
		TQSize popupSize = pop->sizeHint();
		mTopOfStack = mTopOfStack-popupSize.height();
		if (mTopOfStack < 0) mTopOfStack = 0;
		leftCorner.setX(mRightOfStack-popupSize.width());
		leftCorner.setY(mTopOfStack);
	}
	connect(pop, SIGNAL(hidden(KPassivePopup*)), this, SLOT(popupClosed(KPassivePopup*)));
	mPopupList.append(pop);
	pop->show(leftCorner);

	processEvents();
}

void NotifierContainer::processEvents() {
	tqApp->processEvents();
}

void NotifierContainer::popupClosed(KPassivePopup* popup) {
	// Remove the popup from our list of popups
	mPopupList.remove(popup);

	if (mPopupList.isEmpty()) {
		// Determine bottom of desktop
		TQPoint cursorPos = TQCursor::pos();
		TQRect r = KGlobalSettings::desktopGeometry(cursorPos);
		mTopOfStack = r.height();
		mRightOfStack = r.width();
	}
}

#undef signals

#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>

#include <gdk/gdkx.h>

#include "notificationdaemon-dbus-glue.h"

#define IMAGE_SIZE 48

#define NW_GET_NOTIFY_ID(nw) \
	(GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(nw), "_notify_id")))
#define NW_GET_NOTIFY_SENDER(nw) \
	(g_object_get_data(G_OBJECT(nw), "_notify_sender"))
#define NW_GET_DAEMON(nw) \
	(g_object_get_data(G_OBJECT(nw), "_notify_daemon"))

static const char *description =
    I18N_NOOP("A DBUS notification to TDE interface.");
static const char *message =
    I18N_NOOP("First release October 2011.");
static const char *version = "0.01";

typedef struct
{
	GTimeVal expiration;
	GTimeVal paused_diff;
	gboolean has_timeout;
	gboolean paused;
	guint id;
	GtkWindow *nw;

} NotifyTimeout;

static DBusConnection *dbus_conn = NULL;

#define CHECK_DBUS_VERSION(major, minor) \
	(DBUS_MAJOR_VER > (major) || \
	 (DBUS_MAJOR_VER == (major) && DBUS_MINOR_VER >= (minor)))

#if !CHECK_DBUS_VERSION(0, 60)
/* This is a hack that will go away in time. For now, it's fairly safe. */
struct _DBusGMethodInvocation
{
	DBusGConnection *connection;
	DBusGMessage *message;
	const DBusGObjectInfo *object;
	const DBusGMethodInfo *method;
};
#endif /* D-BUS < 0.60 */

G_DEFINE_TYPE(NotifyDaemon, notify_daemon, G_TYPE_OBJECT);

static void
notify_daemon_finalize(GObject *object)
{
	NotifyDaemon *daemon       = NOTIFY_DAEMON(object);
	GObjectClass *parent_class = G_OBJECT_CLASS(notify_daemon_parent_class);

	if (parent_class->finalize != NULL)
		parent_class->finalize(object);
}

static void
notify_daemon_class_init(NotifyDaemonClass *daemon_class)
{
	GObjectClass *object_class = G_OBJECT_CLASS(daemon_class);

	object_class->finalize = notify_daemon_finalize;
}

static void
notify_daemon_init(NotifyDaemon *daemon)
{
}

gboolean
notify_daemon_notify_handler(NotifyDaemon *daemon,
							 const gchar *app_name,
							 guint id,
							 const gchar *icon,
							 const gchar *summary,
							 const gchar *body,
							 gchar **actions,
							 GHashTable *hints,
							 int timeout, DBusGMethodInvocation *context)
{
	NotifyDaemonPrivate *priv = daemon->priv;
	NotifyTimeout *nt = NULL;
	GtkWindow *nw = NULL;
	GValue *data;
	gboolean use_pos_data = FALSE;
	gboolean new_notification = FALSE;
	gint x = 0;
	gint y = 0;
	guint return_id;
	gchar *sender;
	gint i;

	/* deal with x, and y hints */
	if ((data = (GValue *)g_hash_table_lookup(hints, "x")) != NULL)
	{
		x = g_value_get_int(data);

		if ((data = (GValue *)g_hash_table_lookup(hints, "y")) != NULL)
		{
			y = g_value_get_int(data);
			use_pos_data = TRUE;
		}
	}

	// Send a notification request to KDE here...
	TQString messageText = TQString(body);

	GTKNotifierContainer->displayMessage(TQString(summary), TQString(body), TQString(icon), x, y);

	return_id = 0;

	dbus_g_method_return(context, return_id);

	return TRUE;
}

gboolean
notify_daemon_close_notification_handler(NotifyDaemon *daemon,
										 guint id, GError **error)
{
	// Do nothing

	return TRUE;
}

gboolean
notify_daemon_get_capabilities(NotifyDaemon *daemon, char ***caps)
{
	*caps = g_new0(char *, 6);

	(*caps)[0] = g_strdup("actions");
	(*caps)[1] = g_strdup("body");
	(*caps)[2] = g_strdup("body-hyperlinks");
	(*caps)[3] = g_strdup("body-markup");
	(*caps)[4] = g_strdup("icon-static");
	(*caps)[5] = NULL;

	return TRUE;
}

gboolean
notify_daemon_reload_settings (NotifyDaemon *daemon)
{
	// Do nothing

	return TRUE;
}

gboolean
notify_daemon_get_server_information(NotifyDaemon *daemon,
									 char **out_name,
									 char **out_vendor,
									 char **out_version,
									 char **out_spec_ver)
{
	*out_name     = g_strdup("Notification Daemon");
	*out_vendor   = g_strdup("Trinity Desktop Project");
	*out_version  = g_strdup(VERSION);
	*out_spec_ver = g_strdup("0.1");

	return TRUE;
}

int
main(int argc, char **argv)
{
	NotifyDaemon *daemon;
	DBusGConnection *connection;
	DBusGProxy *bus_proxy;
	GError *error;
	guint request_name_result;
	
	g_set_application_name ("notification-daemon-tde");

#ifdef G_ENABLE_DEBUG
	g_log_set_always_fatal(G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
#endif

	gtk_init(&argc, &argv);

	error = NULL;

	connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);

	if (connection == NULL)
	{
		g_printerr("Failed to open connection to bus: %s\n",
				   error->message);
		g_error_free(error);
		exit(1);
	}

	dbus_conn = dbus_g_connection_get_connection(connection);

	dbus_g_object_type_install_info(NOTIFY_TYPE_DAEMON,
									&dbus_glib_notification_daemon_tde_object_info);

	bus_proxy = dbus_g_proxy_new_for_name(connection,
										  "org.freedesktop.DBus",
										  "/org/freedesktop/DBus",
										  "org.freedesktop.DBus");

	if (!dbus_g_proxy_call(bus_proxy, "RequestName", &error,
						   G_TYPE_STRING, "org.freedesktop.Notifications",
						   G_TYPE_UINT, 0,
						   G_TYPE_INVALID,
						   G_TYPE_UINT, &request_name_result,
						   G_TYPE_INVALID))
	{
		g_error("Could not aquire name: %s", error->message);
	}

	daemon = static_cast<NotifyDaemon*>(g_object_new(NOTIFY_TYPE_DAEMON, NULL));

	dbus_g_connection_register_g_object(connection,
										"/org/freedesktop/Notifications",
										G_OBJECT(daemon));

	KAboutData aboutData("notification-daemon-tde", I18N_NOOP("TDE DBUS Notification Daemon"), version,
		description, KAboutData::License_GPL,
		"(c) 2011, Timothy Pearson",
		message, 0 /* TODO: Website */, "kb9vqf@pearsoncomputing.net");

	TDECmdLineArgs::init(argc, argv, &aboutData);

	TDEApplication app;
	NotifierContainer nc;
	app.setMainWidget(&nc);
	GTKNotifierContainer = &nc;
	TQTimer *gtkEventProcessor = new TQTimer( &app );
	TQObject::connect( gtkEventProcessor, SIGNAL(timeout()), &nc, SLOT(handleGTKMain()) );
	gtkEventProcessor->start( 100, FALSE ); // Every 0.1 seconds poll gtk for DBUS events
	app.disableSessionManagement();
	app.exec();

	return 0;
}

void real_handleGTKMain() {
	while (gtk_events_pending())
		gtk_main_iteration();
}