22 #include <QHeaderView>
23 #include <QItemSelectionModel>
24 #include <QSortFilterProxyModel>
26 #include <kiconloader.h>
30 #include <kmessagebox.h>
31 #include <kmimetype.h>
32 #include <ksharedconfig.h>
33 #include <kconfiggroup.h>
34 #include <kfiledialog.h>
35 #include <util/bitset.h>
36 #include <util/error.h>
37 #include <util/functions.h>
38 #include <interfaces/torrentinterface.h>
39 #include <interfaces/torrentfileinterface.h>
40 #include <qfileinfo.h>
42 #include <util/timer.h>
53 setContextMenuPolicy(Qt::CustomContextMenu);
54 setRootIsDecorated(
false);
55 setSortingEnabled(
true);
56 setAlternatingRowColors(
true);
57 setSelectionMode(QAbstractItemView::ExtendedSelection);
58 setSelectionBehavior(QAbstractItemView::SelectRows);
59 setUniformRowHeights(
true);
62 proxy_model->setSortRole(Qt::UserRole);
63 setModel(proxy_model);
65 context_menu =
new KMenu(
this);
66 open_action = context_menu->addAction(KIcon(
"document-open"),i18nc(
"Open file",
"Open"),
this,SLOT(open()));
67 context_menu->addSeparator();
68 download_first_action = context_menu->addAction(i18n(
"Download first"),
this,SLOT(downloadFirst()));
69 download_normal_action = context_menu->addAction(i18n(
"Download normally"),
this,SLOT(downloadNormal()));
70 download_last_action = context_menu->addAction(i18n(
"Download last"),
this,SLOT(downloadLast()));
71 context_menu->addSeparator();
72 dnd_action = context_menu->addAction(i18n(
"Do Not Download"),
this,SLOT(doNotDownload()));
73 delete_action = context_menu->addAction(i18n(
"Delete File(s)"),
this,SLOT(deleteFiles()));
74 context_menu->addSeparator();
75 move_files_action = context_menu->addAction(i18n(
"Move File"),
this,SLOT(moveFiles()));
76 context_menu->addSeparator();
77 collapse_action = context_menu->addAction(i18n(
"Collapse Folder Tree"),
this,SLOT(collapseTree()));
78 expand_action = context_menu->addAction(i18n(
"Expand Folder Tree"),
this,SLOT(expandTree()));
80 connect(
this,SIGNAL(customContextMenuRequested(QPoint)),
81 this,SLOT(showContextMenu(QPoint)));
82 connect(
this,SIGNAL(doubleClicked(QModelIndex)),
83 this,SLOT(onDoubleClicked(QModelIndex)));
86 show_list_of_files =
false;
105 proxy_model->setSourceModel(0);
112 connect(tc,SIGNAL(missingFilesMarkedDND(bt::TorrentInterface*)),
113 this,SLOT(onMissingFileMarkedDND(bt::TorrentInterface*)));
115 if (show_list_of_files)
120 proxy_model->setSourceModel(model);
121 setRootIsDecorated(tc->getStats().multi_file_torrent);
123 QMap<bt::TorrentInterface*,QByteArray>::iterator i = expanded_state_map.find(tc);
124 if (i != expanded_state_map.end())
131 proxy_model->setSourceModel(0);
136 void FileView::onMissingFileMarkedDND(bt::TorrentInterface* tc)
142 void FileView::showContextMenu(
const QPoint & p)
144 const TorrentStats & s = curr_tc->getStats();
146 QModelIndexList sel = selectionModel()->selectedRows();
147 if (sel.count() == 0)
152 download_first_action->setEnabled(
true);
153 download_normal_action->setEnabled(
true);
154 download_last_action->setEnabled(
true);
155 open_action->setEnabled(
false);
156 dnd_action->setEnabled(
true);
157 delete_action->setEnabled(
true);
158 context_menu->popup(mapToGlobal(p));
159 move_files_action->setEnabled(
true);
160 collapse_action->setEnabled(!show_list_of_files);
161 expand_action->setEnabled(!show_list_of_files);
165 QModelIndex item = proxy_model->mapToSource(sel.front());
166 bt::TorrentFileInterface* file = model->
indexToFile(item);
168 download_first_action->setEnabled(
false);
169 download_last_action->setEnabled(
false);
170 download_normal_action->setEnabled(
false);
171 dnd_action->setEnabled(
false);
172 delete_action->setEnabled(
false);
174 if (!s.multi_file_torrent)
176 open_action->setEnabled(
true);
177 move_files_action->setEnabled(
true);
178 preview_path = curr_tc->getStats().output_path;
179 collapse_action->setEnabled(
false);
180 expand_action->setEnabled(
false);
184 move_files_action->setEnabled(
true);
185 collapse_action->setEnabled(
false);
186 expand_action->setEnabled(
false);
189 open_action->setEnabled(
true);
190 preview_path = file->getPathOnDisk();
192 download_first_action->setEnabled(file->getPriority() != FIRST_PRIORITY);
193 download_normal_action->setEnabled(file->getPriority() != NORMAL_PRIORITY);
194 download_last_action->setEnabled(file->getPriority() != LAST_PRIORITY);
195 dnd_action->setEnabled(file->getPriority() != ONLY_SEED_PRIORITY);
196 delete_action->setEnabled(file->getPriority() != EXCLUDED);
200 open_action->setEnabled(
false);
205 move_files_action->setEnabled(
false);
206 download_first_action->setEnabled(
true);
207 download_normal_action->setEnabled(
true);
208 download_last_action->setEnabled(
true);
209 dnd_action->setEnabled(
true);
210 delete_action->setEnabled(
true);
211 open_action->setEnabled(
true);
212 preview_path = curr_tc->getDataDir() + model->
dirPath(item);
213 collapse_action->setEnabled(!show_list_of_files);
214 expand_action->setEnabled(!show_list_of_files);
217 context_menu->popup(mapToGlobal(p));
220 void FileView::open()
222 new KRun(KUrl(preview_path), 0, 0,
true,
true);
225 void FileView::changePriority(bt::Priority newpriority)
227 QModelIndexList sel = selectionModel()->selectedRows(2);
228 for (QModelIndexList::iterator i = sel.begin();i != sel.end();++i)
229 *i = proxy_model->mapToSource(*i);
232 proxy_model->invalidate();
236 void FileView::downloadFirst()
238 changePriority(FIRST_PRIORITY);
241 void FileView::downloadLast()
243 changePriority(LAST_PRIORITY);
246 void FileView::downloadNormal()
248 changePriority(NORMAL_PRIORITY);
251 void FileView::doNotDownload()
253 changePriority(ONLY_SEED_PRIORITY);
256 void FileView::deleteFiles()
258 QModelIndexList sel = selectionModel()->selectedRows();
259 Uint32 n = sel.count();
262 if (!model->
indexToFile(proxy_model->mapToSource(sel.front())))
266 QString msg = i18np(
"You will lose all data in this file, are you sure you want to do this?",
267 "You will lose all data in these files, are you sure you want to do this?", n);
269 if (KMessageBox::warningYesNo(0,msg) == KMessageBox::Yes)
270 changePriority(EXCLUDED);
273 void FileView::moveFiles()
275 if (curr_tc->getStats().multi_file_torrent)
277 QModelIndexList sel = selectionModel()->selectedRows();
278 QMap<bt::TorrentFileInterface*,QString> moves;
280 QString dir = KFileDialog::getExistingDirectory(KUrl(
"kfiledialog:///saveTorrentData"),
281 this,i18n(
"Select a directory to move the data to."));
285 foreach (
const QModelIndex &idx,sel)
287 bt::TorrentFileInterface* tfi = model->
indexToFile(proxy_model->mapToSource(idx));
291 moves.insert(tfi,dir);
294 if (moves.count() > 0)
296 curr_tc->moveTorrentFiles(moves);
301 QString dir = KFileDialog::getExistingDirectory(KUrl(
"kfiledialog:///saveTorrentData"),
302 this,i18n(
"Select a directory to move the data to."));
306 curr_tc->changeOutputDir(dir,bt::TorrentInterface::MOVE_FILES);
310 void FileView::expandCollapseTree(
const QModelIndex& idx,
bool expand)
312 int rowCount = proxy_model->rowCount(idx);
313 for (
int i = 0; i < rowCount; i++)
315 const QModelIndex& ridx = proxy_model->index(i, 0, idx);
316 if (proxy_model->hasChildren(ridx))
317 expandCollapseTree(ridx, expand);
319 setExpanded(idx, expand);
322 void FileView::expandCollapseSelected(
bool expand)
324 QModelIndexList sel = selectionModel()->selectedRows();
325 for (QModelIndexList::iterator i = sel.begin(); i != sel.end(); ++i)
327 if (proxy_model->hasChildren(*i))
328 expandCollapseTree(*i, expand);
332 void FileView::collapseTree()
334 expandCollapseSelected(
false);
337 void FileView::expandTree()
339 expandCollapseSelected(
true);
342 void FileView::onDoubleClicked(
const QModelIndex & index)
347 const TorrentStats & s = curr_tc->getStats();
349 if (s.multi_file_torrent)
351 bt::TorrentFileInterface* file = model->
indexToFile(proxy_model->mapToSource(index));
355 new KRun(KUrl(curr_tc->getDataDir() + model->
dirPath(proxy_model->mapToSource(index))), 0, 0,
true,
true);
360 new KRun(KUrl(file->getPathOnDisk()), 0, 0,
true,
true);
365 new KRun(KUrl(curr_tc->getStats().output_path), 0, 0,
true,
true);
374 KConfigGroup g = cfg->group(
"FileView");
375 QByteArray s = header()->saveState();
376 g.writeEntry(
"state",s.toBase64());
381 KConfigGroup g = cfg->group(
"FileView");
382 QByteArray s = QByteArray::fromBase64(g.readEntry(
"state",QByteArray()));
385 QHeaderView* v = header();
387 sortByColumn(v->sortIndicatorSection(),v->sortIndicatorOrder());
398 scheduleDelayedItemsLayout();
405 expanded_state_map.remove(tc);
410 if (show_list_of_files == on)
413 show_list_of_files = on;
414 if (!model || !curr_tc)
420 proxy_model->setSourceModel(0);
424 if (show_list_of_files)
429 proxy_model->setSourceModel(model);
430 setRootIsDecorated(curr_tc->getStats().multi_file_torrent);
432 QMap<bt::TorrentInterface*,QByteArray>::iterator i = expanded_state_map.find(curr_tc);
433 if (i != expanded_state_map.end())
438 collapse_action->setEnabled(!show_list_of_files);
439 expand_action->setEnabled(!show_list_of_files);
442 bool FileView::viewportEvent(QEvent *event)
444 executeDelayedItemsLayout();
445 return QTreeView::viewportEvent(event);
460 void FileView::dataChanged(
const QModelIndex & topLeft,
const QModelIndex & bottomRight)
463 Q_UNUSED(bottomRight)
468 #include "fileview.moc"
virtual void filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
void filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
virtual void missingFilesMarkedDND()
Missing files have been marked DND, update the preview and selection information. ...
void setShowListOfFiles(bool on, KSharedConfigPtr cfg)
void filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
void loadState(KSharedConfigPtr cfg)
virtual QString dirPath(const QModelIndex &idx)=0
Get the path of a directory (root directory not included)
virtual void loadExpandedState(QSortFilterProxyModel *pm, QTreeView *tv, const QByteArray &state)
Retore the expanded state of the tree.in a QTreeView.
virtual void update()
Update gui if necessary.
virtual void filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
void changeTC(bt::TorrentInterface *tc, KSharedConfigPtr cfg)
void onTorrentRemoved(bt::TorrentInterface *tc)
void saveState(KSharedConfigPtr cfg)
virtual void changePriority(const QModelIndexList &indexes, bt::Priority newpriority)=0
Change the priority of a bunch of items.
virtual QByteArray saveExpandedState(QSortFilterProxyModel *pm, QTreeView *tv)
Save which items are expanded.
virtual bt::TorrentFileInterface * indexToFile(const QModelIndex &idx)=0
Convert a model index to a file.