1 /***************************************************************************
2 Copyright (C) 2003-2009 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 "bookcollection.h"
26 #include "../entrycomparison.h"
27
28 #include <KLocalizedString>
29
30 namespace {
31 static const char* book_general = I18N_NOOP("General");
32 static const char* book_publishing = I18N_NOOP("Publishing");
33 static const char* book_classification = I18N_NOOP("Classification");
34 static const char* book_personal = I18N_NOOP("Personal");
35 }
36
37 using Tellico::Data::BookCollection;
38
BookCollection(bool addDefaultFields_,const QString & title_)39 BookCollection::BookCollection(bool addDefaultFields_, const QString& title_)
40 : Collection(title_.isEmpty() ? i18n("My Books") : title_) {
41 setDefaultGroupField(QStringLiteral("author"));
42 if(addDefaultFields_) {
43 addFields(defaultFields());
44 }
45 }
46
defaultFields()47 Tellico::Data::FieldList BookCollection::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(book_general));
55 field->setFormatType(FieldFormat::FormatTitle);
56 list.append(field);
57
58 field = new Field(QStringLiteral("author"), i18n("Author"));
59 field->setCategory(i18n(book_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("editor"), i18n("Editor"));
65 field->setCategory(i18n(book_general));
66 field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
67 field->setFormatType(FieldFormat::FormatName);
68 list.append(field);
69
70 QStringList binding;
71 binding << i18n("Hardback") << i18n("Paperback") << i18n("Trade Paperback")
72 << i18n("E-Book") << i18n("Magazine") << i18n("Journal");
73 field = new Field(QStringLiteral("binding"), i18n("Binding"), binding);
74 field->setCategory(i18n(book_general));
75 field->setFlags(Field::AllowGrouped);
76 list.append(field);
77
78 field = new Field(QStringLiteral("pur_date"), i18n("Purchase Date"));
79 field->setCategory(i18n(book_general));
80 field->setFormatType(FieldFormat::FormatDate);
81 list.append(field);
82
83 field = new Field(QStringLiteral("pur_price"), i18n("Purchase Price"));
84 field->setCategory(i18n(book_general));
85 list.append(field);
86
87 field = new Field(QStringLiteral("publisher"), i18n("Publisher"));
88 field->setCategory(i18n(book_publishing));
89 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
90 field->setFormatType(FieldFormat::FormatPlain);
91 list.append(field);
92
93 field = new Field(QStringLiteral("edition"), i18n("Edition"));
94 field->setCategory(i18n(book_publishing));
95 field->setFlags(Field::AllowCompletion);
96 field->setFormatType(FieldFormat::FormatPlain);
97 list.append(field);
98
99 field = new Field(QStringLiteral("cr_year"), i18n("Copyright Year"), Field::Number);
100 field->setCategory(i18n(book_publishing));
101 field->setFlags(Field::AllowGrouped | Field::AllowMultiple);
102 list.append(field);
103
104 field = new Field(QStringLiteral("pub_year"), i18n("Publication Year"), Field::Number);
105 field->setCategory(i18n(book_publishing));
106 field->setFlags(Field::AllowGrouped);
107 list.append(field);
108
109 list.append(Field::createDefaultField(Field::IsbnField));
110
111 field = new Field(QStringLiteral("lccn"), i18n("LCCN#"));
112 field->setCategory(i18n(book_publishing));
113 field->setDescription(i18n("Library of Congress Control Number"));
114 list.append(field);
115
116 field = new Field(QStringLiteral("pages"), i18n("Pages"), Field::Number);
117 field->setCategory(i18n(book_publishing));
118 list.append(field);
119
120 field = new Field(QStringLiteral("translator"), i18n("Translator"));
121 field->setCategory(i18n(book_publishing));
122 field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
123 field->setFormatType(FieldFormat::FormatName);
124 list.append(field);
125
126 field = new Field(QStringLiteral("language"), i18n("Language"));
127 field->setCategory(i18n(book_publishing));
128 field->setFlags(Field::AllowCompletion | Field::AllowGrouped | Field::AllowMultiple);
129 list.append(field);
130
131 field = new Field(QStringLiteral("genre"), i18n("Genre"));
132 field->setCategory(i18n(book_classification));
133 field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
134 list.append(field);
135
136 // in document versions < 3, this was "keywords" and not "keyword"
137 // but the title didn't change, only the name
138 field = new Field(QStringLiteral("keyword"), i18n("Keywords"));
139 field->setCategory(i18n(book_classification));
140 field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
141 list.append(field);
142
143 field = new Field(QStringLiteral("series"), i18n("Series"));
144 field->setCategory(i18n(book_classification));
145 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
146 list.append(field);
147
148 field = new Field(QStringLiteral("series_num"), i18n("Series Number"), Field::Number);
149 field->setCategory(i18n(book_classification));
150 list.append(field);
151
152 QStringList cond;
153 cond << i18n("New") << i18n("Used");
154 field = new Field(QStringLiteral("condition"), i18n("Condition"), cond);
155 field->setCategory(i18n(book_classification));
156 list.append(field);
157
158 field = new Field(QStringLiteral("signed"), i18n("Signed"), Field::Bool);
159 field->setCategory(i18n(book_personal));
160 list.append(field);
161
162 field = new Field(QStringLiteral("read"), i18n("Read"), Field::Bool);
163 field->setCategory(i18n(book_personal));
164 list.append(field);
165
166 field = new Field(QStringLiteral("gift"), i18n("Gift"), Field::Bool);
167 field->setCategory(i18n(book_personal));
168 list.append(field);
169
170 field = new Field(QStringLiteral("loaned"), i18n("Loaned"), Field::Bool);
171 field->setCategory(i18n(book_personal));
172 list.append(field);
173
174 field = new Field(QStringLiteral("rating"), i18n("Rating"), Field::Rating);
175 field->setCategory(i18n(book_personal));
176 field->setFlags(Field::AllowGrouped);
177 list.append(field);
178
179 field = new Field(QStringLiteral("cover"), i18n("Front Cover"), Field::Image);
180 list.append(field);
181
182 field = new Field(QStringLiteral("plot"), i18n("Plot Summary"), Field::Para);
183 list.append(field);
184
185 field = new Field(QStringLiteral("comments"), i18n("Comments"), Field::Para);
186 list.append(field);
187
188 list.append(Field::createDefaultField(Field::IDField));
189 list.append(Field::createDefaultField(Field::CreatedDateField));
190 list.append(Field::createDefaultField(Field::ModifiedDateField));
191
192 return list;
193 }
194
sameEntry(Tellico::Data::EntryPtr entry1_,Tellico::Data::EntryPtr entry2_) const195 int BookCollection::sameEntry(Tellico::Data::EntryPtr entry1_, Tellico::Data::EntryPtr entry2_) const {
196 if(!entry1_ || !entry2_) {
197 return 0;
198 }
199 // equal isbn's or lccn's are easy
200 if(EntryComparison::score(entry1_, entry2_, QStringLiteral("isbn"), this) > 0 ||
201 EntryComparison::score(entry1_, entry2_, QStringLiteral("lccn"), this) > 0) {
202 return EntryComparison::ENTRY_PERFECT_MATCH;
203 }
204 int res = 0;
205 res += EntryComparison::MATCH_WEIGHT_HIGH*EntryComparison::score(entry1_, entry2_, QStringLiteral("title"), this);
206 if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
207
208 res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("author"), this);
209 if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
210
211 res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("cr_year"), this);
212 if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
213
214 res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("pub_year"), this);
215 if(res >= EntryComparison::ENTRY_PERFECT_MATCH) return res;
216
217 res += EntryComparison::MATCH_WEIGHT_LOW *EntryComparison::score(entry1_, entry2_, QStringLiteral("binding"), this);
218 return res;
219 }
220