27 #include <QTextStream>
29 #include <KStandardDirs>
31 #include <KTemporaryFile>
44 KJotsBook *book =
new KJotsBook();
53 file.setPrefix( KStandardDirs::locateLocal(
"data",
"kjots/" ) );
54 file.setSuffix(
".book" );
55 file.setAutoRemove(
false );
58 file.write(
"<?xml version='1.0' encoding='UTF-8' ?>\n<!DOCTYPE KJots>\n<KJots>\n" );
59 file.write( m_domDoc.toByteArray() );
60 file.write(
"</KJots>\n" );
61 kDebug() << file.fileName();
62 QString newFileName = file.fileName();
64 book->openBook( newFileName );
72 QDomElement KnowItImporter::addNote(
const KnowItNote& note)
74 QDomElement newElement;
75 int childNotesCount = m_childNotes[ note.
id ].size();
77 kDebug() << note.
title << childNotesCount;
78 if (childNotesCount > 0)
80 newElement = m_domDoc.createElement(QLatin1String(
"KJotsBook"));
83 newElement = m_domDoc.createElement(QLatin1String(
"KJotsPage"));
86 QDomElement titleTag = m_domDoc.createElement( QLatin1String(
"Title") );
87 titleTag.appendChild( m_domDoc.createTextNode( note.
title ) );
88 newElement.appendChild( titleTag );
89 QDomElement idTag = m_domDoc.createElement( QLatin1String(
"ID") );
90 idTag.appendChild( m_domDoc.createTextNode( QLatin1String(
"0") ) );
91 newElement.appendChild( idTag );
93 if (childNotesCount > 0)
95 QDomElement openTag = m_domDoc.createElement( QLatin1String(
"Open") );
96 openTag.appendChild( m_domDoc.createTextNode( QLatin1String(
"1") ) );
97 newElement.appendChild( openTag );
99 QDomElement titlePage = m_domDoc.createElement(QLatin1String(
"KJotsPage"));
100 QDomElement titlePageTitleTag = m_domDoc.createElement( QLatin1String(
"Title") );
101 titlePageTitleTag.appendChild( m_domDoc.createTextNode( note.
title ) );
102 titlePage.appendChild( titlePageTitleTag );
103 QDomElement titlePageIdTag = m_domDoc.createElement( QLatin1String(
"ID" ));
104 titlePageIdTag.appendChild( m_domDoc.createTextNode( QLatin1String(
"0") ) );
105 titlePage.appendChild( titlePageIdTag );
106 QDomElement titlePageTextTag = m_domDoc.createElement( QLatin1String(
"Text" ));
107 titlePageTextTag.appendChild( m_domDoc.createCDATASection( note.
content ) );
108 titlePage.appendChild( titlePageTextTag );
109 newElement.appendChild( titlePage );
111 foreach (
int id, m_childNotes[ note.
id ] )
113 QDomElement e = addNote( m_noteHash.value(
id) );
114 newElement.appendChild(e);
117 QString contents = note.
content;
118 if ( note.
links.size() > 0 ) {
119 if ( contents.endsWith( QLatin1String(
"</body></html>") ) ) {
122 contents.append( QLatin1String(
"<br /><br /><p><b>Links:</b></p>\n<ul>\n") );
123 for (
int i = 0; i < note.
links.size(); ++i ) {
124 kDebug() <<
"link" << note.
links[i].first << note.
links[i].second;
125 contents.append( QString::fromLatin1(
"<li><a href=\"%1\">%2</a></li>\n" )
126 .arg( note.
links[i].first )
127 .arg( note.
links[i].second ) );
129 contents.append( QLatin1String(
"</ul></body></html>") );
133 QDomElement textTag = m_domDoc.createElement( QLatin1String(
"Text") );
134 textTag.appendChild( m_domDoc.createCDATASection( contents ) );
135 newElement.appendChild( textTag );
143 void KnowItImporter::buildDomDocument()
145 QDomElement parent = m_domDoc.createElement( QLatin1String(
"KJotsBook") );
146 QDomElement titleTag = m_domDoc.createElement( QLatin1String(
"Title") );
147 titleTag.appendChild( m_domDoc.createTextNode( i18nc(
"Name for the top level book created to hold the imported data.",
"KNowIt Import") ) );
148 parent.appendChild( titleTag );
149 QDomElement idTag = m_domDoc.createElement( QLatin1String(
"ID") );
150 idTag.appendChild( m_domDoc.createTextNode( QLatin1String(
"0") ) );
151 parent.appendChild( idTag );
152 QDomElement openTag = m_domDoc.createElement( QLatin1String(
"Open") );
153 openTag.appendChild( m_domDoc.createTextNode( QLatin1String(
"1") ) );
154 parent.appendChild( openTag );
155 m_domDoc.appendChild( parent );
159 QDomElement e = addNote( n );
160 parent.appendChild(e);
163 kDebug() << m_domDoc.toString();
166 void KnowItImporter::buildNoteTree(
const KUrl& url )
169 QFile knowItFile( url.toLocalFile() );
170 if ( knowItFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
171 QSet<QByteArray> entryHeaders;
172 entryHeaders <<
"\\NewEntry" <<
"\\CurrentEntry";
176 QTextStream in( &knowItFile );
177 while ( !in.atEnd() ) {
178 QString line = in.readLine();
180 kDebug() <<
"got line: " << line;
182 if ( line.trimmed().isEmpty() ) {
186 foreach(
const QByteArray &header, entryHeaders ) {
187 if ( line.startsWith( QLatin1String(header) ) ) {
188 kDebug() <<
"init" << line << header;
189 line = line.right( line.size() - header.size() ).trimmed();
190 kDebug() <<
"header tag removed: " << line;
192 QStringList list = line.split( QLatin1Char(
' ') );
193 int startOfTitle = line.indexOf( QLatin1Char(
' ') );
196 kDebug() <<
"depth" << list.at( 0 ).trimmed();
198 int depth = list.at( 0 ).trimmed().toInt( &ok );
199 kDebug() << ok <<
"valid depth";
201 QString title = line.right( line.size() - startOfTitle ).trimmed();
210 n.
parent = m_lastNoteAtLevel[depth - 1].id;
213 QString contentLine = in.readLine();
218 while (( !in.atEnd() ) && ( !contentLine.trimmed().isEmpty() ) ) {
219 kDebug() << contentLine;
220 if ( contentLine.startsWith( QLatin1String(
"\\Link") ) ) {
221 url = contentLine.right( contentLine.size() - 5 ).trimmed();
222 contentLine = in.readLine();
225 if ( contentLine.startsWith( QLatin1String(
"\\Descr") ) ) {
226 target = contentLine.right( contentLine.size() - 6 ).trimmed();
227 contentLine = in.readLine();
230 if ( !url.isEmpty() && !target.isEmpty() ) {
231 QPair< QString, QString > link;
233 link.second = target;
238 contents.append( contentLine );
239 contentLine = in.readLine();
245 m_noteHash.insert(
id, n);
246 m_childNotes[n.
parent].append(
id);
249 if ( m_lastNoteAtLevel.size() == depth )
251 m_lastNoteAtLevel.append(n);
253 m_lastNoteAtLevel[depth] = n;
QList< QPair< QString, QString > > links
void importFromUrl(const KUrl &url)
Create a KJotsBook from the knowit file at url.