* fix a lot of compiler warnings
[kdenetwork.git] / kget / transfer-plugins / bittorrent / iwfiletreemodel.cpp
blob73ff53f6cba3bb8936bf0b22879f3e111f2f3176
1 /** IMPORTANT: please keep this file in sync with ktorrent! ****************/
3 /***************************************************************************
4 * Copyright (C) 2007 by Joris Guisson and Ivan Vasic *
5 * joris.guisson@gmail.com *
6 * ivasic@gmail.com *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************/
23 #include <math.h>
24 #include <klocale.h>
25 #include <kglobal.h>
26 #include <util/functions.h>
27 #include <interfaces/torrentinterface.h>
28 #include <interfaces/torrentfileinterface.h>
29 #include "iwfiletreemodel.h"
31 using namespace bt;
33 namespace kt
36 IWFileTreeModel::IWFileTreeModel(bt::TorrentInterface* tc,QObject* parent)
37 : TorrentFileTreeModel(tc,KEEP_FILES,parent)
39 mmfile = IsMultimediaFile(tc->getStats().output_path);
40 preview = false;
41 percentage = 0;
42 for (Uint32 i = 0;i < tc->getNumFiles();i++)
44 bt::TorrentFileInterface & file = tc->getTorrentFile(i);
45 connect(&file,SIGNAL(downloadPercentageChanged( float )),this,SLOT(onPercentageUpdated( float )));
46 connect(&file,SIGNAL(previewAvailable( bool )),this,SLOT(onPreviewAvailable( bool )));
51 IWFileTreeModel::~IWFileTreeModel()
55 int IWFileTreeModel::columnCount(const QModelIndex & /*parent*/) const
57 return 5;
60 QVariant IWFileTreeModel::headerData(int section, Qt::Orientation orientation,int role) const
62 if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
63 return QVariant();
65 if (section < 2)
66 return TorrentFileTreeModel::headerData(section,orientation,role);
68 switch (section)
70 case 2: return i18n("Priority");
71 case 3: return i18n("Preview");
72 case 4: return i18n("% Complete");
73 default: return QVariant();
77 static QString PriorityString(const bt::TorrentFileInterface* file)
79 switch(file->getPriority())
81 case FIRST_PRIORITY: return i18n("First");
82 case LAST_PRIORITY: return i18n("Last");
83 case ONLY_SEED_PRIORITY:
84 case EXCLUDED:
85 case PREVIEW_PRIORITY:
86 return QString::null;
87 default:return i18n("Normal");
91 QVariant IWFileTreeModel::data(const QModelIndex & index, int role) const
93 Node* n = 0;
94 if (index.column() < 2)
95 return TorrentFileTreeModel::data(index,role);
97 if (!index.isValid() || !(n = (Node*)index.internalPointer()))
98 return QVariant();
100 if (role != Qt::DisplayRole)
101 return QVariant();
103 if (tc->getStats().multi_file_torrent && n->file)
105 const bt::TorrentFileInterface* file = n->file;
106 switch (index.column())
108 case 2: return PriorityString(file);
109 case 3:
110 if (file->isMultimedia())
112 if (tc->readyForPreview(file->getFirstChunk(), file->getFirstChunk()+1) )
113 return i18n("Available");
114 else
115 return i18n("Pending");
117 else
118 return i18n("No");
119 case 4:
121 float percent = file->getDownloadPercentage();
122 KLocale* loc = KGlobal::locale();
123 return i18n("%1 %",loc->formatNumber(percent,2));
125 default: return QVariant();
128 else if (!tc->getStats().multi_file_torrent)
130 switch (index.column())
132 case 2: return QVariant();
133 case 3:
134 if (mmfile)
136 if (tc->readyForPreview(0,1))
137 return i18n("Available");
138 else
139 return i18n("Pending");
141 else
142 return i18n("No");
143 case 4:
145 double percent = bt::Percentage(tc->getStats());
146 KLocale* loc = KGlobal::locale();
147 return i18n("%1 %",loc->formatNumber(percent,2));
149 default: return QVariant();
153 return QVariant();
158 bool IWFileTreeModel::setData(const QModelIndex & index, const QVariant & value, int role)
160 if (role == Qt::CheckStateRole)
161 return TorrentFileTreeModel::setData(index,value,role);
163 if (!index.isValid() || role != Qt::UserRole)
164 return false;
166 Node* n = static_cast<Node*>(index.internalPointer());
167 if (!n)
168 return false;
170 if (!n->file)
172 for (Uint32 i = 0;i < n->children.count();i++)
174 // recurse down the tree
175 setData(index.child(i,0),value,role);
178 else
180 bt::TorrentFileInterface* file = n->file;
181 Priority prio = (bt::Priority)value.toInt();
182 Priority old = file->getPriority();
184 if (prio != old)
186 file->setPriority(prio);
187 dataChanged(createIndex(index.row(),0),createIndex(index.row(),4));
188 QModelIndex parent = index.parent();
189 if (parent.isValid())
190 dataChanged(parent,parent); // parent needs to be updated to
194 return true;
199 void IWFileTreeModel::onPercentageUpdated(float /*p*/)
201 bt::TorrentFileInterface* file = (bt::TorrentFileInterface*)sender();
202 update(index(0,0,QModelIndex()),file,4);
205 void IWFileTreeModel::onPreviewAvailable(bool /*av*/)
207 bt::TorrentFileInterface* file = (bt::TorrentFileInterface*)sender();
208 update(index(0,0,QModelIndex()),file,3);
211 void IWFileTreeModel::update(const QModelIndex & idx,bt::TorrentFileInterface* file,int col)
213 Node* n = (Node*)idx.internalPointer();
214 if (n->file && n->file == file)
216 QModelIndex i = createIndex(idx.row(),col,n);
217 emit dataChanged(i,i);
219 else
221 for (Uint32 i = 0;i < n->children.count();i++)
223 // recurse down the tree
224 update(idx.child(i,0),file,col);
229 void IWFileTreeModel::update()
231 if (!tc->getStats().multi_file_torrent)
233 bool changed = false;
234 bool np = mmfile && tc->readyForPreview(0,1);
235 if (preview != np)
237 preview = np;
238 changed = true;
241 double perc = bt::Percentage(tc->getStats());
242 if (fabs(perc - percentage) > 0.01)
244 percentage = perc;
245 changed = true;
248 if (changed)
249 dataChanged(createIndex(0,0),createIndex(0,4));
254 #include "iwfiletreemodel.moc"