Make it possible to use a distinct selection model in the foldertreewidget.
[kdepim.git] / kjots / knowitimporter.cpp
blobd7a11a3f379030a4ec1c1b741d6dc8c80db612ec
1 /*
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
19 * 02110-1301 USA
23 #include "knowitimporter.h"
26 #include <QFile>
27 #include <QTextStream>
29 #include <KStandardDirs>
30 #include <KUrl>
31 #include <KTemporaryFile>
32 #include <KLocale>
34 #include <kdebug.h>
36 KnowItImporter::KnowItImporter()
40 void KnowItImporter::importFromUrl( KUrl url )
42 // TODO PORT
43 #if 0
44 KJotsBook *book = new KJotsBook();
45 buildNoteTree(url);
47 // foreach ()
48 // kDebug();
49 buildDomDocument();
52 KTemporaryFile file;
53 file.setPrefix( KStandardDirs::locateLocal( "data", "kjots/" ) );
54 file.setSuffix( ".book" );
55 file.setAutoRemove( false );
57 if ( file.open() ) {
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();
63 file.close();
64 book->openBook( newFileName );
68 return book;
69 #endif
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");
82 } else {
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);
116 } else {
117 QString contents = note.content;
118 if ( note.links.size() > 0 ) {
119 if ( contents.endsWith( QLatin1String("</body></html>") ) ) {
120 contents.chop( 14 );
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 );
138 return newElement;
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);
161 kDebug() << n.title;
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";
174 int id = 1;
176 QTextStream in( &knowItFile );
177 while ( !in.atEnd() ) {
178 QString line = in.readLine();
180 kDebug() << "got line: " << line;
182 if ( line.trimmed().isEmpty() ) {
183 continue;
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( ' ' );
194 bool ok = false;
196 kDebug() << "depth" << list.at( 0 ).trimmed();
198 int depth = list.at( 0 ).trimmed().toInt( &ok );
199 kDebug() << ok << "valid depth";
200 if ( ok ) {
201 QString title = line.right( line.size() - startOfTitle ).trimmed();
202 KnowItNote n;
203 n.title = title;
204 n.depth = depth;
205 n.id = id;
206 if (depth == 0)
208 n.parent = 0;
209 } else {
210 n.parent = m_lastNoteAtLevel[depth - 1].id;
213 QString contentLine = in.readLine();
214 QList< QPair <QString, QString> > links;
215 QString contents;
216 QString url;
217 QString target;
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();
223 continue;
225 if ( contentLine.startsWith( QLatin1String("\\Descr") ) ) {
226 target = contentLine.right( contentLine.size() - 6 ).trimmed();
227 contentLine = in.readLine();
228 continue;
230 if ( !url.isEmpty() && !target.isEmpty() ) {
231 QPair< QString, QString > link;
232 link.first = url;
233 link.second = target;
234 n.links << link;
235 url.clear();
236 target.clear();
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);
247 id++;
249 if ( m_lastNoteAtLevel.size() == depth )
251 m_lastNoteAtLevel.append(n);
252 } else {
253 m_lastNoteAtLevel[depth] = n;
256 if (depth == 0)
258 m_notes.append(n);
261 break; // If found first header, don't check for second one.
263 } // Foreach header.
264 } // At end of stream
265 } // File open.