void BookmarksManager::load() { if (m_loaded) return; m_loaded = true; QString dir = QDesktopServices::storageLocation(QDesktopServices::DataLocation); QString bookmarkFile = dir + QLatin1String("/bookmarks.xbel"); if (!QFile::exists(bookmarkFile)) bookmarkFile = QLatin1String(":defaultbookmarks.xbel"); XbelReader reader; m_bookmarkRootNode = reader.read(bookmarkFile); if (reader.error() != QXmlStreamReader::NoError) { QMessageBox::warning(0, QLatin1String("Loading Bookmark"), tr("Error when loading bookmarks on line %1, column %2:\n" "%3").arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.errorString())); } QList<BookmarkNode*> others; for (int i = m_bookmarkRootNode->children().count() - 1; i >= 0; --i) { BookmarkNode *node = m_bookmarkRootNode->children().at(i); if (node->type() == BookmarkNode::Folder) { // Automatically convert if ((node->title == tr("Toolbar Bookmarks") || node->title == QLatin1String(BOOKMARKBAR)) && !m_toolbar) { node->title = tr(BOOKMARKBAR); m_toolbar = node; } // Automatically convert if ((node->title == tr("Menu") || node->title == QLatin1String(BOOKMARKMENU)) && !m_menu) { node->title = tr(BOOKMARKMENU); m_menu = node; } } else { others.append(node); } m_bookmarkRootNode->remove(node); } Q_ASSERT(m_bookmarkRootNode->children().count() == 0); if (!m_toolbar) { m_toolbar = new BookmarkNode(BookmarkNode::Folder, m_bookmarkRootNode); m_toolbar->title = tr(BOOKMARKBAR); } else { m_bookmarkRootNode->add(m_toolbar); } if (!m_menu) { m_menu = new BookmarkNode(BookmarkNode::Folder, m_bookmarkRootNode); m_menu->title = tr(BOOKMARKMENU); } else { m_bookmarkRootNode->add(m_menu); } for (int i = 0; i < others.count(); ++i) m_menu->add(others.at(i)); }
QModelIndex BookmarksModel::index(BookmarkNode *node) const { BookmarkNode *parent = node->parent(); if (!parent) return QModelIndex(); return createIndex(parent->children().indexOf(node), 0, node); }
QStringList BookmarksManager::find_tag_urls(BookmarkNode *start_node, const QString& tag) { if (!m_loaded) load(); if (!start_node) start_node = m_bookmarkRootNode; QStringList tags; for (int i = start_node->children().count() - 1; i >= 0; --i) { BookmarkNode *node = start_node->children().at(i); if (node) { if (node->type() == BookmarkNode::Bookmark) { QStringList node_tags = node->tags.split(QRegExp("\\W+"), QString::SkipEmptyParts); foreach(QString node_tag, node_tags) { if (tag.toLower() == node_tag.toLower()) { tags.append(node->url); break; } } } if (node->type() == BookmarkNode::Folder) { tags += find_tag_urls(node, tag); } } }
QModelIndex BookmarksModel::index(int row, int column, const QModelIndex &parent) const { if (row < 0 || column < 0 || row >= rowCount(parent) || column >= columnCount(parent)) return QModelIndex(); // get the parent node BookmarkNode *parentNode = node(parent); return createIndex(row, column, parentNode->children().at(row)); }
bool BookmarksModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) { if (action == Qt::IgnoreAction) return true; if (column > 0) return false; BookmarkNode *parentNode = node(parent); if (!data->hasFormat(MIMETYPE)) { if (!data->hasUrls()) return false; BookmarkNode *node = new BookmarkNode(BookmarkNode::Bookmark, parentNode); node->url = QString::fromUtf8(data->urls().at(0).toEncoded()); if (data->hasText()) node->title = data->text(); else node->title = node->url; m_bookmarksManager->addBookmark(parentNode, node, row); return true; } QByteArray ba = data->data(MIMETYPE); QDataStream stream(&ba, QIODevice::ReadOnly); if (stream.atEnd()) return false; QUndoStack *undoStack = m_bookmarksManager->undoRedoStack(); undoStack->beginMacro(QLatin1String("Move Bookmarks")); while (!stream.atEnd()) { QByteArray encodedData; stream >> encodedData; QBuffer buffer(&encodedData); buffer.open(QBuffer::ReadOnly); XbelReader reader; BookmarkNode *rootNode = reader.read(&buffer); QList<BookmarkNode*> children = rootNode->children(); for (int i = 0; i < children.count(); ++i) { BookmarkNode *bookmarkNode = children.at(i); rootNode->remove(bookmarkNode); row = qMax(0, row); m_bookmarksManager->addBookmark(parentNode, bookmarkNode, row); m_endMacro = true; } delete rootNode; } return true; }
void BookmarksModel::entryAdded(BookmarkNode *item) { Q_ASSERT(item && item->parent()); int row = item->parent()->children().indexOf(item); BookmarkNode *parent = item->parent(); // item was already added so remove beore beginInsertRows is called parent->remove(item); beginInsertRows(index(parent), row, row); parent->add(item, row); endInsertRows(); }
void BookmarksManager::removeBookmark(BookmarkNode *node) { if (!m_loaded) return; Q_ASSERT(node); BookmarkNode *parent = node->parent(); int row = parent->children().indexOf(node); RemoveBookmarksCommand *command = new RemoveBookmarksCommand(this, parent, row); m_commands.push(command); }
Qt::ItemFlags BookmarksModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; BookmarkNode *bookmarkNode = node(index); if (bookmarkNode != m_bookmarksManager->menu() && bookmarkNode != m_bookmarksManager->toolbar()) { flags |= Qt::ItemIsDragEnabled; if (bookmarkNode->type() != BookmarkNode::Separator) flags |= Qt::ItemIsEditable; } if (hasChildren(index)) flags |= Qt::ItemIsDropEnabled; return flags; }
// This will be called before each test function is executed. void tst_AddBookmarkDialog::init() { BookmarksManager *manager = BrowserApplication::bookmarksManager(); BookmarkNode *root = manager->bookmarks(); QList<BookmarkNode*> nodes = root->children(); BookmarkNode *menu = nodes[0]; BookmarkNode *toolbar = nodes[1]; while (!menu->children().isEmpty()) manager->removeBookmark(menu->children().first()); while (!toolbar->children().isEmpty()) manager->removeBookmark(toolbar->children().first()); }
void BookmarksManager::importBookmarks() { QString fileName = QFileDialog::getOpenFileName(0, tr("Open File"), QString(), tr("XBEL (*.xbel *.xml)")); if (fileName.isEmpty()) return; XbelReader reader; BookmarkNode *importRootNode = reader.read(fileName); if (reader.error() != QXmlStreamReader::NoError) { QMessageBox::warning(0, QLatin1String("Loading Bookmark"), tr("Error when loading bookmarks on line %1, column %2:\n" "%3").arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.errorString())); } importRootNode->setType(BookmarkNode::Folder); importRootNode->title = (tr("Imported %1").arg(QDate::currentDate().toString(Qt::SystemLocaleShortDate))); addBookmark(menu(), importRootNode); }
bool BookmarksModel::removeRows(int row, int count, const QModelIndex &parent) { if (row < 0 || count <= 0 || row + count > rowCount(parent)) return false; BookmarkNode *bookmarkNode = node(parent); for (int i = row + count - 1; i >= row; --i) { BookmarkNode *node = bookmarkNode->children().at(i); if (node == m_bookmarksManager->menu() || node == m_bookmarksManager->toolbar()) continue; m_bookmarksManager->removeBookmark(node); } if (m_endMacro) { m_bookmarksManager->undoRedoStack()->endMacro(); m_endMacro = false; } return true; }
void BookmarksManager::importBookmarks() { QString fileName = QFileDialog::getOpenFileName(0, tr("Open File"), QString(), tr("XBEL (*.xbel *.xml *.html)")); if (fileName.isEmpty()) return; XbelReader reader; BookmarkNode *importRootNode = 0; if (fileName.endsWith(QLatin1String(".html"))) { QString program = QLatin1String("htmlToXBel"); QStringList arguments; arguments << fileName; QProcess process; process.start(program, arguments); process.waitForFinished(-1); if (process.error() != QProcess::UnknownError) { QMessageBox::warning(0, QLatin1String("Loading Bookmark"), tr("Error when loading html bookmarks: %1\n").arg(process.errorString())); return; } importRootNode = reader.read(&process); } else { importRootNode = reader.read(fileName); } if (reader.error() != QXmlStreamReader::NoError) { QMessageBox::warning(0, QLatin1String("Loading Bookmark"), tr("Error when loading bookmarks on line %1, column %2:\n" "%3").arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.errorString())); delete importRootNode; return; } importRootNode->setType(BookmarkNode::Folder); importRootNode->title = (tr("Imported %1").arg(QDate::currentDate().toString(Qt::SystemLocaleShortDate))); addBookmark(menu(), importRootNode); }
Qt::ItemFlags BookmarksModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; BookmarkNode *node = this->node(index); BookmarkNode::Type type = node->type(); Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled; if (hasChildren(index)) flags |= Qt::ItemIsDropEnabled; if (node == m_bookmarksManager->menu() || node == m_bookmarksManager->toolbar()) return flags; flags |= Qt::ItemIsDragEnabled; if ((index.column() == 0 && type != BookmarkNode::Separator) || (index.column() == 1 && type == BookmarkNode::Bookmark)) flags |= Qt::ItemIsEditable; return flags; }
void tst_AddBookmarkDialog::addbookmarkdialog() { QFETCH(QString, url); QFETCH(QString, title); QFETCH(QDialogButtonBox::StandardButton, button); QFETCH(int, menuCount); QFETCH(int, toolbarCount); QFETCH(int, select); BookmarksManager *manager = BrowserApplication::bookmarksManager(); qRegisterMetaType<BookmarkNode *>("BookmarkNode *"); QSignalSpy spy(manager, SIGNAL(entryAdded(BookmarkNode *))); BookmarkNode *menu = manager->menu(); BookmarkNode *toolbar = manager->toolbar(); QCOMPARE(menu->children().count(), 0); QCOMPARE(toolbar->children().count(), 0); SubAddBookmarkDialog dialog(0, manager); dialog.setUrl(url); dialog.setTitle(title); QComboBox *combobox = dialog.findChild<QComboBox*>(); QVERIFY(combobox); if (select != -1) { combobox->setCurrentIndex(select); combobox->view()->setCurrentIndex(combobox->model()->index(select, 0)); } QDialogButtonBox *buttonBox = dialog.findChild<QDialogButtonBox*>(); QVERIFY(buttonBox); QPushButton *pushButton = buttonBox->button(button); pushButton->click(); QCOMPARE(spy.count(), menuCount + toolbarCount); QCOMPARE(menu->children().count(), menuCount); QCOMPARE(toolbar->children().count(), toolbarCount); BookmarkNode *node = 0; if (menuCount == 1) node = menu->children()[0]; if (toolbarCount == 1) node = toolbar->children()[0]; if (node) { QCOMPARE(node->title, title); QCOMPARE(node->url, url); } }
QModelIndex BookmarksModel::parent(const QModelIndex &index) const { if (!index.isValid()) return QModelIndex(); BookmarkNode *itemNode = node(index); BookmarkNode *parentNode = (itemNode ? itemNode->parent() : 0); if (!parentNode || parentNode == m_bookmarksManager->bookmarks()) return QModelIndex(); // get the parent's row BookmarkNode *grandParentNode = parentNode->parent(); int parentRow = grandParentNode->children().indexOf(parentNode); Q_ASSERT(parentRow >= 0); return createIndex(parentRow, 0, parentNode); }
void tst_Xbel::readProperly() { SubXbelReader reader; BookmarkNode *root = reader.read("all.xbel"); QCOMPARE(reader.error(), QXmlStreamReader::NoError); QList<BookmarkNode *>children = root->children(); QCOMPARE(children.count(), 4); // null folder QCOMPARE(children[0]->children().count(), 0); QCOMPARE(children[0]->title, QString()); QCOMPARE(children[0]->url, QString()); QCOMPARE(children[0]->expanded, false); // folded = no QCOMPARE(children[1]->children().count(), 0); QCOMPARE(children[1]->title, QString()); QCOMPARE(children[1]->url, QString()); QCOMPARE(children[1]->expanded, true); QCOMPARE(children[1]->type(), BookmarkNode::Folder); // folder with two bookmarks QCOMPARE(children[2]->children().count(), 2); QCOMPARE(children[2]->title, QString("Folder Title")); QCOMPARE(children[2]->url, QString()); QCOMPARE(children[2]->expanded, false); QCOMPARE(children[2]->type(), BookmarkNode::Folder); { children = children[2]->children(); QCOMPARE(children[0]->children().count(), 0); QCOMPARE(children[0]->title, QString("Title")); QCOMPARE(children[0]->url, QString("http://www.foo.com/")); QCOMPARE(children[0]->expanded, false); QCOMPARE(children[0]->type(), BookmarkNode::Bookmark); QCOMPARE(children[1]->children().count(), 0); QCOMPARE(children[1]->title, QString("Title 2")); QCOMPARE(children[1]->url, QString("http://www.bar.com/")); QCOMPARE(children[1]->expanded, false); QCOMPARE(children[1]->type(), BookmarkNode::Bookmark); } children = root->children(); QCOMPARE(children[3]->children().count(), 3); QCOMPARE(children[3]->title, QString("Has SubFolder")); QCOMPARE(children[3]->url, QString()); QCOMPARE(children[3]->expanded, true); QCOMPARE(children[3]->type(), BookmarkNode::Folder); { children = children[3]->children(); QCOMPARE(children[0]->children().count(), 0); QCOMPARE(children[0]->title, QString()); QCOMPARE(children[0]->url, QString()); QCOMPARE(children[0]->expanded, false); QCOMPARE(children[0]->type(), BookmarkNode::Separator); QCOMPARE(children[1]->children().count(), 3); QCOMPARE(children[1]->title, QString("SubFolder")); QCOMPARE(children[1]->url, QString()); QCOMPARE(children[1]->expanded, true); QCOMPARE(children[1]->type(), BookmarkNode::Folder); // children not checked QCOMPARE(children[2]->children().count(), 0); QCOMPARE(children[2]->title, QString()); QCOMPARE(children[2]->url, QString()); QCOMPARE(children[2]->expanded, false); QCOMPARE(children[2]->type(), BookmarkNode::Separator); } }
void BookmarksManager::load() { if (m_loaded) return; m_loaded = true; QString dir = BrowserApplication::dataLocation(); QString bookmarkFile = dir + QLatin1String("/bookmarks.xbel"); if (!QFile::exists(bookmarkFile)) #ifdef WINPE bookmarkFile = QLatin1String(":defaultbookmarks_winpe.xbel"); #else bookmarkFile = QLatin1String(":defaultbookmarks.xbel"); #endif XbelReader reader; m_bookmarkRootNode = reader.read(bookmarkFile); if (reader.error() != QXmlStreamReader::NoError) { QMessageBox::warning(0, tr("Loading Bookmark"), tr("Error when loading bookmarks on line %1, column %2:\n" "%3").arg(reader.lineNumber()).arg(reader.columnNumber()).arg(reader.errorString())); } BookmarkNode *toolbar = 0; BookmarkNode *menu = 0; QList<BookmarkNode*> others; for (int i = m_bookmarkRootNode->children().count() - 1; i >= 0; --i) { BookmarkNode *node = m_bookmarkRootNode->children().at(i); if (node->type() == BookmarkNode::Folder) { // Automatically convert if (node->title == ("Bookmarks Bar") && !toolbar) { node->title = tr("Bookmarks Bar"); } if (node->title == tr("Bookmarks Bar") && !toolbar) { toolbar = node; } // Automatically convert if (node->title == ("Bookmarks Menu") && !menu) { node->title = tr("Bookmarks Menu"); } if (node->title == tr("Bookmarks Menu") && !menu) { menu = node; } } else { others.append(node); } m_bookmarkRootNode->remove(node); } Q_ASSERT(m_bookmarkRootNode->children().count() == 0); if (!toolbar) { toolbar = new BookmarkNode(BookmarkNode::Folder, m_bookmarkRootNode); toolbar->title = tr("Bookmarks Bar"); } else { m_bookmarkRootNode->add(toolbar); } if (!menu) { menu = new BookmarkNode(BookmarkNode::Folder, m_bookmarkRootNode); menu->title = tr("Bookmarks Menu"); } else { m_bookmarkRootNode->add(menu); } for (int i = 0; i < others.count(); ++i) menu->add(others.at(i)); }