~mandel/ubuntu-sso-client/pinned-certs

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/ssl_dialog.py

  • Committer: Manuel de la Pena
  • Date: 2012-02-21 16:04:18 UTC
  • mfrom: (865.1.23 ssl-dialog)
  • Revision ID: manuel.delapena@canonical.com-20120221160418-6m3hj9otd7w2uqh4
Merged ssl-dialog into webclient-use-dialog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
 
1
# -*- coding: utf-8 -*-
2
2
#
3
3
# Copyright 2012 Canonical Ltd.
4
4
#
15
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
16
16
"""Qt implementation of the SSL ui."""
17
17
 
 
18
import argparse
 
19
import sys
 
20
 
18
21
from PyQt4.QtGui import (
19
22
    QApplication,
20
 
    QColor,
21
23
    QDialog,
22
 
    QHBoxLayout,
23
24
    QIcon,
24
 
    QPalette,
25
25
    QScrollArea,
26
26
    QTextEdit,
27
27
)
28
28
 
29
29
from ubuntu_sso.logger import setup_logging
 
30
# Unused import resources_rc, pylint: disable=W0611
 
31
from ubuntu_sso.qt.ui import resources_rc
 
32
# pylint: enable=W0611
30
33
from ubuntu_sso.qt.ui.ssl_dialog_ui import Ui_SSLDialog
31
34
from ubuntu_sso.qt.expander import QExpander
 
35
from ubuntu_sso.utils.ui import (
 
36
    CANCEL_BUTTON,
 
37
    SSL_DIALOG_TITLE,
 
38
    SSL_HEADER,
 
39
    SSL_EXPLANATION,
 
40
    SSL_FIRST_REASON,
 
41
    SSL_SECOND_REASON,
 
42
    SSL_THIRD_REASON,
 
43
    SSL_CERT_DETAILS,
 
44
    SSL_NOT_SURE,
 
45
    SSL_REMEBER_DECITION,
 
46
    SSL_HELP_BUTTON,
 
47
    SSL_CONNECT_BUTTON,
 
48
)
 
49
 
 
50
REASONS_TEMPLATE = ('<p>%(explanation)s</p>'
 
51
                    '<ul style="margin-left:-10px">'
 
52
                    '<li style="margin-bottom:5px;">%(first_reason)s</li>'
 
53
                    '<li style="margin-bottom:5px;">%(second_reason)s</li>'
 
54
                    '<li>%(third_reason)s</li>'
 
55
                    '</ul>')
 
56
 
 
57
CERTIFICATE_ACCEPTED = 0
 
58
USER_CANCELATION = -1
 
59
 
 
60
logger = setup_logging("ubuntu_sso.qt.proxy_dialog")
 
61
 
32
62
 
33
63
class SSLDialog(QDialog):
34
64
    """"Dialog used to show SSL exceptions."""
35
65
 
36
 
    def __init__(self, parent=None):
 
66
    def __init__(self, domain=None, details=None, parent=None):
37
67
        """Create a new instance."""
38
68
        QDialog.__init__(self)
 
69
        if domain is None:
 
70
            logger.debug('Domain passed as None.')
 
71
            domain = ''
 
72
        self.domain = domain
 
73
        if details is None:
 
74
            logger.debug('Details passed as None.')
 
75
            details = ''
 
76
        self.details = details
 
77
        self.ssl_text = None
 
78
        self.expander = None
39
79
        self.ui = Ui_SSLDialog()
40
80
        self.ui.setupUi(self)
41
 
        self.ssl_text = QTextEdit()
42
 
        self.ssl_text.setFixedSize(self.width(), 50)
43
 
        scroll_area = QScrollArea(self.ssl_text)
44
 
        self._set_expander(scroll_area)
 
81
        self.setWindowTitle(SSL_DIALOG_TITLE)
 
82
        self._set_expander()
45
83
        self._set_labels()
46
84
        self._set_buttons()
47
85
        self._set_icon()
48
86
 
49
87
    def _set_labels(self):
50
88
        """Set the labels translations."""
 
89
        self.ui.title_label.setText(SSL_HEADER)
 
90
        explanation = SSL_EXPLANATION % dict(domain=self.domain)
 
91
        intro = REASONS_TEMPLATE % dict(explanation=explanation,
 
92
                                        first_reason=SSL_FIRST_REASON,
 
93
                                        second_reason=SSL_SECOND_REASON,
 
94
                                        third_reason=SSL_THIRD_REASON)
 
95
        self.ui.intro_label.setText(intro)
 
96
        self.ui.not_sure_label.setText(SSL_NOT_SURE)
 
97
        self.ui.remember_checkbox.setText(SSL_REMEBER_DECITION)
 
98
 
 
99
    def _on_cancel_clicked(self):
 
100
        """Cancel was cliked."""
 
101
        logger.debug('User canceled the ssl dialog.')
 
102
        self.done(USER_CANCELATION)
 
103
 
 
104
    def _on_connect_clicked(self):
 
105
        """Connect was clicked."""
 
106
        logger.debug('User accepted the ssl certificate.')
 
107
        self.done(CERTIFICATE_ACCEPTED)
51
108
 
52
109
    def _set_buttons(self):
53
110
        """Set the labels of the buttons."""
54
 
 
55
 
    def _on_expand(self):
56
 
        """Increase the size."""
57
 
        if self.expander.expanded():
58
 
            self.setFixedSize(self.width(), self.height() + 75)
59
 
        else:
60
 
            self.setFixedSize(self.width(), self.height() - 75)
61
 
 
62
 
    def _set_expander(self, widget):
 
111
        self.ui.help_button.setText(SSL_HELP_BUTTON)
 
112
        self.ui.cancel_button.setText(CANCEL_BUTTON)
 
113
        self.ui.cancel_button.clicked.connect(self._on_cancel_clicked)
 
114
        self.ui.connect_button.setText(SSL_CONNECT_BUTTON)
 
115
        self.ui.connect_button.clicked.connect(self._on_connect_clicked)
 
116
 
 
117
    def _set_expander(self):
63
118
        """Set the expander widget."""
64
 
        self.expander = QExpander('Certificate details')
65
 
        self.expander.addWidget(widget)
66
 
        self.expander.label.clicked.connect(self._on_expand)
 
119
        self.ssl_text = QTextEdit()
 
120
        self.ssl_text.setText(self.details)
 
121
        scroll_area = QScrollArea()
 
122
        scroll_area.setViewport(self.ssl_text)
 
123
        scroll_area.setFixedHeight(50)
 
124
 
 
125
        self.expander = QExpander(SSL_CERT_DETAILS)
 
126
        self.expander.addWidget(scroll_area)
67
127
        self.ui.expander_layout.insertWidget(2, self.expander)
68
128
 
69
129
    def _set_icon(self):
72
132
        self.ui.logo_label.setText('')
73
133
        self.ui.logo_label.setPixmap(icon.pixmap(48, 48))
74
134
 
75
 
if __name__ == '__main__':
76
 
    import sys
 
135
 
 
136
def parse_args():
 
137
    """Parse sys.arg options."""
 
138
    parser = argparse.ArgumentParser(
 
139
            description='Open the SSL certificate UI.')
 
140
    parser.add_argument('--domain', required=True,
 
141
        help='the domain whose ssl certificate we are going to show.')
 
142
    parser.add_argument('--details', required=True,
 
143
        help='the details ssl certificate we are going to show.')
 
144
    return parser.parse_args()
 
145
 
 
146
 
 
147
def main():
 
148
    """Main method used to show the creds dialog."""
 
149
    # pylint: disable=W0612
77
150
    app = QApplication(sys.argv)
78
151
    # pylint: enable=W0612
79
 
    win = SSLDialog()
 
152
    args = parse_args()
 
153
    win = SSLDialog(domain=args.domain, details=args.details)
80
154
    return_code = win.exec_()
81
155
    sys.exit(return_code)