#!/usr/bin/env python
import os
import gtk
from kiwi.ui import gadgets
from kiwi.ui.delegates import Delegate, SlaveDelegate
from kiwi.ui.objectlist import ObjectList, Column
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",
my_columns = [Column("title", sorted=True, tooltip="Title of article", width=50),
              Column("author", tooltip="Author of article"),
              Column("url", title="Address", visible=False,
                     tooltip="Address of article")]
class Shell(Delegate):
    widgets = ["ok", "cancel", "header", "footer", "title"]
    def __init__(self):
        Delegate.__init__(self, gladefile="news_shell",
        # paint header and footer; they are eventboxes that hold a
        # label and buttonbox respectively
        gadgets.set_background(self.header, "white")
        gadgets.set_background(self.footer, "#A0A0A0")
        gadgets.set_foreground(self.title, "blue")
        # Create the delegate and set it up
        objectlist = ObjectList(my_columns, news)
        objectlist.connect('selection-changed', self.news_selected)
        objectlist.connect('double-click', self.double_click)
        slave = SlaveDelegate(toplevel=objectlist)
        self.attach_slave("placeholder", slave)
        slave.focus_toplevel() # Must be done after attach
        self.slave = slave
    def news_selected(self, the_list, item):
        objectlist = self.slave.get_toplevel()
        print "%s %s %s\n" % (item.title, item.author, item.url)
    def double_click(self, the_list, selected_object):
        self.emit('result', selected_object.url)
    def on_ok__clicked(self, *args):
        objectlist = self.slave.get_toplevel()
        item = objectlist.get_selected()
        if item:
            self.emit('result', item.url)
    def on_cancel__clicked(self, *args):
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))