FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/desktop/DesktopMainWindow.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
desktop
/
DesktopMainWindow.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
597 lines (495 loc) · 16.4 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
desktop
/
DesktopMainWindow.cpp
Copy path
File metadata and controls
597 lines (495 loc) · 16.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* DesktopMainWindow.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
"
DesktopMainWindow.hpp
"
#
include
<
QToolBar
>
#
include
<
QWebChannel
>
#
include
<
boost/bind.hpp
>
#
include
<
boost/format.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/Macros.hpp
>
#
include
<
core/text/TemplateFilter.hpp
>
#
include
"
DesktopOptions.hpp
"
#
include
"
DesktopSlotBinders.hpp
"
#
include
"
DesktopSessionLauncher.hpp
"
#
include
"
DesktopJobLauncherOverlay.hpp
"
#
include
"
RemoteDesktopSessionLauncherOverlay.hpp
"
#
include
"
DockTileView.hpp
"
#
include
"
DesktopActivationOverlay.hpp
"
#
include
"
DesktopSessionServersOverlay.hpp
"
#
include
"
DesktopRCommandEvaluator.hpp
"
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
desktop
{
namespace
{
#
ifdef
_WIN32
void
CALLBACK
onDialogStart
(
HWINEVENTHOOK
hook,
DWORD
event,
HWND
hwnd,
LONG
idObject,
LONG
idChild,
DWORD
dwEventThread,
DWORD
dwmsEventTime)
{
::BringWindowToTop
(hwnd);
}
#
endif
}
//
end anonymous namespace
MainWindow::MainWindow
(QUrl url,
bool
isRemoteDesktop) :
GwtWindow
(
false
,
false
, QString(), url,
nullptr
,
nullptr
, isRemoteDesktop),
isRemoteDesktop_
(isRemoteDesktop),
menuCallback_
(
this
),
gwtCallback_
(
this
,
this
, isRemoteDesktop),
pSessionLauncher_
(
nullptr
),
pRemoteSessionLauncher_
(
nullptr
),
pLauncher_
(
new
JobLauncher(
this
)),
pCurrentSessionProcess_
(
nullptr
)
{
RCommandEvaluator::setMainWindow
(
this
);
pToolbar_->
setVisible
(
false
);
#
ifdef
_WIN32
eventHook_ =
nullptr
;
#
endif
//
bind GWT callbacks
auto
* channel =
webPage
()->
webChannel
();
channel->
registerObject
(
QStringLiteral
(
"
desktop
"
), &gwtCallback_);
if
(isRemoteDesktop_)
{
//
since the object registration is asynchronous, during the GWT setup code
//
there is a race condition where the initialization can happen before the
//
remoteDesktop object is registered, making the GWT application think that
//
it should use regular desktop objects - to circumvent this, we use a custom
//
user agent string that the GWT code can detect with 100% success rate to
//
get around this race condition
QString userAgent =
webPage
()->
profile
()->
httpUserAgent
().
append
(
QStringLiteral
(
"
; RStudio Remote Desktop
"
));
webPage
()->
profile
()->
setHttpUserAgent
(userAgent);
channel->
registerObject
(
QStringLiteral
(
"
remoteDesktop
"
), &gwtCallback_);
}
channel->
registerObject
(
QStringLiteral
(
"
desktopMenuCallback
"
), &menuCallback_);
//
Dummy menu bar to deal with the fact that
//
the real menu bar isn't ready until well
//
after startup.
#
ifndef
Q_OS_MAC
auto
* pMainMenuStub =
new
QMenuBar
(
this
);
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
File
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Edit
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Code
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
View
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Plots
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Session
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Build
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Debug
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Profile
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Tools
"
));
pMainMenuStub->
addMenu
(
QString::fromUtf8
(
"
Help
"
));
setMenuBar
(pMainMenuStub);
#
endif
connect
(&menuCallback_,
SIGNAL
(
menuBarCompleted
(QMenuBar*)),
this
,
SLOT
(
setMenuBar
(QMenuBar*)));
connect
(&menuCallback_,
SIGNAL
(
commandInvoked
(QString)),
this
,
SLOT
(
invokeCommand
(QString)));
connect
(&menuCallback_,
SIGNAL
(
zoomActualSize
()),
this
,
SLOT
(
zoomActualSize
()));
connect
(&menuCallback_,
SIGNAL
(
zoomIn
()),
this
,
SLOT
(
zoomIn
()));
connect
(&menuCallback_,
SIGNAL
(
zoomOut
()),
this
,
SLOT
(
zoomOut
()));
connect
(&gwtCallback_,
SIGNAL
(
workbenchInitialized
()),
this
,
SIGNAL
(
firstWorkbenchInitialized
()));
connect
(&gwtCallback_,
SIGNAL
(
workbenchInitialized
()),
this
,
SLOT
(
onWorkbenchInitialized
()));
connect
(&gwtCallback_,
SIGNAL
(
sessionQuit
()),
this
,
SLOT
(
onSessionQuit
()));
connect
(
webView
(),
SIGNAL
(
onCloseWindowShortcut
()),
this
,
SLOT
(
onCloseWindowShortcut
()));
connect
(
webView
(), &WebView::urlChanged,
this
, &MainWindow::onUrlChanged);
connect
(
webView
(), &WebView::loadFinished,
this
, &MainWindow::onLoadFinished);
connect
(
webPage
(), &QWebEnginePage::loadFinished,
&menuCallback_, &MenuCallback::cleanUpActions);
connect
(&
desktopInfo
(), &DesktopInfo::fixedWidthFontListChanged, [
this
]() {
QString js =
QStringLiteral
(
"
if (typeof window.onFontListReady === 'function') window.onFontListReady()
"
);
this
->
webPage
()->
runJavaScript
(js);
});
connect
(qApp,
SIGNAL
(
commitDataRequest
(QSessionManager&)),
this
,
SLOT
(
commitDataRequest
(QSessionManager&)),
Qt::DirectConnection);
setWindowIcon
(
QIcon
(
QString::fromUtf8
(
"
:/icons/RStudio.ico
"
)));
setWindowTitle
(
desktop::activation
().
editionName
());
#
ifdef
Q_OS_MAC
auto
* pDefaultMenu =
new
QMenuBar
(
this
);
pDefaultMenu->
addMenu
(
new
WindowMenu
());
#
endif
desktop::enableFullscreenMode
(
this
,
true
);
Error error = pLauncher_->
initialize
();
if
(error)
{
LOG_ERROR
(error);
showError
(
nullptr
,
QStringLiteral
(
"
Initialization error
"
),
QStringLiteral
(
"
Could not initialize Job Launcher
"
),
QString
());
::_exit
(
EXIT_FAILURE
);
}
}
bool
MainWindow::isRemoteDesktop
()
const
{
return
isRemoteDesktop_;
}
QString
MainWindow::getSumatraPdfExePath
()
{
return
desktopInfo
().
getSumatraPdfExePath
();
}
void
MainWindow::launchSession
(
bool
reload)
{
//
we're about to start another session, so clear the workbench init flag
//
(will get set again once the new session has initialized the workbench)
workbenchInitialized_ =
false
;
Error error = pSessionLauncher_->
launchNextSession
(reload);
if
(error)
{
LOG_ERROR
(error);
showMessageBox
(QMessageBox::Critical,
this
,
desktop::activation
().
editionName
(),
QString::fromUtf8
(
"
The R session failed to start.
"
),
QString
());
quit
();
}
}
void
MainWindow::launchRStudio
(
const
std::vector<std::string> &args,
const
std::string& initialDir)
{
pAppLauncher_->
launchRStudio
(args, initialDir);
}
void
MainWindow::saveRemoteCookies
()
{
QStringList cookieList;
std::map<std::string, QNetworkCookie> cookies = pRemoteSessionLauncher_->
getCookies
();
for
(
const
std::pair<std::string, QNetworkCookie>& pair : cookies)
{
cookieList.
push_back
(
QString::fromStdString
(pair.
second
.
toRawForm
().
toStdString
()));
}
cookies = pLauncher_->
getCookies
();
for
(
const
std::pair<std::string, QNetworkCookie>& pair : cookies)
{
cookieList.
push_back
(
QString::fromStdString
(pair.
second
.
toRawForm
().
toStdString
()));
}
options
().
setCookies
(cookieList);
}
void
MainWindow::launchRemoteRStudio
()
{
saveRemoteCookies
();
std::vector<std::string> args;
args.
push_back
(
kSessionServerOption
);
args.
push_back
(pRemoteSessionLauncher_->
sessionServer
().
url
());
pAppLauncher_->
launchRStudio
(args);
}
void
MainWindow::launchRemoteRStudioProject
(
const
QString& projectUrl)
{
saveRemoteCookies
();
std::vector<std::string> args;
args.
push_back
(
kSessionServerOption
);
args.
push_back
(pRemoteSessionLauncher_->
sessionServer
().
url
());
args.
push_back
(
kSessionServerUrlOption
);
args.
push_back
(projectUrl.
toStdString
());
pAppLauncher_->
launchRStudio
(args);
}
bool
MainWindow::workbenchInitialized
()
{
return
workbenchInitialized_;
}
void
MainWindow::onWorkbenchInitialized
()
{
//
QTimer::singleShot(300, this, SLOT(resetMargins()));
//
reset state (in case this occurred in response to a manual reload
//
or reload for a new project context)
quitConfirmed_ =
false
;
geometrySaved_ =
false
;
workbenchInitialized_ =
true
;
webPage
()->
runJavaScript
(
QStringLiteral
(
"
window.desktopHooks.getActiveProjectDir()
"
),
[&](QVariant qProjectDir)
{
QString projectDir = qProjectDir.
toString
();
if
(projectDir.
length
() >
0
)
{
setWindowTitle
(
tr
(
"
%1 - %2
"
).
arg
(projectDir).
arg
(
desktop::activation
().
editionName
()));
DockTileView::setLabel
(projectDir);
}
else
{
setWindowTitle
(
desktop::activation
().
editionName
());
DockTileView::setLabel
(
QString
());
}
avoidMoveCursorIfNecessary
();
});
#
ifdef
Q_OS_MAC
webView
()->
setFocus
();
#
endif
}
void
MainWindow::resetMargins
()
{
setContentsMargins
(
0
,
0
,
0
,
0
);
}
//
this notification occurs when windows or X11 is shutting
//
down -- in this case we want to be a good citizen and just
//
exit right away so we notify the gwt callback that a legit
//
quit and exit is on the way and we set the quitConfirmed_
//
flag so no prompting occurs (note that source documents
//
have already been auto-saved so will be restored next time
//
the current project context is opened)
void
MainWindow::commitDataRequest
(QSessionManager &manager)
{
gwtCallback_.
setPendingQuit
(PendingQuitAndExit);
quitConfirmed_ =
true
;
}
void
MainWindow::loadUrl
(
const
QUrl& url)
{
webView
()->
setBaseUrl
(url);
webView
()->
load
(url);
}
void
MainWindow::loadHtml
(
const
QString& html)
{
webView
()->
setHtml
(html);
}
QWebEngineProfile*
MainWindow::getPageProfile
()
{
return
webView
()->
page
()->
profile
();
}
void
MainWindow::quit
()
{
RCommandEvaluator::setMainWindow
(
nullptr
);
quitConfirmed_ =
true
;
close
();
}
void
MainWindow::invokeCommand
(QString commandId)
{
#
ifdef
Q_OS_MAC
QString fmt =
QStringLiteral
(
R"EOF(
var wnd;
try {
wnd = window.$RStudio.last_focused_window;
} catch (e) {
wnd = window;
}
(wnd || window).desktopHooks.invokeCommand('%1');
)EOF"
);
#
else
QString fmt =
QStringLiteral
(
"
window.desktopHooks.invokeCommand('%1')
"
);
#
endif
QString command = fmt.
arg
(commandId);
webPage
()->
runJavaScript
(command);
}
namespace
{
void
closeAllSatellites
(QWidget* mainWindow)
{
for
(
auto
window:
QApplication::topLevelWidgets
())
if
(window != mainWindow)
window->
close
();
}
}
//
end anonymous namespace
void
MainWindow::onSessionQuit
()
{
if
(isRemoteDesktop_)
{
int
pendingQuit =
collectPendingQuitRequest
();
if
(pendingQuit == PendingQuitAndExit || quitConfirmed_)
{
closeAllSatellites
(
this
);
quit
();
}
}
}
void
MainWindow::closeEvent
(QCloseEvent* pEvent)
{
#
ifdef
_WIN32
if
(eventHook_)
::UnhookWinEvent
(eventHook_);
#
endif
desktopInfo
().
onClose
();
if
(!geometrySaved_)
{
desktop::options
().
saveMainWindowBounds
(
this
);
geometrySaved_ =
true
;
}
CloseServerSessions close =
sessionServerSettings
().
closeServerSessionsOnExit
();
if
(quitConfirmed_ ||
(!isRemoteDesktop_ && pCurrentSessionProcess_ ==
nullptr
) ||
(!isRemoteDesktop_ && pCurrentSessionProcess_->
state
() != QProcess::Running))
{
closeAllSatellites
(
this
);
pEvent->
accept
();
return
;
}
auto
quit = [
this
]()
{
closeAllSatellites
(
this
);
this
->
quit
();
};
pEvent->
ignore
();
webPage
()->
runJavaScript
(
QStringLiteral
(
"
!!window.desktopHooks
"
),
[=](QVariant hasQuitR) {
if
(!hasQuitR.
toBool
())
{
LOG_ERROR_MESSAGE
(
"
Main window closed unexpectedly
"
);
//
exit to avoid user having to kill/force-close the application
quit
();
}
else
{
if
(!isRemoteDesktop_ ||
close == CloseServerSessions::Always)
{
webPage
()->
runJavaScript
(
QStringLiteral
(
"
window.desktopHooks.quitR()
"
),
[=](QVariant ignored)
{
quitConfirmed_ =
true
;
});
}
else
if
(close == CloseServerSessions::Never)
{
quit
();
}
else
{
webPage
()->
runJavaScript
(
QStringLiteral
(
"
window.desktopHooks.promptToQuitR()
"
),
[=](QVariant ignored)
{
quitConfirmed_ =
true
;
});
}
}
});
}
void
MainWindow::setMenuBar
(QMenuBar *pMenubar)
{
delete
menuBar
();
this
->
QMainWindow
::setMenuBar
(pMenubar);
}
void
MainWindow::openFileInRStudio
(QString path)
{
QFileInfo
fileInfo
(path);
if
(!fileInfo.
isAbsolute
() || !fileInfo.
exists
() || !fileInfo.
isFile
())
return
;
path = path.
replace
(
QString::fromUtf8
(
"
\\
"
),
QString::fromUtf8
(
"
\\\\
"
))
.
replace
(
QString::fromUtf8
(
"
\"
"
),
QString::fromUtf8
(
"
\\\"
"
))
.
replace
(
QString::fromUtf8
(
"
\n
"
),
QString::fromUtf8
(
"
\\
n
"
));
webView
()->
page
()->
runJavaScript
(
QString::fromUtf8
(
"
window.desktopHooks.openFile(
\"
"
) + path +
QString::fromUtf8
(
"
\"
)
"
));
}
void
MainWindow::onPdfViewerClosed
(QString pdfPath)
{
webView
()->
page
()->
runJavaScript
(
QString::fromUtf8
(
"
window.synctexNotifyPdfViewerClosed(
\"
"
) +
pdfPath +
QString::fromUtf8
(
"
\"
)
"
));
}
void
MainWindow::onPdfViewerSyncSource
(QString srcFile,
int
line,
int
column)
{
boost::format
fmt
(
"
window.desktopSynctexInverseSearch(
\"
%1%
\"
, %2%, %3%)
"
);
std::string js =
boost::str
(fmt % srcFile.
toStdString
() % line % column);
webView
()->
page
()->
runJavaScript
(
QString::fromStdString
(js));
}
void
MainWindow::onLicenseLost
(QString licenseMessage)
{
webView
()->
page
()->
runJavaScript
(
QString::fromUtf8
(
"
window.desktopHooks.licenseLost('
"
) + licenseMessage +
QString::fromUtf8
(
"
');
"
));
}
void
MainWindow::onUpdateLicenseWarningBar
(QString message)
{
webView
()->
page
()->
runJavaScript
(
QString::fromUtf8
(
"
window.desktopHooks.updateLicenseWarningBar('
"
) + message +
QString::fromUtf8
(
"
');
"
));
}
//
private interface for SessionLauncher
void
MainWindow::setSessionLauncher
(SessionLauncher* pSessionLauncher)
{
pSessionLauncher_ = pSessionLauncher;
}
RemoteDesktopSessionLauncher*
MainWindow::getRemoteDesktopSessionLauncher
()
{
return
pRemoteSessionLauncher_;
}
JobLauncher*
MainWindow::getJobLauncher
()
{
return
pLauncher_;
}
void
MainWindow::setRemoteDesktopSessionLauncher
(RemoteDesktopSessionLauncher* pSessionLauncher)
{
pRemoteSessionLauncher_ = pSessionLauncher;
}
void
MainWindow::setSessionProcess
(QProcess* pSessionProcess)
{
pCurrentSessionProcess_ = pSessionProcess;
//
when R creates dialogs (e.g. through utils::askYesNo), their first
//
invocation might show behind the RStudio window. this allows us
//
to detect when those Windows are opened and focused, and raise them
//
to the front.
#
ifdef
_WIN32
if
(eventHook_)
::UnhookWinEvent
(eventHook_);
if
(pSessionProcess)
{
eventHook_ = ::
SetWinEventHook
(
EVENT_SYSTEM_DIALOGSTART
,
EVENT_SYSTEM_DIALOGSTART
,
nullptr
,
onDialogStart,
pSessionProcess->
processId
(),
0
,
WINEVENT_OUTOFCONTEXT
);
}
#
endif
}
void
MainWindow::setAppLauncher
(ApplicationLaunch *pAppLauncher)
{
pAppLauncher_ = pAppLauncher;
}
//
allow SessionLauncher to collect restart requests from GwtCallback
int
MainWindow::collectPendingQuitRequest
()
{
return
gwtCallback_.
collectPendingQuitRequest
();
}
bool
MainWindow::desktopHooksAvailable
()
{
return
desktopInfo
().
desktopHooksAvailable
();
}
void
MainWindow::onActivated
()
{
}
void
MainWindow::onUrlChanged
(QUrl url)
{
urlChanged
(url);
}
void
MainWindow::onLoadFinished
(
bool
ok)
{
if
(ok || pRemoteSessionLauncher_)
return
;
RS_CALL_ONCE
();
std::map<std::string,std::string> vars;
vars[
"
url
"
] =
webView
()->
url
().
url
().
toStdString
();
std::ostringstream oss;
Error error =
text::renderTemplate
(
options
().
resourcesPath
().
completePath
(
"
html/connect.html
"
),
vars, oss);
if
(error)
LOG_ERROR
(error);
else
loadHtml
(
QString::fromStdString
(oss.
str
()));
}
WebView*
MainWindow::getWebView
()
{
return
webView
();
}
}
//
namespace desktop
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL