You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qtransferlistmodel.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /***************************************************************************
  2. * Copyright (C) 2008 by Oliver Groß *
  3. * z.o.gross@gmx.de *
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "qtransferlistmodel.h"
  21. namespace qftrans {
  22. QTransferListModel::QTransferListModel(QTransferDataList * data, QObject * parent) : QAbstractListModel(parent), m_Data(data) {
  23. }
  24. QTransferListModel::~QTransferListModel() {
  25. }
  26. int QTransferListModel::rowCount(const QModelIndex & /*parent*/) const {
  27. if (m_Data)
  28. return m_Data->size();
  29. else
  30. return 0;
  31. }
  32. QVariant QTransferListModel::data(const QModelIndex & index, int role) const {
  33. if (role != Qt::DisplayRole)
  34. return QVariant();
  35. TransferData * data = static_cast<TransferData *>(index.internalPointer());
  36. if (!data)
  37. return QVariant();
  38. QString status;
  39. switch (data->status) {
  40. case TS_SUSPENDED:
  41. status = tr("suspended");
  42. break;
  43. case TS_PENDING:
  44. status = tr("pending");
  45. break;
  46. case TS_ANNOUNCING:
  47. status = tr("announcing to peer");
  48. break;
  49. case TS_WAITING:
  50. status = tr("waiting");
  51. break;
  52. case TS_TRANSFERING:
  53. status = data->localFile ? tr("Sending") : tr("Recieving");
  54. break;
  55. case TS_CLOSING:
  56. status = tr("closing down");
  57. break;
  58. case TS_FINISHED:
  59. status = (data->transfered == data->size) ? tr("finished") : tr("canceled");
  60. break;
  61. default:
  62. status = tr("unknown status");
  63. break;
  64. }
  65. QString size;
  66. unsigned int fancySize = data->size;
  67. unsigned int siCount = 0;
  68. while (fancySize > 1000) {
  69. fancySize /= 1000;
  70. siCount++;
  71. }
  72. size = QString::number(fancySize);
  73. switch (siCount) {
  74. case 0:
  75. break;
  76. case 1:
  77. size += 'K';
  78. break;
  79. case 2:
  80. size += 'M';
  81. break;
  82. case 3:
  83. size += 'G';
  84. break;
  85. default:
  86. size += "*10^" + QString::number(siCount);
  87. break;
  88. }
  89. size += 'B';
  90. return (data->localFile ? "out #" : "in #") + QString::number(index.row()) + " [" + status + "] (" +
  91. QString::number((unsigned int)((100.0 * data->transfered) / data->size)) + "%) " +
  92. data->fileName + " (size: " + size + ")";
  93. }
  94. QModelIndex QTransferListModel::index (int row, int column, const QModelIndex & /*parent*/) const {
  95. return createIndex(row, column, m_Data->at(row));
  96. }
  97. void QTransferListModel::updateEntry(TransferData * /*entry*/) {
  98. // TODO proper tranfer entry update
  99. emit layoutChanged();
  100. }
  101. }