OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / xbelsupport.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "xbelsupport.h"
34 #include "bookmarkmanager.h"
35
36 #include <QtCore/QCoreApplication>
37
38 using namespace Help::Internal;
39
40 struct Bookmark {
41     QString title;
42     QString url;
43     bool folded;
44 };
45
46 XbelWriter::XbelWriter(BookmarkModel *model)
47     : QXmlStreamWriter()
48     , treeModel(model)
49 {
50     setAutoFormatting(true);
51 }
52
53 void XbelWriter::writeToFile(QIODevice *device)
54 {
55     setDevice(device);
56
57     writeStartDocument();
58     writeDTD(QLatin1String("<!DOCTYPE xbel>"));
59     writeStartElement(QLatin1String("xbel"));
60     writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
61
62     QStandardItem *root = treeModel->invisibleRootItem();
63     for (int i = 0; i < root->rowCount(); ++i)
64         writeData(root->child(i));
65
66     writeEndDocument();
67 }
68
69 void XbelWriter::writeData(QStandardItem *child)
70 {
71     Bookmark entry;
72     entry.title = child->data(Qt::DisplayRole).toString();
73     entry.url = child->data(Qt::UserRole + 10).toString();
74
75     if (entry.url == QLatin1String("Folder")) {
76         writeStartElement(QLatin1String("folder"));
77
78         entry.folded = !child->data(Qt::UserRole + 11).toBool();
79         writeAttribute(QLatin1String("folded"),
80             entry.folded ? QLatin1String("yes") : QLatin1String("no"));
81
82         writeTextElement(QLatin1String("title"), entry.title);
83
84         for (int i = 0; i < child->rowCount(); ++i)
85             writeData(child->child(i));
86
87         writeEndElement();
88     } else {
89         writeStartElement(QLatin1String("bookmark"));
90         writeAttribute(QLatin1String("href"), entry.url);
91         writeTextElement(QLatin1String("title"), entry.title);
92         writeEndElement();
93     }
94 }
95
96
97 // #pragma mark -- XbelReader
98
99
100 XbelReader::XbelReader(BookmarkModel *tree, BookmarkModel *list)
101     : QXmlStreamReader()
102     , treeModel(tree)
103     , listModel(list)
104 {
105     bookmarkIcon = QIcon(QLatin1String(":/help/images/bookmark.png"));
106     folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon);
107 }
108
109 bool XbelReader::readFromFile(QIODevice *device)
110 {
111     setDevice(device);
112
113     while (!atEnd()) {
114         readNext();
115
116         if (isStartElement()) {
117             if (name() == QLatin1String("xbel")
118                 && attributes().value(QLatin1String("version"))
119                     == QLatin1String("1.0")) {
120                 readXBEL();
121             } else {
122                 raiseError(QCoreApplication::translate("Help::Internal::XbelReader", "The file is not an XBEL version 1.0 file."));
123             }
124         }
125     }
126
127     return !error();
128 }
129
130 void XbelReader::readXBEL()
131 {
132     while (!atEnd()) {
133         readNext();
134
135         if (isEndElement())
136             break;
137
138         if (isStartElement()) {
139             if (name() == QLatin1String("folder"))
140                 readFolder(0);
141             else if (name() == QLatin1String("bookmark"))
142                 readBookmark(0);
143             else
144                 readUnknownElement();
145         }
146     }
147 }
148
149 void XbelReader::readUnknownElement()
150 {
151     while (!atEnd()) {
152         readNext();
153
154         if (isEndElement())
155             break;
156
157         if (isStartElement())
158             readUnknownElement();
159     }
160 }
161
162 void XbelReader::readFolder(QStandardItem *item)
163 {
164     QStandardItem *folder = createChildItem(item);
165     folder->setIcon(folderIcon);
166     folder->setData(QLatin1String("Folder"), Qt::UserRole + 10);
167
168     bool expanded =
169         (attributes().value(QLatin1String("folded")) != QLatin1String("no"));
170     folder->setData(expanded, Qt::UserRole + 11);
171
172     while (!atEnd()) {
173         readNext();
174
175         if (isEndElement())
176             break;
177
178         if (isStartElement()) {
179             if (name() == QLatin1String("title"))
180                 folder->setText(readElementText());
181             else if (name() == QLatin1String("folder"))
182                 readFolder(folder);
183             else if (name() == QLatin1String("bookmark"))
184                 readBookmark(folder);
185             else
186                 readUnknownElement();
187         }
188     }
189 }
190
191 void XbelReader::readBookmark(QStandardItem *item)
192 {
193     QStandardItem *bookmark = createChildItem(item);
194     bookmark->setIcon(bookmarkIcon);
195     bookmark->setText(QCoreApplication::translate("Help::Internal::XbelReader", "Unknown title"));
196     bookmark->setData(attributes().value(QLatin1String("href")).toString(),
197         Qt::UserRole + 10);
198
199     while (!atEnd()) {
200         readNext();
201
202         if (isEndElement())
203             break;
204
205         if (isStartElement()) {
206             if (name() == QLatin1String("title"))
207                 bookmark->setText(readElementText());
208             else
209                 readUnknownElement();
210         }
211     }
212
213     listModel->appendRow(bookmark->clone());
214 }
215
216 QStandardItem *XbelReader::createChildItem(QStandardItem *item)
217 {
218     QStandardItem *childItem = new QStandardItem();
219     childItem->setEditable(false);
220
221     if (item)
222         item->appendRow(childItem);
223     else
224         treeModel->appendRow(childItem);
225
226     return childItem;
227 }