forked from simplificator/sitincator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
147 lines (122 loc) · 4.1 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
const { app, BrowserWindow, ipcMain } = require('electron');
const { google } = require('googleapis');
const gcal = require('./src/gcal');
const fs = require('fs');
const readline = require('readline');
const path = require('path');
const CONFIG_DIR = path.resolve(__dirname, './config');
const SITINCATOR_CONFIG = path.resolve(CONFIG_DIR, 'sitincator.json');
global.calendarName = '';
let win;
function createDirectory(directory) {
try {
fs.mkdirSync(directory);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
}
function writeConfiguration(calendar_id) {
return new Promise((resolve, reject) => {
let configuration = { calendar_id: calendar_id, title: "" };
fs.writeFile(SITINCATOR_CONFIG, JSON.stringify(configuration), error => {
if(error)
reject(error);
else
resolve(configuration);
});
});
}
function askForCalendarId() {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the calendar ID (found on the settings page of your calendar in Google Calendar): ', (answer) => {
resolve(answer);
});
});
}
function readConfigurationFile() {
return new Promise((resolve, reject) => {
fs.readFile(SITINCATOR_CONFIG, (error, content) => {
if (error)
reject(error);
else
resolve(JSON.parse(content));
});
});
}
function readConfiguration() {
return new Promise((resolve, reject) => {
readConfigurationFile()
.then(configuration => resolve(configuration))
.catch(error => {
if(error.code != 'ENOENT')
reject(error);
else {
askForCalendarId()
.then(writeConfiguration)
.then(configuration => resolve(configuration))
.catch(error => reject(error));
}
});
});
}
function createWindow () {
win = new BrowserWindow({width: 480, height: 800});
if (process.env.NODE_ENV !== 'development')
win.setFullScreen(true);
if (process.env.NODE_ENV === 'development')
win.webContents.openDevTools();
win.loadURL(`file://${__dirname}/index.html`);
win.on('closed', () => {
// Dereference the window object
win = null;
});
}
app.on('ready', () => {
const gcalApi = new gcal.GCal(process.env.SITINCATOR_CALENDAR_ID);
gcalApi.authorize()
.then(client => {
createWindow();
global.calendarName = process.env.SITINCATOR_ROOM_NAME;
ipcMain.on('calendar:list-events', event => client.listEvents()
.then(items => event.sender.send('calendar:list-events-success', items))
.catch(error => event.sender.send('calendar:list-events-failure', error))
);
ipcMain.on('calendar:status-event', event => client.statusEvent()
.then(item => event.sender.send('calendar:status-event-success', item))
.catch(error => event.sender.send('calendar:status-event-failure', error))
);
ipcMain.on('calendar:quick-reservation', (event, duration) => {
client.insertEvent(duration)
.then(response => event.sender.send('calendar:quick-reservation-success', response))
.catch(error => event.sender.send('calendar:quick-reservation-failure', error));
}
);
ipcMain.on('calendar:finish-reservation', (event, eventId) => {
client.finishEvent(eventId)
.then(response => event.sender.send('calendar:finish-reservation-success', response))
.catch(error => event.sender.send('calendar:finish-reservation-failure', error));
});
})
.catch(() => process.exit());
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS 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', () => {
// On macOS 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 (win === null) {
createWindow();
}
})