~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to indicator/notification.c

  • Committer: Jason Conti
  • Date: 2011-03-26 21:20:00 UTC
  • Revision ID: jason.conti@gmail.com-20110326212000-48rj1zased29tne4
Ported Notification.py to gobject introspection, everything except dbus. Going to attempt that next, but may revert if it doesn't work so well yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
#include <dbus/dbus.h>
 
5
 
 
6
#define NOTIFY_FILTER "type='method_call',interface='org.freedesktop.Notifications',member='Notify'"
 
7
 
 
8
#define MESSAGE_APPNAME 0
 
9
#define MESSAGE_SUMMARY 3
 
10
#define MESSAGE_BODY    4
 
11
 
 
12
static DBusHandlerResult 
 
13
monitor_filter_func(DBusConnection *connection, DBusMessage *message, void *user_data)
 
14
{
 
15
  DBusMessageIter iter;
 
16
  int message_type;
 
17
  int arg_type;
 
18
  int index = 0;
 
19
  char *value;
 
20
 
 
21
  printf("message received\n");
 
22
 
 
23
  message_type = dbus_message_get_type(message);
 
24
  if(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL) {
 
25
    printf("received method_call\n");
 
26
 
 
27
    dbus_message_iter_init(message, &iter);
 
28
    do {
 
29
      arg_type = dbus_message_iter_get_arg_type(&iter);
 
30
 
 
31
      switch(arg_type) {
 
32
        case DBUS_TYPE_STRING:
 
33
          {
 
34
            dbus_message_iter_get_basic(&iter, &value);
 
35
 
 
36
            switch(index) {
 
37
              case MESSAGE_APPNAME:
 
38
                printf("appname: %s\n", value);
 
39
                break;
 
40
              case MESSAGE_SUMMARY:
 
41
                printf("summary: %s\n", value);
 
42
                break;
 
43
              case MESSAGE_BODY:
 
44
                printf("body: %s\n", value);
 
45
                break;
 
46
            }
 
47
            break;
 
48
          }
 
49
      }
 
50
 
 
51
      index++;
 
52
    } while(dbus_message_iter_next(&iter));
 
53
  }
 
54
  else {
 
55
    printf("received invalid message type: %d\n", message_type);
 
56
  }
 
57
  
 
58
  if(dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
 
59
    exit(0);
 
60
  
 
61
  return DBUS_HANDLER_RESULT_HANDLED;
 
62
}
 
63
 
 
64
int 
 
65
main(int argc, char* argv[])
 
66
{
 
67
  DBusConnection *connection;
 
68
  DBusError error;
 
69
  DBusBusType type = DBUS_BUS_SESSION;
 
70
  DBusHandleMessageFunction filter_func = monitor_filter_func;
 
71
 
 
72
  dbus_error_init(&error);
 
73
 
 
74
  connection = dbus_bus_get(type, &error);
 
75
  if(connection == NULL) {
 
76
    fprintf(stderr, "Failed to open connection to session bus: %s\n", error.message);
 
77
    dbus_error_free(&error);
 
78
    exit(1);
 
79
  }
 
80
 
 
81
  dbus_bus_add_match(connection, NOTIFY_FILTER, &error);
 
82
  if(dbus_error_is_set(&error)) {
 
83
    fprintf(stderr, "Failed to add match: %s\n", error.message);
 
84
    dbus_error_free(&error);
 
85
    exit(1);
 
86
  }
 
87
 
 
88
  if(!dbus_connection_add_filter(connection, filter_func, NULL, NULL)) {
 
89
    fprintf(stderr, "Failed to add filter.\n");
 
90
    exit(1);
 
91
  }
 
92
 
 
93
  while(dbus_connection_read_write_dispatch(connection, -1)); 
 
94
 
 
95
  exit(0);
 
96
}