kget
iwfilelistmodel.cpp
Go to the documentation of this file.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 "iwfilelistmodel.h"
00030
00031 using namespace bt;
00032
00033 namespace kt
00034 {
00035
00036 IWFileListModel::IWFileListModel(bt::TorrentInterface* tc,QObject* parent)
00037 : TorrentFileListModel(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 IWFileListModel::~IWFileListModel()
00052 {
00053 }
00054
00055 int IWFileListModel::columnCount(const QModelIndex & parent) const
00056 {
00057 if (!parent.isValid())
00058 return 5;
00059 else
00060 return 0;
00061 }
00062
00063 QVariant IWFileListModel::headerData(int section, Qt::Orientation orientation,int role) const
00064 {
00065 if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
00066 return QVariant();
00067
00068 if (section < 2)
00069 return TorrentFileListModel::headerData(section,orientation,role);
00070
00071 switch (section)
00072 {
00073 case 2: return i18n("Priority");
00074 case 3: return i18n("Preview");
00075
00076 case 4: return i18n("% Complete");
00077 default: return QVariant();
00078 }
00079 }
00080
00081 static QString PriorityString(const bt::TorrentFileInterface* file)
00082 {
00083 switch(file->getPriority())
00084 {
00085 case FIRST_PRIORITY: return i18n("First");
00086 case LAST_PRIORITY: return i18n("Last");
00087 case ONLY_SEED_PRIORITY:
00088 case EXCLUDED:
00089 case PREVIEW_PRIORITY:
00090 return QString::null;
00091 default:return i18n("Normal");
00092 }
00093 }
00094
00095 QVariant IWFileListModel::data(const QModelIndex & index, int role) const
00096 {
00097 if (index.column() < 2)
00098 return TorrentFileListModel::data(index,role);
00099
00100 if (!index.isValid() || index.row() < 0 || index.row() >= rowCount(QModelIndex()))
00101 return QVariant();
00102
00103 if (role != Qt::DisplayRole)
00104 return QVariant();
00105
00106 if (tc->getStats().multi_file_torrent)
00107 {
00108 const bt::TorrentFileInterface* file = &tc->getTorrentFile(index.row());
00109 switch (index.column())
00110 {
00111 case 2: return PriorityString(file);
00112 case 3:
00113 if (file->isMultimedia())
00114 {
00115 if (tc->readyForPreview(file->getFirstChunk(), file->getFirstChunk()+1) )
00116 return i18n("Available");
00117 else
00118 return i18n("Pending");
00119 }
00120 else
00121 return i18n("No");
00122 case 4:
00123 {
00124 float percent = file->getDownloadPercentage();
00125 KLocale* loc = KGlobal::locale();
00126 return i18n("%1 %",loc->formatNumber(percent,2));
00127 }
00128 default: return QVariant();
00129 }
00130 }
00131 else
00132 {
00133 switch (index.column())
00134 {
00135 case 2: return QVariant();
00136 case 3:
00137 if (mmfile)
00138 {
00139 if (tc->readyForPreview(0,1))
00140 return i18n("Available");
00141 else
00142 return i18n("Pending");
00143 }
00144 else
00145 return i18n("No");
00146 case 4:
00147 {
00148 double percent = bt::Percentage(tc->getStats());
00149 KLocale* loc = KGlobal::locale();
00150 return i18n("%1 %",loc->formatNumber(percent,2));
00151 }
00152 default: return QVariant();
00153 }
00154 }
00155
00156 return QVariant();
00157 }
00158
00159
00160
00161 bool IWFileListModel::setData(const QModelIndex & index, const QVariant & value, int role)
00162 {
00163 if (role == Qt::CheckStateRole)
00164 return TorrentFileListModel::setData(index,value,role);
00165
00166 if (!index.isValid() || role != Qt::UserRole)
00167 return false;
00168
00169 int r = index.row();
00170 if (r < 0 || r >= rowCount(QModelIndex()))
00171 return false;
00172
00173 bt::TorrentFileInterface & file = tc->getTorrentFile(r);;
00174 Priority prio = (bt::Priority)value.toInt();
00175 Priority old = file.getPriority();
00176
00177 if (prio != old)
00178 {
00179 file.setPriority(prio);
00180 dataChanged(createIndex(index.row(),0),createIndex(index.row(),4));
00181 }
00182
00183 return true;
00184 }
00185
00186 void IWFileListModel::onPercentageUpdated(float )
00187 {
00188 bt::TorrentFileInterface* file = (bt::TorrentFileInterface*)sender();
00189 QModelIndex idx = createIndex(file->getIndex(),4,file);
00190 emit dataChanged(idx,idx);
00191 }
00192
00193 void IWFileListModel::onPreviewAvailable(bool )
00194 {
00195 bt::TorrentFileInterface* file = (bt::TorrentFileInterface*)sender();
00196 QModelIndex idx = createIndex(file->getIndex(),3,file);
00197 emit dataChanged(idx,idx);
00198 }
00199
00200 void IWFileListModel::update()
00201 {
00202 if (!tc->getStats().multi_file_torrent)
00203 {
00204 bool changed = false;
00205 bool np = mmfile && tc->readyForPreview(0,1);
00206 if (preview != np)
00207 {
00208 preview = np;
00209 changed = true;
00210 }
00211
00212 double perc = bt::Percentage(tc->getStats());
00213 if (fabs(perc - percentage) > 0.01)
00214 {
00215 percentage = perc;
00216 changed = true;
00217 }
00218
00219 if (changed)
00220 dataChanged(createIndex(0,0),createIndex(0,4));
00221 }
00222 }
00223 }
00224
00225 #include "iwfilelistmodel.moc"