FreeCAD

Форк
0
/
addonmanager_uninstaller_gui.py 
137 строк · 6.1 Кб
1
# SPDX-License-Identifier: LGPL-2.1-or-later
2
# ***************************************************************************
3
# *                                                                         *
4
# *   Copyright (c) 2022 FreeCAD Project Association                        *
5
# *                                                                         *
6
# *   This file is part of FreeCAD.                                         *
7
# *                                                                         *
8
# *   FreeCAD is free software: you can redistribute it and/or modify it    *
9
# *   under the terms of the GNU Lesser General Public License as           *
10
# *   published by the Free Software Foundation, either version 2.1 of the  *
11
# *   License, or (at your option) any later version.                       *
12
# *                                                                         *
13
# *   FreeCAD is distributed in the hope that it will be useful, but        *
14
# *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
15
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
16
# *   Lesser General Public License for more details.                       *
17
# *                                                                         *
18
# *   You should have received a copy of the GNU Lesser General Public      *
19
# *   License along with FreeCAD. If not, see                               *
20
# *   <https://www.gnu.org/licenses/>.                                      *
21
# *                                                                         *
22
# ***************************************************************************
23

24
"""GUI functions for uninstalling an Addon or Macro."""
25

26
import FreeCAD
27
import FreeCADGui
28

29
from PySide import QtCore, QtWidgets
30

31
from addonmanager_uninstaller import AddonUninstaller, MacroUninstaller
32
import addonmanager_utilities as utils
33

34
translate = FreeCAD.Qt.translate
35

36

37
class AddonUninstallerGUI(QtCore.QObject):
38
    """User interface for uninstalling an Addon: asks for confirmation, displays a progress dialog,
39
    displays completion and/or error dialogs, and emits the finished() signal when all work is
40
    complete."""
41

42
    finished = QtCore.Signal()
43

44
    def __init__(self, addon_to_remove):
45
        super().__init__()
46
        self.addon_to_remove = addon_to_remove
47
        if hasattr(self.addon_to_remove, "macro") and self.addon_to_remove.macro is not None:
48
            self.uninstaller = MacroUninstaller(self.addon_to_remove)
49
        else:
50
            self.uninstaller = AddonUninstaller(self.addon_to_remove)
51
        self.uninstaller.success.connect(self._succeeded)
52
        self.uninstaller.failure.connect(self._failed)
53
        self.worker_thread = QtCore.QThread()
54
        self.uninstaller.moveToThread(self.worker_thread)
55
        self.uninstaller.finished.connect(self.worker_thread.quit)
56
        self.worker_thread.started.connect(self.uninstaller.run)
57
        self.progress_dialog = None
58
        self.dialog_timer = QtCore.QTimer()
59
        self.dialog_timer.timeout.connect(self._show_progress_dialog)
60
        self.dialog_timer.setSingleShot(True)
61
        self.dialog_timer.setInterval(1000)  # Can override from external (e.g. testing) code
62

63
    def run(self):
64
        """Begin the user interaction: asynchronous, only blocks while showing the initial modal
65
        confirmation dialog."""
66
        ok_to_proceed = self._confirm_uninstallation()
67
        if not ok_to_proceed:
68
            self._finalize()
69
            return
70

71
        self.dialog_timer.start()
72
        self._run_uninstaller()
73

74
    def _confirm_uninstallation(self) -> bool:
75
        """Present a modal dialog asking the user if they really want to uninstall. Returns True to
76
        continue with the uninstallation, or False to stop the process."""
77
        confirm = QtWidgets.QMessageBox.question(
78
            utils.get_main_am_window(),
79
            translate("AddonsInstaller", "Confirm remove"),
80
            translate("AddonsInstaller", "Are you sure you want to uninstall {}?").format(
81
                self.addon_to_remove.display_name
82
            ),
83
            QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Cancel,
84
        )
85
        return confirm == QtWidgets.QMessageBox.Yes
86

87
    def _show_progress_dialog(self):
88
        self.progress_dialog = QtWidgets.QMessageBox(
89
            QtWidgets.QMessageBox.NoIcon,
90
            translate("AddonsInstaller", "Removing Addon"),
91
            translate("AddonsInstaller", "Removing {}").format(self.addon_to_remove.display_name)
92
            + "...",
93
            QtWidgets.QMessageBox.Cancel,
94
            parent=utils.get_main_am_window(),
95
        )
96
        self.progress_dialog.rejected.connect(self._cancel_removal)
97
        self.progress_dialog.show()
98

99
    def _run_uninstaller(self):
100
        self.worker_thread.start()
101

102
    def _cancel_removal(self):
103
        """Ask the QThread to interrupt. Probably has no effect, most of the work is in a single OS
104
        call."""
105
        self.worker_thread.requestInterruption()
106

107
    def _succeeded(self, addon):
108
        """Callback for successful removal"""
109
        self.dialog_timer.stop()
110
        if self.progress_dialog:
111
            self.progress_dialog.hide()
112
        QtWidgets.QMessageBox.information(
113
            utils.get_main_am_window(),
114
            translate("AddonsInstaller", "Uninstall complete"),
115
            translate("AddonInstaller", "Finished removing {}").format(addon.display_name),
116
        )
117
        self._finalize()
118

119
    def _failed(self, addon, message):
120
        """Callback for failed or partially failed removal"""
121
        self.dialog_timer.stop()
122
        if self.progress_dialog:
123
            self.progress_dialog.hide()
124
        QtWidgets.QMessageBox.critical(
125
            utils.get_main_am_window(),
126
            translate("AddonsInstaller", "Uninstall failed"),
127
            translate("AddonInstaller", "Failed to remove some files") + ":\n" + message,
128
        )
129
        self._finalize()
130

131
    def _finalize(self):
132
        """Clean up and emit finished signal"""
133
        if self.worker_thread.isRunning():
134
            self.worker_thread.requestInterruption()
135
            self.worker_thread.quit()
136
            self.worker_thread.wait(500)
137
        self.finished.emit()
138

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.