1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Assistant of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "bookmarkitem.h"
30 
31 #include <QtCore/QCoreApplication>
32 #include <QtCore/QDebug>
33 
34 QT_BEGIN_NAMESPACE
35 
BookmarkItem(const DataVector & data,BookmarkItem * parent)36 BookmarkItem::BookmarkItem(const DataVector &data, BookmarkItem *parent)
37     : m_data(data)
38     , m_parent(parent)
39 {
40 }
41 
~BookmarkItem()42 BookmarkItem::~BookmarkItem()
43 {
44     qDeleteAll(m_children);
45 }
46 
47 BookmarkItem*
parent() const48 BookmarkItem::parent() const
49 {
50     return m_parent;
51 }
52 
53 void
setParent(BookmarkItem * parent)54 BookmarkItem::setParent(BookmarkItem *parent)
55 {
56     m_parent = parent;
57 }
58 
59 void
addChild(BookmarkItem * child)60 BookmarkItem::addChild(BookmarkItem *child)
61 {
62     child->setParent(this);
63     m_children.append(child);
64 }
65 
66 BookmarkItem*
child(int number) const67 BookmarkItem::child(int number) const
68 {
69     if (number >= 0 && number < m_children.count())
70         return m_children[number];
71     return nullptr;
72 }
73 
childCount() const74 int BookmarkItem::childCount() const
75 {
76     return m_children.count();
77 }
78 
childNumber() const79 int BookmarkItem::childNumber() const
80 {
81      if (m_parent)
82          return m_parent->m_children.indexOf(const_cast<BookmarkItem*>(this));
83      return 0;
84 }
85 
86 QVariant
data(int column) const87 BookmarkItem::data(int column) const
88 {
89     if (column == 0)
90         return m_data[0];
91 
92     if (column == 1 || column == UserRoleUrl)
93         return m_data[1];
94 
95     if (column == UserRoleFolder)
96         return m_data[1].toString() == QLatin1String("Folder");
97 
98     if (column == UserRoleExpanded)
99         return m_data[2];
100 
101     return QVariant();
102 }
103 
104 void
setData(const DataVector & data)105 BookmarkItem::setData(const DataVector &data)
106 {
107     m_data = data;
108 }
109 
110 bool
setData(int column,const QVariant & newValue)111 BookmarkItem::setData(int column, const QVariant &newValue)
112 {
113     int index = -1;
114     if (column == 0 || column == 1)
115         index = column;
116 
117     if (column == UserRoleUrl || column == UserRoleFolder)
118         index = 1;
119 
120     if (column == UserRoleExpanded)
121         index = 2;
122 
123     if (index < 0)
124         return false;
125 
126     m_data[index] = newValue;
127     return true;
128 }
129 
130 bool
insertChildren(bool isFolder,int position,int count)131 BookmarkItem::insertChildren(bool isFolder, int position, int count)
132 {
133     if (position < 0 || position > m_children.size())
134         return false;
135 
136     for (int row = 0; row < count; ++row) {
137         m_children.insert(position, new BookmarkItem(DataVector()
138             << (isFolder
139                 ? QCoreApplication::translate("BookmarkItem", "New Folder")
140                 : QCoreApplication::translate("BookmarkItem", "Untitled"))
141             << (isFolder ? "Folder" : "about:blank") << false, this));
142     }
143 
144     return true;
145 }
146 
147 bool
removeChildren(int position,int count)148 BookmarkItem::removeChildren(int position, int count)
149 {
150     if (position < 0 || position > m_children.size())
151         return false;
152 
153     for (int row = 0; row < count; ++row)
154         delete m_children.takeAt(position);
155 
156     return true;
157 }
158 
159 void
dumpTree(int indent) const160 BookmarkItem::dumpTree(int indent) const
161 {
162     const QString tree(indent, ' ');
163     qDebug() << tree + (data(UserRoleFolder).toBool() ? "Folder" : "Bookmark")
164         << "Label:" << data(0).toString() << "parent:" << m_parent << "this:"
165         << this;
166 
167     for (BookmarkItem *item : m_children)
168         item->dumpTree(indent + 4);
169 }
170 
171 QT_END_NAMESPACE
172