From 8e70c556dd46b7295f17108953daa0025b4c69ab Mon Sep 17 00:00:00 2001 From: kh1 Date: Wed, 4 Nov 2009 14:52:29 +0100 Subject: Implement bookmark export/import. Task-number: QTBUG-4125 Reviewed-by: ck --- tools/assistant/tools/assistant/assistant.pro | 6 +- .../assistant/tools/assistant/assistant_images.qrc | 1 + .../assistant/tools/assistant/bookmarkmanager.cpp | 16 +- tools/assistant/tools/assistant/bookmarkmanager.h | 1 + .../assistant/tools/assistant/images/bookmark.png | Bin 0 -> 1266 bytes tools/assistant/tools/assistant/mainwindow.cpp | 51 ++++- tools/assistant/tools/assistant/mainwindow.h | 5 + tools/assistant/tools/assistant/xbelsupport.cpp | 234 +++++++++++++++++++++ tools/assistant/tools/assistant/xbelsupport.h | 87 ++++++++ 9 files changed, 390 insertions(+), 11 deletions(-) create mode 100644 tools/assistant/tools/assistant/images/bookmark.png create mode 100644 tools/assistant/tools/assistant/xbelsupport.cpp create mode 100644 tools/assistant/tools/assistant/xbelsupport.h diff --git a/tools/assistant/tools/assistant/assistant.pro b/tools/assistant/tools/assistant/assistant.pro index 1cbd1d3..00d0060 100644 --- a/tools/assistant/tools/assistant/assistant.pro +++ b/tools/assistant/tools/assistant/assistant.pro @@ -38,7 +38,8 @@ HEADERS += helpviewer.h \ remotecontrol.h \ cmdlineparser.h \ aboutdialog.h \ - qtdocinstaller.h + qtdocinstaller.h \ + xbelsupport.h win32 { HEADERS += remotecontrol_win.h @@ -59,7 +60,8 @@ SOURCES += helpviewer.cpp \ remotecontrol.cpp \ cmdlineparser.cpp \ aboutdialog.cpp \ - qtdocinstaller.cpp + qtdocinstaller.cpp \ + xbelsupport.cpp FORMS += topicchooser.ui \ preferencesdialog.ui \ diff --git a/tools/assistant/tools/assistant/assistant_images.qrc b/tools/assistant/tools/assistant/assistant_images.qrc index 58e03b5..34918c0 100644 --- a/tools/assistant/tools/assistant/assistant_images.qrc +++ b/tools/assistant/tools/assistant/assistant_images.qrc @@ -4,6 +4,7 @@ images/assistant-128.png images/assistant.png images/wrap.png + images/bookmark.png #mac images/mac/addtab.png images/mac/book.png diff --git a/tools/assistant/tools/assistant/bookmarkmanager.cpp b/tools/assistant/tools/assistant/bookmarkmanager.cpp index 9cccd82..881525c 100644 --- a/tools/assistant/tools/assistant/bookmarkmanager.cpp +++ b/tools/assistant/tools/assistant/bookmarkmanager.cpp @@ -301,7 +301,7 @@ bool BookmarkDialog::eventFilter(QObject *object, QEvent *e) } -// #pragma mark -- BookmarkWidget +// BookmarkWidget BookmarkWidget::BookmarkWidget(BookmarkManager *manager, QWidget *parent, @@ -587,7 +587,7 @@ bool BookmarkWidget::eventFilter(QObject *object, QEvent *e) } -// #pragma mark -- BookmarkModel +// BookmarkModel BookmarkModel::BookmarkModel(int rows, int columns, QObject *parent) @@ -615,7 +615,7 @@ Qt::ItemFlags BookmarkModel::flags(const QModelIndex &index) const } -// #pragma mark -- BookmarkManager +// BookmarkManager BookmarkManager::BookmarkManager(QHelpEngineCore *_helpEngine) @@ -624,6 +624,7 @@ BookmarkManager::BookmarkManager(QHelpEngineCore *_helpEngine) , helpEngine(_helpEngine) { folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon); + bookmarkIcon = QIcon(QLatin1String(":/trolltech/assistant/images/bookmark.png")); connect(treeModel, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(itemChanged(QStandardItem*))); @@ -733,6 +734,7 @@ void BookmarkManager::addNewBookmark(const QModelIndex &index, { QStandardItem *item = new QStandardItem(name); item->setEditable(false); + item->setIcon(bookmarkIcon); item->setData(false, Qt::UserRole + 11); item->setData(url, Qt::UserRole + 10); @@ -832,10 +834,12 @@ void BookmarkManager::setupBookmarkModels() } } - if (type == QLatin1String("Folder")) - item->setIcon(folderIcon); - else + if (type != QLatin1String("Folder")) { + item->setIcon(bookmarkIcon); listModel->appendRow(item->clone()); + } else { + item->setIcon(folderIcon); + } } } diff --git a/tools/assistant/tools/assistant/bookmarkmanager.h b/tools/assistant/tools/assistant/bookmarkmanager.h index aba7da0..fab8790 100644 --- a/tools/assistant/tools/assistant/bookmarkmanager.h +++ b/tools/assistant/tools/assistant/bookmarkmanager.h @@ -202,6 +202,7 @@ private: private: QString oldText; QIcon folderIcon; + QIcon bookmarkIcon; BookmarkModel *treeModel; BookmarkModel *listModel; diff --git a/tools/assistant/tools/assistant/images/bookmark.png b/tools/assistant/tools/assistant/images/bookmark.png new file mode 100644 index 0000000..57e57e3 Binary files /dev/null and b/tools/assistant/tools/assistant/images/bookmark.png differ diff --git a/tools/assistant/tools/assistant/mainwindow.cpp b/tools/assistant/tools/assistant/mainwindow.cpp index 2fc34d9..84b1407 100644 --- a/tools/assistant/tools/assistant/mainwindow.cpp +++ b/tools/assistant/tools/assistant/mainwindow.cpp @@ -52,6 +52,7 @@ #include "aboutdialog.h" #include "searchwidget.h" #include "qtdocinstaller.h" +#include "xbelsupport.h" #include #include @@ -76,6 +77,7 @@ #include #include #include +#include #include #include @@ -382,13 +384,17 @@ void MainWindow::checkInitState() void MainWindow::updateBookmarkMenu() { if (m_bookmarkManager) { + m_bookmarkMenu->removeAction(m_importBookmarkAction); + m_bookmarkMenu->removeAction(m_exportBookmarkAction); m_bookmarkMenu->removeAction(m_bookmarkMenuAction); - + m_bookmarkMenu->clear(); - + + m_bookmarkMenu->addAction(m_importBookmarkAction); + m_bookmarkMenu->addAction(m_exportBookmarkAction); m_bookmarkMenu->addAction(m_bookmarkMenuAction); m_bookmarkMenu->addSeparator(); - + m_bookmarkManager->fillBookmarkMenu(m_bookmarkMenu); } } @@ -541,6 +547,10 @@ void MainWindow::setupActions() << QKeySequence(Qt::CTRL + Qt::Key_PageUp)); m_bookmarkMenu = menuBar()->addMenu(tr("&Bookmarks")); + m_importBookmarkAction = m_bookmarkMenu->addAction(tr("Import..."), + this, SLOT(importBookmarks())); + m_exportBookmarkAction = m_bookmarkMenu->addAction(tr("Export..."), + this, SLOT(exportBookmarks())); m_bookmarkMenuAction = m_bookmarkMenu->addAction(tr("Add Bookmark..."), this, SLOT(addBookmark())); m_bookmarkMenuAction->setShortcut(tr("CTRL+D")); @@ -1045,4 +1055,39 @@ QString MainWindow::defaultHelpCollectionFileName() arg(QLatin1String(QT_VERSION_STR)); } +void MainWindow::importBookmarks() +{ + const QString &fileName = QFileDialog::getOpenFileName(0, tr("Open File"), + QDir::currentPath(), tr("Files (*.xbel)")); + + if (fileName.isEmpty()) + return; + + QFile file(fileName); + if (file.open(QIODevice::ReadOnly)) { + XbelReader reader(m_bookmarkManager->treeBookmarkModel(), + m_bookmarkManager->listBookmarkModel()); + reader.readFromFile(&file); + } +} + +void MainWindow::exportBookmarks() +{ + QString fileName = QFileDialog::getSaveFileName(0, tr("Save File"), + "untitled.xbel", tr("Files (*.xbel)")); + + QLatin1String suffix(".xbel"); + if (!fileName.endsWith(suffix)) + fileName.append(suffix); + + QFile file(fileName); + if (file.open(QIODevice::WriteOnly)) { + XbelWriter writer(m_bookmarkManager->treeBookmarkModel()); + writer.writeToFile(&file); + } else { + QMessageBox::information(this, tr("Qt Assistant"), + tr("Unable to save bookmarks."), tr("OK")); + } +} + QT_END_NAMESPACE diff --git a/tools/assistant/tools/assistant/mainwindow.h b/tools/assistant/tools/assistant/mainwindow.h index 6b858e9..8e7618a 100644 --- a/tools/assistant/tools/assistant/mainwindow.h +++ b/tools/assistant/tools/assistant/mainwindow.h @@ -122,6 +122,9 @@ private slots: void updateBookmarkMenu(); void showBookmark(QAction *action); + void importBookmarks(); + void exportBookmarks(); + private: bool initHelpDB(); void setupActions(); @@ -162,6 +165,8 @@ private: QMenu *m_toolBarMenu; QMenu *m_bookmarkMenu; QAction *m_bookmarkMenuAction; + QAction *m_importBookmarkAction; + QAction *m_exportBookmarkAction; CmdLineParser *m_cmdLine; diff --git a/tools/assistant/tools/assistant/xbelsupport.cpp b/tools/assistant/tools/assistant/xbelsupport.cpp new file mode 100644 index 0000000..545be6c --- /dev/null +++ b/tools/assistant/tools/assistant/xbelsupport.cpp @@ -0,0 +1,234 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "xbelsupport.h" +#include "bookmarkmanager.h" + +#include + +struct Bookmark { + QString title; + QString url; + bool folded; +}; + +XbelWriter::XbelWriter(BookmarkModel *model) + : QXmlStreamWriter() + , treeModel(model) +{ + setAutoFormatting(true); +} + +void XbelWriter::writeToFile(QIODevice *device) +{ + setDevice(device); + + writeStartDocument(); + writeDTD(QLatin1String("")); + writeStartElement(QLatin1String("xbel")); + writeAttribute(QLatin1String("version"), QLatin1String("1.0")); + + QStandardItem *root = treeModel->invisibleRootItem(); + for (int i = 0; i < root->rowCount(); ++i) + writeData(root->child(i)); + + writeEndDocument(); +} + +void XbelWriter::writeData(QStandardItem *child) +{ + Bookmark entry; + entry.title = child->data(Qt::DisplayRole).toString(); + entry.url = child->data(Qt::UserRole + 10).toString(); + + if (entry.url == QLatin1String("Folder")) { + writeStartElement(QLatin1String("folder")); + + entry.folded = !child->data(Qt::UserRole + 11).toBool(); + writeAttribute(QLatin1String("folded"), + entry.folded ? QLatin1String("yes") : QLatin1String("no")); + + writeTextElement(QLatin1String("title"), entry.title); + + for (int i = 0; i < child->rowCount(); ++i) + writeData(child->child(i)); + + writeEndElement(); + } else { + writeStartElement(QLatin1String("bookmark")); + writeAttribute(QLatin1String("href"), entry.url); + writeTextElement(QLatin1String("title"), entry.title); + writeEndElement(); + } +} + + +// XbelReader + + +XbelReader::XbelReader(BookmarkModel *tree, BookmarkModel *list) + : QXmlStreamReader() + , treeModel(tree) + , listModel(list) +{ + folderIcon = QApplication::style()->standardIcon(QStyle::SP_DirClosedIcon); + bookmarkIcon = QIcon(QLatin1String(":/trolltech/assistant/images/bookmark.png")); +} + +bool XbelReader::readFromFile(QIODevice *device) +{ + setDevice(device); + + while (!atEnd()) { + readNext(); + + if (isStartElement()) { + if (name() == QLatin1String("xbel") + && attributes().value(QLatin1String("version")) + == QLatin1String("1.0")) { + readXBEL(); + } else { + raiseError(QLatin1String("The file is not an XBEL version 1.0 file.")); + } + } + } + + return !error(); +} + +void XbelReader::readXBEL() +{ + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == QLatin1String("folder")) + readFolder(0); + else if (name() == QLatin1String("bookmark")) + readBookmark(0); + else + readUnknownElement(); + } + } +} + +void XbelReader::readUnknownElement() +{ + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) + readUnknownElement(); + } +} + +void XbelReader::readFolder(QStandardItem *item) +{ + QStandardItem *folder = createChildItem(item); + folder->setIcon(folderIcon); + folder->setData(QLatin1String("Folder"), Qt::UserRole + 10); + + bool expanded = + (attributes().value(QLatin1String("folded")) != QLatin1String("no")); + folder->setData(expanded, Qt::UserRole + 11); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == QLatin1String("title")) + folder->setText(readElementText()); + else if (name() == QLatin1String("folder")) + readFolder(folder); + else if (name() == QLatin1String("bookmark")) + readBookmark(folder); + else + readUnknownElement(); + } + } +} + +void XbelReader::readBookmark(QStandardItem *item) +{ + QStandardItem *bookmark = createChildItem(item); + bookmark->setIcon(bookmarkIcon); + bookmark->setText(QCoreApplication::tr("Unknown title")); + bookmark->setData(attributes().value(QLatin1String("href")).toString(), + Qt::UserRole + 10); + + while (!atEnd()) { + readNext(); + + if (isEndElement()) + break; + + if (isStartElement()) { + if (name() == QLatin1String("title")) + bookmark->setText(readElementText()); + else + readUnknownElement(); + } + } + + listModel->appendRow(bookmark->clone()); +} + +QStandardItem *XbelReader::createChildItem(QStandardItem *item) +{ + QStandardItem *childItem = new QStandardItem(); + childItem->setEditable(false); + + if (item) + item->appendRow(childItem); + else + treeModel->appendRow(childItem); + + return childItem; +} diff --git a/tools/assistant/tools/assistant/xbelsupport.h b/tools/assistant/tools/assistant/xbelsupport.h new file mode 100644 index 0000000..177b04b --- /dev/null +++ b/tools/assistant/tools/assistant/xbelsupport.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Assistant of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef XBELSUPPORT_H +#define XBELSUPPORT_H + +#include +#include + +QT_FORWARD_DECLARE_CLASS(QIODevice) +QT_FORWARD_DECLARE_CLASS(QStandardItem) + +class BookmarkModel; + +class XbelWriter : public QXmlStreamWriter +{ +public: + XbelWriter(BookmarkModel *model); + void writeToFile(QIODevice *device); + +private: + void writeData(QStandardItem *item); + +private: + BookmarkModel *treeModel; +}; + +class XbelReader : public QXmlStreamReader +{ +public: + XbelReader(BookmarkModel *tree, BookmarkModel *list); + bool readFromFile(QIODevice *device); + +private: + void readXBEL(); + void readUnknownElement(); + void readFolder(QStandardItem *item); + void readBookmark(QStandardItem *item); + QStandardItem* createChildItem(QStandardItem *item); + +private: + QIcon folderIcon; + QIcon bookmarkIcon; + + BookmarkModel *treeModel; + BookmarkModel *listModel; +}; + +#endif // XBELSUPPORT_H -- cgit v0.12