File indexing completed on 2024-04-28 16:31:51

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "collectionfactory.h"
0026 #include "collection.h"
0027 #include "tellico_debug.h"
0028 
0029 #include <KLocalizedString>
0030 
0031 using namespace Tellico;
0032 using Tellico::CollectionFactory;
0033 
0034 CollectionFactory::CollectionFactory() {
0035 }
0036 
0037 void CollectionFactory::registerFunction(int type_, const QString& typeName_, CREATE_COLL_FN func_) {
0038   functionRegistry.insert(type_, func_);
0039   nameRegistry.insert(type_, typeName_);
0040 }
0041 
0042 Tellico::Data::CollPtr CollectionFactory::create(int type_, bool addDefaultFields_) const {
0043   Tellico::Data::CollPtr ptr;
0044   if(functionRegistry.contains(type_)) {
0045     ptr = functionRegistry.value(type_)(addDefaultFields_);
0046   } else {
0047     myWarning() << "no collection created for type = " << type_;
0048     ptr = new Data::Collection(addDefaultFields_);
0049   }
0050   return ptr;
0051 }
0052 
0053 //static
0054 CollectionFactory& CollectionFactory::self() {
0055   static CollectionFactory instance;
0056   return instance;
0057 }
0058 
0059 // static
0060 Tellico::Data::CollPtr CollectionFactory::collection(int type_, bool addDefaultFields_) {
0061   return self().create(type_, addDefaultFields_);
0062 }
0063 
0064 // static
0065 Tellico::Data::CollPtr CollectionFactory::collection(const QString& typeName_, bool addDefaultFields_) {
0066   Data::CollPtr coll;
0067   int type = -1;
0068   TypeStringHash::const_iterator i = self().nameRegistry.constBegin();
0069   while(i != self().nameRegistry.constEnd()) {
0070     if(i.value() == typeName_) {
0071       type = i.key();
0072       break;
0073     }
0074     ++i;
0075   }
0076   if(type == -1) {
0077     type = Data::Collection::Base;
0078     myWarning() << "bad collection type name:" << typeName_;
0079   }
0080   return self().create(type, addDefaultFields_);
0081 }
0082 
0083 Tellico::CollectionNameHash CollectionFactory::nameHash() {
0084   CollectionNameHash hash;
0085   hash[Data::Collection::Book]        = i18n("Book Collection");
0086   hash[Data::Collection::Bibtex]      = i18n("Bibliography");
0087   hash[Data::Collection::ComicBook]   = i18n("Comic Book Collection");
0088   hash[Data::Collection::Video]       = i18n("Video Collection");
0089   hash[Data::Collection::Album]       = i18n("Music Collection");
0090   hash[Data::Collection::Coin]        = i18n("Coin Collection");
0091   hash[Data::Collection::Stamp]       = i18n("Stamp Collection");
0092   hash[Data::Collection::Wine]        = i18n("Wine Collection");
0093   hash[Data::Collection::Card]        = i18n("Card Collection");
0094   hash[Data::Collection::Game]        = i18n("Game Collection");
0095   hash[Data::Collection::File]        = i18n("File Catalog");
0096   hash[Data::Collection::BoardGame]   = i18n("Board Game Collection");
0097   hash[Data::Collection::Base]        = i18n("Custom Collection");
0098   return hash;
0099 }
0100 
0101 QString CollectionFactory::typeName(int type_) {
0102   if(self().nameRegistry.contains(type_)) {
0103     return self().nameRegistry.value(type_);
0104   }
0105   myWarning() << "collection type not implemented:" << type_;
0106   return QStringLiteral("entry");
0107 }
0108 
0109 QString CollectionFactory::typeName(Data::CollPtr coll_) {
0110   return coll_ ? typeName(coll_->type()) : QStringLiteral("entry");
0111 }
0112 
0113 bool CollectionFactory::isDefaultField(int type_, const QString& name_) {
0114   Data::CollPtr coll = collection(type_, true);
0115   foreach(Data::FieldPtr field, coll->fields()) {
0116     if(field->name() == name_) {
0117       return true;
0118     }
0119   }
0120   return false;
0121 }