1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog mixin class providing common callback methods for
8the pysvn client.
9"""
10
11from PyQt5.QtWidgets import QApplication, QDialog, QWidget
12
13from E5Gui.E5OverrideCursor import E5OverridenCursor
14
15
16class SvnDialogMixin:
17    """
18    Class implementing a dialog mixin providing common callback methods for
19    the pysvn client.
20    """
21    def __init__(self, log=""):
22        """
23        Constructor
24
25        @param log optional log message (string)
26        """
27        self.shouldCancel = False
28        self.logMessage = log
29
30    def _cancel(self):
31        """
32        Protected method to request a cancellation of the current action.
33        """
34        self.shouldCancel = True
35
36    def _reset(self):
37        """
38        Protected method to reset the internal state of the dialog.
39        """
40        self.shouldCancel = False
41
42    def _clientCancelCallback(self):
43        """
44        Protected method called by the client to check for cancellation.
45
46        @return flag indicating a cancellation
47        """
48        QApplication.processEvents()
49        return self.shouldCancel
50
51    def _clientLoginCallback(self, realm, username, may_save):
52        """
53        Protected method called by the client to get login information.
54
55        @param realm name of the realm of the requested credentials (string)
56        @param username username as supplied by subversion (string)
57        @param may_save flag indicating, that subversion is willing to save
58            the answers returned (boolean)
59        @return tuple of four values (retcode, username, password, save).
60            Retcode should be True, if username and password should be used
61            by subversion, username and password contain the relevant data
62            as strings and save is a flag indicating, that username and
63            password should be saved.
64        """
65        from .SvnLoginDialog import SvnLoginDialog
66
67        with E5OverridenCursor():
68            parent = isinstance(self, QWidget) and self or None
69            dlg = SvnLoginDialog(realm, username, may_save, parent)
70            res = dlg.exec()
71
72        if res == QDialog.DialogCode.Accepted:
73            loginData = dlg.getData()
74            return (True, loginData[0], loginData[1], loginData[2])
75        else:
76            return (False, "", "", False)
77
78    def _clientSslServerTrustPromptCallback(self, trust_dict):
79        """
80        Protected method called by the client to request acceptance for a
81        ssl server certificate.
82
83        @param trust_dict dictionary containing the trust data
84        @return tuple of three values (retcode, acceptedFailures, save).
85            Retcode should be true, if the certificate should be accepted,
86            acceptedFailures should indicate the accepted certificate failures
87            and save should be True, if subversion should save the certificate.
88        """
89        from E5Gui import E5MessageBox
90
91        with E5OverridenCursor():
92            parent = isinstance(self, QWidget) and self or None
93            msgBox = E5MessageBox.E5MessageBox(
94                E5MessageBox.Question,
95                self.tr("Subversion SSL Server Certificate"),
96                self.tr("""<p>Accept the following SSL certificate?</p>"""
97                        """<table>"""
98                        """<tr><td>Realm:</td><td>{0}</td></tr>"""
99                        """<tr><td>Hostname:</td><td>{1}</td></tr>"""
100                        """<tr><td>Fingerprint:</td><td>{2}</td></tr>"""
101                        """<tr><td>Valid from:</td><td>{3}</td></tr>"""
102                        """<tr><td>Valid until:</td><td>{4}</td></tr>"""
103                        """<tr><td>Issuer name:</td><td>{5}</td></tr>"""
104                        """</table>""")
105                    .format(trust_dict["realm"],
106                            trust_dict["hostname"],
107                            trust_dict["finger_print"],
108                            trust_dict["valid_from"],
109                            trust_dict["valid_until"],
110                            trust_dict["issuer_dname"]),
111                modal=True, parent=parent)
112            permButton = msgBox.addButton(self.tr("&Permanent accept"),
113                                          E5MessageBox.AcceptRole)
114            tempButton = msgBox.addButton(self.tr("&Temporary accept"),
115                                          E5MessageBox.AcceptRole)
116            msgBox.addButton(self.tr("&Reject"), E5MessageBox.RejectRole)
117            msgBox.exec()
118
119        if msgBox.clickedButton() == permButton:
120            return (True, trust_dict["failures"], True)
121        elif msgBox.clickedButton() == tempButton:
122            return (True, trust_dict["failures"], False)
123        else:
124            return (False, 0, False)
125
126    def _clientLogCallback(self):
127        """
128        Protected method called by the client to request a log message.
129
130        @return a flag indicating success and the log message (string)
131        """
132        from .SvnCommitDialog import SvnCommitDialog
133        if self.logMessage:
134            return True, self.logMessage
135        else:
136            # call CommitDialog and get message from there
137            dlg = SvnCommitDialog(self)
138            if dlg.exec() == QDialog.DialogCode.Accepted:
139                msg = dlg.logMessage()
140                if msg:
141                    return True, msg
142                else:
143                    return True, "***"  # always supply a valid log message
144            else:
145                return False, ""
146