qBittorrent
torrentcontentmodelfolder.cpp
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2006-2012 Christophe Dumez <chris@qbittorrent.org>
4  *
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.
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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
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.
27  */
28 
30 
31 #include <QVariant>
32 
33 #include "base/global.h"
34 
36  : TorrentContentModelItem(parent)
37 {
38  Q_ASSERT(parent);
39  m_name = name;
40 }
41 
43  : TorrentContentModelItem(nullptr)
44 {
45  Q_ASSERT(data.size() == NB_COL);
46  m_itemData = data;
47 }
48 
50 {
51  qDeleteAll(m_childItems);
52 }
53 
55 {
56  return FolderType;
57 }
58 
60 {
61  Q_ASSERT(isRootItem());
62  qDeleteAll(m_childItems);
63  m_childItems.clear();
64 }
65 
66 const QVector<TorrentContentModelItem *> &TorrentContentModelFolder::children() const
67 {
68  return m_childItems;
69 }
70 
72 {
73  Q_ASSERT(item);
74  m_childItems.append(item);
75  // Update own size
76  if (item->itemType() == FileType)
77  increaseSize(item->size());
78 }
79 
81 {
82  return m_childItems.value(row, nullptr);
83 }
84 
86 {
88  {
89  if ((child->itemType() == FolderType) && (child->name() == name))
90  return static_cast<TorrentContentModelFolder *>(child);
91  }
92  return nullptr;
93 }
94 
96 {
97  return m_childItems.count();
98 }
99 
100 // Only non-root folders use this function
102 {
103  if (isRootItem())
104  return;
105 
106  Q_ASSERT(!m_childItems.isEmpty());
107 
108  // If all children have the same priority
109  // then the folder should have the same
110  // priority
111  const BitTorrent::DownloadPriority prio = m_childItems.first()->priority();
112  for (int i = 1; i < m_childItems.size(); ++i)
113  {
114  if (m_childItems.at(i)->priority() != prio)
115  {
117  return;
118  }
119  }
120  // All child items have the same priority
121  // Update own if necessary
122  setPriority(prio);
123 }
124 
126 {
127  if (m_priority == newPriority)
128  return;
129 
130  m_priority = newPriority;
131 
132  // Update parent priority
133  if (updateParent)
135 
136  // Update children
138  {
140  child->setPriority(m_priority, false);
141  }
142 }
143 
145 {
146  qreal tProgress = 0;
147  qulonglong tSize = 0;
148  qulonglong tRemaining = 0;
150  {
152  continue;
153 
154  if (child->itemType() == FolderType)
156  tProgress += child->progress() * child->size();
157  tSize += child->size();
158  tRemaining += child->remaining();
159  }
160 
161  if (!isRootItem() && (tSize > 0))
162  {
163  m_progress = tProgress / tSize;
164  m_remaining = tRemaining;
165  Q_ASSERT(m_progress <= 1.);
166  }
167 }
168 
170 {
171  qreal tAvailability = 0;
172  qulonglong tSize = 0;
173  bool foundAnyData = false;
175  {
177  continue;
178 
179  if (child->itemType() == FolderType)
181  const qreal childAvailability = child->availability();
182  if (childAvailability >= 0)
183  { // -1 means "no data"
184  tAvailability += childAvailability * child->size();
185  foundAnyData = true;
186  }
187  tSize += child->size();
188  }
189 
190  if (!isRootItem() && (tSize > 0) && foundAnyData)
191  {
192  m_availability = tAvailability / tSize;
193  Q_ASSERT(m_availability <= 1.);
194  }
195  else
196  {
197  m_availability = -1.;
198  }
199 }
200 
202 {
203  if (isRootItem())
204  return;
205 
206  m_size += delta;
207  m_parentItem->increaseSize(delta);
208 }
ItemType itemType() const override
void setPriority(BitTorrent::DownloadPriority newPriority, bool updateParent=true) override
QVector< TorrentContentModelItem * > m_childItems
TorrentContentModelFolder * childFolderWithName(const QString &name) const
void appendChild(TorrentContentModelItem *item)
TorrentContentModelItem * child(int row) const
TorrentContentModelFolder(const QString &name, TorrentContentModelFolder *parent)
const QVector< TorrentContentModelItem * > & children() const
TorrentContentModelFolder * m_parentItem
BitTorrent::DownloadPriority priority() const
virtual void setPriority(BitTorrent::DownloadPriority newPriority, bool updateParent=true)=0
BitTorrent::DownloadPriority m_priority
TorrentContentModelFolder * parent() const
virtual ItemType itemType() const =0
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42