fix profile switching
[stack/code/dboxswitch.git] / dboxswitch / gui.py
1 # -*- coding: utf-8 -*-
2
3 """
4 Dboxswitch dropbox profile switcher
5
6 license: Modified BSD License
7
8 Copyright (c) 2012,  <stack@inventati.org>
9 All rights reserved.
10
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13     * Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15     * Redistributions in binary form must reproduce the above copyright
16       notice, this list of conditions and the following disclaimer in the
17       documentation and/or other materials provided with the distribution.
18     * Neither the name of the <organization> nor the
19       names of its contributors may be used to endorse or promote products
20       derived from this software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
26 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 """
34 import sys
35 import os
36 from time import sleep
37 from PyQt4 import QtGui, QtCore
38 from PyQt4.QtCore import SIGNAL
39 from qmenutooltip import QMenuToolTip
40
41 from apperror import AppError
42 from settings import appconf
43
44 class Gui(QtGui.QDialog):
45     def __init__(self, prManager):
46         """ Gui init, an QApplication is created and stored here, also a main window and a trayIcon
47         are created.
48         Default actions are builded and profile manager stored from argument (instance of ProfHandler """
49
50         self.app = QtGui.QApplication(sys.argv)
51         for i in range(23):
52                 if not QtGui.QSystemTrayIcon.isSystemTrayAvailable():
53                     sleep(4)
54                 else:
55                     break
56         #check if system tray is avaiable on the system
57         if not QtGui.QSystemTrayIcon.isSystemTrayAvailable():
58             QtGui.QMessageBox.critical(None, "Systray",
59                     "I couldn't detect any system tray on this system.")
60             sys.exit(1)
61         
62         QtGui.QApplication.setQuitOnLastWindowClosed(False)
63     
64         super(Gui, self).__init__()
65
66         self.profiles = []
67
68         self.createActions()
69         self.createTrayIcon()
70         self.createMainDialog()
71
72         self.trayIcon.show()
73  
74         self.profileManager = prManager
75
76     def main(self): 
77         """ Like Gtk application this main executes the app, thi is somewhat writed for
78         compatibility in porting the app """
79
80         sys.exit(self.app.exec_())
81
82     def closeEvent(self, event):
83         """ Handle application closing """
84
85         if self.trayIcon.isVisible():
86             self.hide()
87             event.ignore()
88
89     def createActions(self):
90         """ Create actions for the various components """
91
92         self.manageprofiles = QtGui.QAction("Manage &Profiles", self,
93                 triggered=self.show)
94         self.quitAction = QtGui.QAction("&Quit", self,
95                 triggered=QtGui.qApp.quit)
96
97         #profile manager component
98         self.addProfileAction = QtGui.QAction("  Add &Profile  ", self,
99                 triggered=self.addProfile)
100
101     def createMainDialog(self):    
102         """ Build the main dialog layout """ 
103         
104         self.setWindowTitle("Profile manager / Dboxswitch - dropbox profile switcher")
105         #add a default action to create a profile
106         self.addButton = QtGui.QPushButton(QtGui.QIcon(appconf.addpicon), 
107                     "Add Profile")
108         self.addButton.clicked.connect(self.addProfile)
109         self.addButton.setVisible(False)
110
111         self.la            = QtGui.QFormLayout()
112
113         self.la.addRow(self.addButton)
114         self.la.sizePolicy = QtGui.QSizePolicy.Minimum
115         self.la.setVerticalSpacing(15)
116         self.la.setHorizontalSpacing(15)
117         self.formGroupBox = QtGui.QGroupBox("Manage profiles:")
118         self.formGroupBox.setLayout(self.la)
119         mainLayout = QtGui.QVBoxLayout()
120         mainLayout.addWidget(self.formGroupBox)
121         self.setLayout(mainLayout)
122
123
124     def show(self):
125         """ Show the main dialog for dealing with profiles """
126
127         profiles = self.profileManager.getProfilesList()
128
129         for pr in profiles:
130             prname = self.profileManager.getBaseProfileName(pr)
131             delButton = QtGui.QToolButton()
132             label     = QtGui.QLabel(prname)
133             self.la.addRow(label, delButton)
134             delAction = QtGui.QAction(QtGui.QIcon(appconf.delpicon),"Delete", self,
135                 triggered=self.deleteProfileAction(prname, (label, delButton)))
136             delButton.setDefaultAction(delAction)
137             self.profiles.append((label, delButton))
138
139         #if profiles was empty
140         if len(profiles) == 0:
141             self.addButton.setVisible(True)
142         else:
143             self.addButton.setVisible(False)
144
145         super(Gui, self).show()
146
147     def hide(self):
148         """ Hides the main dialog """
149         
150         #Hides all the profiles in the Dialog
151         #User can deal with profiles like removing the dir manually
152
153         for ws in self.profiles:
154             for w in ws:
155                 w.setVisible(False)
156                 w.setParent(None)
157         super(Gui, self).hide()
158
159     def createTrayIcon(self):
160          """ Builds a new tray icon with a context menu and an action for the profile manager menu """
161
162          #context menu build, right click
163          self.trayIconMenu = QtGui.QMenu(self)
164          self.trayIconMenu.addAction(self.manageprofiles)
165          self.trayIconMenu.addSeparator()
166          self.trayIconMenu.addAction(self.quitAction)
167
168          #create the tray with an incon
169          self.trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon(appconf.icon), self.app)
170          #attach a context menu for the right click to the tray
171          self.trayIcon.setContextMenu(self.trayIconMenu)
172          #baloon on hover for the tray
173          self.trayIcon.setToolTip(appconf.appname+" "+appconf.appversion+"\nRight Click to manage profiles.")
174          #left click profiles show for the tray
175          self.trayIcon.activated.connect(self.showTrayProfiles)
176
177
178     def showTrayProfiles(self,reason):
179         """ Pops up a system tray profile Manager with a list of activable profiles and an 
180         action to add a new One """
181
182         if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
183             self.menuProfiles = QMenuToolTip()
184             self.menuProfiles.setTitle("Profiles")
185             QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_DontShowIconsInMenus, False)
186
187             #Get profiles from the ProfHandler embedded in the gui
188             profiles = self.profileManager.getProfilesList()
189
190             for prpath in profiles:
191                 pr = self.profileManager.getBaseProfileName(prpath)
192                 receiver = self.activateProfileAction(prpath)
193                 menuItem_Profile = self.menuProfiles.addAction(pr)
194                 if self.profileManager.isCurrentProfile(prpath):
195                     menuItem_Profile.setIcon(QtGui.QIcon(appconf.cpicon))
196                  
197                 #Using lambda function to pass additional arguments to the function, in this case the path of the profile
198                 self.connect(menuItem_Profile, SIGNAL('triggered()'), receiver)
199                 #set menu item ToolTip
200                 menuItem_Profile.setToolTip("Activate profile: "+pr)
201
202                 self.menuProfiles.addAction(menuItem_Profile)
203
204             self.menuProfiles.addSeparator()
205             #action and menu item for adding a New Profile
206             self.menuProfiles.addAction(self.addProfileAction)
207
208             self.menuProfiles.activateWindow()
209             self.menuProfiles.popup(QtGui.QCursor.pos())
210
211     def activateProfileAction(self, pr):
212         """ Returns a callable to be passed as an action for the switching profile mechanism 
213         to self.connect(menuItem_Profile... 
214         It compiles a function with a profile name as argument and also handle the displaying of errors."""
215
216         def f():
217             try:
218                 self.profileManager.activateProfile(pr)
219             except AppError, e:
220                 self.showError(str(e))
221         return f
222
223     def deleteProfileAction(self, pr, prWidgets):
224         """ Returns a callable to be passed as an action for the profile manager mechanism 
225         to self.connect(menuItem_Profile... 
226         It compiles a function with a profile name as argument and also handle the displaying of errors."""
227
228         def f():
229             try:
230                 self.profileManager.delProfile(pr)
231                 for w in prWidgets:
232                     w.setParent(None)
233                     w.setVisible(False)
234             except AppError, e:
235                 self.showError(str(e))
236         return f
237
238
239     def addProfile(self):
240         """ Gui frontend to add a new Profile, it requests the user a profile name 
241         through a QInputDialog and creates a new profile with the help of the ProfHandler embedded in the Gui """
242
243         self.setWindowTitle("Add New Profile - Dboxswitch - dropbox profile switcher")
244         self.resize(300, 100)
245         text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
246                             'Enter profile name:')
247         if ok:
248             try:
249                 self.profileManager.addProfile(unicode(text))
250             except AppError, e:
251                 self.showError(str(e))
252
253     def showError(self, err):
254         """ Display an error message through a QErrorMessage """
255
256         self.setWindowTitle("Error - Dboxswitch - dropbox profile switcher")
257         self.resize(200, 100)
258         err = QtGui.QErrorMessage.showMessage(QtGui.QErrorMessage.qtHandler(), "Error: "+err)