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

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.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 #define DEBUG_PREFIX "APG::ConstraintFactory"
0018 
0019 #include "Constraint.h"
0020 #include "ConstraintFactory.h"
0021 #include "ConstraintGroup.h"
0022 #include "ConstraintNode.h"
0023 #include "constraints/Checkpoint.h"
0024 #include "constraints/PlaylistDuration.h"
0025 #include "constraints/PlaylistFileSize.h"
0026 #include "constraints/PlaylistLength.h"
0027 #include "constraints/PreventDuplicates.h"
0028 #include "constraints/TagMatch.h"
0029 
0030 #include "core/support/Debug.h"
0031 
0032 #include <QList>
0033 #include <QPair>
0034 #include <QString>
0035 #include <QStringList>
0036 
0037 /******************************************
0038  * Constraint Factory Registry Entries    *
0039  ******************************************/
0040 
0041 ConstraintFactoryEntry::ConstraintFactoryEntry( const QString& name,
0042                                                 const QString& i18nN,
0043                                                 const QString& desc,
0044                                                 Constraint*( *xmlf )( QDomElement&, ConstraintNode* ),
0045                                                 Constraint*( *nf )( ConstraintNode* ) )
0046         : m_name( name )
0047         , m_i18nName( i18nN )
0048         , m_description( desc )
0049         , m_createFromXmlFunc( xmlf )
0050         , m_createNewFunc( nf )
0051 {
0052 }
0053 
0054 /******************************************
0055  * Constraint Factory Singleton           *
0056  ******************************************/
0057 
0058 ConstraintFactory* ConstraintFactory::s_self = nullptr;
0059 
0060 ConstraintFactory* ConstraintFactory::instance()
0061 {
0062     if ( !ConstraintFactory::s_self ) {
0063         ConstraintFactory::s_self = new ConstraintFactory();
0064     }
0065     return ConstraintFactory::s_self;
0066 }
0067 
0068 void ConstraintFactory::destroy()
0069 {
0070     delete ConstraintFactory::s_self;
0071     ConstraintFactory::s_self = nullptr;
0072 }
0073 
0074 ConstraintFactory::ConstraintFactory()
0075 {
0076     ConstraintFactoryEntry* r = nullptr;
0077 
0078     r = ConstraintTypes::TagMatch::registerMe();
0079     m_registryIds[0] = r;
0080     m_registryNames[r->m_name] = r;
0081     m_registryUntranslateNames[r->m_i18nName] = r->m_name;
0082 
0083     r = ConstraintTypes::PlaylistDuration::registerMe();
0084     m_registryIds[1] = r;
0085     m_registryNames[r->m_name] = r;
0086     m_registryUntranslateNames[r->m_i18nName] = r->m_name;
0087 
0088     r = ConstraintTypes::PlaylistLength::registerMe();
0089     m_registryIds[2] = r;
0090     m_registryNames[r->m_name] = r;
0091     m_registryUntranslateNames[r->m_i18nName] = r->m_name;
0092 
0093     r = ConstraintTypes::PreventDuplicates::registerMe();
0094     m_registryIds[3] = r;
0095     m_registryNames[r->m_name] = r;
0096     m_registryUntranslateNames[r->m_i18nName] = r->m_name;
0097 
0098     r = ConstraintTypes::Checkpoint::registerMe();
0099     m_registryIds[4] = r;
0100     m_registryNames[r->m_name] = r;
0101     m_registryUntranslateNames[r->m_i18nName] = r->m_name;
0102 
0103     r = ConstraintTypes::PlaylistFileSize::registerMe();
0104     m_registryIds[5] = r;
0105     m_registryNames[r->m_name] = r;
0106     m_registryUntranslateNames[r->m_i18nName] = r->m_name;
0107 
0108     // ADD NEW CONSTRAINT TYPES HERE FOLLOWING SAME PATTERN (DON'T FORGET TO INCREMENT ID)
0109 }
0110 
0111 ConstraintFactory::~ConstraintFactory()
0112 {
0113     foreach( ConstraintFactoryEntry* e, m_registryIds ) {
0114         delete e;
0115     }
0116 }
0117 
0118 ConstraintNode* ConstraintFactory::createConstraint( QDomElement& xmlelem, ConstraintNode* parent, int row ) const
0119 {
0120     QString t = xmlelem.attributeNode( QStringLiteral("type") ).value();
0121     if ( !m_registryNames.contains( t ) || !parent )
0122         return nullptr;
0123 
0124     ConstraintNode* n = ( *( m_registryNames[t]->m_createFromXmlFunc ) )( xmlelem, parent );
0125     parent->addChild( n, row );
0126     return n;
0127 }
0128 
0129 ConstraintNode* ConstraintFactory::createConstraint( const QString& name, ConstraintNode* parent, int row ) const
0130 {
0131     if ( !m_registryNames.contains( name ) || !parent )
0132         return nullptr;
0133 
0134     ConstraintNode* n = ( *( m_registryNames[name]->m_createNewFunc ) )( parent );
0135     parent->addChild( n, row );
0136     return n;
0137 }
0138 
0139 ConstraintNode* ConstraintFactory::createConstraint( const int idx, ConstraintNode* parent, int row ) const
0140 {
0141     if ( !m_registryIds.contains( idx ) || !parent )
0142         return nullptr;
0143 
0144     ConstraintNode* n = ( *( m_registryIds[idx]->m_createNewFunc ) )( parent );
0145     parent->addChild( n, row );
0146     return n;
0147 }
0148 
0149 ConstraintNode* ConstraintFactory::createGroup( QDomElement& xmlelem, ConstraintNode* parent, int row ) const
0150 {
0151     ConstraintNode* n = ConstraintGroup::createFromXml( xmlelem, parent );
0152     if ( parent )
0153         parent->addChild( n, row );
0154     return n;
0155 }
0156 
0157 ConstraintNode* ConstraintFactory::createGroup( ConstraintNode* parent, int row ) const
0158 {
0159     ConstraintNode* n = ConstraintGroup::createNew( parent );
0160     if ( parent )
0161         parent->addChild( n, row );
0162     return n;
0163 }
0164 
0165 const QStringList ConstraintFactory::names() const
0166 {
0167     return m_registryNames.keys();
0168 }
0169 
0170 const QStringList ConstraintFactory::i18nNames() const
0171 {
0172     return m_registryUntranslateNames.keys();
0173 }
0174 
0175 QList< QPair<int, QString> > ConstraintFactory::registeredConstraints() const
0176 {
0177     QList< QPair<int, QString> > d;
0178     foreach( int i, m_registryIds.keys() ) {
0179         d.append( QPair<int, QString>( i, m_registryIds[i]->m_name ) );
0180     }
0181     return d;
0182 }
0183 
0184 const QString ConstraintFactory::untranslateName( const QString& trn ) const
0185 {
0186     return m_registryUntranslateNames.value( trn );
0187 }