aboutsummaryrefslogtreecommitdiff
path: root/main.js
blob: 3bf20b4777ba712cce15117d9ef83b4f159df140 (plain)
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
const { app, BrowserWindow, ipcMain, webContents } = require('electron');
const path = require("path");
const url = require("url");
const Store = require("electron-store");
const connectionStore = new Store();

let uiWindow;
let newConnectionDialog;
let queryExecutors = [];
let savedConnections = [];

function createMainWindow() {
  uiWindow = new BrowserWindow({
    width: 800,
    height: 600
  });
  uiWindow.loadURL(url.format({
    pathname: path.join(__dirname, "index.html"),
    protocol: "file:",
    slashes: true
  }));

  uiWindow.on("closed", () => {
    uiWindow = null;
    app.quit();
  });
}

app.on("ready", () => {
  createMainWindow();
  restoreSavedConnections();
  console.log(savedConnections);
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (uiWindow === null) {
    createWindow();
  }
});

function createNewConnectionDialog() {
  newConnectionDialog = new BrowserWindow({
    width: 400,
    height: 600
  });
  newConnectionDialog.loadURL(url.format({
    pathname: path.join(__dirname, "new-connection.html"),
    protocol: "file:",
    slashes: true
  }));

  newConnectionDialog.on("closed", () => {
    newConnectionDialog = null;
  });
}

ipcMain.on("instanceManager.openNewConnectionDialog", (event, payload) => {
  createNewConnectionDialog();
});

function createQueryExecutor(payload) {
  let executor = new BrowserWindow({
    show: false,
  });
  
  executor.connectionConfig = payload;
  
  executor.loadURL(url.format({
    pathname: path.join(__dirname, "./query-executor-wrapper.html"),
    protocol: "file:",
    slashes: true
  }));

  queryExecutors.push(executor);

  return executor;
}

ipcMain.on("newConnection.createConnection", (event, payload) => {
  createQueryExecutor(payload);
});


ipcMain.on("queryExecutor.initialiseConnectionCallback", (event, payload) => {
  
  if (payload.error !== undefined) {
    console.log(payload.error);
    queryExecutors.pop().close();
    newConnectionDialog.webContents.send("newConnection.initialisationFailed", payload.error);
  }
  else {
    let connectionConfig = getQueryExecutorInstance().connectionConfig;

    if(connectionConfig.isTest) {
      newConnectionDialog.webContents.send("newConnection.connectionTestOk");
      queryExecutors.pop().close();
    }
    else {
      uiWindow.webContents.send("instanceManager.registerNewInstance", { assignedQueryExecutorId: payload.executorId, connectionConfig: connectionConfig });
      
      persistConnection(connectionConfig);

      if (newConnectionDialog) {
        newConnectionDialog.close();
      }
      
    }
  }
  
});

function getQueryExecutorInstance() {
  // Bit of a hack, cant guarantee this was the executor which just got initalised
  // Should pass it back from the executor via the payload
  return queryExecutors[queryExecutors.length - 1];
}

function persistConnection(connectionConfig) {
  savedConnections.push(connectionConfig);

  connectionStore.set(savedConnections);

  console.log(connectionStore.store);  
}

function restoreSavedConnections() {
  for (let connection of connectionStore) {
    createQueryExecutor(connection[1]);
  }
}

// TODO - only send messages to instance manager which will route request to correct webView, rather than 
// sending to all webViews
ipcMain.on("queryExecutor.runQueryComplete", (event, payload) => {
  webContents.getAllWebContents().forEach((w) => {
    w.send("queryExecutor.runQueryComplete", payload);
  })
});

ipcMain.on("queryExecutor.runQuery", (event, payload) => {
  queryExecutors.forEach((executor) => {
    executor.webContents.send("queryExecutor.runQuery", payload);
  });
});

ipcMain.on("queryExecutor.queryTableMetadataComplete", (event, payload) => {
  webContents.getAllWebContents().forEach((w) => {
    w.send("queryExecutor.queryTableMetadataComplete", payload);
  })
});
;
ipcMain.on("queryExecutor.queryTableMetadata", (event, payload) => {
  queryExecutors.forEach((executor) => {
    executor.webContents.send("queryExecutor.queryTableMetadata", payload);
  });
});