#!/usr/bin/env python
import os
import gtk
from kiwi.ui.delegates import GladeDelegate, SlaveDelegate
from kiwi.ui.gadgets import quit_if_last, set_background, set_foreground
from kiwi.ui.objectlist import Column, ObjectList
class NewsItem:
    def __init__(self, title, author, url):
        self.title, self.author, self.url = title, author, url
# Friendly Pigdog.org news
news = [
 NewsItem("Smallpox Vaccinations for EVERYONE", "JRoyale",
 NewsItem("Is that uranium in your pocket or are you just happy to see me?",
          "Baron Earl",
 NewsItem("Cut 'n Paste", "Baron Earl",
 NewsItem("A Slippery Exit", "Reverend CyberSatan",
 NewsItem("Those Crazy Dutch Have Resurrected Elvis", "Miss Conduct",
class ListSlave(SlaveDelegate):
    def __init__(self, parent):
        self.parent = parent
        self.news_list = ObjectList([
                   Column('title', 'Title of article', str),
                   Column('author', 'Author of article', str),
                   Column('url', 'Address of article', str),
        SlaveDelegate.__init__(self, toplevel=self.news_list)
    def on_news_list__selection_changed(self, list, item):
        print "%s %s %s\n" % (item.title, item.author, item.url)
    def on_news_list__double_click(self, the_list, selected_object):
class Shell(GladeDelegate):
    def __init__(self):
        keyactions = {
            gtk.keysyms.a: self.on_ok__clicked,
            gtk.keysyms.b: self.on_cancel__clicked,
        GladeDelegate.__init__(self, gladefile="news_shell",
                          delete_handler=quit_if_last, keyactions=keyactions)
        # paint header and footer; they are eventboxes that hold a
        # label and buttonbox respectively
        set_background(self.header, "white")
        set_background(self.footer, "#A0A0A0")
        set_foreground(self.title,  "blue")
        self.slave = ListSlave(self)
        self.attach_slave("placeholder", self.slave)
        self.slave.focus_toplevel() # Must be done after attach
    def on_ok__clicked(self, button):
        item = self.slave.news_list.get_selected()
        self.emit('result', item.url)
    def on_cancel__clicked(self, button):
url = None
shell = Shell()
def get_url(view, result):
    global url
    url = result
shell.connect('result', get_url)
if url is not None:
    # Try to run BROWSER (or lynx) on the URL returned
    browser = os.environ.get("BROWSER", "lynx")
    os.system("%s %s" % (browser, url))