get profile names worted by name
[stack/code/dboxswitch.git] / gui.py
diff --git a/gui.py b/gui.py
old mode 100755 (executable)
new mode 100644 (file)
index dd7e81e..920ee85
--- a/gui.py
+++ b/gui.py
@@ -1,4 +1,3 @@
-#!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 """
@@ -34,7 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 """
 import sys
 import os
-from PyQt4 import QtGui
+from PyQt4 import QtGui, QtCore
 from PyQt4.QtCore import SIGNAL
 from qmenutooltip import QMenuToolTip
 
@@ -92,7 +91,7 @@ class Gui(QtGui.QDialog):
 
         #profile manager component
         self.addProfile = QtGui.QAction("  Add &Profile  ", self,
-                triggered=self.addProfile, icon=QtGui.QIcon(appconf.icon))
+                triggered=self.addProfile)
 
 
     def createTrayIcon(self):
@@ -121,16 +120,19 @@ class Gui(QtGui.QDialog):
         if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
             self.menuProfiles = QMenuToolTip()
             self.menuProfiles.setTitle("Profiles")
+            QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_DontShowIconsInMenus, False)
 
             #Get profiles from the ProfHandler embedded in the gui
             profiles = self.profileManager.getProfilesList()
 
-            for pr in profiles:
-                pr = os.path.basename(pr)
+            for prpath in profiles:
+                pr = self.profileManager.getBaseProfileName(prpath)
+                receiver = self.activateProfileAction(prpath)
                 menuItem_Profile = self.menuProfiles.addAction(pr)
-
+                if self.profileManager.isCurrentProfile(prpath):
+                    menuItem_Profile.setIcon(QtGui.QIcon(appconf.cpicon))
+                 
                 #Using lambda function to pass additional arguments to the function, in this case the path of the profile
-                receiver = lambda pr=pr: self.profileManager.activateProfile(pr)
                 self.connect(menuItem_Profile, SIGNAL('triggered()'), receiver)
                 #set menu item ToolTip
                 menuItem_Profile.setToolTip("Activate profile: "+pr)
@@ -144,6 +146,18 @@ class Gui(QtGui.QDialog):
             self.menuProfiles.activateWindow()
             self.menuProfiles.popup(QtGui.QCursor.pos())
 
+    def activateProfileAction(self, pr):
+        """ Returns a callable to be passed as an action for the switching profile mechanism 
+        to self.connect(menuItem_Profile... 
+        It compiles a function with a profile name as argument and also handle the displaying of errors."""
+
+        def f():
+            try:
+                self.profileManager.activateProfile(pr)
+            except AppError, e:
+                self.showError(str(e))
+        return f
+
     def addProfile(self):
         """ Gui frontend to add a new Profile, it requests the user a profile name 
         through a QInputDialog and creates a new profile with the help of the ProfHandler embedded in the Gui """