forked from Algram/Hypha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
231 lines (188 loc) · 6.11 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
'use strict';
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
const Tray = electron.Tray;
const ipcMain = require('electron').ipcMain;
const irc = require('./app/js/network');
const storage = require('./app/js/storage');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
//Create the main network for the client
let network = new irc.Network('default');
// 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();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function () {
// Initial createWindow
createWindow();
restoreState();
//mainWindow.setMenu(null);
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/app/index.html');
network.on('channelData', function (address, channel) {
mainWindow.webContents.send('channelData', address, channel);
})
network.on('messageReceived', function (address, message) {
mainWindow.webContents.send('messageReceived', address, message);
})
network.on('userlistChanged', function (address, channel) {
mainWindow.webContents.send('userlistChanged', address, channel);
})
network.on('usernameChanged', function (address, nick) {
mainWindow.webContents.send('usernameChanged', address, nick);
})
//////////////////////
// Receiving Events //
//////////////////////
/*
They renderer wants to send a message and fires the event for it
that contains the message content
*/
ipcMain.on('messageSent', function (event, address, message) {
let selChannel = network.getClient(address).getChannel(message.to);
//Add message to selected channel
selChannel.addMessage(message);
//Tell the client to send the message to its channel
network.getClient(address).say(message.to, message.message);
});
ipcMain.on('actionSent', function (event, address, target, message) {
let selClient = network.getClient(address);
selClient.sendAction(target, message);
});
/*
In the renderer a channel got selected and the event contains
the name of the selected channel
*/
ipcMain.on('channelSelected', function (event, address, name) {
let selChannel = network.getClient(address).getChannel(name);
network.getClient(address).setSelectedChannel(selChannel);
event.sender.send('channelSelected_reply', address, selChannel, network.getClient(address).getNick());
});
ipcMain.on('channelAdded', function (event, address, channelName, mode) {
network.getClient(address).addChannel(channelName, mode);
});
ipcMain.on('channelRemoved', function (event, address, channelName) {
network.getClient(address).removeChannel(channelName);
});
ipcMain.on('serverAdded', function (event, nick, address, options) {
network.addClient(nick, address, options);
network.getClient(address).connect();
});
ipcMain.on('usernameChanged', function (event, address, nick) {
network.getClient(address).changeNick(nick);
});
ipcMain.on('closeWindow', function (event) {
let cls = network.getAllClients();
for (let key in cls) {
let cl = cls[key];
cl.disconnect('Connection closed');
}
mainWindow.close()
});
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is being closed.
mainWindow.on('close', function () {
saveState();
});
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
require('electron').powerMonitor.on('suspend', function() {
saveState();
});
require('electron').powerMonitor.on('resume', function() {
restoreState();
});
});
function createWindow() {
let lastWindowState = storage.get('lastWindowState');
if (lastWindowState === null) {
lastWindowState = {
width: 800,
height: 600,
maximized: false
}
}
mainWindow = new BrowserWindow({
x: lastWindowState.x,
y: lastWindowState.y,
width: lastWindowState.width,
height: lastWindowState.height,
minWidth: 500,
minHeight: 300,
frame: false,
overlayScrollbar: true,
icon: __dirname + '/app/images/logo.png'
});
if (lastWindowState.maximized) {
mainWindow.maximize();
}
}
function saveState() {
//Set lastWindowState
let bounds = mainWindow.getBounds();
storage.set('lastWindowState', {
x: bounds.x,
y: bounds.y,
width: bounds.width,
height: bounds.height,
maximized: mainWindow.isMaximized()
});
//Set lastConnectionState
let clients = [];
let currClients = network.getAllClients();
for (let key in currClients) {
let currClient = currClients[key];
let client = {};
let nick = currClient.nick;
let address = currClient.address;
let channels = [];
for (let key in currClient.channels) {
let currChannel = currClient.channels[key];
//Only save default channels, not e.g. pms
if (currChannel.mode === 'default') {
channels.push(currChannel.name);
}
}
client.nick = nick;
client.address = address;
client.channels = channels;
clients.push(client);
}
storage.set('lastConnectionState', {
clients: clients
});
}
function restoreState() {
let lastConnectionState = storage.get('lastConnectionState');
if (lastConnectionState !== null) {
let lastClients = lastConnectionState.clients;
for (let key in lastClients) {
let lastClient = lastClients[key];
network.addClient(lastClient.nick, lastClient.address);
//Only connect if server has channels
if (lastClient.channels.length !== 0) {
network.getClient(lastClient.address).connect();
for (let key in lastClient.channels) {
let newChannel = lastClient.channels[key];
network.getClient(lastClient.address).addChannel(newChannel);
}
}
}
}
}