~ubuntu-branches/ubuntu/lucid/lastfm/lucid

« back to all changes in this revision

Viewing changes to src/autoupdater.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Pedro Fragoso
  • Date: 2007-12-31 09:49:54 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071231094954-ix1amvcsj9pk61ya
Tags: 1:1.4.1.57486.dfsg-1ubuntu1
* Merge from Debian unstable (LP: #180254), remaining changes:
  - debian/rules;
    - Added dh_icons
  - Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *   Copyright (C) 2005 - 2007 by                                          *
3
3
 *      Christian Muehlhaeuser, Last.fm Ltd <chris@last.fm>                *
4
4
 *      Erik Jaelevik, Last.fm Ltd <erik@last.fm>                          *
 
5
 *      Jono Cole, Last.fm Ltd <jono@last.fm>                              *
5
6
 *                                                                         *
6
7
 *   This program is free software; you can redistribute it and/or modify  *
7
8
 *   it under the terms of the GNU General Public License as published by  *
16
17
 *   You should have received a copy of the GNU General Public License     *
17
18
 *   along with this program; if not, write to the                         *
18
19
 *   Free Software Foundation, Inc.,                                       *
19
 
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02111-1307, USA.          *
 
20
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
20
21
 ***************************************************************************/
21
22
 
22
23
#include "autoupdater.h"
23
24
#include "logger.h"
24
25
#include "exceptions.h"
25
 
#include "winutils.h"
26
 
#include "containerutils.h"
 
26
#include "MooseCommon.h"
 
27
#include "UnicornCommon.h"
27
28
 
28
29
#ifdef WIN32
29
30
    #include <windows.h>
191
192
        emit updateDownloadDone(true, msg);
192
193
        return;
193
194
    }
194
 
 
 
195
    
195
196
    QUrl url(current.GetDownloadURL());
196
197
 
197
 
    mTransport = new Http(this);
 
198
    mTransport = new CachedHttp(this);
198
199
    mTransport->setHost(url.host());
199
200
 
200
201
    connect(mTransport, SIGNAL(requestFinished(int, bool)),
204
205
    connect(mTransport, SIGNAL(stateChanged(int)),
205
206
            this,       SLOT  (statusChanged(int)));
206
207
 
207
 
    mHttpId = mTransport->QHttp::get( url.path(), &mFile );
 
208
    mHttpId = mTransport->get( url.path(), &mFile );
208
209
 
209
210
    mCancelled = false;
210
211
}
222
223
 
223
224
    Q_ASSERT(mTransport);
224
225
 
 
226
    QHttpResponseHeader header = mTransport->lastResponse();
 
227
    int statusCode = header.statusCode();
 
228
    
 
229
    //Successfull response is 2xx
 
230
    if (statusCode < 200 || statusCode >= 300 )
 
231
        error = true;
 
232
 
225
233
    if (mCancelled)
226
234
    {
227
235
        LOG(3, "Download of update cancelled. Not emitting any signals.\n");
232
240
 
233
241
    if (error)
234
242
    {
 
243
        QString errorString = header.reasonPhrase();
235
244
        // Something went wrong
236
245
        LOG(2, "Download of component failed. Error: " <<
237
 
            mTransport->error() << " - " << mTransport->errorString() << "\n");
 
246
            statusCode << " - " << errorString << "\n");
238
247
 
239
248
        mFile.close();
240
249
        mTransport->deleteLater();
241
250
 
242
 
        emit updateDownloadDone(true, QString(tr("Download failed (%1)")).
243
 
            arg(mTransport->errorString()));
 
251
        emit updateDownloadDone(true, QString(tr("Download failed ( %1 - %2 )")).
 
252
                                                    arg(statusCode).
 
253
                                                    arg(errorString));
244
254
    }
245
255
    else
246
256
    {
306
316
            }
307
317
 
308
318
                        QString installDir = QDir::toNativeSeparators( current.GetInstallPath() );
309
 
 
310
319
            QDir dir( installDir );
311
320
            if ( !dir.exists() )
312
321
            {
345
354
            LOG(3, "Running '" << installerPath << " " << installArgs << "'\n");
346
355
 
347
356
            // Run installer
348
 
                    if ( ( QSysInfo::WindowsVersion & QSysInfo::WV_VISTA ) == 0 )
349
 
                        {
350
 
                                // Use QProcess if not on Vista to not break Win98
351
 
                                QProcess proc( this );
352
 
                                proc.setWorkingDirectory( QCoreApplication::applicationDirPath() );
353
 
                                installerPath += " " + installArgs;
354
 
                                proc.start( installerPath );
355
 
 
356
 
                                if (!proc.waitForStarted())
357
 
                                {
358
 
                                        LOG(1, "Couldn't start " << installerPath <<
359
 
                                                ". Error: " << proc.error() << "\n");
360
 
 
361
 
                                        QString msg = tr("Failed to install '%1'.").arg( installerPath );
362
 
                                        emit updateDownloadDone(true, msg);
363
 
                                        return;
364
 
                                }
365
 
                    
366
 
                                if (!proc.waitForFinished( 10000 ))
367
 
                                {
368
 
                                        LOG(1, installerPath << " never finished. " <<
369
 
                                                 ". Error: " << proc.error() << "\n");
370
 
 
371
 
                                        QString msg = tr("Failed to install '") + installerPath + tr("'.");
372
 
                                        emit updateDownloadDone(true, msg);
373
 
                                        return;
374
 
                                }
375
 
                        }
376
 
                        else
377
 
                        {
378
 
                                // On Vista, use ShellExecute because otherwise the elevation dialog
379
 
                                // doesn't appear when launching the installer
380
 
                SHELLEXECUTEINFOW sei;
381
 
                memset(&sei, 0, sizeof(sei));
382
 
 
383
 
                sei.cbSize = sizeof(sei);
384
 
                sei.fMask  = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_DDEWAIT;
385
 
                sei.hwnd   = GetForegroundWindow();
386
 
                sei.lpVerb = L"open";
387
 
                sei.lpFile = reinterpret_cast<LPCWSTR>(installerPath.utf16());
388
 
                sei.lpParameters = reinterpret_cast<LPCWSTR>(installArgs.utf16());
389
 
                sei.nShow  = SW_SHOWNORMAL;
390
 
 
391
 
                BOOL bOK = ShellExecuteExW(&sei);
392
 
                if (bOK)
393
 
                {
394
 
                    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
395
 
                    WaitForSingleObject(sei.hProcess, 5000);
396
 
                    CloseHandle(sei.hProcess);
397
 
                    QApplication::restoreOverrideCursor();
398
 
                }
399
 
                else
400
 
                {
401
 
                    LOG(1, "Couldn't ShellExecuteEx " << installerPath << " " <<
402
 
                        installArgs << ". GetLastError: " << GetLastError() << "\n");
403
 
 
404
 
                    QString msg = tr("Failed to install '") + installerPath + tr("'.");
405
 
                    emit updateDownloadDone(true, msg);
406
 
                    return;
407
 
                }
408
 
                        
409
 
            }
 
357
                        // Must use ShellExecute because otherwise the elevation dialog
 
358
                        // doesn't appear when launching the installer on Vista.
 
359
            SHELLEXECUTEINFOW sei;
 
360
            memset(&sei, 0, sizeof(sei));
 
361
 
 
362
            sei.cbSize = sizeof(sei);
 
363
            sei.fMask  = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_DDEWAIT;
 
364
            sei.hwnd   = GetForegroundWindow();
 
365
            sei.lpVerb = L"open";
 
366
            sei.lpFile = reinterpret_cast<LPCWSTR>(installerPath.utf16());
 
367
            sei.lpParameters = reinterpret_cast<LPCWSTR>(installArgs.utf16());
 
368
            sei.nShow  = SW_SHOWNORMAL;
 
369
 
 
370
            BOOL bOK = ShellExecuteExW(&sei);
 
371
            if (bOK)
 
372
            {
 
373
                QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
374
                WaitForSingleObject(sei.hProcess, 5000);
 
375
                CloseHandle(sei.hProcess);
 
376
                QApplication::restoreOverrideCursor();
 
377
            }
 
378
            else
 
379
            {
 
380
                LOG(1, "Couldn't ShellExecuteEx " << installerPath << " " <<
 
381
                    installArgs << ". GetLastError: " << GetLastError() << "\n");
 
382
 
 
383
                QString msg = tr("Failed to install '") + installerPath + tr("'.");
 
384
                emit updateDownloadDone(true, msg);
 
385
                return;
 
386
            }
 
387
                
410
388
            
411
389
            if (mnCurrentDownload < (mnDownloadTasks - 1))
412
390
            {
471
449
        #endif
472
450
 
473
451
        // Synchronous
474
 
    QString tempUpdaterPath = savePath( "UpTemp.exe" );
 
452
    QString tempUpdaterPath = MooseUtils::savePath( "Updaterd.exe" );
475
453
    QFile::remove( tempUpdaterPath );
 
454
    if ( QFile::exists( tempUpdaterPath ) )
 
455
    {
 
456
        LOG(1, "Removal of old Updater.exe failed. System error code: " <<
 
457
            GetLastError() << "\n");
 
458
    }
 
459
    
476
460
    bool ok = QFile::copy( updaterPath, tempUpdaterPath );
477
 
 
478
461
    if (!ok)
479
462
    {
480
463
        LOG(1, "Copying of Updater.exe failed. System error code: " <<
485
468
    QStringList args;
486
469
    args << "R" << QString::fromStdString(sMutex) << msAppDLPath;
487
470
 
488
 
    LOG(3, "Will execute: UpTemp.exe " << args.join(" ") << "\n");
 
471
    LOG(3, "Will execute: " << tempUpdaterPath << args.join(" ") << "\n");
489
472
 
490
473
    QProcess proc( this );
491
474
    proc.setWorkingDirectory( QCoreApplication::applicationDirPath() );
506
489
 
507
490
        return true;
508
491
 
 
492
#elif defined Q_WS_MAC
 
493
        QProcess::startDetached( "/bin/bash", QStringList() << QApplication::applicationDirPath() + "/updater-autorestart.sh" << MooseUtils::savePath( "lastfm.pid" ) );
 
494
                
 
495
        return true;
509
496
#else
510
497
    return false;
511
498
#endif // WIN32
569
556
CAutoUpdater::statusChanged(
570
557
    int state)
571
558
{
572
 
    QString msg = QHttpStateToString(state);
 
559
    QString msg = UnicornUtils::QHttpStateToString(state);
573
560
    emit statusChange(msg);
574
561
}
575
562