summaryrefslogtreecommitdiff
path: root/src/downloadlist.cpp
blob: a44850cb41eb5ee5222c5b0dba43f22e385630ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "downloadlist.h"
#include "downloadmanager.h"
#include <QEvent>

#include <QSortFilterProxyModel>


DownloadList::DownloadList(DownloadManager *manager, QObject *parent)
  : QAbstractTableModel(parent), m_Manager(manager)
{
  connect(m_Manager, SIGNAL(update(int)), this, SLOT(update(int)));
  connect(m_Manager, SIGNAL(aboutToUpdate()), this, SLOT(aboutToUpdate()));
}


int DownloadList::rowCount(const QModelIndex&) const
{
  return m_Manager->numTotalDownloads() + m_Manager->numPendingDownloads();
}


int DownloadList::columnCount(const QModelIndex&) const
{
  return 3;
}


QModelIndex DownloadList::index(int row, int column, const QModelIndex&) const
{
  return createIndex(row, column, row);
}


QModelIndex DownloadList::parent(const QModelIndex&) const
{
  return QModelIndex();
}


QVariant DownloadList::headerData(int section, Qt::Orientation orientation, int role) const
{
  if ((role == Qt::DisplayRole) &&
      (orientation == Qt::Horizontal)) {
    switch (section) {
      case COL_NAME   : return tr("Name");
      case COL_SIZE   : return tr("Size");
      case COL_STATUS : return tr("Status");
      default         : return "-";
    }
  } else {
    return QAbstractItemModel::headerData(section, orientation, role);
  }
}

QVariant DownloadList::data(const QModelIndex &index, int role) const
{
  if (role == Qt::DisplayRole) {
    if (index.column() == COL_NAME) {
      return m_Manager->getFileName(index.row());
    } else if (index.column() == COL_SIZE) {
      return sizeFormat(m_Manager->getFileSize(index.row()));
    } else if (index.column() == COL_STATUS) {
      DownloadManager::DownloadState state = m_Manager->getState(index.row());
      switch (state) {
        case DownloadManager::STATE_INSTALLED   : return tr("Installed");
        case DownloadManager::STATE_UNINSTALLED : return tr("Uninstalled");
        case DownloadManager::STATE_READY       : return tr("Downloaded");
        case DownloadManager::STATE_DOWNLOADING : return m_Manager->getProgress(index.row()).second;
        case DownloadManager::STATE_PAUSED      : return tr("Paused");
        default                                 : return state;
      }
    } else {
      return index.row();
    }
  } else if (role == Qt::ToolTipRole) {
    if (index.row() < m_Manager->numTotalDownloads()) {
      QString text = m_Manager->getFileName(index.row()) + "\n";
      if (m_Manager->isInfoIncomplete(index.row())) {
        text += tr("Information missing, please select \"Query Info\" from the context menu to re-retrieve.");
      } else {
        const MOBase::ModRepositoryFileInfo *info = m_Manager->getFileInfo(index.row());
        return QString("%1 (ID %2) %3<br><span>%4</span>").arg(info->modName).arg(m_Manager->getModID(index.row())).arg(info->version.canonicalString()).arg(info->description);
      }
      return text;
    } else {
      return tr("pending download");
    }
  } else {
    return QVariant();
  }
}


void DownloadList::aboutToUpdate()
{
  emit beginResetModel();
}


void DownloadList::update(int row)
{
  if (row < 0) {
    emit endResetModel();
  } else if (row < this->rowCount()) {
    emit dataChanged(this->index(row, 0, QModelIndex()), this->index(row, this->columnCount(QModelIndex())-1, QModelIndex()));
  } else {
    qCritical("invalid row %d in download list, update failed", row);
  }
}

QString DownloadList::sizeFormat(quint64 size) const
{
  qreal calc = size;
  QStringList list;
  list << "MB" << "GB" << "TB";

  QStringListIterator i(list);
  QString unit("KB");

  calc /= 1024.0;
  while (calc >= 1024.0 && i.hasNext())
  {
    unit = i.next();
    calc /= 1024.0;
  }

  return QString().setNum(calc, 'f', 2) + " " + unit;
}