-
Notifications
You must be signed in to change notification settings - Fork 16
/
egJSON.js
152 lines (112 loc) · 4.25 KB
/
egJSON.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
#!/usr/bin/gjs
/*
GJS example showing how to build Gtk javascript applications
reading and writting JSON files, the example makes use of
Gio.File.new_for_path, replace_async, load_contents_async
Run it with:
gjs egJSON.js
*/
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
// Get application folder and add it into the imports path
function getAppFileInfo() {
let stack = (new Error()).stack,
stackLine = stack.split('\n')[1],
coincidence, path, file;
if (!stackLine) throw new Error('Could not find current file (1)');
coincidence = new RegExp('@(.+):\\d+').exec(stackLine);
if (!coincidence) throw new Error('Could not find current file (2)');
path = coincidence[1];
file = Gio.File.new_for_path(path);
return [file.get_path(), file.get_parent().get_path(), file.get_basename()];
}
const path = getAppFileInfo()[1];
imports.searchPath.push(path);
// Import spawn library
//const Spawn = imports.assets.spawn;
const App = function () {
this.title = 'Example JSON';
GLib.set_prgname(this.title);
this.info = {};
};
App.prototype.run = function (ARGV) {
this.application = new Gtk.Application();
this.application.connect('activate', () => { this.onActivate(); });
this.application.connect('startup', () => { this.onStartup(); });
this.application.run([]);
};
App.prototype.onActivate = function () {
this.window.show_all();
};
App.prototype.onStartup = function() {
this.buildUI();
};
App.prototype.buildUI = function() {
this.window = new Gtk.ApplicationWindow({ application: this.application,
title: this.title,
default_height: 200,
default_width: 400,
window_position: Gtk.WindowPosition.CENTER });
try {
this.window.set_icon_from_file(path + '/assets/appIcon.png');
} catch (err) {
this.window.set_icon_name('application-x-executable');
}
this.window.add(this.getBody());
};
App.prototype.getBody = function() {
let buttonR, buttonW, grid;
buttonR = new Gtk.Button({ hexpand: true, halign: Gtk.Align.CENTER, label: 'Read JSON' });
buttonR.connect ('clicked', () => { this.read(); });
buttonW = new Gtk.Button({ hexpand: true, halign: Gtk.Align.CENTER, label: 'Write JSON' });
buttonW.connect ('clicked', () => { this.write(); });
this.label = new Gtk.Label({ label: '' });
grid = new Gtk.Grid({ column_spacing: 6, margin: 15, row_spacing: 6 });
grid.attach(buttonR, 0, 0, 1, 1);
grid.attach(buttonW, 1, 0, 1, 1);
grid.attach(this.label, 0, 1, 2, 1);
return grid;
};
App.prototype.read = function() {
let file;
file = Gio.File.new_for_path(path + '/assets/egJSON.json');
file.load_contents_async(null, (file, res) => {
let contents;
try {
contents = file.load_contents_finish(res)[1].toString();
this.info = JSON.parse(contents);
if (typeof this.info['counter-read'] !== 'undefined') {
this.info['counter-read'] = this.info['counter-read'] + 1;
}
this.label.set_text(JSON.stringify(this.info));
} catch (e) {
return;
}
});
};
App.prototype.write = function() {
let text, file;
if (typeof this.info['counter-read'] !== 'undefined' && typeof this.info['counter-write'] !== 'undefined') {
this.info['counter-write'] = this.info['counter-write'] + 1;
text = JSON.stringify(this.info);
file = Gio.File.new_for_path(path + '/assets/egJSON.json');
file.replace_async(null, false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
GLib.PRIORITY_LOW, null, (file, res) => {
let stream;
if (!res.had_error()) {
stream = file.replace_finish(res);
stream.write(text, null);
// Write more data with stream.write ...
stream.close(null);
}
});
} else {
this.label.set_text('Read the file first');
}
};
//Run the application
let app = new App();
app.run(ARGV);