From e1d0699bdbf1bee5cb29916ba78059872bace0d0 Mon Sep 17 00:00:00 2001 From: "Rebecca N. Palmer" Date: Wed, 10 Feb 2016 22:58:56 +0000 Subject: [PATCH] add new QtFileDialog to avoid using Nasal in file selectors (and hence avoid applying Nasal security rules to them) --- src/GUI/CMakeLists.txt | 2 + src/GUI/FileDialog.cxx | 5 +++ src/GUI/QtFileDialog.cxx | 79 ++++++++++++++++++++++++++++++++++++++++ src/GUI/QtFileDialog.hxx | 16 ++++++++ 4 files changed, 102 insertions(+) create mode 100644 src/GUI/QtFileDialog.cxx create mode 100644 src/GUI/QtFileDialog.hxx diff --git a/src/GUI/CMakeLists.txt b/src/GUI/CMakeLists.txt index ae48f32e0..18d42495d 100644 --- a/src/GUI/CMakeLists.txt +++ b/src/GUI/CMakeLists.txt @@ -106,6 +106,8 @@ if (HAVE_QT) LocationWidget.hxx QtMessageBox.cxx QtMessageBox.hxx + QtFileDialog.cxx + QtFileDialog.hxx ${uic_sources} ${qrc_sources}) diff --git a/src/GUI/FileDialog.cxx b/src/GUI/FileDialog.cxx index ec7769ad4..20478981c 100644 --- a/src/GUI/FileDialog.cxx +++ b/src/GUI/FileDialog.cxx @@ -41,6 +41,9 @@ #if defined(SG_WINDOWS) #include "WindowsFileDialog.hxx" #endif +#if defined(HAVE_QT) + #include "QtFileDialog.hxx" +#endif FGFileDialog::FGFileDialog(Usage use) : _usage(use), @@ -148,6 +151,8 @@ static naRef f_createFileDialog(const nasal::CallContext& ctx) FileDialogPtr fd(new CocoaFileDialog(usage)); #elif defined(SG_WINDOWS) FileDialogPtr fd(new WindowsFileDialog(usage)); +#elif defined(HAVE_QT) + FileDialogPtr fd(new QtFileDialog(usage)); #else FileDialogPtr fd(new PUIFileDialog(usage)); #endif diff --git a/src/GUI/QtFileDialog.cxx b/src/GUI/QtFileDialog.cxx new file mode 100644 index 000000000..5d140db91 --- /dev/null +++ b/src/GUI/QtFileDialog.cxx @@ -0,0 +1,79 @@ +// QtFileDialog.cxx - Qt5 implementation of FGFileDialog +// +// Written by Rebecca Palmer, started February 2016. +// +// Copyright (C) 2015 Rebecca Palmer +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program 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 +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +#include "QtFileDialog.hxx" +#include "QtLauncher.hxx" +#include + +// Qt +#include +#include +#include + +QtFileDialog::QtFileDialog(FGFileDialog::Usage use) : + FGFileDialog(use) +{ + +} + +QtFileDialog::~QtFileDialog() {} + +void QtFileDialog::exec() +{ + int fakeargc = 1; + static char fakeargv0[] = "fgfs"; + static char * fakeargv[2] = {fakeargv0, 0}; + // This does nothing if it has already been run, so the fake argc/argv + // are only used if run without launcher + flightgear::initApp(fakeargc, fakeargv); + + // concatenate filter patterns, as Qt uses a single string + std::string filter=""; + for( string_list::const_iterator it = _filterPatterns.begin(); it != _filterPatterns.end();++it ) { + if(!filter.empty()){ + filter=filter+" "; + } + filter=filter+*it; + } + QFileDialog dlg(0,QString::fromStdString(_title),QString::fromStdString(_initialPath.str()),QString::fromStdString(filter)); + if (_usage==USE_SAVE_FILE) { + dlg.setAcceptMode(QFileDialog::AcceptSave); + } + if (_usage==USE_CHOOSE_DIR) { + dlg.setFileMode(QFileDialog::Directory); + } + if (_usage==USE_OPEN_FILE) { + dlg.setFileMode(QFileDialog::ExistingFile); + } + dlg.setLabelText(QFileDialog::Accept,QString::fromStdString(_buttonText)); + dlg.selectFile(QString::fromStdString(_placeholder)); + if(_showHidden){ + SG_LOG(SG_GENERAL, SG_ALERT, "QtFileDialog: can't show hidden files in Qt"); + } + if(dlg.exec()){ + QStringList result = dlg.selectedFiles(); + if(!(result.isEmpty())){ + _callback->onFileDialogDone(this, SGPath(result[0].toStdString())); + } + } +} + +void QtFileDialog::close(){} + diff --git a/src/GUI/QtFileDialog.hxx b/src/GUI/QtFileDialog.hxx new file mode 100644 index 000000000..2f4e0b27f --- /dev/null +++ b/src/GUI/QtFileDialog.hxx @@ -0,0 +1,16 @@ +#ifndef FG_QT_FILE_DIALOG_HXX +#define FG_QT_FILE_DIALOG_HXX 1 + +#include + +class QtFileDialog : public FGFileDialog +{ +public: + QtFileDialog(FGFileDialog::Usage use); + + virtual ~QtFileDialog(); + + virtual void exec(); + virtual void close(); +}; +#endif -- 2.39.5