• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • transfer-plugins
  • bittorrent
  • advanceddetails
fileview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #include "fileview.h"
21 
22 #include <QHeaderView>
23 #include <QItemSelectionModel>
24 #include <QSortFilterProxyModel>
25 #include <klocale.h>
26 #include <kiconloader.h>
27 #include <kglobal.h>
28 #include <kmenu.h>
29 #include <krun.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>
41 #include <util/log.h>
42 #include <util/timer.h>
43 #include "iwfiletreemodel.h"
44 #include "iwfilelistmodel.h"
45 
46 using namespace bt;
47 
48 namespace kt
49 {
50 
51  FileView::FileView(QWidget *parent) : QTreeView(parent),curr_tc(0),model(0)
52  {
53  setContextMenuPolicy(Qt::CustomContextMenu);
54  setRootIsDecorated(false);
55  setSortingEnabled(true);
56  setAlternatingRowColors(true);
57  setSelectionMode(QAbstractItemView::ExtendedSelection);
58  setSelectionBehavior(QAbstractItemView::SelectRows);
59  setUniformRowHeights(true);
60 
61  proxy_model = new QSortFilterProxyModel(this);
62  proxy_model->setSortRole(Qt::UserRole);
63  setModel(proxy_model);
64 
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()));
79 
80  connect(this,SIGNAL(customContextMenuRequested(QPoint)),
81  this,SLOT(showContextMenu(QPoint)));
82  connect(this,SIGNAL(doubleClicked(QModelIndex)),
83  this,SLOT(onDoubleClicked(QModelIndex)));
84 
85  setEnabled(false);
86  show_list_of_files = false;
87  redraw = false;
88  }
89 
90 
91  FileView::~FileView()
92  {}
93 
94  void FileView::changeTC(bt::TorrentInterface* tc,KSharedConfigPtr cfg)
95  {
96  if (tc == curr_tc)
97  return;
98 
99  if (model)
100  {
101  saveState(cfg);
102  if (curr_tc)
103  expanded_state_map[curr_tc] = model->saveExpandedState(proxy_model,this);
104  }
105  proxy_model->setSourceModel(0);
106  delete model;
107  model = 0;
108  curr_tc = tc;
109  setEnabled(tc != 0);
110  if (tc)
111  {
112  connect(tc,SIGNAL(missingFilesMarkedDND(bt::TorrentInterface*)),
113  this,SLOT(onMissingFileMarkedDND(bt::TorrentInterface*)));
114 
115  if (show_list_of_files)
116  model = new IWFileListModel(tc,this);
117  else
118  model = new IWFileTreeModel(tc,this);
119 
120  proxy_model->setSourceModel(model);
121  setRootIsDecorated(tc->getStats().multi_file_torrent);
122  loadState(cfg);
123  QMap<bt::TorrentInterface*,QByteArray>::iterator i = expanded_state_map.find(tc);
124  if (i != expanded_state_map.end())
125  model->loadExpandedState(proxy_model,this,i.value());
126  else
127  expandAll();
128  }
129  else
130  {
131  proxy_model->setSourceModel(0);
132  model = 0;
133  }
134  }
135 
136  void FileView::onMissingFileMarkedDND(bt::TorrentInterface* tc)
137  {
138  if (curr_tc == tc)
139  model->missingFilesMarkedDND();
140  }
141 
142  void FileView::showContextMenu(const QPoint & p)
143  {
144  const TorrentStats & s = curr_tc->getStats();
145 
146  QModelIndexList sel = selectionModel()->selectedRows();
147  if (sel.count() == 0)
148  return;
149 
150  if (sel.count() > 1)
151  {
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);
162  return;
163  }
164 
165  QModelIndex item = proxy_model->mapToSource(sel.front());
166  bt::TorrentFileInterface* file = model->indexToFile(item);
167 
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);
173 
174  if (!s.multi_file_torrent)
175  {
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);
181  }
182  else if (file)
183  {
184  move_files_action->setEnabled(true);
185  collapse_action->setEnabled(false);
186  expand_action->setEnabled(false);
187  if (!file->isNull())
188  {
189  open_action->setEnabled(true);
190  preview_path = file->getPathOnDisk();
191 
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);
197  }
198  else
199  {
200  open_action->setEnabled(false);
201  }
202  }
203  else
204  {
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);
215  }
216 
217  context_menu->popup(mapToGlobal(p));
218  }
219 
220  void FileView::open()
221  {
222  new KRun(KUrl(preview_path), 0, 0, true, true);
223  }
224 
225  void FileView::changePriority(bt::Priority newpriority)
226  {
227  QModelIndexList sel = selectionModel()->selectedRows(2);
228  for (QModelIndexList::iterator i = sel.begin();i != sel.end();++i)
229  *i = proxy_model->mapToSource(*i);
230 
231  model->changePriority(sel,newpriority);
232  proxy_model->invalidate();
233  }
234 
235 
236  void FileView::downloadFirst()
237  {
238  changePriority(FIRST_PRIORITY);
239  }
240 
241  void FileView::downloadLast()
242  {
243  changePriority(LAST_PRIORITY);
244  }
245 
246  void FileView::downloadNormal()
247  {
248  changePriority(NORMAL_PRIORITY);
249  }
250 
251  void FileView::doNotDownload()
252  {
253  changePriority(ONLY_SEED_PRIORITY);
254  }
255 
256  void FileView::deleteFiles()
257  {
258  QModelIndexList sel = selectionModel()->selectedRows();
259  Uint32 n = sel.count();
260  if (n == 1) // single item can be a directory
261  {
262  if (!model->indexToFile(proxy_model->mapToSource(sel.front())))
263  ++n;
264  }
265 
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);
268 
269  if (KMessageBox::warningYesNo(0,msg) == KMessageBox::Yes)
270  changePriority(EXCLUDED);
271  }
272 
273  void FileView::moveFiles()
274  {
275  if (curr_tc->getStats().multi_file_torrent)
276  {
277  QModelIndexList sel = selectionModel()->selectedRows();
278  QMap<bt::TorrentFileInterface*,QString> moves;
279 
280  QString dir = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///saveTorrentData"),
281  this,i18n("Select a directory to move the data to."));
282  if (dir.isNull())
283  return;
284 
285  foreach (const QModelIndex &idx,sel)
286  {
287  bt::TorrentFileInterface* tfi = model->indexToFile(proxy_model->mapToSource(idx));
288  if (!tfi)
289  continue;
290 
291  moves.insert(tfi,dir);
292  }
293 
294  if (moves.count() > 0)
295  {
296  curr_tc->moveTorrentFiles(moves);
297  }
298  }
299  else
300  {
301  QString dir = KFileDialog::getExistingDirectory(KUrl("kfiledialog:///saveTorrentData"),
302  this,i18n("Select a directory to move the data to."));
303  if (dir.isNull())
304  return;
305 
306  curr_tc->changeOutputDir(dir,bt::TorrentInterface::MOVE_FILES);
307  }
308  }
309 
310  void FileView::expandCollapseTree(const QModelIndex& idx, bool expand)
311  {
312  int rowCount = proxy_model->rowCount(idx);
313  for (int i = 0; i < rowCount; i++)
314  {
315  const QModelIndex& ridx = proxy_model->index(i, 0, idx);
316  if (proxy_model->hasChildren(ridx))
317  expandCollapseTree(ridx, expand);
318  }
319  setExpanded(idx, expand);
320  }
321 
322  void FileView::expandCollapseSelected(bool expand)
323  {
324  QModelIndexList sel = selectionModel()->selectedRows();
325  for (QModelIndexList::iterator i = sel.begin(); i != sel.end(); ++i)
326  {
327  if (proxy_model->hasChildren(*i))
328  expandCollapseTree(*i, expand);
329  }
330  }
331 
332  void FileView::collapseTree()
333  {
334  expandCollapseSelected(false);
335  }
336 
337  void FileView::expandTree()
338  {
339  expandCollapseSelected(true);
340  }
341 
342  void FileView::onDoubleClicked(const QModelIndex & index)
343  {
344  if (!curr_tc)
345  return;
346 
347  const TorrentStats & s = curr_tc->getStats();
348 
349  if (s.multi_file_torrent)
350  {
351  bt::TorrentFileInterface* file = model->indexToFile(proxy_model->mapToSource(index));
352  if (!file)
353  {
354  // directory
355  new KRun(KUrl(curr_tc->getDataDir() + model->dirPath(proxy_model->mapToSource(index))), 0, 0, true, true);
356  }
357  else
358  {
359  // file
360  new KRun(KUrl(file->getPathOnDisk()), 0, 0, true, true);
361  }
362  }
363  else
364  {
365  new KRun(KUrl(curr_tc->getStats().output_path), 0, 0, true, true);
366  }
367  }
368 
369  void FileView::saveState(KSharedConfigPtr cfg)
370  {
371  if (!model)
372  return;
373 
374  KConfigGroup g = cfg->group("FileView");
375  QByteArray s = header()->saveState();
376  g.writeEntry("state",s.toBase64());
377  }
378 
379  void FileView::loadState(KSharedConfigPtr cfg)
380  {
381  KConfigGroup g = cfg->group("FileView");
382  QByteArray s = QByteArray::fromBase64(g.readEntry("state",QByteArray()));
383  if (!s.isNull())
384  {
385  QHeaderView* v = header();
386  v->restoreState(s);
387  sortByColumn(v->sortIndicatorSection(),v->sortIndicatorOrder());
388  }
389  }
390 
391  void FileView::update()
392  {
393  if (model)
394  model->update();
395 
396  if (redraw)
397  {
398  scheduleDelayedItemsLayout();
399  redraw = false;
400  }
401  }
402 
403  void FileView::onTorrentRemoved(bt::TorrentInterface* tc)
404  {
405  expanded_state_map.remove(tc);
406  }
407 
408  void FileView::setShowListOfFiles(bool on,KSharedConfigPtr cfg)
409  {
410  if (show_list_of_files == on)
411  return;
412 
413  show_list_of_files = on;
414  if (!model || !curr_tc)
415  return;
416 
417  saveState(cfg);
418  expanded_state_map[curr_tc] = model->saveExpandedState(proxy_model,this);
419 
420  proxy_model->setSourceModel(0);
421  delete model;
422  model = 0;
423 
424  if (show_list_of_files)
425  model = new IWFileListModel(curr_tc,this);
426  else
427  model = new IWFileTreeModel(curr_tc,this);
428 
429  proxy_model->setSourceModel(model);
430  setRootIsDecorated(curr_tc->getStats().multi_file_torrent);
431  loadState(cfg);
432  QMap<bt::TorrentInterface*,QByteArray>::iterator i = expanded_state_map.find(curr_tc);
433  if (i != expanded_state_map.end())
434  model->loadExpandedState(proxy_model,this,i.value());
435  else
436  expandAll();
437 
438  collapse_action->setEnabled(!show_list_of_files);
439  expand_action->setEnabled(!show_list_of_files);
440  }
441 
442  bool FileView::viewportEvent(QEvent *event)
443  {
444  executeDelayedItemsLayout();
445  return QTreeView::viewportEvent(event);
446  }
447 
448  void FileView::filePercentageChanged(bt::TorrentFileInterface* file,float percentage)
449  {
450  if (model)
451  model->filePercentageChanged(file,percentage);
452  }
453 
454  void FileView::filePreviewChanged(bt::TorrentFileInterface* file,bool preview)
455  {
456  if (model)
457  model->filePreviewChanged(file,preview);
458  }
459 
460  void FileView::dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight)
461  {
462  Q_UNUSED(topLeft)
463  Q_UNUSED(bottomRight)
464  redraw = true;
465  }
466 }
467 
468 #include "fileview.moc"
iwfiletreemodel.h
kt::TorrentFileModel::filePercentageChanged
virtual void filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
Definition: torrentfilemodel.cpp:71
kt::FileView::filePercentageChanged
void filePercentageChanged(bt::TorrentFileInterface *file, float percentage)
Definition: fileview.cpp:448
QWidget
fileview.h
kt::TorrentFileModel::missingFilesMarkedDND
virtual void missingFilesMarkedDND()
Missing files have been marked DND, update the preview and selection information. ...
Definition: torrentfilemodel.cpp:43
kt::FileView::setShowListOfFiles
void setShowListOfFiles(bool on, KSharedConfigPtr cfg)
Definition: fileview.cpp:408
kt::FileView::filePreviewChanged
void filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
Definition: fileview.cpp:454
kt::FileView::loadState
void loadState(KSharedConfigPtr cfg)
Definition: fileview.cpp:379
kt::TorrentFileModel::dirPath
virtual QString dirPath(const QModelIndex &idx)=0
Get the path of a directory (root directory not included)
kt::FileView::update
void update()
Definition: fileview.cpp:391
QTreeView
iwfilelistmodel.h
kt::IWFileListModel
Definition: iwfilelistmodel.h:35
kt::IWFileTreeModel
Definition: iwfiletreemodel.h:35
kt::TorrentFileModel::loadExpandedState
virtual void loadExpandedState(QSortFilterProxyModel *pm, QTreeView *tv, const QByteArray &state)
Retore the expanded state of the tree.in a QTreeView.
Definition: torrentfilemodel.cpp:40
QSortFilterProxyModel
kt::TorrentFileModel::update
virtual void update()
Update gui if necessary.
Definition: torrentfilemodel.cpp:48
kt::FileView::~FileView
virtual ~FileView()
Definition: fileview.cpp:91
kt::TorrentFileModel::filePreviewChanged
virtual void filePreviewChanged(bt::TorrentFileInterface *file, bool preview)
Definition: torrentfilemodel.cpp:77
kt::FileView::changeTC
void changeTC(bt::TorrentInterface *tc, KSharedConfigPtr cfg)
Definition: fileview.cpp:94
kt::FileView::onTorrentRemoved
void onTorrentRemoved(bt::TorrentInterface *tc)
Definition: fileview.cpp:403
kt::FileView::saveState
void saveState(KSharedConfigPtr cfg)
Definition: fileview.cpp:369
kt::TorrentFileModel::changePriority
virtual void changePriority(const QModelIndexList &indexes, bt::Priority newpriority)=0
Change the priority of a bunch of items.
kt::TorrentFileModel::saveExpandedState
virtual QByteArray saveExpandedState(QSortFilterProxyModel *pm, QTreeView *tv)
Save which items are expanded.
Definition: torrentfilemodel.cpp:35
kt::TorrentFileModel::indexToFile
virtual bt::TorrentFileInterface * indexToFile(const QModelIndex &idx)=0
Convert a model index to a file.
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal