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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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