<commit_before>/***************************************************************************
 *   Copyright (c) 2013 <NAME> <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  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 Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#include "PreCompiled.h"
#include <stdio.h>
#include <cmath>

#include <QByteArray>
#include <QDockWidget>
#include <QFileInfo>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMetaEnum>
#include <QSettings>
#include <QFileIconProvider>
#include <QWebSettings>

#include "DownloadItem.h"
#include "DownloadManager.h"
#include "ui_DownloadManager.h"
#include "DockWindowManager.h"
#include "MainWindow.h"

using namespace Gui::Dialog;

/* TRANSLATOR Gui::Dialog::DownloadManager */

DownloadManager* DownloadManager::self = 0;

DownloadManager* DownloadManager::getInstance()
{
    if (!self)
        self = new DownloadManager(Gui::getMainWindow());
    return self;
}

DownloadManager::DownloadManager(QWidget *parent)
    : QDialog(parent)
    , m_autoSaver(new AutoSaver(this))
    , m_manager(new NetworkAccessManager(this))
    , m_iconProvider(0)
    , m_removePolicy(Never)
    , ui(new Ui_DownloadManager())
{
    ui->setupUi(this);
    ui->downloadsView->setShowGrid(false);
    ui->downloadsView->verticalHeader()->hide();
    ui->downloadsView->horizontalHeader()->hide();
    ui->downloadsView->setAlternatingRowColors(true);
    ui->downloadsView->horizontalHeader()->setStretchLastSection(true);
    m_model = new DownloadModel(this);
    ui->downloadsView->setModel(m_model);
    connect(ui->cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup()));
    connect(m_manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));
    load();

    Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
    QDockWidget* dw = pDockMgr->addDockWindow(QT_TR_NOOP("Download Manager"),
        this, Qt::BottomDockWidgetArea);
    dw->setFeatures(QDockWidget::DockWidgetMovable|
                    QDockWidget::DockWidgetFloatable|
                    QDockWidget::DockWidgetClosable);
    dw->setAttribute(Qt::WA_DeleteOnClose);
    dw->show();
}

DownloadManager::~DownloadManager()
{
    m_autoSaver->changeOccurred();
    m_autoSaver->saveIfNeccessary();
    if (m_iconProvider)
        delete m_iconProvider;
    delete ui;
    self = 0;
}

void DownloadManager::closeEvent(QCloseEvent* e)
{
    QDialog::closeEvent(e);
}

int DownloadManager::activeDownloads() const
{
    int count = 0;
    for (int i = 0; i < m_downloads.count(); ++i) {
        if (m_downloads.at(i)->stopButton->isEnabled())
            ++count;
    }
    return count;
}

QUrl DownloadManager::redirectUrl(const QUrl& url) const
{
    QUrl redirectUrl = url;
    if (url.host() == QLatin1String("www.dropbox.com")) {
        QList< QPair<QString, QString> > query = url.queryItems();
        for (QList< QPair<QString, QString> >::iterator it = query.begin(); it != query.end(); ++it) {
            if (it->first == QLatin1String("dl") && it->second == QLatin1String("0\r\n")) {
                redirectUrl.removeQueryItem(QLatin1String("dl"));
                redirectUrl.addQueryItem(QLatin1String("dl"), QLatin1String("1\r\n"));
                break;
            }
        }
    }

    return redirectUrl;
}

void DownloadManager::replyFinished(QNetworkReply* reply)
{
    // The 'requestFileName' is used to store the argument passed by 'download()'
    // and to also distinguish between replies created by 'download()' and
    // this method.
    // Replies from this method shouldn't be further examined because it's not
    // assumed to get re-directed urls over several steps.
    QVariant var = reply->property("requestFileName");
    if (var.isValid()) {
        bool requestFileName = reply->property("requestFileName").toBool();

        QUrl url = reply->url();

        // If this is a redirected url use this instead
        QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
        if (!redirectUrl.isEmpty()) {<commit_msg>+ fixes #0001705: Download manager does not follow redirect (HTTP status 302)<commit_after>/***************************************************************************
 *   Copyright (c) 2013 <NAME> <wmayer[at]users.sourceforge.net>     *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  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 Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/


#include "PreCompiled.h"
#include <stdio.h>
#include <cmath>

#include <QByteArray>
#include <QDockWidget>
#include <QFileInfo>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QMetaEnum>
#include <QSettings>
#include <QFileIconProvider>
#include <QWebSettings>

#include "DownloadItem.h"
#include "DownloadManager.h"
#include "ui_DownloadManager.h"
#include "DockWindowManager.h"
#include "MainWindow.h"

using namespace Gui::Dialog;

/* TRANSLATOR Gui::Dialog::DownloadManager */

DownloadManager* DownloadManager::self = 0;

DownloadManager* DownloadManager::getInstance()
{
    if (!self)
        self = new DownloadManager(Gui::getMainWindow());
    return self;
}

DownloadManager::DownloadManager(QWidget *parent)
    : QDialog(parent)
    , m_autoSaver(new AutoSaver(this))
    , m_manager(new NetworkAccessManager(this))
    , m_iconProvider(0)
    , m_removePolicy(Never)
    , ui(new Ui_DownloadManager())
{
    ui->setupUi(this);
    ui->downloadsView->setShowGrid(false);
    ui->downloadsView->verticalHeader()->hide();
    ui->downloadsView->horizontalHeader()->hide();
    ui->downloadsView->setAlternatingRowColors(true);
    ui->downloadsView->horizontalHeader()->setStretchLastSection(true);
    m_model = new DownloadModel(this);
    ui->downloadsView->setModel(m_model);
    connect(ui->cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup()));
    connect(m_manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));
    load();

    Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance();
    QDockWidget* dw = pDockMgr->addDockWindow(QT_TR_NOOP("Download Manager"),
        this, Qt::BottomDockWidgetArea);
    dw->setFeatures(QDockWidget::DockWidgetMovable|
                    QDockWidget::DockWidgetFloatable|
                    QDockWidget::DockWidgetClosable);
    dw->setAttribute(Qt::WA_DeleteOnClose);
    dw->show();
}

DownloadManager::~DownloadManager()
{
    m_autoSaver->changeOccurred();
    m_autoSaver->saveIfNeccessary();
    if (m_iconProvider)
        delete m_iconProvider;
    delete ui;
    self = 0;
}

void DownloadManager::closeEvent(QCloseEvent* e)
{
    QDialog::closeEvent(e);
}

int DownloadManager::activeDownloads() const
{
    int count = 0;
    for (int i = 0; i < m_downloads.count(); ++i) {
        if (m_downloads.at(i)->stopButton->isEnabled())
            ++count;
    }
    return count;
}

QUrl DownloadManager::redirectUrl(const QUrl& url) const
{
    QUrl redirectUrl = url;
    if (url.host() == QLatin1String("www.dropbox.com")) {
        QList< QPair<QString, QString> > query = url.queryItems();
        for (QList< QPair<QString, QString> >::iterator it = query.begin(); it != query.end(); ++it) {
            if (it->first == QLatin1String("dl") && it->second == QLatin1String("0\r\n")) {
                redirectUrl.removeQueryItem(QLatin1String("dl"));
                redirectUrl.addQueryItem(QLatin1String("dl"), QLatin1String("1\r\n"));
                break;
            }
        }
    }
    else {
        // When the url comes from drag and drop it may end with CR+LF. This may cause problems
        // and thus should be removed.
        QString str = redirectUrl.toString();
        if (str.endsWith(QLatin1String("\r\n"))) {
            str.chop(2);
            redirectUrl.setUrl(str);
        }
    }

    return redirectUrl;
}

void DownloadManager::replyFinished(QNetworkReply* reply)
{
    // The 'requestFileName' is used to store the argument passed by 'download()'
    // and to also distinguish between replies created by 'download()' and
    // this method.
    // Replies from this method shouldn't be further examined because it's not
    // assumed to get re-directed urls over several steps.
    QVariant var = reply->property("requestFileName");
    if (var.isValid()) {
        bool requestFileName = reply->property("requestFileName").toBool();

        QUrl url = reply->url();

        // If this is a redirected url use this instead
        QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
        if (!redirectUrl.isEmpty()) {
{'fraction_non_alphanumeric': 0.13794323105491202, 'fraction_numerical': 0.006720311256521355, 'mean_word_length': 2.54879196736743, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 95, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3073273', 'n_tokens_mistral': 3148, 'n_tokens_neox': 3037, 'n_words': 783}