1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing a dialog to show the output of a pysvn action.
8"""
9
10import pysvn
11
12from PyQt5.QtCore import Qt
13from PyQt5.QtWidgets import QDialog, QApplication, QDialogButtonBox
14
15from .SvnConst import svnNotifyActionMap
16
17from .SvnDialogMixin import SvnDialogMixin
18from .Ui_SvnDialog import Ui_SvnDialog
19
20import Preferences
21
22
23class SvnDialog(QDialog, SvnDialogMixin, Ui_SvnDialog):
24    """
25    Class implementing a dialog to show the output of a pysvn action.
26    """
27    def __init__(self, text, command, pysvnClient, parent=None, log=""):
28        """
29        Constructor
30
31        @param text text to be shown by the label (string)
32        @param command svn command to be executed (display purposes only)
33            (string)
34        @param pysvnClient reference to the pysvn client object (pysvn.Client)
35        @param parent parent widget (QWidget)
36        @param log optional log message (string)
37        """
38        super().__init__(parent)
39        self.setupUi(self)
40        SvnDialogMixin.__init__(self, log)
41        self.setWindowFlags(Qt.WindowType.Window)
42
43        self.buttonBox.button(
44            QDialogButtonBox.StandardButton.Close).setEnabled(False)
45        self.buttonBox.button(
46            QDialogButtonBox.StandardButton.Cancel).setDefault(True)
47
48        self.outputGroup.setTitle(text)
49        self.errorGroup.hide()
50
51        pysvnClient.callback_cancel = self._clientCancelCallback
52
53        pysvnClient.callback_notify = self._clientNotifyCallback
54        pysvnClient.callback_get_login = self._clientLoginCallback
55        pysvnClient.callback_ssl_server_trust_prompt = (
56            self._clientSslServerTrustPromptCallback
57        )
58        pysvnClient.callback_get_log_message = self._clientLogCallback
59
60        self.__hasAddOrDelete = False
61
62        if command:
63            self.resultbox.append(command)
64            self.resultbox.append('')
65
66        self.show()
67        QApplication.processEvents()
68
69    def _clientNotifyCallback(self, eventDict):
70        """
71        Protected method called by the client to send events.
72
73        @param eventDict dictionary containing the notification event
74        """
75        msg = ""
76        if eventDict["action"] == pysvn.wc_notify_action.update_completed:
77            msg = self.tr("Revision {0}.\n").format(
78                eventDict["revision"].number)
79        elif (
80            eventDict["path"] != "" and
81            eventDict["action"] in svnNotifyActionMap and
82            svnNotifyActionMap[eventDict["action"]] is not None
83        ):
84            mime = (
85                eventDict["mime_type"] == "application/octet-stream" and
86                self.tr(" (binary)") or ""
87            )
88            msg = self.tr("{0} {1}{2}\n").format(
89                self.tr(svnNotifyActionMap[eventDict["action"]]),
90                eventDict["path"],
91                mime)
92            if (
93                '.epj' in eventDict["path"] or
94                '.e4p' in eventDict["path"]
95            ):
96                self.__hasAddOrDelete = True
97            if eventDict["action"] in [pysvn.wc_notify_action.add,
98                                       pysvn.wc_notify_action.commit_added,
99                                       pysvn.wc_notify_action.commit_deleted,
100                                       pysvn.wc_notify_action.delete,
101                                       pysvn.wc_notify_action.update_add,
102                                       pysvn.wc_notify_action.update_delete]:
103                self.__hasAddOrDelete = True
104        if msg:
105            self.showMessage(msg)
106
107    def showMessage(self, msg):
108        """
109        Public slot to show a message.
110
111        @param msg message to show (string)
112        """
113        self.resultbox.insertPlainText(msg)
114        self.resultbox.ensureCursorVisible()
115        QApplication.processEvents()
116
117    def showError(self, msg):
118        """
119        Public slot to show an error message.
120
121        @param msg error message to show (string)
122        """
123        self.errorGroup.show()
124        self.errors.insertPlainText(msg)
125        self.errors.ensureCursorVisible()
126        QApplication.processEvents()
127
128    def finish(self):
129        """
130        Public slot called when the process finished or the user pressed the
131        button.
132        """
133        self.buttonBox.button(
134            QDialogButtonBox.StandardButton.Close).setEnabled(True)
135        self.buttonBox.button(
136            QDialogButtonBox.StandardButton.Cancel).setEnabled(False)
137        self.buttonBox.button(
138            QDialogButtonBox.StandardButton.Close).setDefault(True)
139
140        self._cancel()
141
142        if Preferences.getVCS("AutoClose"):
143            self.accept()
144
145    def on_buttonBox_clicked(self, button):
146        """
147        Private slot called by a button of the button box clicked.
148
149        @param button button that was clicked (QAbstractButton)
150        """
151        if button == self.buttonBox.button(
152            QDialogButtonBox.StandardButton.Close
153        ):
154            self.close()
155        elif button == self.buttonBox.button(
156            QDialogButtonBox.StandardButton.Cancel
157        ):
158            self.finish()
159
160    def hasAddOrDelete(self):
161        """
162        Public method to check, if the last action contained an add or delete.
163
164        @return flag indicating the presence of an add or delete (boolean)
165        """
166        return self.__hasAddOrDelete
167