WebUI: Minor optimizations to the login page
[qBittorrent.git] / src / gui / torrentcontentmodelfolder.cpp
blobabece9c9ba67a8e110da8add1ac3e7f4b283be4b
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 QVector<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 QVector<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() && (tSize > 0))
152 m_progress = tProgress / tSize;
153 m_remaining = tRemaining;
154 Q_ASSERT(m_progress <= 1.);
158 void TorrentContentModelFolder::recalculateAvailability()
160 qreal tAvailability = 0;
161 qulonglong tSize = 0;
162 bool foundAnyData = false;
163 for (TorrentContentModelItem *child : asConst(m_childItems))
165 if (child->priority() == BitTorrent::DownloadPriority::Ignored)
166 continue;
168 if (child->itemType() == FolderType)
169 static_cast<TorrentContentModelFolder*>(child)->recalculateAvailability();
170 const qreal childAvailability = child->availability();
171 if (childAvailability >= 0)
172 { // -1 means "no data"
173 tAvailability += childAvailability * child->size();
174 foundAnyData = true;
176 tSize += child->size();
179 if (!isRootItem() && (tSize > 0) && foundAnyData)
181 m_availability = tAvailability / tSize;
182 Q_ASSERT(m_availability <= 1.);
184 else
186 m_availability = -1.;
190 void TorrentContentModelFolder::increaseSize(qulonglong delta)
192 if (isRootItem())
193 return;
195 m_size += delta;
196 m_parentItem->increaseSize(delta);