File indexing completed on 2025-04-27 03:58:22

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * date        : 2017-07-04
0007  * Description : wrapper for the QFileDialog
0008  *
0009  * SPDX-FileCopyrightText: 2014-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2017-2022 by Maik Qualmann <metzpinguin at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "dfiledialog.h"
0017 
0018 // Qt includes
0019 
0020 #include <QApplication>
0021 
0022 // KDE includes
0023 
0024 #include <ksharedconfig.h>
0025 #include <kconfiggroup.h>
0026 
0027 // Local includes
0028 
0029 #include "digikam_config.h"
0030 
0031 namespace Digikam
0032 {
0033 
0034 DFileDialog::DFileDialog(QWidget* const parent, Qt::WindowFlags flags)
0035     : QFileDialog(parent, flags)
0036 {
0037     setOption(getNativeFileDialogOption());
0038 }
0039 
0040 DFileDialog::DFileDialog(QWidget* const parent, const QString& caption,
0041                                                 const QString& directory,
0042                                                 const QString& filter)
0043     : QFileDialog(parent, caption, directory, filter)
0044 {
0045     setOption(getNativeFileDialogOption());
0046 }
0047 
0048 DFileDialog::~DFileDialog()
0049 {
0050 }
0051 
0052 bool DFileDialog::hasAcceptedUrls() const
0053 {
0054     return (!selectedUrls().isEmpty()
0055 
0056 #ifndef Q_OS_MACOS
0057 
0058             && (result() == QDialog::Accepted)
0059 
0060 #endif
0061 
0062            );
0063 }
0064 
0065 QString DFileDialog::getExistingDirectory(QWidget* const parent, const QString& caption,
0066                                                                  const QString& dir,
0067                                                                  Options options)
0068 {
0069     options |= getNativeFileDialogOption();
0070 
0071     return QFileDialog::getExistingDirectory(parent, caption, dir, options);
0072 }
0073 
0074 QUrl DFileDialog::getExistingDirectoryUrl(QWidget* const parent, const QString& caption,
0075                                                                  const QUrl& dir,
0076                                                                  Options options,
0077                                                                  const QStringList& supportedSchemes)
0078 {
0079     options |= getNativeFileDialogOption();
0080 
0081     return QFileDialog::getExistingDirectoryUrl(parent, caption, dir, options, supportedSchemes);
0082 }
0083 
0084 QString DFileDialog::getOpenFileName(QWidget* const parent, const QString& caption,
0085                                                             const QString& dir,
0086                                                             const QString& filter,
0087                                                             QString* selectedFilter,
0088                                                             Options options)
0089 {
0090     options |= getNativeFileDialogOption();
0091 
0092     return QFileDialog::getOpenFileName(parent, caption, dir, filter, selectedFilter, options);
0093 }
0094 
0095 QStringList DFileDialog::getOpenFileNames(QWidget* const parent, const QString& caption,
0096                                                                  const QString& dir,
0097                                                                  const QString& filter,
0098                                                                  QString* selectedFilter,
0099                                                                  Options options)
0100 {
0101     options |= getNativeFileDialogOption();
0102 
0103     return QFileDialog::getOpenFileNames(parent, caption, dir, filter, selectedFilter, options);
0104 }
0105 
0106 QUrl DFileDialog::getOpenFileUrl(QWidget* const parent, const QString& caption,
0107                                                         const QUrl& dir,
0108                                                         const QString& filter,
0109                                                         QString* selectedFilter,
0110                                                         Options options,
0111                                                         const QStringList& supportedSchemes)
0112 {
0113     options |= getNativeFileDialogOption();
0114 
0115     return QFileDialog::getOpenFileUrl(parent, caption, dir, filter, selectedFilter, options, supportedSchemes);
0116 }
0117 
0118 QList<QUrl> DFileDialog::getOpenFileUrls(QWidget* const parent, const QString& caption,
0119                                                                 const QUrl& dir,
0120                                                                 const QString& filter,
0121                                                                 QString* selectedFilter,
0122                                                                 Options options,
0123                                                                 const QStringList& supportedSchemes)
0124 {
0125     options |= getNativeFileDialogOption();
0126 
0127     return QFileDialog::getOpenFileUrls(parent, caption, dir, filter, selectedFilter, options, supportedSchemes);
0128 }
0129 
0130 QString DFileDialog::getSaveFileName(QWidget* const parent, const QString& caption,
0131                                                             const QString& dir,
0132                                                             const QString& filter,
0133                                                             QString* selectedFilter,
0134                                                             Options options)
0135 {
0136     options |= getNativeFileDialogOption();
0137 
0138     return QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
0139 }
0140 
0141 QUrl DFileDialog::getSaveFileUrl(QWidget* const parent, const QString& caption,
0142                                                         const QUrl& dir,
0143                                                         const QString& filter,
0144                                                         QString* selectedFilter,
0145                                                         Options options,
0146                                                         const QStringList& supportedSchemes)
0147 {
0148     options |= getNativeFileDialogOption();
0149 
0150     return QFileDialog::getSaveFileUrl(parent, caption, dir, filter, selectedFilter, options, supportedSchemes);
0151 }
0152 
0153 QFileDialog::Option DFileDialog::getNativeFileDialogOption()
0154 {
0155     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0156     KConfigGroup group;
0157 
0158     if (qApp->applicationName() == QLatin1String("digikam"))
0159     {
0160         group = config->group(QLatin1String("General Settings"));
0161     }
0162     else
0163     {
0164         group = config->group(QLatin1String("ImageViewer Settings"));
0165     }
0166 
0167 #ifdef Q_OS_MACOS
0168 
0169     bool useNativeFileDialog  = group.readEntry(QLatin1String("Use Native File Dialog"), true);
0170 
0171 #else
0172 
0173     bool useNativeFileDialog  = group.readEntry(QLatin1String("Use Native File Dialog"), false);
0174 
0175 #endif
0176 
0177     if (useNativeFileDialog)
0178     {
0179         return (QFileDialog::Option)0;
0180     }
0181 
0182     return QFileDialog::DontUseNativeDialog;
0183 }
0184 
0185 } // namespace Digikam
0186 
0187 #include "moc_dfiledialog.cpp"