|
1 /* |
|
2 * Copyright (c) 2009 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 <QDir> |
|
19 #include <QFileInfo> |
|
20 |
|
21 #include <HbMainWindow> |
|
22 #include <HbView> |
|
23 |
|
24 #include "hsselectbackgroundstate.h" |
|
25 #include "hsscene.h" |
|
26 #include "hsscenedata.h" |
|
27 #include "hswallpaper.h" |
|
28 #include "hsdatabase.h" |
|
29 |
|
30 #include "xqaiwgetimageclient.h" |
|
31 #include "xqaiwcommon.h" |
|
32 |
|
33 /*! |
|
34 \class HsSelectBackgroundState |
|
35 \ingroup group_hshomescreenstateprovider |
|
36 \brief Implements imagefetcher event listening and handles those events. |
|
37 |
|
38 \sa StateMachine |
|
39 */ |
|
40 |
|
41 /*! |
|
42 Constructor. |
|
43 \a parent Owner. |
|
44 */ |
|
45 HsSelectBackgroundState::HsSelectBackgroundState(QState *parent): |
|
46 QState(parent), |
|
47 mXQAIWGetImageClient(0), |
|
48 mSourceView(0) |
|
49 { |
|
50 mXQAIWGetImageClient = new XQAIWGetImageClient; |
|
51 connect(this, SIGNAL(entered()), SLOT(selectPageBackgroundAction())); |
|
52 connect(this, SIGNAL(exited()), SLOT(disconnectImageFetcherAction())); |
|
53 } |
|
54 |
|
55 /*! |
|
56 Destructor. |
|
57 |
|
58 */ |
|
59 HsSelectBackgroundState::~HsSelectBackgroundState() |
|
60 { |
|
61 delete mXQAIWGetImageClient; |
|
62 } |
|
63 |
|
64 /*! |
|
65 \internal |
|
66 Scales and crops (if needed) \a image for given \a orientation |
|
67 and saves it to \a path. Return true on successful. |
|
68 */ |
|
69 bool HsSelectBackgroundState::saveImage(QImage &image, const QString &path, Qt::Orientation orientation) |
|
70 { |
|
71 QRect rect; |
|
72 if (orientation == Qt::Vertical) { |
|
73 rect.setRect(0, 0, 2 * 360, 640); |
|
74 } else { |
|
75 rect.setRect(0, 0, 2 * 640, 360); |
|
76 } |
|
77 |
|
78 if (image.rect().contains(rect)) { |
|
79 rect.moveCenter(image.rect().center()); |
|
80 return image.copy(rect).save(path); |
|
81 } else { |
|
82 QImage scaledImage = image.scaledToWidth(rect.width()); |
|
83 rect.moveCenter(scaledImage.rect().center()); |
|
84 return scaledImage.copy(rect).save(path); |
|
85 } |
|
86 } |
|
87 |
|
88 /*! |
|
89 \internal |
|
90 Connects to image fetcher and launches "remote" ui from photos |
|
91 from which user can select background image |
|
92 */ |
|
93 void HsSelectBackgroundState::selectPageBackgroundAction() |
|
94 { |
|
95 mSourceView = HsScene::mainWindow()->currentView(); |
|
96 |
|
97 connect(mXQAIWGetImageClient, SIGNAL(fetchComplete(QStringList)), |
|
98 SLOT(onBackgroundImageFetched(QStringList))); |
|
99 connect(mXQAIWGetImageClient, SIGNAL(fetchFailed(int)), |
|
100 SLOT(onBackgroundImageFetchFailed(int))); |
|
101 |
|
102 QVariantMap filter; |
|
103 mXQAIWGetImageClient->fetch(filter, SelectionSingle); |
|
104 } |
|
105 |
|
106 /*! |
|
107 \internal |
|
108 disconnects photos image fetcher services slots. |
|
109 */ |
|
110 |
|
111 void HsSelectBackgroundState::disconnectImageFetcherAction() |
|
112 { |
|
113 HsScene::mainWindow()->setCurrentView(mSourceView); |
|
114 mXQAIWGetImageClient->disconnect(this); |
|
115 } |
|
116 |
|
117 /*! |
|
118 \internal |
|
119 Called when user has selected an image |
|
120 */ |
|
121 void HsSelectBackgroundState::onBackgroundImageFetched(QStringList imageStringList) |
|
122 { |
|
123 HsScene *scene = HsScene::instance(); |
|
124 |
|
125 HsDatabase *db = HsDatabase::instance(); |
|
126 Q_ASSERT(db); |
|
127 |
|
128 HsSceneData sceneData; |
|
129 if (!db->scene(sceneData)) { |
|
130 emit event_waitInput(); |
|
131 return; |
|
132 } |
|
133 |
|
134 QFileInfo fileInfo(imageStringList.first()); |
|
135 QString fileExtension(""); |
|
136 if (!fileInfo.suffix().isEmpty()) { |
|
137 fileExtension = fileInfo.suffix(); |
|
138 } |
|
139 |
|
140 QFile::remove(sceneData.portraitWallpaper()); |
|
141 QFile::remove(sceneData.landscapeWallpaper()); |
|
142 |
|
143 QString wallpaperDir = HsWallpaper::wallpaperDirectory(); |
|
144 QDir dir(wallpaperDir); |
|
145 if (!dir.exists()) { |
|
146 dir.mkpath(wallpaperDir); |
|
147 } |
|
148 |
|
149 QString portraitPath = HsWallpaper::wallpaperPath( |
|
150 Qt::Vertical, QString(), fileExtension); |
|
151 QString landscapePath = HsWallpaper::wallpaperPath( |
|
152 Qt::Horizontal, QString(), fileExtension); |
|
153 |
|
154 QImage image(imageStringList.first()); |
|
155 |
|
156 if (saveImage(image, portraitPath, Qt::Vertical) && |
|
157 saveImage(image, landscapePath, Qt::Horizontal)) { |
|
158 sceneData.setPortraitWallpaper(portraitPath); |
|
159 sceneData.setLandscapeWallpaper(landscapePath); |
|
160 if (db->updateScene(sceneData)) { |
|
161 scene->wallpaper()->setImagesById(QString(), fileInfo.suffix()); |
|
162 } |
|
163 } |
|
164 |
|
165 emit event_waitInput(); |
|
166 } |
|
167 |
|
168 /*! |
|
169 \internal |
|
170 Called when selection of background image fails |
|
171 */ |
|
172 void HsSelectBackgroundState::onBackgroundImageFetchFailed(int error) |
|
173 { |
|
174 Q_UNUSED(error) |
|
175 emit event_waitInput(); |
|
176 } |