MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / ShowOpenFileDialog

Method ShowOpenFileDialog

Source/Engine/Platform/Linux/LinuxFileSystem.cpp:23–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21const DateTime UnixEpoch(1970, 1, 1);
22
23bool LinuxFileSystem::ShowOpenFileDialog(Window* parentWindow, const StringView& initialDirectory, const StringView& filter, bool multiSelect, const StringView& title, Array<String, HeapAllocation>& filenames)
24{
25 const StringAsUTF8<> initialDirectoryAnsi(*initialDirectory, initialDirectory.Length());
26 const StringAsUTF8<> titleAnsi(*title, title.Length());
27 const char* initDir = initialDirectory.HasChars() ? initialDirectoryAnsi.Get() : ".";
28 String xdgCurrentDesktop;
29 StringBuilder fileFilter;
30 Array<String> fileFilterEntries;
31 Platform::GetEnvironmentVariable(TEXT("XDG_CURRENT_DESKTOP"), xdgCurrentDesktop);
32 StringUtils::GetZZString(filter.Get()).Split('\0', fileFilterEntries);
33
34 const bool zenitySupported = FileSystem::FileExists(TEXT("/usr/bin/zenity"));
35 const bool kdialogSupported = FileSystem::FileExists(TEXT("/usr/bin/kdialog"));
36 char cmd[2048];
37 if (zenitySupported && (xdgCurrentDesktop != TEXT("KDE") || !kdialogSupported)) // Prefer kdialog when running on KDE
38 {
39 for (int32 i = 1; i < fileFilterEntries.Count(); i += 2)
40 {
41 String extensions(fileFilterEntries[i]);
42 fileFilterEntries[i].Replace(TEXT(";"), TEXT(" "));
43 fileFilter.Append(String::Format(TEXT("{0}--file-filter=\"{1}|{2}\""), i > 1 ? TEXT(" ") : TEXT(""), fileFilterEntries[i-1].Get(), extensions.Get()));
44 }
45
46 sprintf(cmd, "/usr/bin/zenity --modal --file-selection %s--filename=\"%s\" --title=\"%s\" %s ", multiSelect ? "--multiple --separator=$'\n' " : " ", initDir, titleAnsi.Get(), fileFilter.ToStringView().ToStringAnsi().GetText());
47 }
48 else if (kdialogSupported)
49 {
50 for (int32 i = 1; i < fileFilterEntries.Count(); i += 2)
51 {
52 String extensions(fileFilterEntries[i]);
53 fileFilterEntries[i].Replace(TEXT(";"), TEXT(" "));
54 fileFilter.Append(String::Format(TEXT("{0}\"{1}({2})\""), i > 1 ? TEXT(" ") : TEXT(""), fileFilterEntries[i-1].Get(), extensions.Get()));
55 }
56 fileFilter.Append(String::Format(TEXT("{0}\"{1}({2})\""), TEXT(" "), TEXT("many things"), TEXT("*.png *.jpg")));
57
58 sprintf(cmd, "/usr/bin/kdialog --getopenfilename %s--title \"%s\" \"%s\" %s ", multiSelect ? "--multiple --separate-output " : " ", titleAnsi.Get(), initDir, fileFilter.ToStringView().ToStringAnsi().GetText());
59 }
60 else
61 {
62 LOG(Error, "Missing file picker (install zenity or kdialog).");
63 return true;
64 }
65 FILE* f = popen(cmd, "r");
66 char buf[2048];
67 char* writePointer = buf;
68 int remainingCapacity = ARRAY_COUNT(buf);
69 // make sure we read all output from kdialog
70 while (remainingCapacity > 0 && fgets(writePointer, remainingCapacity, f))
71 {
72 int r = strlen(writePointer);
73 writePointer += r;
74 remainingCapacity -= r;
75 }
76 if (remainingCapacity <= 0)
77 {
78 LOG(Error, "You selected more files than an internal buffer can hold. Try selecting fewer files at a time.");
79 // in case of an overflow we miss the closing null byte, add it after the rightmost linefeed
80 while (*writePointer != '\n')

Callers

nothing calls this directly

Calls 13

ReplaceMethod · 0.80
ToStringViewMethod · 0.80
FormatFunction · 0.50
StringClass · 0.50
LengthMethod · 0.45
HasCharsMethod · 0.45
GetMethod · 0.45
SplitMethod · 0.45
CountMethod · 0.45
AppendMethod · 0.45
GetTextMethod · 0.45
ToStringAnsiMethod · 0.45

Tested by

no test coverage detected