Debian Codegraph @

src/GUI/FileDialog.cxx

raw symbols SCIP
1 // FileDialog.cxx - generic FileDialog interface and Nasal wrapper
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 // SPDX-FileCopyrightText: Copyright (C) 2012 James Turner - [email protected]
4
5
6 #include "config.h"
7
8 #include "FileDialog.hxx"
9
10 #include <simgear/nasal/cppbind/Ghost.hxx>
11
12 #include <Main/globals.hxx>
13 #include <Scripting/NasalSGPath.hxx>
14 #include <Scripting/NasalSys.hxx>
15
16 #if defined(HAVE_PUI)
17 #include "PUIFileDialog.hxx"
18 #endif
19
20 #if defined(SG_MAC)
21 #include "CocoaFileDialog.hxx"
22 #endif
23
24 #if defined(HAVE_QT)
25 #include "QtFileDialog.hxx"
26 #endif
27
28 FGFileDialog::FGFileDialog(Usage use) : _usage(use),
29 _showHidden(false)
30 {
31 }
32
33 FGFileDialog::~FGFileDialog()
34 {
35 // ensure this is concrete so callback gets cleaned up.
36 }
37
38 void FGFileDialog::setTitle(const std::string& aText)
39 {
40 _title = aText;
41 }
42
43 void FGFileDialog::setButton(const std::string& aText)
44 {
45 _buttonText = aText;
46 }
47
48 void FGFileDialog::setDirectory(const SGPath& aPath)
49 {
50 _initialPath = aPath;
51 }
52
53 void FGFileDialog::setStandardLocation(const std::string& s)
54 {
55 try {
56 const auto l = standardLocationFromString(s);
57 _initialPath = SGPath::standardLocation(l);
58 } catch (sg_exception&) {
59 SG_LOG(SG_GUI, SG_DEV_ALERT, "FileDialog was requested to use invalid standard location: " + s);
60 }
61 }
62
63 void FGFileDialog::setFilterPatterns(const string_list& patterns)
64 {
65 _filterPatterns = patterns;
66 }
67
68 void FGFileDialog::setPlaceholderName(const std::string& aName)
69 {
70 _placeholder = aName;
71 }
72
73 void FGFileDialog::setCallback(Callback* aCB)
74 {
75 _callback.reset(aCB);
76 }
77
78 void FGFileDialog::setShowHidden(bool show)
79 {
80 _showHidden = show;
81 }
82
83 bool FGFileDialog::handleSelectedPath(const SGPath& p)
84 {
85 // mark the path as allowed. For CHOOSE_DIR we will get read permissions as well
86 const auto perm = (_usage == USE_SAVE_FILE) ? SGPath::Permissions{false, true} : SGPath::Permissions{true, false};
87
88 bool ok = false;
89 const auto pathString = p.realpath().utf8Str();
90 if (_usage == USE_CHOOSE_DIR) {
91 ok = SGPath::addAllowedDirectoryHierarchy(pathString, perm);
92 } else {
93 ok = SGPath::addAllowedPath(pathString, perm);
94 }
95
96 if (!ok) {
97 SG_LOG(SG_IO, SG_POPUP, "The selected location '" << p.utf8Str() << "' is not allowed. (The location was resolved to a path that contains characters such as '*')");
98 return false;
99 }
100
101 _callback->onFileDialogDone(this, p);
102 return true;
103 }
104
105 class NasalCallback : public FGFileDialog::Callback
106 {
107 public:
108 NasalCallback(naRef f, naRef obj) : func(f),
109 object(obj)
110 {
111 auto sys = globals->get_subsystem<FGNasalSys>();
112 _gcKeys[0] = sys->gcSave(f);
113 _gcKeys[1] = sys->gcSave(obj);
114 }
115
116 void onFileDialogDone(FGFileDialog* instance, const SGPath& aPath) override
117 {
118 auto sys = globals->get_subsystem<FGNasalSys>();
119
120 naContext ctx = naNewContext();
121 naRef args[1];
122 args[0] = nasal::to_nasal(ctx, aPath);
123
124 sys->callMethod(func, object, 1, args, naNil() /* locals */);
125 naFreeContext(ctx);
126 }
127
128 ~NasalCallback()
129 {
130 auto sys = globals->get_subsystem<FGNasalSys>();
131 if (!sys) // happens during Nasal shutdown on reset
132 return;
133
134 sys->gcRelease(_gcKeys[0]);
135 sys->gcRelease(_gcKeys[1]);
136 }
137
138 private:
139 naRef func;
140 naRef object;
141 int _gcKeys[2];
142 };
143
144 void FGFileDialog::setCallbackFromNasal(const nasal::CallContext& ctx)
145 {
146 // wrap up the naFunc in our callback type
147 naRef func = ctx.requireArg<naRef>(0);
148 naRef object = ctx.getArg<naRef>(1, naNil());
149
150 setCallback(new NasalCallback(func, object));
151 }
152
153 typedef std::shared_ptr<FGFileDialog> FileDialogPtr;
154 typedef nasal::Ghost<FileDialogPtr> NasalFileDialog;
155
156 /**
157 * Create new FGFileDialog and get ghost for it.
158 */
159 static naRef f_createFileDialog(const nasal::CallContext& ctx)
160 {
161 FGFileDialog::Usage usage = (FGFileDialog::Usage)ctx.requireArg<int>(0);
162
163 #if defined(SG_MAC)
164 FileDialogPtr fd(new CocoaFileDialog(usage));
165 #elif defined(HAVE_QT)
166 FileDialogPtr fd(new QtFileDialog(usage));
167 #elif defined(HAVE_PUI)
168 FileDialogPtr fd(new PUIFileDialog(usage));
169 #else
170 // we need a fallback implementation
171 FileDialogPtr fd;
172 #endif
173
174 return ctx.to_nasal(fd);
175 }
176
177 void postinitNasalGUI(naRef globals, naContext c)
178 {
179 NasalFileDialog::init("gui._FileDialog")
180 .member("title", &FGFileDialog::getTitle, &FGFileDialog::setTitle)
181 .member("button", &FGFileDialog::getButton, &FGFileDialog::setButton)
182 .member("location", &FGFileDialog::setStandardLocation)
183 .member("directory", &FGFileDialog::getDirectory, &FGFileDialog::setDirectory)
184 .member("show_hidden", &FGFileDialog::showHidden, &FGFileDialog::setShowHidden)
185 .member("placeholder", &FGFileDialog::getPlaceholder, &FGFileDialog::setPlaceholderName)
186 .member("pattern", &FGFileDialog::filterPatterns, &FGFileDialog::setFilterPatterns)
187 .method("open", &FGFileDialog::exec)
188 .method("close", &FGFileDialog::close)
189 .method("setCallback", &FGFileDialog::setCallbackFromNasal);
190
191 nasal::Hash guiModule = nasal::Hash(globals, c).get<nasal::Hash>("gui");
192
193 guiModule.set("FILE_DIALOG_OPEN_FILE", (int)FGFileDialog::USE_OPEN_FILE);
194 guiModule.set("FILE_DIALOG_SAVE_FILE", (int)FGFileDialog::USE_SAVE_FILE);
195 guiModule.set("FILE_DIALOG_CHOOSE_DIR", (int)FGFileDialog::USE_CHOOSE_DIR);
196 guiModule.set("_createFileDialog", f_createFileDialog);
197 }