-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathautoConfig.ts
More file actions
99 lines (85 loc) · 3.15 KB
/
Copy pathautoConfig.ts
File metadata and controls
99 lines (85 loc) · 3.15 KB
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
/*
Copyright 2025 Roman Lefler
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
import GLib from "gi://GLib";
import Gio from "gi://Gio";
import { UnitPreset, writeGTypeAS } from "./config.js";
import { getMyLocation } from "./myLocation.js";
import { Location } from "./location.js";
import { gettext as _g } from "./gettext.js"
import { AutoConfigFailError } from "./errors.js";
// Denmark, Finland, Sweden, Norway, Iceland, Faroe Islands, Greenland
const NORDIC : string[] = [ "DK", "FI", "SE", "NO", "IS", "FO", "GL" ];
async function readFileAsync(path : string) : Promise<string | null> {
const f = Gio.File.new_for_path(path);
return new Promise<string | null>(resolve => {
f.load_contents_async(null, (_, res) => {
try {
const [ ok, contents ] = f.load_contents_finish(res);
if(!ok) {
resolve(null);
return;
}
const str = new TextDecoder().decode(contents);
resolve(str);
return;
} catch(e) {
resolve(null);
return;
}
});
});
}
/**
* Tests if this computer is a desktop.
* @returns True if a desktop, otherwise false if not or unknown.
*/
async function isDesktop() : Promise<boolean> {
const str = await readFileAsync("/sys/class/dmi/id/chassis_type");
// Return false if file read failed
if(!str) return false;
// Chassis 3 = desktop
return str === "3\n";
}
/**
* Guesses based on the specific computer what settings
* he/she will want.
*/
export async function setFirstTimeConfig(settings : Gio.Settings) {
let myLoc;
try {
myLoc = await getMyLocation();
} catch(e) {
console.log("Caught get my location error in autoconfig.");
throw new AutoConfigFailError();
}
// If it isn't a laptop then set your location once and never query the server again
if(await isDesktop()) {
const loc = Location.newCoords(myLoc.city ?? _g("My Location"), myLoc.lat, myLoc.lon);
const strArr = [ loc.toString() ];
settings.set_value("locations", writeGTypeAS(strArr));
}
const cc = myLoc.country;
if(cc === "US") {
settings.set_enum("unit-preset", UnitPreset.US);
}
else if(cc === "UK" || cc === "GB") {
settings.set_enum("unit-preset", UnitPreset.UK);
}
else if(cc && NORDIC.includes(cc)) {
settings.set_enum("unit-preset", UnitPreset.Nordic);
}
else {
settings.set_enum("unit-preset", UnitPreset.Metric);
}
}