1 /***************************************************************************
2     Copyright (C) 2003-2016 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #include "comicbookcollection.h"
26 #include "../entrycomparison.h"
27 
28 #include <KLocalizedString>
29 
30 namespace {
31   static const char* comic_general = I18N_NOOP("General");
32   static const char* comic_publishing = I18N_NOOP("Publishing");
33   static const char* comic_classification = I18N_NOOP("Classification");
34   static const char* comic_personal = I18N_NOOP("Personal");
35 }
36 
37 using Tellico::Data::ComicBookCollection;
38 
ComicBookCollection(bool addDefaultFields_,const QString & title_)39 ComicBookCollection::ComicBookCollection(bool addDefaultFields_, const QString& title_)
40    : Collection(title_.isEmpty() ? i18n("My Comic Books") : title_) {
41   setDefaultGroupField(QStringLiteral("series"));
42   if(addDefaultFields_) {
43     addFields(defaultFields());
44   }
45 }
46 
defaultFields()47 Tellico::Data::FieldList ComicBookCollection::defaultFields() {
48   FieldList list;
49   FieldPtr field;
50 
51   list.append(Field::createDefaultField(Field::TitleField));
52 
53   field = new Field(QStringLiteral("subtitle"), i18n("Subtitle"));
54   field->setCategory(i18n(comic_general));
55   field->setFormatType(FieldFormat::FormatTitle);
56   list.append(field);
57 
58   field = new Field(QStringLiteral("writer"), i18n("Writer"));
59   field->setCategory(i18n(comic_general));
60   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
61   field->setFormatType(FieldFormat::FormatName);
62   list.append(field);
63 
64   field = new Field(QStringLiteral("artist"), i18nc("Comic Book Illustrator", "Artist"));
65   field->setCategory(i18n(comic_general));
66   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
67   field->setFormatType(FieldFormat::FormatName);
68   list.append(field);
69 
70   field = new Field(QStringLiteral("series"), i18n("Series"));
71   field->setCategory(i18n(comic_general));
72   field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
73   field->setFormatType(FieldFormat::FormatTitle);
74   list.append(field);
75 
76   field = new Field(QStringLiteral("issue"), i18n("Issue"), Field::Number);
77   field->setCategory(i18n(comic_general));
78   field->setFlags(Field::AllowMultiple);
79   list.append(field);
80 
81   field = new Field(QStringLiteral("publisher"), i18n("Publisher"));
82   field->setCategory(i18n(comic_publishing));
83   field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
84   field->setFormatType(FieldFormat::FormatPlain);
85   list.append(field);
86 
87   field = new Field(QStringLiteral("edition"), i18n("Edition"));
88   field->setCategory(i18n(comic_publishing));
89   field->setFlags(Field::AllowCompletion);
90   field->setFormatType(FieldFormat::FormatPlain);
91   list.append(field);
92 
93   field = new Field(QStringLiteral("pub_year"), i18n("Publication Year"),  Field::Number);
94   field->setCategory(i18n(comic_publishing));
95   field->setFlags(Field::AllowGrouped);
96   list.append(field);
97 
98   field = new Field(QStringLiteral("pages"), i18n("Pages"), Field::Number);
99   field->setCategory(i18n(comic_publishing));
100   list.append(field);
101 
102   field = new Field(QStringLiteral("country"), i18n("Country"));
103   field->setCategory(i18n(comic_publishing));
104   field->setFlags(Field::AllowCompletion | Field::AllowGrouped | Field::AllowMultiple);
105   field->setFormatType(FieldFormat::FormatPlain);
106   list.append(field);
107 
108   field = new Field(QStringLiteral("language"), i18n("Language"));
109   field->setCategory(i18n(comic_publishing));
110   field->setFlags(Field::AllowCompletion | Field::AllowGrouped | Field::AllowMultiple);
111   field->setFormatType(FieldFormat::FormatPlain);
112   list.append(field);
113 
114   field = new Field(QStringLiteral("genre"), i18n("Genre"));
115   field->setCategory(i18n(comic_classification));
116   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
117   field->setFormatType(FieldFormat::FormatPlain);
118   list.append(field);
119 
120   field = new Field(QStringLiteral("keyword"), i18n("Keywords"));
121   field->setCategory(i18n(comic_classification));
122   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
123   list.append(field);
124 
125   QStringList cond = i18nc("Comic book grade levels - "
126                            "Mint,Near Mint,Very Fine,Fine,Very Good,Good,Fair,Poor",
127                            "Mint,Near Mint,Very Fine,Fine,Very Good,Good,Fair,Poor")
128 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
129                      .split(QRegularExpression(QLatin1String("\\s*,\\s*")), QString::SkipEmptyParts);
130 #else
131                      .split(QRegularExpression(QLatin1String("\\s*,\\s*")), Qt::SkipEmptyParts);
132 #endif
133   field = new Field(QStringLiteral("condition"), i18n("Condition"), cond);
134   field->setCategory(i18n(comic_classification));
135   list.append(field);
136 
137   field = new Field(QStringLiteral("pur_date"), i18n("Purchase Date"));
138   field->setCategory(i18n(comic_personal));
139   field->setFormatType(FieldFormat::FormatDate);
140   list.append(field);
141 
142   field = new Field(QStringLiteral("pur_price"), i18n("Purchase Price"));
143   field->setCategory(i18n(comic_personal));
144   list.append(field);
145 
146   field = new Field(QStringLiteral("signed"), i18n("Signed"), Field::Bool);
147   field->setCategory(i18n(comic_personal));
148   list.append(field);
149 
150   field = new Field(QStringLiteral("gift"), i18n("Gift"), Field::Bool);
151   field->setCategory(i18n(comic_personal));
152   list.append(field);
153 
154   field = new Field(QStringLiteral("loaned"), i18n("Loaned"), Field::Bool);
155   field->setCategory(i18n(comic_personal));
156   list.append(field);
157 
158   field = new Field(QStringLiteral("rating"), i18n("Rating"), Field::Rating);
159   field->setCategory(i18n(comic_personal));
160   field->setFlags(Field::AllowGrouped);
161   list.append(field);
162 
163   field = new Field(QStringLiteral("cover"), i18n("Front Cover"), Field::Image);
164   list.append(field);
165 
166   field = new Field(QStringLiteral("plot"), i18n("Plot Summary"), Field::Para);
167   list.append(field);
168 
169   field = new Field(QStringLiteral("comments"), i18n("Comments"), Field::Para);
170   list.append(field);
171 
172   list.append(Field::createDefaultField(Field::IDField));
173   list.append(Field::createDefaultField(Field::CreatedDateField));
174   list.append(Field::createDefaultField(Field::ModifiedDateField));
175 
176   return list;
177 }
178 
sameEntry(Tellico::Data::EntryPtr entry1_,Tellico::Data::EntryPtr entry2_) const179 int ComicBookCollection::sameEntry(Tellico::Data::EntryPtr entry1_, Tellico::Data::EntryPtr entry2_) const {
180   if(!entry1_ || !entry2_) {
181     return 0;
182   }
183   // equal isbn's or lccn's are easy, give it a weight of 100
184   if(EntryComparison::score(entry1_, entry2_, QStringLiteral("isbn"), this) > 0 ||
185      EntryComparison::score(entry1_, entry2_, QStringLiteral("lccn"), this) > 0) {
186     return EntryComparison::ENTRY_PERFECT_MATCH;
187   }
188   // special for Bedetheque links for match
189   if(hasField(QStringLiteral("lien-bel")) && EntryComparison::score(entry1_, entry2_, QStringLiteral("lien-bel"), this) > 0) {
190     return EntryComparison::ENTRY_PERFECT_MATCH;
191   }
192   int res = 0;
193   res += EntryComparison::MATCH_WEIGHT_HIGH*EntryComparison::score(entry1_, entry2_, QStringLiteral("title"), this);
194   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
195 
196   res += EntryComparison::MATCH_WEIGHT_MED *EntryComparison::score(entry1_, entry2_, QStringLiteral("series"), this);
197   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
198 
199   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("pub_year"), this);
200   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
201 
202   res += EntryComparison::MATCH_WEIGHT_MED *EntryComparison::score(entry1_, entry2_, QStringLiteral("writer"), this);
203   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
204 
205   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("artist"), this);
206   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
207 
208   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("issue"), this);
209   if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
210 
211   res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("publisher"), this);
212   return res;
213 }
214