#!/usr/bin/env python
# vim:shiftwidth=2:tabstop=2
#
# pidgin-away.py
# 
#   Copyright (c) 2007, David Mohr <david@mcbf.net> 
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#
# This program uses the D-Bus protocol to
#   * Create a new away message, and activate it              (set)
#   * Delete an away message, and set the status to available (del)
#
# Note: this works on my system, but I haven't tested it anywhere else.
# In particular, I don't know if setting the status back to available
# will work. Please email me if it doesn't!
#
# Largely based on the great article by ars technica:
#   http://arstechnica.com/reviews/apps/pidgin-2-0.ars/4
# 
# 2007-05-20  v0.1  Initial release 
# 2007-06-25  v0.2  Minor bugfixes
#

import gtk, dbus, sys


# Specify status ID values
STATUS_AVAILABLE = 2
STATUS_AWAY = 5

def find_min(a):
  min = 999999999999
  for i in range(len(a)):
    #print a[i]
    if a[i] < min:
      min = a[i]
  return min

def set_status(name, kind, message):
  # Create a new saved status with the specified name and kind
  status = purple.PurpleSavedstatusNew(name, kind)
  # Associate the specified availability message with the new saved status
  purple.PurpleSavedstatusSetMessage(status, message)
  # Activate the new saved status
  purple.PurpleSavedstatusActivate(status)

def remove_status(name, kind):
  all = purple.PurpleSavedstatusesGetAll()
  avail = find_min(all)
  purple.PurpleSavedstatusActivate(avail)
  res = purple.PurpleSavedstatusDelete(name)
  #print "Removing Status %s = %d" % (name, res)

def usage():
  print "Usage: %s set NAME MESSAGE" % sys.argv[0]
  print "            Sets pidgin to the new away message NAME, with text MESSAGE"
  print "       %s del NAME" % sys.argv[0]
  print "            Deletes pidgin away message with title NAME"
  print "       %s show" % sys.argv[0]
  print "            Shows the current account status"
  sys.exit(1)


if len(sys.argv) < 2:
  usage()

# Initiate a connection to the Session Bus
try:
  bus = dbus.SessionBus()
except Exception:
  print "Could not connect to the dbus!"
  sys.exit(2)

# Associate Pidgin's D-Bus interface with Python objects
try:
  obj = bus.get_object(
      "im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
  purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
except Exception:
  print "Could not connect to Pidgin on the dbus!"
  sys.exit(3)


if sys.argv[1] == "set":
  if len(sys.argv) < 4:
    usage()
  name = sys.argv[2]
  message = sys.argv[3]
  set_status(name, STATUS_AWAY, message)
elif sys.argv[1] == "del":
  if len(sys.argv) < 3:
    usage()
  name = sys.argv[2]
  remove_status(name, STATUS_AVAILABLE) 
elif sys.argv[1] == "show":
  # Iterate through every active account
  print "Current status:"
  for acctID in purple.PurpleAccountsGetAllActive():
    # Retrieve the current status
    status = purple.PurpleSavedstatusGetCurrent()

    # Display the username and availability status/message for each account
    print "%d | %s - %s: %s" % (
      status,
      purple.PurpleAccountGetUsername(acctID),
      purple.PurplePrimitiveGetIdFromType(
        purple.PurpleSavedstatusGetType(status)),
      purple.PurpleSavedstatusGetMessage(status) or None)

  #print 
  #print "Default: %d" % ( purple.PurpleSavedstatusGetDefault() )
  #all = purple.PurpleSavedstatusesGetAll()
  #print "All: ", all
  #avail = find_min(all)
  #print "Avail: ", avail

