diff options
author | Rael <rael.gc@gmail.com> | 2015-06-18 09:12:46 -0300 |
---|---|---|
committer | Rael <rael.gc@gmail.com> | 2015-06-18 09:12:46 -0300 |
commit | 5a30d7434173888fb53e2cdb89b3f32db28aeb61 (patch) | |
tree | cb1c8fdfc229ead21a2cf1d09887181354ee3164 | |
parent | 13d7bc0dbb2b370ddf049477654dea5029546496 (diff) | |
download | scudcloud-5a30d7434173888fb53e2cdb89b3f32db28aeb61.zip scudcloud-5a30d7434173888fb53e2cdb89b3f32db28aeb61.tar.gz scudcloud-5a30d7434173888fb53e2cdb89b3f32db28aeb61.tar.bz2 |
Some code improvements
-rw-r--r-- | scudcloud-1.0/lib/leftpane.py | 10 | ||||
-rw-r--r-- | scudcloud-1.0/lib/resources.py | 16 | ||||
-rwxr-xr-x | scudcloud-1.0/lib/scudcloud.py | 36 | ||||
-rw-r--r-- | scudcloud-1.0/lib/systray.py | 3 | ||||
-rw-r--r-- | scudcloud-1.0/lib/wrapper.py | 60 | ||||
-rwxr-xr-x | scudcloud-1.0/scudcloud | 49 |
6 files changed, 80 insertions, 94 deletions
diff --git a/scudcloud-1.0/lib/leftpane.py b/scudcloud-1.0/lib/leftpane.py index 125325a..aeb1dfc 100644 --- a/scudcloud-1.0/lib/leftpane.py +++ b/scudcloud-1.0/lib/leftpane.py @@ -2,20 +2,20 @@ from PyQt4 import QtWebKit, QtGui, QtCore from PyQt4.Qt import QApplication, QKeySequence from PyQt4.QtCore import QBuffer, QByteArray, QUrl, SIGNAL from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings - -from resources import get_resource_path - +from resources import Resources class LeftPane(QWebView): def __init__(self, window): QWebView.__init__(self) self.window = window - with open(get_resource_path("leftpane.js"), "r") as f: + with open(Resources.get_path("leftpane.js"), "r") as f: self.js = f.read() self.setFixedWidth(0) self.setVisible(False) - self.setUrl(QUrl.fromLocalFile(get_resource_path("leftpane.html"))) + # We don't want plugins for this simple pane + self.settings().setAttribute(QWebSettings.PluginsEnabled, False) + self.setUrl(QUrl.fromLocalFile(Resources.get_path("leftpane.html"))) self.page().currentFrame().addToJavaScriptWindowObject("leftPane", self) self.page().currentFrame().evaluateJavaScript(self.js) diff --git a/scudcloud-1.0/lib/resources.py b/scudcloud-1.0/lib/resources.py index ece6632..36ca223 100644 --- a/scudcloud-1.0/lib/resources.py +++ b/scudcloud-1.0/lib/resources.py @@ -1,7 +1,15 @@ -import os +import os, re -INSTALL_DIR = '/opt/scudcloud' +class Resources: + APP_NAME = "ScudCloud Slack_SSB" + SIGNIN_URL = "https://slack.com/signin" + MAINPAGE_URL_RE = re.compile(r'^http[s]://[a-zA-Z0-9_\-]+.slack.com/?$') + MESSAGES_URL_RE = re.compile(r'^http[s]://[a-zA-Z0-9_\-]+.slack.com/messages/.*') + SSO_URL_RE = re.compile(r'^http[s]://[a-zA-Z0-9_\-]+.slack.com/sso/saml/start$') -def get_resource_path(filename): - return os.path.join(INSTALL_DIR, 'resources', filename) + # It's initialized in /scudcloud script + INSTALL_DIR = None + + def get_path(filename): + return os.path.join(Resources.INSTALL_DIR, 'resources', filename) diff --git a/scudcloud-1.0/lib/scudcloud.py b/scudcloud-1.0/lib/scudcloud.py index 8ece221..f2a7bd3 100755 --- a/scudcloud-1.0/lib/scudcloud.py +++ b/scudcloud-1.0/lib/scudcloud.py @@ -3,12 +3,14 @@ import sys, os from cookiejar import PersistentCookieJar from leftpane import LeftPane from notifier import Notifier +from resources import Resources from systray import Systray from wrapper import Wrapper from os.path import expanduser from PyQt4 import QtCore, QtGui, QtWebKit from PyQt4.Qt import QApplication, QKeySequence from PyQt4.QtCore import QUrl, QSettings +from PyQt4.QtWebKit import QWebSettings # Auto-detection of Unity and Dbusmenu in gi repository try: @@ -18,31 +20,25 @@ except ImportError: Dbusmenu = None from launcher import DummyLauncher -from resources import get_resource_path - class ScudCloud(QtGui.QMainWindow): - APP_NAME = "ScudCloud Slack_SSB" - SIGNIN_URL = "https://slack.com/signin" - STATUS_URL = "https://status.slack.com/" debug = False forceClose = False messages = 0 - def __init__(self, parent = None, settings_path = "~/.config/scudcloud"): + def __init__(self, parent = None, settings_path = ""): super(ScudCloud, self).__init__(parent) self.setWindowTitle('ScudCloud') self.settings_path = settings_path - self.notifier = Notifier(self.APP_NAME, get_resource_path('scudcloud.png')) + self.notifier = Notifier(Resources.APP_NAME, Resources.get_path('scudcloud.png')) self.settings = QSettings(self.settings_path + '/scudcloud.cfg', QSettings.IniFormat) self.identifier = self.settings.value("Domain") if Unity is not None: self.launcher = Unity.LauncherEntry.get_for_desktop_id("scudcloud.desktop") else: self.launcher = DummyLauncher(self) + self.webSettings() self.leftPane = LeftPane(self) - self.cookiesjar = PersistentCookieJar(self) - self.zoom = self.readZoom() webView = Wrapper(self) webView.page().networkAccessManager().setCookieJar(self.cookiesjar) self.stackedWidget = QtGui.QStackedWidget() @@ -60,11 +56,26 @@ class ScudCloud(QtGui.QMainWindow): self.systray(ScudCloud.minimized) self.installEventFilter(self) if self.identifier is None: - webView.load(QtCore.QUrl(self.SIGNIN_URL)) + webView.load(QtCore.QUrl(Resources.SIGNIN_URL)) else: webView.load(QtCore.QUrl(self.domain())) webView.show() + def webSettings(self): + self.cookiesjar = PersistentCookieJar(self) + self.zoom = self.readZoom() + # We don't want Java + QWebSettings.globalSettings().setAttribute(QWebSettings.JavaEnabled, False) + # We don't need History + QWebSettings.globalSettings().setAttribute(QWebSettings.PrivateBrowsingEnabled, True) + # Required for copy and paste clipboard integration + QWebSettings.globalSettings().setAttribute(QWebSettings.JavascriptCanAccessClipboard, True) + # Enabling Local Storage + QWebSettings.globalSettings().setAttribute(QWebSettings.LocalStorageEnabled, True) + QWebSettings.globalSettings().enablePersistentStorage(self.settings_path) + # Enabling Inspeclet only when --debug=True (requires more CPU usage) + QWebSettings.globalSettings().setAttribute(QWebSettings.DeveloperExtrasEnabled, self.debug) + def toogleFullScreen(self): if self.isFullScreen(): self.restore() @@ -218,6 +229,7 @@ class ScudCloud(QtGui.QMainWindow): self.leftPane.addTeam(t['id'], t['team_name'], t['team_url'], '', t == teams[0]) def switchTo(self, url): + qUrl = QtCore.QUrl(url) index = -1 for i in range(0, self.stackedWidget.count()): if self.stackedWidget.widget(i).url().toString().startswith(url): @@ -228,12 +240,14 @@ class ScudCloud(QtGui.QMainWindow): else: webView = Wrapper(self) webView.page().networkAccessManager().setCookieJar(self.cookiesjar) - webView.load(QtCore.QUrl(url)) + webView.load(qUrl) webView.show() self.stackedWidget.addWidget(webView) self.stackedWidget.setCurrentWidget(webView) self.quicklist(self.current().listChannels()) self.enableMenus(self.current().isConnected()) + # Save the last used team as default + self.window.settings.setValue("Domain", 'https://'+qUrl.host()) def eventFilter(self, obj, event): if event.type() == QtCore.QEvent.ActivationChange and self.isActiveWindow(): diff --git a/scudcloud-1.0/lib/systray.py b/scudcloud-1.0/lib/systray.py index 0108abe..1cc38bf 100644 --- a/scudcloud-1.0/lib/systray.py +++ b/scudcloud-1.0/lib/systray.py @@ -1,4 +1,5 @@ from PyQt4 import QtCore, QtGui +from resources import Resources class Systray(QtGui.QSystemTrayIcon): @@ -8,7 +9,7 @@ class Systray(QtGui.QSystemTrayIcon): super(Systray, self).__init__(QtGui.QIcon.fromTheme("scudcloud"), window) self.connect(self, QtCore.SIGNAL("activated(QSystemTrayIcon::ActivationReason)"), self.activatedEvent) self.window = window - self.setToolTip(self.window.APP_NAME) + self.setToolTip(Resources.APP_NAME) self.menu = QtGui.QMenu(self.window) self.menu.addAction('Show', self.restore) self.menu.addSeparator() diff --git a/scudcloud-1.0/lib/wrapper.py b/scudcloud-1.0/lib/wrapper.py index d4f25b4..18b2435 100644 --- a/scudcloud-1.0/lib/wrapper.py +++ b/scudcloud-1.0/lib/wrapper.py @@ -1,4 +1,4 @@ -import sys, subprocess, re, os +import sys, subprocess, os from PyQt4 import QtWebKit, QtGui, QtCore from PyQt4.Qt import QApplication, QKeySequence from PyQt4.QtCore import QBuffer, QByteArray, QUrl, SIGNAL @@ -6,38 +6,20 @@ from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings from PyQt4.QtNetwork import QNetworkProxy from urllib.parse import urlparse -from resources import get_resource_path +from resources import Resources class Wrapper(QWebView): - MAINPAGE_URL_RE = re.compile( - r'^http[s]://[a-zA-Z0-9_\-]+.slack.com/?$') - MESSAGES_URL_RE = re.compile( - r'^http[s]://[a-zA-Z0-9_\-]+.slack.com/messages/.*') - SSO_URL_RE = re.compile( - r'^http[s]://[a-zA-Z0-9_\-]+.slack.com/sso/saml/start$') - messages = 0 def __init__(self, window): self.configure_proxy() QWebView.__init__(self) self.window = window - with open(get_resource_path("scudcloud.js"), "r") as f: + with open(Resources.get_path("scudcloud.js"), "r") as f: self.js = f.read() # Required by Youtube videos (HTML5 video support only on Qt5) - QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True) - # We don't want Java - QWebSettings.globalSettings().setAttribute(QWebSettings.JavaEnabled, False) - # We don't need History - QWebSettings.globalSettings().setAttribute(QWebSettings.PrivateBrowsingEnabled, True) - # Required for copy and paste clipboard integration - QWebSettings.globalSettings().setAttribute(QWebSettings.JavascriptCanAccessClipboard, True) - # Local Storage - QWebSettings.globalSettings().setAttribute(QWebSettings.LocalStorageEnabled, True) - QWebSettings.globalSettings().enablePersistentStorage(self.window.settings_path) - # Enabling Inspeclet only when --debug=True (requires more CPU usage) - QWebSettings.globalSettings().setAttribute(QWebSettings.DeveloperExtrasEnabled, self.window.debug) + self.settings().setAttribute(QWebSettings.PluginsEnabled, True) self.setZoomFactor(self.window.zoom) self.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks) self.connect(self, SIGNAL("urlChanged(const QUrl&)"), self.urlChanged) @@ -75,27 +57,29 @@ class Wrapper(QWebView): return self.page().currentFrame().evaluateJavaScript("ScudCloud."+function+"("+arg+");") def urlChanged(self, qUrl): - self.settings().setUserStyleSheetUrl( - QUrl.fromLocalFile(get_resource_path("login.css"))) - self.page().currentFrame().addToJavaScriptWindowObject("desktop", self) - boot_data = self.page().currentFrame().evaluateJavaScript(self.js) - self.window.quicklist(boot_data['channels']) - self.window.teams(boot_data['teams']) - self.window.enableMenus(self.isConnected()) url = qUrl.toString() - # Never save the signin or status URLs - if self.window.SIGNIN_URL != url and self.window.STATUS_URL != url and url.endswith(".slack.com/"): - self.window.settings.setValue("Domain", 'https://'+qUrl.host()) + # URLs ending with /messages/general/ indicates a new team was loaded + if url.endswith("/messages/general/"): + self.settings().setUserStyleSheetUrl( + QUrl.fromLocalFile(Resources.get_path("login.css"))) + self.page().currentFrame().addToJavaScriptWindowObject("desktop", self) + boot_data = self.page().currentFrame().evaluateJavaScript(self.js) + self.window.quicklist(boot_data['channels']) + self.window.teams(boot_data['teams']) + self.window.enableMenus(self.isConnected()) + # Save the loading team as default + if url.endswith("/messages"): + self.window.settings.setValue("Domain", 'https://'+qUrl.host()) def linkClicked(self, qUrl): url = qUrl.toString() - should_open_link_in_scudcloud = ( - self.window.SIGNIN_URL == url or - self.MAINPAGE_URL_RE.match(url) or - self.MESSAGES_URL_RE.match(url) or - self.SSO_URL_RE.match(url) or + handle_link = ( + Resources.SIGNIN_URL == url or + Resources.MAINPAGE_URL_RE.match(url) or + Resources.MESSAGES_URL_RE.match(url) or + Resources.SSO_URL_RE.match(url) or url.startswith("https://accounts.google.com/o/oauth")) - if should_open_link_in_scudcloud: + if handle_link: self.load(qUrl) else: subprocess.call(('xdg-open', url)) diff --git a/scudcloud-1.0/scudcloud b/scudcloud-1.0/scudcloud index b83dcbd..ef4b8c7 100755 --- a/scudcloud-1.0/scudcloud +++ b/scudcloud-1.0/scudcloud @@ -4,73 +4,52 @@ import sys INSTALL_DIR = "/opt/scudcloud/" sys.path.append(INSTALL_DIR+'lib') from PyQt4 import QtGui +from resources import Resources from scudcloud import ScudCloud from qsingleapplication import QSingleApplication -from resources import get_resource_path # If the environment variable XDG_CONFIG_HOME is non-empty, CONFDIR is ignored # and the configuration directory will be $XDG_CONFIG_HOME/scudcloud instead. CONFDIR = '~/.config/scudcloud' - def main(): + Resources.INSTALL_DIR = INSTALL_DIR app = QSingleApplication(sys.argv) - app.setApplicationName(ScudCloud.APP_NAME) - app.setWindowIcon(QtGui.QIcon(get_resource_path('scudcloud.png'))) - + app.setApplicationName(Resources.APP_NAME) + app.setWindowIcon(QtGui.QIcon(Resources.get_path('scudcloud.png'))) args = parse_arguments() ScudCloud.debug = args.debug ScudCloud.minimized = True if args.minimized is True else None - - settings_path = load_settings(args.confdir) - + try: + settings_path = load_settings(args.confdir) + except: + print("Configuration directory "+args.confdir+" could not be created! Exiting...") + raise SystemExit() main = ScudCloud(settings_path=settings_path) app.singleStart(main, "scudcloud.pid") main.restore() sys.exit(app.exec_()) - -def ensure_config_dir_exists(confdir): - from errno import EEXIST - - try: +def load_settings(confdir): + if not os.path.isdir(confdir): os.makedirs(confdir) - except OSError as error: - if error.errno != EEXIST: - print("This configuration directory could not be created:") - print(confdir) - raise SystemExit() - if confdir not in sys.path: sys.path[0:0] = [confdir] - - -def load_settings(confdir): - ensure_config_dir_exists(confdir) return confdir - def parse_arguments(): from argparse import ArgumentParser from os.path import expanduser - if 'XDG_CONFIG_HOME' in os.environ and os.environ['XDG_CONFIG_HOME']: default_confdir = os.environ['XDG_CONFIG_HOME'] + '/scudcloud' else: default_confdir = CONFDIR - parser = ArgumentParser() - parser.add_argument('--confdir', dest='confdir', - metavar='dir', default=default_confdir, - help="change the configuration directory") - parser.add_argument('--debug', dest='debug', type=bool, default=False, - help="enable webkit debug console (default: False)") - parser.add_argument('--minimized', dest='minimized', type=bool, default=False, - help="start minimized to tray (default: False)") - + parser.add_argument('--confdir', dest='confdir', metavar='dir', default=default_confdir, help="change the configuration directory") + parser.add_argument('--debug', dest='debug', type=bool, default=False, help="enable webkit debug console (default: False)") + parser.add_argument('--minimized', dest='minimized', type=bool, default=False, help="start minimized to tray (default: False)") args = parser.parse_args() args.confdir = expanduser(args.confdir) - return args if __name__ == '__main__': |