示例#1
0
class FeedWatchWidget(QWidget):
    def __init__(self):
        super(FeedWatchWidget, self).__init__()

        self.feedList = AmuleFeedWatcher()
        self.eventList = EventServer.events

        # Control buttons: check now, add feed, remove feed
        checkNowButton = QPushButton('Check feeds now!')
        self.connect(checkNowButton, SIGNAL('clicked()'),
                     self.checkNow)

        addFeedButton = QPushButton('Add feed...')
        self.connect(addFeedButton, SIGNAL('clicked()'),
                     self.addFeed)

        removeFeedButton = QPushButton('Remove feed...')
        self.connect(removeFeedButton, SIGNAL('clicked()'),
                     self.removeFeed)

        buttons = QHBoxLayout()
        buttons.addWidget(addFeedButton)
        buttons.addWidget(removeFeedButton)
        buttons.addStretch()
        buttons.addWidget(checkNowButton)

        # main layout
        layout = QVBoxLayout()
        self.feedView = QListView()
        self.feedView.setModel(self.feedList)
        layout.addWidget(self.feedView)

        self.connect(self.feedView, SIGNAL('doubleClicked(const QModelIndex&)'),
                     self.feedEdit)

        layout.addLayout(buttons)

        self.eventView = QListView()
        self.eventView.setSelectionMode(QAbstractItemView.NoSelection)
        self.eventView.setModel(self.eventList)
        layout.addWidget(self.eventView)

        self.setLayout(layout)

        # check for feeds every 2 hours
        self.timer = QTimer(self)
        self.connect(self.timer, SIGNAL('timeout()'),
                     self.checkNow)
        self.timer.start(2*60*60*1000)

    def feedEdit(self, index):
        fullFeed = self.feedList.getFullFeedIndex(index.row())
        epSelect = EpisodeSelector(fullFeed)
        if epSelect.exec_() == QDialog.Accepted:
            self.feedList.setLastUpdateIndex(index.row(), epSelect.lastUpdate)

    def checkNow(self):
        # this can last a while, do not block the UI meanwhile
        CheckThread(self, self.feedList).start()

    def addFeed(self):
        result, ok = QInputDialog.getText(self, 'Add feed...', 'Enter the feed url:', QLineEdit.Normal, '')
        if ok and not result.isEmpty():
            try:
                self.feedList.addFeed(result)
            except Exception, e:
                QMessageBox.critical(self, 'Add feed...', 'Error: %s' % str(e))