File indexing completed on 2024-05-12 04:57:56

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "bookmarkitem.h"
0019 #include "iconprovider.h"
0020 
0021 BookmarkItem::BookmarkItem(BookmarkItem::Type type, BookmarkItem* parent)
0022     : m_type(type)
0023     , m_parent(parent)
0024     , m_visitCount(0)
0025     , m_expanded(false)
0026     , m_sidebarExpanded(false)
0027 {
0028     if (m_parent) {
0029         parent->addChild(this);
0030     }
0031 }
0032 
0033 BookmarkItem::~BookmarkItem()
0034 {
0035     qDeleteAll(m_children);
0036 }
0037 
0038 BookmarkItem::Type BookmarkItem::type() const
0039 {
0040     return m_type;
0041 }
0042 
0043 void BookmarkItem::setType(BookmarkItem::Type type)
0044 {
0045     m_type = type;
0046 }
0047 
0048 bool BookmarkItem::isFolder() const
0049 {
0050     return m_type == Folder;
0051 }
0052 
0053 bool BookmarkItem::isUrl() const
0054 {
0055     return m_type == Url;
0056 }
0057 
0058 bool BookmarkItem::isSeparator() const
0059 {
0060     return m_type == Separator;
0061 }
0062 
0063 BookmarkItem* BookmarkItem::parent() const
0064 {
0065     return m_parent;
0066 }
0067 
0068 QList<BookmarkItem*> BookmarkItem::children() const
0069 {
0070     return m_children;
0071 }
0072 
0073 QIcon BookmarkItem::icon()
0074 {
0075     // Cache icon for 20 seconds
0076     const int iconCacheTime = 20 * 1000;
0077 
0078     switch (m_type) {
0079     case Url:
0080         if ((!m_iconTime.isValid()) || (m_iconTime.elapsed() > iconCacheTime)) {
0081             m_icon = IconProvider::iconForUrl(m_url);
0082             m_iconTime.restart();
0083         }
0084         return m_icon;
0085     case Folder:
0086         return IconProvider::standardIcon(QStyle::SP_DirIcon);
0087     default:
0088         return {};
0089     }
0090 }
0091 
0092 void BookmarkItem::setIcon(const QIcon &icon)
0093 {
0094     m_icon = icon;
0095 }
0096 
0097 QString BookmarkItem::urlString() const
0098 {
0099     return QString::fromUtf8(m_url.toEncoded());
0100 }
0101 
0102 QUrl BookmarkItem::url() const
0103 {
0104     return  m_url;
0105 }
0106 
0107 void BookmarkItem::setUrl(const QUrl &url)
0108 {
0109     m_url = url;
0110 }
0111 
0112 QString BookmarkItem::title() const
0113 {
0114     return m_title;
0115 }
0116 
0117 void BookmarkItem::setTitle(const QString &title)
0118 {
0119     m_title = title;
0120 }
0121 
0122 QString BookmarkItem::description() const
0123 {
0124     return m_description;
0125 }
0126 
0127 void BookmarkItem::setDescription(const QString &description)
0128 {
0129     m_description = description;
0130 }
0131 
0132 QString BookmarkItem::keyword() const
0133 {
0134     return m_keyword;
0135 }
0136 
0137 void BookmarkItem::setKeyword(const QString &keyword)
0138 {
0139     m_keyword = keyword;
0140 }
0141 
0142 int BookmarkItem::visitCount() const
0143 {
0144     return m_visitCount;
0145 }
0146 
0147 void BookmarkItem::setVisitCount(int count)
0148 {
0149     m_visitCount = count;
0150 }
0151 
0152 void BookmarkItem::updateVisitCount()
0153 {
0154     m_visitCount++;
0155 }
0156 
0157 bool BookmarkItem::isExpanded() const
0158 {
0159     return m_type == Root ? true : m_expanded;
0160 }
0161 
0162 void BookmarkItem::setExpanded(bool expanded)
0163 {
0164     m_expanded = expanded;
0165 }
0166 
0167 bool BookmarkItem::isSidebarExpanded() const
0168 {
0169     return m_type == Root ? true : m_sidebarExpanded;
0170 }
0171 
0172 void BookmarkItem::setSidebarExpanded(bool expanded)
0173 {
0174     m_sidebarExpanded = expanded;
0175 }
0176 
0177 void BookmarkItem::addChild(BookmarkItem* child, int index)
0178 {
0179     if (child->m_parent) {
0180         child->m_parent->removeChild(child);
0181     }
0182 
0183     child->m_parent = this;
0184 
0185     if (index < 0) {
0186         m_children.append(child);
0187     }
0188     else {
0189         m_children.insert(index, child);
0190     }
0191 }
0192 
0193 void BookmarkItem::removeChild(BookmarkItem* child)
0194 {
0195     child->m_parent = nullptr;
0196     m_children.removeOne(child);
0197 }
0198 
0199 BookmarkItem::Type BookmarkItem::typeFromString(const QString &string)
0200 {
0201     if (string == QLatin1String("url")) {
0202         return Url;
0203     }
0204 
0205     if (string == QLatin1String("folder")) {
0206         return Folder;
0207     }
0208 
0209     if (string == QLatin1String("separator")) {
0210         return Separator;
0211     }
0212 
0213     return Invalid;
0214 }
0215 
0216 QString BookmarkItem::typeToString(BookmarkItem::Type type)
0217 {
0218     switch (type) {
0219     case Url:
0220         return QSL("url");
0221 
0222     case Folder:
0223         return QSL("folder");
0224 
0225     case Separator:
0226         return QSL("separator");
0227 
0228     default:
0229         return QSL("invalid");
0230     }
0231 }