Ran am2cmake.
[basket4.git] / src / softwareimporters.cpp
blob4b437b22de09d14c1c27a94ef6d8aa6a41c78a4f
1 /***************************************************************************
2 * Copyright (C) 2003 by Sébastien Laoût *
3 * slaout@linux62.org *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <qstring.h>
22 #include <qdir.h>
23 //Added by qt3to4:
24 #include <Q3TextStream>
25 #include <Q3HBoxLayout>
26 #include <Q3VBoxLayout>
27 #include <kstandarddirs.h>
28 #include <klocale.h>
29 #include <kfiledialog.h>
30 #include <q3ptrstack.h>
31 #include <qlayout.h>
32 #include <qvbuttongroup.h>
33 #include <qradiobutton.h>
34 #include <kmessagebox.h>
35 #include <q3textedit.h>
37 #include "softwareimporters.h"
38 #include "basket.h"
39 #include "basketfactory.h"
40 #include "notefactory.h"
41 #include "global.h"
42 #include "bnpview.h"
43 #include "xmlwork.h"
44 #include "tools.h"
46 /** class TreeImportDialog: */
48 TreeImportDialog::TreeImportDialog(QWidget *parent)
49 : KDialogBase(KDialogBase::Swallow, i18n("Import Hierarchy"), KDialogBase::Ok | KDialogBase::Cancel,
50 KDialogBase::Ok, parent, /*name=*/"ImportHierarchy", /*modal=*/true, /*separator=*/false)
52 QWidget *page = new QWidget(this);
53 Q3VBoxLayout *topLayout = new Q3VBoxLayout(page, /*margin=*/0, spacingHint());
55 m_choices = new Q3VButtonGroup(i18n("How to Import the Notes?"), page);
56 new QRadioButton(i18n("&Keep original hierarchy (all notes in separate baskets)"), m_choices);
57 new QRadioButton(i18n("&First level notes in separate baskets"), m_choices);
58 new QRadioButton(i18n("&All notes in one basket"), m_choices);
59 m_choices->setButton(0);
60 topLayout->addWidget(m_choices);
61 topLayout->addStretch(10);
63 setMainWidget(page);
66 TreeImportDialog::~TreeImportDialog()
70 int TreeImportDialog::choice()
72 return m_choices->selectedId();
75 /** class TextFileImportDialog: */
77 TextFileImportDialog::TextFileImportDialog(QWidget *parent)
78 : KDialogBase(KDialogBase::Swallow, i18n("Import Text File"), KDialogBase::Ok | KDialogBase::Cancel,
79 KDialogBase::Ok, parent, /*name=*/"ImportTextFile", /*modal=*/true, /*separator=*/false)
81 QWidget *page = new QWidget(this);
82 Q3VBoxLayout *topLayout = new Q3VBoxLayout(page, /*margin=*/0, spacingHint());
84 m_choices = new Q3VButtonGroup(i18n("Format of the Text File"), page);
85 new QRadioButton(i18n("Notes separated by an &empty line"), m_choices);
86 new QRadioButton(i18n("One &note per line"), m_choices);
87 new QRadioButton(i18n("Notes begin with a &dash (-)"), m_choices);
88 new QRadioButton(i18n("Notes begin with a &star (*)"), m_choices);
89 m_anotherSeparator = new QRadioButton(i18n("&Use another separator:"), m_choices);
91 QWidget *indentedTextEdit = new QWidget(m_choices);
92 Q3HBoxLayout *hLayout = new Q3HBoxLayout(indentedTextEdit, /*margin=*/0, spacingHint());
93 hLayout->addSpacing(20);
94 m_customSeparator = new Q3TextEdit(indentedTextEdit);
95 m_customSeparator->setTextFormat(Qt::PlainText);
96 hLayout->addWidget(m_customSeparator);
97 m_choices->insertChild(indentedTextEdit);
99 new QRadioButton(i18n("&All in one note"), m_choices);
100 m_choices->setButton(0);
101 topLayout->addWidget(m_choices);
103 connect( m_customSeparator, SIGNAL(textChanged()), this, SLOT(customSeparatorChanged()) );
105 setMainWidget(page);
108 TextFileImportDialog::~TextFileImportDialog()
112 QString TextFileImportDialog::separator()
114 switch (m_choices->selectedId()) {
115 default:
116 case 0: return "\n\n";
117 case 1: return "\n";
118 case 2: return "\n-";
119 case 3: return "\n*";
120 case 4: return m_customSeparator->text();
121 case 5: return "";
125 void TextFileImportDialog::customSeparatorChanged()
127 if (!m_anotherSeparator->isOn())
128 m_anotherSeparator->toggle();
131 /** namespace SoftwareImporters: */
133 QString SoftwareImporters::fromICS(const QString &ics)
135 QString result = ics;
137 // Remove escaped '\' characters and append the text to the body
138 int pos = 0;
139 while ( (pos = result.find('\\', pos)) != -1 ) {
140 if ((uint)pos == result.length() - 1) // End of string
141 break;
142 if (result[pos+1] == 'n') {
143 result.replace(pos, 2, '\n');
144 } else if (result[pos+1] == 'r') {
145 result.replace(pos, 2, '\r');
146 } else if (result[pos+1] == 't') {
147 result.replace(pos, 2, '\t');
148 } else if (result[pos] == '\\') {
149 result.remove(pos, 1); // Take care of "\\", "\,", "\;" and other escaped characters I haven't noticed
150 ++pos;
154 return result;
157 QString SoftwareImporters::fromTomboy(QString tomboy)
159 // The first line is the note title, and we already have it, so we remove it (yes, that's pretty stupid to duplicate it in the content...):
160 tomboy = tomboy.mid(tomboy.find("\n")).stripWhiteSpace();
162 // Font styles and decorations:
163 tomboy.replace("<bold>", "<b>");
164 tomboy.replace("</bold>", "</b>");
165 tomboy.replace("<italic>", "<i>");
166 tomboy.replace("</italic>", "</i>");
167 tomboy.replace("<strikethrough>", "<span style='text-decoration: line-through'>");
168 tomboy.replace("</strikethrough>", "</span>");
170 // Highlight not supported by QTextEdit:
171 tomboy.replace("<highlight>", "<span style='color:#ff0080'>");
172 tomboy.replace("</highlight>", "</span>");
174 // Font sizes:
175 tomboy.replace("<size:small>", "<span style='font-size: 7pt'>");
176 tomboy.replace("</size:small>", "</span>");
177 tomboy.replace("<size:large>", "<span style='font-size: 16pt'>");
178 tomboy.replace("</size:large>", "</span>");
179 tomboy.replace("<size:huge>", "<span style='font-size: 20pt'>");
180 tomboy.replace("</size:huge>", "</span>");
182 // Internal links to other notes aren't supported yet by BasKet Note Pads:
183 tomboy.replace("<link:internal>", "");
184 tomboy.replace("</link:internal>", "");
186 // In the Tomboy file, new lines are "\n" and not "<br>":
187 tomboy.replace("\n", "<br>\n");
189 // Preserve consecutive spaces:
190 return "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>" + tomboy + "</body></html>";
193 Note* SoftwareImporters::insertTitledNote(Basket *parent, const QString &title, const QString &content, Qt::TextFormat format/* = Qt::PlainText*/, Note *parentNote/* = 0*/)
195 Note *nGroup = new Note(parent);
197 Note *nTitle = NoteFactory::createNoteText(title, parent);
198 nTitle->addState(Tag::stateForId("title"));
200 Note *nContent;
201 if (format == Qt::PlainText)
202 nContent = NoteFactory::createNoteText(content, parent);
203 else
204 nContent = NoteFactory::createNoteHtml(content, parent);
206 if (parentNote == 0)
207 parentNote = parent->firstNote(); // In the first column!
208 parent->insertNote(nGroup, parentNote, Note::BottomColumn, QPoint(), /*animate=*/false);
209 parent->insertNote(nTitle, nGroup, Note::BottomColumn, QPoint(), /*animate=*/false);
210 parent->insertNote(nContent, nTitle, Note::BottomInsert, QPoint(), /*animate=*/false);
212 return nGroup;
215 void SoftwareImporters::finishImport(Basket *basket)
217 // Unselect the last inserted group:
218 basket->unselectAll();
220 // Focus the FIRST note (the last inserted note is currently focused!):
221 basket->setFocusedNote(basket->firstNoteShownInStack());
223 // Relayout every notes at theire new place and simulate a load animation (because already loaded just after the creation).
224 // Without a relayouting, notes on the bottom would comes from the top (because they were inserted on top) and clutter the animation load:
225 basket->relayoutNotes(/*animate=*/false);
226 basket->animateLoad();
227 basket->save();
232 void SoftwareImporters::importKJots()
234 QString dirPath = locateLocal("appdata","") + "/../kjots/"; // I think the assumption is good
235 QDir dir(dirPath, QString::null, QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
237 QStringList list = dir.entryList();
238 if (list.isEmpty())
239 return;
241 BasketFactory::newBasket(/*icon=*/"kjots", /*name=*/i18n("From KJots"), /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/0);
242 Basket *kjotsBasket = Global::bnpView->currentBasket();
244 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { // For each file
245 QFile file(dirPath + *it);
246 if (file.open(QIODevice::ReadOnly)) {
247 Q3TextStream stream(&file);
248 stream.setEncoding(Q3TextStream::Locale);
249 QString buf = stream.readLine();
251 // IT IS A NOTEBOOK FILE, AT THE VERION 0.6.x and older:
252 if ( !buf.isNull() && buf.left(9) == "\\NewEntry") {
254 // First create a basket for it:
255 BasketFactory::newBasket(/*icon=*/"kjots", /*name=*/KURL(file.name()).fileName(), /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/kjotsBasket);
256 Basket *basket = Global::bnpView->currentBasket();
257 basket->load();
259 QString title, body;
260 bool haveAnEntry = false;
261 while (1) {
262 if (buf.left(9) == "\\NewEntry") {
263 if (haveAnEntry) // Do not add note the first time
264 insertTitledNote(basket, title, Tools::stripEndWhiteSpaces(body));
265 title = buf.mid(10, buf.length()); // Problem : basket will be saved
266 body = ""; // New note will then be created // EACH time a note is imported
267 haveAnEntry = true;
268 } else if (buf.left(3) != "\\ID") { // Don't care of the ID
269 // Remove escaped '\' characters and append the text to the body
270 int pos = 0;
271 while ( (pos = buf.find('\\', pos)) != -1 )
272 if (buf[++pos] == '\\')
273 buf.remove(pos, 1);
274 body.append(buf + "\n");
276 buf = stream.readLine();
277 if (buf.isNull()) // OEF
278 break;
280 // Add the ending note (there isn't any other "\\NewEntry" to do it):
281 if (haveAnEntry)
282 insertTitledNote(basket, title, Tools::stripEndWhiteSpaces(body));
283 finishImport(basket);
285 // IT IS A NOTEBOOK XML FILE, AT THE VERION 0.7.0 and later:
286 } else if ( (*it).endsWith(".book") /*&& !buf.isNull() && (buf.left(2) == "<!" / *<!DOCTYPE...* / || buf.left(2) == "<?" / *<?xml...* /)*/) {
288 QDomDocument *doc = XMLWork::openFile("KJots", dirPath + *it);
289 if (doc == 0)
290 continue;
292 QString bookTitle = XMLWork::getElementText(doc->documentElement(), "KJotsBook/Title");
294 // First create a basket for it:
295 BasketFactory::newBasket(/*icon=*/"kjots", /*name=*/bookTitle, /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/kjotsBasket);
296 Basket *basket = Global::bnpView->currentBasket();
297 basket->load();
299 QDomElement docElem = XMLWork::getElement(doc->documentElement(), "KJotsBook");
300 for ( QDomNode n = docElem.firstChild(); !n.isNull(); n = n.nextSibling() ) {
301 QDomElement e = n.toElement();
302 if ( (!e.isNull()) && e.tagName() == "KJotsPage" )
303 insertTitledNote(basket, XMLWork::getElementText(e, "Title"), XMLWork::getElementText(e, "Text"));
305 finishImport(basket);
309 file.close();
314 void SoftwareImporters::importKNotes()
316 QString dirPath = locateLocal("appdata","") + "/../knotes/"; // I thing the assumption is good
317 QDir dir(dirPath, QString::null, QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
319 QStringList list = dir.entryList();
320 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { // For each file
321 if ( ! (*it).endsWith(".ics") ) // Don't process *.ics~ and otehr files
322 continue;
323 QFile file(dirPath + *it);
324 if (file.open(QIODevice::ReadOnly)) {
325 Q3TextStream stream(&file);
326 stream.setEncoding(Q3TextStream::UnicodeUTF8);
328 // First create a basket for it:
329 BasketFactory::newBasket(/*icon=*/"knotes", /*name=*/i18n("From KNotes"), /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/0);
330 Basket *basket = Global::bnpView->currentBasket();
331 basket->load();
333 bool inVJournal = false;
334 bool inDescription = false;
335 bool isRichText = false;
336 QString title, body;
337 QString buf;
338 while (1) {
339 buf = stream.readLine();
340 if (buf.isNull()) // OEF
341 break;
343 if ( !buf.isNull() && buf == "BEGIN:VJOURNAL") {
344 inVJournal = true;
345 } else if (inVJournal && buf.startsWith("SUMMARY:")) {
346 title = buf.mid(8, buf.length());
347 } else if (inVJournal && buf.startsWith("DESCRIPTION:")) {
348 body = buf.mid(12, buf.length());
349 inDescription = true;
350 } else if (inDescription && buf.startsWith(" ")) {
351 body += buf.mid(1, buf.length());
352 } else if (buf.startsWith("X-KDE-KNotes-RichText:")) {
353 isRichText = XMLWork::trueOrFalse(buf.mid(22, buf.length() - 22).stripWhiteSpace(), "false");
354 } else if (buf == "END:VJOURNAL") {
355 insertTitledNote(basket, fromICS(title), fromICS(body), (isRichText ? Qt::RichText : Qt::PlainText));
356 inVJournal = false;
357 inDescription = false;
358 isRichText = false;
359 title = "";
360 body = "";
361 } else
362 inDescription = false;
365 // Bouh : duplicate code
366 // In case of unvalide ICAL file!
367 if ( ! body.isEmpty() ) // Add the ending note
368 insertTitledNote(basket, fromICS(title), fromICS(body), (isRichText ? Qt::RichText : Qt::PlainText));
369 file.close();
370 finishImport(basket);
375 void SoftwareImporters::importStickyNotes()
377 // Sticky Notes file is usually located in ~/.gnome2/stickynotes_applet
378 // We will search all directories in "~/" that contain "gnome" in the name,
379 // and will search for "stickynotes_applet" file (that should be XML file with <stickynotes> root.
380 QDir dir(QDir::home().absPath(), QString::null, QDir::Name | QDir::IgnoreCase,
381 QDir::Dirs | QDir::NoSymLinks | QDir::Hidden);
382 QStringList founds;
384 QStringList list = dir.entryList();
385 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { // For each folder
386 if ( (*it).contains("gnome", false) ) {
387 QString fullPath = QDir::home().absPath() + "/" + (*it) + "/stickynotes_applet";
388 if (dir.exists(fullPath))
389 founds += fullPath;
393 for ( QStringList::Iterator it = founds.begin(); it != founds.end(); ++it ) { // For each file
394 QFile file(*it);
395 QDomDocument *doc = XMLWork::openFile("stickynotes", *it);
396 if (doc == 0)
397 continue;
399 // First create a basket for it:
400 BasketFactory::newBasket(/*icon=*/"gnome", /*name=*/i18n("From Sticky Notes"), /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/0);
401 Basket *basket = Global::bnpView->currentBasket();
402 basket->load();
404 QDomElement docElem = doc->documentElement();
405 for ( QDomNode n = docElem.firstChild(); !n.isNull(); n = n.nextSibling() ) {
406 QDomElement e = n.toElement();
407 if ( (!e.isNull()) && e.tagName() == "note" )
408 insertTitledNote(basket, e.attribute("title"), e.text());
410 finishImport(basket);
416 // TODO: FIXME: Code duplicated from notecontent.cpp but with UTF-8 encoding.
417 // TODO: FIXME: Later, merge!
418 QString loadUtf8FileToString(const QString &fileName)
420 QFile file(fileName);
421 if (file.open(QIODevice::ReadOnly)) {
422 Q3TextStream stream(&file);
423 stream.setEncoding(Q3TextStream::UnicodeUTF8);
424 QString text;
425 text = stream.read();
426 file.close();
427 return text;
428 } else
429 return "";
433 void SoftwareImporters::importTomboy()
435 QString dirPath = QDir::home().absPath() + "/.tomboy/"; // I thing the assumption is good
436 QDir dir(dirPath, QString::null, QDir::Name | QDir::IgnoreCase, QDir::Files | QDir::NoSymLinks);
438 Basket *basket = 0; // Create the basket ONLY if we found at least one note to add!
440 QStringList list = dir.entryList();
441 for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { // For each file
442 if ( ! (*it).endsWith(".note") )
443 continue;
444 QDomDocument *doc = XMLWork::openFile("note", dirPath + *it);
445 if (doc == 0)
446 continue;
448 if (basket == 0) {
449 // First create a basket for it:
450 BasketFactory::newBasket(/*icon=*/"tomboy", /*name=*/i18n("From Tomboy"), /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/0);
451 basket = Global::bnpView->currentBasket();
452 basket->load();
455 QDomElement docElem = doc->documentElement();
456 QString title = XMLWork::getElementText(docElem, "title");
458 // DOES NOT REALLY WORKS:
459 //QDomElement contentElement = XMLWork::getElement(docElem, "text/note-content");
460 //QString content = XMLWork::innerXml(contentElement);
462 // Isolate "<note-content version="0.1">CONTENT</note-content>"!
463 QString xml = loadUtf8FileToString(dirPath + *it);
464 xml = xml.mid(xml.find("<note-content "));
465 xml = xml.mid(xml.find(">") + 1);
466 xml = xml.mid(0, xml.find("</note-content>"));
468 if (!title.isEmpty() && !/*content*/xml.isEmpty())
469 insertTitledNote(basket, title, fromTomboy(xml/*content*/), Qt::RichText);
472 if (basket)
473 finishImport(basket);
476 void SoftwareImporters::importTextFile()
478 QString fileName = KFileDialog::getOpenFileName(":ImportTextFile", "*|All files");
479 if (fileName.isEmpty())
480 return;
482 TextFileImportDialog dialog;
483 if (dialog.exec() == QDialog::Rejected)
484 return;
485 QString separator = dialog.separator();
487 QFile file(fileName);
488 if (file.open(QIODevice::ReadOnly)) {
489 Q3TextStream stream(&file);
490 stream.setEncoding(Q3TextStream::Locale);
491 QString content = stream.read();
492 QStringList list = (separator.isEmpty()
493 ? QStringList(content)
494 : QStringList::split(separator, content, /*allowEmptyEntries=*/false)
497 // First create a basket for it:
498 QString title = i18n("From TextFile.txt", "From %1").arg(KURL(fileName).fileName());
499 BasketFactory::newBasket(/*icon=*/"txt", title, /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", /*createIn=*/0);
500 Basket *basket = Global::bnpView->currentBasket();
501 basket->load();
503 // Import every notes:
504 for (QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
505 Note *note = NoteFactory::createNoteFromText((*it).stripWhiteSpace(), basket);
506 basket->insertNote(note, basket->firstNote(), Note::BottomColumn, QPoint(), /*animate=*/false);
509 // Finish the export:
510 finishImport(basket);
514 /** @author Petri Damsten <petri.damsten@iki.fi>
516 void SoftwareImporters::importKnowIt()
518 KURL url = KFileDialog::getOpenURL(":ImportKnowIt",
519 "*.kno|KnowIt files\n*|All files");
520 if (!url.isEmpty())
522 QFile file(url.path());
523 QFileInfo info(url.path());
524 Basket* basket = 0;
525 Q3PtrStack<Basket> baskets;
526 QString text;
527 int hierarchy = 0;
529 TreeImportDialog dialog;
530 if (dialog.exec() == QDialog::Rejected)
531 return;
533 hierarchy = dialog.choice();
535 BasketFactory::newBasket(/*icon=*/"knowit",
536 /*name=*/info.baseName(),
537 /*backgroundImage=*/"",
538 /*backgroundColor=*/QColor(),
539 /*textColor=*/QColor(),
540 /*templateName=*/"1column",
541 /*createIn=*/0);
542 basket = Global::bnpView->currentBasket();
543 basket->load();
544 baskets.push(basket);
546 if(file.open(QIODevice::ReadOnly))
548 Q3TextStream stream(&file);
549 uint level = 0;
550 QString name;
551 QString line;
552 QStringList links;
553 QStringList descriptions;
555 stream.setEncoding(Q3TextStream::UnicodeUTF8);
556 while(1)
558 line = stream.readLine();
560 if(line.startsWith("\\NewEntry") ||
561 line.startsWith("\\CurrentEntry") || stream.atEnd())
563 while(level + 1 < baskets.count())
564 baskets.pop();
565 if(level + 1 > baskets.count())
566 baskets.push(basket);
568 if(!name.isEmpty())
570 if((level == 0 && hierarchy == 1) ||
571 (hierarchy == 0))
573 BasketFactory::newBasket(/*icon=*/"knowit",
574 /*name=*/name,
575 /*backgroundImage=*/"",
576 /*backgroundColor=*/QColor(),
577 /*textColor=*/QColor(),
578 /*templateName=*/"1column",
579 /*createIn=*/baskets.top());
580 basket = Global::bnpView->currentBasket();
581 basket->load();
584 if(!text.stripWhiteSpace().isEmpty() ||
585 hierarchy == 2 ||
586 (hierarchy == 1 && level > 0))
588 insertTitledNote(basket, name, text, Qt::RichText);
590 for(uint j = 0; j < links.count(); ++j)
592 Note* link;
593 if(descriptions.count() < j+1 || descriptions[j].isEmpty())
594 link = NoteFactory::createNoteLink(links[j], basket);
595 else
596 link = NoteFactory::createNoteLink(links[j],
597 descriptions[j], basket);
598 basket->insertCreatedNote(link);
600 finishImport(basket);
602 if(stream.atEnd())
603 break;
605 int i = line.find("Entry") + 6;
606 int n = line.find(' ', i);
607 level = line.mid(i, n - i).toInt();
608 name = line.mid(n+1);
609 text = "";
610 links.clear();
611 descriptions.clear();
613 else if(line.startsWith("\\Link"))
615 links.append(line.mid(6));
617 else if(line.startsWith("\\Descr"))
619 while(descriptions.count() < links.count() - 1)
620 descriptions.append("");
621 descriptions.append(line.mid(7));
623 else
625 text += line + "\n";
628 file.close();
633 void SoftwareImporters::importTuxCards()
635 QString fileName = KFileDialog::getOpenFileName(":ImportTuxCards", "*|All files");
636 if (fileName.isEmpty())
637 return;
639 TreeImportDialog dialog;
640 if (dialog.exec() == QDialog::Rejected)
641 return;
643 int hierarchy = dialog.choice();
645 QDomDocument *document = XMLWork::openFile("tuxcards_data_file"/*"InformationCollection"*/, fileName);
646 if (document == 0) {
647 KMessageBox::error(0, i18n("Can not import that file. It is either corrupted or not a TuxCards file."), i18n("Bad File Format"));
648 return;
651 QDomElement collection = document->documentElement();
652 int remainingHierarchy = (hierarchy == 0 ? 65000 : 3 - hierarchy);
653 importTuxCardsNode(collection, /*parentBasket=*/0, /*parentNote=*/0, remainingHierarchy);
656 // TODO: <InformationElement isOpen="true" isEncripted="false"
658 void SoftwareImporters::importTuxCardsNode(const QDomElement &element, Basket *parentBasket, Note *parentNote, int remainingHierarchy)
660 for (QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling()) {
661 QDomElement e = n.toElement();
662 if (e.isNull() || e.tagName() != "InformationElement") // Cannot handle that!
663 continue;
665 QString icon = e.attribute("iconFileName");
666 QString name = XMLWork::getElementText(e, "Description");
667 QString content = XMLWork::getElementText(e, "Information");
668 bool isRichText = (e.attribute("informationFormat") == "RTF");
669 bool isEncrypted = (e.attribute("isEncripted") == "true");
670 if (icon.isEmpty() || icon == "none")
671 icon = "tuxcards";
672 Note *nContent;
674 if (isEncrypted) {
675 KMessageBox::information(0, i18n("A note is encrypted. The importer does not yet support encrypted notes. Please remove the encryption with TuxCards and re-import the file."), i18n("Encrypted Notes not Supported Yet"));
676 isRichText = true;
677 content = i18n("<font color='red'><b>Encrypted note.</b><br>The importer do not support encrypted notes yet. Please remove the encryption with TuxCards and re-import the file.</font>");
680 if (remainingHierarchy > 0) {
681 BasketFactory::newBasket(icon, name, /*backgroundImage=*/"", /*backgroundColor=*/QColor(), /*textColor=*/QColor(), /*templateName=*/"1column", parentBasket);
682 Basket *basket = Global::bnpView->currentBasket();
683 basket->load();
685 if (isRichText)
686 nContent = NoteFactory::createNoteHtml(content, basket);
687 else
688 nContent = NoteFactory::createNoteText(content, basket);
689 basket->insertNote(nContent, basket->firstNote(), Note::BottomColumn, QPoint(), /*animate=*/false);
691 importTuxCardsNode(e, basket, 0, remainingHierarchy - 1);
692 finishImport(basket);
693 } else {
694 Note *nGroup = insertTitledNote(parentBasket, name, content, (isRichText ? Qt::RichText : Qt::PlainText), parentNote);
695 importTuxCardsNode(e, parentBasket, nGroup, remainingHierarchy - 1);
700 #include "softwareimporters.moc"