1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2013 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the .desktop wizard plug-in.
8"""
9
10from PyQt5.QtCore import QObject
11from PyQt5.QtWidgets import QDialog
12
13from E5Gui.E5Application import e5App
14from E5Gui.E5Action import E5Action
15from E5Gui import E5MessageBox
16
17import UI.Info
18
19# Start-of-Header
20name = ".desktop Wizard Plug-in"
21author = "Detlev Offenbach <detlev@die-offenbachs.de>"
22autoactivate = True
23deactivateable = True
24version = UI.Info.VersionOnly
25className = "DotDesktopWizard"
26packageName = "__core__"
27shortDescription = "Wizard for the creation of a .desktop file."
28longDescription = (
29    """This plug-in implements a wizard to generate code for"""
30    """ a .desktop file."""
31)
32needsRestart = False
33pyqtApi = 2
34# End-of-Header
35
36error = ""
37
38
39class DotDesktopWizard(QObject):
40    """
41    Class implementing the .desktop wizard plug-in.
42    """
43    def __init__(self, ui):
44        """
45        Constructor
46
47        @param ui reference to the user interface object (UI.UserInterface)
48        """
49        super().__init__(ui)
50        self.__ui = ui
51        self.__action = None
52
53    def __initialize(self):
54        """
55        Private slot to (re)initialize the plug-in.
56        """
57        self.__act = None
58
59    def activate(self):
60        """
61        Public method to activate this plug-in.
62
63        @return tuple of None and activation status (boolean)
64        """
65        self.__initAction()
66        self.__initMenu()
67
68        return None, True
69
70    def deactivate(self):
71        """
72        Public method to deactivate this plug-in.
73        """
74        menu = self.__ui.getMenu("wizards")
75        if menu:
76            menu.removeAction(self.__action)
77        self.__ui.removeE5Actions([self.__action], 'wizards')
78
79    def __initAction(self):
80        """
81        Private method to initialize the action.
82        """
83        self.__action = E5Action(
84            self.tr('.desktop Wizard'),
85            self.tr('.&desktop Wizard...'),
86            0, 0, self,
87            'wizards_dotdesktop')
88        self.__action.setStatusTip(self.tr('.desktop Wizard'))
89        self.__action.setWhatsThis(self.tr(
90            """<b>.desktop Wizard</b>"""
91            """<p>This wizard opens a dialog for entering all the parameters"""
92            """ needed to create the contents of a .desktop file. The"""
93            """ generated code replaces the text of the current editor."""
94            """ Alternatively a new editor is opened.</p>"""
95        ))
96        self.__action.triggered.connect(self.__handle)
97
98        self.__ui.addE5Actions([self.__action], 'wizards')
99
100    def __initMenu(self):
101        """
102        Private method to add the actions to the right menu.
103        """
104        menu = self.__ui.getMenu("wizards")
105        if menu:
106            menu.addAction(self.__action)
107
108    def __handle(self):
109        """
110        Private method to handle the wizards action.
111        """
112        editor = e5App().getObject("ViewManager").activeWindow()
113
114        if editor is None:
115            E5MessageBox.critical(
116                self.__ui,
117                self.tr('No current editor'),
118                self.tr('Please open or create a file first.'))
119        else:
120            if editor.text():
121                ok = E5MessageBox.yesNo(
122                    self.__ui,
123                    self.tr(".desktop Wizard"),
124                    self.tr("""The current editor contains text."""
125                            """ Shall this be replaced?"""),
126                    icon=E5MessageBox.Critical)
127                if not ok:
128                    e5App().getObject("ViewManager").newEditor()
129                    editor = e5App().getObject("ViewManager").activeWindow()
130
131            from WizardPlugins.DotDesktopWizard.DotDesktopWizardDialog import (
132                DotDesktopWizardDialog
133            )
134            dlg = DotDesktopWizardDialog(None)
135            if dlg.exec() == QDialog.DialogCode.Accepted:
136                code = dlg.getCode()
137                if code:
138                    editor.selectAll()
139                    # It should be done on this way to allow undo
140                    editor.beginUndoAction()
141                    editor.replaceSelectedText(code)
142                    editor.endUndoAction()
143
144                    editor.setLanguage("dummy.desktop")
145
146#
147# eflag: noqa = M801
148