File indexing completed on 2023-05-30 11:30:45

0001 /**
0002  * Copyright (C) 2009 Michael Pyne <mpyne@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "dbuscollectionproxy.h"
0018 
0019 #include <QStringList>
0020 #include <QFile>
0021 #include <QPixmap>
0022 #include <QDBusConnection>
0023 #include <QTemporaryFile>
0024 
0025 #include "collectionadaptor.h"
0026 #include "playlistcollection.h"
0027 #include "covermanager.h"
0028 #include "collectionlist.h"
0029 #include "coverinfo.h"
0030 #include "filehandle.h"
0031 #include "juk_debug.h"
0032 
0033 DBusCollectionProxy::DBusCollectionProxy (QObject *parent, PlaylistCollection *collection) :
0034     QObject(parent), m_collection(collection)
0035 {
0036     setObjectName( QLatin1String("DBusCollectionProxy" ));
0037 
0038     new CollectionAdaptor(this);
0039     QDBusConnection::sessionBus().registerObject("/Collection",this);
0040 }
0041 
0042 DBusCollectionProxy::~DBusCollectionProxy()
0043 {
0044     // Clean's the way to be
0045     if(!m_lastCover.isEmpty())
0046         QFile::remove(m_lastCover);
0047 }
0048 
0049 void DBusCollectionProxy::openFile(const QString &file)
0050 {
0051     m_collection->open(QStringList(file));
0052 }
0053 
0054 void DBusCollectionProxy::openFile(const QStringList &files)
0055 {
0056     m_collection->open(files);
0057 }
0058 
0059 void DBusCollectionProxy::openFile(const QString &playlist, const QString &file)
0060 {
0061     m_collection->open(playlist, QStringList(file));
0062 }
0063 
0064 void DBusCollectionProxy::openFile(const QString &playlist, const QStringList &files)
0065 {
0066     m_collection->open(playlist, files);
0067 }
0068 
0069 QString DBusCollectionProxy::visiblePlaylist()
0070 {
0071     return m_collection->playlist();
0072 }
0073 
0074 QString DBusCollectionProxy::playingPlaylist()
0075 {
0076     return m_collection->playingPlaylist();
0077 }
0078 
0079 QStringList DBusCollectionProxy::playlists()
0080 {
0081     return m_collection->playlists();
0082 }
0083 
0084 QStringList DBusCollectionProxy::playlistTracks(const QString &playlist)
0085 {
0086     return m_collection->playlistTracks(playlist);
0087 }
0088 
0089 QString DBusCollectionProxy::trackProperty(const QString &file, const QString &property)
0090 {
0091     return m_collection->trackProperty(file, property);
0092 }
0093 
0094 void DBusCollectionProxy::createPlaylist(const QString &name)
0095 {
0096     m_collection->createPlaylist(name);
0097 }
0098 
0099 void DBusCollectionProxy::setPlaylist(const QString &name)
0100 {
0101     m_collection->setPlaylist(name);
0102 }
0103 
0104 void DBusCollectionProxy::remove()
0105 {
0106     m_collection->remove();
0107 }
0108 
0109 void DBusCollectionProxy::removeTrack(const QString &playlist, const QStringList &files)
0110 {
0111     m_collection->removeTrack(playlist, files);
0112 }
0113 
0114 QString DBusCollectionProxy::trackCover(const QString &track)
0115 {
0116     coverKey id = CoverManager::idForTrack(track);
0117     if(id != CoverManager::NoMatch) {
0118         CoverData coverData = CoverManager::coverInfo(id);
0119         return coverData.path;
0120     }
0121 
0122     // No cover, let's see if one is embedded.
0123     CollectionListItem *collectionItem = CollectionList::instance()->lookup(track);
0124 
0125     if(!collectionItem)
0126         return QString();
0127 
0128     CoverInfo *coverInfo = collectionItem->file().coverInfo();
0129     if(!coverInfo)
0130         return QString();
0131 
0132     QPixmap cover = coverInfo->pixmap(CoverInfo::FullSize);
0133     if(cover.isNull())
0134         return QString();
0135 
0136     // We have a cover, extract it and save it to a temporary file.
0137     QTemporaryFile tempFile;
0138 
0139     tempFile.setFileTemplate(QStringLiteral("juk_cover_XXXXXX.png"));
0140     tempFile.setAutoRemove(false);
0141 
0142     if(!tempFile.open()) {
0143         qCCritical(JUK_LOG) << "Unable to open temporary file for embedded cover art.";
0144         return QString();
0145     }
0146 
0147     // Save last file name cover, remove it if it's there so that we don't fill
0148     // the temp directory with pixmaps.
0149     if(!m_lastCover.isEmpty())
0150         QFile::remove(m_lastCover);
0151     m_lastCover = tempFile.fileName();
0152 
0153     cover.save(&tempFile, "PNG");
0154     return tempFile.fileName();
0155 }
0156 
0157 // vim: set et sw=4 tw=0 sta: