homescreenapp/hsdomainmodel/src/hswallpaper.cpp
changeset 62 341166945d65
parent 51 4785f57bf3d4
child 69 87476091b3f5
--- a/homescreenapp/hsdomainmodel/src/hswallpaper.cpp	Thu Jun 24 13:11:40 2010 +0100
+++ b/homescreenapp/hsdomainmodel/src/hswallpaper.cpp	Fri Jun 25 19:19:22 2010 +0300
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
@@ -16,112 +16,313 @@
 */
 
 #include <QDir>
+#include <QFile>
+#include <QVariantHash>
 #include <QGraphicsLinearLayout>
+
+#include <HbMainWindow>
 #include <HbIconItem>
-#include <HbMainWindow>
+
 #include "hswallpaper.h"
 #include "hsscene.h"
+#include "hspage.h"
+#include "hswallpaperloader.h"
+#include "hsconfiguration.h"
 
+/*!
+    \class HsWallpaper
+    \ingroup group_hsdomainmodel
+    \brief 
+*/
+
+/*!
+
+*/
 HsWallpaper::HsWallpaper(QGraphicsItem *parent)
   : HbWidget(parent),
-    mOrientation(Qt::Vertical),
-    mIconItem(0)
+    mId(-1),
+    mIsDefaultImage(false),
+    mIconItem(0),
+    mLoader(0)
 {
+    setFlag(ItemHasNoContents);
+    setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
+
+    mLoader = new HsWallpaperLoader(this);
+    connect(mLoader, SIGNAL(finished()), SLOT(onLoaderFinished()), Qt::UniqueConnection);
+    connect(mLoader, SIGNAL(failed()), SLOT(onLoaderFailed()), Qt::UniqueConnection);
+
     mIconItem = new HbIconItem();
-    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout();
+    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout;
     layout->setContentsMargins(0, 0, 0, 0);
     layout->addItem(mIconItem);
     setLayout(layout);
 
     connect(HsScene::mainWindow(),
         SIGNAL(orientationChanged(Qt::Orientation)),
-        SLOT(onOrientationChanged(Qt::Orientation)));
+        SLOT(updateIconItem(Qt::Orientation)));
 }
 
+/*!
+
+*/
 HsWallpaper::~HsWallpaper()
 {
 }
 
-bool HsWallpaper::setImagesById(const QString &id,
-                                const QString &ext)
-{
-    return setImagesByPaths(
-        wallpaperPath(Qt::Horizontal, id, ext),
-        wallpaperPath(Qt::Vertical, id, ext));
-}
+/*!
 
-bool HsWallpaper::setImagesByPaths(const QString &landscapeImagePath,
-                                   const QString &portraitImagePath)
+*/
+void HsWallpaper::setImage(const QString &path)
 {
-    mLImagePath = landscapeImagePath;
-    mPImagePath = portraitImagePath;
-    mOrientation = HsScene::orientation();
-
-    if (mOrientation == Qt::Horizontal) {
-        setLandscapeImage(mLImagePath, true);
+    if (!path.isEmpty()) {
+        if(mLoader->isRunning()) {
+            mLoader->cancel();
+        }
+    
+        mLoader->setSourcePath(path);
+        mLoader->setTargets(createTargets(path));
+        mLoader->start();
     } else {
-        setPortraitImage(mPImagePath, true);
-    }
-    return true;
-}
-
-void HsWallpaper::setPortraitImage(const QString &path, bool activate)
-{
-    mPImagePath = path;
-    if (activate) {
-        mIconItem->setIcon(HbIcon(QIcon(path)));
-    }
-}
-
-void HsWallpaper::setLandscapeImage(const QString &path, bool activate)
-{
-    mLImagePath = path;
-    if (activate) {
-        mIconItem->setIcon(HbIcon(QIcon(path)));
+        emit imageSetFailed();
     }
 }
 
-bool HsWallpaper::removeImages()
+/*!
+
+*/
+void HsWallpaper::setDefaultImage()
+{
+    if (mIsDefaultImage) {
+        return;
+    }
+
+    QDir dir(wallpaperDirectory());
+    QStringList filters;
+    filters << "default_portrait.*";
+    filters << "default_landscape.*";
+    QFileInfoList infos = dir.entryInfoList(
+        filters, QDir::Files, QDir::Name | QDir::IgnoreCase);
+
+    Q_ASSERT(infos.count() == 2);
+
+    mIsDefaultImage = true;
+    mLandscapeImagePath = infos.first().absoluteFilePath();
+    mPortraitImagePath = infos.last().absoluteFilePath();
+
+    updateIconItem(HsScene::orientation());
+}
+
+/*!
+
+*/
+void HsWallpaper::remove()
 {
-    if (mLImagePath != wallpaperPath(Qt::Horizontal)) {
-        QFile::remove(mLImagePath);
-        mLImagePath.clear();
+    if (mIsDefaultImage) {
+        mIsDefaultImage = false;
+    } else {
+        QFile::remove(mPortraitImagePath);
+        QFile::remove(mLandscapeImagePath);
     }
-    if (mPImagePath != wallpaperPath(Qt::Vertical)) {
-        QFile::remove(mPImagePath);
-        mPImagePath.clear();
+    mPortraitImagePath.clear();
+    mLandscapeImagePath.clear();
+}
+
+/*!
+
+*/
+bool HsWallpaper::setExistingImage()
+{
+    QDir dir(wallpaperDirectory());
+    QStringList filters;
+    filters << QString("%1_portrait.*").arg(mId);
+    filters << QString("%1_landscape.*").arg(mId);
+    QFileInfoList infos = dir.entryInfoList(
+        filters, QDir::Files, QDir::Name | QDir::IgnoreCase);
+
+    Q_ASSERT(infos.isEmpty() || infos.count() == 2);
+
+    if (infos.isEmpty()) {
+        return false;
     }
+
+    mIsDefaultImage = false;
+    mLandscapeImagePath = infos.first().absoluteFilePath();
+    mPortraitImagePath = infos.last().absoluteFilePath();
+
+    updateIconItem(HsScene::orientation());
     return true;
 }
 
-QString HsWallpaper::wallpaperDirectory()
+QString HsWallpaper::rootDirectory() const
 {
 #ifdef Q_OS_SYMBIAN
-    static QString directory =
-        QDir::toNativeSeparators("c:/private/20022f35/wallpapers/");
+    QString directory("c:/private/20022f35/wallpapers/");
 #else
-    static QString directory =
-        QDir::toNativeSeparators(QDir::currentPath() + "/private/20022f35/wallpapers/");
+    QString directory(QDir::currentPath() + "/private/20022f35/wallpapers/");
 #endif
 
+#ifdef HSDOMAINMODEL_TEST
+    directory = QDir::currentPath() + "/wallpapers/";
+#endif
     return directory;
 }
 
-QString HsWallpaper::wallpaperPath(Qt::Orientation orientation,
-                                   const QString &id,
-                                   const QString &ext)
+/*!
+
+*/
+void HsWallpaper::onLoaderFinished()
+{
+    if (mIsDefaultImage) {
+        mIsDefaultImage = false;
+    } else {
+        QFile::remove(mPortraitImagePath);
+        QFile::remove(mLandscapeImagePath);
+    }
+    foreach (QString path, mLoader->targets().keys()) {
+        QString newName = path; newName.remove("temp");
+        QFile::rename(path, newName);
+    }
+    setExistingImage();
+    emit imageSet();
+}
+
+/*!
+
+*/
+void HsWallpaper::onLoaderFailed()
+{
+    foreach (QString path, mLoader->targets().keys()) {
+        QFile::remove(path);
+    }
+    emit imageSetFailed();
+}
+
+/*!
+
+*/
+void HsWallpaper::updateIconItem(Qt::Orientation orientation)
 {
-    QString orientationString = orientation == Qt::Horizontal ?
-        "_landscape." : "_portrait.";
-    return wallpaperDirectory() + id +
-           orientationString + ext;
+    if (orientation == Qt::Vertical) {
+        mIconItem->setIcon(HbIcon(QIcon(mPortraitImagePath)));
+    } else {
+        mIconItem->setIcon(HbIcon(QIcon(mLandscapeImagePath)));
+    }
+}
+
+/*!
+
+*/
+HsSceneWallpaper::HsSceneWallpaper(HsScene *scene, QGraphicsItem *parent)
+  : HsWallpaper(parent),
+    mScene(0)
+{
+    setScene(scene);
+}
+
+/*!
+
+*/
+HsSceneWallpaper::~HsSceneWallpaper()
+{
+}
+
+/*!
+
+*/
+void HsSceneWallpaper::setScene(HsScene *scene)
+{
+    if (!scene) {
+        return;
+    }
+
+    mScene = scene;
+    mId = scene->databaseId();
+    if (!setExistingImage()) {
+        setDefaultImage();
+    }
 }
 
-void HsWallpaper::onOrientationChanged(Qt::Orientation orientation)
+/*!
+
+*/
+QString HsSceneWallpaper::wallpaperDirectory() const
+{
+    return QDir::toNativeSeparators(rootDirectory() + "scene/");
+}
+
+/*!
+
+*/
+QVariantHash HsSceneWallpaper::createTargets(const QString &sourcePath)
 {
-    if (orientation == Qt::Horizontal) {
-        mIconItem->setIcon(HbIcon(QIcon(mLImagePath)));
-    } else {
-        mIconItem->setIcon(HbIcon(QIcon(mPImagePath)));
+    QVariantHash targets;
+
+    QString path = wallpaperDirectory()
+                   + QString("temp%1_").arg(mId)
+                   + QString("%1.")
+                   + QFileInfo(sourcePath).suffix();
+
+    targets.insert(path.arg("portrait"), QSize((2 * 360) + HSCONFIGURATION_GET(bounceEffect), 640));
+    targets.insert(path.arg("landscape"), QSize((2 * 640) + HSCONFIGURATION_GET(bounceEffect), 360));
+    return targets;
+}
+
+/*!
+ PAGE
+*/
+HsPageWallpaper::HsPageWallpaper(HsPage *page, QGraphicsItem *parent)
+  : HsWallpaper(parent),
+    mPage(0)
+{
+    setPage(page);
+}
+
+/*!
+
+*/
+HsPageWallpaper::~HsPageWallpaper()
+{
+}
+
+/*!
+
+*/
+void HsPageWallpaper::setPage(HsPage *page)
+{
+    if (!page) {
+        return;
+    }
+
+    mPage = page;
+    mId = page->databaseId();
+    if (!setExistingImage()) {
+        setDefaultImage();
     }
 }
+
+/*!
+
+*/
+QString HsPageWallpaper::wallpaperDirectory() const
+{
+    return QDir::toNativeSeparators(rootDirectory() + "page/");
+}
+
+/*!
+
+*/
+QVariantHash HsPageWallpaper::createTargets(const QString &sourcePath)
+{
+    QVariantHash targets;
+
+    QString path = wallpaperDirectory()
+                   + QString("temp%1_").arg(mId)
+                   + QString("%1.")
+                   + QFileInfo(sourcePath).suffix();
+
+    targets.insert(path.arg("portrait"), QSize(360, 640));
+    targets.insert(path.arg("landscape"), QSize(640, 360));
+    return targets;
+}
+