1 /*
2  * C++ Implementation: keduvockvtml1compability_p
3  * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
4  * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 #include "keduvockvtmlcompability.h"
7 #include "keduvocwordtype.h"
8 
9 #include <KLocalizedString>
10 
11 #include <QDebug>
12 
13 const QString KEduVocKvtmlCompability::KVTML_1_USER_DEFINED = QStringLiteral( "#" );
14 const QString KEduVocKvtmlCompability::KVTML_1_SEPERATOR = QStringLiteral( ":" );
15 
16 
KEduVocKvtmlCompability()17 KEduVocKvtmlCompability::KEduVocKvtmlCompability()
18 {
19     m_userdefinedTenseCounter = 0;
20     m_userdefinedTypeCounter = 0;
21 
22     initOldTypeLists();
23     initOldTenses();
24 }
25 
26 
27 ////////////////// TYPES /////////////////////////////////////////
initOldTypeLists()28 void KEduVocKvtmlCompability::initOldTypeLists()
29 {
30     m_oldMainTypeNames.clear();
31     m_oldMainTypeNames.insert( QStringLiteral("v"), i18nc( "@item:inlistbox The grammatical type of a word", "Verb" ) );
32     m_oldMainTypeNames.insert( QStringLiteral("n"), i18nc( "@item:inlistbox The grammatical type of a word", "Noun" ) );
33     m_oldMainTypeNames.insert( QStringLiteral("nm"), i18nc( "@item:inlistbox The grammatical type of a word", "Name" ) );
34     m_oldMainTypeNames.insert( QStringLiteral("ar"), i18nc( "@item:inlistbox The grammatical type of a word", "Article" ) );
35     m_oldMainTypeNames.insert( QStringLiteral("aj"), i18nc( "@item:inlistbox The grammatical type of a word", "Adjective" ) );
36     m_oldMainTypeNames.insert( QStringLiteral("av"), i18nc( "@item:inlistbox The grammatical type of a word", "Adverb" ) );
37     m_oldMainTypeNames.insert( QStringLiteral("pr"), i18nc( "@item:inlistbox The grammatical type of a word", "Pronoun" ) );
38     m_oldMainTypeNames.insert( QStringLiteral("ph"), i18nc( "@item:inlistbox The grammatical type of an entry", "Phrase" ) );
39     m_oldMainTypeNames.insert( QStringLiteral("num"), i18nc( "@item:inlistbox The grammatical type of a word", "Numeral" ) );
40     m_oldMainTypeNames.insert( QStringLiteral("con"), i18nc( "@item:inlistbox The grammatical type of a word", "Conjunction" ) );
41     m_oldMainTypeNames.insert( QStringLiteral("pre"), i18nc( "@item:inlistbox The grammatical type of a word", "Preposition" ) );
42     m_oldMainTypeNames.insert( QStringLiteral("qu"), i18nc( "@item:inlistbox The grammatical type of an entry", "Question" ) );
43 
44 
45     m_oldSubTypeNames.clear();
46     m_oldSubTypeNames.insert( QStringLiteral("ord"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal  (first, second, third, ...)","Ordinal" ) );
47     m_oldSubTypeNames.insert( QStringLiteral("crd"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (one, two, three, ...)","Cardinal" ) );
48     m_oldSubTypeNames.insert( QStringLiteral("def"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (the)","Definite" ) );
49     m_oldSubTypeNames.insert( QStringLiteral("ind"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (a)","Indefinite" ) );
50     m_oldSubTypeNames.insert( QStringLiteral("re"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation","Regular" ) );
51     m_oldSubTypeNames.insert( QStringLiteral("ir"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation","Irregular" ) );
52     m_oldSubTypeNames.insert( QStringLiteral("pos"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive" ) );
53     m_oldSubTypeNames.insert( QStringLiteral("per"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal" ) );
54     m_oldSubTypeNames.insert( QStringLiteral("m"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Male" ) );
55     m_oldSubTypeNames.insert( QStringLiteral("f"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Female" ) );
56     m_oldSubTypeNames.insert( QStringLiteral("s"), i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral" ) );
57 }
58 
59 
typeFromOldFormat(KEduVocWordType * parent,const QString & typeSubtypeString) const60 KEduVocWordType* KEduVocKvtmlCompability::typeFromOldFormat(KEduVocWordType* parent, const QString & typeSubtypeString ) const
61 {
62     // check if it's user defined
63     if ( typeSubtypeString.length() >= 2 && typeSubtypeString.left( 1 ) == QM_USER_TYPE ) {
64         // they started counting at 1, we need to know which index we are dealing with:
65         int selfDefinedTypeIndex = typeSubtypeString.rightRef( typeSubtypeString.count()-1 ).toInt() -1;
66         return static_cast<KEduVocWordType*>(parent->childContainer(selfDefinedTypeIndex));
67     }
68 
69     // assume the parent is set up to contain the old types correctly
70     QString mainType;
71     QString subType;
72     int i;
73 
74     if (( i = typeSubtypeString.indexOf( KVTML_1_SEPERATOR ) ) >= 0 ) {
75         mainType = typeSubtypeString.left( i );
76         subType = typeSubtypeString.right( i+1 );
77     } else {
78         mainType = typeSubtypeString;
79     }
80 
81     // convert from pre-0.5 versions (I guess we can just leave that in here.
82     // I seriously doubt that any such documents exist...
83     if ( mainType == QLatin1Char('1') ) {
84         mainType = QM_VERB;
85     } else if ( mainType == QLatin1Char('2') ) {
86         mainType = QM_NOUN;
87     } else if ( mainType == QLatin1Char('3') ) {
88         mainType = QM_NAME;
89     }
90 
91     QString typeName = m_oldMainTypeNames.value( mainType );
92     if ( typeName.isEmpty() ) {
93         qDebug() << "Unknown old maintype: " << typeSubtypeString;
94         return 0;
95     }
96 
97     QString subTypeName = m_oldSubTypeNames.value( subType );
98 
99     foreach (KEduVocContainer* wordType, parent->childContainers()) {
100         if (wordType->name() == typeName) {
101             if (subType.isEmpty()) {
102                 return static_cast<KEduVocWordType*>(wordType);
103             } else {
104                 foreach (KEduVocContainer* subWordType, wordType->childContainers()) {
105                     if (subWordType->name() == subTypeName) {
106                         return static_cast<KEduVocWordType*>(subWordType);
107                     }
108                 }
109             }
110         }
111     }
112 
113     return 0;
114 }
115 
116 
117 
118 
119 
120 
121 /*
122 if ( type.length() >= 2 && type.left( 1 ) == QM_USER_TYPE ) {
123                 // they started counting at 1, we need to know which index we are dealing with:
124                 int selfDefinedTypeIndex = type.right( type.count()-1 ).toInt() -1;
125 
126                 // append invented types (do we not trust our own writer?)
127                 if ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
128                     while ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
129                         m_oldSelfDefinedTypes.append( i18n( "User defined word type %1", m_oldSelfDefinedTypes.count() - 1 ) );
130                     }
131                 }
132                 type = m_oldSelfDefinedTypes.value( selfDefinedTypeIndex );
133             } else {
134                 type = m_compability.mainTypeFromOldFormat( oldType );
135                 subType = m_compability.subTypeFromOldFormat( oldType );
136             } // not user defined - preset types
137 
138 
139 if ( oldType.length() >= 2 && type.left( 1 ) == QM_USER_TYPE ) {
140             // they started counting at 1
141             int selfDefinedTypeIndex = oldType.right( type.count()-1 ).toInt() -1;
142             // append invented types (do we not trust our own writer?)
143             if ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
144                 while ( selfDefinedTypeIndex >= m_oldSelfDefinedTypes.count() ) {
145                     m_oldSelfDefinedTypes.append( i18n( "User defined word type %1", m_oldSelfDefinedTypes.count() - 1 ) );
146                 }
147             }
148             type = m_oldSelfDefinedTypes.value( selfDefinedTypeIndex );
149         } else {
150             type = m_compability.mainTypeFromOldFormat( oldType );
151             subType = m_compability.subTypeFromOldFormat( oldType );
152         } // not user defined - preset types
153     }
154 */
155 
156 
initOldTenses()157 void KEduVocKvtmlCompability::initOldTenses()
158 {
159     m_oldTenses[QStringLiteral("PrSi")] = i18n( "Simple Present" );
160     m_oldTenses[QStringLiteral("PrPr")] = i18n( "Present Progressive" );
161     m_oldTenses[QStringLiteral("PrPe")] = i18n( "Present Perfect" );
162     m_oldTenses[QStringLiteral("PaSi")] = i18n( "Simple Past" );
163     m_oldTenses[QStringLiteral("PaPr")] = i18n( "Past Progressive" );
164     m_oldTenses[QStringLiteral("PaPa")] = i18n( "Past Participle" );
165     m_oldTenses[QStringLiteral("FuSi")] = i18n( "Future" );
166 }
167 
168 
addUserdefinedTense(const QString & tense)169 void KEduVocKvtmlCompability::addUserdefinedTense(const QString & tense)
170 {
171     m_userdefinedTenseCounter++;
172     m_oldTenses[KVTML_1_USER_DEFINED + QString::number( m_userdefinedTenseCounter )] = tense;
173     m_tenses.insert(tense);
174 
175     qDebug() << " Add tense: " << KVTML_1_USER_DEFINED + QString::number( m_userdefinedTenseCounter ) << " - " << tense;
176 }
177 
178 
tenseFromKvtml1(const QString & oldTense)179 QString KEduVocKvtmlCompability::tenseFromKvtml1(const QString & oldTense)
180 {
181     // in case the document got chaged, at least make up something as tense
182     if (!m_oldTenses.keys().contains(oldTense)) {
183         m_oldTenses[oldTense] = oldTense;
184         qDebug() << "Warning, tense " << oldTense << " not found in document!";
185     }
186     m_tenses.insert(m_oldTenses.value(oldTense));
187     return m_oldTenses.value(oldTense);
188 }
189 
190 
documentTenses() const191 QStringList KEduVocKvtmlCompability::documentTenses() const
192 {
193     return m_tenses.values();
194 }
195 
196 
oldTense(const QString & tense)197 QString KEduVocKvtmlCompability::oldTense(const QString & tense)
198 {
199 ///@todo writing of the user defined tenses is probably messed up
200     if ( !m_oldTenses.values().contains(tense) ) {
201         m_userdefinedTenseCounter++;
202         m_oldTenses[KVTML_1_USER_DEFINED + QString::number( m_userdefinedTenseCounter )] = tense;
203     }
204     return m_oldTenses.key(tense);
205 }
206 
setupWordTypes(KEduVocWordType * parent)207 void KEduVocKvtmlCompability::setupWordTypes(KEduVocWordType * parent)
208 {
209     QStringList wordTypeNames;
210     wordTypeNames
211         << i18nc( "The grammatical type of a word", "Verb" ) // 0
212         << i18nc( "The grammatical type of a word", "Noun" ) // 1
213         << i18nc( "The grammatical type of a word", "Name" )
214         << i18nc( "The grammatical type of a word", "Article" ) // 3
215         << i18nc( "The grammatical type of a word", "Adjective" ) // 4
216         << i18nc( "The grammatical type of a word", "Adverb" ) // 5
217         << i18nc( "The grammatical type of a word", "Pronoun" ) // 6
218         << i18nc( "The grammatical type of an entry", "Phrase" )
219         << i18nc( "The grammatical type of a word", "Numeral" ) // 8
220         << i18nc( "The grammatical type of a word", "Conjunction" )
221         << i18nc( "The grammatical type of a word", "Preposition" )
222         << i18nc( "The grammatical type of an entry", "Question" );
223 
224     foreach (const QString &typeName, wordTypeNames) {
225         KEduVocWordType* wordType = new KEduVocWordType(typeName, parent);
226         parent->appendChildContainer(wordType);
227         m_userdefinedTypeCounter++;
228     }
229     static_cast<KEduVocWordType*>(parent->childContainer(4))->setWordType(KEduVocWordFlag::Adjective);
230     static_cast<KEduVocWordType*>(parent->childContainer(5))->setWordType(KEduVocWordFlag::Adverb);
231 
232     KEduVocWordType* numeral = static_cast<KEduVocWordType*>(parent->childContainer(8));
233     KEduVocWordType* wordType = new KEduVocWordType(
234         i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Ordinal  (first, second, third, ...)","Ordinal" ), numeral);
235     wordType->setWordType(KEduVocWordFlag::Adjective);
236     numeral->appendChildContainer(wordType);
237     wordType = new KEduVocWordType(
238         i18nc( "@item:inlistbox A subtype of the grammatical word type: Numeral Cardinal (one, two, three, ...)","Cardinal" ), numeral);
239 
240     wordType->setWordType(KEduVocWordFlag::Adjective);
241     numeral->appendChildContainer(wordType);
242 
243     KEduVocWordType* article = static_cast<KEduVocWordType*>(parent->childContainer(3));
244     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (the)","Definite" ), article);
245     wordType->setWordType(KEduVocWordFlag::Article | KEduVocWordFlag::Definite);
246     article->appendChildContainer(wordType);
247     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Article (a)","Indefinite" ), article);
248     wordType->setWordType(KEduVocWordFlag::Article | KEduVocWordFlag::Indefinite);
249     article->appendChildContainer(wordType);
250 
251     KEduVocWordType* verb = static_cast<KEduVocWordType*>(parent->childContainer(0));
252     verb->setWordType(KEduVocWordFlag::Verb);
253     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with regular conjugation","Regular" ), verb);
254     wordType->setWordType(KEduVocWordFlag::Verb | KEduVocWordFlag::Regular);
255     verb->appendChildContainer(wordType);
256     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Verb with irregular conjugation","Irregular" ), verb);
257     verb->appendChildContainer(wordType);
258     wordType->setWordType(KEduVocWordFlag::Verb | KEduVocWordFlag::Irregular);
259 
260     KEduVocWordType* noun = static_cast<KEduVocWordType*>(parent->childContainer(1));
261     noun->setWordType(KEduVocWordFlag::Noun);
262     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Male" ), noun);
263     noun->appendChildContainer(wordType);
264     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
265     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Female" ), noun);
266     noun->appendChildContainer(wordType);
267     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Feminine);
268     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Noun", "Neutral" ), noun);
269     noun->appendChildContainer(wordType);
270     wordType->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Neuter);
271 
272 
273     KEduVocWordType* pronoun = static_cast<KEduVocWordType*>(parent->childContainer(6));
274     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (my, your, his, her...)", "Possessive" ), pronoun);
275     wordType->setWordType(KEduVocWordFlag::Pronoun);
276     pronoun->appendChildContainer(wordType);
277     wordType = new KEduVocWordType(i18nc( "@item:inlistbox A subtype of the grammatical word type: Pronoun (I, you, he...)", "Personal" ), pronoun);
278     wordType->setWordType(KEduVocWordFlag::Pronoun);
279     pronoun->appendChildContainer(wordType);
280 }
281