1 /***************************************************************************
2 Copyright (C) 2005-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 "filecatalog.h"
26 #include "../entrycomparison.h"
27
28 #include <KLocalizedString>
29
30 namespace {
31 static const char* file_general = I18N_NOOP("General");
32 }
33
34 using Tellico::Data::FileCatalog;
35
FileCatalog(bool addDefaultFields_,const QString & title_)36 FileCatalog::FileCatalog(bool addDefaultFields_, const QString& title_)
37 : Collection(title_.isEmpty() ? i18n("My Files") : title_) {
38 setDefaultGroupField(QStringLiteral("volume"));
39 if(addDefaultFields_) {
40 addFields(defaultFields());
41 }
42 }
43
defaultFields()44 Tellico::Data::FieldList FileCatalog::defaultFields() {
45 FieldList list;
46 FieldPtr field;
47
48 field = Field::createDefaultField(Field::TitleField);
49 field->setTitle(i18n("Name"));
50 field->setFormatType(FieldFormat::FormatNone);
51 list.append(field);
52
53 field = new Field(QStringLiteral("url"), i18n("URL"), Field::URL);
54 field->setCategory(i18n(file_general));
55 list.append(field);
56
57 field = new Field(QStringLiteral("description"), i18n("Description"));
58 field->setCategory(i18n(file_general));
59 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
60 list.append(field);
61
62 field = new Field(QStringLiteral("volume"), i18nc("File catalog", "Volume"));
63 field->setCategory(i18n(file_general));
64 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
65 list.append(field);
66
67 field = new Field(QStringLiteral("folder"), i18n("Folder"));
68 field->setCategory(i18n(file_general));
69 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
70 list.append(field);
71
72 field = new Field(QStringLiteral("mimetype"), i18n("Mimetype"));
73 field->setCategory(i18n(file_general));
74 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
75 list.append(field);
76
77 field = new Field(QStringLiteral("size"), i18n("Size"));
78 field->setCategory(i18n(file_general));
79 list.append(field);
80
81 field = new Field(QStringLiteral("permissions"), i18n("Permissions"));
82 field->setCategory(i18n(file_general));
83 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
84 list.append(field);
85
86 field = new Field(QStringLiteral("owner"), i18n("Owner"));
87 field->setCategory(i18n(file_general));
88 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
89 list.append(field);
90
91 field = new Field(QStringLiteral("group"), i18n("Group"));
92 field->setCategory(i18n(file_general));
93 field->setFlags(Field::AllowCompletion | Field::AllowGrouped);
94 list.append(field);
95
96 // these dates are string fields, not dates, since the time is included
97 field = new Field(QStringLiteral("created"), i18n("Created"));
98 field->setCategory(i18n(file_general));
99 list.append(field);
100
101 field = new Field(QStringLiteral("modified"), i18n("Modified"));
102 field->setCategory(i18n(file_general));
103 list.append(field);
104
105 field = new Field(QStringLiteral("metainfo"), i18n("Meta Info"), Field::Table);
106 field->setProperty(QStringLiteral("columns"), QStringLiteral("2"));
107 field->setProperty(QStringLiteral("column1"), i18n("Property"));
108 field->setProperty(QStringLiteral("column2"), i18n("Value"));
109 list.append(field);
110
111 field = new Field(QStringLiteral("icon"), i18n("Icon"), Field::Image);
112 list.append(field);
113
114 return list;
115 }
116
sameEntry(Tellico::Data::EntryPtr entry1_,Tellico::Data::EntryPtr entry2_) const117 int FileCatalog::sameEntry(Tellico::Data::EntryPtr entry1_, Tellico::Data::EntryPtr entry2_) const {
118 // equal urls are always equal, even if modification time or something is different
119 if(EntryComparison::score(entry1_, entry2_, QStringLiteral("url"), this) > 0) {
120 return EntryComparison::ENTRY_PERFECT_MATCH;
121 }
122 // if volume or created time is different, it can't be same entry
123 if(EntryComparison::score(entry1_, entry2_, QStringLiteral("volume"), this) == 0 ||
124 EntryComparison::score(entry1_, entry2_, QStringLiteral("created"), this) == 0 ||
125 EntryComparison::score(entry1_, entry2_, QStringLiteral("size"), this) == 0) {
126 return EntryComparison::ENTRY_BAD_MATCH;
127 }
128 int res = 0;
129 res += EntryComparison::MATCH_WEIGHT_LOW*EntryComparison::score(entry1_, entry2_, QStringLiteral("title"), this);
130 res += EntryComparison::MATCH_WEIGHT_LOW*EntryComparison::score(entry1_, entry2_, QStringLiteral("description"), this);
131 res += EntryComparison::MATCH_WEIGHT_LOW*EntryComparison::score(entry1_, entry2_, QStringLiteral("mimetype"), this);
132 return res;
133 }
134