File indexing completed on 2024-05-12 04:58:06

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-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 "historyitem.h"
0019 #include "qztools.h"
0020 
0021 HistoryItem::HistoryItem(HistoryItem* parent)
0022     : canFetchMore(false)
0023     , m_parent(parent)
0024     , m_startTimestamp(0)
0025     , m_endTimestamp(0)
0026 {
0027     if (m_parent) {
0028         m_parent->appendChild(this);
0029     }
0030 }
0031 
0032 void HistoryItem::changeParent(HistoryItem* parent)
0033 {
0034     if (m_parent) {
0035         m_parent->removeChild(this);
0036     }
0037 
0038     m_parent = parent;
0039 
0040     if (m_parent) {
0041         m_parent->prependChild(this);
0042     }
0043 }
0044 
0045 HistoryItem* HistoryItem::parent() const
0046 {
0047     return m_parent;
0048 }
0049 
0050 void HistoryItem::prependChild(HistoryItem* child)
0051 {
0052     if (m_children.contains(child)) {
0053         m_children.removeAll(child);
0054     }
0055 
0056     child->m_parent = this;
0057     m_children.prepend(child);
0058 }
0059 
0060 void HistoryItem::appendChild(HistoryItem* child)
0061 {
0062     if (m_children.contains(child)) {
0063         m_children.removeAll(child);
0064     }
0065 
0066     child->m_parent = this;
0067     m_children.append(child);
0068 }
0069 
0070 void HistoryItem::insertChild(int row, HistoryItem* child)
0071 {
0072     if (m_children.contains(child)) {
0073         m_children.removeAll(child);
0074     }
0075 
0076     if (m_children.count() >= row) {
0077         child->m_parent = this;
0078         m_children.insert(row, child);
0079     }
0080 }
0081 
0082 void HistoryItem::removeChild(int row)
0083 {
0084     if (QzTools::containsIndex(m_children, row)) {
0085         removeChild(m_children.at(row));
0086     }
0087 }
0088 
0089 void HistoryItem::removeChild(HistoryItem* child)
0090 {
0091     m_children.removeOne(child);
0092 }
0093 
0094 HistoryItem* HistoryItem::child(int row) const
0095 {
0096     if (QzTools::containsIndex(m_children, row)) {
0097         return m_children.at(row);
0098     }
0099 
0100     return nullptr;
0101 }
0102 
0103 int HistoryItem::childCount() const
0104 {
0105     return m_children.count();
0106 }
0107 
0108 int HistoryItem::row()
0109 {
0110     return m_parent ? m_parent->indexOfChild(this) : 0;
0111 }
0112 
0113 int HistoryItem::indexOfChild(HistoryItem* child)
0114 {
0115     return m_children.indexOf(child);
0116 }
0117 
0118 bool HistoryItem::isTopLevel() const
0119 {
0120     return (m_startTimestamp != 0);
0121 }
0122 
0123 QIcon HistoryItem::icon() const
0124 {
0125     return m_icon;
0126 }
0127 
0128 void HistoryItem::setIcon(const QIcon &icon)
0129 {
0130     m_icon = icon;
0131 }
0132 
0133 void HistoryItem::setStartTimestamp(qint64 start)
0134 {
0135     m_startTimestamp = start;
0136 }
0137 
0138 qint64 HistoryItem::startTimestamp() const
0139 {
0140     if (m_startTimestamp == -1) {
0141         return QDateTime::currentDateTime().toMSecsSinceEpoch();
0142     }
0143 
0144     return m_startTimestamp;
0145 }
0146 
0147 void HistoryItem::setEndTimestamp(qint64 end)
0148 {
0149     m_endTimestamp = end;
0150 
0151 }
0152 
0153 qint64 HistoryItem::endTimestamp() const
0154 {
0155     return m_endTimestamp;
0156 }
0157 
0158 HistoryItem::~HistoryItem()
0159 {
0160     if (m_parent) {
0161         m_parent->removeChild(this);
0162     }
0163 
0164     qDeleteAll(m_children);
0165 }