@@ -35,10 +35,30 @@ if (env.name !== 'production') {
35
35
app . setPath ( 'userData' , userDataPath + ' (' + env . name + ')' ) ;
36
36
}
37
37
38
+ const processProtocolURI = ( uri ) => {
39
+ if ( uri && uri . startsWith ( 'rocketchat://' ) ) {
40
+ const site = uri . split ( / \/ | \? / ) [ 2 ] ;
41
+ if ( site ) {
42
+ let scheme = 'https://' ;
43
+ if ( uri . includes ( 'insecure=true' ) ) {
44
+ scheme = 'http://' ;
45
+ }
46
+ return scheme + site ;
47
+ }
48
+ }
49
+ } ;
50
+ const processProtocolArgv = ( argv ) => {
51
+ const protocolURI = argv . find ( arg => arg . startsWith ( 'rocketchat://' ) ) ;
52
+ if ( protocolURI ) {
53
+ return processProtocolURI ( protocolURI ) ;
54
+ }
55
+ } ;
56
+ let mainWindow = null ;
57
+
38
58
app . on ( 'ready' , function ( ) {
39
59
setApplicationMenu ( ) ;
40
60
41
- const mainWindow = createWindow ( 'main' , {
61
+ mainWindow = createWindow ( 'main' , {
42
62
width : 1000 ,
43
63
titleBarStyle : 'hidden' ,
44
64
height : 600
@@ -55,8 +75,77 @@ app.on('ready', function () {
55
75
if ( env . name === 'development' ) {
56
76
mainWindow . openDevTools ( ) ;
57
77
}
78
+ if ( process . argv . length > 1 ) {
79
+ const site = processProtocolArgv ( process . argv ) ;
80
+ if ( site ) {
81
+ const dialog = require ( 'electron' ) . dialog ;
82
+ dialog . showMessageBox ( {
83
+ type : 'question' ,
84
+ buttons : [ 'Add' , 'Cancel' ] ,
85
+ defaultId : 0 ,
86
+ title : 'Add Server' ,
87
+ message : `Do you want to add "${ site } " to your list of servers?`
88
+ } , ( response ) => {
89
+ if ( response === 0 ) {
90
+ mainWindow . send ( 'add-host' , site ) ;
91
+ }
92
+ } ) ;
93
+ }
94
+ }
95
+
58
96
} ) ;
59
97
60
98
app . on ( 'window-all-closed' , function ( ) {
61
99
app . quit ( ) ;
62
100
} ) ;
101
+ const appIsReady = new Promise ( resolve => {
102
+ if ( app . isReady ( ) ) {
103
+ resolve ( ) ;
104
+ } else {
105
+ app . on ( 'ready' , resolve ) ;
106
+ }
107
+ } ) ;
108
+
109
+
110
+ if ( process . platform === 'darwin' ) {
111
+ // Open protocol urls on mac as open-url is not yet implemented on other OS's
112
+ app . on ( 'open-url' , function ( e , url ) {
113
+ e . preventDefault ( ) ;
114
+ const site = processProtocolURI ( url ) ;
115
+ if ( site ) {
116
+ appIsReady . then ( ( ) => {
117
+ mainWindow . send ( 'add-host' , site ) ;
118
+ } ) ;
119
+ }
120
+ } ) ;
121
+ } else {
122
+ const shouldQuit = app . makeSingleInstance ( ( argv ) => {
123
+ // Someone tried to run a second instance, we should focus our window.
124
+ const site = processProtocolArgv ( argv ) ;
125
+ if ( site ) {
126
+ const dialog = require ( 'electron' ) . dialog ;
127
+ dialog . showMessageBox ( {
128
+ type : 'question' ,
129
+ buttons : [ 'Add' , 'Cancel' ] ,
130
+ defaultId : 0 ,
131
+ title : 'Add Server' ,
132
+ message : `Do you want to add "${ site } " to your list of servers?`
133
+ } , ( response ) => {
134
+ if ( response === 0 ) {
135
+ mainWindow . send ( 'add-host' , site ) ;
136
+ }
137
+ } ) ;
138
+ }
139
+ if ( mainWindow ) {
140
+ if ( mainWindow . isMinimized ( ) ) {
141
+ mainWindow . restore ( ) ;
142
+ }
143
+ mainWindow . show ( ) ;
144
+ mainWindow . focus ( ) ;
145
+ }
146
+ } ) ;
147
+
148
+ if ( shouldQuit ) {
149
+ app . quit ( ) ;
150
+ }
151
+ }
0 commit comments