Commits

thijsdezoete committed a75c0ba

Working with a more decent interface

Comments (0)

Files changed (1)

 class SetupTrayWindow(QtGui.QDialog):
     def __init__(self, parent=None):
         super(SetupTrayWindow, self).__init__()
-        self.url = QtGui.QLineEdit("www.google.com")
+        self.url = QtGui.QLineEdit("www.last.fm")
         self.parent = parent
         self.url.selectAll()
-        QtCore.QObject.connect(self.url, QtCore.SIGNAL('returnPressed()'), self.go_get_favicon)
-        QtCore.QObject.connect(self, QtCore.SIGNAL('aboutToShow()'), self.url.setFocus)
+        QtCore.QObject.connect(self.url, QtCore.SIGNAL('returnPressed()'), self.get_favicon)
+        QtCore.QObject.connect(self, QtCore.SIGNAL('focusIn()'), self.url.setFocus)
         self.layout = QtGui.QGridLayout()
         self.layout.addWidget(QtGui.QLabel("Url for the url"), 0, 0)
         self.layout.addWidget(self.url, 1, 1)
         self.url.setFocus()
         self.setWindowTitle('Setup New Tray Icon')
 
-    def go_get_favicon(self):
+    def read_icon_into_qicon(self, icon_file):
+        dataBuffer = QtCore.QByteArray(icon_file)
+        new_icon = QtGui.QImage(dataBuffer)
+        pm = QtGui.QPixmap(32, 32)
+        pm.loadFromData(dataBuffer)
+        self._icon_pixmap = pm
+        self.icon = QtGui.QIcon(pm)
+
+        return self.icon
+
+    def get_favicon(self):
+        url = str(self.url.text())
+        _file = os.path.abspath(os.path.dirname(__file__))
+        print _file
+        icon_file = 'favicon-%s.ico' % url
+        files = os.listdir(_file)
+
+        if not icon_file in files:
+            print 'File not in local-cache'
+            icon = self.download_favicon()
+        else:
+            _loc = _file + '/' + files[files.index(icon_file)]
+            print _loc
+            icon_data = open(_loc, 'rb').read()
+            icon = self.read_icon_into_qicon(icon_data)
+
+        self.parent.add_icon(icon, url)
+        lbl = QtGui.QLabel("placeholder")
+
+        lbl.setPixmap(self._icon_pixmap)
+        self.layout.addWidget(
+            lbl, 1, 0
+        )
+
+        self.parent.add_icon_to_menu(url, 'Change tray to this image', lambda: self.hello(url))
+
+    def hello(self, icon_name):
+        print 'Hello world!'
+        self.parent.change_top_icon(icon_name)
+
+    def download_favicon(self):
         import requests
-        # import urllib
         url = str(self.url.text())
-        # from StringIO import StringIO
+
         if not 'http' in url:
             url = 'http://' + url
         print 'Get favicon from %s' % url
-        icon_url = "http://www.google.com/s2/favicons?domain="
+        #icon_url = "http://www.google.com/s2/favicons?domain="
+
         icon_url = "http://g.etfv.co/"
-        #icon = requests.get("http://g.etfv.co/%s" % self.url.text(), stream=True)
-        icon = requests.get(icon_url + url, stream=True)
-        # test = StringIO(icon.content)
-        test = icon.raw.read()
-        bla = QtGui.QtImage.from_data(test)
-        pm = QtGui.QPixmap.fromImage(bla)
-        icn = QtGui.QIcon(pm)
-        #print test
-        # test = urllib.urlretrieve(str(icon_url + self.url.text()))
-        # test = test[0]
-        # test = urllib.urlretrieve(str(self.url.text()))
-        # from PyQt4.QtWebKit import QWebSettings
-        # test = QWebSettings.iconForUrl(QtCore.QUrl(str(icon_url + self.url.text())))
-        # print test
-        # print test.isNull()
-        # test = icon.raw.read()
-        #icn = QtGui.QIcon(test)
-        print 'icn:'
-        print icn
-        print icn.availableSizes()
-        print icn.isNull()
-        print 'icn:'
-        self.icon = icn
-        self.parent.add_icon(icn, str(self.url.text()))
-        print self.parent.icons
-        # pprint.pprint(dir(icn.pixmap(32)))
-        lbl = QtGui.QLabel("test")
-        print "Setting icon..."
-        print icn.pixmap(32)
-        #icn.show()
-        lbl.setPixmap(icn.pixmap(32))
-        self.layout.addWidget(
-            lbl, 1, 0
-        )
+        icon = requests.get(icon_url + url)
 
-    def get_icon(self):
-        return self.icon
+        _file = os.path.abspath(os.path.dirname(__file__))
+        icon_file = _file + '/favicon-%s.ico' % str(self.url.text())
+        with open(icon_file, 'wb') as new_icon:
+            print 'Writing to file %s' % (icon_file)
+            new_icon.write(icon.content)
 
+        return self.read_icon_into_qicon(icon.content)
 
+    def get_icon(self):
+        return self.icon
 
 
 class MyWindow(QtGui.QDialog):    # any super class is okay
         child.show()
 
 
-class Indicatr(QtGui.QSystemTrayIcon):
+class Indicatr(object):
     def __init__(self):
         self.app = QtGui.QApplication([])
         self.app.setApplicationName("TrAyPI")
         self.new_tray.raise_()
         self.new_tray.activateWindow()
         self.app.setActiveWindow(self.new_tray)
+        self.app.restoreOverrideCursor()
         self.new_tray.show()
 
     def add_quit_btn(self):
+        # self.menu.addAction('Initialise API', self.init_api)
         self.menu.addAction('New Tray App', self._show_tray_add)
         self.menu.addAction('Quit', self._quit)
 
     def _quit(self):
+        for icon in self.icons:
+            print 'Should remove %s' % icon
         QtGui.QApplication.quit()
 
     def run(self):
     #         # Don't do anything if we can't remove dock icon...
     #         print 'Cant remove icon from dock. Install pyobjc to fix this'
     #         pass
+
 if __name__ == '__main__':
     x = Indicatr()
     x.add_icon('apple.png')
     x.change_top_icon('apple')
     x.add_quit_btn()
-    x._show_tray_add()
+    #x._show_tray_add()
     x.run()