void HtmlBookmarksImporter::processElement(const QWebElement &element) { QWebElement entryElement(element.findFirst(QLatin1String("dt, hr"))); while (!entryElement.isNull()) { if (entryElement.tagName().toLower() == QLatin1String("hr")) { BookmarksManager::addBookmark(BookmarksModel::SeparatorBookmark, {}, getCurrentFolder()); ++m_currentAmount; emit importProgress(BookmarksImport, m_totalAmount, m_currentAmount); } else { BookmarksModel::BookmarkType type(BookmarksModel::UnknownBookmark); QWebElement matchedElement(entryElement.findFirst(QLatin1String("dt > h3"))); if (matchedElement.isNull()) { matchedElement = entryElement.findFirst(QLatin1String("dt > a")); if (!matchedElement.isNull()) { type = (matchedElement.hasAttribute(QLatin1String("FEEDURL")) ? BookmarksModel::FeedBookmark : BookmarksModel::UrlBookmark); } } else { type = BookmarksModel::FolderBookmark; } if (type != BookmarksModel::UnknownBookmark && !matchedElement.isNull()) { QMap<int, QVariant> metaData({{BookmarksModel::TitleRole, matchedElement.toPlainText()}}); const bool isUrlBookmark(type == BookmarksModel::UrlBookmark || BookmarksModel::FeedBookmark); if (isUrlBookmark) { const QUrl url(matchedElement.attribute(QLatin1String("HREF"))); if (!areDuplicatesAllowed() && BookmarksManager::hasBookmark(url)) { entryElement = entryElement.nextSibling(); continue; } metaData[BookmarksModel::UrlRole] = url; } if (matchedElement.hasAttribute(QLatin1String("SHORTCUTURL"))) { const QString keyword(matchedElement.attribute(QLatin1String("SHORTCUTURL"))); if (!keyword.isEmpty() && !BookmarksManager::hasKeyword(keyword)) { metaData[BookmarksModel::KeywordRole] = keyword; } } if (matchedElement.hasAttribute(QLatin1String("ADD_DATE"))) { const QDateTime dateTime(getDateTime(matchedElement, QLatin1String("ADD_DATE"))); if (dateTime.isValid()) { metaData[BookmarksModel::TimeAddedRole] = dateTime; metaData[BookmarksModel::TimeModifiedRole] = dateTime; } } if (matchedElement.hasAttribute(QLatin1String("LAST_MODIFIED"))) { const QDateTime dateTime(getDateTime(matchedElement, QLatin1String("LAST_MODIFIED"))); if (dateTime.isValid()) { metaData[BookmarksModel::TimeModifiedRole] = dateTime; } } if (isUrlBookmark && matchedElement.hasAttribute(QLatin1String("LAST_VISITED"))) { const QDateTime dateTime(getDateTime(matchedElement, QLatin1String("LAST_VISITED"))); if (dateTime.isValid()) { metaData[BookmarksModel::TimeVisitedRole] = dateTime; } } BookmarksModel::Bookmark *bookmark(BookmarksManager::addBookmark(type, metaData, getCurrentFolder())); ++m_currentAmount; emit importProgress(BookmarksImport, m_totalAmount, m_currentAmount); if (type == BookmarksModel::FolderBookmark) { setCurrentFolder(bookmark); processElement(entryElement); } if (entryElement.nextSibling().tagName().toLower() == QLatin1String("dd")) { bookmark->setItemData(entryElement.nextSibling().toPlainText(), BookmarksModel::DescriptionRole); entryElement = entryElement.nextSibling(); } } } entryElement = entryElement.nextSibling(); } goToParent(); }
bool OperaBookmarksImporter::import(const QString &path) { QFile file(getSuggestedPath(path)); if (!file.open(QIODevice::ReadOnly)) { emit importFinished(BookmarksImport, FailedImport, 0); return false; } QTextStream stream(&file); stream.setCodec("UTF-8"); QString line(stream.readLine()); if (line != QLatin1String("Opera Hotlist version 2.0")) { emit importFinished(BookmarksImport, FailedImport, 0); return false; } emit importStarted(BookmarksImport, -1); if (m_optionsWidget) { if (m_optionsWidget->hasToRemoveExisting()) { removeAllBookmarks(); if (m_optionsWidget->isImportingIntoSubfolder()) { setImportFolder(BookmarksManager::addBookmark(BookmarksModel::FolderBookmark, {{BookmarksModel::TitleRole, m_optionsWidget->getSubfolderName()}}, BookmarksManager::getModel()->getRootItem())); } else { setImportFolder(BookmarksManager::getModel()->getRootItem()); } } else { setAllowDuplicates(m_optionsWidget->areDuplicatesAllowed()); setImportFolder(m_optionsWidget->getTargetFolder()); } } const int estimatedAmount((file.size() > 0) ? (file.size() / 250) : 0); int totalAmount(0); BookmarksManager::getModel()->beginImport(getImportFolder(), estimatedAmount, qMin(estimatedAmount, 100)); BookmarksItem *bookmark(nullptr); OperaBookmarkEntry type(NoEntry); bool isHeader(true); while (!stream.atEnd()) { line = stream.readLine(); if (isHeader && (line.isEmpty() || line.at(0) != QLatin1Char('#'))) { continue; } isHeader = false; if (line.startsWith(QLatin1String("#URL"))) { bookmark = BookmarksManager::addBookmark(BookmarksModel::UrlBookmark, {}, getCurrentFolder()); type = UrlEntry; ++totalAmount; } else if (line.startsWith(QLatin1String("#FOLDER"))) { bookmark = BookmarksManager::addBookmark(BookmarksModel::FolderBookmark, {}, getCurrentFolder()); type = FolderStartEntry; ++totalAmount; } else if (line.startsWith(QLatin1String("#SEPERATOR"))) { bookmark = BookmarksManager::addBookmark(BookmarksModel::SeparatorBookmark, {}, getCurrentFolder()); type = SeparatorEntry; ++totalAmount; } else if (line == QLatin1String("-")) { type = FolderEndEntry; } else if (line.startsWith(QLatin1String("\tURL=")) && bookmark) { const QUrl url(line.section(QLatin1Char('='), 1, -1)); if (!areDuplicatesAllowed() && BookmarksManager::hasBookmark(url)) { bookmark->remove(); bookmark = nullptr; } else { bookmark->setData(url, BookmarksModel::UrlRole); } } else if (line.startsWith(QLatin1String("\tNAME=")) && bookmark) { bookmark->setData(line.section(QLatin1Char('='), 1, -1), BookmarksModel::TitleRole); } else if (line.startsWith(QLatin1String("\tDESCRIPTION=")) && bookmark) { bookmark->setData(line.section(QLatin1Char('='), 1, -1).replace(QLatin1String("\x02\x02"), QLatin1String("\n")), BookmarksModel::DescriptionRole); } else if (line.startsWith(QLatin1String("\tSHORT NAME=")) && bookmark) { const QString keyword(line.section(QLatin1Char('='), 1, -1)); if (!BookmarksManager::hasKeyword(keyword)) { bookmark->setData(keyword, BookmarksModel::KeywordRole); } } else if (line.startsWith(QLatin1String("\tCREATED=")) && bookmark) { bookmark->setData(QDateTime::fromTime_t(line.section(QLatin1Char('='), 1, -1).toUInt()), BookmarksModel::TimeAddedRole); } else if (line.startsWith(QLatin1String("\tVISITED=")) && bookmark) { bookmark->setData(QDateTime::fromTime_t(line.section(QLatin1Char('='), 1, -1).toUInt()), BookmarksModel::TimeVisitedRole); } else if (line.isEmpty()) { if (bookmark) { if (type == FolderStartEntry) { setCurrentFolder(bookmark); } bookmark = nullptr; } else if (type == FolderEndEntry) { goToParent(); } type = NoEntry; } } BookmarksManager::getModel()->endImport(); emit importFinished(BookmarksImport, SuccessfullImport, totalAmount); file.close(); return true; }