gui.py better function documentation
[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         """ Gui init, an QApplication is created and stored here, also a main window and a trayIcon
46         are created.
47         Default actions are builded and profile manager stored from argument (instance of ProfHandler """
48
49         self.app = QtGui.QApplication(sys.argv)
50     
51         #check if system tray is avaiable on the system
52         if not QtGui.QSystemTrayIcon.isSystemTrayAvailable():
53             QtGui.QMessageBox.critical(None, "Systray",
54                     "I couldn't detect any system tray on this system.")
55             sys.exit(1)
56         
57         QtGui.QApplication.setQuitOnLastWindowClosed(False)
58     
59         super(Gui, self).__init__()
60
61         self.createActions()
62         self.createTrayIcon()
63
64         self.trayIcon.show()
65
66         self.setWindowTitle("Profile manager / Dboxswitch - dropbox profile switcher")
67         self.resize(200, 200)
68
69         self.profileManager = prManager
70
71     def main(self): 
72         """ Like Gtk application this main executes the app, thi is somewhat writed for
73         compatibility in porting the app """
74
75         sys.exit(self.app.exec_())
76
77     def closeEvent(self, event):
78         """ Handle application closing """
79
80         if self.trayIcon.isVisible():
81             self.hide()
82             event.ignore()
83
84     def createActions(self):
85         """ Create actions for the various components """
86
87         self.manageprofiles = QtGui.QAction("Manage &Profiles", self,
88                 triggered=self.hide)
89         self.quitAction = QtGui.QAction("&Quit", self,
90                 triggered=QtGui.qApp.quit)
91
92
93     def createTrayIcon(self):
94          """ Builds a new tray icon with a context menu and an action for the profile manager menu """
95
96          #context menu build, right click
97          self.trayIconMenu = QtGui.QMenu(self)
98          self.trayIconMenu.addAction(self.manageprofiles)
99          self.trayIconMenu.addSeparator()
100          self.trayIconMenu.addAction(self.quitAction)
101
102          #create the tray with an incon
103          self.trayIcon = QtGui.QSystemTrayIcon(QtGui.QIcon(appconf.icon), self.app)
104          #attach a context menu for the right click to the tray
105          self.trayIcon.setContextMenu(self.trayIconMenu)
106          #baloon on hover for the tray
107          self.trayIcon.setToolTip(appconf.appname+" "+appconf.appversion+"\nRight Click to manage profiles.")
108          #left click profiles show for the tray
109          self.trayIcon.activated.connect(self.showTrayProfiles)
110
111
112     def showTrayProfiles(self,reason):
113         """ Pops up a system tray profile Manager with a list of activable profiles and an 
114         action to add a new One """
115
116         if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
117             self.menuProfiles = QtGui.QMenu()
118             self.menuProfiles.setTitle("Profiles")
119
120             #Get profiles from the ProfHandler embedded in the gui
121             profiles = self.profileManager.getProfilesList()
122
123             for pr in profiles:
124                 pr = os.path.basename(pr)
125                 menuItem_Profile = self.menuProfiles.addAction(pr)
126
127                 #Using lambda function to pass additional arguments to the function, in this case the path of the profile
128                 receiver = lambda pr=pr: self.profileManager.activateProfile(pr)
129                 self.connect(menuItem_Profile, SIGNAL('triggered()'), receiver)
130
131                 self.menuProfiles.addAction(menuItem_Profile)
132
133             #menuItem_Profile = self.menuProfiles.addAction("")
134             #self.menuProfiles.addAction(menuItem_Profile)
135             self.menuProfiles.addSeparator()
136             menuItem_Profile = QtGui.QAction("  Add &Profile  ", self,
137             triggered=self.addProfile, icon=QtGui.QIcon(appconf.icon))
138             self.menuProfiles.addAction(menuItem_Profile)
139
140             self.menuProfiles.activateWindow()
141             self.menuProfiles.popup(QtGui.QCursor.pos())
142
143     def addProfile(self):
144         """ Gui frontend to add a new Profile, it requests the user a profile name 
145         through a QInputDialog and creates a new profile with the help of the ProfHandler embedded in the Gui """
146
147         self.setWindowTitle("Add New Profile - Dboxswitch - dropbox profile switcher")
148         self.resize(300, 100)
149         text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
150                             'Enter profile name:')
151         if ok:
152             try:
153                 self.profileManager.addProfile(unicode(text))
154             except AppError, e:
155                 self.showError(str(e))
156
157     def showError(self, err):
158         """ Display an error message through a QErrorMessage """
159
160         self.setWindowTitle("Error - Dboxswitch - dropbox profile switcher")
161         self.resize(200, 100)
162         err = QtGui.QErrorMessage.showMessage(QtGui.QErrorMessage.qtHandler(), "Error: "+err)