/
DesktopMain.cpp
356 lines (295 loc) · 10.4 KB
/
DesktopMain.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
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
/*
* DesktopMain.cpp
*
* Copyright (C) 2009-11 by RStudio, Inc.
*
* 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.
*
*/
// TODO: test on other platforms
// TODO: open -a RStudio on the Mac
// TODO: file-system project double-click doesn't activate new one
// (this is because it is sending the message to the other?).
// I think we have a change for this -- must activate explicitly!
// TODO: no longer possible for projects to come in through file
// associations. nix that codepath
// TODO: click on project self should open options
// TODO: consider whether .Rdata should also not re-activate existing
#include <QtGui>
#include <QtWebKit>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <core/Log.hpp>
#include <core/system/FileScanner.hpp>
#include <core/Error.hpp>
#include <core/FileInfo.hpp>
#include <core/FilePath.hpp>
#include <core/SafeConvert.hpp>
#include <core/StringUtils.hpp>
#include <core/system/System.hpp>
#include <core/system/Environment.hpp>
#include <core/r_util/RProjectFile.hpp>
#include "DesktopApplicationLaunch.hpp"
#include "DesktopSlotBinders.hpp"
#include "DesktopDetectRHome.hpp"
#include "DesktopOptions.hpp"
#include "DesktopUtils.hpp"
#include "DesktopSessionLauncher.hpp"
QProcess* pRSessionProcess;
QString sharedSecret;
using namespace core;
using namespace desktop;
namespace {
void initializeSharedSecret()
{
sharedSecret = QString::number(rand())
+ QString::number(rand())
+ QString::number(rand());
std::string value = sharedSecret.toUtf8().constData();
core::system::setenv("RS_SHARED_SECRET", value);
}
void initializeWorkingDirectory(int argc,
char* argv[],
const QString& filename)
{
// calculate what our initial working directory should be
std::string workingDir;
// if there is a filename passed to us then use it's path
if (filename != QString())
{
FilePath filePath(filename.toUtf8().constData());
if (filePath.exists())
{
if (filePath.isDirectory())
workingDir = filePath.absolutePath();
else
workingDir = filePath.parent().absolutePath();
}
}
// do additinal detection if necessary
if (workingDir.empty())
{
// get current path
FilePath currentPath = FilePath::safeCurrentPath(
core::system::userHomePath());
#if defined(_WIN32) || defined(__APPLE__)
// detect whether we were launched from the system application menu
// (e.g. Dock, Program File icon, etc.). we do this by checking
// whether the executable path is within the current path. if we
// weren't launched from the system app menu that set the initial
// wd to the current path
FilePath exePath;
Error error = core::system::executablePath(argc, argv, &exePath);
if (!error)
{
if (!exePath.isWithin(currentPath))
workingDir = currentPath.absolutePath();
}
else
{
LOG_ERROR(error);
}
#else
// on linux we take the current working dir if we were launched
// from within a terminal
if (core::system::stdoutIsTerminal())
{
workingDir = currentPath.absolutePath();
}
#endif
}
// set the working dir if we have one
if (!workingDir.empty())
core::system::setenv("RS_INITIAL_WD", workingDir);
}
void setInitialProject(const FilePath& projectFile, QString* pFilename)
{
core::system::setenv("RS_INITIAL_PROJECT", projectFile.absolutePath());
pFilename->clear();
}
void initializeStartupEnvironment(QString* pFilename)
{
// if the filename ends with .RData or .rda then this is an
// environment file. if it ends with .Rproj then it is
// a project file. we handle both cases by setting an environment
// var and then resetting the pFilename so it isn't processed
// using the standard open file logic
FilePath filePath(pFilename->toUtf8().constData());
if (filePath.exists())
{
std::string ext = filePath.extensionLowerCase();
// if it is a directory or just an .rdata file then we can see
// whether there is a project file we can automatically attach to
if (filePath.isDirectory())
{
FilePath projectFile = r_util::projectFromDirectory(filePath);
if (!projectFile.empty())
{
setInitialProject(projectFile, pFilename);
}
}
else if (ext == ".rproj")
{
setInitialProject(filePath, pFilename);
}
else if (ext == ".rdata" || ext == ".rda")
{
core::system::setenv("RS_INITIAL_ENV", filePath.absolutePath());
pFilename->clear();
}
}
}
QString verifyAndNormalizeFilename(QString filename)
{
if (filename.isNull() || filename.isEmpty())
return QString();
QFileInfo fileInfo(filename);
if (fileInfo.exists())
return fileInfo.absoluteFilePath();
else
return QString();
}
bool isNonProjectFilename(QString filename)
{
if (filename.isNull() || filename.isEmpty())
return false;
FilePath filePath(filename.toUtf8().constData());
return filePath.exists() && filePath.extensionLowerCase() != ".rproj";
}
bool dummy(const FileInfo& file)
{
return true;
}
} // anonymous namespace
int main(int argc, char* argv[])
{
core::system::initHook();
try
{
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
// initialize log
FilePath userHomePath = core::system::userHomePath("R_USER|HOME");
FilePath logPath = core::system::userSettingsPath(
userHomePath,
"RStudio-Desktop").childPath("log");
core::system::initializeLog("rdesktop",
core::system::kLogLevelWarning,
logPath);
boost::scoped_ptr<QApplication> pApp;
boost::scoped_ptr<ApplicationLaunch> pAppLaunch;
ApplicationLaunch::init(QString::fromAscii("RStudio"),
argc,
argv,
&pApp,
&pAppLaunch);
// determine the filename that was passed to us
QString filename;
#ifdef __APPLE__
// get filename from OpenFile apple-event (pump to ensure delivery)
pApp->processEvents();
filename = verifyAndNormalizeFilename(
pAppLaunch->startupOpenFileRequest());
#endif
// allow all platforms (including OSX) to check the command line.
// we include OSX because the way Qt handles apple events is to
// re-route them to the first instance to register for events. in
// this case (for projects) we use this to initiate a launch
// of the application with the project filename on the command line
if (filename.isEmpty())
{
// get filename from command line arguments
if (pApp->arguments().size() > 1)
filename = verifyAndNormalizeFilename(pApp->arguments().last());
}
// if we have a filename and it is NOT a project file then see
// if we can open it within an existing instance
if (isNonProjectFilename(filename))
{
if (pAppLaunch->sendMessage(filename))
return 0;
}
pApp->setAttribute(Qt::AA_MacDontSwapCtrlAndMeta);
initializeSharedSecret();
initializeWorkingDirectory(argc, argv, filename);
initializeStartupEnvironment(&filename);
Options& options = desktop::options();
if (!prepareEnvironment(options))
return 1;
// get install path
FilePath installPath;
Error error = core::system::installPath("..", argc, argv, &installPath);
if (error)
{
LOG_ERROR(error);
return EXIT_FAILURE;
}
#ifdef _WIN32
RVersion version = detectRVersion(false);
#endif
// calculate paths to config file and rsession
FilePath confPath, sessionPath;
// check for debug configuration
#ifndef NDEBUG
FilePath currentPath = FilePath::safeCurrentPath(installPath);
if (currentPath.complete("conf/rdesktop-dev.conf").exists())
{
confPath = currentPath.complete("conf/rdesktop-dev.conf");
sessionPath = currentPath.complete("session/rsession");
#ifdef _WIN32
if (version.architecture() == ArchX64)
sessionPath = installPath.complete("x64/rsession");
#endif
}
#endif
// if there is no conf path then release mode
if (confPath.empty())
{
// default session path (then tweak)
sessionPath = installPath.complete("bin/rsession");
// check for win64 binary on windows
#ifdef _WIN32
if (version.architecture() == ArchX64)
sessionPath = installPath.complete("bin/x64/rsession");
#endif
// check for running in a bundle on OSX
#ifdef __APPLE__
if (installPath.complete("Info.plist").exists())
sessionPath = installPath.complete("MacOS/rsession");
#endif
}
core::system::fixupExecutablePath(&sessionPath);
// launch session
SessionLauncher sessionLauncher(sessionPath, confPath);
error = sessionLauncher.launchFirstSession(filename, pAppLaunch.get());
if (!error)
{
int result = pApp->exec();
sessionLauncher.cleanupAtExit();
options.cleanUpScratchTempDir();
return result;
}
else
{
LOG_ERROR(error);
// These calls to processEvents() seem to be necessary to get
// readAllStandardError to work.
pApp->processEvents();
pApp->processEvents();
pApp->processEvents();
QMessageBox errorMsg(safeMessageBoxIcon(QMessageBox::Critical),
QString::fromUtf8("RStudio"),
sessionLauncher.launchFailedErrorMessage());
errorMsg.addButton(new QPushButton(QString::fromUtf8("OK")),
QMessageBox::AcceptRole);
errorMsg.show();
pApp->exec();
return EXIT_FAILURE;
}
}
CATCH_UNEXPECTED_EXCEPTION
}