summaryrefslogtreecommitdiffstats
path: root/ktouch/src/ktouchleveldata.cpp
blob: de9b1257cf44bc3cc1bd48638af302f2901ea96a (plain)
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
/***************************************************************************
 *   ktouchleveldata.cpp                                                   *
 *   -----------------                                                     *
 *   Copyright (C) 2000 by Håvard Frøiland, 2003 by Andreas Nicolai        *
 *   ghorwin@users.sourceforge.net                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "ktouchleveldata.h"
#include <tqiodevice.h>
#include <tqdom.h>
#include <klocale.h>
#include <kdebug.h>
#include <tqstringlist.h>

const TQString& KTouchLevelData::line(unsigned int lineNumber) const {
    if (lineNumber>=m_lines.size())
        lineNumber=0;
    return m_lines[lineNumber];
}

void KTouchLevelData::createDefault() {
    m_newChars = i18n("Quite a lot");
    m_lines.clear();
    TQString text = i18n("This is a small default text. If you want\n"
                        "to start practicing touch typing, open\n"
                        "a lecture/training file from the main menu.\n"
                        "If you press enter after the next line you have\n"
                        "successfully completed the default lecture. Hurray!!!");
    TQStringList l( TQStringList::split(TQChar('\n'), text) );
    for (TQStringList::const_iterator it = l.constBegin(); it != l.constEnd(); ++it)
        m_lines.push_back( *it );
}

bool KTouchLevelData::readLevel(TQTextStream& in) {
    TQString line;
    m_comment = m_newChars = TQString();
    m_lines.clear();
    // now read all the lines of the level, until EOF or an empty line
    line = in.readLine();
    while (!line.isNull()) {
        if (!line.isEmpty()) {
            // Do we have a comment here? And if yes, is a keyword in the line?
            if (line.find("# Comment:")==0)
                m_comment = line.right(line.length() - 10).stripWhiteSpace();
            else if (line[0]!='#') {
                // the first line is the new characters line
                if (m_newChars.isNull())  m_newChars = line;
                else                              m_lines.push_back(line);
            }
        }
        line = in.readLine();
    };
    if (m_lines.empty()) {
        createDefault();
        return false;
    }
    return true;
}

bool KTouchLevelData::readLevel(TQDomNode in) {
	TQDomNode newChars = in.namedItem("NewCharacters");
	if (newChars.isNull())  m_newChars = i18n("basically all characters on the keyboard","abcdefghijklmnopqrstuvwxyz");
	else					m_newChars = newChars.firstChild().nodeValue();
	TQDomNode levelComment = in.namedItem("LevelComment");
	if (!levelComment.isNull())  m_comment = levelComment.firstChild().nodeValue();
    m_lines.clear();
	TQDomNode line = in.namedItem("Line");
	while ( !line.isNull() ) {
        TQString l = line.firstChild().nodeValue();
		m_lines.push_back( l.stripWhiteSpace() );
        line = line.nextSibling();
    }
    if (m_lines.empty()) {
        createDefault();
        return false;
    }
    return true;
}

void KTouchLevelData::writeLevel(TQTextStream& out) const {
    out << "# Comment: " << m_comment << endl;   // comment line
    out << m_newChars << endl;                   // new characters line
    for (TQValueVector<TQString>::const_iterator it=m_lines.begin(); it!=m_lines.end(); ++it)
        out << *it << endl;
    out << endl;
}

void KTouchLevelData::writeLevel(TQDomDocument& doc, TQDomElement& root) const {
	TQDomElement level = doc.createElement("Level");
	root.appendChild(level);
	// comment (optional)
	if (!m_comment.isEmpty()) {
		TQDomElement comment = doc.createElement("LevelComment");
		TQDomText commentText = doc.createTextNode(m_comment);
		comment.appendChild(commentText);
		level.appendChild(comment);
	}
	// new characters (must have that)
	TQDomElement newchars = doc.createElement("NewCharacters");
	TQDomText newcharsText;
	if (m_newChars.isEmpty())
		newcharsText = doc.createTextNode(i18n("basically all characters on the keyboard","abcdefghijklmnopqrstuvwxyz"));
	else
		newcharsText = doc.createTextNode(m_newChars);
	newchars.appendChild(newcharsText);
	level.appendChild(newchars);
	// the lines
    for (TQValueVector<TQString>::const_iterator it=m_lines.begin(); it!=m_lines.end(); ++it) {
		TQDomElement line = doc.createElement("Line");
		TQDomText lineText = doc.createTextNode(*it);
		line.appendChild(lineText);
		level.appendChild(line);
	}
}