insomnia/app/main.js

524 lines
12 KiB
JavaScript
Raw Normal View History

2016-04-20 06:09:46 +00:00
'use strict';
2016-03-22 18:20:05 +00:00
2016-07-18 19:44:46 +00:00
if (require('electron-squirrel-startup')) {
process.exit(0);
}
// Some useful helpers
const IS_DEV = process.env.INSOMNIA_ENV === 'development';
const IS_MAC = process.platform === 'darwin';
const IS_WINDOWS = process.platform === 'win32';
const IS_LINUX = !IS_MAC && !IS_WINDOWS;
2016-08-17 22:44:03 +00:00
var raven = require('raven');
var ravenClient = new raven.Client(
'https://fb3242f902b54cdd934b8ffa204426c0:23430fbe203a' +
2016-08-19 23:12:33 +00:00
'4189a68efb63c38fc50b@app.getsentry.com/88289', {
environment: process.env.INSOMNIA_ENV || 'production',
2016-09-15 22:45:08 +00:00
release: require('./package.json').version,
logger: 'main'
2016-08-19 23:12:33 +00:00
});
2016-08-17 22:44:03 +00:00
2016-09-13 02:09:35 +00:00
if (!IS_DEV) {
ravenClient.patchGlobal();
}
2016-08-17 22:44:03 +00:00
2016-03-22 18:20:05 +00:00
// Don't npm install this (it breaks). Rely on the global one.
2016-08-20 20:41:44 +00:00
const request = require('request');
2016-07-29 17:18:04 +00:00
const path = require('path');
2016-09-15 22:45:08 +00:00
const {version: appVersion, productName: appName} = require('./package.json');
2016-08-20 20:41:44 +00:00
const {LocalStorage} = require('node-localstorage');
const electron = require('electron');
const {
app,
dialog,
2016-08-20 20:41:44 +00:00
shell,
ipcMain,
autoUpdater,
Menu,
BrowserWindow,
webContents
} = require('electron');
2016-07-29 17:18:04 +00:00
2016-07-18 19:44:46 +00:00
const UPDATE_URLS = {
2016-09-21 19:03:40 +00:00
// Add `r` param to help cache bust
2016-09-22 23:18:46 +00:00
darwin: `https://updates.insomnia.rest/builds/check/mac?v=${appVersion}`,
linux: `https://updates.insomnia.rest/builds/check/linux?v=${appVersion}`,
win32: `https://downloads.insomnia.rest/win`
2016-07-18 19:44:46 +00:00
};
2016-07-14 01:12:42 +00:00
2016-08-20 20:41:44 +00:00
const DOWNLOAD_URL = 'http://download.insomnia.rest';
2016-07-07 20:10:55 +00:00
let mainWindow = null;
2016-08-03 16:35:43 +00:00
let localStorage = null;
2016-08-23 20:54:11 +00:00
let hasPromptedForUpdates = false;
2016-03-22 18:20:05 +00:00
2016-05-01 19:56:30 +00:00
// Enable this for CSS grid layout :)
2016-07-16 07:22:08 +00:00
app.commandLine.appendSwitch('enable-experimental-web-platform-features');
2016-05-01 19:56:30 +00:00
2016-09-13 17:29:09 +00:00
process.on('uncaughtException', e => {
if (IS_DEV) {
console.error(e);
} else {
ravenClient.captureError(e, {});
}
});
2016-08-17 22:44:03 +00:00
autoUpdater.on('error', e => {
// Failed to launch auto updater
2016-09-13 02:09:35 +00:00
if (IS_DEV) {
console.error(e);
} else {
ravenClient.captureError(e, {
level: 'warning'
});
2016-09-13 02:09:35 +00:00
}
2016-08-17 22:44:03 +00:00
});
2016-07-14 01:12:42 +00:00
autoUpdater.on('update-not-available', () => {
console.log('-- Update Not Available --')
});
autoUpdater.on('update-available', () => {
console.log('-- Update Available --');
});
autoUpdater.on('update-downloaded', (e, releaseNotes, releaseName, releaseDate, updateUrl) => {
console.log(`-- Update Downloaded ${releaseName} --`);
showUpdateNotification();
2016-07-14 01:12:42 +00:00
});
2016-08-17 22:44:03 +00:00
function checkForUpdates () {
if (hasPromptedForUpdates) {
// We've already prompted for updates. Don't bug the user anymore
2016-09-13 02:09:35 +00:00
return;
}
if (IS_DEV) {
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
console.log('-- Skipping update check in Development --');
2016-08-23 20:54:11 +00:00
return;
}
2016-08-20 20:41:44 +00:00
if (IS_LINUX) {
try {
request.get(UPDATE_URLS.linux, null, (err, response) => {
if (err) {
ravenClient.captureError(err);
return;
}
if (response.statusCode === 200) {
showDownloadModal(response.body);
} else {
// No update available (should be STATUS=204)
}
});
} catch (e) {
ravenClient.captureException(e);
}
} else {
try {
autoUpdater.setFeedURL(UPDATE_URLS[process.platform]);
autoUpdater.checkForUpdates();
} catch (e) {
// This will fail in development
}
2016-08-17 22:44:03 +00:00
}
}
2016-09-13 02:09:35 +00:00
function showUnresponsiveModal () {
dialog.showMessageBox({
type: 'info',
2016-10-11 21:06:25 +00:00
buttons: ['Cancel', 'Reload'],
2016-09-13 02:09:35 +00:00
defaultId: 1,
cancelId: 0,
title: 'Unresponsive',
message: 'Insomnia has become unresponsive. Do you want to reload?'
}, id => {
if (id === 1) {
mainWindow.destroy();
createWindow();
}
});
}
function showUpdateNotification () {
if (hasPromptedForUpdates) {
return;
}
2016-08-23 20:54:11 +00:00
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
}
2016-10-11 21:06:25 +00:00
window.webContents.send('update-available');
hasPromptedForUpdates = true;
}
2016-08-20 20:41:44 +00:00
function showDownloadModal (version) {
2016-08-23 20:54:11 +00:00
hasPromptedForUpdates = true;
2016-08-20 20:41:44 +00:00
dialog.showMessageBox({
type: 'info',
buttons: ['Download', 'Later'],
2016-08-20 20:41:44 +00:00
defaultId: 0,
cancelId: 1,
title: 'Insomnia Update Available',
message: `Exciting news!\n\nVersion ${version} of Insomnia is now available.\n\n\n~Gregory`
2016-08-20 20:41:44 +00:00
}, id => {
if (id === 0) {
console.log('-- Installing Update --');
shell.openExternal(DOWNLOAD_URL);
} else {
console.log('-- Cancel Update --');
}
});
}
ipcMain.on('check-for-updates', () => {
console.log('-- Checking for Updates --');
2016-08-17 22:44:03 +00:00
checkForUpdates();
});
function storeValue (key, obj) {
try {
localStorage.setItem(key, JSON.stringify(obj));
} catch (e) {
console.error('Failed to save to LocalStorage', obj, e);
ravenClient.captureError(e, {
level: 'warning'
});
}
}
function getValue (key, defaultObj) {
try {
return JSON.parse(localStorage.getItem(key) || JSON.stringify(defaultObj))
} catch (e) {
console.error('Failed to get from LocalStorage', e);
ravenClient.captureError(e, {
level: 'warning'
});
}
}
2016-08-17 22:44:03 +00:00
function saveBounds () {
if (!mainWindow) {
return;
}
const fullscreen = mainWindow.isFullScreen();
// Only save the size if we're not in fullscreen
if (!fullscreen) {
storeValue('bounds', mainWindow.getBounds());
storeValue('fullscreen', false);
} else {
storeValue('fullscreen', true);
}
2016-07-29 17:18:04 +00:00
}
2016-08-17 22:44:03 +00:00
function getBounds () {
2016-07-29 17:18:04 +00:00
let bounds = {};
let fullscreen = false;
2016-07-29 17:18:04 +00:00
try {
bounds = getValue('bounds', {});
fullscreen = getValue('fullscreen', false);
2016-07-29 17:18:04 +00:00
} catch (e) {
// This should never happen, but if it does...!
console.error('Failed to parse window bounds', e);
}
return {bounds, fullscreen};
2016-07-29 17:18:04 +00:00
}
2016-08-17 22:44:03 +00:00
function saveZoomFactor (zoomFactor) {
storeValue('zoomFactor', zoomFactor);
2016-07-29 17:18:04 +00:00
}
2016-08-17 22:44:03 +00:00
function getZoomFactor () {
2016-07-29 17:18:04 +00:00
let zoomFactor = 1;
try {
zoomFactor = getValue('zoomFactor', 1);
2016-07-29 17:18:04 +00:00
} catch (e) {
// This should never happen, but if it does...!
console.error('Failed to parse zoomFactor', e);
}
return zoomFactor;
}
2016-07-09 04:49:09 +00:00
// Quit when all windows are closed (except on Mac).
app.on('window-all-closed', () => {
2016-03-23 18:34:39 +00:00
if (!IS_MAC) {
2016-03-22 18:20:05 +00:00
app.quit();
}
});
// Mac-only, when the user clicks the doc icon
app.on('activate', (e, hasVisibleWindows) => {
// Create a new window when clicking the doc icon if there isn't one open
if (!hasVisibleWindows) {
createWindow()
}
});
// When the app is first launched
app.on('ready', () => {
createWindow();
2016-10-11 21:06:25 +00:00
checkForUpdates();
});
2016-08-17 22:44:03 +00:00
function initLocalStorage () {
if (!localStorage) {
localStorage = new LocalStorage(path.join(app.getPath('userData'), 'localStorage'));
}
}
function createWindow () {
initLocalStorage();
2016-08-03 16:35:43 +00:00
2016-07-29 17:18:04 +00:00
const zoomFactor = getZoomFactor();
const {bounds, fullscreen} = getBounds();
const {x, y, width, height} = bounds;
// Make sure we don't place the window outside of the visible space
let maxX = 0;
let maxY = 0;
for (const d of electron.screen.getAllDisplays()) {
// Set the maximum placement location to 50 pixels short of the end
maxX = Math.max(maxX, d.bounds.x + d.bounds.width - 50);
maxY = Math.max(maxY, d.bounds.y + d.bounds.height - 50);
}
const finalX = Math.min(maxX, x);
const finalY = Math.min(maxX, y);
2016-03-22 18:20:05 +00:00
2016-07-29 17:18:04 +00:00
mainWindow = new BrowserWindow({
// Make sure we don't initialize the window outside the bounds
x: finalX,
y: finalY,
fullscreen: fullscreen,
fullscreenable: true,
2016-09-13 02:09:35 +00:00
title: appName,
2016-07-29 17:18:04 +00:00
width: width || 1200,
height: height || 600,
minHeight: 500,
minWidth: 500,
acceptFirstMouse: true,
webPreferences: {
zoomFactor: zoomFactor
2016-07-27 20:07:50 +00:00
}
2016-07-29 17:18:04 +00:00
});
2016-07-29 17:18:04 +00:00
mainWindow.on('resize', e => saveBounds());
mainWindow.on('move', e => saveBounds());
2016-09-13 02:09:35 +00:00
mainWindow.on('unresponsive', e => showUnresponsiveModal());
2016-04-20 16:15:19 +00:00
2016-07-29 17:18:04 +00:00
// and load the app.html of the app.
2016-09-17 05:46:44 +00:00
mainWindow.loadURL(`file://${__dirname}/main.html`);
2016-07-27 20:07:50 +00:00
2016-07-29 17:18:04 +00:00
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// 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;
});
2016-07-27 20:07:50 +00:00
2016-08-25 19:44:51 +00:00
require('electron-context-menu')({});
2016-08-22 20:08:22 +00:00
var template = [
{
label: "Application",
submenu: [
{
label: `About ${appName}`,
2016-08-22 20:05:42 +00:00
role: 'about',
visible: IS_MAC
},
{
type: "separator",
visible: IS_MAC
},
{
label: "Preferences",
accelerator: "CmdOrCtrl+,",
2016-08-22 20:05:42 +00:00
click: function (menuItem, window, e) {
if (!window || !window.webContents) {
return
2016-08-23 20:54:11 +00:00
}
window.webContents.send('toggle-preferences');
}
},
{
2016-08-23 20:54:11 +00:00
type: "separator",
visible: IS_MAC
},
{
2016-08-23 20:54:11 +00:00
role: "hide",
visible: IS_MAC
},
{
role: "hideothers",
visible: IS_MAC
},
{type: "separator"},
{
label: "Quit",
accelerator: "Command+Q",
click: function () {
app.quit();
}
}
]
2016-08-22 20:08:22 +00:00
},
{
label: "Edit",
submenu: [{
label: "Undo",
accelerator: "CmdOrCtrl+Z",
selector: "undo:"
}, {
label: "Redo",
accelerator: "Shift+CmdOrCtrl+Z",
selector: "redo:"
}, {
type: "separator"
}, {
label: "Cut",
accelerator: "CmdOrCtrl+X",
selector: "cut:"
}, {
label: "Copy",
accelerator: "CmdOrCtrl+C",
selector: "copy:"
}, {
label: "Paste",
accelerator: "CmdOrCtrl+V",
selector: "paste:"
}, {
label: "Select All",
accelerator: "CmdOrCtrl+A",
selector: "selectAll:"
}]
},
{
label: "View",
submenu: [
{
role: 'togglefullscreen'
},
{
label: "Actual Size",
accelerator: "CmdOrCtrl+0",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
2016-09-13 02:09:35 +00:00
}
const zoomFactor = 1;
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-08-22 20:08:22 +00:00
}
},
{
label: "Zoom In",
accelerator: IS_MAC ? "CmdOrCtrl+Plus" : "CmdOrCtrl+=",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
2016-09-13 02:09:35 +00:00
}
2016-10-26 22:13:16 +00:00
const zoomFactor = Math.min(1.8, getZoomFactor() + 0.05);
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-08-22 20:08:22 +00:00
}
},
{
label: "Zoom Out",
accelerator: "CmdOrCtrl+-",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
2016-09-13 02:09:35 +00:00
}
2016-10-26 22:13:16 +00:00
const zoomFactor = Math.max(0.5, getZoomFactor() - 0.05);
window.webContents.setZoomFactor(zoomFactor);
saveZoomFactor(zoomFactor);
2016-08-22 20:08:22 +00:00
}
},
{
label: "Toggle Sidebar",
accelerator: "CmdOrCtrl+\\",
click: () => {
const window = BrowserWindow.getFocusedWindow();
if (!window || !window.webContents) {
return;
}
window.webContents.send('toggle-sidebar');
}
}
2016-08-22 20:08:22 +00:00
]
}, {
label: "Window",
role: "window",
submenu: [
{
role: 'minimize'
},
{
role: 'close'
}
2016-08-22 20:08:22 +00:00
]
}, {
label: "Help",
role: "help",
id: "help",
submenu: [
{
label: "Contact Support",
2016-08-22 20:08:22 +00:00
click: () => {
shell.openExternal('mailto:support@insomnia.rest');
2016-08-22 20:08:22 +00:00
}
},
{
2016-09-13 17:39:55 +00:00
label: "Insomnia Help",
2016-08-22 20:08:22 +00:00
accelerator: "CmdOrCtrl+?",
click: () => {
shell.openExternal('http://docs.insomnia.rest');
2016-08-22 20:08:22 +00:00
}
}
2016-08-22 20:08:22 +00:00
]
}
];
2016-04-20 17:38:27 +00:00
2016-07-29 17:18:04 +00:00
if (IS_DEV) {
template.push({
label: 'Developer',
position: 'before=help',
submenu: [{
label: 'Reload',
accelerator: 'Command+R',
2016-08-17 22:44:03 +00:00
click: function () {
2016-07-29 17:18:04 +00:00
mainWindow.reload();
}
}, {
label: 'Toggle DevTools',
accelerator: 'Alt+Command+I',
2016-08-17 22:44:03 +00:00
click: function () {
2016-07-29 17:18:04 +00:00
mainWindow.toggleDevTools();
}
}]
});
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}