Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- import threading;
- import urllib;
- import GoogleReader
- import pprint
- import time
- from PreferencesWindow import Ui_PreferencesDialog
- from PyQt4 import QtGui, QtCore
- app = QtGui.QApplication(sys.argv)
- preferencesFile = "/home/harry/.grprefs"
- username = ""
- password = ""
- updateTimeMinutes = 2;
- keepGoing = True;
- readIcon = QtGui.QIcon('reader64-off.png')
- unreadIcon = QtGui.QIcon('reader64.png')
- class ReaderNotification(QtGui.QSystemTrayIcon):
- contextMenu = QtGui.QMenu()
- goToReaderItem = ""
- checkForUpdatesItem = ""
- quitItem = ""
- preferencesItem = ""
- preferencesWindow = ""
- def __init__(self):
- QtGui.QSystemTrayIcon.__init__(self)
- self.setIcon(readIcon)
- self.preferencesWindow = PreferencesWindow(self)
- #Build the context menu
- self.goToReaderItem = self.contextMenu.addAction("Go to &Reader")
- self.checkForUpdatesItem = self.contextMenu.addAction("&Check for updates");
- self.preferencesItem = self.contextMenu.addAction("&Preferences")
- self.quitItem = self.contextMenu.addAction("&Quit");
- self.setContextMenu(self.contextMenu)
- self.connect(self.quitItem, QtCore.SIGNAL('triggered()'),quit)
- self.connect(self.preferencesItem,QtCore.SIGNAL('triggered()'),self.preferencesWindow.show)
- def setUnreadCounts(self, listOSubscriptions):
- #list is in the following format
- # {
- # categoryLabel:
- # {
- # ListTitle: No.Unread
- # }
- # }
- self.setToolTip("");
- count = 0;
- for cat in listOSubscriptions.keys():
- self.setToolTip(self.toolTip()+"\n"+cat);
- for list in listOSubscriptions[cat].keys():
- self.setToolTip(self.toolTip()+"\n\t"+list+"("+str(listOSubscriptions[cat][list])+")");
- count += listOSubscriptions[cat][list];
- if count > 0:
- self.setIcon(unreadIcon)
- else:
- self.setIcon(readIcon)
- class CheckerThread(threading.Thread):
- reader = GoogleReader.GoogleReader()
- def run(self):
- self.reader.identify(username,password)
- self.reader.login()
- while(keepGoing == True):
- self.updateSubscriptions()
- time.sleep(updateTimeMinutes*60)
- def updateSubscriptions(self):
- listOSubscriptions = self.reader.get_subscription_list()
- if listOSubscriptions == None :
- return None
- los ={};
- for s in listOSubscriptions[u'subscriptions']:
- los[s[u'id']] = { 'title':s[u'title'],'category':s[u'categories'][0][u'label'],'unread':0}
- listOSubscriptions = self.reader.get_unread_count_list()
- for s in listOSubscriptions[u'unreadcounts']:
- try:
- los[s[u'id']]['unread'] = s[u'count'];
- except:
- pass
- things = {}
- for s in los:
- if los[s]['category'] not in things:
- things[los[s]['category']] = {}
- things[los[s]['category']] [los[s]['title']] = los[s]['unread'];
- trayIcon.setUnreadCounts(things)
- class Preferences:
- filename = ""
- def __init__(self, filename):
- print "Preferences are located in "+filename
- self.filename = filename
- def read(self):
- try:
- file = open(self.filename)
- content = file.read()
- lines = content.splitlines()
- for line in lines:
- bits = lines.split();
- if(bits[0] == "username"):
- username = bits[1]
- elif(bits[0] == "password"):
- password = bits[1]
- elif(bits[0] == "period"):
- updateTimeMinutes = bits[1]
- file.close()
- except:
- pass
- def save(self):
- file = open(self.filename,"w")
- file.write("username\t"+username+"\n")
- file.write("password\t"+password+"\n")
- file.write("period\t"+str(updateTimeMinutes)+"\n")
- file.close()
- class PreferencesWindow(QtGui.QDialog):
- readerCheckerInstance = ""
- def __init__(self,rcInstance):
- QtGui.QDialog.__init__(self,None,QtCore.Qt.WindowTitleHint| QtCore.Qt.CustomizeWindowHint)
- self.ui = Ui_PreferencesDialog()
- self.ui.setupUi(self);
- def accept(self):
- global username,password,updateTimeMinutes
- username = self.ui.UsernameBox.text()
- password = self.ui.PasswordBox.text()
- updateTimeMinutes = self.ui.UpdateTimeMinutes.value()
- preferences.save()
- self.hide()
- def reject(self):
- self.hide();
- def populateValues(self):
- self.ui.UsernameBox.setText(username);
- self.ui.PasswordBox.setText(password);
- self.ui.UpdateTimeMinutes.setValue(updateTimeMinutes);
- def closeEvent(self,event):
- self.hide()
- event.ignore()
- def showEvent(self,event):
- self.populateValues()
- preferences = Preferences(preferencesFile)
- preferences.read()
- trayIcon = ReaderNotification()
- trayIcon.show()
- thread = CheckerThread();
- thread.start()
- sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment