summaryrefslogtreecommitdiff
path: root/notificationmonitor_p.h
blob: 069abc47a6e49846df49130c8b1e90f98920146b (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
#ifndef WATCHFISH_NOTIFICATIONMONITOR_P_H
#define WATCHFISH_NOTIFICATIONMONITOR_P_H

#include <QtCore/QObject>
#include <dbus/dbus.h>

#include "notificationmonitor.h"

#define CATEGORY_DEFINITION_FILE_DIRECTORY "/usr/share/lipstick/notificationcategories"
#define CATEGORY_REFRESH_CHECK_TIME 120

namespace watchfish
{

struct NotificationCategoryCacheEntry
{
	QHash<QString, QString> data;
	QDateTime lastReadTime;
	QDateTime lastCheckTime;
};

struct ProtoNotification
{
	QString sender;
	QString appIcon;
	QString summary;
	QString body;
	QHash<QString, QString> hints;
	int expireTimeout;
	QStringList actions;
};

class NotificationMonitorPrivate : public QObject
{
	Q_OBJECT

public:
	NotificationMonitorPrivate(NotificationMonitor *q);
	~NotificationMonitorPrivate();

private:
	/** Converts a ProtoNotification into a Notification object and raises it. */
	void processIncomingNotification(quint32 id, const ProtoNotification &proto);
	/** Raises appropiate notification signal. */
	void processCloseNotification(quint32 id, quint32 reason);

	/** Sends a D-Bus message for a method call with a single string argument. */
	void sendMessageWithString(const char *service, const char *path, const char *iface, const char *method, const char *arg);
	/** Adds a D-Bus filter match rule. */
	void addMatchRule(const char *rule);
	void removeMatchRule(const char *rule);

	/** Converts a fdo Notification D-Bus message into a ProtoNotification object. */
	ProtoNotification parseNotifyCall(DBusMessage *msg) const;

	QHash<QString,QString> getCategoryInfo(const QString &s) const;

	static dbus_bool_t busWatchAdd(DBusWatch *watch, void *data);
	static void busWatchRemove(DBusWatch *watch, void *data);
	static void busWatchToggle(DBusWatch *watch, void *data);

	static DBusHandlerResult busMessageFilter(DBusConnection *conn, DBusMessage *msg, void *user_data);

private slots:
	void handleBusSocketActivated();

private:
	NotificationMonitor * const q_ptr;
	Q_DECLARE_PUBLIC(NotificationMonitor)

	/** The current set of monitored notifications, indexed by id. */
	QMap<quint32, Notification*> _notifs;
	/** Low level dbus connection used for sniffing. */
	DBusConnection *_conn;
	/** Serials of DBUS method calls of which we are expecting a reply. */
	QHash<quint32, ProtoNotification> _pendingConfirmation;
	/** Cache of notification category info. */
	mutable QHash<QString, NotificationCategoryCacheEntry> _categoryCache;
};


}

#endif // WATCHFISH_NOTIFICATIONMONITOR_P_H