d8c33a1f63f604ac47b3aff2487c98cc0a24d3d2
[stack/code/dboxswitch.git] / gui.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 Dboxswitch dropbox profile switcher
6
7 license: Modified BSD License
8
9 Copyright (c) 2012,  <stack@inventati.org>
10 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14     * Redistributions of source code must retain the above copyright
15       notice, this list of conditions and the following disclaimer.
16     * Redistributions in binary form must reproduce the above copyright
17       notice, this list of conditions and the following disclaimer in the
18       documentation and/or other materials provided with the distribution.
19     * Neither the name of the <organization> nor the
20       names of its contributors may be used to endorse or promote products
21       derived from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
27 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 """
35 import sys
36 import os
37 from PyQt4 import QtGui
38 from PyQt4.QtCore import SIGNAL
39
40 from apperror import AppError
41 from settings import appconf
42
43 class Gui(QtGui.QDialog):
44     def __init__(self, prManager):
45         self.app = QtGui.QApplication(sys.argv)
46     
47         #check if system tray is avaiable on the system
48         if not QtGui.QSystemTrayIcon.isSystemTrayAvailable():
49             QtGui.QMessageBox.critical(None, "Systray",
50                     "I couldn't detect any system tray on this system.")
51             sys.exit(1)
52         
53         QtGui.QApplication.setQuitOnLastWindowClosed(False)
54     
55         super(Gui, self).__init__()
56
57         self.createActions()
58         self.createTrayIcon()
59
60         self.trayIcon.show()
61
62         self.setWindowTitle("Profile manager / Dboxswitch - dropbox profile switcher")
63         self.resize(200, 200)
64
65         self.profileManager = prManager
66
67     def main(self): 
68         sys.exit(self.app.exec_())
69
70     def closeEvent(self, event):
71         if self.trayIcon.isVisible():
72             self.hide()
73             event.ignore()
74
75     def createActions(self):
76         self.manageprofiles = QtGui.QAction("Manage &Profiles", self,
77                 triggered=self.hide)
78         self.quitAction = QtGui.QAction("&Quit", self,
79                 triggered=QtGui.qApp.quit)
80
81
82     def createTrayIcon(self):
83          #context menu build, right click
84          self.trayIconMenu = QtGui.QMenu(self)
85          self.trayIconMenu.addAction(self.manageprofiles)
86          self.trayIconMenu.addSeparator()
87          self.trayIconMenu.addAction(self.quitAction)
88
89          self.trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon(appconf.icon), self.app)
90          self.trayIcon.setContextMenu(self.trayIconMenu)
91          #baloon on hover
92          self.trayIcon.setToolTip(appconf.appname+" "+appconf.appversion+"\nRight Click to manage profiles.")
93          #left click profiles show
94          self.trayIcon.activated.connect(self.showTrayProfiles)
95
96
97     def showTrayProfiles(self,reason):
98         if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
99             print "Catched left click"
100             self.menuProfiles = QtGui.QMenu()
101             self.menuProfiles.setTitle("Profiles")
102
103             profiles = self.profileManager.getProfilesList()
104
105             for pr in profiles:
106                 pr = os.path.basename(pr)
107                 menuItem_Profile = self.menuProfiles.addAction(pr)
108
109                 #Using lambda function to pass additional arguments to the function, in this case the path of the profile
110                 receiver = lambda pr=pr: self.profileManager.activateProfile(pr)
111                 self.connect(menuItem_Profile, SIGNAL('triggered()'), receiver)
112
113                 self.menuProfiles.addAction(menuItem_Profile)
114
115             #menuItem_Profile = self.menuProfiles.addAction("")
116             #self.menuProfiles.addAction(menuItem_Profile)
117             self.menuProfiles.addSeparator()
118             menuItem_Profile = QtGui.QAction("  Add &Profile  ", self,
119             triggered=self.addProfile, icon=QtGui.QIcon(appconf.icon))
120             self.menuProfiles.addAction(menuItem_Profile)
121
122             self.menuProfiles.activateWindow()
123             self.menuProfiles.popup(QtGui.QCursor.pos())
124
125     def addProfile(self):
126         self.setWindowTitle("Add New Profile - Dboxswitch - dropbox profile switcher")
127         self.resize(300, 100)
128         text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
129                             'Enter profile name:')
130                 
131         if ok:
132             try:
133                 self.profileManager.addProfile(unicode(text))
134             except AppError, e:
135                 self.showError(str(e))
136
137     def showError(self, err):
138         """ Display an error message """
139         self.setWindowTitle("Error - Dboxswitch - dropbox profile switcher")
140         self.resize(200, 100)
141         err = QtGui.QErrorMessage.showMessage(QtGui.QErrorMessage.qtHandler(), "Error: "+err)
142
143
144