• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kjots

  • sources
  • kde-4.14
  • kdepim
  • kjots
knowitimporter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KJots
3  *
4  * Copyright 2008 Stephen Kelly <steveire@gmail.com>
5  *
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.
10  *
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.
15  *
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
20  */
21 
22 
23 #include "knowitimporter.h"
24 
25 
26 #include <QFile>
27 #include <QTextStream>
28 
29 #include <KStandardDirs>
30 #include <KUrl>
31 #include <KTemporaryFile>
32 #include <KLocalizedString>
33 
34 #include <kdebug.h>
35 
36 KnowItImporter::KnowItImporter()
37 {
38 }
39 
40 void KnowItImporter::importFromUrl( const KUrl& url )
41 {
42 // TODO PORT
43 #if 0
44  KJotsBook *book = new KJotsBook();
45  buildNoteTree(url);
46 
47 // foreach ()
48 // kDebug();
49  buildDomDocument();
50 
51 
52  KTemporaryFile file;
53  file.setPrefix( KStandardDirs::locateLocal( "data", "kjots/" ) );
54  file.setSuffix( ".book" );
55  file.setAutoRemove( false );
56 
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 );
65  }
66 
67 
68  return book;
69 #endif
70 }
71 
72 QDomElement KnowItImporter::addNote( const KnowItNote& note)
73 {
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)
79  {
80  newElement = m_domDoc.createElement(QLatin1String("KJotsBook"));
81 
82  } else {
83  newElement = m_domDoc.createElement(QLatin1String("KJotsPage"));
84  }
85 
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") ) ); // Gets a valid id later.
91  newElement.appendChild( idTag );
92 
93  if (childNotesCount > 0)
94  {
95  QDomElement openTag = m_domDoc.createElement( QLatin1String("Open") );
96  openTag.appendChild( m_domDoc.createTextNode( QLatin1String("1") ) );
97  newElement.appendChild( openTag );
98 
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") ) ); // Gets a valid id later.
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 );
110 
111  foreach (int id, m_childNotes[ note.id ] )
112  {
113  QDomElement e = addNote( m_noteHash.value(id) );
114  newElement.appendChild(e);
115  }
116  } else {
117  QString contents = note.content;
118  if ( note.links.size() > 0 ) {
119  if ( contents.endsWith( QLatin1String("</body></html>") ) ) {
120  contents.chop( 14 );
121  }
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 ) );
128  }
129  contents.append( QLatin1String("</ul></body></html>") );
130  }
131 
132 
133  QDomElement textTag = m_domDoc.createElement( QLatin1String("Text") );
134  textTag.appendChild( m_domDoc.createCDATASection( contents ) );
135  newElement.appendChild( textTag );
136  }
137 
138  return newElement;
139 
140 
141 }
142 
143 void KnowItImporter::buildDomDocument()
144 {
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") ) ); // Gets a valid id later.
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 );
156 
157  foreach (const KnowItNote &n, m_notes)
158  {
159  QDomElement e = addNote( n );
160  parent.appendChild(e);
161  kDebug() << n.title;
162  }
163  kDebug() << m_domDoc.toString();
164 }
165 
166 void KnowItImporter::buildNoteTree( const KUrl& url )
167 {
168 
169  QFile knowItFile( url.toLocalFile() );
170  if ( knowItFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
171  QSet<QByteArray> entryHeaders;
172  entryHeaders << "\\NewEntry" << "\\CurrentEntry";
173 
174  int id = 1;
175 
176  QTextStream in( &knowItFile );
177  while ( !in.atEnd() ) {
178  QString line = in.readLine();
179 
180  kDebug() << "got line: " << line;
181 
182  if ( line.trimmed().isEmpty() ) {
183  continue;
184  }
185 
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;
191 
192  QStringList list = line.split( QLatin1Char(' ') );
193  int startOfTitle = line.indexOf( QLatin1Char(' ') );
194  bool ok = false;
195 
196  kDebug() << "depth" << list.at( 0 ).trimmed();
197 
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)
207  {
208  n.parent = 0;
209  } else {
210  n.parent = m_lastNoteAtLevel[depth - 1].id;
211  }
212 
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;
224  }
225  if ( contentLine.startsWith( QLatin1String("\\Descr") ) ) {
226  target = contentLine.right( contentLine.size() - 6 ).trimmed();
227  contentLine = in.readLine();
228  continue;
229  }
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();
237  }
238  contents.append( contentLine );
239  contentLine = in.readLine();
240  }
241 
242 
243  n.content = contents;
244 
245  m_noteHash.insert(id, n);
246  m_childNotes[n.parent].append(id);
247  id++;
248 
249  if ( m_lastNoteAtLevel.size() == depth )
250  {
251  m_lastNoteAtLevel.append(n);
252  } else {
253  m_lastNoteAtLevel[depth] = n;
254  }
255 
256  if (depth == 0)
257  {
258  m_notes.append(n);
259  }
260  }
261  break; // If found first header, don't check for second one.
262  }
263  } // Foreach header.
264  } // At end of stream
265  } // File open.
266 }
267 
268 
KnowItNote::parent
int parent
Definition: knowitimporter.h:38
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QString::append
QString & append(QChar ch)
QHash::insert
iterator insert(const Key &key, const T &value)
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
QByteArray
QDomDocument::toString
QString toString(int indent) const
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KnowItNote::links
QList< QPair< QString, QString > > links
Definition: knowitimporter.h:40
QHash::size
int size() const
QList::at
const T & at(int i) const
QString::size
int size() const
KnowItNote::id
int id
Definition: knowitimporter.h:37
QString::chop
void chop(int n)
QFile
QTextStream
QList::size
int size() const
KnowItNote
Definition: knowitimporter.h:32
QString::clear
void clear()
QList::append
void append(const T &value)
QString::isEmpty
bool isEmpty() const
QString::trimmed
QString trimmed() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString::endsWith
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
QSet
QList::first
T & first()
QString
QStringList
QPair
QString::right
QString right(int n) const
QDomDocument::createTextNode
QDomText createTextNode(const QString &value)
QHash::value
const T value(const Key &key) const
QLatin1Char
QDomDocument::createCDATASection
QDomCDATASection createCDATASection(const QString &value)
KnowItNote::content
QString content
Definition: knowitimporter.h:36
KnowItImporter::importFromUrl
void importFromUrl(const KUrl &url)
Create a KJotsBook from the knowit file at url.
Definition: knowitimporter.cpp:40
QLatin1String
KnowItNote::depth
int depth
Definition: knowitimporter.h:35
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
QDomElement
KnowItNote::title
QString title
Definition: knowitimporter.h:34
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
KnowItImporter::KnowItImporter
KnowItImporter()
Definition: knowitimporter.cpp:36
QDomDocument::toByteArray
QByteArray toByteArray(int indent) const
knowitimporter.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjots

Skip menu "kjots"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal