diff options
Diffstat (limited to 'src/leap/gui')
| -rw-r--r-- | src/leap/gui/__init__.py | 21 | ||||
| -rw-r--r-- | src/leap/gui/loggerwindow.py | 124 | ||||
| -rw-r--r-- | src/leap/gui/mainwindow.py | 37 | ||||
| -rw-r--r-- | src/leap/gui/ui/loggerwindow.ui | 155 | ||||
| -rw-r--r-- | src/leap/gui/ui/mainwindow.ui | 10 | 
5 files changed, 345 insertions, 2 deletions
| diff --git a/src/leap/gui/__init__.py b/src/leap/gui/__init__.py index e69de29b..d31dac64 100644 --- a/src/leap/gui/__init__.py +++ b/src/leap/gui/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# __init__.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/>. +""" +init file for leap.gui +""" +from .. import app +__all__ = [app] diff --git a/src/leap/gui/loggerwindow.py b/src/leap/gui/loggerwindow.py new file mode 100644 index 00000000..dd724ac7 --- /dev/null +++ b/src/leap/gui/loggerwindow.py @@ -0,0 +1,124 @@ +# -*- coding: utf-8 -*- +# loggerwindow.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/>. + +""" +History log window +""" +import logging + +from PySide import QtGui +from ui_loggerwindow import Ui_LoggerWindow +from leap.common.check import leap_assert, leap_assert_type +from leap.util.leap_log_handler import LeapLogHandler + +logger = logging.getLogger(__name__) + + +class LoggerWindow(QtGui.QWidget): +    """ +    Window that displays a history of the logged messages in the app. +    """ +    def __init__(self, handler): +        """ +        Initialize the widget with the custom handler. + +        :param handler: Custom handler that supports history and signal. +        :type handler: LeapLogHandler. +        """ +        QtGui.QWidget.__init__(self) +        leap_assert(handler, "We need a handler for the logger window") +        leap_assert_type(handler, LeapLogHandler) + +        # Load UI +        self.ui = Ui_LoggerWindow() +        self.ui.setupUi(self) + +        # Make connections +        self.ui.btnSave.clicked.connect(self._save_log_to_file) +        self.ui.btnDebug.toggled.connect(self._load_history), +        self.ui.btnInfo.toggled.connect(self._load_history), +        self.ui.btnWarning.toggled.connect(self._load_history), +        self.ui.btnError.toggled.connect(self._load_history), +        self.ui.btnCritical.toggled.connect(self._load_history) + +        # Load logging history and connect logger with the widget +        self._logging_handler = handler +        self._connect_to_handler() +        self._load_history() + +    def _connect_to_handler(self): +        """ +        This method connects the loggerwindow with the handler through a +        signal communicate the logger events. +        """ +        self._logging_handler.new_log.connect(self._add_log_line) + +    def _add_log_line(self, log): +        """ +        Adds a line to the history, only if it's in the desired levels to show. + +        :param log: a log record to be inserted in the widget +        :type log: a dict with RECORD_KEY and MESSAGE_KEY. +            the record contains the LogRecord of the logging module, +            the message contains the formatted message for the log. +        """ +        level = log[LeapLogHandler.RECORD_KEY].levelname +        message = log[LeapLogHandler.MESSAGE_KEY] + +        if self._logs_to_display[level]: +            self.ui.txtLogHistory.append(message) + +    def _load_history(self): +        """ +        Load the previous logged messages in the widget. +        They are stored in the custom handler. +        """ +        self._set_logs_to_display() +        self.ui.txtLogHistory.clear() +        history = self._logging_handler.log_history +        for line in history: +            self._add_log_line(line) + +    def _set_logs_to_display(self): +        """ +        Sets the logs_to_display dict getting the toggled options from the ui +        """ +        self._logs_to_display = { +            'DEBUG': self.ui.btnDebug.isChecked(), +            'INFO': self.ui.btnInfo.isChecked(), +            'WARNING': self.ui.btnWarning.isChecked(), +            'ERROR': self.ui.btnError.isChecked(), +            'CRITICAL': self.ui.btnCritical.isChecked() +        } + +    def _save_log_to_file(self): +        """ +        Lets the user save the current log to a file +        """ +        fileName, filtr = QtGui.QFileDialog.getSaveFileName( +            self, self.tr("Save As")) + +        if fileName: +            try: +                with open(fileName, 'w') as output: +                    output.write(self.ui.txtLogHistory.toPlainText()) +                    output.write('\n') +                logger.debug('Log saved in %s' % (fileName, )) +            except IOError, e: +                logger.error("Error saving log file: %r" % (e, )) +        else: +            logger.debug('Log not saved!') diff --git a/src/leap/gui/mainwindow.py b/src/leap/gui/mainwindow.py index a8aa1b1c..12187f51 100644 --- a/src/leap/gui/mainwindow.py +++ b/src/leap/gui/mainwindow.py @@ -35,6 +35,7 @@ from leap.common.events import events_pb2 as proto  from leap.config.leapsettings import LeapSettings  from leap.config.providerconfig import ProviderConfig  from leap.crypto.srpauth import SRPAuth +from leap.gui.loggerwindow import LoggerWindow  from leap.gui.wizard import Wizard  from leap.services.eip.eipbootstrapper import EIPBootstrapper  from leap.services.eip.eipconfig import EIPConfig @@ -208,6 +209,7 @@ class MainWindow(QtGui.QMainWindow):          self.ui.action_about_leap.triggered.connect(self._about)          self.ui.action_quit.triggered.connect(self.quit)          self.ui.action_wizard.triggered.connect(self._launch_wizard) +        self.ui.action_show_logs.triggered.connect(self._show_logger_window)          self.raise_window.connect(self._do_raise_mainwindow)          # Used to differentiate between real quits and close to tray @@ -249,6 +251,8 @@ class MainWindow(QtGui.QMainWindow):          self._wizard = None          self._wizard_firstrun = False +        self._logger_window = None +          self._bypass_checks = bypass_checks          self._soledad = None @@ -282,6 +286,35 @@ class MainWindow(QtGui.QMainWindow):          self._wizard.exec_()          self._wizard = None +    def _get_leap_logging_handler(self): +        """ +        Gets the leap handler from the top level logger + +        :return: a logging handler or None +        :rtype: LeapLogHandler or None +        """ +        from leap.util.leap_log_handler import LeapLogHandler +        leap_logger = logging.getLogger('leap') +        for h in leap_logger.handlers: +            if isinstance(h, LeapLogHandler): +                return h +        return None + +    def _show_logger_window(self): +        """ +        Displays the window with the history of messages logged until now +        and displays the new ones on arrival. +        """ +        if self._logger_window is None: +            leap_log_handler = self._get_leap_logging_handler() +            if leap_log_handler is None: +                logger.error('Leap logger handler not found') +            else: +                self._logger_window = LoggerWindow(handler=leap_log_handler) +                self._logger_window.show() +        else: +            self._logger_window.show() +      def _remember_state_changed(self, state):          enable = True if state == QtCore.Qt.Checked else False          self.ui.chkAutoLogin.setEnabled(enable) @@ -1101,6 +1134,10 @@ class MainWindow(QtGui.QMainWindow):          self._really_quit = True          if self._wizard:              self._wizard.close() + +        if self._logger_window: +            self._logger_window.close() +          self.close()          if self._quit_callback: diff --git a/src/leap/gui/ui/loggerwindow.ui b/src/leap/gui/ui/loggerwindow.ui new file mode 100644 index 00000000..28325cdf --- /dev/null +++ b/src/leap/gui/ui/loggerwindow.ui @@ -0,0 +1,155 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>LoggerWindow</class> + <widget class="QWidget" name="LoggerWindow"> +  <property name="geometry"> +   <rect> +    <x>0</x> +    <y>0</y> +    <width>648</width> +    <height>469</height> +   </rect> +  </property> +  <property name="windowTitle"> +   <string>Logs</string> +  </property> +  <property name="windowIcon"> +   <iconset resource="../../../../data/resources/mainwindow.qrc"> +    <normaloff>:/images/leap-color-big.png</normaloff>:/images/leap-color-big.png</iconset> +  </property> +  <layout class="QGridLayout" name="gridLayout"> +   <item row="2" column="0" colspan="2"> +    <widget class="QTextBrowser" name="txtLogHistory"/> +   </item> +   <item row="0" column="0" colspan="2"> +    <layout class="QHBoxLayout" name="horizontalLayout_2"> +     <item> +      <widget class="QPushButton" name="btnDebug"> +       <property name="text"> +        <string>Debug</string> +       </property> +       <property name="icon"> +        <iconset resource="../../../../data/resources/loggerwindow.qrc"> +         <normaloff>:/images/oxygen-icons/script-error.png</normaloff>:/images/oxygen-icons/script-error.png</iconset> +       </property> +       <property name="checkable"> +        <bool>true</bool> +       </property> +       <property name="checked"> +        <bool>true</bool> +       </property> +       <property name="flat"> +        <bool>true</bool> +       </property> +      </widget> +     </item> +     <item> +      <widget class="QPushButton" name="btnInfo"> +       <property name="text"> +        <string>Info</string> +       </property> +       <property name="icon"> +        <iconset resource="../../../../data/resources/loggerwindow.qrc"> +         <normaloff>:/images/oxygen-icons/dialog-information.png</normaloff>:/images/oxygen-icons/dialog-information.png</iconset> +       </property> +       <property name="checkable"> +        <bool>true</bool> +       </property> +       <property name="checked"> +        <bool>true</bool> +       </property> +       <property name="flat"> +        <bool>true</bool> +       </property> +      </widget> +     </item> +     <item> +      <widget class="QPushButton" name="btnWarning"> +       <property name="text"> +        <string>Warning</string> +       </property> +       <property name="icon"> +        <iconset resource="../../../../data/resources/loggerwindow.qrc"> +         <normaloff>:/images/oxygen-icons/dialog-warning.png</normaloff>:/images/oxygen-icons/dialog-warning.png</iconset> +       </property> +       <property name="checkable"> +        <bool>true</bool> +       </property> +       <property name="checked"> +        <bool>true</bool> +       </property> +       <property name="flat"> +        <bool>true</bool> +       </property> +      </widget> +     </item> +     <item> +      <widget class="QPushButton" name="btnError"> +       <property name="text"> +        <string>Error</string> +       </property> +       <property name="icon"> +        <iconset resource="../../../../data/resources/loggerwindow.qrc"> +         <normaloff>:/images/oxygen-icons/dialog-error.png</normaloff>:/images/oxygen-icons/dialog-error.png</iconset> +       </property> +       <property name="checkable"> +        <bool>true</bool> +       </property> +       <property name="checked"> +        <bool>true</bool> +       </property> +       <property name="flat"> +        <bool>true</bool> +       </property> +      </widget> +     </item> +     <item> +      <widget class="QPushButton" name="btnCritical"> +       <property name="text"> +        <string>Critical</string> +       </property> +       <property name="icon"> +        <iconset resource="../../../../data/resources/loggerwindow.qrc"> +         <normaloff>:/images/oxygen-icons/edit-bomb.png</normaloff>:/images/oxygen-icons/edit-bomb.png</iconset> +       </property> +       <property name="checkable"> +        <bool>true</bool> +       </property> +       <property name="checked"> +        <bool>true</bool> +       </property> +       <property name="flat"> +        <bool>true</bool> +       </property> +      </widget> +     </item> +     <item> +      <widget class="QPushButton" name="btnSave"> +       <property name="text"> +        <string>Save to file</string> +       </property> +       <property name="icon"> +        <iconset resource="../../../../data/resources/loggerwindow.qrc"> +         <normaloff>:/images/oxygen-icons/document-save-as.png</normaloff>:/images/oxygen-icons/document-save-as.png</iconset> +       </property> +      </widget> +     </item> +    </layout> +   </item> +  </layout> + </widget> + <tabstops> +  <tabstop>btnDebug</tabstop> +  <tabstop>btnInfo</tabstop> +  <tabstop>btnWarning</tabstop> +  <tabstop>btnError</tabstop> +  <tabstop>btnCritical</tabstop> +  <tabstop>btnSave</tabstop> +  <tabstop>txtLogHistory</tabstop> + </tabstops> + <resources> +  <include location="../../../../data/resources/loggerwindow.qrc"/> +  <include location="../../../../data/resources/mainwindow.qrc"/> + </resources> + <connections/> +</ui> diff --git a/src/leap/gui/ui/mainwindow.ui b/src/leap/gui/ui/mainwindow.ui index b42ff180..fdf5c167 100644 --- a/src/leap/gui/ui/mainwindow.ui +++ b/src/leap/gui/ui/mainwindow.ui @@ -396,7 +396,7 @@       <x>0</x>       <y>0</y>       <width>415</width> -     <height>25</height> +     <height>21</height>      </rect>     </property>     <widget class="QMenu" name="menuSession"> @@ -417,9 +417,10 @@     </widget>     <widget class="QMenu" name="menuSettings">      <property name="title"> -     <string>S&ettings</string> +     <string>&Utils</string>      </property>      <addaction name="action_wizard"/> +    <addaction name="action_show_logs"/>     </widget>     <addaction name="menuSession"/>     <addaction name="menuSettings"/> @@ -451,6 +452,11 @@      <string>&Wizard</string>     </property>    </action> +  <action name="action_show_logs"> +   <property name="text"> +    <string>Show &logs</string> +   </property> +  </action>   </widget>   <tabstops>    <tabstop>lnUser</tabstop> | 
