Guest User

Untitled

a guest
Mar 11th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import threading;
  5. import urllib;
  6. import GoogleReader
  7. import pprint
  8. import time
  9. from PreferencesWindow import Ui_PreferencesDialog
  10. from PyQt4 import QtGui, QtCore
  11.  
  12. app = QtGui.QApplication(sys.argv)
  13.  
  14.  
  15. preferencesFile = "/home/harry/.grprefs"
  16. username = ""
  17. password = ""
  18. updateTimeMinutes = 2;
  19.  
  20.  
  21. keepGoing = True;
  22.  
  23. readIcon = QtGui.QIcon('reader64-off.png')
  24. unreadIcon = QtGui.QIcon('reader64.png')
  25.  
  26. class ReaderNotification(QtGui.QSystemTrayIcon):
  27. contextMenu = QtGui.QMenu()
  28. goToReaderItem = ""
  29. checkForUpdatesItem = ""
  30. quitItem = ""
  31. preferencesItem = ""
  32. preferencesWindow = ""
  33.  
  34. def __init__(self):
  35. QtGui.QSystemTrayIcon.__init__(self)
  36. self.setIcon(readIcon)
  37. self.preferencesWindow = PreferencesWindow(self)
  38.  
  39. #Build the context menu
  40. self.goToReaderItem = self.contextMenu.addAction("Go to &Reader")
  41. self.checkForUpdatesItem = self.contextMenu.addAction("&Check for updates");
  42. self.preferencesItem = self.contextMenu.addAction("&Preferences")
  43. self.quitItem = self.contextMenu.addAction("&Quit");
  44. self.setContextMenu(self.contextMenu)
  45. self.connect(self.quitItem, QtCore.SIGNAL('triggered()'),quit)
  46. self.connect(self.preferencesItem,QtCore.SIGNAL('triggered()'),self.preferencesWindow.show)
  47.  
  48. def setUnreadCounts(self, listOSubscriptions):
  49. #list is in the following format
  50. # {
  51. # categoryLabel:
  52. # {
  53. # ListTitle: No.Unread
  54. # }
  55. # }
  56. self.setToolTip("");
  57. count = 0;
  58. for cat in listOSubscriptions.keys():
  59. self.setToolTip(self.toolTip()+"\n"+cat);
  60. for list in listOSubscriptions[cat].keys():
  61. self.setToolTip(self.toolTip()+"\n\t"+list+"("+str(listOSubscriptions[cat][list])+")");
  62. count += listOSubscriptions[cat][list];
  63.  
  64. if count > 0:
  65. self.setIcon(unreadIcon)
  66. else:
  67. self.setIcon(readIcon)
  68.  
  69.  
  70. class CheckerThread(threading.Thread):
  71.  
  72. reader = GoogleReader.GoogleReader()
  73. def run(self):
  74. self.reader.identify(username,password)
  75. self.reader.login()
  76.  
  77. while(keepGoing == True):
  78. self.updateSubscriptions()
  79. time.sleep(updateTimeMinutes*60)
  80.  
  81. def updateSubscriptions(self):
  82. listOSubscriptions = self.reader.get_subscription_list()
  83. if listOSubscriptions == None :
  84. return None
  85.  
  86. los ={};
  87.  
  88. for s in listOSubscriptions[u'subscriptions']:
  89. los[s[u'id']] = { 'title':s[u'title'],'category':s[u'categories'][0][u'label'],'unread':0}
  90.  
  91. listOSubscriptions = self.reader.get_unread_count_list()
  92.  
  93.  
  94. for s in listOSubscriptions[u'unreadcounts']:
  95. try:
  96. los[s[u'id']]['unread'] = s[u'count'];
  97. except:
  98. pass
  99.  
  100. things = {}
  101.  
  102. for s in los:
  103. if los[s]['category'] not in things:
  104. things[los[s]['category']] = {}
  105. things[los[s]['category']] [los[s]['title']] = los[s]['unread'];
  106.  
  107. trayIcon.setUnreadCounts(things)
  108.  
  109. class Preferences:
  110. filename = ""
  111. def __init__(self, filename):
  112. print "Preferences are located in "+filename
  113. self.filename = filename
  114.  
  115. def read(self):
  116. try:
  117. file = open(self.filename)
  118. content = file.read()
  119. lines = content.splitlines()
  120.  
  121. for line in lines:
  122. bits = lines.split();
  123. if(bits[0] == "username"):
  124. username = bits[1]
  125. elif(bits[0] == "password"):
  126. password = bits[1]
  127. elif(bits[0] == "period"):
  128. updateTimeMinutes = bits[1]
  129. file.close()
  130. except:
  131. pass
  132.  
  133. def save(self):
  134. file = open(self.filename,"w")
  135. file.write("username\t"+username+"\n")
  136. file.write("password\t"+password+"\n")
  137. file.write("period\t"+str(updateTimeMinutes)+"\n")
  138. file.close()
  139.  
  140. class PreferencesWindow(QtGui.QDialog):
  141. readerCheckerInstance = ""
  142. def __init__(self,rcInstance):
  143. QtGui.QDialog.__init__(self,None,QtCore.Qt.WindowTitleHint| QtCore.Qt.CustomizeWindowHint)
  144. self.ui = Ui_PreferencesDialog()
  145. self.ui.setupUi(self);
  146.  
  147. def accept(self):
  148. global username,password,updateTimeMinutes
  149. username = self.ui.UsernameBox.text()
  150. password = self.ui.PasswordBox.text()
  151. updateTimeMinutes = self.ui.UpdateTimeMinutes.value()
  152. preferences.save()
  153. self.hide()
  154.  
  155. def reject(self):
  156. self.hide();
  157.  
  158. def populateValues(self):
  159. self.ui.UsernameBox.setText(username);
  160. self.ui.PasswordBox.setText(password);
  161. self.ui.UpdateTimeMinutes.setValue(updateTimeMinutes);
  162.  
  163. def closeEvent(self,event):
  164. self.hide()
  165. event.ignore()
  166.  
  167. def showEvent(self,event):
  168. self.populateValues()
  169.  
  170.  
  171. preferences = Preferences(preferencesFile)
  172. preferences.read()
  173.  
  174. trayIcon = ReaderNotification()
  175. trayIcon.show()
  176.  
  177. thread = CheckerThread();
  178. thread.start()
  179.  
  180. sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment