-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathElectronUtils.cpp
325 lines (249 loc) · 9.43 KB
/
ElectronUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* InterSpec: an application to analyze spectral gamma radiation data.
Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC
(NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
Government retains certain rights in this software.
For questions contact William Johnson via email at [email protected], or
alternative emails of [email protected], or [email protected].
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "InterSpec_config.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <csignal>
//#include <unistd.h>
#include <sys/stat.h>
#include <string>
#include <memory>
#include <Wt/WServer>
#include <Wt/WIOService>
#include <Wt/WApplication>
#include "SpecUtils/Filesystem.h"
#include "SpecUtils/SerialToDetectorModel.h"
#include "InterSpec/InterSpec.h"
#include "InterSpec/InterSpecApp.h"
#include "InterSpec/DataBaseUtils.h"
#include "InterSpec/InterSpecServer.h"
#include "InterSpec/UndoRedoManager.h"
#include "InterSpec/MassAttenuationTool.h"
#include "InterSpec/DataBaseVersionUpgrade.h"
#include "target/electron/ElectronUtils.h"
#include "target/electron/InterSpecAddOn.h"
using namespace std;
namespace ElectronUtils
{
bool requestNewCleanSession()
{
auto app = dynamic_cast<InterSpecApp *>(wApp);
const string oldexternalid = app ? app->externalToken() : string();
if( !oldexternalid.empty() )
{
//Have electron reload the page.
ElectronUtils::send_nodejs_message("NewCleanSession", "");
#if( USING_ELECTRON_NATIVE_MENU )
//should check ns_externalid==oldexternalid
string js;
//Speed up loading by deferring calls to Menu.setApplicationMenu() until app
// is fully reloaded.
js += "$(window).data('HaveTriggeredMenuUpdate',null);";
//Just in case the page reload doesnt go through, make sure menus will get updated eventually
// (this shouldnt be necassary, right?)
js += "setTimeout(function(){$(window).data('HaveTriggeredMenuUpdate',true);},5000);";
wApp->doJavaScript(js);
#endif
return true;
}else
{
cerr << "requestNewCleanSession(): failed; couldnt get external token." << endl;
}
return false;
}//void requestNewCleanSession()
bool notifyNodeJsOfNewSessionLoad()
{
auto app = dynamic_cast<InterSpecApp *>(wApp);
if( !app )
{
cerr << "Error: notifyNodeJsOfNewSessionLoad: wApp is null!!!" << endl;
return false;
}
const string oldexternalid = app->externalToken();
if( !oldexternalid.empty() )
ElectronUtils::send_nodejs_message("SessionFinishedLoading", "");
app->triggerUpdate();
return true;
}//bool notifyNodeJsOfNewSessionLoad( const std::string sessionid )
void send_nodejs_message( const std::string msg_name, const std::string msg_data )
{
auto app = dynamic_cast<InterSpecApp *>(wApp);
if( !app )
{
cerr << "Error: send_nodejs_message: wApp is null!!!" << endl;
return;
}
const string session_token = app->externalToken();
Wt::WServer *server = Wt::WServer::instance();
assert( server );
Wt::WIOService &io = server->ioService();
io.boost::asio::io_service::post( [=](){
InterSpecAddOn::send_nodejs_message( session_token, msg_name, msg_data );
} );
}//void send_nodejs_message(...)
bool handle_message_from_nodejs( const std::string &session_token,
const std::string &msg_name, const std::string &msg_data )
{
InterSpecApp *app = InterSpecApp::instanceFromExtenalToken( session_token );
if( !app )
{
// We will get here if the app-instance with this token hasnt yet loaded
cerr << "Failed to find app instance for token='" << session_token << "'" << endl;
//assert( 0 );
return false;
}//if( !app )
Wt::WApplication::UpdateLock lock( app );
if( !lock )
{
cerr << "Failed to get WApplication::UpdateLock lock token='" << session_token << "'" << endl;
return false;
}
// TODO: maybe we should make a own function for each of the cases below; maybe make things both
// clearer here, as well as in main.js
if( msg_name == "..." )
{
}
else if( msg_name == "OnMaximize" )
{
cout << "\n\nOnMaximize\n\n";
app->doJavaScript( "Wt.WT.TitleBarChangeMaximized(true);" );
}else if( msg_name == "OnUnMaximize" )
{
cout << "\n\nOnUnMaximize\n\n";
app->doJavaScript( "Wt.WT.TitleBarChangeMaximized(false);" );
}else if( msg_name == "OnBlur" )
{
app->doJavaScript( "$('.app-titlebar').addClass('inactive');" );
}else if( msg_name == "OnFocus" )
{
app->doJavaScript( "$('.app-titlebar').removeClass('inactive');" );
}else if( msg_name == "OnLeaveFullScreen" )
{
cout << "Left to fullscreen." << endl;
}else if( msg_name == "OnEnterFullScreen" )
{
cout << "Went to fullscreen." << endl;
}else
{
cerr << "Unrecognized msg_name from nodejs: '" << msg_name << "'" << endl;
return false;
}
app->triggerUpdate();
return true;
}//handle_message_from_nodejs(...)
bool browse_for_directory( const std::string &window_title,
const std::string &window_message,
std::function<void(std::string)> callback )
{
InterSpecApp *app = dynamic_cast<InterSpecApp *>( Wt::WApplication::instance() );
if( !app )
throw runtime_error( "ElectronUtils::browse_for_directory(): must be called from within Wt event-loop." );
if( !InterSpecApp::isPrimaryWindowInstance() )
{
cerr << "Browse for directory should only be called from a primary instance\n";
assert( 0 );
return false;
}
//session_token
assert( callback );
if( !callback )
return false;
const string session_id = app->sessionId();
std::function<void(std::string)> wrapped_callback = [session_id,callback](string result_path){
Wt::WServer *server = Wt::WServer::instance();
if( !server ){
cerr << "browse_for_directory callback wrapper: WServer no longer available." << endl;
assert( 0 );
return;
}
server->post(session_id, [callback,result_path](){
Wt::WApplication *app = Wt::WApplication::instance();
if( !app )
return;
callback( result_path );
app->triggerUpdate();
});
};//wrapped_callback(...)
const string token = app->externalToken();
auto worker = [=](){
InterSpecAddOn::browse_for_directory( token, window_title, window_message, wrapped_callback );
};
Wt::WServer *server = Wt::WServer::instance();
assert( server );
Wt::WIOService &io = server->ioService();
io.boost::asio::io_service::post( worker );
return true;
}//bool browse_for_directory(...)
}//namespace ElectronUtils
int interspec_start_server( const char *process_name, const char *userdatadir,
const char *basedir, const char *xml_config_path,
const unsigned short int server_port_num )
{
return InterSpecServer::start_server( process_name, userdatadir, basedir, xml_config_path, server_port_num );
}//int interspec_start_server( int argc, char *argv[] )
void interspec_set_require_session_token( const bool require_token )
{
InterSpecServer::set_require_tokened_sessions( require_token );
}
void interspec_set_max_undo_steps( const int max_items )
{
UndoRedoManager::setMaxUndoRedoSteps( max_items );
}
void interspec_add_allowed_primary_session_token( const char *session_token )
{
InterSpecServer::add_allowed_session_token( session_token, InterSpecServer::SessionType::PrimaryAppInstance );
}//void interspec_add_allowed_primary_session_token( const char *session_id )
void interspec_add_allowed_external_session_token( const char *session_token )
{
InterSpecServer::add_allowed_session_token( session_token, InterSpecServer::SessionType::ExternalBrowserInstance );
}
int interspec_remove_allowed_session_token( const char *session_token )
{
return InterSpecServer::remove_allowed_session_token( session_token );
}//int interspec_remove_allowed_session_token( const char *session_token )
int interspec_session_is_alive( const char *session_token )
{
const int status = InterSpecServer::session_status( session_token );
return (status == 2);
}
int interspec_open_file( const char *session_token, const char *files_json )
{
return InterSpecServer::open_file_in_session( session_token, files_json );
}
bool interspec_open_app_url( const char *session_token, const char *files_json )
{
return InterSpecServer::pass_app_url_to_session( session_token, files_json );
}
bool interspec_set_initial_file_to_open( const char *session_token, const char *file_path )
{
try
{
InterSpecServer::set_file_to_open_on_load( session_token, file_path );
}catch( std::exception &e )
{
cerr << "interspec_set_initial_file_to_open: " << e.what() << endl;
return false;
}
return true;
}
void interspec_kill_server()
{
InterSpecServer::killServer();
}//void interspec_kill_server()