-
Notifications
You must be signed in to change notification settings - Fork 2
/
settings.py
208 lines (148 loc) · 6.52 KB
/
settings.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from gi.repository import Gtk, GLib, GObject, Gio, WebKit2
import os
# Single instance
settings = None
# Auxiliary methods
def plugin_dir():
user_data_dir = os.path.join(GLib.get_user_data_dir(), "gedit")
user_plugin_dir = os.path.join(user_data_dir, "plugins")
return os.path.join(user_plugin_dir, "gedit-markdown")
def css_dir():
return os.path.join(plugin_dir(), "css")
def css_file(var):
return os.path.join(css_dir(), var)
def data_file(var):
return os.path.join(plugin_dir(), var)
def config_file():
path = os.path.join(GLib.get_user_config_dir(), "gedit-markdown")
return os.path.join(path, "settings.conf")
def html_file():
path = os.path.join(plugin_dir(), "data")
return os.path.join(path, "html")
def is_css(f):
return f.endswith(".css")
def check_config_file():
path = os.path.join(GLib.get_user_config_dir(), "gedit-markdown")
if not os.path.exists(path):
os.mkdir(path)
if not os.path.exists(config_file()):
# Create the config file when it doesn't exists
file = open(config_file(), "w+")
file.close()
return False
return True
def get_stylesheet_title(css):
f = open(css, "r")
first_line = f.readline()
return first_line.replace("/*","").replace("*/","").strip()
# MdSettings
class MdSettings(GObject.Object):
__gsignals__ = {
"css-file-selected": (GObject.SIGNAL_RUN_FIRST, None, ()),
"show-preview-window": (GObject.SIGNAL_RUN_FIRST, None, ()),
"margin-changed": (GObject.SIGNAL_RUN_FIRST, None, ())
}
def __init__(self):
GObject.Object.__init__(self)
# Builder
self.ui = Gtk.Builder()
self.ui.add_from_file(data_file("settings.ui"))
# Dialog
self.dialog = self.ui.get_object('settings_dialog')
self.dialog.set_modal(True)
self.listbox = self.ui.get_object('listbox')
self.listbox.connect("row-activated", self.on_listbox_row_activated)
# Preview window
self.preview_window = self.ui.get_object('preview_window')
self.preview_window_scroll = self.ui.get_object('preview_scroll')
# CSS files
self.css_files = [f for f in os.listdir(css_dir()) if is_css(os.path.join(css_dir(), f))]
self.update_css_list()
# Spin
self.margin = self.ui.get_object('margin_spin')
# Buttons
self.save_button = self.ui.get_object('save_button')
self.cancel_button = self.ui.get_object('cancel_button')
# Switches
self.auto_switch = self.ui.get_object('auto_switch')
self.preview_switch = self.ui.get_object('attached_switch')
# Signals
self.connect_signals()
# Settings file
file_exists = check_config_file()
self.key_file = GLib.KeyFile()
self.key_file.load_from_file(config_file(), GLib.KeyFileFlags.NONE)
# Update dialog preferences
if file_exists:
self.auto_switch.set_active(self.key_file.get_boolean("Config", "AutoPreview"))
self.preview_switch.set_active(self.key_file.get_boolean("Config", "ShowPreviewWindow"))
self.margin.set_value(self.key_file.get_integer("Config", "Margin"))
else:
self.key_file.set_boolean("Config", "AutoPreview", False)
self.key_file.set_boolean("Config", "ShowPreviewWindow", False)
self.key_file.set_integer("Config", "Margin", self.margin.get_value_as_int())
self.key_file.set_string("Config", "CSS", css_file("github.css"))
self.key_file.save_to_file(config_file())
def connect_signals(self):
self.save_button.connect("clicked", self.on_dialog_button_clicked)
self.cancel_button.connect("clicked", self.on_dialog_button_clicked)
self.preview_switch.connect("notify::active", self.on_preview_switch_activate)
self.margin.connect("changed", self.on_margin_changed)
def show_settings_window(self, parent):
self.dialog.run()
def show_preview_window(self):
self.preview_window.show()
def on_dialog_button_clicked(self, button):
# Save config contents
if button is self.save_button:
self.key_file.set_boolean("Config", "AutoPreview", self.auto_switch.get_active())
self.key_file.set_boolean("Config", "ShowPreviewWindow", self.preview_switch.get_active())
self.key_file.set_integer("Config", "Margin", self.margin.get_value_as_int())
self.key_file.save_to_file(config_file())
self.dialog.hide();
def update_css_list(self):
for css_file in self.css_files:
css_entry = CssListItem()
css_entry.name.set_text(get_stylesheet_title(os.path.join(css_dir(), css_file)))
css_entry.filename.set_text(css_file)
css_entry.show()
self.listbox.add(css_entry)
def on_listbox_row_activated(self, unused_widget, row):
self.key_file.set_string("Config", "CSS", css_file(row.filename.get_text()))
self.emit("css-file-selected")
# Get AutoPreview option
def get_autopreview(self):
return self.auto_switch.get_active()
def get_show_preview_window(self):
return self.preview_switch.get_active()
def get_window_webview(self):
return self.webview
# Get base html content
def get_html(self):
f = open(html_file(), "r")
html_content = f.read()
f.close()
return html_content
def on_preview_switch_activate(self, unused_switch, unused_data):
self.emit("show-preview-window")
def on_margin_changed(self, unused_switch):
self.emit("margin-changed")
# Get selected CSS content
def get_css(self):
css_f = self.key_file.get_string("Config", "CSS")
if css_f is None:
return ""
path = css_file(css_f)
css = open(path, "r")
css_content = css.read()
css.close()
return css_content
# CssList item entry
class CssListItem(Gtk.ListBoxRow):
def __init__(self):
Gtk.ListBoxRow.__init__(self)
self.ui = Gtk.Builder()
self.ui.add_from_file(data_file("settings.ui"))
self.add (self.ui.get_object('listbox_child'))
self.name = self.ui.get_object('name_label')
self.filename = self.ui.get_object('filename_label')