File indexing completed on 2024-05-19 16:41:36

0001 /*
0002     SPDX-FileCopyrightText: 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
0003     SPDX-FileCopyrightText: 2012 Marco Gulino <marco.gulino@xpeppers.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "konqueror.h"
0009 #include "bookmarkmatch.h"
0010 
0011 #include <QIcon>
0012 #include <QStack>
0013 #include <QUrl>
0014 
0015 #include <KBookmarkManager>
0016 #include <KIO/Global>
0017 
0018 QIcon KDEFavicon::iconFor(const QString &url)
0019 {
0020     const QString iconFile = KIO::favIconForUrl(QUrl(url));
0021     if (iconFile.isEmpty()) {
0022         return defaultIcon();
0023     }
0024     return QIcon::fromTheme(iconFile);
0025 }
0026 
0027 KDEFavicon::KDEFavicon(QObject *parent)
0028     : Favicon(parent)
0029 {
0030 }
0031 
0032 Konqueror::Konqueror(QObject *parent)
0033     : QObject(parent)
0034     , m_bookmarkManager(KBookmarkManager::userBookmarksManager())
0035     , m_favicon(new KDEFavicon(this))
0036 {
0037 }
0038 
0039 QList<BookmarkMatch> Konqueror::match(const QString &term, bool addEverything)
0040 {
0041     KBookmarkGroup bookmarkGroup = m_bookmarkManager->root();
0042 
0043     QList<BookmarkMatch> matches;
0044     QStack<KBookmarkGroup> groups;
0045 
0046     KBookmark bookmark = bookmarkGroup.first();
0047     while (!bookmark.isNull()) {
0048         //         if (!context.isValid()) {
0049         //             return;
0050         //         } TODO: restore?
0051 
0052         if (bookmark.isSeparator()) {
0053             bookmark = bookmarkGroup.next(bookmark);
0054             continue;
0055         }
0056 
0057         if (bookmark.isGroup()) { // descend
0058             // qDebug(kdbg_code) << "descending into" << bookmark.text();
0059             groups.push(bookmarkGroup);
0060             bookmarkGroup = bookmark.toGroup();
0061             bookmark = bookmarkGroup.first();
0062 
0063             while (bookmark.isNull() && !groups.isEmpty()) {
0064                 //                 if (!context.isValid()) {
0065                 //                     return;
0066                 //                 } TODO: restore?
0067 
0068                 bookmark = bookmarkGroup;
0069                 bookmarkGroup = groups.pop();
0070                 bookmark = bookmarkGroup.next(bookmark);
0071             }
0072 
0073             continue;
0074         }
0075 
0076         const QString url = bookmark.url().url();
0077         BookmarkMatch bookmarkMatch(m_favicon->iconFor(url), term, bookmark.text(), url);
0078         bookmarkMatch.addTo(matches, addEverything);
0079 
0080         bookmark = bookmarkGroup.next(bookmark);
0081         while (bookmark.isNull() && !groups.isEmpty()) {
0082             //             if (!context.isValid()) {
0083             //                 return;
0084             //             } // TODO: restore?
0085 
0086             bookmark = bookmarkGroup;
0087             bookmarkGroup = groups.pop();
0088             ////qDebug() << "ascending from" << bookmark.text() << "to" << bookmarkGroup.text();
0089             bookmark = bookmarkGroup.next(bookmark);
0090         }
0091     }
0092     return matches;
0093 }