00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <math.h>
00024 #include <klocale.h>
00025 #include <kglobal.h>
00026 #include <util/functions.h>
00027 #include <interfaces/torrentinterface.h>
00028 #include <interfaces/torrentfileinterface.h>
00029 #include "iwfiletreemodel.h"
00030
00031 using namespace bt;
00032
00033 namespace kt
00034 {
00035
00036 IWFileTreeModel::IWFileTreeModel(bt::TorrentInterface* tc,QObject* parent)
00037 : TorrentFileTreeModel(tc,KEEP_FILES,parent)
00038 {
00039 mmfile = IsMultimediaFile(tc->getStats().output_path);
00040 preview = false;
00041 percentage = 0;
00042 for (Uint32 i = 0;i < tc->getNumFiles();i++)
00043 {
00044 bt::TorrentFileInterface & file = tc->getTorrentFile(i);
00045 connect(&file,SIGNAL(downloadPercentageChanged( float )),this,SLOT(onPercentageUpdated( float )));
00046 connect(&file,SIGNAL(previewAvailable( bool )),this,SLOT(onPreviewAvailable( bool )));
00047 }
00048 }
00049
00050
00051 IWFileTreeModel::~IWFileTreeModel()
00052 {
00053 }
00054
00055 int IWFileTreeModel::columnCount(const QModelIndex & ) const
00056 {
00057 return 5;
00058 }
00059
00060 QVariant IWFileTreeModel::headerData(int section, Qt::Orientation orientation,int role) const
00061 {
00062 if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
00063 return QVariant();
00064
00065 if (section < 2)
00066 return TorrentFileTreeModel::headerData(section,orientation,role);
00067
00068 switch (section)
00069 {
00070 case 2: return i18n("Priority");
00071 case 3: return i18n("Preview");
00072
00073 case 4: return i18n("% Complete");
00074 default: return QVariant();
00075 }
00076 }
00077
00078 static QString PriorityString(const bt::TorrentFileInterface* file)
00079 {
00080 switch(file->getPriority())
00081 {
00082 case FIRST_PRIORITY: return i18n("First");
00083 case LAST_PRIORITY: return i18n("Last");
00084 case ONLY_SEED_PRIORITY:
00085 case EXCLUDED:
00086 case PREVIEW_PRIORITY:
00087 return QString::null;
00088 default:return i18n("Normal");
00089 }
00090 }
00091
00092 QVariant IWFileTreeModel::data(const QModelIndex & index, int role) const
00093 {
00094 Node* n = 0;
00095 if (index.column() < 2)
00096 return TorrentFileTreeModel::data(index,role);
00097
00098 if (!index.isValid() || !(n = (Node*)index.internalPointer()))
00099 return QVariant();
00100
00101 if (role != Qt::DisplayRole)
00102 return QVariant();
00103
00104 if (tc->getStats().multi_file_torrent && n->file)
00105 {
00106 const bt::TorrentFileInterface* file = n->file;
00107 switch (index.column())
00108 {
00109 case 2: return PriorityString(file);
00110 case 3:
00111 if (file->isMultimedia())
00112 {
00113 if (tc->readyForPreview(file->getFirstChunk(), file->getFirstChunk()+1) )
00114 return i18n("Available");
00115 else
00116 return i18n("Pending");
00117 }
00118 else
00119 return i18n("No");
00120 case 4:
00121 {
00122 float percent = file->getDownloadPercentage();
00123 KLocale* loc = KGlobal::locale();
00124 return i18n("%1 %",loc->formatNumber(percent,2));
00125 }
00126 default: return QVariant();
00127 }
00128 }
00129 else if (!tc->getStats().multi_file_torrent)
00130 {
00131 switch (index.column())
00132 {
00133 case 2: return QVariant();
00134 case 3:
00135 if (mmfile)
00136 {
00137 if (tc->readyForPreview(0,1))
00138 return i18n("Available");
00139 else
00140 return i18n("Pending");
00141 }
00142 else
00143 return i18n("No");
00144 case 4:
00145 {
00146 double percent = bt::Percentage(tc->getStats());
00147 KLocale* loc = KGlobal::locale();
00148 return i18n("%1 %",loc->formatNumber(percent,2));
00149 }
00150 default: return QVariant();
00151 }
00152 }
00153
00154 return QVariant();
00155 }
00156
00157
00158
00159 bool IWFileTreeModel::setData(const QModelIndex & index, const QVariant & value, int role)
00160 {
00161 if (role == Qt::CheckStateRole)
00162 return TorrentFileTreeModel::setData(index,value,role);
00163
00164 if (!index.isValid() || role != Qt::UserRole)
00165 return false;
00166
00167 Node* n = static_cast<Node*>(index.internalPointer());
00168 if (!n)
00169 return false;
00170
00171 if (!n->file)
00172 {
00173 for (Uint32 i = 0;i < n->children.count();i++)
00174 {
00175
00176 setData(index.child(i,0),value,role);
00177 }
00178 }
00179 else
00180 {
00181 bt::TorrentFileInterface* file = n->file;
00182 Priority prio = (bt::Priority)value.toInt();
00183 Priority old = file->getPriority();
00184
00185 if (prio != old)
00186 {
00187 file->setPriority(prio);
00188 dataChanged(createIndex(index.row(),0),createIndex(index.row(),4));
00189 QModelIndex parent = index.parent();
00190 if (parent.isValid())
00191 dataChanged(parent,parent);
00192 }
00193 }
00194
00195 return true;
00196 }
00197
00198
00199
00200 void IWFileTreeModel::onPercentageUpdated(float )
00201 {
00202 bt::TorrentFileInterface* file = (bt::TorrentFileInterface*)sender();
00203 update(index(0,0,QModelIndex()),file,4);
00204 }
00205
00206 void IWFileTreeModel::onPreviewAvailable(bool )
00207 {
00208 bt::TorrentFileInterface* file = (bt::TorrentFileInterface*)sender();
00209 update(index(0,0,QModelIndex()),file,3);
00210 }
00211
00212 void IWFileTreeModel::update(const QModelIndex & idx,bt::TorrentFileInterface* file,int col)
00213 {
00214 Node* n = (Node*)idx.internalPointer();
00215 if (n->file && n->file == file)
00216 {
00217 QModelIndex i = createIndex(idx.row(),col,n);
00218 emit dataChanged(i,i);
00219 }
00220 else
00221 {
00222 for (Uint32 i = 0;i < n->children.count();i++)
00223 {
00224
00225 update(idx.child(i,0),file,col);
00226 }
00227 }
00228 }
00229
00230 void IWFileTreeModel::update()
00231 {
00232 if (!tc->getStats().multi_file_torrent)
00233 {
00234 bool changed = false;
00235 bool np = mmfile && tc->readyForPreview(0,1);
00236 if (preview != np)
00237 {
00238 preview = np;
00239 changed = true;
00240 }
00241
00242 double perc = bt::Percentage(tc->getStats());
00243 if (fabs(perc - percentage) > 0.01)
00244 {
00245 percentage = perc;
00246 changed = true;
00247 }
00248
00249 if (changed)
00250 dataChanged(createIndex(0,0),createIndex(0,4));
00251 }
00252 }
00253 }
00254
00255 #include "iwfiletreemodel.moc"