-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.js
165 lines (149 loc) · 5.99 KB
/
utils.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
// Earth View 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
/*global imports, log*/
const {Gio, GLib, GdkPixbuf, Soup} = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const Gettext = imports.gettext.domain('BingWallpaper');
const _ = Gettext.gettext;
const ByteArray = imports.byteArray;
var icon_list = ['pin', 'globe','official'];
var icon_list_filename = ['pin-symbolic', 'globe-symbolic', 'official'];
var backgroundStyle = ['none', 'wallpaper', 'centered', 'scaled', 'stretched', 'zoom', 'spanned'];
var gitreleaseurl = 'https://api.github.com/repos/neffo/earth-view-wallpaper-gnome-extension/releases/tags/';
var schema = 'org.gnome.shell.extensions.googleearthwallpaper';
var DESKTOP_SCHEMA = 'org.gnome.desktop.background';
function friendly_time_diff(time, short = true) {
// short we want to keep ~4-5 characters
let timezone = GLib.TimeZone.new_local();
let now = GLib.DateTime.new_now(timezone).to_unix();
let seconds = time.to_unix() - now;
if (seconds <= 0) {
return "now";
}
else if (seconds < 60) {
return "< 1 "+(short?"m":_("minutes"));
}
else if (seconds < 3600) {
return Math.round(seconds/60)+" "+(short?"m":_("minutes"));
}
else if (seconds > 86400) {
return Math.round(seconds/86400)+" "+(short?"d":_("days"));
}
else {
return Math.round(seconds/3600)+" "+(short?"h":_("hours"));
}
}
function friendly_coordinates(lat, lon) {
return Math.abs(lat).toFixed(4)+(lat>0 ? 'N': 'S')+', '+Math.abs(lon).toFixed(4)+(lon>0 ? 'E':'W');
}
function clamp_value(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function fetch_change_log(version, label, httpSession) {
// create an http message
let url = gitreleaseurl + "v" + version;
let request = Soup.Message.new('GET', url);
httpSession.user_agent = 'User-Agent: Mozilla/5.0 (X11; GNOME Shell/' + imports.misc.config.PACKAGE_VERSION + '; Linux x86_64; +https://github.com/neffo/earth-view-wallpaper-gnome-extension ) Google Earth Wallpaper Gnome Extension/' + Me.metadata.version;
// queue the http request
log("Fetching "+url);
try {
if (Soup.MAJOR_VERSION >= 3) {
httpSession.send_and_read_async(request, GLib.PRIORITY_DEFAULT, null, (httpSession, message) => {
let data = ByteArray.toString(httpSession.send_and_read_finish(message).get_data());
let text = JSON.parse(data).body;
label.set_label(text);
});
}
else {
httpSession.queue_message(request, (httpSession, message) => {
let data = message.response_body.data;
let text = JSON.parse(data).body;
label.set_label(text);
});
}
}
catch (error) {
log("Error fetching change log: " + error);
label.set_label(_("Error fetching change log: "+error));
}
}
function validate_icon(settings, icon_image = null) {
log('validate_icon()');
let icon_name = settings.get_string('icon');
if (icon_name == "" || icon_list.indexOf(icon_name) == -1) {
settings.reset('icon');
icon_name = settings.get_string('icon');
}
// if called from prefs
if (icon_image) {
log('set icon to: ' + Me.dir.get_path() + '/icons/' + icon_name + '-symbolic.svg');
let pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(Me.dir.get_path() + '/icons/' + icon_list_filename[icon_list.indexOf(icon_name)] + '.svg', 32, 32);
icon_image.set_from_pixbuf(pixbuf);
}
}
// Utility function
function dump(object, level = 0) {
let output = '';
for (let property in object) {
output += "-".repeat(level)+property + ': ' + object[property]+'\n ';
if ( typeof property === 'object' )
output += dump(property, level+1);
}
if (level == 0)
log(output);
return(output);
}
function moveImagesToNewFolder(settings, oldPath, newPath) {
let dir = Gio.file_new_for_path(oldPath);
let dirIter = dir.enumerate_children('', Gio.FileQueryInfoFlags.NONE, null );
let newDir = Gio.file_new_for_path(newPath);
if (!newDir.query_exists(null)) {
newDir.make_directory_with_parents(null);
}
let file = null;
while (file = dirIter.next_file(null)) {
let filename = file.get_name(); // we only want to move files that we think we own
if (filename.match(/.+\.jpg/i)) {
log('file: ' + slash(oldPath) + filename + ' -> ' + slash(newPath) + filename);
let cur = Gio.file_new_for_path(slash(oldPath) + filename);
let dest = Gio.file_new_for_path(slash(newPath) + filename);
cur.move(dest, Gio.FileCopyFlags.OVERWRITE, null, () => {
log ('...moved');
});
}
}
// correct filenames for GNOME backgrounds
if (settings.get_boolean('set-background'))
moveBackground(oldPath, newPath, DESKTOP_SCHEMA);
}
function dirname(path) {
return path.match(/.*\//);
}
function slash(path) {
if (!path.endsWith('/'))
return path+'/';
return path;
}
function moveBackground(oldPath, newPath, schema) {
let gsettings = new Gio.Settings({schema: schema});
let uri;
let dark_uri;
uri = gsettings.get_string('picture-uri');
gsettings.set_string('picture-uri', uri.replace(oldPath, newPath));
try {
dark_uri = gsettings.get_string('picture-uri-dark');
gsettings.set_string('picture-uri-dark', dark_uri.replace(oldPath, newPath));
}
catch (e) {
log('no dark background gsettings key found ('+e+')');
}
Gio.Settings.sync();
gsettings.apply();
}