homescreenapp/hsdomainmodel/src/hswallpaperloader.cpp
changeset 62 341166945d65
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QFileInfo>
       
    19 #include <QVariantHash>
       
    20 
       
    21 #include "hswallpaperloader.h"
       
    22 #include "hswallpaperloaderthread.h"
       
    23 
       
    24 /*!
       
    25     \class HsWallpaperLoader
       
    26     \ingroup group_hsdomainmodel
       
    27     \brief 
       
    28 */
       
    29 
       
    30 /*!
       
    31 
       
    32 */
       
    33 HsWallpaperLoader::HsWallpaperLoader(QObject *parent)
       
    34   : QObject(parent),
       
    35     mIsRunning(false),
       
    36     mNumberOfFinishedThreads(0)
       
    37 {
       
    38 }
       
    39 
       
    40 /*!
       
    41 
       
    42 */
       
    43 HsWallpaperLoader::~HsWallpaperLoader()
       
    44 {
       
    45 }
       
    46 
       
    47 /*!
       
    48 
       
    49 */
       
    50 void HsWallpaperLoader::start()
       
    51 {
       
    52     if (mTargets.isEmpty()) {
       
    53         emit finished();
       
    54         return;
       
    55     }
       
    56 
       
    57     mNumberOfFinishedThreads = 0;
       
    58     
       
    59     HsWallpaperLoaderThread *thread = 0;
       
    60     QHashIterator<QString, QVariant> i(mTargets);
       
    61     while (i.hasNext()) {
       
    62         i.next();
       
    63         thread = new HsWallpaperLoaderThread;
       
    64         mThreads.append(thread);
       
    65         thread->setSourcePath(mSourcePath);
       
    66         thread->setTargetPath(i.key());
       
    67         thread->setTargetSize(i.value().toSize());
       
    68         connect(thread, SIGNAL(finished()), SLOT(onThreadFinished()));
       
    69         thread->start();
       
    70     }
       
    71 
       
    72     mIsRunning = true;
       
    73 }
       
    74 
       
    75 /*!
       
    76 
       
    77 */
       
    78 void HsWallpaperLoader::cancel()
       
    79 {
       
    80     foreach (QThread *thread, mThreads) {
       
    81         thread->disconnect(this);
       
    82         thread->wait();
       
    83     }
       
    84     cleanup();
       
    85 }
       
    86 
       
    87 /*!
       
    88 
       
    89 */
       
    90 void HsWallpaperLoader::onThreadFinished()
       
    91 {
       
    92     ++mNumberOfFinishedThreads;
       
    93     if (mNumberOfFinishedThreads == mThreads.count()) {
       
    94         bool ok = true;
       
    95         foreach (HsWallpaperLoaderThread *thread, mThreads) {
       
    96             if (!thread->result()) {
       
    97                 ok = false;
       
    98                 break;
       
    99             }
       
   100         }
       
   101         if (ok) {
       
   102             emit finished();
       
   103         } else {            
       
   104             emit failed();
       
   105         }
       
   106         cleanup();
       
   107     }
       
   108 }
       
   109 
       
   110 /*!
       
   111 
       
   112 */
       
   113 void HsWallpaperLoader::cleanup()
       
   114 {
       
   115     mIsRunning = false;
       
   116     mNumberOfFinishedThreads = 0;
       
   117     qDeleteAll(mThreads);
       
   118     mThreads.clear();
       
   119 }