-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
192 lines (164 loc) · 4.9 KB
/
main.js
File metadata and controls
192 lines (164 loc) · 4.9 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
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
const electron = require("electron")
const fs = require("fs")
const os = require("os")
const crypto = require("crypto")
const path = require("path")
const windowStateKeeper = require('electron-window-state');
const ipc = electron.ipcMain;
const isDev = require('electron-is-dev');
const applicationPath = isDev ? electron.app.getAppPath() : path.dirname(electron.app.getPath("exe"));
let userData = null;
let mainWindow = null;
let tray = null;
let isTimerRunning = false;
function initialiseUserData() {
let dataPath = path.join(electron.app.getPath("appData"), "JiraTimer", "userdata"); // do i need to put app name in user data file name?
if (fs.existsSync(dataPath)) {
let decipher = crypto.createDecipher("aes-256-ctr", "c187a809b");
let encrypted = fs.readFileSync(dataPath).toString();
let decrypted = decipher.update(encrypted, "hex", "utf8") + decipher.final("utf8");
userData = JSON.parse(decrypted);
}
else {
userData = {
settings: {}, // defaults are handled in ModelConverterService
selectedConnection: -1, // index of selected connection
connections: [] // { hostname, icon, username, password, history }
// history : { worklogIds, jiras, startedAt, pausedDuration, endedAt, description }
};
saveUserData();
}
}
function saveUserData() {
let dataPath = path.join(electron.app.getPath("appData"), "JiraTimer", "userdata"); // do i need to put app name in user data file name?
let decrypted = JSON.stringify(userData);
let cipher = crypto.createCipher("aes-256-ctr", "c187a809b");
let encrypted = cipher.update(decrypted, "utf8", "hex") + cipher.final("hex");
if (!fs.existsSync(path.dirname(dataPath))) {
fs.mkdirSync(path.dirname(dataPath));
}
fs.writeFile(dataPath, encrypted, () => {}); // callback required but we don't have any use for it
}
function createWindow() {
let windowState = windowStateKeeper({
defaultWidth: 1400,
defaultHeight: 800
});
let window =
new electron.BrowserWindow({
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height
});
windowState.manage(window);
window.webContents.setUserAgent("Mozilla/5.0 (Windows NT 6.3; Win64; x64; AppleWebKit-537.36; Chrome-45.0.2454.85; Electron-0.34.2; Safari-537.36) like Gecko");
window.loadFile("dist/index.html");
window.setIcon(path.join(applicationPath, "dist/assets/icon.ico"));
window.on("ready-to-show", () => {
if (userData.settings.openInBackground === false) {
mainWindow.show();
}
});
window.on("close", e => {
e.preventDefault();
if (os.platform() === "darwin") {
mainWindow.hide();
tray.destroy();
tray = null;
}
else if (userData.settings.keepOpenInTray === true) {
mainWindow.hide();
}
else {
handleCloseRequest();
}
});
mainWindow = window;
}
function createTray() {
tray = new electron.Tray(path.join(applicationPath, "dist/assets/icon-stop.png"));
tray.setToolTip("Timer is stopped."); // if timer is active "Keeping track of your time..." else "Timer is stopped."
let contextMenu = new electron.Menu();
contextMenu.append(new electron.MenuItem({
id: "openWindow",
label: "Open Window",
click: () => mainWindow.show()
}));
contextMenu.append(new electron.MenuItem({
label: "Exit",
click: handleCloseRequest
}));
tray.setContextMenu(contextMenu);
tray.on("click", () => mainWindow.show());
}
function handleCloseRequest() {
if (isTimerRunning === true) {
electron.dialog.showMessageBox(
{
type: "question",
message: "You have a timer running. Are you sure you wish to quit?",
buttons: ["Keep open", "Quit anyway"],
defaultId: 0,
cancelId: 0
},
() => {
if (response === 1) {
electron.app.quit();
}
});
}
else {
electron.app.quit();
}
}
electron.app.on("ready", () => {
initialiseUserData();
createTray();
createWindow();
});
electron.app.on("activate", () => {
mainWindow.show();
if (tray === null) {
createTray();
}
});
electron.app.on('before-quit', () => {
mainWindow.show();
mainWindow.destroy();
if (tray !== null) {
tray.destroy();
}
});
ipc.on("timerState", (_, state) => {
switch (state) {
case "running":
tray.setToolTip("Keeping track of your time...");
tray.setImage(path.join(applicationPath, "dist/assets/icon-play.png"));
break;
case "stopped":
tray.setToolTip("Timer is stopped.");
tray.setImage(path.join(applicationPath, "dist/assets/icon-stop.png"));
break;
case "paused":
tray.setToolTip("Timer is paused.");
tray.setImage(path.join(applicationPath, "dist/assets/icon-pause.png"));
break;
}
});
ipc.on("userDataRequest", e => {
e.sender.send('userData', userData);
});
ipc.on("userData", (e, arg) => {
if (arg.settings.startOnStartup !== userData.settings.startOnStartup) {
electron.app.setLoginItemSettings({
openAtLogin: arg.settings.startOnStartup,
path: electron.app.getPath("exe")
});
}
userData = arg;
saveUserData();
});
ipc.on("openUrl", (_, url) => {
electron.shell.openExternal(url);
});