Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / torrentcontentmodelfolder.cpp
blob57d3b43cfccc93625a9f9f38093f5e2926c6f253
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "torrentcontentmodelfolder.h"
31 #include <QVariant>
33 #include "base/global.h"
35 TorrentContentModelFolder::TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent)
36 : TorrentContentModelItem(parent)
38 Q_ASSERT(parent);
39 m_name = name;
42 TorrentContentModelFolder::TorrentContentModelFolder(const QList<QString> &data)
43 : TorrentContentModelItem(nullptr)
45 Q_ASSERT(data.size() == NB_COL);
46 m_itemData = data;
49 TorrentContentModelFolder::~TorrentContentModelFolder()
51 qDeleteAll(m_childItems);
54 TorrentContentModelItem::ItemType TorrentContentModelFolder::itemType() const
56 return FolderType;
59 void TorrentContentModelFolder::deleteAllChildren()
61 Q_ASSERT(isRootItem());
62 qDeleteAll(m_childItems);
63 m_childItems.clear();
66 const QList<TorrentContentModelItem *> &TorrentContentModelFolder::children() const
68 return m_childItems;
71 void TorrentContentModelFolder::appendChild(TorrentContentModelItem *item)
73 Q_ASSERT(item);
74 m_childItems.append(item);
75 // Update own size
76 if (item->itemType() == FileType)
77 increaseSize(item->size());
80 TorrentContentModelItem *TorrentContentModelFolder::child(int row) const
82 return m_childItems.value(row, nullptr);
84 int TorrentContentModelFolder::childCount() const
86 return m_childItems.count();
89 // Only non-root folders use this function
90 void TorrentContentModelFolder::updatePriority()
92 if (isRootItem())
93 return;
95 Q_ASSERT(!m_childItems.isEmpty());
97 // If all children have the same priority
98 // then the folder should have the same
99 // priority
100 const BitTorrent::DownloadPriority prio = m_childItems.first()->priority();
101 for (int i = 1; i < m_childItems.size(); ++i)
103 if (m_childItems.at(i)->priority() != prio)
105 setPriority(BitTorrent::DownloadPriority::Mixed);
106 return;
109 // All child items have the same priority
110 // Update own if necessary
111 setPriority(prio);
114 void TorrentContentModelFolder::setPriority(BitTorrent::DownloadPriority newPriority, bool updateParent)
116 if (m_priority == newPriority)
117 return;
119 m_priority = newPriority;
121 // Update parent priority
122 if (updateParent)
123 m_parentItem->updatePriority();
125 // Update children
126 if (m_priority != BitTorrent::DownloadPriority::Mixed)
128 for (TorrentContentModelItem *child : asConst(m_childItems))
129 child->setPriority(m_priority, false);
133 void TorrentContentModelFolder::recalculateProgress()
135 qreal tProgress = 0;
136 qulonglong tSize = 0;
137 qulonglong tRemaining = 0;
138 for (TorrentContentModelItem *child : asConst(m_childItems))
140 if (child->priority() == BitTorrent::DownloadPriority::Ignored)
141 continue;
143 if (child->itemType() == FolderType)
144 static_cast<TorrentContentModelFolder *>(child)->recalculateProgress();
145 tProgress += child->progress() * child->size();
146 tSize += child->size();
147 tRemaining += child->remaining();
150 if (!isRootItem())
152 if (tSize > 0)
154 m_progress = tProgress / tSize;
155 m_remaining = tRemaining;
157 else
159 m_progress = 1.0;
160 m_remaining = 0;
163 Q_ASSERT(m_progress <= 1.);
167 void TorrentContentModelFolder::recalculateAvailability()
169 qreal tAvailability = 0;
170 qulonglong tSize = 0;
171 bool foundAnyData = false;
172 for (TorrentContentModelItem *child : asConst(m_childItems))
174 if (child->priority() == BitTorrent::DownloadPriority::Ignored)
175 continue;
177 if (child->itemType() == FolderType)
178 static_cast<TorrentContentModelFolder*>(child)->recalculateAvailability();
179 const qreal childAvailability = child->availability();
180 if (childAvailability >= 0)
181 { // -1 means "no data"
182 tAvailability += childAvailability * child->size();
183 foundAnyData = true;
185 tSize += child->size();
188 if (!isRootItem() && (tSize > 0) && foundAnyData)
190 m_availability = tAvailability / tSize;
191 Q_ASSERT(m_availability <= 1.);
193 else
195 m_availability = -1.;
199 void TorrentContentModelFolder::increaseSize(qulonglong delta)
201 if (isRootItem())
202 return;
204 m_size += delta;
205 m_parentItem->increaseSize(delta);