/
__init__.py
120 lines (98 loc) · 4.02 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Save all files when enki loses focus.
"""
__author__ = "Marco Laspe"
__pluginname__ = "Autosave"
__copyright__ = "Copyright 2017"
__credits__ = ["Marco Laspe", "Andrei Kopats", "Filipe Azevedo", "Bryan A. Jones"]
__license__ = "GPL3"
__version__ = "0.0.1"
__maintainer__ = "Marco Laspe"
__email__ = "marco@rockiger.com"
__status__ = "Beta"
import os
from PyQt5.QtWidgets import (QApplication, QWidget, QCheckBox, QVBoxLayout,
QSpacerItem, QSizePolicy, QLabel)
from enki.core.core import core
from enki.core.uisettings import CheckableOption
# DONE Setting page
# DONE checkbox if autosave is activated,
# DONE Cleanup code (Terminate plugin, etc.)
# DONE Save only if file is changed
# DONE Save only if file is writable, else warn the user.
class SettingsPage(QWidget):
"""Settings page for Autosave plugi"""
def __init__(self, parent):
QWidget.__init__(self, parent)
text = """
<h2>Autosave</h2>
<p> The Autosave plugin saves your files, if Enki looses focus.</p>
<p></p>"""
self._label = QLabel(text, self)
self.checkbox = QCheckBox('Enable Autosave')
self._layout = QVBoxLayout(self)
self._layout.addWidget(self._label)
self._layout.addWidget(self.checkbox)
self._layout.addSpacerItem(QSpacerItem(0, 0,
QSizePolicy.MinimumExpanding,
QSizePolicy.MinimumExpanding))
class Plugin:
"""Plugin interface implementation
The plugin looks for focusWindowChanged event, get all opened files
and saves them.
"""
def __init__(self):
"""Connect to QApplication OnFocusChanged event"""
self._qapplication = QApplication.instance()
self._qapplication.focusWindowChanged.connect(self._onFocusChanged)
self._qmainwindow = core.mainWindow()
core.aboutToTerminate.connect(self._onClose)
#self._checkSettings()
#core.uiSettingsManager().aboutToExecute.connect(
# self._onSettingsDialogAboutToExecute)
def terminate(self):
"""clean up"""
pass
#core.uiSettingsManager().aboutToExecute.disconnect(
# self._onSettingsDialogAboutToExecute)
def _onFocusChanged(self, newWindow):
"""Tests if no window is focused"""
#if newWindow is None and self._isAutoSaveActive():
self._saveFiles()
def _onClose(self, qobject):
"""Save files on close of main window"""
self._saveFiles()
def _saveFiles(self):
"""Saves all open files"""
for document in core.workspace().documents():
if document.qutepart.document().isModified():
try:
if not os.access(document.filePath(), os.W_OK):
core.mainWindow().appendMessage(
"Can not save file '%s', because it is not writable!" %
document.filePath(), 4000)
else:
document.saveFile()
except TypeError:
pass # we don't have a path, so don't do anything
def _onSettingsDialogAboutToExecute(self, dialog):
"""UI settings dialogue is about to execute.
Add own options
"""
page = SettingsPage(dialog)
dialog.appendPage(u"Autosave", page)
# Options
dialog.appendOption(CheckableOption(dialog, core.config(),
"Autosave/Active",
page.checkbox))
def _checkSettings(self):
"""Check if settings are present in the core configuration file,
else create and return them.
"""
if "Autosave" not in core.config():
core.config()["Autosave"] = {}
core.config()["Autosave"]["Active"] = False
return core.config()["Autosave"]["Active"]
def _isAutoSaveActive(self):
"""Return if autosave is enabled"""
return core.config()["Autosave"]["Active"]