2 * This file is part of KJots
4 * Copyright 2008 Stephen Kelly <steveire@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "knowitimporter.h"
27 #include <QTextStream>
29 #include <KStandardDirs>
31 #include <KTemporaryFile>
36 KnowItImporter::KnowItImporter()
40 void KnowItImporter::importFromUrl( KUrl url
)
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( KnowItNote note
)
74 QDomElement newElement
;
75 int childNotesCount
= m_childNotes
[ note
.id
].size();
76 // int childNotesCount = note.childNotes.size();
77 kDebug() << note
.title
<< childNotesCount
;
78 if (childNotesCount
> 0)
80 newElement
= m_domDoc
.createElement("KJotsBook");
83 newElement
= m_domDoc
.createElement("KJotsPage");
86 QDomElement titleTag
= m_domDoc
.createElement( "Title" );
87 titleTag
.appendChild( m_domDoc
.createTextNode( note
.title
) );
88 newElement
.appendChild( titleTag
);
89 QDomElement idTag
= m_domDoc
.createElement( "ID" );
90 idTag
.appendChild( m_domDoc
.createTextNode( "0" ) ); // Gets a valid id later.
91 newElement
.appendChild( idTag
);
93 if (childNotesCount
> 0)
95 QDomElement openTag
= m_domDoc
.createElement( "Open" );
96 openTag
.appendChild( m_domDoc
.createTextNode( "1" ) );
97 newElement
.appendChild( openTag
);
99 QDomElement titlePage
= m_domDoc
.createElement("KJotsPage");
100 QDomElement titlePageTitleTag
= m_domDoc
.createElement( "Title" );
101 titlePageTitleTag
.appendChild( m_domDoc
.createTextNode( note
.title
) );
102 titlePage
.appendChild( titlePageTitleTag
);
103 QDomElement titlePageIdTag
= m_domDoc
.createElement( "ID" );
104 titlePageIdTag
.appendChild( m_domDoc
.createTextNode( "0" ) ); // Gets a valid id later.
105 titlePage
.appendChild( titlePageIdTag
);
106 QDomElement titlePageTextTag
= m_domDoc
.createElement( "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( "<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( "<li><a href=\"%1\">%2</a></li>\n" )
126 .arg( note
.links
[i
].first
)
127 .arg( note
.links
[i
].second
) );
129 contents
.append( "</ul></body></html>" );
133 QDomElement textTag
= m_domDoc
.createElement( "Text" );
134 textTag
.appendChild( m_domDoc
.createCDATASection( contents
) );
135 newElement
.appendChild( textTag
);
143 void KnowItImporter::buildDomDocument()
145 QDomElement parent
= m_domDoc
.createElement( "KJotsBook" );
146 QDomElement titleTag
= m_domDoc
.createElement( "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( "ID" );
150 idTag
.appendChild( m_domDoc
.createTextNode( "0" ) ); // Gets a valid id later.
151 parent
.appendChild( idTag
);
152 QDomElement openTag
= m_domDoc
.createElement( "Open" );
153 openTag
.appendChild( m_domDoc
.createTextNode( "1" ) );
154 parent
.appendChild( openTag
);
155 m_domDoc
.appendChild( parent
);
157 foreach (const KnowItNote
&n
, m_notes
)
159 QDomElement e
= addNote( n
);
160 parent
.appendChild(e
);
163 kDebug() << m_domDoc
.toString();
166 void KnowItImporter::buildNoteTree( 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( 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( ' ' );
193 int startOfTitle
= line
.indexOf( ' ' );
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();
214 QList
< QPair
<QString
, QString
> > links
;
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();
243 n
.content
= contents
;
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
;
261 break; // If found first header, don't check for second one.
264 } // At end of stream