summaryrefslogtreecommitdiff
path: root/src/downloadlistwidget.cpp
blob: 915ddda595deb038f306947625ed419bb5992d42 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
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 "downloadlistwidget.h"
#include "ui_downloadlistwidget.h"
#include <QPainter>
#include <QMouseEvent>
#include <QMenu>
#include <QMessageBox>
#include <QSortFilterProxyModel>


DownloadListWidget::DownloadListWidget(QWidget *parent)
  : QTreeView(parent)
{
  connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onDoubleClick(QModelIndex)));
  connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onCustomContextMenu(QPoint)));
}

DownloadListWidget::~DownloadListWidget()
{
}

void DownloadListWidget::setManager(DownloadManager *manager)
{
  m_Manager = manager;
}

void DownloadListWidget::onDoubleClick(const QModelIndex &index)
{
  QModelIndex sourceIndex = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index);
  if (m_Manager->getState(sourceIndex.row()) >= DownloadManager::STATE_READY) {
    emit installDownload(sourceIndex.row());
  } else if ((m_Manager->getState(sourceIndex.row()) >= DownloadManager::STATE_PAUSED) || (m_Manager->getState(sourceIndex.row()) == DownloadManager::STATE_PAUSING)) {
    emit resumeDownload(sourceIndex.row());
  }
}

void DownloadListWidget::onCustomContextMenu(const QPoint &point)
{
  QMenu menu(this);
  QModelIndex index = indexAt(point);
  bool hidden = false;
  if (index.row() >= 0) {
    m_ContextRow = qobject_cast<QSortFilterProxyModel*>(model())->mapToSource(index).row();
    DownloadManager::DownloadState state = m_Manager->getState(m_ContextRow);
    hidden = m_Manager->isHidden(m_ContextRow);
    if (state >= DownloadManager::STATE_READY) {
      menu.addAction(tr("Install"), this, SLOT(issueInstall()));
      if (m_Manager->isInfoIncomplete(m_ContextRow)) {
        menu.addAction(tr("Query Info"), this, SLOT(issueQueryInfo()));
      }
      else {
        menu.addAction(tr("Visit on Nexus"), this, SLOT(issueVisitOnNexus()));
      }

      menu.addAction(tr("Open File"), this, SLOT(issueOpenFile()));
      menu.addAction(tr("Show in Folder"), this, SLOT(issueOpenInDownloadsFolder()));

      menu.addSeparator();

      menu.addAction(tr("Delete"), this, SLOT(issueDelete()));
      if (hidden) {
        menu.addAction(tr("Un-Hide"), this, SLOT(issueRestoreToView()));
      }
      else {
        menu.addAction(tr("Hide"), this, SLOT(issueRemoveFromView()));
      }
    }
    else if (state == DownloadManager::STATE_DOWNLOADING) {
      menu.addAction(tr("Cancel"), this, SLOT(issueCancel()));
      menu.addAction(tr("Pause"), this, SLOT(issuePause()));
      menu.addAction(tr("Show in Folder"), this, SLOT(issueOpenInDownloadsFolder()));
    }
    else if ((state == DownloadManager::STATE_PAUSED) || (state == DownloadManager::STATE_ERROR) || (state == DownloadManager::STATE_PAUSING)) {
      menu.addAction(tr("Delete"), this, SLOT(issueDelete()));
      menu.addAction(tr("Resume"), this, SLOT(issueResume()));
      menu.addAction(tr("Show in Folder"), this, SLOT(issueOpenInDownloadsFolder()));
    }

    menu.addSeparator();
  }
  menu.addAction(tr("Delete Installed..."), this, SLOT(issueDeleteCompleted()));
  menu.addAction(tr("Delete Uninstalled..."), this, SLOT(issueDeleteUninstalled()));
  menu.addAction(tr("Delete All..."), this, SLOT(issueDeleteAll()));

  if (!hidden) {
    menu.addSeparator();
    menu.addAction(tr("Hide Installed..."), this, SLOT(issueRemoveFromViewCompleted()));
    menu.addAction(tr("Hide Uninstalled..."), this, SLOT(issueRemoveFromViewUninstalled()));
    menu.addAction(tr("Hide All..."), this, SLOT(issueRemoveFromViewAll()));
  }
  if (hidden) {
    menu.addSeparator();
    menu.addAction(tr("Un-Hide All..."), this, SLOT(issueRestoreToViewAll()));
  }

  menu.exec(viewport()->mapToGlobal(point));
}

void DownloadListWidget::issueInstall()
{
  emit installDownload(m_ContextRow);
}

void DownloadListWidget::issueQueryInfo()
{
  emit queryInfo(m_ContextRow);
}

void DownloadListWidget::issueDelete()
{
	if (QMessageBox::question(nullptr, tr("Delete Files?"),
		tr("This will permanently delete the selected download."),
		QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
		emit removeDownload(m_ContextRow, true);
	}
}

void DownloadListWidget::issueRemoveFromView()
{
  qDebug() << "removing from view: " << m_ContextRow;
  emit removeDownload(m_ContextRow, false);
}

void DownloadListWidget::issueRestoreToView()
{
		emit restoreDownload(m_ContextRow);
}

void DownloadListWidget::issueRestoreToViewAll()
{
	emit restoreDownload(-1);
}

void DownloadListWidget::issueVisitOnNexus()
{
	emit visitOnNexus(m_ContextRow);
}

void DownloadListWidget::issueOpenFile()
{
  emit openFile(m_ContextRow);
}

void DownloadListWidget::issueOpenInDownloadsFolder()
{
  emit openInDownloadsFolder(m_ContextRow);
}

void DownloadListWidget::issueCancel()
{
  emit cancelDownload(m_ContextRow);
}

void DownloadListWidget::issuePause()
{
  emit pauseDownload(m_ContextRow);
}

void DownloadListWidget::issueResume()
{
  emit resumeDownload(m_ContextRow);
}

void DownloadListWidget::issueDeleteAll()
{
  if (QMessageBox::question(nullptr, tr("Delete Files?"),
                            tr("This will remove all finished downloads from this list and from disk."),
                            QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    emit removeDownload(-1, true);
  }
}

void DownloadListWidget::issueDeleteCompleted()
{
  if (QMessageBox::question(nullptr, tr("Delete Files?"),
                            tr("This will remove all installed downloads from this list and from disk."),
                            QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    emit removeDownload(-2, true);
  }
}

void DownloadListWidget::issueDeleteUninstalled()
{
  if (QMessageBox::question(nullptr, tr("Delete Files?"),
    tr("This will remove all uninstalled downloads from this list and from disk."),
    QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    emit removeDownload(-3, true);
  }
}

void DownloadListWidget::issueRemoveFromViewAll()
{
  if (QMessageBox::question(nullptr, tr("Are you sure?"),
                            tr("This will remove all finished downloads from this list (but NOT from disk)."),
                            QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    emit removeDownload(-1, false);
  }
}

void DownloadListWidget::issueRemoveFromViewCompleted()
{
  if (QMessageBox::question(nullptr, tr("Are you sure?"),
                            tr("This will remove all installed downloads from this list (but NOT from disk)."),
                            QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    emit removeDownload(-2, false);
  }
}

void DownloadListWidget::issueRemoveFromViewUninstalled()
{
  if (QMessageBox::question(nullptr, tr("Are you sure?"),
    tr("This will remove all uninstalled downloads from this list (but NOT from disk)."),
    QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
    emit removeDownload(-3, false);
  }
}