MCPcopy Create free account
hub / github.com/TeXworks/texworks / openFileFromScript

Method openFileFromScript

src/TWApp.cpp:1516–1558  ·  view source on GitHub ↗

Q_INVOKABLE

Source from the content-addressed store, hash-verified

1514
1515//Q_INVOKABLE
1516QMap<QString, QVariant> TWApp::openFileFromScript(const QString& fileName, QObject * scriptApiObj, const int pos /* = -1 */, const bool askUser /* = false */)
1517{
1518 Tw::Settings settings;
1519 QMap<QString, QVariant> retVal;
1520 QObject * doc = nullptr;
1521 QFileInfo fi(fileName);
1522 Tw::Scripting::ScriptAPI * scriptApi = qobject_cast<Tw::Scripting::ScriptAPI*>(scriptApiObj);
1523
1524 retVal[QString::fromLatin1("status")] = Tw::Scripting::ScriptAPI::SystemAccess_PermissionDenied;
1525
1526 // for absolute paths and full reading permissions, we don't have to care
1527 // about peculiarities of the script; in that case, this even succeeds
1528 // if no valid scriptApi is passed; otherwise, we need to investigate further
1529 if (fi.isRelative() || !settings.value(QString::fromLatin1("allowScriptFileReading"), kDefault_AllowScriptFileReading).toBool()) {
1530 if (!scriptApi)
1531 return retVal;
1532 Tw::Scripting::ScriptObject * scriptObj = qobject_cast<Tw::Scripting::ScriptObject*>(scriptApi->GetScript());
1533 if (!scriptObj)
1534 return retVal; // this should never happen
1535
1536 // relative paths are taken to be relative to the folder containing the
1537 // executing script's file
1538 QDir scriptDir(QFileInfo(scriptObj->getFilename()).dir());
1539 QString path = scriptDir.absoluteFilePath(fileName);
1540
1541 if (!scriptApi->mayReadFile(path, scriptApi->GetTarget())) {
1542 // Possibly ask user to override the permissions
1543 if (!askUser)
1544 return retVal;
1545 if (QMessageBox::warning(qobject_cast<QWidget*>(scriptApi->GetTarget()),
1546 tr("Permission request"),
1547 tr("The script \"%1\" is trying to open the file \"%2\" without sufficient permissions. Do you want to open the file?")\
1548 .arg(scriptObj->getTitle(), path),
1549 QMessageBox::Yes | QMessageBox::No, QMessageBox::No
1550 ) != QMessageBox::Yes)
1551 return retVal;
1552 }
1553 }
1554 doc = openFile(fileName, pos);
1555 retVal[QString::fromLatin1("result")] = QVariant::fromValue(doc);
1556 retVal[QString::fromLatin1("status")] = (doc ? Tw::Scripting::ScriptAPI::SystemAccess_OK : Tw::Scripting::ScriptAPI::SystemAccess_Failed);
1557 return retVal;
1558}
1559
1560void TWApp::doResourcesDialog() const
1561{

Callers

nothing calls this directly

Calls 8

openFileFunction · 0.85
getTitleMethod · 0.80
valueMethod · 0.45
GetScriptMethod · 0.45
getFilenameMethod · 0.45
absoluteFilePathMethod · 0.45
mayReadFileMethod · 0.45
GetTargetMethod · 0.45

Tested by

no test coverage detected