File: entry.cpp

package info (click to toggle)
kiten 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 34,156 kB
  • sloc: cpp: 6,079; xml: 239; makefile: 8; sh: 2
file content (390 lines) | stat: -rw-r--r-- 11,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
    This file is part of Kiten, a KDE Japanese Reference Tool
    SPDX-FileCopyrightText: 2001 Jason Katz-Brown <jason@katzbrown.com>
    SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
    SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>
    SPDX-FileCopyrightText: 2011 Daniel E. Moctezuma <democtezuma@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "entry.h"

#include <KLocalizedString>

using namespace Qt::StringLiterals;

/**
 * The default constructor, unless you really know what you're doing,
 * THIS SHOULD NOT BE USED. For general use, other entities will need
 * to have the information provided by the other constructors
 * (particularly the sourceDictionary).
 */
Entry::Entry()
{
    init();
}

Entry::Entry(const QString &sourceDictionary)
    : sourceDict(sourceDictionary)
{
    init();
}

Entry::Entry(const QString &sourceDictionary, const QString &word, const QStringList &reading, const QStringList &meanings)
    : Word(word)
    , Meanings(meanings)
    , Readings(reading)
    , sourceDict(sourceDictionary)
{
    init();
}

Entry::Entry(const Entry &src)
    : Word(src.Word)
    , Meanings(src.Meanings)
    , Readings(src.Readings)
    , ExtendedInfo(src.ExtendedInfo)
    , sourceDict(src.sourceDict)
{
    outputListDelimiter = src.outputListDelimiter;
}

bool Entry::extendedItemCheck(const QString &key, const QString &value) const
{
    return getExtendedInfoItem(key) == value;
}

/**
 * Get the dictionary name that generated this Entry. I can't think of a reason to be changing this
 */
QString Entry::getDictName() const
{
    return sourceDict;
}

/**
 * Get the word from this Entry. If the entry is of type kanji/kana/meaning/etc, this will return
 * the kanji. If it is of kana/meaning/etc, it will return kana.
 */
QString Entry::getWord() const
{
    return Word;
}

/**
 * Get a QString containing all of the meanings known, connected by the outputListDelimiter
 */
QString Entry::getMeanings() const
{
    return Meanings.join(outputListDelimiter);
}

/**
 * Simple accessor
 */
QStringList Entry::getMeaningsList() const
{
    return Meanings;
}

/**
 * Simple accessor
 */
QString Entry::getReadings() const
{
    return Readings.join(outputListDelimiter);
}

/**
 * Simple accessor
 */
QStringList Entry::getReadingsList() const
{
    return Readings;
}

/**
 * Simple accessor
 */
QHash<QString, QString> Entry::getExtendedInfo() const
{
    return ExtendedInfo;
}

/**
 * Simple accessor
 *
 * @param x the key for the extended info item to get
 */
QString Entry::getExtendedInfoItem(const QString &x) const
{
    return ExtendedInfo[x];
}

/**
 * Prepares Meanings for output as HTML
 */
inline QString Entry::HTMLMeanings() const
{
    return QStringLiteral("<span class=\"Meanings\">%1</span>").arg(Meanings.join(outputListDelimiter));
}

/* Prepares Readings for output as HTML */
inline QString Entry::HTMLReadings() const
{
    QStringList list;
    for (const QString &it : Readings) {
        list += makeLink(it);
    }

    return QStringLiteral("<span class=\"Readings\">%1</span>").arg(list.join(outputListDelimiter));
}

/**
 * Prepares Word for output as HTML
 */
inline QString Entry::HTMLWord() const
{
    return QStringLiteral("<span class=\"Word\">%1</span>").arg(Word);
}

void Entry::init()
{
    outputListDelimiter = i18n("; ");
}

/**
 * Determines whether @param character is a kanji character.
 */
bool Entry::isKanji(const QChar &character) const
{
    ushort value = character.unicode();
    if (value < 255) {
        return false;
    }
    if (0x3040 <= value && value <= 0x30FF) {
        return false; // Kana
    }

    return true; // Note our folly here... we assuming any non-ascii/kana is kanji
}

/**
 * Returns true if all members of test are in list
 */
bool Entry::listMatch(const QStringList &list, const QStringList &test, DictQuery::MatchType type) const
{
    if (type == DictQuery::Exact) {
        for (const QString &it : test) {
            if (!list.contains(it)) {
                return false;
            }
        }
    } else if (type == DictQuery::Beginning) {
        for (const QString &it : test) {
            bool found = false;
            for (const QString &it2 : list) {
                if (it2.startsWith(it)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return false;
            }
        }
    } else if (type == DictQuery::Ending) {
        for (const QString &it : test) {
            bool found = false;
            for (const QString &it2 : list) {
                if (it2.endsWith(it)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return false;
            }
        }
    } else {
        for (const QString &it : test) {
            bool found = false;
            for (const QString &it2 : list) {
                if (it2.contains(it)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                return false;
            }
        }
    }

    return true;
}

/**
 * New functions for Entry doing direct display.
 *
 * Creates a link for the given @p entryString.
 */
inline QString Entry::makeLink(const QString &entryString) const
{
    return QStringLiteral("<a href=\"%1\">%1</a>").arg(entryString);
}

bool Entry::matchesQuery(const DictQuery &query) const
{
    if (!query.getWord().isEmpty()) {
        if (query.getMatchType() == DictQuery::Exact && this->getWord() != query.getWord()) {
            return false;
        }
        if (query.getMatchType() == DictQuery::Beginning && !this->getWord().startsWith(query.getWord())) {
            return false;
        }
        if (query.getMatchType() == DictQuery::Ending && !this->getWord().endsWith(query.getWord())) {
            return false;
        }
        if (query.getMatchType() == DictQuery::Anywhere && !this->getWord().contains(query.getWord())) {
            return false;
        }
    }

    if (!query.getPronunciation().isEmpty() && !getReadings().isEmpty()) {
        if (!listMatch(Readings, query.getPronunciation().split(DictQuery::mainDelimiter), query.getMatchType())) {
            return false;
        }
    }

    if (!query.getPronunciation().isEmpty() && getReadings().isEmpty() && !getWord().isEmpty()) {
        switch (query.getMatchType()) {
        case DictQuery::Exact:
            if (getWord() != query.getPronunciation()) {
                return false;
            }
            break;
        case DictQuery::Beginning:
            if (!getWord().startsWith(query.getPronunciation())) {
                return false;
            }
            break;
        case DictQuery::Ending:
            if (!getWord().endsWith(query.getPronunciation())) {
                return false;
            } // fallthrough
        case DictQuery::Anywhere:
            if (!getWord().contains(query.getPronunciation())) {
                return false;
            }
            break;
        }
    }

    if (!query.getMeaning().isEmpty()) {
        if (!listMatch(Meanings.join(QLatin1Char(' ')).toLower().split(' '_L1),
                       query.getMeaning().toLower().split(DictQuery::mainDelimiter),
                       query.getMatchType())) {
            return false;
        }
    }

    QList<QString> propList = query.listPropertyKeys();
    for (const QString &key : propList) {
        if (!extendedItemCheck(key, query.getProperty(key))) {
            return false;
        }
    }

    return true;
}

/**
 * Main switching function for displaying to the user
 */
QString Entry::toHTML() const
{
    return QStringLiteral("<div class=\"Entry\">%1%2%3</div>").arg(HTMLWord()).arg(HTMLReadings()).arg(HTMLMeanings());
}

inline QString Entry::toKVTML() const
{
    /*
<e m="1" s="1">
       <o width="414" l="en" q="t">(eh,) excuse me</o>
       <t width="417" l="jp" q="o">(あのう、) すみません </t>
</e>
*/
    // TODO: en should not necessarily be the language here.
    return QStringLiteral(
               "<e>\n<o l=\"en\">%1</o>\n"
               "<t l=\"jp-kanji\">%2</t>\n"
               "<t l=\"jp-kana\">%3</t></e>\n\n")
        .arg(getMeanings())
        .arg(getWord())
        .arg(getReadings());
}

/**
 * This method should return the entry object in a simple QString format
 * Brief form should be usable in quick summaries, for example
 * Verbose form might be used to save a complete list of entries to a file, for example.
 */
QString Entry::toString() const
{
    return QStringLiteral("%1 (%2) %3").arg(Word).arg(getReadings()).arg(getMeanings());
}

/**
 * This version of sort only sorts dictionaries...
 * This is a replacement for an operator\< function... so we return true if
 * "this" should show up first on the list.
 */
bool Entry::sort(const Entry &that, const QStringList &dictOrder, const QStringList &fields) const
{
    if (this->sourceDict != that.sourceDict) {
        for (const QString &dict : dictOrder) {
            if (dict == that.sourceDict) {
                return false;
            }
            if (dict == this->sourceDict) {
                return true;
            }
        }
    } else {
        for (const QString &field : fields) {
            if (field == QLatin1String("Word/Kanji")) {
                return this->getWord() < that.getWord();
            } else if (field == QLatin1String("Meaning")) {
                return listMatch(that.getMeaningsList(), this->getMeaningsList(), DictQuery::Exact)
                    && (that.getMeaningsList().count() != this->getMeaningsList().count());
            } else if (field == QLatin1String("Reading")) {
                return listMatch(that.getReadingsList(), this->getReadingsList(), DictQuery::Exact)
                    && (that.getReadingsList().count() != this->getReadingsList().count());
            } else {
                const QString thisOne = this->getExtendedInfoItem(field);
                const QString thatOne = that.getExtendedInfoItem(field);
                // Only sort by this field if the values differ, otherwise move to the next field
                if (thisOne != thatOne) {
                    // If the second item does not have this field, sort this one first
                    if (thatOne.isEmpty()) {
                        return true;
                    }
                    // If we don't have this field, sort "this" to second
                    if (thisOne.isEmpty()) {
                        return false;
                    }
                    // Otherwise, send it to a virtual function (to allow dictionaries to override sorting)
                    return this->sortByField(that, field);
                }
            }
        }
    }
    return false; // If we reach here, they match as much as possible
}

bool Entry::sortByField(const Entry &that, const QString &field) const
{
    return this->getExtendedInfoItem(field) < that.getExtendedInfoItem(field);
}