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 *
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. *
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. *
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 ***************************************************************************/
26 #include <util/functions.h>
27 #include <interfaces/torrentinterface.h>
28 #include <interfaces/torrentfileinterface.h>
29 #include "iwfiletreemodel.h"
36 IWFileTreeModel::IWFileTreeModel(bt::TorrentInterface
* tc
,QObject
* parent
)
37 : TorrentFileTreeModel(tc
,KEEP_FILES
,parent
)
39 mmfile
= IsMultimediaFile(tc
->getStats().output_path
);
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
60 QVariant
IWFileTreeModel::headerData(int section
, Qt::Orientation orientation
,int role
) const
62 if (role
!= Qt::DisplayRole
|| orientation
!= Qt::Horizontal
)
66 return TorrentFileTreeModel::headerData(section
,orientation
,role
);
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
:
85 case PREVIEW_PRIORITY
:
87 default:return i18n("Normal");
91 QVariant
IWFileTreeModel::data(const QModelIndex
& index
, int role
) const
94 if (index
.column() < 2)
95 return TorrentFileTreeModel::data(index
,role
);
97 if (!index
.isValid() || !(n
= (Node
*)index
.internalPointer()))
100 if (role
!= Qt::DisplayRole
)
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
);
110 if (file
->isMultimedia())
112 if (tc
->readyForPreview(file
->getFirstChunk(), file
->getFirstChunk()+1) )
113 return i18n("Available");
115 return i18n("Pending");
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();
136 if (tc
->readyForPreview(0,1))
137 return i18n("Available");
139 return i18n("Pending");
145 double percent
= bt::Percentage(tc
->getStats());
146 KLocale
* loc
= KGlobal::locale();
147 return i18n("%1 %",loc
->formatNumber(percent
,2));
149 default: 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
)
166 Node
* n
= static_cast<Node
*>(index
.internalPointer());
172 for (Uint32 i
= 0;i
< n
->children
.count();i
++)
174 // recurse down the tree
175 setData(index
.child(i
,0),value
,role
);
180 bt::TorrentFileInterface
* file
= n
->file
;
181 Priority prio
= (bt::Priority
)value
.toInt();
182 Priority old
= file
->getPriority();
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
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
);
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);
241 double perc
= bt::Percentage(tc
->getStats());
242 if (fabs(perc
- percentage
) > 0.01)
249 dataChanged(createIndex(0,0),createIndex(0,4));
254 #include "iwfiletreemodel.moc"