| |
| |
| |
| |
| |
| |
| |
| |
| const axios = require('axios'); |
| const fs = require('fs'); |
| const path = require('path'); |
| const misc = require('../js/misc.js'); |
| const spawn = require('child_process').spawn; |
| const {EOL} = require('os'); |
|
|
| let pgadminServerProcess = null; |
| let startPageUrl = null; |
| let serverCheckUrl = null; |
| let addMenuCompleted = false; |
| let pgAdminMainScreen = null; |
|
|
| let serverPort = 5050; |
| let appStartTime = (new Date()).getTime(); |
|
|
| let docsURLSubStrings = ['www.enterprisedb.com', 'www.postgresql.org', 'www.pgadmin.org', 'help/help']; |
|
|
| |
| let pythonPath = misc.getPythonPath(); |
| let pgadminFile = '../web/pgAdmin4.py'; |
| let configFile = '../web/config.py'; |
|
|
| |
| if (fs.existsSync('dev_config.json')) { |
| try { |
| let dev_config = JSON.parse(fs.readFileSync('dev_config.json')); |
| pythonPath = dev_config['pythonPath']; |
| pgadminFile = dev_config['pgadminFile']; |
| } catch (error) { |
| |
| } |
| } |
|
|
| |
| |
| function startDesktopMode() { |
| |
| |
| if (pgadminServerProcess != null) |
| return; |
|
|
| let UUID = crypto.randomUUID(); |
| |
| |
| process.env.PGADMIN_INT_PORT = serverPort; |
| process.env.PGADMIN_INT_KEY = UUID; |
| process.env.PGADMIN_SERVER_MODE = 'OFF'; |
|
|
| |
| startPageUrl = 'http://127.0.0.1:' + serverPort + '/?key=' + UUID; |
| serverCheckUrl = 'http://127.0.0.1:' + serverPort + '/misc/ping?key=' + UUID; |
|
|
| document.getElementById('loader-text-status').innerHTML = 'Starting pgAdmin 4...'; |
|
|
| |
| misc.writeServerLog('pgAdmin Runtime Environment'); |
| misc.writeServerLog('--------------------------------------------------------'); |
| let command = path.resolve(pythonPath) + ' -s ' + path.resolve(pgadminFile); |
| misc.writeServerLog('Python Path: "' + path.resolve(pythonPath) + '"'); |
| misc.writeServerLog('Runtime Config File: "' + path.resolve(misc.getRunTimeConfigFile()) + '"'); |
| misc.writeServerLog('pgAdmin Config File: "' + path.resolve(configFile) + '"'); |
| misc.writeServerLog('Webapp Path: "' + path.resolve(pgadminFile) + '"'); |
| misc.writeServerLog('pgAdmin Command: "' + command + '"'); |
| misc.writeServerLog('Environment: '); |
| Object.keys(process.env).forEach(function (key) { |
| |
| |
| |
| if (platform() === 'darwin' && key === 'PATH') { |
| let updated_path = process.env[key] + ':/usr/local/bin'; |
| process.env[key] = updated_path; |
| } |
|
|
| if (platform() === 'win32' && key.toUpperCase() === 'PATH') { |
| let _libpq_path = path.join(path.dirname(path.dirname(path.resolve(pgadminFile))), 'runtime'); |
| process.env[key] = _libpq_path + ';' + process.env[key]; |
| } |
|
|
| misc.writeServerLog(' - ' + key + ': ' + process.env[key]); |
| }); |
| misc.writeServerLog('--------------------------------------------------------\n'); |
|
|
| |
| let spawnStartTime = (new Date).getTime(); |
| pgadminServerProcess = spawn(path.resolve(pythonPath), ['-s', path.resolve(pgadminFile)]); |
| pgadminServerProcess.on('error', function (err) { |
| |
| misc.writeServerLog('Failed to launch pgAdmin4. Error:'); |
| misc.writeServerLog(err); |
| }); |
| let spawnEndTime = (new Date).getTime(); |
| misc.writeServerLog('Total spawn time to start the pgAdmin4 server: ' + (spawnEndTime - spawnStartTime) / 1000 + ' Sec'); |
|
|
| pgadminServerProcess.stdout.setEncoding('utf8'); |
| pgadminServerProcess.stdout.on('data', (chunk) => { |
| misc.writeServerLog(chunk); |
| }); |
|
|
| pgadminServerProcess.stderr.setEncoding('utf8'); |
| pgadminServerProcess.stderr.on('data', (chunk) => { |
| misc.writeServerLog(chunk); |
| }); |
|
|
| |
| |
| function pingServer() { |
| return axios.get(serverCheckUrl); |
| } |
|
|
| let connectionTimeout = misc.ConfigureStore.get('connectionTimeout', 90) * 1000; |
| let currentTime = (new Date).getTime(); |
| let endTime = currentTime + connectionTimeout; |
| let midTime1 = currentTime + (connectionTimeout / 2); |
| let midTime2 = currentTime + (connectionTimeout * 2 / 3); |
| let pingInProgress = false; |
|
|
| |
| let pingStartTime = (new Date).getTime(); |
| let intervalID = setInterval(function () { |
| |
| |
| if (pingInProgress) |
| return; |
|
|
| pingServer().then(() => { |
| pingInProgress = false; |
| document.getElementById('loader-text-status').innerHTML = 'pgAdmin 4 started'; |
| |
| misc.setProcessObject(pgadminServerProcess); |
|
|
| clearInterval(intervalID); |
| let appEndTime = (new Date).getTime(); |
| misc.writeServerLog('------------------------------------------'); |
| misc.writeServerLog('Total time taken to ping pgAdmin4 server: ' + (appEndTime - pingStartTime) / 1000 + ' Sec'); |
| misc.writeServerLog('------------------------------------------'); |
| misc.writeServerLog('Total launch time of pgAdmin4: ' + (appEndTime - appStartTime) / 1000 + ' Sec'); |
| misc.writeServerLog('------------------------------------------'); |
| launchPgAdminWindow(); |
| }).catch(() => { |
| pingInProgress = false; |
| let curTime = (new Date).getTime(); |
| |
| |
| if (curTime >= endTime) { |
| clearInterval(intervalID); |
| splashWindow.hide(); |
|
|
| nw.Window.open('src/html/server_error.html', { |
| 'frame': true, |
| 'width': 790, |
| 'height': 430, |
| 'position': 'center', |
| 'resizable': false, |
| 'focus': true, |
| 'show': true, |
| }); |
| } |
|
|
| if (curTime > midTime1) { |
| if (curTime < midTime2) { |
| document.getElementById('loader-text-status').innerHTML = 'Taking longer than usual...'; |
| } else { |
| document.getElementById('loader-text-status').innerHTML = 'Almost there...'; |
| } |
| } else { |
| document.getElementById('loader-text-status').innerHTML = 'Waiting for pgAdmin 4 to start...'; |
| } |
| }); |
|
|
| pingInProgress = true; |
| }, 250); |
| } |
|
|
| |
| |
| function launchPgAdminWindow() { |
| |
| misc.writeServerLog('Application Server URL: ' + startPageUrl); |
| nw.Window.open(startPageUrl, { |
| 'id': 'pgadmin-main', |
| 'icon': '../../assets/pgAdmin4.png', |
| 'frame': true, |
| 'position': 'center', |
| 'resizable': true, |
| 'min_width': 640, |
| 'min_height': 480, |
| 'width': 1024, |
| 'height': 768, |
| 'focus': true, |
| 'show': false, |
| }, (pgadminWindow) => { |
| pgAdminMainScreen = pgadminWindow; |
| |
| misc.setPgAdminWindowObject(pgadminWindow); |
|
|
| |
| pgadminWindow.zoomLevel = misc.ConfigureStore.get('zoomLevel', 0); |
|
|
| |
| misc.setZoomEvents(); |
|
|
| pgadminWindow.on('closed', function () { |
| misc.cleanupAndQuitApp(); |
| }); |
|
|
| |
| |
| pgadminWindow.on('new-win-policy', function (frame, url, policy) { |
| if (!frame) { |
| let openDocsInBrowser = misc.ConfigureStore.get('openDocsInBrowser', true); |
| let isDocURL = false; |
| docsURLSubStrings.forEach(function (key) { |
| if (url.indexOf(key) >= 0) { |
| isDocURL = true; |
| } |
|
|
| }); |
|
|
|
|
| if (openDocsInBrowser && isDocURL) { |
| |
| policy.ignore(); |
| |
| nw.Shell.openExternal(url); |
| } else { |
| policy.setNewWindowManifest({ |
| 'icon': '../../assets/pgAdmin4.png', |
| 'frame': true, |
| 'position': 'center', |
| 'min_width': 640, |
| 'min_height': 480, |
| 'width': pgadminWindow.width, |
| 'height': pgadminWindow.height, |
| }); |
| } |
| } |
| }); |
|
|
| pgadminWindow.on('loaded', function () { |
| |
| |
| |
| |
| pgadminWindow.window.hookConsole((method, args)=>{ |
| misc.writeServerLog( |
| `--------------[UI ${method}]---------------${EOL}${misc.parseConsoleArgs(method, args)}${EOL}------------[UI End]----------------`); |
| }); |
| pgadminWindow.window.opener = null; |
|
|
| |
| pgadminWindow.show(); |
| pgadminWindow.focus(); |
|
|
| nativeMenu = new gui.Menu({ type: 'menubar' }); |
| |
| if (platform() === 'darwin') { |
| nativeMenu.createMacBuiltin('pgAdmin 4'); |
| |
| nativeMenu?.items[0].submenu.removeAt(0); |
| |
| nativeMenu?.items[2].submenu.removeAt(1); |
| pgAdminMainScreen.menu = nativeMenu; |
| } |
|
|
| try { |
| pgAdminMainScreen.isCustomMenusAdded = false; |
| let addMenuInterval = setInterval(() => { |
| if (pgadminWindow?.window?.pgAdmin?.Browser?.Events && pgadminWindow?.window?.pgAdmin?.Browser?.MainMenus?.length > 0) { |
| pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-enable-disable-menu-items', enableDisableMenuItem); |
| pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-refresh-menu-item', refreshMenuItems); |
| pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-update-checked-menu-item', updateCheckedMenuItem); |
| pgadminWindow.window.pgAdmin.Browser.Events.on('pgadmin:nw-set-new-window-open-size', setNewWindowSize) |
| |
| pgadminWindow.window.pgAdmin.Browser.MainMenus.forEach((menu)=> { |
| addMenu(menu) |
| }) |
| clearInterval(addMenuInterval); |
| } |
| }, 250) |
| } catch (e) { |
| console.error('Error in add native menus'); |
| } |
|
|
| |
| splashWindow.hide(); |
| }); |
|
|
| pgadminWindow.on('blur', function () { |
| misc.unregisterZoomEvents(); |
| }); |
|
|
| pgadminWindow.on('focus', function () { |
| misc.registerZoomEvents(); |
| }); |
| }); |
| } |
|
|
| |
| let gui = require('nw.gui'); |
| let splashWindow = gui.Window.get(); |
|
|
| |
| let isDragging = false; |
| let dragOrigin = { x: 0, y: 0 }; |
| document.mouseleave = () => isDragging = false; |
| document.onmouseup = () => isDragging = false; |
|
|
| document.onmousedown = (e) => { |
| isDragging = true; |
| dragOrigin.x = e.x; |
| dragOrigin.y = e.y; |
| }; |
|
|
| document.onmousemove = (e) => { |
| if (isDragging) { |
| splashWindow.moveTo(e.screenX - dragOrigin.x, e.screenY - dragOrigin.y); |
| } |
| }; |
|
|
| |
| nw.App.clearCache(); |
|
|
| let nativeMenu; |
|
|
| splashWindow.on('loaded', function () { |
| |
| misc.ConfigureStore.init(); |
|
|
| let fixedPortCheck = misc.ConfigureStore.get('fixedPort', false); |
| if (fixedPortCheck) { |
| serverPort = misc.ConfigureStore.get('portNo'); |
| |
| startDesktopMode(); |
| } else { |
| |
| misc.getAvailablePort(0) |
| .then((pythonApplicationPort) => { |
| serverPort = pythonApplicationPort; |
| |
| startDesktopMode(); |
| }) |
| .catch((errCode) => { |
| if (errCode === 'EADDRINUSE') { |
| alert('The port specified is already in use. Please enter a free port number.'); |
| } else { |
| alert(errCode); |
| } |
| }); |
| } |
| }); |
|
|
| splashWindow.on('close', function () { |
| misc.cleanupAndQuitApp(); |
| }); |
|
|
| function setNewWindowSize(){ |
| misc.setZoomLevelForAllWindows(); |
| } |
|
|
|
|
| function addCommonMenus(menu) { |
| let _menu = new gui.Menu(); |
|
|
| menu.menuItems.forEach((menuItem) => { |
| let submenu = getSubMenu(menuItem); |
|
|
| let _menuItem = new gui.MenuItem({ |
| label: menuItem.label, |
| enabled: !menuItem.isDisabled, |
| type: menuItem.type || 'normal', |
| priority: menuItem.priority, |
| ...(submenu.items.length > 0) && { |
| submenu: submenu, |
| }, |
| click: function () { |
| menuItem.callback(); |
| }, |
| }); |
| _menu.append(_menuItem); |
| }); |
|
|
| if (menu.name == 'file') { |
| let runtimeMenu = getRuntimeMenu(); |
| _menu.append(runtimeMenu); |
| } |
|
|
| if (menu.menuItems.length == 0) { |
| let _menuItem = new gui.MenuItem({ |
| label: 'No object selected', |
| enabled: false, |
| priority: 0, |
| }); |
| _menu.append(_menuItem); |
| } |
|
|
| if (platform() == 'darwin') { |
| pgAdminMainScreen.menu.insert(new gui.MenuItem({ |
| label: menu.label, |
| name: menu.name, |
| submenu: _menu, |
| }), menu.index); |
| } else { |
| nativeMenu.append(new gui.MenuItem({ |
| label: menu.label, |
| name: menu.name, |
| submenu: _menu, |
| })); |
| pgAdminMainScreen.menu = nativeMenu; |
| } |
|
|
| } |
|
|
| function getRuntimeMenu() { |
| let subMenus = new gui.Menu(); |
| let rtmenudt = pgAdminMainScreen.window.pgAdmin.Browser.RUNTIME_MENUS_OPTIONS['runtime'] |
| let runtimeSubMenus = pgAdminMainScreen.window.pgAdmin.Browser.RUNTIME_MENUS_OPTIONS['runtime']['submenus'] |
| subMenus.append(new gui.MenuItem({ |
| label: runtimeSubMenus['configure'].label, |
| enabled: runtimeSubMenus['configure'].enable, |
| priority: runtimeSubMenus['configure'].priority, |
| type: 'normal', |
| checked: false, |
| click: function () { |
| |
| nw.Window.open('src/html/configure.html', { |
| 'frame': true, |
| 'width': 600, |
| 'height': 585, |
| 'position': 'center', |
| 'resizable': false, |
| 'focus': true, |
| 'show': true, |
| }); |
| }, |
| })); |
| subMenus.append(new gui.MenuItem({ |
| label: runtimeSubMenus['view_log'].label, |
| enabled: runtimeSubMenus['view_log'].enable, |
| priority: runtimeSubMenus['view_log'].priority, |
| type: 'normal', |
| checked: false, |
| click: function () { |
| |
| nw.Window.open('src/html/view_log.html', { |
| 'frame': true, |
| 'width': 790, |
| 'height': 425, |
| 'position': 'center', |
| 'resizable': false, |
| 'focus': true, |
| 'show': true, |
| }); |
| }, |
| })); |
| subMenus.append(new nw.MenuItem({ type: 'separator' })); |
| subMenus.append(new gui.MenuItem({ |
| label: pgAdminMainScreen?.isFullscreen ? runtimeSubMenus['exit_full_screen'].label : runtimeSubMenus['enter_full_screen'].label, |
| enabled: runtimeSubMenus['enter_full_screen'].enable, |
| priority: runtimeSubMenus['enter_full_screen'].priority, |
| type: 'normal', |
| checked: false, |
| key: runtimeSubMenus['enter_full_screen'].key, |
| modifiers: runtimeSubMenus['enter_full_screen'].modifiers, |
| click: function () { |
| this.label = !pgAdminMainScreen?.isFullscreen ? runtimeSubMenus['exit_full_screen'].label : runtimeSubMenus['enter_full_screen'].label; |
| misc.toggleFullScreen(); |
| }, |
| })); |
| subMenus.append(new gui.MenuItem({ |
| label: runtimeSubMenus['actual_size'].label, |
| enabled: runtimeSubMenus['actual_size'].enable, |
| priority: runtimeSubMenus['actual_size'].priority, |
| type: 'normal', |
| checked: false, |
| key: runtimeSubMenus['actual_size'].key, |
| modifiers: runtimeSubMenus['actual_size'].modifiers, |
| click: function () { |
| misc.actualSize(); |
| }, |
| })); |
| subMenus.append(new gui.MenuItem({ |
| label: runtimeSubMenus['zoom_in'].label, |
| enabled: runtimeSubMenus['zoom_in'].enable, |
| priority: runtimeSubMenus['zoom_in'].priority, |
| type: 'normal', |
| checked: false, |
| key: runtimeSubMenus['zoom_in'].key, |
| modifiers: runtimeSubMenus['zoom_in'].modifiers, |
| click: function () { |
| misc.zoomIn(); |
| }, |
| })); |
| subMenus.append(new gui.MenuItem({ |
| label: runtimeSubMenus['zoom_out'].label, |
| enabled: runtimeSubMenus['zoom_out'].enable, |
| priority: runtimeSubMenus['zoom_out'].priority, |
| type: 'normal', |
| checked: false, |
| key: runtimeSubMenus['zoom_out'].key, |
| modifiers: runtimeSubMenus['zoom_out'].modifiers, |
| click: function () { |
| misc.zoomOut(); |
| }, |
| })); |
|
|
| let runtimeMenu = new gui.MenuItem({ |
| label: rtmenudt.label, |
| enabled: true, |
| priority: rtmenudt.priority, |
| type: 'normal', |
| checked: false, |
| submenu: subMenus, |
| }) |
|
|
| return runtimeMenu; |
|
|
| } |
|
|
| function getSubMenu(menuItem) { |
| let submenu = new gui.Menu(); |
| if (menuItem.menu_items) { |
| menuItem.menu_items.forEach((item) => { |
| let menuType = typeof item.checked == 'boolean' ? 'checkbox' : item.type; |
| submenu.append(new gui.MenuItem({ |
| label: item.label, |
| enabled: !item.isDisabled, |
| priority: item.priority, |
| type: menuType, |
| checked: item.checked, |
| click: function () { |
| if (menuType == 'checkbox') { |
| pgAdminMainScreen.menu.items.forEach(el => { |
| el.submenu.items.forEach((sub) => { |
| if (sub.submenu?.items?.length) { |
| sub.submenu.items.forEach((m) => { |
| if (m.type == 'checkbox') { |
| m.label == item.label ? m.checked = true : m.checked = false; |
| } |
| }); |
| } |
| }); |
| }); |
| } |
| item.callback(); |
| }, |
| })); |
| }); |
| } |
| return submenu; |
| } |
|
|
| function addMacMenu(menu) { |
| if (menu.name == 'file' && platform() === 'darwin') { |
| let rootMenu = nativeMenu.items[0].submenu; |
| let indx = 0; |
| menu.menuItems.forEach((menuItem) => { |
| let submenu = getSubMenu(menuItem); |
|
|
| rootMenu.insert( |
| new gui.MenuItem({ |
| label: menuItem.label, |
| type: menuItem.type || 'normal', |
| enabled: !menuItem.isDisabled, |
| priority: menuItem.priority, |
| ...(submenu.items.length > 0) && { |
| submenu: submenu, |
| }, |
| click: function () { |
| |
| menuItem.callback(); |
| }, |
| }), indx); |
| indx++; |
| }); |
| let runtimeMenu = getRuntimeMenu(); |
| rootMenu.insert(runtimeMenu, indx++); |
| let separator_menu = new nw.MenuItem({ type: 'separator' }); |
| rootMenu.insert(separator_menu, indx); |
| indx++; |
|
|
| pgAdminMainScreen.menu = nativeMenu; |
| } else { |
| addCommonMenus(menu) |
| } |
| } |
|
|
| function addOtherOsMenu(menu) { |
| addCommonMenus(menu) |
| } |
|
|
|
|
| function addMenu(menu) { |
| pgAdminMainScreen.isCustomMenusAdded = true; |
| if (platform() === 'darwin') { |
| addMacMenu(menu); |
| } else { |
| addOtherOsMenu(menu); |
| } |
| addMenuCompleted = true; |
| } |
|
|
| function enableDisableMenuItem(menu, menuItem) { |
| if (addMenuCompleted) { |
| |
| pgAdminMainScreen.menu.items.forEach(el => { |
| if (el?.label == menu?.label) { |
| el.submenu.items.forEach((sub) => { |
| if (sub.label == menuItem.label) { |
| sub.enabled = !menuItem.isDisabled; |
| } |
| }); |
| } |
| }); |
| } |
| } |
|
|
| function updateCheckedMenuItem(menuItem) { |
| |
| pgAdminMainScreen.menu.items.forEach(el => { |
| el.submenu.items.forEach((sub) => { |
| if(sub.label == menuItem.parentMenu.label) { |
| sub.submenu.items.forEach((sm)=> { |
| if (sm.label == menuItem.label && sm.type == 'checkbox') { |
| sm.checked = menuItem.checked |
| } |
| }) |
| } else if (sub.label == menuItem.label && type == 'checkbox') { |
| sub.checked = menuItem.checked |
| } |
| }); |
| }); |
| } |
|
|
| function refreshMenuItems(menu) { |
| |
| pgAdminMainScreen.menu.items.forEach(el => { |
| if (el.label == menu.label) { |
| let totalSubItems = el.submenu.items.length; |
|
|
| |
| for (let i = 0; i < totalSubItems; i++) { |
| el.submenu.removeAt(0); |
| } |
| menu.menuItems.forEach((item) => { |
|
|
| let submenu = new gui.Menu(); |
| if (item.menu_items) { |
| item.menu_items.forEach((subItem) => { |
| submenu.append(new gui.MenuItem({ |
| label: subItem.label, |
| enabled: !subItem.isDisabled, |
| priority: subItem.priority, |
| type: [true, false].includes(subItem.checked) ? 'checkbox' : 'normal', |
| checked: subItem.checked, |
| click: function () { |
| subItem.callback(); |
| }, |
| })); |
| }); |
| } |
| let _menuItem = new gui.MenuItem({ |
| label: item.label, |
| enabled: !item.isDisabled, |
| priority: item.priority, |
| type: item.type, |
| ...(submenu.items.length > 0) && { |
| submenu: submenu, |
| }, |
| click: function () { |
| item.callback(); |
| }, |
| }); |
|
|
| el.submenu.append(_menuItem); |
| }); |
| } |
| }); |
|
|
| } |
|
|