9
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
*
|
|
5 |
* This program is free software: you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU Lesser General Public License as published by
|
|
7 |
* the Free Software Foundation, version 2.1 of the License.
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU Lesser General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU Lesser General Public License
|
|
15 |
* along with this program. If not,
|
|
16 |
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
|
|
17 |
*
|
|
18 |
*/
|
|
19 |
|
|
20 |
#include <QString>
|
|
21 |
#include <QXmlStreamWriter>
|
|
22 |
#include <QIODevice>
|
|
23 |
#include <QDebug>
|
|
24 |
|
|
25 |
#include "xbelwriter.h"
|
|
26 |
#include "BookmarksManager.h"
|
|
27 |
#include "BookmarkFav.h"
|
|
28 |
#include "BookmarkResults.h"
|
|
29 |
#include "TagResults.h"
|
|
30 |
|
|
31 |
|
|
32 |
XbelWriter::XbelWriter(BookmarksManager *bmgr)
|
|
33 |
: m_bmgr(bmgr), m_xml(new QXmlStreamWriter())
|
|
34 |
{
|
|
35 |
}
|
|
36 |
|
|
37 |
XbelWriter::~XbelWriter() {
|
|
38 |
if(m_xml) delete m_xml;
|
|
39 |
}
|
|
40 |
|
|
41 |
bool XbelWriter::writeFile(QIODevice *device)
|
|
42 |
{
|
|
43 |
bool retVal = false;
|
|
44 |
if(device && m_bmgr) {
|
|
45 |
m_xml->setDevice(device);
|
|
46 |
m_xml->setAutoFormatting(true);
|
|
47 |
|
|
48 |
m_xml->writeStartDocument();
|
|
49 |
m_xml->writeDTD("<!DOCTYPE xbel>");
|
|
50 |
m_xml->writeStartElement("xbel");
|
|
51 |
m_xml->writeAttribute("version", "1.0");
|
|
52 |
|
|
53 |
BookmarkResults *bookmarks = m_bmgr->findAllBookmarks();
|
|
54 |
if(bookmarks) {
|
|
55 |
while (BookmarkFav *bm = bookmarks->nextBookmark()) {
|
|
56 |
if(!bm->url().isEmpty())
|
|
57 |
writeBookmark(bm);
|
|
58 |
delete bm;
|
|
59 |
bm = NULL;
|
|
60 |
}
|
|
61 |
delete bookmarks;
|
|
62 |
}
|
|
63 |
m_xml->writeEndElement(); // xbel
|
|
64 |
m_xml->writeEndDocument();
|
|
65 |
retVal = true;
|
|
66 |
}
|
|
67 |
return retVal;
|
|
68 |
}
|
|
69 |
|
|
70 |
void XbelWriter::writeBookmark(BookmarkFav *bm)
|
|
71 |
{
|
|
72 |
m_xml->writeStartElement("bookmark");
|
|
73 |
m_xml->writeAttribute("href", bm->url());
|
|
74 |
m_xml->writeTextElement("title", bm->title());
|
|
75 |
writeTags(bm->id());
|
|
76 |
m_xml->writeEndElement(); // bookmark
|
|
77 |
}
|
|
78 |
|
|
79 |
void XbelWriter::writeTags(int bmId)
|
|
80 |
{
|
|
81 |
if(bmId > 0) {
|
|
82 |
|
|
83 |
TagResults * tr = m_bmgr->findTagsByBookmark(bmId);
|
|
84 |
|
|
85 |
if(tr) {
|
|
86 |
m_xml->writeStartElement("info");
|
|
87 |
m_xml->writeStartElement("metadata");
|
|
88 |
m_xml->writeAttribute("owner", "http://www.nokia.com");
|
|
89 |
m_xml->writeStartElement("tags");
|
|
90 |
try {
|
|
91 |
while(QString * tag = tr->nextTag()) {
|
|
92 |
m_xml->writeTextElement("tag", *tag);
|
|
93 |
delete tag;
|
|
94 |
tag = NULL;
|
|
95 |
}
|
|
96 |
} catch(...) {
|
|
97 |
qDebug() << "Exception Thrown while writing tags.";
|
|
98 |
}
|
|
99 |
|
|
100 |
m_xml->writeEndElement(); // tags
|
|
101 |
m_xml->writeEndElement(); // metadata
|
|
102 |
m_xml->writeEndElement(); // info
|
|
103 |
delete tr;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|