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