Make Linux file dialog API better

This commit is contained in:
Ilya Fedin 2021-06-23 03:20:40 +04:00 committed by John Preston
parent 8b839f46b2
commit f011c84ce8
8 changed files with 38 additions and 54 deletions

View File

@ -171,7 +171,7 @@ bool Get(
parent = parent->window();
}
#ifndef DESKTOP_APP_DISABLE_DBUS_INTEGRATION
if (XDP::Use(type)) {
{
const auto result = XDP::Get(
parent,
files,
@ -187,15 +187,17 @@ bool Get(
}
#endif // !DESKTOP_APP_DISABLE_DBUS_INTEGRATION
if (const auto integration = GtkIntegration::Instance()) {
if (integration->useFileDialog(type)) {
return integration->getFileDialog(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
const auto result = integration->getFileDialog(
parent,
files,
remoteContent,
caption,
filter,
type,
startFile);
if (result.has_value()) {
return *result;
}
}
// avoid situation when portals don't work

View File

@ -22,10 +22,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
namespace Platform {
namespace FileDialog {
namespace Gtk {
namespace {
using namespace Platform::Gtk;
namespace {
using Type = ::FileDialog::internal::Type;
// GTK file chooser image preview: thanks to Chromium
@ -640,16 +640,7 @@ void GtkFileDialog::setNameFilters(const QStringList &filters) {
} // namespace
bool Use(Type type) {
if (!Supported()) {
return false;
}
return qEnvironmentVariableIsSet("TDESKTOP_USE_GTK_FILE_DIALOG")
|| DesktopEnvironment::IsGtkBased();
}
bool Get(
std::optional<bool> Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
@ -657,6 +648,12 @@ bool Get(
const QString &filter,
Type type,
QString startFile) {
if (!Supported()
|| (!qEnvironmentVariableIsSet("TDESKTOP_USE_GTK_FILE_DIALOG")
&& !DesktopEnvironment::IsGtkBased())) {
return std::nullopt;
}
if (cDialogLastPath().isEmpty()) {
InitLastPath();
}

View File

@ -13,16 +13,13 @@ namespace Platform {
namespace FileDialog {
namespace Gtk {
using Type = ::FileDialog::internal::Type;
bool Use(Type type = Type::ReadFile);
bool Get(
std::optional<bool> Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
Type type,
::FileDialog::internal::Type type,
QString startFile);
} // namespace Gtk

View File

@ -158,17 +158,13 @@ std::optional<int> GtkIntegration::scaleFactor() const {
return gdk_monitor_get_scale_factor(monitor);
}
bool GtkIntegration::useFileDialog(FileDialogType type) const {
return FileDialog::Gtk::Use(type);
}
bool GtkIntegration::getFileDialog(
std::optional<bool> GtkIntegration::getFileDialog(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
FileDialogType type,
::FileDialog::internal::Type type,
QString startFile) const {
return FileDialog::Gtk::Get(
parent,

View File

@ -20,16 +20,13 @@ public:
[[nodiscard]] std::optional<int> scaleFactor() const;
using FileDialogType = ::FileDialog::internal::Type;
[[nodiscard]] bool useFileDialog(
FileDialogType type = FileDialogType::ReadFile) const;
[[nodiscard]] bool getFileDialog(
[[nodiscard]] std::optional<bool> getFileDialog(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
FileDialogType type,
::FileDialog::internal::Type type,
QString startFile) const;
[[nodiscard]] bool showOpenWithDialog(const QString &filepath) const;

View File

@ -24,19 +24,15 @@ std::optional<int> GtkIntegration::scaleFactor() const {
return std::nullopt;
}
bool GtkIntegration::useFileDialog(FileDialogType type) const {
return false;
}
bool GtkIntegration::getFileDialog(
std::optional<bool> GtkIntegration::getFileDialog(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
FileDialogType type,
::FileDialog::internal::Type type,
QString startFile) const {
return false;
return std::nullopt;
}
bool GtkIntegration::showOpenWithDialog(const QString &filepath) const {

View File

@ -28,6 +28,8 @@ namespace FileDialog {
namespace XDP {
namespace {
using Type = ::FileDialog::internal::Type;
constexpr auto kXDGDesktopPortalService = "org.freedesktop.portal.Desktop"_cs;
constexpr auto kXDGDesktopPortalObjectPath = "/org/freedesktop/portal/desktop"_cs;
constexpr auto kXDGDesktopPortalFileChooserInterface = "org.freedesktop.portal.FileChooser"_cs;
@ -703,11 +705,6 @@ void Start() {
ComputeFileChooserPortalVersion();
}
bool Use(Type type) {
return FileChooserPortalVersion.has_value()
&& (type != Type::ReadFolder || *FileChooserPortalVersion >= 3);
}
std::optional<bool> Get(
QPointer<QWidget> parent,
QStringList &files,
@ -716,6 +713,11 @@ std::optional<bool> Get(
const QString &filter,
Type type,
QString startFile) {
if (!FileChooserPortalVersion.has_value()
|| (type == Type::ReadFolder && *FileChooserPortalVersion < 3)) {
return std::nullopt;
}
static const auto docRegExp = QRegularExpression("^/run/user/\\d+/doc");
if (cDialogLastPath().isEmpty()
|| cDialogLastPath().contains(docRegExp)) {

View File

@ -13,17 +13,14 @@ namespace Platform {
namespace FileDialog {
namespace XDP {
using Type = ::FileDialog::internal::Type;
void Start();
bool Use(Type type = Type::ReadFile);
std::optional<bool> Get(
QPointer<QWidget> parent,
QStringList &files,
QByteArray &remoteContent,
const QString &caption,
const QString &filter,
Type type,
::FileDialog::internal::Type type,
QString startFile);
} // namespace XDP