FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/desktop/DesktopUtils.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
/
DesktopUtils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
466 lines (383 loc) · 11.5 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
desktop
/
DesktopUtils.cpp
Copy path
File metadata and controls
466 lines (383 loc) · 11.5 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
/*
* DesktopUtils.cpp
*
* Copyright (C) 2009-18 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
"
DesktopUtils.hpp
"
#
include
<
set
>
#
include
<
QPushButton
>
#
include
<
QTimer
>
#
include
<
QDesktopServices
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/system/Environment.hpp
>
#
include
<
core/system/Xdg.hpp
>
#
include
"
DesktopOptions.hpp
"
#
include
"
DesktopMainWindow.hpp
"
#
ifdef
Q_OS_WIN
#
include
<
windows.h
>
#
endif
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
desktop
{
#
ifdef
Q_OS_WIN
void
reattachConsoleIfNecessary
()
{
if
(::
AttachConsole
(
ATTACH_PARENT_PROCESS
))
{
freopen
(
"
CONOUT$
"
,
"
wb
"
,stdout);
freopen
(
"
CONOUT$
"
,
"
wb
"
,stderr);
freopen
(
"
CONIN$
"
,
"
rb
"
,stdin);
std::ios::sync_with_stdio
();
}
}
#
else
void
reattachConsoleIfNecessary
()
{
}
#
endif
//
NOTE: this code is duplicated in diagnostics as well (and also in
//
SessionOptions.hpp although the code path isn't exactly the same)
FilePath
userLogPath
()
{
FilePath userHomePath =
core::system::userHomePath
(
"
R_USER|HOME
"
);
FilePath logPath =
core::system::userSettingsPath
(
userHomePath,
"
RStudio-Desktop
"
).
completeChildPath
(
"
log
"
);
return
logPath;
}
FilePath
userWebCachePath
()
{
return
core::system::xdg::userDataDir
().
completeChildPath
(
"
web-cache
"
);
}
bool
isWindows
()
{
#
ifdef
Q_OS_WIN
return
true
;
#
else
return
false
;
#
endif
}
#
ifndef
Q_OS_MAC
double
devicePixelRatio
(QMainWindow* pMainWindow)
{
return
1.0
;
}
bool
isMacOS
()
{
return
false
;
}
//
NOTE: also RHEL
bool
isCentOS
()
{
FilePath
redhatRelease
(
"
/etc/redhat-release
"
);
if
(!redhatRelease.
exists
())
return
false
;
std::string contents;
Error error =
readStringFromFile
(redhatRelease, &contents);
if
(error)
return
false
;
return
contents.
find
(
"
CentOS
"
) != std::string::npos ||
contents.
find
(
"
Red Hat Enterprise Linux
"
) != std::string::npos;
}
QString
browseDirectory
(
const
QString& caption,
const
QString& label,
const
QString& dir,
QWidget* pOwner)
{
QFileDialog
dialog
(
pOwner,
caption,
resolveAliasedPath
(dir));
dialog.
setLabelText
(QFileDialog::Accept, label);
dialog.
setFileMode
(QFileDialog::Directory);
dialog.
setOption
(QFileDialog::ShowDirsOnly,
true
);
dialog.
setWindowModality
(Qt::WindowModal);
QString result;
if
(dialog.
exec
() == QDialog::Accepted)
result = dialog.
selectedFiles
().
value
(
0
);
if
(pOwner)
raiseAndActivateWindow
(pOwner);
return
createAliasedPath
(result);
}
#
endif
bool
isGnomeDesktop
()
{
if
(
core::system::getenv
(
"
DESKTOP_SESSION
"
) ==
"
gnome
"
)
return
true
;
std::string desktop =
core::system::getenv
(
"
XDG_CURRENT_DESKTOP
"
);
if
(desktop.
find
(
"
GNOME
"
) != std::string::npos)
return
true
;
return
false
;
}
#
ifndef
Q_OS_MAC
QString
getFixedWidthFontList
()
{
return
desktopInfo
().
getFixedWidthFontList
();
}
#
endif
void
applyDesktopTheme
(QWidget* window,
bool
isDark)
{
#
ifndef
Q_OS_MAC
std::string lightSheetName =
isWindows
()
?
"
rstudio-windows-light.qss
"
:
"
rstudio-gnome-light.qss
"
;
std::string darkSheetName =
isWindows
()
?
"
rstudio-windows-dark.qss
"
:
"
rstudio-gnome-dark.qss
"
;
FilePath stylePath = isDark
?
options
().
resourcesPath
().
completePath
(
"
stylesheets
"
).
completePath
(darkSheetName)
:
options
().
resourcesPath
().
completePath
(
"
stylesheets
"
).
completePath
(lightSheetName);
std::string stylesheet;
Error error =
core::readStringFromFile
(stylePath, &stylesheet);
if
(error)
LOG_ERROR
(error);
window->
setStyleSheet
(
QString::fromStdString
(stylesheet));
#
endif
}
#
ifndef
Q_OS_MAC
void
enableFullscreenMode
(QMainWindow* pMainWindow,
bool
primary)
{
}
void
toggleFullscreenMode
(QMainWindow* pMainWindow)
{
}
bool
supportsFullscreenMode
(QMainWindow* pMainWindow)
{
return
false
;
}
void
initializeLang
()
{
}
void
finalPlatformInitialize
(MainWindow* pMainWindow)
{
}
#
endif
void
raiseAndActivateWindow
(QWidget* pWindow)
{
//
WId wid = pWindow->effectiveWinId(); -- gets X11 window id
//
gtk_window_present_with_time(GTK_WINDOW, timestamp)
if
(pWindow->
isMinimized
())
{
pWindow->
setWindowState
(
pWindow->
windowState
() & ~Qt::WindowMinimized);
}
pWindow->
raise
();
pWindow->
activateWindow
();
}
void
moveWindowBeneath
(QWidget* pTop, QWidget* pBottom)
{
#
ifdef
WIN32
HWND
hwndTop =
reinterpret_cast
<
HWND
>(pTop->
winId
());
HWND
hwndBottom =
reinterpret_cast
<
HWND
>(pBottom->
winId
());
::SetWindowPos
(hwndBottom, hwndTop,
0
,
0
,
0
,
0
,
SWP_NOMOVE
|
SWP_NOSIZE
|
SWP_NOACTIVATE
);
#
endif
//
not currently supported on Linux--Qt doesn't provide a way to view or
//
change the window stacking order
}
void
closeWindow
(QWidget* pWindow)
{
pWindow->
close
();
}
QMessageBox::Icon
safeMessageBoxIcon
(QMessageBox::Icon icon)
{
//
if a gtk theme has a missing or corrupt icon for one of the stock
//
dialog images, qt crashes when attempting to show the dialog
#
ifdef
Q_OS_LINUX
return
QMessageBox::NoIcon;
#
else
return
icon;
#
endif
}
bool
showYesNoDialog
(QMessageBox::Icon icon,
QWidget *parent,
const
QString &title,
const
QString& text,
const
QString& informativeText,
bool
yesDefault)
{
//
basic message box attributes
QMessageBox
messageBox
(parent);
messageBox.
setIcon
(
safeMessageBoxIcon
(icon));
messageBox.
setWindowTitle
(title);
messageBox.
setText
(text);
if
(informativeText.
length
() >
0
)
messageBox.
setInformativeText
(informativeText);
messageBox.
setWindowModality
(Qt::WindowModal);
messageBox.
setWindowFlag
(Qt::WindowContextHelpButtonHint,
false
);
//
initialize buttons
QPushButton* pYes = messageBox.
addButton
(QMessageBox::Yes);
QPushButton* pNo = messageBox.
addButton
(QMessageBox::No);
if
(yesDefault)
messageBox.
setDefaultButton
(pYes);
else
messageBox.
setDefaultButton
(pNo);
//
show the dialog modally
messageBox.
exec
();
//
return true if the user clicked yes
return
messageBox.
clickedButton
() == pYes;
}
void
showMessageBox
(QMessageBox::Icon icon,
QWidget *parent,
const
QString &title,
const
QString& text,
const
QString& informativeText)
{
QMessageBox
messageBox
(parent);
messageBox.
setIcon
(
safeMessageBoxIcon
(icon));
messageBox.
setWindowTitle
(title);
messageBox.
setText
(text);
if
(informativeText.
length
() >
0
)
messageBox.
setInformativeText
(informativeText);
messageBox.
setWindowModality
(Qt::WindowModal);
messageBox.
setWindowFlag
(Qt::WindowContextHelpButtonHint,
false
);
messageBox.
addButton
(
new
QPushButton
(
QString::fromUtf8
(
"
OK
"
)), QMessageBox::AcceptRole);
messageBox.
exec
();
}
void
showError
(QWidget *parent,
const
QString &title,
const
QString& text,
const
QString& informativeText)
{
showMessageBox
(QMessageBox::Critical, parent, title, text, informativeText);
}
void
showWarning
(QWidget *parent,
const
QString &title,
const
QString& text,
const
QString& informativeText)
{
showMessageBox
(QMessageBox::Warning, parent, title, text, informativeText);
}
void
showInfo
(QWidget* parent,
const
QString& title,
const
QString& text,
const
QString& informativeText)
{
showMessageBox
(QMessageBox::Information, parent, title, text, informativeText);
}
void
showFileError
(
const
QString& action,
const
QString& file,
const
QString& error)
{
QString msg =
QString::fromUtf8
(
"
Error
"
) + action +
QString::fromUtf8
(
"
"
) + file +
QString::fromUtf8
(
"
-
"
) + error;
showMessageBox
(QMessageBox::Critical,
nullptr
,
QString::fromUtf8
(
"
File Error
"
),
msg,
QString
());
}
bool
isFixedWidthFont
(
const
QFont& font)
{
QFontMetrics
metrics
(font);
int
width = metrics.
width
(
QChar::fromLatin1
(
'
'
));
char
chars[] = {
'
m
'
,
'
i
'
,
'
A
'
,
'
/
'
,
'
-
'
,
'
1
'
,
'
l
'
,
'
!
'
,
'
x
'
,
'
X
'
,
'
y
'
,
'
Y
'
};
for
(
char
i : chars)
{
if
(metrics.
width
(
QChar::fromLatin1
(i)) != width)
return
false
;
}
return
true
;
}
int
getDpi
()
{
//
TODO: we may need to tweak this to ensure that the DPI
//
discovered respects the screen a particular instance
//
that RStudio lives on (e.g. for users with multiple
//
displays with different DPIs)
return
(
int
) qApp->
primaryScreen
()->
logicalDotsPerInch
();
}
double
getDpiZoomScaling
()
{
//
TODO: because Qt is already high-DPI aware and automatically
//
scales in most scenarios, we no longer need to detect and
//
apply a custom scale -- but more testing is warranted
return
1.0
;
}
#
ifdef
_WIN32
void
openFile
(
const
QString& file)
{
return
openUrl
(
QUrl::fromLocalFile
(file));
}
//
on Win32 open urls using our special urlopener.exe -- this is
//
so that the shell exec is made out from under our windows "job"
void
openUrl
(
const
QUrl& url)
{
//
we allow default handling for mailto and file schemes because qt
//
does custom handling for them and they aren't affected by the chrome
//
job object issue noted above
if
(url.
scheme
() ==
QString::fromUtf8
(
"
mailto
"
) ||
url.
scheme
() ==
QString::fromUtf8
(
"
file
"
))
{
QDesktopServices::openUrl
(url);
}
else
{
core::system::ProcessOptions options;
options.
breakawayFromJob
=
true
;
options.
detachProcess
=
true
;
std::vector<std::string> args;
args.
push_back
(url.
toString
().
toStdString
());
core::system::ProcessResult result;
Error error =
core::system::runProgram
(
desktop::options
().
urlopenerPath
().
getAbsolutePath
(),
args,
"
"
,
options,
&result);
if
(error)
LOG_ERROR
(error);
else
if
(result.
exitStatus
!=
EXIT_SUCCESS
)
LOG_ERROR_MESSAGE
(result.
stdErr
);
}
}
//
Qt 4.8.3 on Win7 (32-bit) has problems with opening the ~ directory
//
(it attempts to navigate to the "Documents library" and then hangs)
//
So we use the Qt file dialog implementations when we are running
//
on Win32
QFileDialog::Options
standardFileDialogOptions
()
{
return
0
;
}
#
else
void
openFile
(
const
QString& file)
{
QDesktopServices::openUrl
(
QUrl::fromLocalFile
(file));
}
void
openUrl
(
const
QUrl& url)
{
QDesktopServices::openUrl
(url);
}
QFileDialog::Options
standardFileDialogOptions
()
{
return
nullptr
;
}
#
endif
FilePath
userHomePath
()
{
return
core::system::userHomePath
(
"
R_USER|HOME
"
);
}
QString
createAliasedPath
(
const
QString& path)
{
std::string aliased =
FilePath::createAliasedPath
(
FilePath
(path.
toUtf8
().
constData
()),
desktop::userHomePath
());
return
QString::fromUtf8
(aliased.
c_str
());
}
QString
resolveAliasedPath
(
const
QString& path)
{
FilePath
resolved
(
FilePath::resolveAliasedPath
(path.
toUtf8
().
constData
(),
userHomePath
()));
return
QString::fromUtf8
(resolved.
getAbsolutePath
().
c_str
());
}
}
//
namespace desktop
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL