File indexing completed on 2024-04-28 03:40:31

0001 /***************************************************************************
0002  *   Copyright (C) 2002 by Gunnar Schmi Dt <kmouth@schmi-dt.de             *
0003  *             (C) 2015,2022 by Jeremy Whiting <jpwhiting@kde.org>         *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
0019  ***************************************************************************/
0020 
0021 #include "phrasebookreader.h"
0022 
0023 #include <QtDebug>
0024 
0025 PhraseBookReader::PhraseBookReader()
0026     : level(0)
0027     , starting(true)
0028 {
0029 }
0030 
0031 PhraseBookReader::~PhraseBookReader()
0032 {
0033 }
0034 
0035 bool PhraseBookReader::read(QIODevice *device)
0036 {
0037     xml.setDevice(device);
0038 
0039     list.clear();
0040     level = 0;
0041     starting = true;
0042 
0043     qDebug() << "Beginning to read xml document";
0044 
0045     if (xml.readNextStartElement()) {
0046         qDebug() << "First node name " << xml.name();
0047         if (xml.name() == QLatin1String("phrasebook")) {
0048             readbook();
0049         } else if (xml.name() == QLatin1String("phrase")) {
0050             readphrase();
0051         } else {
0052             xml.raiseError(QLatin1String("The file is not a valid phrasebook xml file"));
0053         }
0054     } else {
0055         xml.raiseError(QLatin1String("Unable to get first xml element"));
0056     }
0057 
0058     return !xml.error();
0059 }
0060 
0061 void PhraseBookReader::readbook()
0062 {
0063     Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("phrasebook"));
0064 
0065     // Get the book's name attribute if any
0066     QString name = xml.attributes().value(QLatin1String("name")).toString();
0067 
0068     qDebug() << "Reading book with name " << name << " level is " << level;
0069     Phrase phrase;
0070     phrase.setPhrase(name);
0071 
0072     if ((phrase.getPhrase().isNull() || phrase.getPhrase().isEmpty()) && starting) {
0073         // Phrase book name is empty, and starting, so don't increase level or add empty entry
0074     } else {
0075         list += PhraseBookEntry(phrase, level, false);
0076         level++;
0077     }
0078     starting = false;
0079 
0080     while (xml.readNextStartElement()) {
0081         if (xml.name() == QLatin1String("phrase")) {
0082             readphrase();
0083         } else if (xml.name() == QLatin1String("phrasebook")) {
0084             readbook();
0085         } else
0086             xml.skipCurrentElement();
0087     }
0088 
0089     level--;
0090 }
0091 
0092 void PhraseBookReader::readphrase()
0093 {
0094     Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("phrase"));
0095 
0096     QString shortcut = xml.attributes().value(QLatin1String("shortcut")).toString();
0097 
0098     Phrase phrase;
0099     phrase.setShortcut(shortcut);
0100     phrase.setPhrase(xml.readElementText());
0101 
0102     qDebug() << "Reading phrase with text " << phrase.getPhrase();
0103 
0104     list += PhraseBookEntry(phrase, level, true);
0105 }
0106 
0107 QString PhraseBookReader::errorString() const
0108 {
0109     return QLatin1String("%1\nLine %2, column %3").arg(xml.errorString()).arg(xml.lineNumber()).arg(xml.columnNumber());
0110 }
0111 
0112 PhraseBookEntryList PhraseBookReader::getPhraseList()
0113 {
0114     return list;
0115 }