File indexing completed on 2024-05-05 04:48:23

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
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 "DBusQueryHelper.h"
0018 
0019 #include "core/collections/QueryMaker.h"
0020 #include "core/meta/Meta.h"
0021 #include "core/meta/support/MetaUtility.h"
0022 #include "core/support/Debug.h"
0023 
0024 #include <QTimer>
0025 
0026 Q_DECLARE_METATYPE( VariantMapList )
0027 
0028 DBusQueryHelper::DBusQueryHelper( QObject *parent, Collections::QueryMaker *qm, const QDBusConnection &conn, const QDBusMessage &msg, bool mprisCompatible )
0029     : QObject( parent )
0030     , m_connection( conn )
0031     , m_message( msg )
0032     , m_mprisCompatibleResult( mprisCompatible )
0033     , m_timeout( false )
0034 {
0035     qm->setAutoDelete( true );
0036     qm->setQueryType( Collections::QueryMaker::Track );
0037     connect( qm, &Collections::QueryMaker::newTracksReady, this, &DBusQueryHelper::slotResultReady, Qt::QueuedConnection );
0038     connect( qm, &Collections::QueryMaker::queryDone, this, &DBusQueryHelper::slotQueryDone, Qt::QueuedConnection );
0039     qm->run();
0040 
0041     //abort query after 15 seconds in case the query does not return
0042     QTimer::singleShot( 15000, this, &DBusQueryHelper::abortQuery );
0043 }
0044 
0045 void
0046 DBusQueryHelper::slotResultReady( const Meta::TrackList &tracks )
0047 {
0048     foreach( const Meta::TrackPtr &track, tracks )
0049     {
0050         if( m_mprisCompatibleResult )
0051             m_result.append( Meta::Field::mprisMapFromTrack( track ) );
0052         else
0053             m_result.append( Meta::Field::mapFromTrack( track ) );
0054     }
0055 }
0056 
0057 void
0058 DBusQueryHelper::slotQueryDone()
0059 {
0060     deleteLater();
0061 
0062     if( m_timeout )
0063         return;
0064 
0065     QDBusMessage reply = m_message.createReply( QVariant::fromValue( m_result ) );
0066     bool success = m_connection.send( reply );
0067     if( !success )
0068         debug() << "sending async reply failed";
0069 }
0070 
0071 void
0072 DBusQueryHelper::abortQuery()
0073 {
0074     deleteLater();
0075     m_timeout = true;
0076 
0077     QDBusMessage error = m_message.createErrorReply( QDBusError::InternalError, QStringLiteral("Internal timeout") );
0078     bool success = m_connection.send( error );
0079     if( !success )
0080         debug() << "sending async error failed";
0081 }