Skip to content

Commit 7e5e0bb

Browse files
WaleyChenrueckstiess
authored andcommitted
INT-677 autofill connection dialog from mongoldb URI in clipboard
1 parent f86cf98 commit 7e5e0bb

File tree

4 files changed

+107
-43
lines changed

4 files changed

+107
-43
lines changed

package.json

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
"./src/index.js"
1919
],
2020
"ignore": [
21-
"bootstrap",
22-
"font-awesome",
23-
"octicons",
24-
"app",
25-
"auto-updater",
26-
"crash-reporter",
27-
"dialog",
28-
"browser-window",
29-
"menu",
30-
"jade",
31-
"scout-server",
32-
"glob",
33-
"electron-squirrel-startup",
34-
"ipc",
35-
"keytar"
21+
"app",
22+
"auto-updater",
23+
"bootstrap",
24+
"browser-window",
25+
"clipboard",
26+
"crash-reporter",
27+
"dialog",
28+
"electron-squirrel-startup",
29+
"font-awesome",
30+
"glob",
31+
"ipc",
32+
"jade",
33+
"keytar",
34+
"menu",
35+
"octicons",
36+
"scout-server"
3637
]
3738
},
3839
"fonts": [

src/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ app.extend({
287287
sendMessage: function(msg, arg1) {
288288
ipc.send('message', msg, arg1);
289289
},
290-
onMessageReceived: function(msg) {
290+
onMessageReceived: function(msg, arg1) {
291291
debug('message received from main process:', msg);
292-
this.trigger(msg);
292+
this.trigger(msg, arg1);
293293
},
294294
metrics: metrics,
295295
init: function() {

src/connect/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ var ConnectView = View.extend({
227227
this.replaceAuthMethodFields.bind(this));
228228
this.listenToAndRun(this, 'change:sslMethod',
229229
this.replaceSslMethodFields.bind(this));
230+
this.listenTo(app, 'update-connection',
231+
this.updateConnectionFromMsg.bind(this));
230232

231233
// always start in NEW_EMPTY state
232234
this.dispatch('new connection clicked');
@@ -308,6 +310,16 @@ var ConnectView = View.extend({
308310
});
309311
},
310312

313+
updateConnectionFromMsg: function(connection) {
314+
this.connection = new Connection();
315+
/* eslint guard-for-in: 0 */
316+
for (var attr in connection) {
317+
this.connection[attr] = connection[attr];
318+
}
319+
/* eslint guard-for-in: 1 */
320+
this.updateForm();
321+
},
322+
311323
/**
312324
*
313325
* remove favorite, then saves the connection (if it has been used before)

src/electron/window-manager.js

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
* [BrowserWindow](https://github.com/atom/electron/blob/master/docs/api/browser-window.md)
44
* class
55
*/
6-
var path = require('path');
7-
var _ = require('lodash');
8-
var app = require('app');
6+
97
var AppMenu = require('./menu');
108
var BrowserWindow = require('browser-window');
9+
var Clipboard = require('clipboard');
10+
var Connection = require('mongodb-connection-model');
11+
var Notifier = require('node-notifier');
12+
13+
var _ = require('lodash');
14+
var app = require('app');
1115
var config = require('./config');
1216
var debug = require('debug')('scout-electron:window-manager');
1317
var dialog = require('dialog');
14-
var Notifier = require('node-notifier');
18+
var path = require('path');
1519

1620
/**
1721
* When running in electron, we're in `RESOURCES/src/electron`.
1822
*/
1923
var RESOURCES = path.resolve(__dirname, '../../');
24+
var SCOUT_ICON_PATH = RESOURCES + '/images/scout.png';
2025

2126
/**
2227
* The app's HTML shell which is the output of `./src/index.jade`
@@ -30,12 +35,56 @@ var DEFAULT_URL = 'file://' + path.join(RESOURCES, 'index.html#connect');
3035
* so we'll use scope to essentially make it a Singleton.
3136
*/
3237
var connectWindow;
38+
var curNotifierOnClickFn;
39+
var lastClipboardTxt;
3340

3441
// @todo (imlucas): Removed in setup branch as we dont need to do this anymore
3542
// as a `all-windows-closed` event has been added to the `app` event api
3643
// since this code was laid down.
3744
var windowsOpenCount = 0;
3845

46+
var autofillConnect = function() {
47+
debug('autofillConnect()');
48+
var connection = Connection.from(Clipboard.readText()).toJSON();
49+
connectWindow.webContents.send('message', 'update-connection', connection);
50+
};
51+
52+
function isConnectDialog(url) {
53+
return url === DEFAULT_URL;
54+
}
55+
56+
function isMongoURI(str) {
57+
return str.indexOf('mongodb://') > -1;
58+
}
59+
60+
// returns true if the application is a single instance application otherwise
61+
// focus the second window (which we'll quit from) and return false
62+
// see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md
63+
function isSingleInstance(_window) {
64+
var isNotSingle = app.makeSingleInstance(function(commandLine, workingDirectory) {
65+
debug('Someone tried to run a second instance! We should focus our window', {
66+
commandLine: commandLine,
67+
workingDirectory: workingDirectory
68+
});
69+
if (_window) {
70+
if (_window.isMinimized()) {
71+
_window.restore();
72+
}
73+
_window.focus();
74+
}
75+
return true;
76+
});
77+
78+
return !isNotSingle;
79+
}
80+
81+
function openDevTools() {
82+
debug('openDevTools()');
83+
AppMenu.lastFocusedWindow.openDevTools({
84+
detach: true
85+
});
86+
}
87+
3988
/**
4089
* Call me instead of using `new BrowserWindow()` directly because i'll:
4190
*
@@ -66,23 +115,7 @@ module.exports.create = function(opts) {
66115
});
67116
AppMenu.load(_window);
68117

69-
// makes the application a single instance application
70-
// see "app.makeSingleInstance" in https://github.com/atom/electron/blob/master/docs/api/app.md
71-
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
72-
debug('Someone tried to run a second instance! We should focus our window', {
73-
commandLine: commandLine,
74-
workingDirectory: workingDirectory
75-
});
76-
if (_window) {
77-
if (_window.isMinimized()) {
78-
_window.restore();
79-
}
80-
_window.focus();
81-
}
82-
return true;
83-
});
84-
85-
if (shouldQuit) {
118+
if (!isSingleInstance(_window)) {
86119
app.quit();
87120
return null;
88121
}
@@ -99,9 +132,28 @@ module.exports.create = function(opts) {
99132
});
100133
});
101134

102-
if (opts.url === DEFAULT_URL) { // if it's the connect dialog
135+
if (isConnectDialog(opts.url)) {
103136
AppMenu.hideConnect(_window);
104137
connectWindow = _window;
138+
connectWindow.on('focus', function() {
139+
debug('connect window focused.');
140+
var cbTxt = Clipboard.readText();
141+
if (cbTxt === lastClipboardTxt) {
142+
return;
143+
}
144+
lastClipboardTxt = cbTxt;
145+
146+
if (isMongoURI(cbTxt)) {
147+
debug('mongoURI detected.');
148+
curNotifierOnClickFn = autofillConnect;
149+
Notifier.notify({
150+
'icon': SCOUT_ICON_PATH,
151+
'message': 'Click this notification to autofill the connection fields from your clipboard.',
152+
'title': 'Autofill Connection',
153+
'wait': true
154+
});
155+
}
156+
});
105157
connectWindow.on('closed', function() {
106158
debug('connect window closed.');
107159
connectWindow = null;
@@ -166,8 +218,9 @@ app.on('show share submenu', function() {
166218

167219
app.on('show bugsnag OS notification', function(errorMsg) {
168220
if (_.contains(['development', 'testing'], process.env.NODE_ENV)) {
221+
curNotifierOnClickFn = openDevTools;
169222
Notifier.notify({
170-
'icon': RESOURCES + '/images/scout.png',
223+
'icon': SCOUT_ICON_PATH,
171224
'message': errorMsg,
172225
'title': 'MongoDB Compass Exception',
173226
'wait': true
@@ -185,9 +238,7 @@ app.on('ready', function() {
185238
app.emit('show connect dialog');
186239

187240
Notifier.on('click', function() {
188-
AppMenu.lastFocusedWindow.openDevTools({
189-
detach: true
190-
});
241+
curNotifierOnClickFn();
191242
});
192243
});
193244

0 commit comments

Comments
 (0)