File indexing completed on 2025-02-23 04:34:21

0001 /**
0002  * \file guiplatformtools.cpp
0003  * Platform specific tools for QtGui (without QtWidget).
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 15 Jul 2019
0008  *
0009  * Copyright (C) 2019-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 "guiplatformtools.h"
0028 #include <QGuiApplication>
0029 #include <QClipboard>
0030 #include "taggedfileiconprovider.h"
0031 #include "config.h"
0032 #ifdef HAVE_QTMULTIMEDIA
0033 #include "audioplayer.h"
0034 #ifdef HAVE_QTDBUS
0035 #include "mprisinterface.h"
0036 #endif
0037 #endif
0038 
0039 /**
0040  * Destructor.
0041  */
0042 GuiPlatformTools::~GuiPlatformTools()
0043 {
0044   // Must not be inline because of forwared declared QScopedPointer.
0045 }
0046 
0047 /**
0048  * Get icon provider for tagged files.
0049  * @return icon provider.
0050  */
0051 CoreTaggedFileIconProvider* GuiPlatformTools::iconProvider()
0052 {
0053   if (!m_iconProvider) {
0054     m_iconProvider.reset(new TaggedFileIconProvider);
0055   }
0056   return m_iconProvider.data();
0057 }
0058 
0059 /**
0060  * Write text to clipboard.
0061  * @param text text to write
0062  * @return true if operation is supported.
0063  */
0064 bool GuiPlatformTools::writeToClipboard(const QString& text) const
0065 {
0066   QGuiApplication::clipboard()->setText(text, QClipboard::Clipboard);
0067   return true;
0068 }
0069 
0070 /**
0071  * Read text from clipboard.
0072  * @return text, null if operation not supported.
0073  */
0074 QString GuiPlatformTools::readFromClipboard() const
0075 {
0076   QClipboard* cb = QGuiApplication::clipboard();
0077   QString text = cb->text(QClipboard::Clipboard);
0078   if (text.isNull())
0079     text = cb->text(QClipboard::Selection);
0080   return text;
0081 }
0082 
0083 /**
0084  * Create an audio player instance.
0085  * @param app application context
0086  * @param dbusEnabled true to enable MPRIS D-Bus interface
0087  * @return audio player, nullptr if not supported.
0088  */
0089 QObject* GuiPlatformTools::createAudioPlayer(Kid3Application* app,
0090                                              bool dbusEnabled) const
0091 {
0092 #ifdef HAVE_QTMULTIMEDIA
0093   auto player = new AudioPlayer(app);
0094 #ifdef HAVE_QTDBUS
0095   if (dbusEnabled) {
0096     new MprisInterface(player);
0097     new MprisPlayerInterface(player);
0098   }
0099 #else
0100   Q_UNUSED(dbusEnabled)
0101 #endif
0102   return player;
0103 #else
0104   Q_UNUSED(app)
0105   Q_UNUSED(dbusEnabled)
0106   return nullptr;
0107 #endif
0108 }