ShareOnInternet.cpp 6.72 KB
Newer Older
1 2 3
/******************************************************************************
 * ShareOnInternet.cpp: Configure Video Export to Internet
 ******************************************************************************
4
 * Copyright (C) 2008-2016 VideoLAN
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Authors: Rohit Yadav <rohityadav89@gmail.com>
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23
#include "Settings/Settings.h"
24
#include "ShareOnInternet.h"
25
#include "Tools/VlmcDebug.h"
26 27
#include "Services/AbstractSharingService.h"
#include "Services/YouTube/YouTubeService.h"
28 29 30 31 32 33 34 35

#include <QDesktopServices>
#include <QClipboard>
#include <QMessageBox>

ShareOnInternet::ShareOnInternet( QWidget* parent )
    : QDialog( parent, Qt::Sheet | Qt::Dialog )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
36
    m_service = nullptr;
37 38 39 40 41 42 43 44 45 46 47 48
    m_serviceProvider = 0;
    m_ui.setupUi( this );
    m_ui.progressBar->setVisible( false );

    /* Get DevKey from VLMC settings */
    m_devKey = VLMC_GET_STRING( "youtube/DeveloperKey" );

    switch( m_serviceProvider )
    {
        case YOUTUBE:
            m_ui.username->setText( VLMC_GET_STRING( "youtube/Username" ) );
            m_ui.password->setText( VLMC_GET_STRING( "youtube/Password" ) );
49
            m_ui.title->setText( Core::getInstance()->project()->name() );
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
            break;
    }
}

ShareOnInternet::~ShareOnInternet()
{
    delete m_service;
}

void
ShareOnInternet::accept()
{
    if( m_ui.title->text().isEmpty() |
        m_ui.username->text().isEmpty() |
        m_ui.password->text().isEmpty() )
    {
        QMessageBox::critical( this, tr("Error"),
                               tr("'Username' or 'Password' or 'Title' cannot be empty. "
                                  "Please check these fields.") );
        return;
    }

    publish();
}

void
ShareOnInternet::publish()
{
    m_ui.publishButton->setEnabled( false );

    /* Check for service provider, if changes delete old one */
    if( m_serviceProvider != m_ui.serviceBox->currentIndex() )
    {
        if( m_service )
            delete m_service;

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
86
        m_service = nullptr;
87 88 89 90 91 92 93 94 95

        m_serviceProvider = m_ui.serviceBox->currentIndex();
    }

    if( !m_service )
    {
        switch( m_serviceProvider )
        {
            case VIMEO:  /* TODO: Implement services for Vimeo */
96
                vlmcDebug() << "[SHARE ON INTERNET]: VIMEO"; break;
97 98 99
            case YOUTUBE:
            default:
                m_service = new YouTubeService( m_devKey, getUsername(), getPassword() );
100
                vlmcDebug() << "[SHARE ON INTERNET]: YOUTUBE"; break;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        }
    }
    else
        m_service->setCredentials( getUsername(), getPassword() );

    m_ui.statusLabel->setText( tr("Authenticating...") );
    m_service->authenticate();

    connect( m_service, SIGNAL(authOver()), this, SLOT(authFinished()) );
    connect( m_service, SIGNAL(error(QString)), this, SLOT(serviceError(QString)) );
}

void
ShareOnInternet::authFinished()
{
116
    vlmcDebug() << "[SHARE ON INTERNET]: AUTH FINISHED";
117 118 119 120

    /*On Finish, extract out the auth token and upload a test video */
    disconnect( m_service, SIGNAL(authOver()), this, SLOT(authFinished()) );

121
    AbstractVideoData videoData = getVideoData();
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

    m_service->setVideoParameters( m_fileName, videoData );

    connect( m_service, SIGNAL(uploadOver(QString)), this, SLOT(uploadFinished(QString)));
    connect( m_service, SIGNAL(uploadProgress(qint64,qint64)),
             this, SLOT(uploadProgress(qint64,qint64)) );

    if( !m_service->upload() )
    {
        m_ui.publishButton->setEnabled( true );

        disconnect( m_service, SIGNAL(uploadOver(QString)), this, SLOT(uploadFinished(QString)));
        disconnect( m_service, SIGNAL(uploadProgress(qint64,qint64)),
                    this, SLOT(uploadProgress(qint64,qint64)) );
        disconnect( m_service, SIGNAL(error(QString)), this, SLOT(serviceError(QString)) );

138
        vlmcDebug() << "[SHARE ON INTERNET]: AUTH Failed";
139 140 141 142 143 144 145 146

        return;
    }
    m_ui.statusLabel->setText( tr("Authenticated!") );
    m_ui.progressBar->setEnabled( true );
    m_ui.progressBar->setVisible( true );
    m_ui.mainPanel->setEnabled( false );

147
    vlmcDebug() << "[SHARE ON INTERNET]: UPLOAD STARTED";
148 149 150 151 152
}

void
ShareOnInternet::uploadFinished( QString result )
{
153
    vlmcDebug() << "[SHARE ON INTERNET]: UPLOAD FINISHED";
154

155 156 157 158
    /* Add code here to abort stuff */
    m_ui.progressBar->setEnabled( false );
    m_ui.progressBar->setVisible( false );

159 160 161 162 163 164 165 166 167 168 169
    QString msg;
    if( result != "" )
    {
        msg = tr( "Your video has been uploaded."
                  "\nURL (copied to your clipboard):\n%1"
                  "\n\nOpen this in your default web browser?" ).arg( result );
        QApplication::clipboard()->setText( result );
    }
    else
        msg = tr( "Some error has occured while processing your video."
                  "\nPlease check with your video service provider." );
170

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
171
    if( QMessageBox::information( nullptr, tr("Video Uploaded"), msg,
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
            QMessageBox::Open | QMessageBox::Close ) == QMessageBox::Open )
    {
        QDesktopServices::openUrl( result );
    }

    /* Finish exec() */
    QDialog::accept();
}

void
ShareOnInternet::uploadProgress(qint64 received, qint64 total)
{
    if( total != 0 )
    {
        qint64 progress = received * 100 / total;
        m_ui.progressBar->setValue( progress );
188
        m_ui.statusLabel->setText( tr("%1 kB Uploaded").arg( received / 1024 ) );
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    }
}

void
ShareOnInternet::serviceError(QString e)
{
    m_ui.statusLabel->setText( e );
    emit error( e );
}

QString
ShareOnInternet::getUsername() const
{
    return m_ui.username->text();
}

QString
ShareOnInternet::getPassword() const
{
    return m_ui.password->text();
}

211
AbstractVideoData
212 213
ShareOnInternet::getVideoData() const
{
214
    AbstractVideoData data;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    data.title       = m_ui.title->text();
    data.category    = m_ui.category->currentText().split(" & ").at( 0 );
    data.description = m_ui.description->toPlainText();
    data.keywords    = m_ui.keywords->text();
    data.isPrivate   = m_ui.videoPrivacy->isChecked();

    return data;
}

void
ShareOnInternet::setVideoFile( QString& fileName )
{
    m_fileName = fileName;
}