#!/usr/bin/python

import xmlrpclib
import pynotify
import time
import cgi

pynotify.init("msgclient")

s = xmlrpclib.ServerProxy("http://qnap.local:8000")

def test(s):
  print s.system.listMethods()

  num_messages = s.num_messages()
  print "Number of waiting messages:", num_messages

  print "Adding default test message"
  s.add_message()

  num_messages = s.num_messages()
  print "Number of waiting messages:", num_messages

  print "Adding ('testme','testme')"
  s.add_message('testc','testc')

  num = s.num_messages()
  print "Number of waiting messages:", num
  print "Getting messages..."

if __name__=='__main__':

  n = pynotify.Notification("msgclient","started")
  n.show()

  while 1:
    i = 0
    j = 0
    print "Pinging..."
    num = s.num_messages()
    # if there are more than 5, group them...
    while j < num and j < 5:
      t = s.next_message()
      n = pynotify.Notification(t[0],cgi.escape(t[1]))
      n.show()  
      print t
      j = j + 1
    time.sleep(5)
