1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2014  David Rosca <nowrep@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "bookmarkitem.h"
19 #include "iconprovider.h"
20 
BookmarkItem(BookmarkItem::Type type,BookmarkItem * parent)21 BookmarkItem::BookmarkItem(BookmarkItem::Type type, BookmarkItem* parent)
22     : m_type(type)
23     , m_parent(parent)
24     , m_visitCount(0)
25     , m_expanded(false)
26     , m_sidebarExpanded(false)
27 {
28     if (m_parent) {
29         parent->addChild(this);
30     }
31 }
32 
~BookmarkItem()33 BookmarkItem::~BookmarkItem()
34 {
35     qDeleteAll(m_children);
36 }
37 
type() const38 BookmarkItem::Type BookmarkItem::type() const
39 {
40     return m_type;
41 }
42 
setType(BookmarkItem::Type type)43 void BookmarkItem::setType(BookmarkItem::Type type)
44 {
45     m_type = type;
46 }
47 
isFolder() const48 bool BookmarkItem::isFolder() const
49 {
50     return m_type == Folder;
51 }
52 
isUrl() const53 bool BookmarkItem::isUrl() const
54 {
55     return m_type == Url;
56 }
57 
isSeparator() const58 bool BookmarkItem::isSeparator() const
59 {
60     return m_type == Separator;
61 }
62 
parent() const63 BookmarkItem* BookmarkItem::parent() const
64 {
65     return m_parent;
66 }
67 
children() const68 QList<BookmarkItem*> BookmarkItem::children() const
69 {
70     return m_children;
71 }
72 
icon()73 QIcon BookmarkItem::icon()
74 {
75     // Cache icon for 20 seconds
76     const int iconCacheTime = 20 * 1000;
77 
78     switch (m_type) {
79     case Url:
80         if (m_iconTime.isNull() || m_iconTime.elapsed() > iconCacheTime) {
81             m_icon = IconProvider::iconForUrl(m_url);
82             m_iconTime.restart();
83         }
84         return m_icon;
85     case Folder:
86         return IconProvider::standardIcon(QStyle::SP_DirIcon);
87     default:
88         return QIcon();
89     }
90 }
91 
setIcon(const QIcon & icon)92 void BookmarkItem::setIcon(const QIcon &icon)
93 {
94     m_icon = icon;
95 }
96 
urlString() const97 QString BookmarkItem::urlString() const
98 {
99     return QString::fromUtf8(m_url.toEncoded());
100 }
101 
url() const102 QUrl BookmarkItem::url() const
103 {
104     return  m_url;
105 }
106 
setUrl(const QUrl & url)107 void BookmarkItem::setUrl(const QUrl &url)
108 {
109     m_url = url;
110 }
111 
title() const112 QString BookmarkItem::title() const
113 {
114     return m_title;
115 }
116 
setTitle(const QString & title)117 void BookmarkItem::setTitle(const QString &title)
118 {
119     m_title = title;
120 }
121 
description() const122 QString BookmarkItem::description() const
123 {
124     return m_description;
125 }
126 
setDescription(const QString & description)127 void BookmarkItem::setDescription(const QString &description)
128 {
129     m_description = description;
130 }
131 
keyword() const132 QString BookmarkItem::keyword() const
133 {
134     return m_keyword;
135 }
136 
setKeyword(const QString & keyword)137 void BookmarkItem::setKeyword(const QString &keyword)
138 {
139     m_keyword = keyword;
140 }
141 
visitCount() const142 int BookmarkItem::visitCount() const
143 {
144     return m_visitCount;
145 }
146 
setVisitCount(int count)147 void BookmarkItem::setVisitCount(int count)
148 {
149     m_visitCount = count;
150 }
151 
updateVisitCount()152 void BookmarkItem::updateVisitCount()
153 {
154     m_visitCount++;
155 }
156 
isExpanded() const157 bool BookmarkItem::isExpanded() const
158 {
159     return m_type == Root ? true : m_expanded;
160 }
161 
setExpanded(bool expanded)162 void BookmarkItem::setExpanded(bool expanded)
163 {
164     m_expanded = expanded;
165 }
166 
isSidebarExpanded() const167 bool BookmarkItem::isSidebarExpanded() const
168 {
169     return m_type == Root ? true : m_sidebarExpanded;
170 }
171 
setSidebarExpanded(bool expanded)172 void BookmarkItem::setSidebarExpanded(bool expanded)
173 {
174     m_sidebarExpanded = expanded;
175 }
176 
addChild(BookmarkItem * child,int index)177 void BookmarkItem::addChild(BookmarkItem* child, int index)
178 {
179     if (child->m_parent) {
180         child->m_parent->removeChild(child);
181     }
182 
183     child->m_parent = this;
184 
185     if (index < 0) {
186         m_children.append(child);
187     }
188     else {
189         m_children.insert(index, child);
190     }
191 }
192 
removeChild(BookmarkItem * child)193 void BookmarkItem::removeChild(BookmarkItem* child)
194 {
195     child->m_parent = nullptr;
196     m_children.removeOne(child);
197 }
198 
typeFromString(const QString & string)199 BookmarkItem::Type BookmarkItem::typeFromString(const QString &string)
200 {
201     if (string == QLatin1String("url")) {
202         return Url;
203     }
204 
205     if (string == QLatin1String("folder")) {
206         return Folder;
207     }
208 
209     if (string == QLatin1String("separator")) {
210         return Separator;
211     }
212 
213     return Invalid;
214 }
215 
typeToString(BookmarkItem::Type type)216 QString BookmarkItem::typeToString(BookmarkItem::Type type)
217 {
218     switch (type) {
219     case Url:
220         return QSL("url");
221 
222     case Folder:
223         return QSL("folder");
224 
225     case Separator:
226         return QSL("separator");
227 
228     default:
229         return QSL("invalid");
230     }
231 }
232