view MainWindow.py @ 83:19394162a39d

Automated merge with ssh://xanthippe.dyndns.org//home/dirk/Projekte/Python/Feedworm
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 22 Aug 2010 16:35:08 +0200
parents 2dd6e6894772
children bacf42da9e36
line wrap: on
line source


from AddFeed import AddFeed
from DisplayModel import DisplayModel
from Feed import Feed
from FeedEntry import FeedEntry
from FeedEntryItemDelegate import FeedEntryItemDelegate
from FeedSettings import FeedSettings
from Preferences import Preferences
from PreferencesDialog import PreferencesDialog
from PyQt4 import QtGui
from PyQt4.QtCore import QUrl
from Ui_MainWindow import Ui_MainWindow
import FeedList
import subprocess

STATUS_MESSAGE_DISPLAY_MILLIS = 20000

class MainWindow(QtGui.QMainWindow):
    def __init__(self, session=None):
        QtGui.QWidget.__init__(self, None)
        self.session = session
        self.preferences = Preferences(session)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.updateFeedList()
        self.initFeedEntryList()
        
    def updateFeedList(self):
        self.allFeeds = FeedList.getFeeds(self.session)
        feedModel = DisplayModel(self, self.allFeeds, Feed.userPresentableString)
        self.ui.feedList.setModel(feedModel)
        self.ui.feedList.update()

    def initFeedEntryList(self):
        self.ui.feedEntryList.setItemDelegate(FeedEntryItemDelegate())

    def feedSelected(self, index):
        self.selectedFeed = self.allFeeds[index.row()]
        self.enableFeedRelatedWidgets()
        self.setupFeedEntries()

    def enableFeedRelatedWidgets(self):
        self.ui.actionFeedSettings.setEnabled(True)
        self.ui.actionMarkFeedRead.setEnabled(True)

    def setupFeedEntries(self):
        hideReadEntries = self.preferences.hideReadFeedEntries()
        self.feedEntries = self.selectedFeed.entriesSortedByUpdateDate(hideReadEntries)
        self.initFeedDisplayModel()
        
    def initFeedDisplayModel(self):
        model = DisplayModel(self, self.feedEntries, FeedEntry.userPresentableString)
        self.ui.feedEntryList.setModel(model)
        self.ui.feedEntryList.itemDelegate().entries = self.feedEntries
        self.ui.feedEntryList.update()
     
    def feedEntrySelected(self, index):
        self.ui.menuArticle.setEnabled(True)
        
        row = index.row()
        self.selectedEntry = self.feedEntries[row]
        self.selectedEntry.markRead()
        
        if self.selectedEntry.feed.auto_load_entry_link:
            self.openLinkFromSelectedEntry()
        else:
            self.openSummaryFromSelectedEntry()
            
    def openSummaryFromSelectedEntry(self):
        # TODO this is the wrong base url, figure out the correct one
        baseUrl = QUrl(self.selectedEntry.link)
        self.ui.webView.setHtml(self.selectedEntry.summary, baseUrl)

    def openLinkFromSelectedEntry(self):
        if self.selectedEntry.feed.always_open_in_browser:
            self.openSelectedEntryInBrowser()
        else:
            self.openSelectedEntryInWebView()
            
    def openSelectedEntryInWebView(self):
        message = "Open %s ..." % (self.selectedEntry.title)
        self._updateStatusBar(message)
        url = QUrl(self.selectedEntry.link)
        self.ui.webView.load(url)
        self.ui.webView.show()

    def openSelectedEntryInBrowser(self):
        message = "Open %s in external browser", (self.selectedEntry.title)
        self._updateStatusBar(message)
        # TODO make browser configurable
        browser = "/usr/local/bin/opera"
        subprocess.Popen([browser, self.selectedEntry.link])

    def toggleReadOnSelectedEntry(self):
        self.selectedEntry.toggleRead()
        self.session.commit()
        self.ui.feedEntryList.doItemsLayout()
        
    def markSelectedFeedRead(self):
        self.selectedFeed.markAllEntriesRead()
        self.session.commit()
        self.ui.feedEntryList.doItemsLayout()
    
    def addFeed(self):
        addFeed = AddFeed(self.session)
        success = addFeed.exec_()
        if not success:
            return
        
        if addFeed.exception is not None:
            message = "Error while adding feed: " + str(addFeed.exception)
            self._updateStatusBar(message)
        else:
            self.session.commit()
            self.updateFeedList()
                
    def showPreferences(self):
        preferences = PreferencesDialog(self.session)
        preferences.exec_()
        
    def showFeedSettings(self):
        feedSettings = FeedSettings(self.session, self.selectedFeed)
        feedSettings.exec_()
        
    def _updateStatusBar(self, message):
        self.ui.statusbar.showMessage(message, STATUS_MESSAGE_DISPLAY_MILLIS)