• 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
  • core
filemodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19 
20 #include "filemodel.h"
21 
22 #include "signature.h"
23 #include "verifier.h"
24 
25 #include <KIcon>
26 #include <KLocale>
27 #include <KMimeType>
28 
29 FileItem::FileItem(const QString &name, FileItem *parent)
30  : m_name(name),
31  m_state(Qt::Checked),
32  m_status(Job::Stopped),
33  m_totalSize(0),
34  m_checkusmVerified(0),
35  m_signatureVerified(0),
36  m_parent(parent)
37 {
38 }
39 
40 FileItem::~FileItem()
41 {
42  qDeleteAll(m_childItems);
43 }
44 
45 void FileItem::appendChild(FileItem *child)
46 {
47  m_childItems.append(child);
48 }
49 
50 FileItem *FileItem::child(int row)
51 {
52  return m_childItems.value(row);
53 }
54 
55 int FileItem::childCount() const
56 {
57  return m_childItems.count();
58 }
59 
60 bool FileItem::isFile() const
61 {
62  return m_childItems.isEmpty();
63 }
64 
65 int FileItem::columnCount() const
66 {
67  return 5;
68 }
69 
70 QVariant FileItem::data(int column, int role) const
71 {
72  if (column == FileItem::File)
73  {
74  if (role == Qt::CheckStateRole)
75  {
76  return m_state;
77  }
78  else if (role == Qt::DisplayRole)
79  {
80  return m_name;
81  }
82  else if (role == Qt::DecorationRole)
83  {
84  if (m_mimeType.isNull()) {
85  if (isFile()) {
86  m_mimeType = KIcon(KMimeType::iconNameForUrl(KUrl(m_name)));
87  } else {
88  m_mimeType = KIcon("folder");
89  }
90  }
91 
92  return m_mimeType;
93  }
94  }
95  else if (column == FileItem::Status)
96  {
97  if ((role == Qt::DisplayRole) || (role == Qt::DecorationRole))
98  {
99  if (isFile()) {
100  return m_status;
101  }
102  }
103  }
104  else if (column == FileItem::Size)
105  {
106  if (role == Qt::DisplayRole)
107  {
108  return KIO::convertSize(m_totalSize);
109  }
110  } else if (column == FileItem::ChecksumVerified) {
111  if (role == Qt::DecorationRole) {
112  switch (m_checkusmVerified) {
113  case Verifier::Verified:
114  return KIcon("dialog-ok");
115  case Verifier::NotVerified:
116  return KIcon("dialog-error");
117  case Verifier::NoResult:
118  default:
119  return KIcon();
120  }
121  }
122  } else if (column == FileItem::SignatureVerified) {//TODO implement all cases
123  if (role == Qt::DecorationRole) {
124  switch (m_signatureVerified) {
125  case Signature::Verified:
126  return KIcon("dialog-ok");
127  case Signature::VerifiedInformation:
128  return KIcon("dialog-information");
129  case Signature::VerifiedWarning:
130  return KIcon("dialog-warning");
131  case Signature::NotVerified:
132  return KIcon("dialog-error");
133  case Signature::NoResult:
134  default:
135  return KIcon();
136  }
137  }
138  }
139 
140  return QVariant();
141 }
142 
143 bool FileItem::setData(int column, const QVariant &value, FileModel *model, int role)
144 {
145  if (value.isNull())
146  {
147  return false;
148  }
149 
150  if (column == FileItem::File)
151  {
152  if (role == Qt::CheckStateRole)
153  {
154  m_state = static_cast<Qt::CheckState>(value.toInt());
155  model->changeData(this->row(), column, this);
156  checkParents(m_state, model);
157  checkChildren(m_state, model);
158  return true;
159  }
160  else if (role == Qt::EditRole)
161  {
162  m_name = value.toString();
163  model->changeData(this->row(), column, this);
164  return true;
165  }
166  }
167  else if (column == FileItem::Status)
168  {
169  if (role == Qt::EditRole)
170  {
171  if (isFile()) {
172  m_status = static_cast<Job::Status>(value.toInt());
173  bool finished = (m_status == Job::Finished);
174  model->changeData(this->row(), column, this, finished);
175 
176  return true;
177  }
178  }
179  }
180  else if (column == FileItem::Size)
181  {
182  if (role == Qt::EditRole)
183  {
184  KIO::fileoffset_t newSize = value.toLongLong();
185  if (m_parent)
186  {
187  m_parent->addSize(newSize - m_totalSize, model);
188  }
189  m_totalSize = newSize;
190  model->changeData(this->row(), column, this);
191  return true;
192  }
193  } else if (column == FileItem::ChecksumVerified) {
194  m_checkusmVerified = value.toInt();
195  model->changeData(this->row(), column, this);
196  return true;
197  } else if (column == FileItem::SignatureVerified) {
198  m_signatureVerified = value.toInt();
199  model->changeData(this->row(), column, this);
200  return true;
201  }
202 
203  return false;
204 }
205 
206 void FileItem::checkParents(Qt::CheckState state, FileModel *model)
207 {
208  if (!model)
209  {
210  return;
211  }
212 
213  if (!m_parent)
214  {
215  return;
216  }
217 
218  foreach (FileItem *child, m_parent->m_childItems)
219  {
220  if (child->m_state != state)
221  {
222  state = Qt::Unchecked;
223  break;
224  }
225  }
226 
227  m_parent->m_state = state;
228  model->changeData(m_parent->row(), FileItem::File, m_parent);
229  m_parent->checkParents(state, model);
230 }
231 
232 void FileItem::checkChildren(Qt::CheckState state, FileModel *model)
233 {
234  if (!model)
235  {
236  return;
237  }
238 
239  m_state = state;
240  model->changeData(row(), FileItem::File, this);
241 
242  foreach (FileItem *child, m_childItems)
243  {
244  child->checkChildren(state, model);
245  }
246 }
247 
248 FileItem *FileItem::parent()
249 {
250  return m_parent;
251 }
252 
253 int FileItem::row() const
254 {
255  if (m_parent)
256  {
257  return m_parent->m_childItems.indexOf(const_cast<FileItem*>(this));
258  }
259 
260  return 0;
261 }
262 
263 void FileItem::addSize(KIO::fileoffset_t size, FileModel *model)
264 {
265  if (!isFile())
266  {
267  m_totalSize += size;
268  model->changeData(this->row(), FileItem::Size, this);
269  if (m_parent)
270  {
271  m_parent->addSize(size, model);
272  }
273  }
274 }
275 
276 
277 FileModel::FileModel(const QList<KUrl> &files, const KUrl &destDirectory, QObject *parent)
278  : QAbstractItemModel(parent),
279  m_destDirectory(destDirectory),
280  m_checkStateChanged(false)
281 {
282  m_rootItem = new FileItem("root");
283  m_header << i18nc("file in a filesystem", "File") << i18nc("status of the download", "Status") << i18nc("size of the download", "Size") << i18nc("checksum of a file", "Checksum") << i18nc("signature of a file", "Signature");
284 
285  setupModelData(files);
286 }
287 
288 FileModel::~FileModel()
289 {
290  delete m_rootItem;
291 }
292 
293 void FileModel::setupModelData(const QList<KUrl> &files)
294 {
295  QString destDirectory = m_destDirectory.pathOrUrl();
296 
297  foreach (const KUrl &file, files)
298  {
299  FileItem *parent = m_rootItem;
300  QStringList directories = file.pathOrUrl().remove(destDirectory).split('/', QString::SkipEmptyParts);
301  FileItem *child = 0;
302  while (directories.count())
303  {
304  QString part = directories.takeFirst();
305  for (int i = 0; i < parent->childCount(); ++i)
306  {
307  //folder already exists
308  if (parent->child(i)->data(0, Qt::DisplayRole).toString() == part)
309  {
310  parent = parent->child(i);
311  //file already exists
312  if (!directories.count())
313  {
314  break;
315  }
316  part = directories.takeFirst();
317  i = -1;
318  continue;
319  }
320  }
321  child = new FileItem(part, parent);
322  parent->appendChild(child);
323  parent = parent->child(parent->childCount() - 1);
324  }
325  if (child)
326  {
327  m_files.append(child);
328  }
329  }
330 }
331 
332 int FileModel::columnCount(const QModelIndex &parent) const
333 {
334  if (parent.isValid())
335  {
336  return static_cast<FileItem*>(parent.internalPointer())->columnCount();
337  }
338  else
339  {
340  return m_rootItem->columnCount();
341  }
342 }
343 
344 QVariant FileModel::data(const QModelIndex &index, int role) const
345 {
346  if (!index.isValid())
347  {
348  return QVariant();
349  }
350 
351 
352  FileItem *item = static_cast<FileItem*>(index.internalPointer());
353  const QVariant data = item->data(index.column(), role);
354 
355  //get the status icon as well as status text
356  if (index.column() == FileItem::Status)
357  {
358  const Job::Status status = static_cast<Job::Status>(data.toInt());
359  if (item->isFile()) {
360  if (role == Qt::DisplayRole)
361  {
362  if (m_customStatusTexts.contains(status))
363  {
364  return m_customStatusTexts[status];
365  }
366  else
367  {
368  return Transfer::statusText(status);
369  }
370  }
371  else if (role == Qt::DecorationRole)
372  {
373  if (m_customStatusIcons.contains(status))
374  {
375  return m_customStatusIcons[status];
376  }
377  else
378  {
379  return Transfer::statusPixmap(status);
380  }
381  }
382  } else {
383  return QVariant();
384  }
385  }
386 
387  return data;
388 }
389 
390 bool FileModel::setData(const QModelIndex &index, const QVariant &value, int role)
391 {
392  if (!index.isValid())
393  {
394  return false;
395  }
396 
397  FileItem *item = static_cast<FileItem*>(index.internalPointer());
398 
399  if ((index.column() == FileItem::File) && (role == Qt::CheckStateRole)) {
400  const bool worked = item->setData(index.column(), value, this, role);
401  if (worked) {
402  m_checkStateChanged = true;
403  }
404 
405  return worked;
406  }
407 
408  return item->setData(index.column(), value, this, role);
409 }
410 
411 Qt::ItemFlags FileModel::flags(const QModelIndex &index) const
412 {
413  if (!index.isValid())
414  {
415  return 0;
416  }
417 
418  if (index.column() == FileItem::File)
419  {
420  return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
421  }
422 
423  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
424 }
425 
426 QVariant FileModel::headerData(int section, Qt::Orientation orientation, int role) const
427 {
428  if ((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
429  {
430  return m_header.value(section);
431  }
432 
433  return QVariant();
434 }
435 
436 QModelIndex FileModel::index(int row, int column, const QModelIndex &parent) const
437 {
438  if (!hasIndex(row, column, parent))
439  {
440  return QModelIndex();
441  }
442 
443  FileItem *parentItem;
444  if (parent.isValid())
445  {
446  parentItem = static_cast<FileItem*>(parent.internalPointer());
447  }
448  else
449  {
450  parentItem = m_rootItem;
451  }
452 
453  FileItem *childItem = parentItem->child(row);
454  if (childItem)
455  {
456  return createIndex(row, column, childItem);
457  }
458  else
459  {
460  return QModelIndex();
461  }
462 }
463 
464 QModelIndex FileModel::index(const KUrl &file, int column)
465 {
466  FileItem *item = getItem(file);
467  if (!item)
468  {
469  return QModelIndex();
470  }
471 
472  return createIndex(item->row(), column, item);
473 }
474 
475 QModelIndexList FileModel::fileIndexes(int column) const
476 {
477  QModelIndexList indexList;
478  foreach (FileItem *item, m_files)
479  {
480  int row = item->row();
481  indexList.append(createIndex(row, column, item));
482  }
483 
484  return indexList;
485 }
486 
487 QModelIndex FileModel::parent(const QModelIndex &index) const
488 {
489  if (!index.isValid())
490  {
491  return QModelIndex();
492  }
493 
494  FileItem *childItem = static_cast<FileItem*>(index.internalPointer());
495  FileItem *parentItem = childItem->parent();
496  if ((parentItem == m_rootItem) || (!parentItem))
497  {
498  return QModelIndex();
499  }
500  else
501  {
502  return createIndex(parentItem->row(), 0, parentItem);
503  }
504 }
505 
506 int FileModel::rowCount(const QModelIndex &parent) const
507 {
508  if (parent.column() > 0)
509  {
510  return 0;
511  }
512 
513  FileItem *parentItem;
514  if (parent.isValid())
515  {
516  parentItem = static_cast<FileItem*>(parent.internalPointer());
517  }
518  else
519  {
520  parentItem = m_rootItem;
521  }
522 
523  return parentItem->childCount();
524 }
525 
526 void FileModel::changeData(int row, int column, FileItem *item, bool finished)
527 {
528  QModelIndex index = createIndex(row, column, item);
529  emit dataChanged(index, index);
530 
531  if (finished) {
532  const KUrl file = getUrl(index);
533  emit fileFinished(file);
534  }
535 }
536 
537 
538 void FileModel::setDirectory(const KUrl &newDirectory)
539 {
540  m_destDirectory = newDirectory;
541  m_itemCache.clear();
542 }
543 
544 KUrl FileModel::getUrl(const QModelIndex &index)
545 {
546  if (!index.isValid()) {
547  return KUrl();
548  }
549 
550  const QModelIndex file = index.sibling(index.row(), FileItem::File);
551 
552  return getUrl(static_cast<FileItem*>(file.internalPointer()));
553 }
554 
555 KUrl FileModel::getUrl(FileItem *item)
556 {
557  const QString path = getPath(item);
558  const QString name = item->data(FileItem::File, Qt::DisplayRole).toString();
559  KUrl url = m_destDirectory;
560  url.addPath(path + name);
561 
562  return url;
563 }
564 
565 QString FileModel::getPath(FileItem *item)
566 {
567  FileItem *parent = item->parent();
568  QString path;
569  while (parent && parent->parent())
570  {
571  path = parent->data(FileItem::File, Qt::DisplayRole).toString() + '/' + path;
572  parent = parent->parent();
573  }
574 
575  return path;
576 }
577 
578 FileItem *FileModel::getItem(const KUrl &file)
579 {
580  if (m_itemCache.contains(file))
581  {
582  return m_itemCache[file];
583  }
584 
585  QString destDirectory = m_destDirectory.pathOrUrl();
586 
587  FileItem *item = m_rootItem;
588  QStringList directories = file.pathOrUrl().remove(destDirectory).split('/', QString::SkipEmptyParts);
589  while (directories.count())
590  {
591  QString part = directories.takeFirst();
592  for (int i = 0; i < item->childCount(); ++i)
593  {
594  //folder already exists
595  if (item->child(i)->data(FileItem::File, Qt::DisplayRole).toString() == part)
596  {
597  item = item->child(i);
598  //file already exists
599  if (!directories.count())
600  {
601  break;
602  }
603  part = directories.takeFirst();
604  i = -1;
605  continue;
606  }
607  }
608  }
609 
610  if (item == m_rootItem)
611  {
612  item = 0;
613  }
614  else
615  {
616  m_itemCache[file] = item;
617  }
618 
619  return item;
620 }
621 
622 bool FileModel::downloadFinished(const KUrl &file)
623 {
624  FileItem *item = getItem(file);
625  if (item)
626  {
627  const Job::Status status = static_cast<Job::Status>(item->data(FileItem::Status, Qt::DisplayRole).toInt());
628  if (status == Job::Finished)
629  {
630  return true;
631  }
632  }
633 
634  return false;
635 }
636 
637 bool FileModel::isFile(const QModelIndex &index) const
638 {
639  if (!index.isValid()) {
640  return false;
641  }
642 
643  FileItem *item = static_cast<FileItem*>(index.internalPointer());
644 
645  //only files can be renamed, no folders
646  return item->isFile();
647 }
648 
649 void FileModel::rename(const QModelIndex &file, const QString &newName)
650 {
651  if (!file.isValid() || (file.column() != FileItem::File))
652  {
653  return;
654  }
655 
656  FileItem *item = static_cast<FileItem*>(file.internalPointer());
657  //only files can be renamed, no folders
658  if (!item->isFile()) {
659  return;
660  }
661 
662  //Find out the old and the new KUrl
663  QString oldName = file.data(Qt::DisplayRole).toString();
664  QString path = getPath(item);
665 
666  KUrl oldUrl = m_destDirectory;
667  oldUrl.addPath(path + oldName);
668  KUrl newUrl = m_destDirectory;
669  newUrl.addPath(path + newName);
670 
671  m_itemCache.remove(oldUrl);
672 
673  setData(file, newName);
674 
675  emit rename(oldUrl, newUrl);
676 }
677 
678 void FileModel::renameFailed(const KUrl &beforeRename, const KUrl &afterRename)
679 {
680  Q_UNUSED(beforeRename)
681  Q_UNUSED(afterRename)
682 }
683 
684 void FileModel::watchCheckState()
685 {
686  m_checkStateChanged = false;
687 }
688 
689 void FileModel::stopWatchCheckState()
690 {
691  if (m_checkStateChanged)
692  {
693  emit checkStateChanged();
694  }
695 
696  m_checkStateChanged = false;
697 }
698 
FileModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: filemodel.cpp:411
FileItem::parent
FileItem * parent()
Definition: filemodel.cpp:248
FileItem::File
Definition: filemodel.h:45
Transfer::statusPixmap
QPixmap statusPixmap() const
Definition: transfer.h:173
Job::Finished
The job is stopped, but this also indicates that it stopped because an error occurred.
Definition: job.h:47
FileItem::appendChild
void appendChild(FileItem *child)
Definition: filemodel.cpp:45
Job::Status
Status
The status property describes the current job status.
Definition: job.h:42
FileItem::FileItem
FileItem(const QString &name, FileItem *parent=0)
Definition: filemodel.cpp:29
FileModel::stopWatchCheckState
void stopWatchCheckState()
Emits checkStateChanged if a CheckState of an entry changend.
Definition: filemodel.cpp:689
Verifier::Verified
Definition: verifier.h:80
FileItem::setData
bool setData(int column, const QVariant &value, FileModel *model, int role=Qt::EditRole)
Definition: filemodel.cpp:143
FileItem
Definition: filemodel.h:37
Job
Definition: job.h:35
Verifier::NoResult
Definition: verifier.h:78
FileModel::fileFinished
void fileFinished(const KUrl &file)
Signature::VerifiedInformation
Definition: signature.h:63
FileModel::parent
QModelIndex parent(const QModelIndex &index) const
Definition: filemodel.cpp:487
FileModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: filemodel.cpp:426
QObject
FileModel::fileIndexes
QModelIndexList fileIndexes(int column) const
Returns a list of pointers to all files of this model.
Definition: filemodel.cpp:475
FileModel::checkStateChanged
void checkStateChanged()
Signature::Verified
Definition: signature.h:62
FileModel::watchCheckState
void watchCheckState()
Watches if the check state changes, the result of that will be emitted when stopWatchCheckState() is ...
Definition: filemodel.cpp:684
FileModel
This model represents the files that are being downloaded.
Definition: filemodel.h:101
FileItem::columnCount
int columnCount() const
Definition: filemodel.cpp:65
FileModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: filemodel.cpp:506
FileModel::FileItem
friend class FileItem
Definition: filemodel.h:105
Signature::NoResult
Definition: signature.h:59
FileItem::child
FileItem * child(int row)
Definition: filemodel.cpp:50
FileItem::childCount
int childCount() const
Definition: filemodel.cpp:55
Signature::NotVerified
Definition: signature.h:61
FileModel::setDirectory
void setDirectory(const KUrl &newDirectory)
Set the url to the directory the files are stored in, the filemodel stores its entries as relative pa...
Definition: filemodel.cpp:538
signature.h
FileModel::FileModel
FileModel(const QList< KUrl > &files, const KUrl &destDirectory, QObject *parent=0)
Definition: filemodel.cpp:277
verifier.h
QAbstractItemModel
Signature::VerifiedWarning
Definition: signature.h:64
FileItem::ChecksumVerified
Definition: filemodel.h:48
Transfer::statusText
QString statusText() const
Definition: transfer.h:172
FileModel::rename
void rename(const QModelIndex &file, const QString &newName)
Definition: filemodel.cpp:649
FileModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: filemodel.cpp:344
FileModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: filemodel.cpp:390
FileItem::Status
Definition: filemodel.h:46
FileItem::SignatureVerified
Definition: filemodel.h:49
FileModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: filemodel.cpp:332
FileModel::downloadFinished
bool downloadFinished(const KUrl &file)
Checks if the download for file has been finished.
Definition: filemodel.cpp:622
FileItem::Size
Definition: filemodel.h:47
FileModel::getUrl
KUrl getUrl(const QModelIndex &index)
The url on the filesystem (no check if the file exists yet!) of index, it can be a folder or file...
Definition: filemodel.cpp:544
FileItem::data
QVariant data(int column, int role) const
Definition: filemodel.cpp:70
FileItem::isFile
bool isFile() const
Returns true if the index represents a file.
Definition: filemodel.cpp:60
FileModel::isFile
bool isFile(const QModelIndex &index) const
Returns true if the index represents a file.
Definition: filemodel.cpp:637
Verifier::NotVerified
Definition: verifier.h:79
FileItem::~FileItem
~FileItem()
Definition: filemodel.cpp:40
FileModel::~FileModel
~FileModel()
Definition: filemodel.cpp:288
FileModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: filemodel.cpp:436
filemodel.h
FileItem::row
int row() const
Definition: filemodel.cpp:253
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