This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
359 lines (301 loc) · 12.6 KB
/
main.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'use strict'
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
var ipcMain = require('electron').ipcMain
var configuration = require('./app/configuration')
let mainWindow
let projectsWindow
let settingsWindow
const activitiesStorage = require('./app/storage/activitiesStorage')
const projectsStorage = require('./app/storage/projectsStorage')
const pdfReport = require('./app/storage/pdfReport')
const mapHandling = require('./app/mapHandling')
const fs = require('fs')
let loggedActivities
let savedProjects
function createMainWindow () {
mainWindow = new BrowserWindow({
width: 500,
height: 680
})
mainWindow.loadURL(`file://${__dirname}/app/main-window.html`)
if (configuration.readSettings('debug-mode') === true) {
mainWindow.webContents.openDevTools()
}
// Emitted when the window is closed.
mainWindow.on('closed', function () {
mainWindow = null
})
}
function createProjectsWindow () {
projectsWindow = new BrowserWindow({
height: 680,
width: 500
})
projectsWindow.loadURL(`file://${__dirname}/app/projects-window.html`)
if (configuration.readSettings('debug-mode') === true) {
projectsWindow.webContents.openDevTools()
}
projectsWindow.on('closed', function () {
projectsWindow = null
})
}
function createSettingsWindow () {
settingsWindow = new BrowserWindow({
height: 600,
width: 400
})
settingsWindow.loadURL(`file://${__dirname}/app/settings-window.html`)
if (configuration.readSettings('debug-mode') === true) {
settingsWindow.webContents.openDevTools()
}
settingsWindow.on('closed', function () {
settingsWindow = null
})
}
app.on('ready', function () {
// Save default settings to the config file if these settings haven't been set yet
if (!configuration.readSettings('time-format')) {
configuration.saveSettings('time-format', 'european')
}
if (!configuration.readSettings('savefile-directory')) {
configuration.saveSettings('savefile-directory', configuration.getUserHome())
}
if (!configuration.readSettings('show-deletion-confirmation')) {
configuration.saveSettings('show-deletion-confirmation', true)
}
if (!configuration.readSettings('debug-mode')) {
configuration.saveSettings('debug-mode', false)
}
if (!configuration.readSettings('project-colors')) {
configuration.saveSettings('project-colors', ['#e51c23', '#ff9800', '#9c27b0', '#4caf50', '#2196f3'])
}
// load activities and projects
loggedActivities = activitiesStorage.readActivities()
savedProjects = projectsStorage.readProjects()
createMainWindow()
})
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createMainWindow()
}
})
ipcMain.on('sheeptime:projects:open', function () {
if (projectsWindow) {
return
}
createProjectsWindow()
})
ipcMain.on('sheeptime:settings:open', function () {
if (settingsWindow) {
return
}
createSettingsWindow()
})
// Send the activities to the view
// Flow: controller -> main window/projects-window
ipcMain.on('sheeptime:loggedActivities:send', function (event, targetWindow) {
event.sender.send('sheeptime:loggedActivities:get', loggedActivities)
})
ipcMain.on('sheeptime:savedProjects:send', function (event, targetWindow) {
event.sender.send('sheeptime:savedProjects:get', savedProjects)
})
ipcMain.on('sheeptime:config:deletion-confirmation:send', function (event, arg) {
event.sender.send('sheeptime:config:deletion-confirmation:get', configuration.readSettings('show-deletion-confirmation'))
})
ipcMain.on('sheeptime:config:debug-mode:send', function (event, arg) {
event.sender.send('sheeptime:config:debug-mode:get', configuration.readSettings('debug-mode'))
})
ipcMain.on('sheeptime:config:colors:send', function (event, arg) {
event.sender.send('sheeptime:config:colors:get', configuration.readSettings('project-colors'))
})
ipcMain.on('sheeptime:config:time-format:send', function (event, arg) {
event.sender.send('sheeptime:config:time-format:get', configuration.readSettings('time-format'))
})
ipcMain.on('sheeptime:config:savefile-location:send', function (event, arg) {
event.sender.send('sheeptime:config:savefile-location:get', configuration.readSettings('savefile-directory'))
})
// Config setters
// Flow: main window/projects-window -> controller
ipcMain.on('sheeptime:config:deletion-confirmation:set', function (event, newStatus) {
configuration.saveSettings('show-deletion-confirmation', newStatus)
// Update the status in the projects window and the settings window
if (settingsWindow) {
settingsWindow.webContents.send('sheeptime:config:deletion-confirmation:get', newStatus)
}
if (projectsWindow) {
projectsWindow.webContents.send('sheeptime:config:deletion-confirmation:get', newStatus)
}
})
ipcMain.on('sheeptime:config:debug-mode:set', function (event, newDebugMode) {
configuration.saveSettings('debug-mode', newDebugMode)
})
ipcMain.on('sheeptime:config:time-format:set', function (event, newTimeFormat) {
configuration.saveSettings('time-format', newTimeFormat)
})
ipcMain.on('sheeptime:config:savefile-location:set', function (event, newSaveFileDir) {
fs.access(newSaveFileDir, fs.W_OK, function (err) {
if (!err) {
// path exists and can be written -> save path
configuration.saveSettings('savefile-directory', newSaveFileDir)
activitiesStorage.saveActivities(loggedActivities)
projectsStorage.saveProjects(savedProjects)
event.sender.send('sheeptime:config:savefile-location:set:done', true)
} else {
// path error
event.sender.send('sheeptime:config:savefile-location:set:done', false)
}
})
})
// Event: user deleted an activity
// Flow: main window -> controller
ipcMain.on('sheeptime:activity:delete', function (event, deletedActivityID) {
// Save a reference to the activity for finding it's project
var activity = mapHandling.getElement(loggedActivities.activitiesArray, deletedActivityID)
// Delete the activity from the storage object
loggedActivities.activitiesArray = mapHandling.deleteElement(loggedActivities.activitiesArray, deletedActivityID)
// Pass the changed activities storage object to the view
// Flow: controller -> main window
if (mainWindow) {
mainWindow.webContents.send('sheeptime:activity:deleted', loggedActivities)
}
// Then update the new total time of the project the activity was associated with
var activityProject = mapHandling.getElement(savedProjects.projectsArray, activity.projectID)
activityProject.totalSeconds += -(activity.duration)
savedProjects.projectsArray = mapHandling.setElement(savedProjects.projectsArray, activity.projectID, activityProject)
activitiesStorage.saveActivities(loggedActivities)
projectsStorage.saveProjects(savedProjects)
})
// Event: user added an activity
// Flow: main window -> controller
ipcMain.on('sheeptime:activity:add', function (event, addedActivity) {
// Add the new activity to the loggedActivities map
loggedActivities.activitiesArray = mapHandling.setElement(loggedActivities.activitiesArray, loggedActivities.freshID, addedActivity)
// Increment the fresh ID
loggedActivities.freshID++
// Pass the changed activities storage object to the view
// Flow: controller -> main window
event.sender.send('sheeptime:activity:added', loggedActivities)
// Update the total time of the activity's project in the project map by adding the number of elapsed seconds
let activityProject = mapHandling.getElement(savedProjects.projectsArray, addedActivity.projectID)
activityProject.totalSeconds += addedActivity.duration
savedProjects.projectsArray = mapHandling.setElement(savedProjects.projectsArray, addedActivity.projectID, activityProject)
// Pass the changed projects storage object to the view
// Flow: controller -> projects window
if (projectsWindow) {
projectsWindow.webContents.send('sheeptime:activity:added', savedProjects)
}
activitiesStorage.saveActivities(loggedActivities)
projectsStorage.saveProjects(savedProjects)
})
// Event: user edited an activity
// Flow: main window -> controller
ipcMain.on('sheeptime:activity:edit', function (event, editedActivity) {
// Update the activity in the storage object
loggedActivities.activitiesArray = mapHandling.setElement(loggedActivities.activitiesArray, editedActivity[0], editedActivity[1])
activitiesStorage.saveActivities(loggedActivities)
})
// Event: user edited a project
// Flow: projects window -> controller
ipcMain.on('sheeptime:project:edit', function (event, editedProject) {
// Update the project in the storage object
savedProjects.projectsArray = mapHandling.setElement(savedProjects.projectsArray, editedProject[0], editedProject[1])
projectsStorage.saveProjects(savedProjects)
})
// Event: user deleted a project
// Flow: projects window -> controller
ipcMain.on('sheeptime:project:delete', function (event, deletedProjectsID) {
// Delete the project from the storage object
savedProjects.projectsArray = mapHandling.deleteElement(savedProjects.projectsArray, deletedProjectsID)
// Pass the changed storage object to the view
// Flow: controller -> projects window, main window
if (projectsWindow) {
projectsWindow.webContents.send('sheeptime:project:deleted', savedProjects)
}
if (mainWindow) {
mainWindow.webContents.send('sheeptime:project:deleted', savedProjects)
}
// Delete all the activities that were associated with that project
var activitiesMap = mapHandling.arrayToMap(loggedActivities.activitiesArray)
activitiesMap.forEach(function (elem, id) {
if (elem.projectID === deletedProjectsID) {
activitiesMap.delete(id)
}
})
loggedActivities.activitiesArray = mapHandling.mapToArray(activitiesMap)
// Pass the changed activities storage object to the view
// Flow: controller -> main window
if (mainWindow) {
mainWindow.webContents.send('sheeptime:activity:deleted', loggedActivities)
}
activitiesStorage.saveActivities(loggedActivities)
projectsStorage.saveProjects(savedProjects)
})
// Event: user added a project
// Flow: projects window -> controller
ipcMain.on('sheeptime:project:add', function (event, newProjectObject) {
// Construct new project, add it to the projects storage and increase the fresh ID
var newProject = {name: newProjectObject.name, color: newProjectObject.selectedColor, totalSeconds: 0}
savedProjects.projectsArray = mapHandling.setElement(savedProjects.projectsArray, savedProjects.freshID, newProject)
savedProjects.freshID++
// Pass the changed storage object to the view
// Flow: controller -> projects window, main window
if (projectsWindow) {
projectsWindow.webContents.send('sheeptime:project:added', savedProjects)
}
if (mainWindow) {
mainWindow.webContents.send('sheeptime:project:added', savedProjects)
}
projectsStorage.saveProjects(savedProjects)
})
// Event: user started a stopwatch
// Flow: activities window -> controller
ipcMain.on('sheeptime:stopwatch:started', function (event, arg) {
// Tell the projects window to prevent the user from deleting projects
// Flow: controller -> projects window
if (projectsWindow) {
projectsWindow.webContents.send('sheeptime:stopwatch:started')
}
})
// Event: user stopped a stopwatch
// Flow: activities window -> controller
ipcMain.on('sheeptime:stopwatch:stopped', function (event, arg) {
// Tell the projects window to allow the user to delete projects
// Flow: controller -> projects window
if (projectsWindow) {
projectsWindow.webContents.send('sheeptime:stopwatch:stopped')
}
})
// Event: user wants a report
// Flow: projects window -> controller
ipcMain.on('sheeptime:report:send', function (event, projectID) {
var result = []
var activitiesMap = mapHandling.arrayToMap(loggedActivities.activitiesArray)
activitiesMap.forEach(function (elem) {
if (elem.projectID === projectID) {
result.push(elem)
}
})
// Flow: controller -> projects window
projectsWindow.webContents.send('sheeptime:report:get', result)
})
// Event: user wants a PDF report
// Flow: projects window -> controller
ipcMain.on('sheeptime:report:PDF', function (event, reportProject) {
if (!pdfReport.savePDFReport(event, reportProject)) {
event.sender.send('sheeptime:report:PDF:error')
}
})