File indexing completed on 2024-04-28 05:08:15
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() = default; 0035 0036 void CollectionFactory::registerFunction(int type_, const QString& typeName_, CREATE_COLL_FN func_) { 0037 functionRegistry.insert(type_, func_); 0038 nameRegistry.insert(type_, typeName_); 0039 } 0040 0041 Tellico::Data::CollPtr CollectionFactory::create(int type_, bool addDefaultFields_) const { 0042 Tellico::Data::CollPtr ptr; 0043 if(functionRegistry.contains(type_)) { 0044 ptr = functionRegistry.value(type_)(addDefaultFields_); 0045 } else { 0046 myWarning() << "no collection created for type = " << type_; 0047 ptr = new Data::Collection(addDefaultFields_); 0048 } 0049 return ptr; 0050 } 0051 0052 //static 0053 CollectionFactory& CollectionFactory::self() { 0054 static CollectionFactory instance; 0055 return instance; 0056 } 0057 0058 // static 0059 Tellico::Data::CollPtr CollectionFactory::collection(int type_, bool addDefaultFields_) { 0060 return self().create(type_, addDefaultFields_); 0061 } 0062 0063 // static 0064 Tellico::Data::CollPtr CollectionFactory::collection(const QString& typeName_, bool addDefaultFields_) { 0065 Data::CollPtr coll; 0066 int type = -1; 0067 TypeStringHash::const_iterator i = self().nameRegistry.constBegin(); 0068 while(i != self().nameRegistry.constEnd()) { 0069 if(i.value() == typeName_) { 0070 type = i.key(); 0071 break; 0072 } 0073 ++i; 0074 } 0075 if(type == -1) { 0076 type = Data::Collection::Base; 0077 myWarning() << "bad collection type name:" << typeName_; 0078 } 0079 return self().create(type, addDefaultFields_); 0080 } 0081 0082 Tellico::CollectionNameHash CollectionFactory::nameHash() { 0083 CollectionNameHash hash; 0084 hash[Data::Collection::Book] = i18n("Book Collection"); 0085 hash[Data::Collection::Bibtex] = i18n("Bibliography"); 0086 hash[Data::Collection::ComicBook] = i18n("Comic Book Collection"); 0087 hash[Data::Collection::Video] = i18n("Video Collection"); 0088 hash[Data::Collection::Album] = i18n("Music Collection"); 0089 hash[Data::Collection::Coin] = i18n("Coin Collection"); 0090 hash[Data::Collection::Stamp] = i18n("Stamp Collection"); 0091 hash[Data::Collection::Wine] = i18n("Wine Collection"); 0092 hash[Data::Collection::Card] = i18n("Card Collection"); 0093 hash[Data::Collection::Game] = i18n("Video Game Collection"); 0094 hash[Data::Collection::File] = i18n("File Catalog"); 0095 hash[Data::Collection::BoardGame] = i18n("Board Game Collection"); 0096 hash[Data::Collection::Base] = i18n("Custom Collection"); 0097 return hash; 0098 } 0099 0100 QString CollectionFactory::typeName(int type_) { 0101 if(self().nameRegistry.contains(type_)) { 0102 return self().nameRegistry.value(type_); 0103 } 0104 myWarning() << "collection type not implemented:" << type_; 0105 return QStringLiteral("entry"); 0106 } 0107 0108 QString CollectionFactory::typeName(Data::CollPtr coll_) { 0109 return coll_ ? typeName(coll_->type()) : QStringLiteral("entry"); 0110 } 0111 0112 bool CollectionFactory::isDefaultField(int type_, const QString& name_) { 0113 Data::CollPtr coll = collection(type_, true); 0114 foreach(Data::FieldPtr field, coll->fields()) { 0115 if(field->name() == name_) { 0116 return true; 0117 } 0118 } 0119 return false; 0120 }