File indexing completed on 2024-05-26 04:57:40

0001 /**
0002  * \file kdeplatformtools.cpp
0003  * KDE platform specific tools.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 30 Mar 2013
0008  *
0009  * Copyright (C) 2013-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "kdeplatformtools.h"
0028 #include <QCoreApplication>
0029 #include <QtConfig>
0030 #include <kconfig_version.h>
0031 #include <kwidgetsaddons_version.h>
0032 #include <KMessageBox>
0033 #include <KSharedConfig>
0034 #include <KIO/CopyJob>
0035 #include <QUrl>
0036 #include <QFileDialog>
0037 #include <QDesktopServices>
0038 #include "mainwindowconfig.h"
0039 #include "kdesettings.h"
0040 
0041 /**
0042  * Constructor.
0043  */
0044 KdePlatformTools::KdePlatformTools()
0045 {
0046 }
0047 
0048 /**
0049  * Destructor.
0050  */
0051 KdePlatformTools::~KdePlatformTools()
0052 {
0053   // Must not be inline because of forwared declared QScopedPointer.
0054 }
0055 
0056 /**
0057  * Get application settings.
0058  * @return settings instance.
0059  */
0060 ISettings* KdePlatformTools::applicationSettings()
0061 {
0062   if (!m_config) {
0063     auto cfg = KSharedConfig::openConfig();
0064 #if KCONFIG_VERSION >= 0x054300
0065     auto stateCfg = KSharedConfig::openStateConfig();
0066 #else
0067     auto stateCfg = cfg;
0068 #endif
0069     m_config.reset(new KdeSettings(cfg, stateCfg));
0070   }
0071   return m_config.data();
0072 }
0073 
0074 /**
0075  * Get icon provider for tagged files.
0076  * @return icon provider.
0077  */
0078 CoreTaggedFileIconProvider* KdePlatformTools::iconProvider()
0079 {
0080   return GuiPlatformTools::iconProvider();
0081 }
0082 
0083 /**
0084  * Write text to clipboard.
0085  * @param text text to write
0086  * @return true if operation is supported.
0087  */
0088 bool KdePlatformTools::writeToClipboard(const QString& text) const
0089 {
0090   return GuiPlatformTools::writeToClipboard(text);
0091 }
0092 
0093 /**
0094  * Read text from clipboard.
0095  * @return text, null if operation not supported.
0096  */
0097 QString KdePlatformTools::readFromClipboard() const
0098 {
0099   return GuiPlatformTools::readFromClipboard();
0100 }
0101 
0102 /**
0103  * Create an audio player instance.
0104  * @param app application context
0105  * @param dbusEnabled true to enable MPRIS D-Bus interface
0106  * @return audio player, nullptr if not supported.
0107  */
0108 QObject* KdePlatformTools::createAudioPlayer(Kid3Application* app,
0109                                    bool dbusEnabled) const
0110 {
0111   return GuiPlatformTools::createAudioPlayer(app, dbusEnabled);
0112 }
0113 
0114 /**
0115  * Move file or directory to trash.
0116  *
0117  * @param path path to file or directory
0118  *
0119  * @return true if ok.
0120  */
0121 bool KdePlatformTools::moveToTrash(const QString& path) const
0122 {
0123   KIO::Job* job = KIO::trash(QUrl::fromLocalFile(path));
0124   return job->exec();
0125 }
0126 
0127 /**
0128  * Display help for a topic.
0129  *
0130  * @param anchor anchor in help document
0131  */
0132 void KdePlatformTools::displayHelp(const QString& anchor)
0133 {
0134   QUrl url(QLatin1String("help:/kid3/index.html"));
0135   if (!anchor.isEmpty()) {
0136     url.setFragment(anchor);
0137   }
0138   QDesktopServices::openUrl(url);
0139 }
0140 
0141 /**
0142  * Get a themed icon by name.
0143  * @param name name of icon
0144  * @return icon.
0145  */
0146 QIcon KdePlatformTools::iconFromTheme(const QString& name) const
0147 {
0148   return QIcon::fromTheme(name);
0149 }
0150 
0151 /**
0152  * Construct a name filter string suitable for file dialogs.
0153  * @param nameFilters list of description, filter pairs, e.g.
0154  * [("Images", "*.jpg *.jpeg *.png"), ("All Files", "*")].
0155  * @return name filter string.
0156  */
0157 QString KdePlatformTools::fileDialogNameFilter(
0158     const QList<QPair<QString, QString> >& nameFilters) const
0159 {
0160   return ICorePlatformTools::qtFileDialogNameFilter(nameFilters);
0161 }
0162 
0163 /**
0164  * Get file pattern part of m_nameFilter.
0165  * @param nameFilter name filter string
0166  * @return file patterns, e.g. "*.mp3".
0167  */
0168 QString KdePlatformTools::getNameFilterPatterns(const QString& nameFilter) const
0169 {
0170   return ICorePlatformTools::qtNameFilterPatterns(nameFilter);
0171 }
0172 
0173 /**
0174  * Display error dialog with item list.
0175  * @param parent parent widget
0176  * @param text text
0177  * @param strlist list of items
0178  * @param caption caption
0179  */
0180 void KdePlatformTools::errorList(QWidget* parent, const QString& text,
0181     const QStringList& strlist, const QString& caption)
0182 {
0183   KMessageBox::errorList(parent, text, strlist, caption);
0184 }
0185 
0186 /**
0187  * Display warning dialog with yes, no, cancel buttons.
0188  * @param parent parent widget
0189  * @param text text
0190  * @param caption caption
0191  * @return QMessageBox::Yes, QMessageBox::No or QMessageBox::Cancel.
0192  */
0193 int KdePlatformTools::warningYesNoCancel(QWidget* parent, const QString& text,
0194     const QString& caption)
0195 {
0196 #if KWIDGETSADDONS_VERSION >= 0x05f000
0197 switch (KMessageBox::warningTwoActionsCancel(parent, text, caption,
0198           KGuiItem(QCoreApplication::translate("@default", "&Yes")),
0199           KGuiItem(QCoreApplication::translate("@default", "&No")))) {
0200   case KMessageBox::Ok:
0201     return QMessageBox::Ok;
0202   case KMessageBox::Cancel:
0203     return QMessageBox::Cancel;
0204   case KMessageBox::PrimaryAction:
0205     return QMessageBox::Yes;
0206   case KMessageBox::SecondaryAction:
0207     return QMessageBox::No;
0208   case KMessageBox::Continue:
0209   default:
0210     return QMessageBox::Ignore;
0211   }
0212 #else
0213   switch (KMessageBox::warningYesNoCancel(parent, text, caption)) {
0214   case KMessageBox::Ok:
0215     return QMessageBox::Ok;
0216   case KMessageBox::Cancel:
0217     return QMessageBox::Cancel;
0218   case KMessageBox::Yes:
0219     return QMessageBox::Yes;
0220   case KMessageBox::No:
0221     return QMessageBox::No;
0222   case KMessageBox::Continue:
0223   default:
0224     return QMessageBox::Ignore;
0225   }
0226 #endif
0227 }
0228 
0229 /**
0230  * Display warning dialog with item list.
0231  * @param parent parent widget
0232  * @param text text
0233  * @param strlist list of items
0234  * @param caption caption
0235  * @return QMessageBox::Yes or QMessageBox::No.
0236  */
0237 int KdePlatformTools::warningYesNoList(QWidget* parent, const QString& text,
0238     const QStringList& strlist, const QString& caption)
0239 {
0240 #if KWIDGETSADDONS_VERSION >= 0x05f000
0241   switch (KMessageBox::warningTwoActionsList(parent, text, strlist, caption,
0242             KGuiItem(QCoreApplication::translate("@default", "&Yes")),
0243             KGuiItem(QCoreApplication::translate("@default", "&No")))) {
0244   case KMessageBox::PrimaryAction:
0245     return QMessageBox::Yes;
0246   case KMessageBox::SecondaryAction:
0247   default:
0248     return QMessageBox::No;
0249   }
0250 #else
0251   switch (KMessageBox::warningYesNoList(parent, text, strlist, caption)) {
0252   case KMessageBox::Yes:
0253     return QMessageBox::Yes;
0254   case KMessageBox::No:
0255   default:
0256     return QMessageBox::No;
0257   }
0258 #endif
0259 }
0260 
0261 /**
0262  * Display dialog to select an existing file.
0263  * @param parent parent widget
0264  * @param caption caption
0265  * @param dir directory
0266  * @param filter filter
0267  * @param selectedFilter the selected filter is returned here
0268  * @return selected file, empty if canceled.
0269  */
0270 QString KdePlatformTools::getOpenFileName(QWidget* parent,
0271     const QString& caption, const QString& dir, const QString& filter,
0272     QString* selectedFilter)
0273 {
0274   return QFileDialog::getOpenFileName(
0275         parent, caption, dir, filter, selectedFilter,
0276         MainWindowConfig::instance().dontUseNativeDialogs()
0277         ? QFileDialog::DontUseNativeDialog : QFileDialog::Options());
0278 }
0279 
0280 /**
0281  * Display dialog to select existing files.
0282  * @param parent parent widget
0283  * @param caption caption
0284  * @param dir directory
0285  * @param filter filter
0286  * @param selectedFilter the selected filter is returned here
0287  * @return selected files, empty if canceled.
0288  */
0289 QStringList KdePlatformTools::getOpenFileNames(QWidget* parent,
0290     const QString& caption, const QString& dir,
0291     const QString& filter, QString* selectedFilter)
0292 {
0293   return QFileDialog::getOpenFileNames(
0294         parent, caption, dir, filter, selectedFilter,
0295         MainWindowConfig::instance().dontUseNativeDialogs()
0296         ? QFileDialog::DontUseNativeDialog : QFileDialog::Options());
0297 }
0298 
0299 /**
0300  * Display dialog to select a file to save.
0301  * @param parent parent widget
0302  * @param caption caption
0303  * @param dir directory
0304  * @param filter filter
0305  * @param selectedFilter the selected filter is returned here
0306  * @return selected file, empty if canceled.
0307  */
0308 QString KdePlatformTools::getSaveFileName(QWidget* parent,
0309     const QString& caption, const QString& dir, const QString& filter,
0310     QString* selectedFilter)
0311 {
0312   return QFileDialog::getSaveFileName(
0313         parent, caption, dir, filter, selectedFilter,
0314         MainWindowConfig::instance().dontUseNativeDialogs()
0315         ? QFileDialog::DontUseNativeDialog : QFileDialog::Options());
0316 }
0317 
0318 /**
0319  * Display dialog to select an existing directory.
0320  * @param parent parent widget
0321  * @param caption caption
0322  * @param startDir start directory
0323  * @return selected directory, empty if canceled.
0324  */
0325 QString KdePlatformTools::getExistingDirectory(QWidget* parent,
0326     const QString& caption, const QString& startDir)
0327 {
0328   return QFileDialog::getExistingDirectory(parent, caption, startDir,
0329       MainWindowConfig::instance().dontUseNativeDialogs()
0330       ? QFileDialog::ShowDirsOnly | QFileDialog::DontUseNativeDialog
0331       : QFileDialog::ShowDirsOnly);
0332 }
0333 
0334 /**
0335  * Check if platform has a graphical user interface.
0336  * @return true if platform has GUI.
0337  */
0338 bool KdePlatformTools::hasGui() const
0339 {
0340   return true;
0341 }
0342 
0343 /**
0344  * Display warning dialog.
0345  * @param parent parent widget
0346  * @param text text
0347  * @param details detailed message
0348  * @param caption caption
0349  */
0350 void KdePlatformTools::warningDialog(QWidget* parent,
0351     const QString& text, const QString& details, const QString& caption)
0352 {
0353   KMessageBox::error(parent, text + details, caption);
0354 }
0355 
0356 /**
0357  * Display warning dialog with options to continue or cancel.
0358  * @param parent parent widget
0359  * @param text text
0360  * @param strlist list of items
0361  * @param caption caption
0362  * @return true if continue was selected.
0363  */
0364 bool KdePlatformTools::warningContinueCancelList(QWidget* parent,
0365     const QString& text, const QStringList& strlist, const QString& caption)
0366 {
0367   return KMessageBox::warningContinueCancelList(parent, text, strlist, caption,
0368       KStandardGuiItem::ok(), KStandardGuiItem::cancel(), QString(),
0369       KMessageBox::Dangerous) == KMessageBox::Continue;
0370 }