Source

pida-hacks / pida-plugins / trac / trac.py

# -*- coding: utf-8 -*- 

# Copyright (c) 2007 The PIDA Project

from urlparse import urljoin

import gtk, webkit

from pygtkhelpers.ui.objectlist import Column

# PIDA Imports
from pida.core.service import Service
from pida.core.features import FeaturesConfig
from pida.core.actions import ActionsConfig
from pida.ui.actions import PidaRememberToggle
from pida.ui.views import PidaView
from pida.utils.web import fetch_url
from .feedparser import parse

# locale
from pida.core.locale import Locale
locale = Locale('trac')
_ = locale.gettext

class TracView(PidaView):

    key = 'trac.browser'

    builder_file = 'trac_browser'
    locale = locale
    icon_name = 'trac_logo'
    label_text = _('Trac')

    def create_ui(self):
        self.tickets_list.set_columns([
            Column('ticket', sorted=True, type=int),
            Column('summary'),
        ])
        self.tickets_list.connect('item-double-clicked', self.on_tickets_list__item_double_clicked)
        self.set_base_address('http://pida.co.uk/trac/')
        self.item_text = webkit.WebView()
        self.item_text_holder.add(self.item_text)
        self.item_text.show()

    def set_base_address(self, address):
        self._address = address
        self.address_entry.set_text(address)

    def get_base_address(self):
        return self.address_entry.get_text()

    def get_auth_data(self):
        if self.toggle_auth.get_active():
            return (self.user_entry.get_text(), self.password_entry.get_text())

    def on_connect_button__clicked(self, button):
        trac_report(self.get_base_address(), 1, self.report_received,
                    self.get_auth_data())

    def on_tickets_list__selection_changed(self, ol):
        item = ol.selected_item
        self.item_text.load_html_string(item.description.strip(), self._address)

    def on_tickets_list__item_double_clicked(self, ot, item, event):
        self.svc.browse(url=item.link)

    def on_toggle_auth__toggled(self, btn):
        self.auth_box.set_sensitive(btn.get_active())

    def report_received(self, url, data):
        self.tickets_list.clear()
        if url:
            # no error
            for item in parse_report(data):
                self.tickets_list.append(item)
        else:
            # an error occured
            self.svc.boss.cmd('notify', 'notify', title=data,
                data=_('An error occured when fetching trac tickets.'))


    def can_be_closed(self):
        self.svc.get_action('show_trac').set_active(False)


class ReportItem(object):
    def __init__(self, entry):
        ticket, summary = entry['title'].split(':', 1)
        self.ticket = int(ticket.strip('#').strip())
        self.summary = summary.strip()
        self.description = entry['description']
        self.link = entry['link']


def parse_report(data):
    feed = parse(data)
    for entry in feed.entries:
        yield ReportItem(entry)


def trac_report(base_address, report_id, callback, auth):
    action_fragment = 'report/%s?format=rss' % report_id
    action_url = urljoin(base_address, action_fragment)
    fetch_url(action_url, callback, auth=auth)


class TracActions(ActionsConfig):


    actions = [
        PidaRememberToggle('show_trac',
            _('Trac Viewer'),
            _('Show the Trac Viewer'),
            gtk.STOCK_INFO),
    ]

    accels = {
        'show_trac': '<Shift><Control>j'
    }

    global_accels = {
    }

    def on_show_trac(self, action):
        if action.get_active():
            self.svc.show_trac()
        else:
            self.svc.hide_trac()


# Service class
class Trac(Service):
    """Describe your Service Here"""

    actions_config = TracActions

    def pre_start(self):
        self._view = TracView(self)

    def show_trac(self):
        self.boss.cmd('window', 'add_view', paned='Plugin', view=self._view)

    def hide_trac(self):
        self.boss.cmd('window', 'remove_view', view=self._view)

    def ensure_view_visible(self):
        action = self.get_action('show_trac')
        if not action.get_active():
            action.set_active(True)
        self.boss.cmd('window', 'presnet_view', view=self._view)

    def stop(self):
        if self.get_action('show_trac').get_active():
            self.hide_trac()

    def browse(self, url):
        self.boss.cmd('browseweb', 'browse', url=url)

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: