aboutsummaryrefslogtreecommitdiff
path: root/src/notification.c
blob: 9b0a3c9bfbd78692bddc7e517eff9c7487acd2ce (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
/*
 * notification.c - A gobject subclass to represent a org.freedesktop.Notification.Notify message.
 */

#include "notification.h"

#define COLUMN_APP_NAME       0
#define COLUMN_REPLACES_ID    1
#define COLUMN_APP_ICON       2
#define COLUMN_SUMMARY        3
#define COLUMN_BODY           4
#define COLUMN_ACTIONS        5
#define COLUMN_HINTS          6
#define COLUMN_EXPIRE_TIMEOUT 7

#define COLUMN_COUNT 8

static void notification_class_init(NotificationClass *klass);
static void notification_init(Notification *self);
static void notification_dispose(GObject *object);

G_DEFINE_TYPE (Notification, notification, G_TYPE_OBJECT);

static void
notification_class_init(NotificationClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS(klass);

  g_type_class_add_private(klass, sizeof(NotificationPrivate));

  object_class->dispose = notification_dispose;
}

static void
notification_init(Notification *self)
{
  self->priv = NOTIFICATION_GET_PRIVATE(self);

  self->priv->app_name = NULL;
  self->priv->replaces_id = 0;
  self->priv->app_icon = NULL;
  self->priv->summary = NULL;
  self->priv->body = NULL;
  self->priv->expire_timeout = 0;
}

static void
notification_dispose(GObject *object)
{
  Notification *self = NOTIFICATION(object);

  if(self->priv->app_name != NULL) {
    g_free(self->priv->app_name);
    self->priv->app_name = NULL;
  }

  if(self->priv->app_icon != NULL) {
    g_free(self->priv->app_icon);
    self->priv->app_icon = NULL;
  }

  if(self->priv->summary != NULL) {
    g_free(self->priv->summary);
    self->priv->summary = NULL;
  }

  if(self->priv->body != NULL) {
    g_free(self->priv->body);
    self->priv->body = NULL;
  }

  G_OBJECT_CLASS(notification_parent_class)->dispose(object);
}

Notification* 
notification_new(void)
{
  return NOTIFICATION(g_object_new(NOTIFICATION_TYPE, NULL));
}

Notification*
notification_new_from_dbus_message(GDBusMessage *message)
{
  Notification *self = notification_new();

  GVariant *body = g_dbus_message_get_body(message);
  GVariant *child = NULL;
  g_assert(g_variant_is_of_type(body, G_VARIANT_TYPE_TUPLE));
  g_assert(g_variant_n_children(body) == COLUMN_COUNT);

  /* app_name */
  child = g_variant_get_child_value(body, COLUMN_APP_NAME);
  g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_STRING));
  self->priv->app_name = g_variant_dup_string(child, 
      &(self->priv->app_name_length));

  /* replaces_id */
  child = g_variant_get_child_value(body, COLUMN_REPLACES_ID);
  g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_UINT32));
  self->priv->replaces_id = g_variant_get_uint32(child);

  /* app_icon */
  child = g_variant_get_child_value(body, COLUMN_APP_ICON);
  g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_STRING));
  self->priv->app_icon = g_variant_dup_string(child,
      &(self->priv->app_icon_length));

  /* summary */
  child = g_variant_get_child_value(body, COLUMN_SUMMARY);
  g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_STRING));
  self->priv->summary = g_variant_dup_string(child,
      &(self->priv->summary_length));

  /* body */
  child = g_variant_get_child_value(body, COLUMN_BODY);
  g_assert(g_variant_is_of_type(child, G_VARIANT_TYPE_STRING));
  self->priv->body = g_variant_dup_string(child,
      &(self->priv->body_length));

  child = NULL;

  return self;
}

void
notification_print(Notification *self)
{
  g_print("app_name = %s\n", self->priv->app_name);
  g_print("app_icon = %s\n", self->priv->app_icon);
  g_print("summary = %s\n", self->priv->summary);
  g_print("body = %s\n", self->priv->body);
}