File indexing completed on 2024-05-12 05:09:22

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 "bookcollection.h"
0026 #include "../entrycomparison.h"
0027 
0028 #include <KLocalizedString>
0029 
0030 namespace {
0031   static const char* book_general = I18N_NOOP("General");
0032   static const char* book_publishing = I18N_NOOP("Publishing");
0033   static const char* book_classification = I18N_NOOP("Classification");
0034   static const char* book_personal = I18N_NOOP("Personal");
0035 }
0036 
0037 using Tellico::Data::BookCollection;
0038 
0039 BookCollection::BookCollection(bool addDefaultFields_, const QString& title_)
0040    : Collection(title_.isEmpty() ? i18n("My Books") : title_) {
0041   setDefaultGroupField(QStringLiteral("author"));
0042   if(addDefaultFields_) {
0043     addFields(defaultFields());
0044   }
0045 }
0046 
0047 Tellico::Data::FieldList BookCollection::defaultFields() {
0048   FieldList list;
0049   FieldPtr field;
0050 
0051   list.append(Field::createDefaultField(Field::TitleField));
0052 
0053   field = new Field(QStringLiteral("subtitle"), i18n("Subtitle"));
0054   field->setCategory(i18n(book_general));
0055   field->setFormatType(FieldFormat::FormatTitle);
0056   list.append(field);
0057 
0058   field = new Field(QStringLiteral("author"), i18n("Author"));
0059   field->setCategory(i18n(book_general));
0060   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
0061   field->setFormatType(FieldFormat::FormatName);
0062   list.append(field);
0063 
0064   field = new Field(QStringLiteral("editor"), i18n("Editor"));
0065   field->setCategory(i18n(book_general));
0066   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
0067   field->setFormatType(FieldFormat::FormatName);
0068   list.append(field);
0069 
0070   QStringList binding;
0071   binding << i18n("Hardback") << i18n("Paperback") << i18n("Trade Paperback")
0072           << i18n("E-Book") << i18n("Magazine") << i18n("Journal");
0073   field = new Field(QStringLiteral("binding"), i18n("Binding"), binding);
0074   field->setCategory(i18n(book_general));
0075   field->setFlags(Field::AllowGrouped);
0076   list.append(field);
0077 
0078   field = new Field(QStringLiteral("pur_date"), i18n("Purchase Date"));
0079   field->setCategory(i18n(book_general));
0080   field->setFormatType(FieldFormat::FormatDate);
0081   list.append(field);
0082 
0083   field = new Field(QStringLiteral("pur_price"), i18n("Purchase Price"));
0084   field->setCategory(i18n(book_general));
0085   list.append(field);
0086 
0087   field = new Field(QStringLiteral("publisher"), i18n("Publisher"));
0088   field->setCategory(i18n(book_publishing));
0089   field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
0090   field->setFormatType(FieldFormat::FormatPlain);
0091   list.append(field);
0092 
0093   field = new Field(QStringLiteral("edition"), i18n("Edition"));
0094   field->setCategory(i18n(book_publishing));
0095   field->setFlags(Field::AllowCompletion);
0096   field->setFormatType(FieldFormat::FormatPlain);
0097   list.append(field);
0098 
0099   field = new Field(QStringLiteral("cr_year"), i18n("Copyright Year"), Field::Number);
0100   field->setCategory(i18n(book_publishing));
0101   field->setFlags(Field::AllowGrouped | Field::AllowMultiple);
0102   list.append(field);
0103 
0104   field = new Field(QStringLiteral("pub_year"), i18n("Publication Year"), Field::Number);
0105   field->setCategory(i18n(book_publishing));
0106   field->setFlags(Field::AllowGrouped);
0107   list.append(field);
0108 
0109   list.append(Field::createDefaultField(Field::IsbnField));
0110 
0111   field = new Field(QStringLiteral("lccn"), i18n("LCCN#"));
0112   field->setCategory(i18n(book_publishing));
0113   field->setDescription(i18n("Library of Congress Control Number"));
0114   list.append(field);
0115 
0116   field = new Field(QStringLiteral("pages"), i18n("Pages"), Field::Number);
0117   field->setCategory(i18n(book_publishing));
0118   list.append(field);
0119 
0120   field = new Field(QStringLiteral("translator"), i18n("Translator"));
0121   field->setCategory(i18n(book_publishing));
0122   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
0123   field->setFormatType(FieldFormat::FormatName);
0124   list.append(field);
0125 
0126   field = new Field(QStringLiteral("language"), i18n("Language"));
0127   field->setCategory(i18n(book_publishing));
0128   field->setFlags(Field::AllowCompletion | Field::AllowGrouped | Field::AllowMultiple);
0129   list.append(field);
0130 
0131   field = new Field(QStringLiteral("genre"), i18n("Genre"));
0132   field->setCategory(i18n(book_classification));
0133   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
0134   list.append(field);
0135 
0136   // in document versions < 3, this was "keywords" and not "keyword"
0137   // but the title didn't change, only the name
0138   field = new Field(QStringLiteral("keyword"), i18n("Keywords"));
0139   field->setCategory(i18n(book_classification));
0140   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
0141   list.append(field);
0142 
0143   field = new Field(QStringLiteral("series"), i18n("Series"));
0144   field->setCategory(i18n(book_classification));
0145   field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
0146   list.append(field);
0147 
0148   field = new Field(QStringLiteral("series_num"), i18n("Series Number"), Field::Number);
0149   field->setCategory(i18n(book_classification));
0150   list.append(field);
0151 
0152   QStringList cond;
0153   cond << i18n("New") << i18n("Used");
0154   field = new Field(QStringLiteral("condition"), i18n("Condition"), cond);
0155   field->setCategory(i18n(book_classification));
0156   list.append(field);
0157 
0158   field = new Field(QStringLiteral("signed"), i18n("Signed"), Field::Bool);
0159   field->setCategory(i18n(book_personal));
0160   list.append(field);
0161 
0162   field = new Field(QStringLiteral("read"), i18n("Read"), Field::Bool);
0163   field->setCategory(i18n(book_personal));
0164   list.append(field);
0165 
0166   field = new Field(QStringLiteral("gift"), i18n("Gift"), Field::Bool);
0167   field->setCategory(i18n(book_personal));
0168   list.append(field);
0169 
0170   field = new Field(QStringLiteral("loaned"), i18n("Loaned"), Field::Bool);
0171   field->setCategory(i18n(book_personal));
0172   list.append(field);
0173 
0174   field = new Field(QStringLiteral("rating"), i18n("Rating"), Field::Rating);
0175   field->setCategory(i18n(book_personal));
0176   field->setFlags(Field::AllowGrouped);
0177   list.append(field);
0178 
0179   list.append(Field::createDefaultField(Field::FrontCoverField));
0180 
0181   field = new Field(QStringLiteral("plot"), i18n("Plot Summary"), Field::Para);
0182   list.append(field);
0183 
0184   field = new Field(QStringLiteral("comments"), i18n("Comments"), Field::Para);
0185   list.append(field);
0186 
0187   list.append(Field::createDefaultField(Field::IDField));
0188   list.append(Field::createDefaultField(Field::CreatedDateField));
0189   list.append(Field::createDefaultField(Field::ModifiedDateField));
0190 
0191   return list;
0192 }
0193 
0194 int BookCollection::sameEntry(Tellico::Data::EntryPtr entry1_, Tellico::Data::EntryPtr entry2_) const {
0195   if(!entry1_ || !entry2_) {
0196     return 0;
0197   }
0198   // equal isbn's or lccn's are easy
0199   if(EntryComparison::score(entry1_, entry2_, QStringLiteral("isbn"), this) > 0 ||
0200      EntryComparison::score(entry1_, entry2_, QStringLiteral("lccn"), this) > 0) {
0201     return EntryComparison::ENTRY_PERFECT_MATCH;
0202   }
0203   int res = 0;
0204   res += EntryComparison::MATCH_WEIGHT_HIGH*EntryComparison::score(entry1_, entry2_, QStringLiteral("title"), this);
0205   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
0206 
0207   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("author"), this);
0208   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
0209 
0210   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("cr_year"), this);
0211   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
0212 
0213   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("pub_year"), this);
0214   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
0215 
0216   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("publisher"), this);
0217   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("binding"), this);
0218   return res;
0219 }