forked from neffo/bing-wallpaper-gnome-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefs.js
237 lines (207 loc) · 11 KB
/
prefs.js
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
// Bing Wallpaper GNOME extension
// Copyright (C) 2017-2021 Michael Carroll
// This extension is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// See the GNU General Public License, version 3 or later for details.
// Based on GNOME shell extension NASA APOD by Elia Argentieri https://github.com/Elinvention/gnome-shell-extension-nasa-apod
const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const GdkPixbuf = imports.gi.GdkPixbuf;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const Lang = imports.lang;
const Convenience = Me.imports.convenience;
const Gettext = imports.gettext.domain('BingWallpaper');
const _ = Gettext.gettext;
let settings;
let marketDescription = null;
let icon_image = null;
let lastreq = null;
let provider = new Gtk.CssProvider();
const BingImageURL = Utils.BingImageURL;
function init() {
settings = Utils.getSettings(Me);
Convenience.initTranslations("BingWallpaper");
}
function buildPrefsWidget() {
// Prepare labels and controls
let buildable = new Gtk.Builder();
if (Gtk.get_major_version() == 4) { // GTK4 removes some properties, and builder breaks when it sees them
buildable.add_from_file( Me.dir.get_path() + '/Settings4.ui' );
/* // CSS not yet used
provider.load_from_path(Me.dir.get_path() + '/prefs.css');
Gtk.StyleContext.add_provider_for_display(
Gdk.Display.get_default(),
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); */
}
else {
buildable.add_from_file( Me.dir.get_path() + '/Settings.ui' );
}
let box = buildable.get_object('prefs_widget');
buildable.get_object('extension_version').set_text(Me.metadata.version.toString());
buildable.get_object('extension_name').set_text(Me.metadata.name.toString());
let hideSwitch = buildable.get_object('hide');
let iconEntry = buildable.get_object('icon');
let notifySwitch = buildable.get_object('notify');
let bgSwitch = buildable.get_object('background');
let lsSwitch = buildable.get_object('lock_screen');
let fileChooserBtn = buildable.get_object('download_folder');
let fileChooser = buildable.get_object('file_chooser'); // this should only exist on Gtk4
let folderOpenBtn = buildable.get_object('button_open_download_folder');
let marketEntry = buildable.get_object('market');
let resolutionEntry = buildable.get_object('resolution');
let historyEntry = buildable.get_object('history');
let deleteSwitch = buildable.get_object('delete_previous');
marketDescription = buildable.get_object('market_description');
icon_image = buildable.get_object('icon_image');
let overrideSwitch = buildable.get_object('lockscreen_override');
let strengthEntry = buildable.get_object('entry_strength');
let brightnessEntry = buildable.get_object('entry_brightness');
let debugSwitch = buildable.get_object('debug_switch');
let revertSwitch = buildable.get_object('revert_switch');
let unsafeSwitch = buildable.get_object('unsafe_switch');
let change_log = buildable.get_object('change_log');
let buttonGDMdefault = buildable.get_object('button_default_gnome');
let buttonnoblur = buildable.get_object('button_no_blur');
let buttonslightblur = buildable.get_object('button_slight_blur');
// check that these are valid (can be edited through dconf-editor)
//Utils.validate_market(settings, marketDescription);
Utils.validate_resolution(settings);
Utils.validate_icon(settings, icon_image);
// Indicator & notifications
settings.bind('hide', hideSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('notify', notifySwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
Utils.icon_list.forEach(function (iconname, index) { // add markets to dropdown list (aka a GtkComboText)
iconEntry.append(iconname, iconname);
});
settings.bind('icon-name', iconEntry, 'active_id', Gio.SettingsBindFlags.DEFAULT);
settings.connect('changed::icon-name', function() {
Utils.validate_icon(settings, icon_image);
});
iconEntry.set_active_id(settings.get_string('icon-name'));
settings.bind('set-background', bgSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('set-lock-screen', lsSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('debug-logging', debugSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('revert-to-current-image', revertSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('override-unsafe-wayland', unsafeSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
folderOpenBtn.connect('clicked', function(widget) {
Utils.openImageFolder(settings);
});
//download folder
if (Gtk.get_major_version() == 4) { // we need to use native file choosers in Gtk4
fileChooserBtn.set_label(settings.get_string('download-folder'));
fileChooser.set_current_folder(Gio.File.new_for_path(Utils.getWallpaperDir(settings)).get_parent());
fileChooserBtn.connect('clicked', function(widget) {
let parent = widget.get_root();
fileChooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER);
fileChooser.set_transient_for(parent);
fileChooser.show();
});
fileChooser.connect('response', function(widget, response) {
if (response !== Gtk.ResponseType.ACCEPT) {
return;
}
let fileURI = fileChooser.get_file().get_uri().replace('file://', '');
log("fileChooser returned: "+fileURI);
fileChooserBtn.set_label(fileURI);
Utils.moveImagesToNewFolder(settings, settings.get_string('download-folder'), fileURI);
settings.set_string('download-folder', fileURI);
});
// in Gtk 4 instead we use a DropDown, but we need to treat it a bit special
let market_grid = buildable.get_object('market_grid');
marketEntry = Gtk.DropDown.new_from_strings(Utils.marketName);
marketEntry.set_selected(Utils.markets.indexOf(settings.get_string('market')));
market_grid.attach(marketEntry, 1, 0, 1, 2);
marketEntry.connect('notify::selected-item', function() {
let id = marketEntry.get_selected();
settings.set_string('market', Utils.markets[id]);
log('dropdown selected '+id+' = '+Utils.markets[id]+" - "+Utils.marketName[id]);
});
settings.connect('changed::market', function() {
Utils.validate_market(settings, marketDescription, lastreq);
lastreq = GLib.DateTime.new_now_utc();
marketEntry.set_selected(Utils.markets.indexOf(settings.get_string('market')));
});
}
else { // Gtk 3
fileChooserBtn.set_filename(Utils.getWallpaperDir(settings));
log("fileChooser filename/dirname set to '"+fileChooserBtn.get_filename()+"' setting is '"+settings.get_string('download-folder')+"'");
fileChooserBtn.add_shortcut_folder_uri("file://" + GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES)+"/BingWallpaper");
fileChooserBtn.connect('file-set', function(widget) {
Utils.moveImagesToNewFolder(settings, settings.get_string('download-folder'), widget.get_filename());
settings.set_string('download-folder', widget.get_filename());
});
Utils.markets.forEach(function (bingmarket, index) { // add markets to dropdown list (aka a GtkComboText)
marketEntry.append(bingmarket, bingmarket+": "+Utils.marketName[index]);
});
//marketEntry.set_active_id(settings.get_string('market')); // set to current
settings.bind('market', marketEntry, 'active_id', Gio.SettingsBindFlags.DEFAULT);
settings.connect('changed::market', function() {
Utils.validate_market(settings, marketDescription, lastreq);
lastreq = GLib.DateTime.new_now_utc();
});
}
// Resolution
Utils.resolutions.forEach(function (res) { // add res to dropdown list (aka a GtkComboText)
resolutionEntry.append(res, res);
});
settings.bind('resolution', resolutionEntry, 'active_id', Gio.SettingsBindFlags.DEFAULT);
settings.connect('changed::resolution', function() {
Utils.validate_resolution(settings);
});
// History
let imageList = Utils.getImageList(settings);
historyEntry.append('current', _('Most recent image'));
historyEntry.append('random', _('Random image'));
imageList.forEach(function (image) {
historyEntry.append(image.urlbase.replace('/th?id=OHR.', ''), Utils.shortenName(Utils.getImageTitle(image), 50));
});
settings.bind('selected-image', historyEntry, 'active_id', Gio.SettingsBindFlags.DEFAULT);
settings.connect('changed::selected-image', function() {
Utils.validate_imagename(settings);
});
settings.bind('delete-previous', deleteSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
if (Convenience.currentVersionGreaterEqual("3.36")) {
// lockscreen and desktop wallpaper are shared in GNOME 3.36+
lsSwitch.set_sensitive(false);
buildable.get_object('lock_screen_listboxrow').set_tooltip_text(_("Disabled on current GNOME version"));
// GDM3 lockscreen blur override
settings.bind('override-lockscreen-blur', overrideSwitch, 'active', Gio.SettingsBindFlags.DEFAULT);
settings.bind('lockscreen-blur-strength', strengthEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
settings.bind('lockscreen-blur-brightness', brightnessEntry, 'value', Gio.SettingsBindFlags.DEFAULT);
buttonGDMdefault.connect('clicked', function(widget) {
Utils.set_blur_preset(settings, Utils.PRESET_GNOME_DEFAULT);
});
buttonnoblur.connect('clicked', function(widget) {
Utils.set_blur_preset(settings, Utils.PRESET_NO_BLUR);
});
buttonslightblur.connect('clicked', function(widget) {
Utils.set_blur_preset(settings, Utils.PRESET_SLIGHT_BLUR);
});
} else {
// older version of GNOME
buildable.get_object('lockscreen_box').set_tooltip_text(_("Disabled on current GNOME version"));
overrideSwitch.set_sensitive(false);
strengthEntry.set_sensitive(false);
brightnessEntry.set_sensitive(false);
buttonGDMdefault.set_sensitive(false);
buttonnoblur.set_sensitive(false);
buttonslightblur.set_sensitive(false);
}
// not required in GTK4 as widgets are displayed by default
if (Gtk.get_major_version() < 4)
box.show_all();
// fetch
Utils.fetch_change_log(Me.metadata.version.toString(), change_log);
lastreq = GLib.DateTime.new_now_utc();
return box;
}
function log(msg) {
if (settings.get_boolean('debug-logging'))
print("BingWallpaper extension: " + msg); // disable to keep the noise down in journal
}