from gettext import gettext as _

import gtk
import deskbar
from deskbar.Handler import Handler
from deskbar.Match import Match

import gnomevfs

import re

HANDLERS = {
        "MySQLBugHandler" : {
                "name": _("MySQL Bugs"),
                "description": _("goes to the web page for a MySQL bug number"),
        }
}

BUGEXPR = re.compile(r"^((?P<cmd>e(dit)?)\s*)?BUG\#?\s*(?P<bugno>\d+)", re.IGNORECASE)

class MySQLBugMatch(Match):
        def __init__(self, backend, bugno, cmd , **args):
                deskbar.Match.Match.__init__(self, backend, **args)
		self.bugno= bugno
		self.cmd= cmd
                
        def action(self, text=None):
		if self.cmd=='e' or self.cmd=='edit':
			gnomevfs.url_show("http://bugs.mysql.com/bug.php?id=" + self.bugno + "&edit=1")
		else:
			gnomevfs.url_show("http://bugs.mysql.com/bug.php?id=" + self.bugno)

        def get_category(self):
                return "actions"

	def get_name(self, text=None):
		return {"bugno":self.bugno}

        def get_verb(self):
		if self.cmd=='e' or self.cmd=='edit':
	                return _("Edit MySQL Bug <b>%(bugno)s</b>")
		else:
	                return _("Open MySQL Bug <b>%(bugno)s</b>")
        
        def get_hash(self, text=None):
                return self.bugno

class MySQLBugHandler(Handler):
        def __init__(self):
                deskbar.Handler.Handler.__init__(self, "mysql_bugs")
                

        def query(self, query, max=1):
		match = BUGEXPR.match(query)
		if match != None:
			return [MySQLBugMatch(self,
					(match.group('bugno')),
					(match.group('cmd'))
				)]
		return []
