File indexing completed on 2024-05-19 04:49:41

0001 /****************************************************************************************
0002  * Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de>                                      *
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) version 3 or        *
0007  * any later version accepted by the membership of KDE e.V. (or its successor approved  *
0008  * by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of  *
0009  * version 3 of the license.                                                            *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #define DEBUG_PREFIX "SearchQueryBias"
0020 
0021 #include "SearchQueryBias.h"
0022 
0023 #include "core/collections/Collection.h"
0024 #include "core/collections/QueryMaker.h"
0025 #include "core/support/Debug.h"
0026 #include "core-impl/collections/support/CollectionManager.h"
0027 #include "core-impl/collections/support/TextualQueryFilter.h"
0028 #include "dynamic/TrackSet.h"
0029 
0030 #include <QLineEdit>
0031 
0032 #include <QLabel>
0033 #include <QCheckBox>
0034 #include <QHBoxLayout>
0035 #include <QVBoxLayout>
0036 #include <QXmlStreamReader>
0037 #include <QXmlStreamWriter>
0038 
0039 
0040 QString
0041 Dynamic::SearchQueryBiasFactory::i18nName() const
0042 { return i18nc("Name of the \"SearchQuery\" bias", "Search"); }
0043 
0044 QString
0045 Dynamic::SearchQueryBiasFactory::name() const
0046 { return Dynamic::SearchQueryBias::sName(); }
0047 
0048 QString
0049 Dynamic::SearchQueryBiasFactory::i18nDescription() const
0050 { return i18nc("Description of the \"SearchQuery\" bias",
0051                "The \"SearchQuery\" bias adds tracks that are\n"
0052                "found by a search query. It uses the same search\n"
0053                "query as the collection browser."); }
0054 
0055 Dynamic::BiasPtr
0056 Dynamic::SearchQueryBiasFactory::createBias()
0057 { return Dynamic::BiasPtr( new Dynamic::SearchQueryBias() ); }
0058 
0059 // ----- SearchQueryBias --------
0060 
0061 Dynamic::SearchQueryBias::SearchQueryBias( const QString &filter )
0062     : SimpleMatchBias()
0063     , m_filter( filter )
0064 { }
0065 
0066 void
0067 Dynamic::SearchQueryBias::fromXml( QXmlStreamReader *reader )
0068 {
0069     DEBUG_BLOCK;
0070 
0071     while (!reader->atEnd()) {
0072         reader->readNext();
0073 
0074         if( reader->isStartElement() )
0075         {
0076             QStringRef name = reader->name();
0077             if( name == "filter" )
0078                 m_filter = reader->readElementText(QXmlStreamReader::SkipChildElements);
0079             else
0080             {
0081                 debug()<<"Unexpected xml start element"<<reader->name()<<"in input";
0082                 reader->skipCurrentElement();
0083             }
0084         }
0085         else if( reader->isEndElement() )
0086         {
0087             break;
0088         }
0089     }
0090 }
0091 
0092 void
0093 Dynamic::SearchQueryBias::toXml( QXmlStreamWriter *writer ) const
0094 {
0095     writer->writeTextElement( QStringLiteral("filter"), m_filter );
0096 }
0097 
0098 QString
0099 Dynamic::SearchQueryBias::sName()
0100 {
0101     return QStringLiteral( "searchQueryBias" );
0102 }
0103 
0104 QString
0105 Dynamic::SearchQueryBias::name() const
0106 {
0107     return Dynamic::SearchQueryBias::sName();
0108 }
0109 
0110 QString
0111 Dynamic::SearchQueryBias::toString() const
0112 {
0113     if( m_filter.isEmpty() )
0114         return i18nc("Random bias representation",
0115                      "Random tracks");
0116     else
0117         return i18nc("SearchQuery bias representation",
0118                      "Search for: %1", m_filter );
0119 }
0120 
0121 QWidget*
0122 Dynamic::SearchQueryBias::widget( QWidget* parent )
0123 {
0124     QWidget *widget = new QWidget( parent );
0125     QVBoxLayout *layout = new QVBoxLayout( widget );
0126 
0127     QLineEdit *edit = new QLineEdit( m_filter );
0128     layout->addWidget( edit );
0129 
0130     connect( edit, &QLineEdit::textChanged,
0131              this, &SearchQueryBias::setFilter );
0132 
0133     return widget;
0134 }
0135 
0136 QString
0137 Dynamic::SearchQueryBias::filter() const
0138 {
0139     return m_filter;
0140 }
0141 
0142 void
0143 Dynamic::SearchQueryBias::setFilter( const QString &filter )
0144 {
0145     DEBUG_BLOCK;
0146     if( filter == m_filter )
0147         return;
0148 
0149     m_filter = filter;
0150     invalidate();
0151     Q_EMIT changed( BiasPtr(this) );
0152 }
0153 
0154 void
0155 Dynamic::SearchQueryBias::newQuery()
0156 {
0157     DEBUG_BLOCK;
0158 
0159     // ok, I need a new query maker
0160     m_qm.reset( CollectionManager::instance()->queryMaker() );
0161     Collections::addTextualFilter( m_qm.data(), m_filter );
0162     m_qm->setQueryType( Collections::QueryMaker::Custom );
0163     m_qm->addReturnValue( Meta::valUniqueId );
0164 
0165     connect( m_qm.data(), &Collections::QueryMaker::newResultReady,
0166              this, &SearchQueryBias::updateReady, Qt::QueuedConnection );
0167     connect( m_qm.data(), &Collections::QueryMaker::queryDone,
0168              this, &SearchQueryBias::updateFinished, Qt::QueuedConnection );
0169     m_qm.data()->run();
0170 }
0171 
0172