From 6714b423a4ae879e23a82a60480387186e737ab1 Mon Sep 17 00:00:00 2001
From: Ivan Alejandro <ivanalejandro0@gmail.com>
Date: Thu, 7 Nov 2013 17:46:02 -0300
Subject: Use custom systray that ease the tooltip use.

- Create a custom SysTray that allows us to set services tooltips
independently.
- Initialize tooltip with service name at start.
- Update required service status on tooltip update.

[Closes #3998]
---
 src/leap/bitmask/gui/eip_status.py  | 17 ++++++-------
 src/leap/bitmask/gui/mail_status.py | 21 ++++++----------
 src/leap/bitmask/gui/mainwindow.py  |  4 ++-
 src/leap/bitmask/gui/systray.py     | 49 +++++++++++++++++++++++++++++++++++++
 4 files changed, 67 insertions(+), 24 deletions(-)
 create mode 100644 src/leap/bitmask/gui/systray.py

(limited to 'src')

diff --git a/src/leap/bitmask/gui/eip_status.py b/src/leap/bitmask/gui/eip_status.py
index fbb8ffe1..c41d9706 100644
--- a/src/leap/bitmask/gui/eip_status.py
+++ b/src/leap/bitmask/gui/eip_status.py
@@ -183,21 +183,22 @@ class EIPStatusWidget(QtGui.QWidget):
 
     def set_systray(self, systray):
         """
-        Sets the systray object to use.
+        Sets the systray object to use and adds the service line for EIP.
 
         :param systray: Systray object
         :type systray: QtGui.QSystemTrayIcon
         """
         leap_assert_type(systray, QtGui.QSystemTrayIcon)
         self._systray = systray
-        self._systray.setToolTip(self.tr("All services are OFF"))
+        eip_status = self.tr("{0}: OFF").format(self._service_name)
+        self._systray.set_service_tooltip(EIP_SERVICE, eip_status)
 
     def _update_systray_tooltip(self):
         """
-        Updates the system tray icon tooltip using the eip and mx status.
+        Updates the system tray tooltip using the eip status.
         """
-        status = self.tr("Encrypted Internet: {0}").format(self._eip_status)
-        self._systray.setToolTip(status)
+        eip_status = u"{0}: {1}".format(self._service_name, self._eip_status)
+        self._systray.set_service_tooltip(EIP_SERVICE, eip_status)
 
     def set_action_eip_startstop(self, action_eip_startstop):
         """
@@ -395,10 +396,8 @@ class EIPStatusWidget(QtGui.QWidget):
             # the UI won't update properly
             QtCore.QTimer.singleShot(
                 0, self.eipconnection.qtsigs.do_disconnect_signal)
-            QtCore.QTimer.singleShot(0, partial(self.set_eip_status,
-                                                self.tr("Unable to start VPN, "
-                                                        "it's already "
-                                                        "running.")))
+            msg = self.tr("Unable to start VPN, it's already running.")
+            QtCore.QTimer.singleShot(0, partial(self.set_eip_status, msg))
         else:
             self.set_eip_status(status)
 
diff --git a/src/leap/bitmask/gui/mail_status.py b/src/leap/bitmask/gui/mail_status.py
index 2ca0fb0a..e89fb376 100644
--- a/src/leap/bitmask/gui/mail_status.py
+++ b/src/leap/bitmask/gui/mail_status.py
@@ -152,29 +152,22 @@ class MailStatusWidget(QtGui.QWidget):
 
     def set_systray(self, systray):
         """
-        Sets the systray object to use.
+        Sets the systray object to use and adds the service line for MX.
 
         :param systray: Systray object
         :type systray: QtGui.QSystemTrayIcon
         """
         leap_assert_type(systray, QtGui.QSystemTrayIcon)
         self._systray = systray
-        self._systray.setToolTip(self.tr("All services are OFF"))
+        mx_status = self.tr("{0}: OFF").format(self._service_name)
+        self._systray.set_service_tooltip(MX_SERVICE, mx_status)
 
     def _update_systray_tooltip(self):
         """
-        Updates the system tray icon tooltip using the eip and mx status.
-        """
-        # TODO: Figure out how to handle this with the two status in different
-        # classes
-        # XXX right now we could connect the state transition signals of the
-        # two connection machines (EIP/Mail) to a class that keeps track of the
-        # state -- kali
-        # status = self.tr("Encrypted Internet: {0}").format(self._eip_status)
-        # status += '\n'
-        # status += self.tr("Mail is {0}").format(self._mx_status)
-        # self._systray.setToolTip(status)
-        pass
+        Updates the system tray tooltip using the mx status.
+        """
+        mx_status = u"{0}: {1}".format(self._service_name, self._mx_status)
+        self._systray.set_service_tooltip(MX_SERVICE, mx_status)
 
     def set_action_mail_status(self, action_mail_status):
         """
diff --git a/src/leap/bitmask/gui/mainwindow.py b/src/leap/bitmask/gui/mainwindow.py
index 91bbe9cb..f7142df7 100644
--- a/src/leap/bitmask/gui/mainwindow.py
+++ b/src/leap/bitmask/gui/mainwindow.py
@@ -36,6 +36,7 @@ from leap.bitmask.gui import statemachines
 from leap.bitmask.gui.eip_status import EIPStatusWidget
 from leap.bitmask.gui.mail_status import MailStatusWidget
 from leap.bitmask.gui.wizard import Wizard
+from leap.bitmask.gui.systray import SysTray
 
 from leap.bitmask import provider
 from leap.bitmask.platform_init import IS_WIN, IS_MAC
@@ -628,12 +629,13 @@ class MainWindow(QtGui.QMainWindow):
         systrayMenu.addAction(self._action_mail_status)
         systrayMenu.addSeparator()
         systrayMenu.addAction(self.ui.action_quit)
-        self._systray = QtGui.QSystemTrayIcon(self)
+        self._systray = SysTray(self)
         self._systray.setContextMenu(systrayMenu)
         self._systray.setIcon(self._eip_status.ERROR_ICON_TRAY)
         self._systray.setVisible(True)
         self._systray.activated.connect(self._tray_activated)
 
+        self._mail_status.set_systray(self._systray)
         self._eip_status.set_systray(self._systray)
 
     def _tray_activated(self, reason=None):
diff --git a/src/leap/bitmask/gui/systray.py b/src/leap/bitmask/gui/systray.py
new file mode 100644
index 00000000..d30b5f32
--- /dev/null
+++ b/src/leap/bitmask/gui/systray.py
@@ -0,0 +1,49 @@
+# -*- coding: utf-8 -*-
+# systray.py
+# Copyright (C) 2013 LEAP
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+"""
+Custom system tray manager.
+"""
+
+from PySide import QtGui
+
+from leap.common.check import leap_assert_type
+
+
+class SysTray(QtGui.QSystemTrayIcon):
+    """
+    Custom system tray that allows us to use a more 'intelligent' tooltip.
+    """
+
+    def __init__(self, parent=None):
+        QtGui.QSystemTrayIcon.__init__(self, parent)
+        self._services = {}
+
+    def set_service_tooltip(self, service, tooltip):
+        """
+        Sets the service tooltip.
+
+        :param service: service name identifier
+        :type service: unicode
+        :param tooltip: tooltip to display for that service
+        :type tooltip: unicode
+        """
+        leap_assert_type(service, unicode)
+        leap_assert_type(tooltip, unicode)
+
+        self._services[service] = tooltip
+        tooltip = "\n".join(self._services.values())
+        self.setToolTip(tooltip)
-- 
cgit v1.2.3