1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-22 18:22:10 +02:00
falkonOfficial/src/downloads/downloadmanager.cpp

265 lines
8.3 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2011 nowrep
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "downloadmanager.h"
#include "ui_downloadmanager.h"
#include "qupzilla.h"
#include "downloadoptionsdialog.h"
#include "downloaditem.h"
#include "ecwin7.h"
DownloadManager::DownloadManager(QWidget *parent) :
QWidget(parent)
,ui(new Ui::DownloadManager)
,m_isClosing(false)
{
ui->setupUi(this);
ui->clearButton->setIcon(QIcon::fromTheme("edit-clear"));
//CENTER on screen
const QRect screen = QApplication::desktop()->screenGeometry();
const QRect &size = QWidget::geometry();
QWidget::move( (screen.width()-size.width())/2, (screen.height()-size.height())/2 );
m_iconProvider = new QFileIconProvider();
m_networkManager = new QNetworkAccessManager();
2011-03-04 13:59:07 +01:00
QSettings settings(mApp->getActiveProfil()+"settings.ini", QSettings::IniFormat);
2011-03-02 16:57:41 +01:00
settings.beginGroup("DownloadManager");
2011-03-05 11:28:09 +01:00
m_downloadPath = settings.value("defaultDownloadPath", "").toString();
2011-03-05 11:27:30 +01:00
m_lastDownloadPath = settings.value("lastDownloadPath",QDir::homePath()+"/").toString();
2011-03-02 16:57:41 +01:00
settings.endGroup();
connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clearList()));
2011-03-05 10:57:21 +01:00
#ifdef W7TASKBAR
2011-03-02 16:57:41 +01:00
win7.init(this->winId());
#endif
}
2011-03-05 10:57:21 +01:00
#ifdef W7TASKBAR
2011-03-02 16:57:41 +01:00
bool DownloadManager::winEvent(MSG *message, long *result)
{
return win7.winEvent(message, result);
}
#endif
void DownloadManager::timerEvent(QTimerEvent *event)
{
QList<QTime> remTimes;
QList<int> progresses;
QList<double> speeds;
if (event->timerId() == m_timer.timerId()) {
if (!ui->list->count()) {
ui->speedLabel->clear();
setWindowTitle(tr("Download Manager"));
return;
}
for (int i = 0; i < ui->list->count(); i++) {
DownloadItem* downItem = qobject_cast<DownloadItem*>(ui->list->itemWidget(ui->list->item(i)));
if (!downItem || (downItem && downItem->isCancelled()))
continue;
if (!downItem->isDownloading()) {
progresses.append(100);
continue;
} else
progresses.append(downItem->progress());
remTimes.append(downItem->remainingTime());
speeds.append(downItem->currentSpeed());
}
if (remTimes.isEmpty()) {
ui->speedLabel->clear();
setWindowTitle(tr("Download Manager"));
return;
}
QTime remaining;
foreach (QTime time, remTimes) {
if (time > remaining)
remaining = time;
}
int progress = 0;
foreach (int prog, progresses)
progress+=prog;
progress = progress / progresses.count();
double speed = 0.00;
foreach (double spee, speeds)
speed+=spee;
ui->speedLabel->setText(tr("%1% of %2 files (%3) %4 remaining").arg(QString::number(progress),QString::number(progresses.count()),
DownloadItem::currentSpeedToString(speed),
DownloadItem::remaingTimeToString(remaining)));
setWindowTitle(QString::number(progress) + tr("% - Download Manager"));
2011-03-05 10:57:21 +01:00
#ifdef W7TASKBAR
2011-03-02 16:57:41 +01:00
win7.setProgressValue(progress, 100);
win7.setProgressState(win7.Normal);
#endif
} else
QWidget::timerEvent(event);
}
void DownloadManager::clearList()
{
QList<DownloadItem*> items;
for (int i = 0; i < ui->list->count(); i++) {
DownloadItem* downItem = qobject_cast<DownloadItem*>(ui->list->itemWidget(ui->list->item(i)));
if (!downItem)
continue;
if (downItem->isDownloading())
continue;
items.append(downItem);
}
qDeleteAll(items);
}
void DownloadManager::download(const QNetworkRequest &request)
{
handleUnsupportedContent(m_networkManager->get(request));
}
void DownloadManager::handleUnsupportedContent(QNetworkReply *reply)
{
// DownloadOptionsDialog* dialog = new DownloadOptionsDialog();
// dialog->show();
// dialog->setAttribute(Qt::WA_DeleteOnClose);
QString path;
QString fileName;
QString userFileName;
QString _fileName = getFileName(reply);
if (m_downloadPath.isEmpty())
2011-03-04 13:59:07 +01:00
userFileName = QFileDialog::getSaveFileName(mApp->getWindow(), tr("Save file as..."),m_lastDownloadPath+_fileName);
2011-03-02 16:57:41 +01:00
else
userFileName = m_downloadPath+_fileName;
if (userFileName.isEmpty()) {
reply->abort();
return;
}
int pos = userFileName.lastIndexOf("/");
if (pos!=-1) {
int size = userFileName.size();
path = userFileName.left(pos+1);
fileName = userFileName.right(size-pos-1);
}
m_lastDownloadPath = path;
2011-03-04 13:59:07 +01:00
QSettings settings(mApp->getActiveProfil()+"settings.ini", QSettings::IniFormat);
2011-03-02 16:57:41 +01:00
settings.beginGroup("DownloadManager");
settings.setValue("lastDownloadPath",m_lastDownloadPath);
settings.endGroup();
QFileInfo info(reply->url().toString());
QTemporaryFile tempFile("XXXXXX."+info.suffix());
tempFile.open();
QFileInfo tempInfo(tempFile.fileName());
QPixmap fileIcon = m_iconProvider->icon(tempInfo).pixmap(30,30);
QListWidgetItem* item = new QListWidgetItem(ui->list);
2011-03-04 15:40:45 +01:00
DownloadItem* downItem = new DownloadItem(item, reply, path, fileName, fileIcon, this);
2011-03-02 16:57:41 +01:00
connect(downItem, SIGNAL(deleteItem(DownloadItem*)), this, SLOT(deleteItem(DownloadItem*)));
ui->list->setItemWidget(item, downItem);
item->setSizeHint(downItem->sizeHint());
show();
activateWindow();
}
void DownloadManager::deleteItem(DownloadItem *item)
{
if (item && !item->isDownloading())
delete item;
}
QString DownloadManager::getFileName(QNetworkReply *reply)
{
QString path;
if (reply->hasRawHeader("Content-Disposition")) {
QString value = reply->rawHeader("Content-Disposition");
int pos = value.indexOf("filename=");
if (pos!=-1) {
QString name = value.mid(pos + 9);
if (name.startsWith('"') && name.endsWith('"'))
name = name.mid(1, name.size() - 2);
path = name;
}
}
if (path.isEmpty())
path = reply->url().path();
QFileInfo info(path);
QString baseName = info.completeBaseName();
QString endName = info.suffix();
if (baseName.isEmpty()) {
baseName = tr("NoNameDownload");
}
if (!endName.isEmpty())
endName="."+endName;
return baseName+endName;
}
bool DownloadManager::canClose()
{
if (m_isClosing)
return true;
bool isDownloading = false;
for (int i = 0; i < ui->list->count(); i++) {
DownloadItem* downItem = qobject_cast<DownloadItem*>(ui->list->itemWidget(ui->list->item(i)));
if (!downItem)
continue;
if (downItem->isDownloading()) {
isDownloading = true;
break;
}
}
return !isDownloading;
}
void DownloadManager::closeEvent(QCloseEvent *e)
{
2011-03-05 13:16:13 +01:00
if (mApp->windowCount() == 0) { // No main windows -> we are going to quit
2011-03-02 16:57:41 +01:00
if (!canClose()){
QMessageBox::StandardButton button = QMessageBox::warning(this, tr("Warning"),
tr("Are you sure to quit? All uncompleted downloads will be cancelled!"), QMessageBox::Yes | QMessageBox::No);
if (button != QMessageBox::Yes) {
e->ignore();
return;
}
m_isClosing = true;
}
2011-03-04 13:59:07 +01:00
mApp->quitApplication();
2011-03-02 16:57:41 +01:00
}
e->accept();
}
DownloadManager::~DownloadManager()
{
delete ui;
delete m_networkManager;
delete m_iconProvider;
}