| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <cstdio> |
| 24 |
#include <iostream> |
| 25 |
|
| 26 |
#include <QByteArray> |
| 27 |
#include <QDockWidget> |
| 28 |
#include <QFileInfo> |
| 29 |
#include <QNetworkRequest> |
| 30 |
#include <QNetworkReply> |
| 31 |
#include <QMetaEnum> |
| 32 |
#include <QSettings> |
| 33 |
#include <QFileIconProvider> |
| 34 |
#include <QUrlQuery> |
| 35 |
|
| 36 |
#include "DownloadManager.h" |
| 37 |
#include "ui_DownloadManager.h" |
| 38 |
#include "DockWindowManager.h" |
| 39 |
#include "DownloadItem.h" |
| 40 |
#include "MainWindow.h" |
| 41 |
|
| 42 |
|
| 43 |
using namespace Gui::Dialog; |
| 44 |
|
| 45 |
/* TRANSLATOR Gui::Dialog::DownloadManager */ |
| 46 |
|
| 47 |
DownloadManager* DownloadManager::self = nullptr; |
| 48 |
|
| 49 |
DownloadManager* DownloadManager::getInstance() |
| 50 |
{ |
| 51 |
if (!self) { |
| 52 |
self = new DownloadManager(Gui::getMainWindow()); |
| 53 |
} |
| 54 |
return self; |
| 55 |
} |
| 56 |
|
| 57 |
DownloadManager::DownloadManager(QWidget* parent) |
| 58 |
: QDialog(parent) |
| 59 |
, m_autoSaver(new AutoSaver(this)) |
| 60 |
, m_manager(new NetworkAccessManager(this)) |
| 61 |
, m_iconProvider(nullptr) |
| 62 |
, m_removePolicy(Never) |
| 63 |
, ui(new Ui_DownloadManager()) |
| 64 |
{ |
| 65 |
ui->setupUi(this); |
| 66 |
ui->downloadsView->setShowGrid(false); |
| 67 |
ui->downloadsView->verticalHeader()->hide(); |
| 68 |
ui->downloadsView->horizontalHeader()->hide(); |
| 69 |
ui->downloadsView->setAlternatingRowColors(true); |
| 70 |
ui->downloadsView->horizontalHeader()->setStretchLastSection(true); |
| 71 |
m_model = new DownloadModel(this); |
| 72 |
ui->downloadsView->setModel(m_model); |
| 73 |
connect(ui->cleanupButton, &QPushButton::clicked, this, &DownloadManager::cleanup); |
| 74 |
load(); |
| 75 |
|
| 76 |
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance(); |
| 77 |
QDockWidget* dw |
| 78 |
= pDockMgr->addDockWindow(QT_TR_NOOP("Download Manager"), this, Qt::BottomDockWidgetArea); |
| 79 |
dw->setFeatures( |
| 80 |
QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable |
| 81 |
| QDockWidget::DockWidgetClosable |
| 82 |
); |
| 83 |
dw->setAttribute(Qt::WA_DeleteOnClose); |
| 84 |
dw->show(); |
| 85 |
} |
| 86 |
|
| 87 |
DownloadManager::~DownloadManager() |
| 88 |
{ |
| 89 |
m_autoSaver->changeOccurred(); |
| 90 |
m_autoSaver->saveIfNecessary(); |
| 91 |
if (m_iconProvider) { |
| 92 |
delete m_iconProvider; |
| 93 |
} |
| 94 |
delete ui; |
| 95 |
self = nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
void DownloadManager::closeEvent(QCloseEvent* e) |
| 99 |
{ |
| 100 |
QDialog::closeEvent(e); |
| 101 |
} |
| 102 |
|
| 103 |
int DownloadManager::activeDownloads() const |
| 104 |
{ |
| 105 |
int count = 0; |
| 106 |
for (int i = 0; i < m_downloads.count(); ++i) { |
| 107 |
if (m_downloads.at(i)->stopButton->isEnabled()) { |
| 108 |
++count; |
| 109 |
} |
| 110 |
} |
| 111 |
return count; |
| 112 |
} |
| 113 |
|
| 114 |
QUrl DownloadManager::redirectUrl(const QUrl& url) const |
| 115 |
{ |
| 116 |
QUrl redirectUrl = url; |
| 117 |
if (url.host() == QLatin1String("www.dropbox.com")) { |
| 118 |
QUrlQuery urlQuery(url); |
| 119 |
QList<QPair<QString, QString>> query = urlQuery.queryItems(); |
| 120 |
for (const auto& it : query) { |
| 121 |
if (it.first == QLatin1String("dl")) { |
| 122 |
if (it.second == QLatin1String("0\r\n")) { |
| 123 |
urlQuery.removeQueryItem(QLatin1String("dl")); |
| 124 |
urlQuery.addQueryItem(QLatin1String("dl"), QLatin1String("1\r\n")); |
| 125 |
} |
| 126 |
else if (it.second == QLatin1String("0")) { |
| 127 |
urlQuery.removeQueryItem(QLatin1String("dl")); |
| 128 |
urlQuery.addQueryItem(QLatin1String("dl"), QLatin1String("1")); |
| 129 |
} |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
redirectUrl.setQuery(urlQuery); |
| 134 |
} |
| 135 |
else { |
| 136 |
// When the url comes from drag and drop it may end with CR+LF. This may cause problems |
| 137 |
// and thus should be removed. |
| 138 |
QString str = redirectUrl.toString(); |
| 139 |
if (str.endsWith(QLatin1String("\r\n"))) { |
| 140 |
str.chop(2); |
| 141 |
redirectUrl.setUrl(str); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return redirectUrl; |
| 146 |
} |
| 147 |
|
| 148 |
void DownloadManager::download(const QNetworkRequest& request, bool requestFileName) |
| 149 |
{ |
| 150 |
if (request.url().isEmpty()) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
std::cout << request.url().toString().toStdString() << std::endl; |
| 155 |
handleUnsupportedContent(m_manager->get(request), requestFileName); |
| 156 |
} |
| 157 |
|
| 158 |
void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, bool requestFileName) |
| 159 |
{ |
| 160 |
if (!reply || reply->url().isEmpty()) { |
| 161 |
return; |
| 162 |
} |
| 163 |
QVariant header = reply->header(QNetworkRequest::ContentLengthHeader); |
| 164 |
bool ok; |
| 165 |
int size = header.toInt(&ok); |
| 166 |
if (ok && size == 0) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
auto item = new DownloadItem(reply, requestFileName, this); |
| 171 |
addItem(item); |
| 172 |
} |
| 173 |
|
| 174 |
void DownloadManager::addItem(DownloadItem* item) |
| 175 |
{ |
| 176 |
connect(item, &DownloadItem::statusChanged, this, &DownloadManager::updateRow); |
| 177 |
int row = m_downloads.count(); |
| 178 |
m_model->beginInsertRows(QModelIndex(), row, row); |
| 179 |
m_downloads.append(item); |
| 180 |
m_model->endInsertRows(); |
| 181 |
updateItemCount(); |
| 182 |
show(); |
| 183 |
ui->downloadsView->setIndexWidget(m_model->index(row, 0), item); |
| 184 |
QIcon icon = style()->standardIcon(QStyle::SP_FileIcon); |
| 185 |
item->fileIcon->setPixmap(icon.pixmap(48, 48)); |
| 186 |
ui->downloadsView->setRowHeight(row, item->sizeHint().height()); |
| 187 |
} |
| 188 |
|
| 189 |
void DownloadManager::updateRow() |
| 190 |
{ |
| 191 |
auto item = qobject_cast<DownloadItem*>(sender()); |
| 192 |
int row = m_downloads.indexOf(item); |
| 193 |
if (-1 == row) { |
| 194 |
return; |
| 195 |
} |
| 196 |
if (!m_iconProvider) { |
| 197 |
m_iconProvider = new QFileIconProvider(); |
| 198 |
} |
| 199 |
QIcon icon = m_iconProvider->icon(QFileInfo(item->m_output.fileName())); |
| 200 |
if (icon.isNull()) { |
| 201 |
icon = style()->standardIcon(QStyle::SP_FileIcon); |
| 202 |
} |
| 203 |
item->fileIcon->setPixmap(icon.pixmap(48, 48)); |
| 204 |
ui->downloadsView->setRowHeight(row, item->minimumSizeHint().height()); |
| 205 |
|
| 206 |
bool remove = false; |
| 207 |
if (item->downloadedSuccessfully() && removePolicy() == DownloadManager::SuccessFullDownload) { |
| 208 |
remove = true; |
| 209 |
} |
| 210 |
if (remove) { |
| 211 |
m_model->removeRow(row); |
| 212 |
} |
| 213 |
|
| 214 |
ui->cleanupButton->setEnabled(m_downloads.count() - activeDownloads() > 0); |
| 215 |
} |
| 216 |
|
| 217 |
DownloadManager::RemovePolicy DownloadManager::removePolicy() const |
| 218 |
{ |
| 219 |
return m_removePolicy; |
| 220 |
} |
| 221 |
|
| 222 |
void DownloadManager::setRemovePolicy(RemovePolicy policy) |
| 223 |
{ |
| 224 |
if (policy == m_removePolicy) { |
| 225 |
return; |
| 226 |
} |
| 227 |
m_removePolicy = policy; |
| 228 |
m_autoSaver->changeOccurred(); |
| 229 |
} |
| 230 |
|
| 231 |
void DownloadManager::save() const |
| 232 |
{ |
| 233 |
QSettings settings; |
| 234 |
settings.beginGroup(QLatin1String("downloadmanager")); |
| 235 |
QMetaEnum removePolicyEnum = staticMetaObject.enumerator( |
| 236 |
staticMetaObject.indexOfEnumerator("RemovePolicy") |
| 237 |
); |
| 238 |
settings.setValue( |
| 239 |
QLatin1String("removeDownloadsPolicy"), |
| 240 |
QLatin1String(removePolicyEnum.valueToKey(m_removePolicy)) |
| 241 |
); |
| 242 |
settings.setValue(QLatin1String("size"), size()); |
| 243 |
if (m_removePolicy == Exit) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
for (int i = 0; i < m_downloads.count(); ++i) { |
| 248 |
QString key = QString(QLatin1String("download_%1_")).arg(i); |
| 249 |
settings.setValue(key + QLatin1String("url"), m_downloads[i]->m_url); |
| 250 |
settings.setValue( |
| 251 |
key + QLatin1String("location"), |
| 252 |
QFileInfo(m_downloads[i]->m_output).filePath() |
| 253 |
); |
| 254 |
settings.setValue(key + QLatin1String("done"), m_downloads[i]->downloadedSuccessfully()); |
| 255 |
} |
| 256 |
int i = m_downloads.count(); |
| 257 |
QString key = QString(QLatin1String("download_%1_")).arg(i); |
| 258 |
while (settings.contains(key + QLatin1String("url"))) { |
| 259 |
settings.remove(key + QLatin1String("url")); |
| 260 |
settings.remove(key + QLatin1String("location")); |
| 261 |
settings.remove(key + QLatin1String("done")); |
| 262 |
key = QString(QLatin1String("download_%1_")).arg(++i); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void DownloadManager::load() |
| 267 |
{ |
| 268 |
QSettings settings; |
| 269 |
settings.beginGroup(QLatin1String("downloadmanager")); |
| 270 |
QSize size = settings.value(QLatin1String("size")).toSize(); |
| 271 |
if (size.isValid()) { |
| 272 |
resize(size); |
| 273 |
} |
| 274 |
QByteArray value |
| 275 |
= settings.value(QLatin1String("removeDownloadsPolicy"), QLatin1String("Never")).toByteArray(); |
| 276 |
QMetaEnum removePolicyEnum = staticMetaObject.enumerator( |
| 277 |
staticMetaObject.indexOfEnumerator("RemovePolicy") |
| 278 |
); |
| 279 |
m_removePolicy = removePolicyEnum.keyToValue(value) == -1 |
| 280 |
? Never |
| 281 |
: static_cast<RemovePolicy>(removePolicyEnum.keyToValue(value)); |
| 282 |
|
| 283 |
int i = 0; |
| 284 |
QString key = QString(QLatin1String("download_%1_")).arg(i); |
| 285 |
while (settings.contains(key + QLatin1String("url"))) { |
| 286 |
QUrl url = settings.value(key + QLatin1String("url")).toUrl(); |
| 287 |
QString fileName = settings.value(key + QLatin1String("location")).toString(); |
| 288 |
bool done = settings.value(key + QLatin1String("done"), true).toBool(); |
| 289 |
if (!url.isEmpty() && !fileName.isEmpty()) { |
| 290 |
auto item = new DownloadItem(nullptr, false, this); |
| 291 |
item->m_output.setFileName(fileName); |
| 292 |
item->fileNameLabel->setText(QFileInfo(item->m_output.fileName()).fileName()); |
| 293 |
item->m_url = url; |
| 294 |
item->stopButton->setVisible(false); |
| 295 |
item->stopButton->setEnabled(false); |
| 296 |
item->tryAgainButton->setVisible(!done); |
| 297 |
item->tryAgainButton->setEnabled(!done); |
| 298 |
item->progressBar->setVisible(!done); |
| 299 |
addItem(item); |
| 300 |
} |
| 301 |
key = QString(QLatin1String("download_%1_")).arg(++i); |
| 302 |
} |
| 303 |
ui->cleanupButton->setEnabled(m_downloads.count() - activeDownloads() > 0); |
| 304 |
} |
| 305 |
|
| 306 |
void DownloadManager::cleanup() |
| 307 |
{ |
| 308 |
if (m_downloads.isEmpty()) { |
| 309 |
return; |
| 310 |
} |
| 311 |
m_model->removeRows(0, m_downloads.count()); |
| 312 |
updateItemCount(); |
| 313 |
if (m_downloads.isEmpty() && m_iconProvider) { |
| 314 |
delete m_iconProvider; |
| 315 |
m_iconProvider = nullptr; |
| 316 |
} |
| 317 |
m_autoSaver->changeOccurred(); |
| 318 |
} |
| 319 |
|
| 320 |
void DownloadManager::updateItemCount() |
| 321 |
{ |
| 322 |
int count = m_downloads.count(); |
| 323 |
ui->itemCount->setText(count == 1 ? tr("1 Download") : tr("%1 Downloads").arg(count)); |
| 324 |
} |
| 325 |
|
| 326 |
// ---------------------------------------------------------------------------- |
| 327 |
|
| 328 |
DownloadModel::DownloadModel(DownloadManager* downloadManager, QObject* parent) |
| 329 |
: QAbstractListModel(parent) |
| 330 |
, m_downloadManager(downloadManager) |
| 331 |
{} |
| 332 |
|
| 333 |
QVariant DownloadModel::data(const QModelIndex& index, int role) const |
| 334 |
{ |
| 335 |
if (index.row() < 0 || index.row() >= rowCount(index.parent())) { |
| 336 |
return {}; |
| 337 |
} |
| 338 |
if (role == Qt::ToolTipRole) { |
| 339 |
if (!m_downloadManager->m_downloads.at(index.row())->downloadedSuccessfully()) { |
| 340 |
return m_downloadManager->m_downloads.at(index.row())->downloadInfoLabel->text(); |
| 341 |
} |
| 342 |
} |
| 343 |
return {}; |
| 344 |
} |
| 345 |
|
| 346 |
int DownloadModel::rowCount(const QModelIndex& parent) const |
| 347 |
{ |
| 348 |
return (parent.isValid()) ? 0 : m_downloadManager->m_downloads.count(); |
| 349 |
} |
| 350 |
|
| 351 |
bool DownloadModel::removeRows(int row, int count, const QModelIndex& parent) |
| 352 |
{ |
| 353 |
if (parent.isValid()) { |
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
int lastRow = row + count - 1; |
| 358 |
for (int i = lastRow; i >= row; --i) { |
| 359 |
if (m_downloadManager->m_downloads.at(i)->downloadedSuccessfully() |
| 360 |
|| m_downloadManager->m_downloads.at(i)->tryAgainButton->isEnabled()) { |
| 361 |
beginRemoveRows(parent, i, i); |
| 362 |
m_downloadManager->m_downloads.takeAt(i)->deleteLater(); |
| 363 |
endRemoveRows(); |
| 364 |
} |
| 365 |
} |
| 366 |
m_downloadManager->m_autoSaver->changeOccurred(); |
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
#include "moc_DownloadManager.cpp" |