|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbServers module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include "hbthemewatcher_symbian_p.h" |
|
27 #include "hbthemeserver_symbian_p_p.h" |
|
28 #include "hbthemeutils_p.h" |
|
29 |
|
30 #include <QFile> |
|
31 #include <e32property.h> |
|
32 #include <e32base.h> |
|
33 #include <e32svr.h> |
|
34 |
|
35 // Publish/Subscribe themeRequestProp specific |
|
36 static _LIT_SECURITY_POLICY_PASS(KAllowAllPolicy); |
|
37 static _LIT_SECURITY_POLICY_C1(KThemeChangerPolicy,ECapabilityWriteDeviceData); |
|
38 |
|
39 CHbThemeWatcher::CHbThemeWatcher(HbThemeServerPrivate& aObserver) : CActive(EPriorityStandard), |
|
40 iObserver(aObserver) |
|
41 { |
|
42 CActiveScheduler::Add(this); |
|
43 } |
|
44 |
|
45 void CHbThemeWatcher::ConstructL() |
|
46 { |
|
47 User::LeaveIfError(iFs.Connect()); |
|
48 } |
|
49 |
|
50 CHbThemeWatcher* CHbThemeWatcher::NewL(HbThemeServerPrivate& aObserver) |
|
51 { |
|
52 CHbThemeWatcher* self = new (ELeave) CHbThemeWatcher(aObserver); |
|
53 CleanupStack::PushL(self); |
|
54 self->ConstructL(); |
|
55 CleanupStack::Pop(self); |
|
56 return self; |
|
57 } |
|
58 |
|
59 CHbThemeWatcher::~CHbThemeWatcher() |
|
60 { |
|
61 Cancel(); |
|
62 iFs.Close(); |
|
63 } |
|
64 |
|
65 void CHbThemeWatcher::startWatchingL(const QString &file) |
|
66 { |
|
67 // Cancel ongoing watch |
|
68 if (IsActive()) { |
|
69 Cancel(); |
|
70 } |
|
71 iFile = file; |
|
72 |
|
73 TBuf<256> fileToWatch(iFile.utf16()); |
|
74 iFs.NotifyChange(ENotifyAll, iStatus, fileToWatch); |
|
75 SetActive(); |
|
76 } |
|
77 |
|
78 void CHbThemeWatcher::RunL() |
|
79 { |
|
80 if (iStatus != KErrNone) { |
|
81 return; |
|
82 } |
|
83 |
|
84 QFile file(iFile); |
|
85 if (file.open(QIODevice::ReadOnly)) { |
|
86 file.close(); |
|
87 |
|
88 // theme exists continue watching |
|
89 TBuf<256> fileToWatch(iFile.utf16()); |
|
90 iFs.NotifyChange(ENotifyAll, iStatus, fileToWatch); |
|
91 SetActive(); |
|
92 return; |
|
93 } |
|
94 |
|
95 // theme doesn't exist, change active theme to default |
|
96 iObserver.HandleThemeSelection(HbThemeUtils::defaultTheme().name); |
|
97 } |
|
98 |
|
99 void CHbThemeWatcher::DoCancel() |
|
100 { |
|
101 iFs.NotifyChangeCancel(iStatus); |
|
102 } |
|
103 |
|
104 CHbThemeChangeNotificationListener* CHbThemeChangeNotificationListener::NewL(HbThemeServerPrivate& aObserver) |
|
105 { |
|
106 CHbThemeChangeNotificationListener* self = new (ELeave) CHbThemeChangeNotificationListener(aObserver); |
|
107 CleanupStack::PushL(self); |
|
108 self->ConstructL(); |
|
109 CleanupStack::Pop(self); |
|
110 return self; |
|
111 } |
|
112 |
|
113 CHbThemeChangeNotificationListener::CHbThemeChangeNotificationListener(HbThemeServerPrivate& aObserver) |
|
114 :CActive(EPriorityStandard),iObserver(aObserver) |
|
115 { |
|
116 |
|
117 } |
|
118 |
|
119 void CHbThemeChangeNotificationListener::ConstructL() |
|
120 { |
|
121 TInt err = RProperty::Define( KServerUid3, KNewThemeForThemeChanger, RProperty::ELargeText, KAllowAllPolicy, KThemeChangerPolicy ); |
|
122 if ( err != KErrAlreadyExists ) { |
|
123 User::LeaveIfError( err ); |
|
124 } |
|
125 err = themeRequestProp.Attach(KServerUid3, KNewThemeForThemeChanger ); |
|
126 User::LeaveIfError(err); |
|
127 |
|
128 CActiveScheduler::Add(this); |
|
129 } |
|
130 |
|
131 CHbThemeChangeNotificationListener::~CHbThemeChangeNotificationListener() |
|
132 { |
|
133 stopListening(); |
|
134 } |
|
135 |
|
136 void CHbThemeChangeNotificationListener::startListeningL() |
|
137 { |
|
138 if (IsActive()) { |
|
139 return; //do nothing if already listening |
|
140 } |
|
141 |
|
142 User::LeaveIfError(themeRequestProp.Attach(KServerUid3,KNewThemeForThemeChanger)); |
|
143 //Subscribe for updates |
|
144 themeRequestProp.Subscribe(iStatus); |
|
145 |
|
146 SetActive(); |
|
147 |
|
148 } |
|
149 |
|
150 void CHbThemeChangeNotificationListener::stopListening() |
|
151 { |
|
152 Cancel(); // cancel |
|
153 if(IsActive()) { // only if already listening |
|
154 themeRequestProp.Close(); // Close the handle since it is not needed anymore |
|
155 } |
|
156 } |
|
157 |
|
158 /* |
|
159 * Returns TRUE if parsing succeeded, FALSE otherwise |
|
160 */ |
|
161 bool CHbThemeChangeNotificationListener::parseData(const TDesC& requestData, HbThemeServerRequest& etype, TDes& data) |
|
162 { |
|
163 TInt result = 0; |
|
164 const TChar delimiter = ':'; |
|
165 // initialize return value as failed |
|
166 bool bSuccess = false; |
|
167 |
|
168 result = requestData.Locate( delimiter ); |
|
169 if( KErrNotFound != result ) { |
|
170 TInt len = requestData.Length(); |
|
171 const TDesC& typestr = requestData.Mid(0, result); |
|
172 TLex atype(typestr); |
|
173 TInt iType; |
|
174 atype.Val( iType ); |
|
175 etype = static_cast<HbThemeServerRequest>(iType); |
|
176 data.Copy( requestData.Mid( result + 1, len - result - 1 ) ); |
|
177 bSuccess = true; |
|
178 } else { |
|
179 bSuccess = false; |
|
180 } |
|
181 |
|
182 return bSuccess; |
|
183 } |
|
184 |
|
185 static const TInt KThemeChangeDataBufferSize = 256; |
|
186 |
|
187 void CHbThemeChangeNotificationListener::RunL() |
|
188 { |
|
189 // Subscribe first to make sure we don't miss any |
|
190 // when handling this one. |
|
191 themeRequestProp.Subscribe(iStatus); |
|
192 |
|
193 SetActive(); |
|
194 |
|
195 TBuf<KThemeChangeDataBufferSize> requestData; |
|
196 TInt ret = themeRequestProp.Get(requestData); |
|
197 switch (ret) { |
|
198 case KErrNone: |
|
199 { |
|
200 QString qrequestData((QChar*)requestData.Ptr(),requestData.Length()); |
|
201 HbThemeServerRequest etype = EInvalidServerRequest; |
|
202 TBuf<KThemeChangeDataBufferSize> data; |
|
203 ///Parse the data from the Publisher |
|
204 bool bSuccess = parseData( requestData, etype, data); |
|
205 if( bSuccess && EThemeSelection == etype) { |
|
206 QString str((QChar*)data.Ptr(), data.Length()); |
|
207 str = str.trimmed(); |
|
208 iObserver.HandleThemeSelection( str ); |
|
209 } |
|
210 } |
|
211 break; |
|
212 case KErrPermissionDenied: |
|
213 qDebug() << "KErrPermissionDenied"; |
|
214 break; |
|
215 case KErrNotFound: |
|
216 qDebug() << "KErrNotFound"; |
|
217 break; |
|
218 case KErrArgument: |
|
219 qDebug() << "KErrArgument"; |
|
220 break; |
|
221 case KErrOverflow: |
|
222 qDebug() << "KErrOverflow"; |
|
223 break; |
|
224 } |
|
225 } |
|
226 |
|
227 void CHbThemeChangeNotificationListener::DoCancel() |
|
228 { |
|
229 themeRequestProp.Cancel(); |
|
230 } |