Example #1
0
void DownloadItem::startDownloadingFromFtp(const QUrl &url)
{
    if (!m_outputFile.isOpen() && !m_outputFile.open(QIODevice::WriteOnly)) {
        stop(false);
        ui->downloadInfo->setText(tr("Error: Cannot write to file!"));
        return;
    }

#if QTWEBENGINE_DISABLED
    m_ftpDownloader = new FtpDownloader(this);
    connect(m_ftpDownloader, SIGNAL(finished()), this, SLOT(finished()));
    connect(m_ftpDownloader, SIGNAL(dataTransferProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    connect(m_ftpDownloader, SIGNAL(errorOccured(QFtp::Error)), this, SLOT(error()));
    connect(m_ftpDownloader, SIGNAL(ftpAuthenticationRequierd(QUrl,QAuthenticator*)), mApp->networkManager(), SLOT(ftpAuthentication(QUrl,QAuthenticator*)));

    m_ftpDownloader->download(url, &m_outputFile);
    m_downloading = true;
    m_timer.start(1000, this);

    QTimer::singleShot(200, this, SLOT(updateDownload()));

    if (m_ftpDownloader->error() != QFtp::NoError) {
        error();
    }
#endif
}
Example #2
0
void DownloadItem::startDownloadingFromFtp(const QUrl &url)
{
    if (!outputFile_.isOpen() && !outputFile_.open(QIODevice::WriteOnly)) {
        stop(false);
        downloadInfo_->setText(tr("Error: Cannot write to file!"));
        return;
    }

    ftpDownloader_ = new FtpDownloader(this);
    connect(ftpDownloader_, SIGNAL(finished()), this, SLOT(finished()));
    connect(ftpDownloader_, SIGNAL(dataTransferProgress(qint64, qint64)),
            this, SLOT(downloadProgress(qint64, qint64)));
    connect(ftpDownloader_, SIGNAL(errorOccured(QFtp::Error)), this, SLOT(error()));
    connect(ftpDownloader_, SIGNAL(ftpAuthenticationRequierd(const QUrl &, QAuthenticator*)),
            mainApp->networkManager(), SLOT(ftpAuthentication(const QUrl &, QAuthenticator*)));

    ftpDownloader_->download(url, &outputFile_);
    downloading_ = true;
    updateInfoTimer_.start(1000);

    QTimer::singleShot(200, this, SLOT(updateDownload()));

    if (ftpDownloader_->error() != QFtp::NoError) {
        error();
    }
}
Example #3
0
void DownloadItem::startDownloading()
{
    QUrl locationHeader = m_reply->header(QNetworkRequest::LocationHeader).toUrl();

    bool hasFtpUrlInHeader = locationHeader.isValid() && (locationHeader.scheme() == "ftp");
    if (m_reply->url().scheme() == "ftp" || hasFtpUrlInHeader) {
        QUrl url = hasFtpUrlInHeader ? locationHeader : m_reply->url();
        m_reply->abort();
        m_reply->deleteLater();
        m_reply = 0;

        startDownloadingFromFtp(url);
        return;
    }
    else if (locationHeader.isValid()) {
        m_reply->abort();
        m_reply->deleteLater();

        m_reply = mApp->networkManager()->get(QNetworkRequest(locationHeader));
    }

    m_reply->setParent(this);
    connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
    connect(m_reply, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(m_reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
    connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(error()));
    connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(metaDataChanged()));

    m_downloading = true;
    m_timer.start(1000, this);
    readyRead();
    QTimer::singleShot(200, this, SLOT(updateDownload()));

    if (m_reply->error() != QNetworkReply::NoError) {
        stop(false);
        error();
    }
}
    void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
    {
        if( bytesTotal == 0 ){
            return;
        }
        QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());

        if ( reply->error() == QNetworkReply::NoError) {

            Downloads item = m_downloadHash[reply];

            qint64 actualReceived = item.m_tempSize + bytesReceived;
            if( actualReceived == 0 || item.m_tempSize == 0 ){
                return;
            }

            double speed = actualReceived * 1000.0 / item.m_time.elapsed();
            qint64 actualTotal = item.m_tempSize + bytesTotal;

            if( actualTotal == 0 ){
                return;
            }
            QString unit;
            if (speed < 1024) {
                unit = "bytes/sec";
            } else if (speed < 1024*1024) {
                speed /= 1024;
                unit = "kB/s";
            } else {
                speed /= 1024*1024;
                unit = "MB/s";
            }
            int percent = actualReceived * 100 / actualTotal;

            emit progress( item.m_url, actualReceived, actualTotal, percent, speed, unit );
            updateDownload( item.m_url, actualReceived, actualTotal, percent, speed, unit );
        }
    }
Example #5
0
FenPrincipale::FenPrincipale(QWidget *parent) :
    QMainWindow(parent), ui(new Ui::FenPrincipale), m_fenOptions(NULL), m_trayWarningShown(false), m_auth(new Auth(this)), m_handler(new DownloadHandler(this)),
    m_infoExtractor(new InfoExtractor(this)), m_vitesseTransfert(new VitesseTransfert(this, DOWNLOAD_SPEED_UPDATE_INTERVAL, DOWNLOAD_SPEED_AVERAGE_TIME)),
    m_versionCheck(new VersionCheckThread(this, VERSION_HOST, APP_NAME, VERSION, VERSION_NBR)), m_currentDownload(), m_waitTimer(new QTimer(this)),
    m_updateDownloadTimer(new QTimer(this)), m_waitTime(0), m_isDownloading(false)
{
    ui->setupUi(this);
    setWindowTitle(APP_NAME " - v" VERSION);

    //Icône système
    m_menu = new QMenu(this);
    m_retablirAction = new QAction("Rétablir", m_menu);
    m_startAction = new QAction("Télécharger", m_menu);
    m_stopAction = new QAction("Arrêter", m_menu);
    m_quitterAction = new QAction("Quitter", m_menu);
    m_tray = new QSystemTrayIcon(windowIcon(), this);

    m_menu->addAction(m_retablirAction);
    m_menu->addAction(m_startAction);
    m_menu->addAction(m_stopAction);
    m_menu->addAction(m_quitterAction);
    m_retablirAction->setVisible(false);
    m_stopAction->setVisible(false);
    m_tray->setContextMenu(m_menu);
    m_tray->show();

    qApp->setActiveWindow(this);
    loadSettings();

    //UI
    connect(ui->adresse, SIGNAL(returnPressed()), this, SLOT(on_btn_ajouter_clicked()));
    connect(m_waitTimer, SIGNAL(timeout()), this, SLOT(waitTimerTick()));
    connect(m_infoExtractor, SIGNAL(infoAvailable(QString,QString,QString,QString)), this, SLOT(infoAvailable(QString,QString,QString,QString)));
    connect(m_infoExtractor, SIGNAL(infoUnavailable(QString,ExtractionError)), this, SLOT(infoUnavailable(QString,ExtractionError)));
    connect(m_updateDownloadTimer, SIGNAL(timeout()), this, SLOT(updateDownloadTick()));
    connect(m_versionCheck, SIGNAL(update(QString, QString)), this, SLOT(updateAvailable(QString, QString)));
    connect(m_tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayClicked(QSystemTrayIcon::ActivationReason)));
    connect(m_retablirAction, SIGNAL(triggered()), this, SLOT(retablir()));
    connect(m_startAction, SIGNAL(triggered()), this, SLOT(on_btn_go_clicked()));
    connect(m_stopAction, SIGNAL(triggered()), this, SLOT(on_btn_arreter_clicked()));
    connect(m_quitterAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    connect(QApplication::clipboard(), SIGNAL(changed(QClipboard::Mode)), this, SLOT(clipboardChange()));

    //Raccourcis
    connect(new QShortcut(QKeySequence("Ctrl+Q"), this), SIGNAL(activated()), qApp, SLOT(quit()));
    connect(new QShortcut(QKeySequence("Ctrl+D"), this), SIGNAL(activated()), ui->btn_details, SLOT(toggle()));
    connect(new QShortcut(QKeySequence("Ctrl+Up"), this), SIGNAL(activated()), this, SLOT(on_btn_monter_clicked()));
    connect(new QShortcut(QKeySequence("Ctrl+Shift+Up"), this), SIGNAL(activated()), this, SLOT(on_btn_monter_clicked()));
    connect(new QShortcut(QKeySequence("Ctrl+Down"), this), SIGNAL(activated()), this, SLOT(on_btn_descendre_clicked()));
    connect(new QShortcut(QKeySequence("Ctrl+Shift+Down"), this), SIGNAL(activated()), this, SLOT(on_btn_descendre_clicked()));
    connect(new QShortcut(QKeySequence("Del"), this), SIGNAL(activated()), this, SLOT(on_btn_supprimer_clicked()));

    //Téléchargement
    connect(m_auth, SIGNAL(authed(AuthInfo)), this, SLOT(authSuccess(AuthInfo)));
    connect(m_auth, SIGNAL(authError(AuthError)), this, SLOT(authFail(AuthError)));
    connect(m_handler, SIGNAL(error(DownloadError)), this, SLOT(error(DownloadError)));
    connect(m_handler, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(updateDownload(qint64, qint64)));
    connect(m_handler, SIGNAL(finished()), this, SLOT(downloadComplete()));
    connect(m_handler, SIGNAL(waitTime(int, QString)), this, SLOT(waitTimerStart(int, QString)));

    console("Authentification...");
    m_auth->login(m_login, m_password);
    m_updateDownloadTimer->setInterval(DOWNLOAD_SPEED_UPDATE_INTERVAL);
    m_versionCheck->start();
    m_waitTimer->setInterval(1000);
    clipboardChange();  //Initialise la zone d'adresse avec les adresses déjà présentes en mémoire.
    setDetailsVisible(ui->btn_details->isChecked());
    ui->btn_arreter->hide();
    ui->liste->setCurrentRow(0);

    sLog->out(APP_NAME " startup.");
}
void IVLEFetcher::beginDownload(){
    emit statusUpdate(downloading);
    numOfFiles = toDownload.count();
    toDlIt = toDownload.begin();
    updateDownload();
}
void IVLEFetcher::gotReply(QNetworkReply *reply){
    qDebug()<<"redirect"<<reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (reply->error() == QNetworkReply::NoError)
    {
        if(reply->url().path() == QString("/api/Lapi.svc/Validate")){
            QVariantMap map = QtJson::Json::parse(QString(reply->readAll())).toMap();
            QString newToken = map.value("Token").toString();
            if(newToken != token){
                this->token = newToken;
                emit tokenUpdated(newToken);
            }
            fetchUserInfo();
        }else if(reply->url().path() == QString("/api/Lapi.svc/UserName_Get")){
            QString re = QString(reply->readAll());
            qDebug()<<re;
            _username = QtJson::Json::parse(re).toString();
            qDebug()<<_username;
            emit statusUpdate(gottenUserInfo);
            fetchModules();
        }else if(reply->url().path() == QString("/api/Lapi.svc/Modules_Student")){
            QVariantList courseList = QtJson::Json::parse(QString(reply->readAll())).toMap().value("Results").toList();
            for(int i = 0; i < courseList.count(); i++){
                QVariantMap map = courseList[i].toMap();
                if(map.value("isActive") == QString("Y")){
                    QVariantMap course;
                    course.insert("name",map.value("CourseCode").toString().replace('/',"-"));
                    courses.insert(map.value("ID").toString(), course);
                }
            }
            fetchWorkbins();
        }else if(reply->url().path() == QString("/api/Lapi.svc/Workbins")){
            //if more than 1 workbin, just fetch the first one
            //TODO: multiple workbin support.
            QVariantList folderlist = (QtJson::Json::parse(QString(reply->readAll())).toMap().value("Results").toList())[0].toMap().value("Folders").toList();
            QVariantMap folders;
            for(int i = 0; i < folderlist.count(); i++){
                QVariantMap folder, files;
                QVariantList filelist = folderlist[i].toMap().value("Files").toList();
                for(int j = 0; j < filelist.count(); j++){
                    QVariantMap file, fileJS = filelist[j].toMap();
                    file.insert("name",fileJS.value("FileName"));
                    file.insert("uploadTime",fileJS.value("UploadTime_js").toDate());
                    folder.insert(fileJS.value("ID").toString(),file);
                }
                folders.insert(folderlist[i].toMap().value("FolderName").toString(),folder);
            }
            QString key = courses.keys()[currentWebBinFetching];
            QVariantMap tmp = courses.value(key).toMap();
            courses.remove(key);
            tmp.insert("folders",folders);
            courses.insert(key,tmp);
            fetchWorkBin();
        }else if(reply->url().path() == QString("/api/downloadfile.ashx")){
            numOfFiles--;
            updateDownload();
        }else{
            qDebug()<<reply->url().path();
            //qDebug()<< QtJson::Json::parse(QString(reply->readAll()));
        }
    }
    else
    {
        qDebug()<<reply->errorString();
        // handle errors here
    }

    reply->deleteLater();
}