bitwarden-estensione-browser/src/main/menu.main.ts

417 lines
17 KiB
TypeScript
Raw Normal View History

2018-02-08 21:58:47 +01:00
import {
app,
BrowserWindow,
2018-02-12 22:07:14 +01:00
clipboard,
2018-02-09 19:21:23 +01:00
dialog,
2018-02-10 21:30:42 +01:00
ipcMain,
2018-02-08 21:58:47 +01:00
Menu,
2018-02-13 21:00:38 +01:00
MenuItem,
2018-02-08 21:58:47 +01:00
MenuItemConstructorOptions,
2018-02-09 18:36:37 +01:00
shell,
2018-02-08 21:58:47 +01:00
} from 'electron';
2018-02-14 05:38:18 +01:00
import { Main } from '../main';
2018-04-24 22:30:47 +02:00
import { BaseMenu } from 'jslib/electron/baseMenu';
2018-04-24 22:30:47 +02:00
import { isMacAppStore, isSnapStore, isWindowsStore } from 'jslib/electron/utils';
2018-02-09 06:21:00 +01:00
2018-02-19 14:52:53 +01:00
import { ConstantsService } from 'jslib/services/constants.service';
export class MenuMain extends BaseMenu {
2018-02-14 06:26:32 +01:00
menu: Menu;
updateMenuItem: MenuItem;
addNewLogin: MenuItem;
addNewItem: MenuItem;
addNewFolder: MenuItem;
syncVault: MenuItem;
settings: MenuItem;
lockNow: MenuItem;
logOut: MenuItem;
twoStepLogin: MenuItem;
changeEmail: MenuItem;
changeMasterPass: MenuItem;
premiumMembership: MenuItem;
passwordGenerator: MenuItem;
2018-02-18 04:37:43 +01:00
passwordHistory: MenuItem;
2018-02-14 06:26:32 +01:00
searchVault: MenuItem;
unlockedRequiredMenuItems: MenuItem[] = [];
constructor(private main: Main) {
super(main.i18nService, main.windowMain, 'Bitwarden', () => this.main.messagingService.send('logout'));
}
2018-02-08 19:10:13 +01:00
init() {
this.initProperties();
2018-02-14 05:06:45 +01:00
this.initContextMenu();
this.initApplicationMenu();
2018-02-14 06:26:32 +01:00
this.updateMenuItem = this.menu.getMenuItemById('checkForUpdates');
this.addNewLogin = this.menu.getMenuItemById('addNewLogin');
this.addNewItem = this.menu.getMenuItemById('addNewItem');
this.addNewFolder = this.menu.getMenuItemById('addNewFolder');
this.syncVault = this.menu.getMenuItemById('syncVault');
this.settings = this.menu.getMenuItemById('settings');
this.lockNow = this.menu.getMenuItemById('lockNow');
this.logOut = this.menu.getMenuItemById('logOut');
this.twoStepLogin = this.menu.getMenuItemById('twoStepLogin');
this.changeEmail = this.menu.getMenuItemById('changeEmail');
this.changeMasterPass = this.menu.getMenuItemById('changeMasterPass');
this.premiumMembership = this.menu.getMenuItemById('premiumMembership');
this.passwordGenerator = this.menu.getMenuItemById('passwordGenerator');
2018-02-18 04:37:43 +01:00
this.passwordHistory = this.menu.getMenuItemById('passwordHistory');
2018-02-14 06:26:32 +01:00
this.searchVault = this.menu.getMenuItemById('searchVault');
this.unlockedRequiredMenuItems = [
this.addNewLogin, this.addNewItem, this.addNewFolder,
this.syncVault, this.settings, this.lockNow, this.twoStepLogin, this.changeEmail,
2018-02-18 04:37:43 +01:00
this.changeMasterPass, this.premiumMembership, this.passwordGenerator, this.passwordHistory,
this.searchVault];
2018-02-14 06:26:32 +01:00
this.updateApplicationMenuState(false, true);
}
updateApplicationMenuState(isAuthenticated: boolean, isLocked: boolean) {
this.unlockedRequiredMenuItems.forEach((mi: MenuItem) => {
mi.enabled = isAuthenticated && !isLocked;
});
this.logOut.enabled = isAuthenticated;
2018-02-14 05:06:45 +01:00
}
private initApplicationMenu() {
2018-02-28 04:15:52 +01:00
const accountSubmenu: MenuItemConstructorOptions[] = [
{
label: this.main.i18nService.t('changeMasterPass'),
id: 'changeMasterPass',
click: async () => {
const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('changeMasterPass'),
message: this.main.i18nService.t('changeMasterPass'),
detail: this.main.i18nService.t('changeMasterPasswordConfirmation'),
buttons: [this.main.i18nService.t('yes'), this.main.i18nService.t('no')],
cancelId: 1,
defaultId: 0,
noLink: true,
});
if (result === 0) {
await this.openWebVault();
}
},
},
{
label: this.main.i18nService.t('changeEmail'),
id: 'changeEmail',
click: async () => {
const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('changeEmail'),
message: this.main.i18nService.t('changeEmail'),
detail: this.main.i18nService.t('changeEmailConfirmation'),
buttons: [this.main.i18nService.t('yes'), this.main.i18nService.t('no')],
cancelId: 1,
defaultId: 0,
noLink: true,
});
if (result === 0) {
await this.openWebVault();
}
},
},
{
label: this.main.i18nService.t('twoStepLogin'),
id: 'twoStepLogin',
click: async () => {
const result = dialog.showMessageBox(this.main.windowMain.win, {
title: this.main.i18nService.t('twoStepLogin'),
message: this.main.i18nService.t('twoStepLogin'),
detail: this.main.i18nService.t('twoStepLoginConfirmation'),
buttons: [this.main.i18nService.t('yes'), this.main.i18nService.t('no')],
cancelId: 1,
defaultId: 0,
noLink: true,
});
if (result === 0) {
await this.openWebVault();
}
},
},
{ type: 'separator' },
this.logOutMenuItemOptions,
2018-02-28 04:15:52 +01:00
];
2018-04-03 23:03:11 +02:00
if (!isMacAppStore() && !isWindowsStore()) {
2018-02-28 04:15:52 +01:00
accountSubmenu.unshift({
label: this.main.i18nService.t('premiumMembership'),
click: () => this.main.messagingService.send('openPremium'),
id: 'premiumMembership',
});
}
2018-02-08 19:10:13 +01:00
const template: MenuItemConstructorOptions[] = [
2018-02-08 21:58:47 +01:00
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('file'),
2018-02-08 21:58:47 +01:00
submenu: [
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('addNewLogin'),
click: () => this.main.messagingService.send('newLogin'),
2018-02-10 21:30:42 +01:00
accelerator: 'CmdOrCtrl+N',
2018-02-14 06:26:32 +01:00
id: 'addNewLogin',
2018-02-09 06:21:00 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('addNewItem'),
2018-02-14 06:26:32 +01:00
id: 'addNewItem',
2018-02-08 21:58:47 +01:00
submenu: [
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('typeLogin'),
click: () => this.main.messagingService.send('newLogin'),
accelerator: 'CmdOrCtrl+Shift+L',
2018-02-08 21:58:47 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('typeCard'),
click: () => this.main.messagingService.send('newCard'),
accelerator: 'CmdOrCtrl+Shift+C',
2018-02-08 21:58:47 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('typeIdentity'),
click: () => this.main.messagingService.send('newIdentity'),
accelerator: 'CmdOrCtrl+Shift+I',
2018-02-08 21:58:47 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('typeSecureNote'),
click: () => this.main.messagingService.send('newSecureNote'),
accelerator: 'CmdOrCtrl+Shift+S',
2018-02-10 21:30:42 +01:00
},
],
2018-02-08 21:58:47 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('addNewFolder'),
2018-02-14 06:26:32 +01:00
id: 'addNewFolder',
2018-02-14 05:38:18 +01:00
click: () => this.main.messagingService.send('newFolder'),
2018-02-08 21:58:47 +01:00
},
2018-02-09 06:21:00 +01:00
{ type: 'separator' },
2018-02-09 19:21:23 +01:00
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('syncVault'),
2018-02-14 06:26:32 +01:00
id: 'syncVault',
2018-02-14 05:38:18 +01:00
click: () => this.main.messagingService.send('syncVault'),
2018-02-09 19:21:23 +01:00
},
2018-02-10 21:30:42 +01:00
],
2018-02-08 21:58:47 +01:00
},
this.editMenuItemOptions,
2018-02-08 19:10:13 +01:00
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('view'),
submenu: ([
2018-02-18 04:37:43 +01:00
{
label: this.main.i18nService.t('searchVault'),
id: 'searchVault',
click: () => this.main.messagingService.send('focusSearch'),
accelerator: 'CmdOrCtrl+F',
},
{ type: 'separator' },
2018-02-09 06:21:00 +01:00
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('passwordGenerator'),
2018-02-14 06:26:32 +01:00
id: 'passwordGenerator',
2018-02-14 05:38:18 +01:00
click: () => this.main.messagingService.send('openPasswordGenerator'),
2018-02-10 21:30:42 +01:00
accelerator: 'CmdOrCtrl+G',
2018-02-09 18:12:41 +01:00
},
2018-02-09 06:21:00 +01:00
{
2018-02-18 04:37:43 +01:00
label: this.main.i18nService.t('passwordHistory'),
id: 'passwordHistory',
click: () => this.main.messagingService.send('openPasswordHistory'),
2018-02-09 06:21:00 +01:00
},
2018-02-08 19:10:13 +01:00
{ type: 'separator' },
] as MenuItemConstructorOptions[]).concat(this.viewSubMenuItemOptions),
2018-02-08 19:10:13 +01:00
},
2018-02-08 21:58:47 +01:00
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('account'),
2018-02-28 04:15:52 +01:00
submenu: accountSubmenu,
2018-02-08 21:58:47 +01:00
},
this.windowMenuItemOptions,
2018-02-08 19:10:13 +01:00
{
2018-02-26 03:37:00 +01:00
label: this.main.i18nService.t('help'),
2018-02-08 19:10:13 +01:00
role: 'help',
submenu: [
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('emailUs'),
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('mailTo:hello@bitwarden.com'),
2018-02-09 18:36:37 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('visitOurWebsite'),
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://bitwarden.com/contact'),
2018-02-09 18:36:37 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('fileBugReport'),
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://github.com/bitwarden/desktop'),
2018-02-09 18:36:37 +01:00
},
{ type: 'separator' },
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('followUs'),
2018-02-09 19:21:23 +01:00
submenu: [
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('blog'),
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://blog.bitwarden.com'),
2018-02-09 19:21:23 +01:00
},
{
label: 'Twitter',
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://twitter.com/bitwarden_app'),
2018-02-09 19:21:23 +01:00
},
{
label: 'Facebook',
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://www.facebook.com/bitwarden/'),
2018-02-09 19:21:23 +01:00
},
{
label: 'Google+',
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://plus.google.com/114869903467947368993'),
2018-02-09 19:21:23 +01:00
},
{
label: 'GitHub',
2018-02-10 22:06:39 +01:00
click: () => shell.openExternal('https://github.com/bitwarden'),
2018-02-10 21:30:42 +01:00
},
],
2018-02-09 18:36:37 +01:00
},
2018-02-09 19:21:23 +01:00
{ type: 'separator' },
2018-02-09 18:36:37 +01:00
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('goToWebVault'),
2018-02-19 14:52:53 +01:00
click: async () => await this.openWebVault(),
2018-02-09 18:36:37 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('getMobileApp'),
2018-02-09 19:21:23 +01:00
submenu: [
{
label: 'iOS',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://itunes.apple.com/app/' +
'bitwarden-free-password-manager/id1137397744?mt=8');
2018-02-10 21:30:42 +01:00
},
2018-02-09 19:21:23 +01:00
},
{
label: 'Android',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://play.google.com/store/apps/' +
'details?id=com.x8bit.bitwarden');
2018-02-10 21:30:42 +01:00
},
},
],
2018-02-09 18:36:37 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('getBrowserExtension'),
2018-02-09 19:21:23 +01:00
submenu: [
{
label: 'Chrome',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://chrome.google.com/webstore/detail/' +
2018-04-17 21:07:11 +02:00
'bitwarden-free-password-m/nngceckbapebfimnlniiiahkandclblb');
2018-02-10 21:30:42 +01:00
},
2018-02-09 19:21:23 +01:00
},
{
label: 'Firefox',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://addons.mozilla.org/firefox/addon/' +
'bitwarden-password-manager/');
2018-02-10 21:30:42 +01:00
},
2018-02-09 19:21:23 +01:00
},
{
label: 'Opera',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://addons.opera.com/extensions/details/' +
'bitwarden-free-password-manager/');
2018-02-10 21:30:42 +01:00
},
2018-02-09 19:21:23 +01:00
},
{
label: 'Edge',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://www.microsoft.com/store/p/' +
'bitwarden-free-password-manager/9p6kxl0svnnl');
2018-02-10 21:30:42 +01:00
},
2018-02-09 19:21:23 +01:00
},
{
label: 'Safari',
2018-02-10 21:30:42 +01:00
click: () => {
2018-02-09 19:21:23 +01:00
shell.openExternal('https://safari-extensions.apple.com/details/' +
'?id=com.bitwarden.safari-LTZ2PFU5D6');
2018-02-10 21:30:42 +01:00
},
},
],
},
],
},
2018-02-08 19:10:13 +01:00
];
2018-02-10 23:07:46 +01:00
const firstMenuOptions: MenuItemConstructorOptions[] = [
{ type: 'separator' },
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('settings'),
2018-02-14 06:26:32 +01:00
id: 'settings',
2018-02-14 05:38:18 +01:00
click: () => this.main.messagingService.send('openSettings'),
2018-02-10 23:07:46 +01:00
},
{
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('lockNow'),
2018-02-14 06:26:32 +01:00
id: 'lockNow',
2018-02-14 05:38:18 +01:00
click: () => this.main.messagingService.send('lockVault'),
2018-02-10 23:07:46 +01:00
accelerator: 'CmdOrCtrl+L',
},
];
2018-02-14 04:33:01 +01:00
const updateMenuItem = {
2018-02-14 05:38:18 +01:00
label: this.main.i18nService.t('checkForUpdates'),
click: () => this.main.updaterMain.checkForUpdate(true),
2018-02-14 04:33:01 +01:00
id: 'checkForUpdates',
};
2018-02-08 19:10:13 +01:00
if (process.platform === 'darwin') {
2018-02-10 23:07:46 +01:00
const firstMenuPart: MenuItemConstructorOptions[] = [
2018-02-23 05:19:35 +01:00
{
label: this.main.i18nService.t('aboutBitwarden'),
role: 'about',
},
2018-02-10 23:07:46 +01:00
];
2018-02-27 05:43:27 +01:00
if (!isMacAppStore()) {
firstMenuPart.push(updateMenuItem);
}
2018-02-10 23:07:46 +01:00
template.unshift({
label: this.appName,
2018-02-10 23:07:46 +01:00
submenu: firstMenuPart.concat(firstMenuOptions, [
{ type: 'separator' },
], this.macAppMenuItemOptions),
2018-02-10 23:07:46 +01:00
});
2018-02-08 19:10:13 +01:00
// Window menu
template[template.length - 2].submenu = this.macWindowSubmenuOptions;
2018-02-10 23:07:46 +01:00
} else {
2018-02-12 22:07:14 +01:00
// File menu
2018-02-10 23:07:46 +01:00
template[0].submenu = (template[0].submenu as MenuItemConstructorOptions[]).concat(
firstMenuOptions);
2018-02-12 22:07:14 +01:00
// About menu
2018-03-15 18:00:08 +01:00
const aboutMenuAdditions: MenuItemConstructorOptions[] = [
{ type: 'separator' },
];
if (!isWindowsStore() && !isSnapStore()) {
2018-03-15 18:00:08 +01:00
aboutMenuAdditions.push(updateMenuItem);
}
aboutMenuAdditions.push(this.aboutMenuItemOptions);
2018-03-15 18:00:08 +01:00
2018-02-12 22:07:14 +01:00
template[template.length - 1].submenu =
2018-03-15 18:00:08 +01:00
(template[template.length - 1].submenu as MenuItemConstructorOptions[]).concat(aboutMenuAdditions);
2018-02-08 21:58:47 +01:00
}
2018-02-08 19:10:13 +01:00
2018-02-14 06:26:32 +01:00
this.menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(this.menu);
2018-02-08 19:10:13 +01:00
}
2018-02-19 14:52:53 +01:00
private async openWebVault() {
let webUrl = 'https://vault.bitwarden.com';
const urlsObj: any = await this.main.storageService.get(ConstantsService.environmentUrlsKey);
if (urlsObj != null) {
if (urlsObj.base != null) {
webUrl = urlsObj.base;
} else if (urlsObj.webVault != null) {
webUrl = urlsObj.webVault;
}
}
shell.openExternal(webUrl);
}
2018-02-08 19:10:13 +01:00
}