aboutsummaryrefslogtreecommitdiff
path: root/tests/test-approver.c
blob: df06df24d6551df8af66e0d7e826682b5a85542d (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
#include <glib.h>
#include <glib-object.h>

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

#include "notification-watcher-client.h"
#include "dbus-shared.h"

#define APPROVER_PATH  "/my/approver"

#define TEST_APPROVER_TYPE            (test_approver_get_type ())
#define TEST_APPROVER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_APPROVER_TYPE, TestApprover))
#define TEST_APPROVER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_APPROVER_TYPE, TestApproverClass))
#define IS_TEST_APPROVER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_APPROVER_TYPE))
#define IS_TEST_APPROVER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_APPROVER_TYPE))
#define TEST_APPROVER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_APPROVER_TYPE, TestApproverClass))

typedef struct _TestApprover      TestApprover;
typedef struct _TestApproverClass TestApproverClass;

struct _TestApproverClass {
	GObjectClass parent_class;
};

struct _TestApprover {
	GObject parent;
};

GType test_approver_get_type (void);

static void test_approver_class_init (TestApproverClass *klass);
static void test_approver_init       (TestApprover *self);
static void _notification_approver_server_approve_item (void);

#include "../src/notification-approver-server.h"

GMainLoop * main_loop = NULL;
DBusGConnection * session_bus = NULL;
DBusGProxy * bus_proxy = NULL;
gboolean passed = FALSE;

G_DEFINE_TYPE (TestApprover, test_approver, G_TYPE_OBJECT);

static void
test_approver_class_init (TestApproverClass *klass)
{
	dbus_g_object_type_install_info(TEST_APPROVER_TYPE,
	                                &dbus_glib__notification_approver_server_object_info);

	return;
}

static void
test_approver_init (TestApprover *self)
{
	dbus_g_connection_register_g_object(session_bus,
	                                    APPROVER_PATH,
	                                    G_OBJECT(self));

	return;
}

static void
_notification_approver_server_approve_item (void)
{
	return;
}

gint owner_count = 0;
gboolean
check_for_service (gpointer user_data)
{
	g_debug("Checking for Watcher");

	if (owner_count > 100) {
		g_warning("Couldn't find watcher after 100 tries.");
		g_main_loop_quit(main_loop);
		return FALSE;
	}

	owner_count++;

	gboolean has_owner = FALSE;
	org_freedesktop_DBus_name_has_owner(bus_proxy, NOTIFICATION_WATCHER_DBUS_ADDR, &has_owner, NULL);

	if (has_owner) {
		const char * cats = NULL;
		DBusGProxy * proxy = dbus_g_proxy_new_for_name(session_bus,
		                                               NOTIFICATION_WATCHER_DBUS_ADDR,
		                                               NOTIFICATION_WATCHER_DBUS_OBJ,
		                                               NOTIFICATION_WATCHER_DBUS_IFACE);

		g_debug("Registering Approver");
		GError * error = NULL;
		org_kde_StatusNotifierWatcher_register_notification_approver (proxy, APPROVER_PATH, &cats, &error);

		if (error != NULL) {
			g_warning("Unable to register approver: %s", error->message);
			g_error_free(error);
			g_main_loop_quit(main_loop);
		}

		return FALSE;
	}

	return TRUE;
}

int
main (int argc, char ** argv)
{
	GError * error = NULL;

	g_type_init();
	g_debug("Initing");

	session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
	if (error != NULL) {
		g_warning("Unable to get session bus: %s", error->message);
		g_error_free(error);
		return -1;
	}

	TestApprover * approver = g_object_new(TEST_APPROVER_TYPE, NULL);

	bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);

	g_timeout_add(100, check_for_service, NULL);

	main_loop = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(main_loop);

	g_object_unref(approver);

	if (!passed) {
		return -1;
	}

	return 0;
}