Quick bug opening with application indicators

I deal a lot with launchpad bugs. Some time ago I coded a simplistic ~/bin/bugopen:

#!/bin/sh
if [ "x$1" = "x" ]; then
	echo "Usage: $0 "
	exit 1
fi
xdg-open https://launchpad.net/bugs/$1

But I find that’s still too much work – highlighting the bug id, finding or opening a new terminal, and typing ‘bugopen ‘. Too slow.

So I decided an appindicator would be perfect. I started with the canonical python example by Jono Bacon. Then added a class which monitors the clipboard for changes – two, actually, as I need one for primary and one for clipboard selection. When clipboard changes, if it is all digits, I add the new entry to the indicator. Then I can just click on a number and it’ll open the corresponding launchpad bug in my browser of choice.

#!/usr/bin/python

#
# This program creates an indicator showing the latest primary and
# clipboard selections, and allows you to run the 'bugopen' program
# on them.  This obviously should be made more generic, but I'm going
# to use it as is a bit until I think of other uses, then start making
# it more generic to support those.
#
# Copyright 2009-2011 Canonical Ltd.
#
# Authors: Neil Jagdish Patel 
#          Jono Bacon 
#          Serge Hallyn 
#
# This program is free software: you can redistribute it and/or modify it 
# under the terms of either or both of the following licenses:
#
# 1) the GNU Lesser General Public License version 3, as published by the 
# Free Software Foundation; and/or
# 2) the GNU Lesser General Public License version 2.1, as published by 
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the applicable version of the GNU Lesser General Public 
# License for more details.
#
# You should have received a copy of both the GNU Lesser General Public 
# License version 3 and version 2.1 along with this program.  If not, see 
# 
#

import gobject
import gtk
import appindicator
import os
import commands

# for now I'm only interested in valid launchpad bugs
def validate_text(buf):
  bug=buf
  if buf[0] == '#':
    bug=buf[1:]
  for i in range(len(bug)):
    if not buf[i].isdigit():
      return False
  return True

def bugopen(w, buf):
  bug=buf
  if buf[0] == '#':
    bug=buf[1:]
  if not validate_text(bug):
    return
  os.system('xdg-open https://launchpad.net/bugs/%d' % (int(bug)))

def quit(widget, data=None):
  gtk.main_quit()

def popupmenu(clipboard, menu):
  buf = clipboard.wait_for_text()
  if not validate_text(buf):
	  return
  menu_items = gtk.MenuItem(buf)
  menu.prepend(menu_items)
  menu_items.connect("activate", bugopen, buf)
  menu_items.show()

  #if len(menu.items) > 10:
  #  del(men.items[10])

class ClipboardMonitor (object):
  def clipboard_changed(self, clipboard, event):
    popupmenu(clipboard, self.menu)

  def __init__(self, cliptype, menu):
    clip = gtk.clipboard_get(cliptype)
    clip.connect("owner-change", self.clipboard_changed)
    self.menu = menu

if __name__ == "__main__":
  ind = appindicator.Indicator ("bugopen",
                              "debian-swirl",
                              appindicator.CATEGORY_COMMUNICATIONS)
  ind.set_status (appindicator.STATUS_ACTIVE)
  ind.set_attention_icon ("indicator-messages-new")

  # create a menu
  menu = gtk.Menu()

  image = gtk.ImageMenuItem(gtk.STOCK_QUIT)
  image.connect("activate", quit)
  image.show()
  menu.append(image)

  ind.set_menu(menu)

  # Create two ClipboardMonitors.
  monitor1 = ClipboardMonitor(gtk.gdk.SELECTION_CLIPBOARD, menu)
  monitor2 = ClipboardMonitor(gtk.gdk.SELECTION_PRIMARY, menu)
  # popuplate with existing entries

  gtk.main()
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s