-
Notifications
You must be signed in to change notification settings - Fork 9
/
templateManager.js
95 lines (89 loc) · 3.32 KB
/
templateManager.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
/* LICENSE INFORMATION
*
* Desktop Icons: Neo - A desktop icons extension for GNOME with numerous features,
* customizations, and optimizations.
*
* Copyright 2021 Abdurahman Elmawi (cooper64doom@gmail.com)
*
* This project is based on Desktop Icons NG (https://gitlab.com/rastersoft/desktop-icons-ng),
* a desktop icons extension for GNOME licensed under the GPL v3.
*
* This project is free and open source software as described in the GPL v3.
*
* This project (Desktop Icons: Neo) is licensed under the GPL v3. To view the details of this license,
* visit https://www.gnu.org/licenses/gpl-3.0.html for the necessary information
* regarding this project's license.
*/
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Enums = imports.enums;
const DesktopIconsUtil = imports.desktopIconsUtil;
var TemplateManager = class {
constructor() {
this._templates = [];
this._templatesEnumerateCancellable = null;
this._templateDir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_TEMPLATES);
if (this._templateDir == GLib.get_home_dir()) {
this._templateDir = null;
}
if (this._templateDir != null) {
this._templateGFile = Gio.File.new_for_path(this._templateDir);
this._monitor = this._templateGFile.monitor_directory(Gio.FileMonitorFlags.NONE, null);
this._monitor.connect("changed", () => {
this._refreshTemplates();
});
this._refreshTemplates();
} else {
this._templateGFile = null;
}
}
getTemplates() {
let templates = [];
for(let template of this._templates) {
let data = {};
data["icon"] = template.get_icon();
let name = template.get_name();
let offset = DesktopIconsUtil.getFileExtensionOffset(name, false);
data["name"] = name.substring(0, offset);
data["extension"] = name.substring(offset);
data["file"] = name;
templates.push(data);
}
return templates;
}
_refreshTemplates() {
if (this._templatesEnumerateCancellable) {
this._templatesEnumerateCancellable.cancel();
}
this._templatesEnumerateCancellable = new Gio.Cancellable();
this._templateGFile.enumerate_children_async(
Enums.DEFAULT_ATTRIBUTES,
Gio.FileQueryInfoFlags.NONE,
GLib.PRIORITY_DEFAULT,
this._templatesEnumerateCancellable,
(source, result) => {
try {
let fileEnum = source.enumerate_children_finish(result);
this._templates = [];
let info;
while ((info = fileEnum.next_file(null))) {
if (info.get_file_type() != Gio.FileType.DIRECTORY) {
this._templates.push(info);
}
}
} catch(e) {}
}
);
}
getTemplateFile(name) {
if (this._templateGFile == null) {
return null;
}
let template = Gio.File.new_for_path(GLib.build_filenamev([this._templateDir, name]));
if (template.query_exists(null)) {
return template;
} else {
return null;
}
}
}