-
Notifications
You must be signed in to change notification settings - Fork 107
/
options.py
404 lines (316 loc) · 13.3 KB
/
options.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# -----------------------------------------------------------------------
# Copyright (c) 2008-2024 Jendrik Seipp
#
# RedNotebook is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# RedNotebook is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with RedNotebook; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# -----------------------------------------------------------------------
import logging
import os
import platform
from gi.repository import Gtk
from rednotebook import info
from rednotebook.configuration import Config
from rednotebook.gui import editor
from rednotebook.gui.customwidgets import ActionButton, CustomComboBoxEntry, UrlButton
from rednotebook.util import dates, filesystem, utils
class Option(Gtk.HBox):
def __init__(self, text, option_name, tooltip=""):
Gtk.HBox.__init__(self)
self.text = text
self.option_name = option_name
self.set_spacing(5)
self.label = Gtk.Label(label=self.text)
self.pack_start(self.label, False, False, 0)
if tooltip:
self.set_tooltip_text(tooltip)
def get_value(self):
raise NotImplementedError
class TickOption(Option):
def __init__(self, text, name, value=None, tooltip=""):
Option.__init__(self, "", name, tooltip=tooltip)
self.check_button = Gtk.CheckButton(text)
if value is None:
self.check_button.set_active(Option.config.read(name) == 1)
else:
self.check_button.set_active(value)
self.check_button.connect("clicked", self.on_check_button_clicked)
self.pack_start(self.check_button, False, False, 0)
def on_check_button_clicked(self, widget):
pass
# TODO: Apply corresponding actions.
def get_value(self):
"""
We use 0 and 1 internally for bool options
"""
return int(self.check_button.get_active())
class AutostartOption(TickOption):
def __init__(self):
self.autostart_file = os.path.expanduser(
"~/.config/autostart/rednotebook.desktop"
)
autostart_file_exists = os.path.exists(self.autostart_file)
TickOption.__init__(
self, _("Load RedNotebook at startup"), None, value=autostart_file_exists
)
def get_value(self):
return self.check_button.get_active()
def set(self):
"""Apply the current setting"""
if self.get_value():
# Add autostart file if it is not present
filesystem.make_file_with_dir(self.autostart_file, info.desktop_file)
elif os.path.exists(self.autostart_file):
os.remove(self.autostart_file)
class TextOption(Option):
def __init__(self, text, option_name, default="", **kwargs):
Option.__init__(self, text, option_name, **kwargs)
# directly read the string, not the list
value = Option.config.read(option_name, default)
# Ensure that we have a string here
value = str(value)
self.entry = Gtk.Entry()
self.entry.set_text(value)
self.pack_start(self.entry, True, True, 0)
def get_value(self):
return self.entry.get_text()
class IntegerOption(Option):
def __init__(
self,
text,
option_name,
default=0,
min_value=0,
max_value=1000,
increment=1,
**kwargs,
):
Option.__init__(self, text, option_name, **kwargs)
value = Option.config.read(option_name, default=default)
value = int(value)
self.spin_button = Gtk.SpinButton()
adjustment = Gtk.Adjustment(value, min_value, max_value, increment, 10, 0)
self.spin_button.set_adjustment(adjustment)
self.spin_button.set_value(value)
self.spin_button.set_numeric(True)
self.spin_button.set_update_policy(Gtk.SpinButtonUpdatePolicy.IF_VALID)
self.pack_start(self.spin_button, True, True, 0)
def get_value(self):
return self.spin_button.get_value_as_int()
class ComboBoxOption(Option):
def __init__(self, text, name, entries, tooltip=""):
Option.__init__(self, text, name, tooltip=tooltip)
self.combo = CustomComboBoxEntry(Gtk.ComboBox.new_with_entry())
self.combo.set_entries(entries)
self.pack_start(self.combo.combo_box, False, False, 0)
def get_value(self):
return self.combo.get_active_text()
class DateFormatOption(ComboBoxOption):
def __init__(self, text, name, tooltip):
date_formats = [
"%A, %x %X",
_("%A, %x, Day %j"),
"%H:%M",
_("Week %W of Year %Y"),
"%y-%m-%d",
_("Day %j"),
"%A",
"%B",
]
ComboBoxOption.__init__(self, text, name, date_formats, tooltip=tooltip)
date_url = "http://docs.python.org/library/time.html#time.strftime"
date_format_help_button = UrlButton(_("Help"), date_url)
self.preview = Gtk.Label()
self.pack_start(self.preview, False, False, 0)
self.pack_end(date_format_help_button, False, False, 0)
# Set default format if not present
format = Option.config.read(name, "%A, %x %X")
format = str(format)
self.combo.set_active_text(format)
self.combo.combo_box.connect("changed", self.on_format_changed)
# Update the preview
self.on_format_changed(None)
def on_format_changed(self, widget):
format_string = self.get_value()
date_string = dates.format_date(format_string)
# Translators: Noun
label_text = f'{_("Preview:")} {date_string}'
self.preview.set_text(label_text)
class FontOption(Option):
def __init__(self, text, name):
Option.__init__(self, text, name, "")
self.dialog = None
self.font_name = Option.config.read(name, editor.DEFAULT_FONT)
self.label = Gtk.Label()
self.label.set_text(self.font_name)
self.button = Gtk.Button(_("Choose font ..."))
self.button.connect("clicked", self.on_button_clicked)
self.pack_start(self.label, False, False, 0)
self.pack_start(self.button, False, False, 0)
def on_button_clicked(self, widget):
if not self.dialog:
self.dialog = Gtk.FontSelectionDialog(_("Choose font"))
self.dialog.set_font_name(self.font_name)
self.dialog.set_modal(True)
self.dialog.set_transient_for(
Option.main_window.options_manager.dialog.dialog
)
self.dialog.connect("destroy", self.dialog_destroyed)
self.dialog.get_ok_button().connect("clicked", self.font_selection_ok)
self.dialog.get_cancel_button().connect_object(
"clicked", lambda window: window.destroy(), self.dialog
)
self.dialog.show()
def dialog_destroyed(self, widget):
self.dialog = None
def font_selection_ok(self, widget):
self.font_name = self.dialog.get_font_name()
self.label.set_text(self.font_name)
Option.main_window.set_font(self.font_name)
self.dialog.destroy()
def get_value(self):
return self.font_name
class OptionsDialog:
def __init__(self, dialog):
self.dialog = dialog
self.categories = {}
def __getattr__(self, attr):
"""Wrap the dialog"""
return getattr(self.dialog, attr)
def add_option(self, category, option):
self.categories[category].pack_start(option, False, False, 0)
option.show_all()
def add_category(self, name, vbox):
self.categories[name] = vbox
def clear(self):
for vbox in self.categories.values():
for option in vbox.get_children():
vbox.remove(option)
class OptionsManager:
def __init__(self, main_window):
self.main_window = main_window
self.builder = main_window.builder
self.journal = main_window.journal
self.config = self.journal.config
self.dialog = OptionsDialog(self.builder.get_object("options_dialog"))
self.dialog.set_transient_for(self.main_window.main_frame)
self.dialog.set_default_size(600, 300)
self.dialog.add_category("general", self.builder.get_object("general_vbox"))
def on_options_dialog(self):
self.dialog.clear()
# Make the config globally available
Option.config = self.config
Option.main_window = self.main_window
self.options = []
if platform.system() == "Linux" and os.path.exists("/usr/bin/rednotebook"):
logging.debug("Running on Linux. Is installed. Adding autostart option")
self.options.insert(0, AutostartOption())
# Most modern Linux distributions do not have a systray anymore.
# If this option is activated on a system without a systray, the
# application keeps on running in the background after it has been
# closed. The option can still be activated in the configuration file.
if filesystem.has_system_tray():
self.options.append(
TickOption(
_("Close to system tray"),
"closeToTray",
tooltip=_("Closing the window will send RedNotebook to the tray"),
)
)
# Automatic switching between preview and edit mode.
self.options.append(
TickOption(
_("Switch between edit and preview mode automatically"),
"autoSwitchMode",
)
)
# Check for new version
check_version_option = TickOption(
_("Check for new version at startup"), "checkForNewVersion"
)
self.options.append(TickOption(_("Search as you type"), "instantSearch"))
def check_version_action(widget):
utils.check_new_version(
self.main_window.journal, info.version, startup=False
)
# Apply changes from dialog to options window
check = bool(self.journal.config.read("checkForNewVersion"))
check_version_option.check_button.set_active(check)
check_version_button = ActionButton(_("Check now"), check_version_action)
check_version_option.pack_start(check_version_button, False, False, 0)
self.options.append(check_version_option)
self.options.extend(
[
# Use separate fonts since the preview often doesn't support the edit font.
FontOption(_("Edit font:"), "mainFont"),
TextOption(
_("Preview font:"),
"previewFont",
default=Config.defaults["previewFont"],
tooltip=_("Comma-separated font names"),
),
DateFormatOption(
_("Date/Time format"),
"dateTimeString",
tooltip=_("Used by Date/Time button and $date$ template macro."),
),
DateFormatOption(
_("Date format"),
"exportDateFormat",
tooltip=_("Used for dates in titlebar and exports."),
),
IntegerOption(
_("Tags in cloud"),
"cloudMaxTags",
tooltip=_("Maximum number of tags displayed in the cloud"),
),
TextOption(
_("Exclude from cloud"),
"cloudIgnoreList",
tooltip=_(
"Do not show these comma separated words and #tags in the clouds"
),
),
TextOption(
_("Include small words in cloud"),
"cloudIncludeList",
tooltip=_("Allow these words with 4 letters or less"),
),
]
)
self.add_all_options()
response = self.dialog.run()
if response == Gtk.ResponseType.OK:
self.save_options()
# Apply some options
self.main_window.cloud.update_lists()
self.main_window.cloud.update(force_update=True)
visible = self.config.read("closeToTray") == 1
self.main_window.tray_icon.set_visible(visible)
else:
# Reset some options
self.main_window.set_font(self.config.read("mainFont", editor.DEFAULT_FONT))
self.dialog.hide()
def add_all_options(self):
for option in self.options:
self.dialog.add_option("general", option)
def save_options(self):
logging.debug("Saving Options")
for option in self.options:
value = option.get_value()
if option.option_name is not None:
logging.debug(f"Setting {option.option_name} = {repr(value)}")
self.config[option.option_name] = value
else:
# We don't save the autostart setting in the config file
option.set()