1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2007 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the PySvn version control plugin.
8"""
9
10import os
11import contextlib
12
13from PyQt5.QtCore import QObject, QCoreApplication
14
15from E5Gui.E5Application import e5App
16
17import Preferences
18from Preferences.Shortcuts import readShortcuts
19import UI.Info
20
21from VcsPlugins.vcsPySvn.SvnUtilities import getConfigPath, getServersPath
22
23# Start-Of-Header
24name = "PySvn Plugin"
25author = "Detlev Offenbach <detlev@die-offenbachs.de>"
26autoactivate = False
27deactivateable = True
28version = UI.Info.VersionOnly
29pluginType = "version_control"
30pluginTypename = "PySvn"
31className = "VcsPySvnPlugin"
32packageName = "__core__"
33shortDescription = "Implements the PySvn version control interface."
34longDescription = (
35    """This plugin provides the PySvn version control interface."""
36)
37pyqtApi = 2
38# End-Of-Header
39
40error = ""
41
42
43def exeDisplayData():
44    """
45    Public method to support the display of some executable info.
46
47    @return dictionary containing the data to be shown
48    """
49    try:
50        import pysvn
51        try:
52            text = os.path.dirname(pysvn.__file__)
53        except AttributeError:
54            text = "PySvn"
55        version = ".".join([str(v) for v in pysvn.version])
56    except ImportError:
57        text = "PySvn"
58        version = ""
59
60    data = {
61        "programEntry": False,
62        "header": QCoreApplication.translate(
63            "VcsPySvnPlugin", "Version Control - Subversion (pysvn)"),
64        "text": text,
65        "version": version,
66    }
67
68    return data
69
70
71def getVcsSystemIndicator():
72    """
73    Public function to get the indicators for this version control system.
74
75    @return dictionary with indicator as key and a tuple with the vcs name
76        (string) and vcs display string (string)
77    """
78    global pluginTypename
79    data = {}
80    data[".svn"] = (pluginTypename, displayString())
81    data["_svn"] = (pluginTypename, displayString())
82    return data
83
84
85def displayString():
86    """
87    Public function to get the display string.
88
89    @return display string (string)
90    """
91    try:
92        import pysvn        # __IGNORE_WARNING__
93        return QCoreApplication.translate('VcsPySvnPlugin',
94                                          'Subversion (pysvn)')
95    except ImportError:
96        return ""
97
98subversionCfgPluginObject = None
99
100
101def createConfigurationPage(configDlg):
102    """
103    Module function to create the configuration page.
104
105    @param configDlg reference to the configuration dialog (QDialog)
106    @return reference to the configuration page
107    """
108    global subversionCfgPluginObject
109    from VcsPlugins.vcsPySvn.ConfigurationPage.SubversionPage import (
110        SubversionPage
111    )
112    if subversionCfgPluginObject is None:
113        subversionCfgPluginObject = VcsPySvnPlugin(None)
114    page = SubversionPage(subversionCfgPluginObject)
115    return page
116
117
118def getConfigData():
119    """
120    Module function returning data as required by the configuration dialog.
121
122    @return dictionary with key "zzz_subversionPage" containing the relevant
123    data
124    """
125    return {
126        "zzz_subversionPage":
127        [QCoreApplication.translate("VcsPySvnPlugin", "Subversion"),
128         os.path.join("VcsPlugins", "vcsPySvn", "icons",
129                      "preferences-subversion.svg"),
130         createConfigurationPage, "vcsPage", None],
131    }
132
133
134def prepareUninstall():
135    """
136    Module function to prepare for an uninstallation.
137    """
138    if not e5App().getObject("PluginManager").isPluginLoaded(
139            "PluginVcsSubversion"):
140        Preferences.Prefs.settings.remove("Subversion")
141
142
143class VcsPySvnPlugin(QObject):
144    """
145    Class implementing the PySvn version control plugin.
146    """
147    def __init__(self, ui):
148        """
149        Constructor
150
151        @param ui reference to the user interface object (UI.UserInterface)
152        """
153        super().__init__(ui)
154        self.__ui = ui
155
156        self.__subversionDefaults = {
157            "StopLogOnCopy": 1,
158            "LogLimit": 20,
159            "CommitMessages": 20,
160        }
161
162        from VcsPlugins.vcsPySvn.ProjectHelper import PySvnProjectHelper
163        self.__projectHelperObject = PySvnProjectHelper(None, None)
164        with contextlib.suppress(KeyError):
165            e5App().registerPluginObject(
166                pluginTypename, self.__projectHelperObject, pluginType)
167        readShortcuts(pluginName=pluginTypename)
168
169    def getProjectHelper(self):
170        """
171        Public method to get a reference to the project helper object.
172
173        @return reference to the project helper object
174        """
175        return self.__projectHelperObject
176
177    def initToolbar(self, ui, toolbarManager):
178        """
179        Public slot to initialize the VCS toolbar.
180
181        @param ui reference to the main window (UserInterface)
182        @param toolbarManager reference to a toolbar manager object
183            (E5ToolBarManager)
184        """
185        if self.__projectHelperObject:
186            self.__projectHelperObject.initToolbar(ui, toolbarManager)
187
188    def activate(self):
189        """
190        Public method to activate this plugin.
191
192        @return tuple of reference to instantiated viewmanager and
193            activation status (boolean)
194        """
195        from VcsPlugins.vcsPySvn.subversion import Subversion
196        self.__object = Subversion(self, self.__ui)
197
198        tb = self.__ui.getToolbar("vcs")[1]
199        tb.setVisible(False)
200        tb.setEnabled(False)
201
202        tb = self.__ui.getToolbar("pysvn")[1]
203        tb.setVisible(Preferences.getVCS("ShowVcsToolbar"))
204        tb.setEnabled(True)
205
206        return self.__object, True
207
208    def deactivate(self):
209        """
210        Public method to deactivate this plugin.
211        """
212        self.__object = None
213
214        tb = self.__ui.getToolbar("pysvn")[1]
215        tb.setVisible(False)
216        tb.setEnabled(False)
217
218        tb = self.__ui.getToolbar("vcs")[1]
219        tb.setVisible(Preferences.getVCS("ShowVcsToolbar"))
220        tb.setEnabled(True)
221
222    def getPreferences(self, key):
223        """
224        Public method to retrieve the various settings.
225
226        @param key the key of the value to get
227        @return the requested refactoring setting
228        """
229        if key in ["StopLogOnCopy"]:
230            return Preferences.toBool(Preferences.Prefs.settings.value(
231                "Subversion/" + key, self.__subversionDefaults[key]))
232        elif key in ["LogLimit", "CommitMessages"]:
233            return int(Preferences.Prefs.settings.value(
234                "Subversion/" + key,
235                self.__subversionDefaults[key]))
236        elif key in ["Commits"]:
237            return Preferences.toList(Preferences.Prefs.settings.value(
238                "Subversion/" + key))
239        else:
240            return Preferences.Prefs.settings.value("Subversion/" + key)
241
242    def setPreferences(self, key, value):
243        """
244        Public method to store the various settings.
245
246        @param key the key of the setting to be set
247        @param value the value to be set
248        """
249        Preferences.Prefs.settings.setValue("Subversion/" + key, value)
250
251    def getServersPath(self):
252        """
253        Public method to get the filename of the servers file.
254
255        @return filename of the servers file (string)
256        """
257        return getServersPath()
258
259    def getConfigPath(self):
260        """
261        Public method to get the filename of the config file.
262
263        @return filename of the config file (string)
264        """
265        return getConfigPath()
266
267    def prepareUninstall(self):
268        """
269        Public method to prepare for an uninstallation.
270        """
271        e5App().unregisterPluginObject(pluginTypename)
272
273    def prepareUnload(self):
274        """
275        Public method to prepare for an unload.
276        """
277        if self.__projectHelperObject:
278            self.__projectHelperObject.removeToolbar(
279                self.__ui, e5App().getObject("ToolbarManager"))
280        e5App().unregisterPluginObject(pluginTypename)
281