|
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: MediaIdUpdater |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <algorithm> |
|
20 #include <memory> |
|
21 |
|
22 #include "mediaidupdater.h" |
|
23 #include "logger.h" |
|
24 #include "javacommonutils.h" |
|
25 #include "driveutilities.h" |
|
26 #include "javastorageentry.h" |
|
27 #include "javastorage.h" |
|
28 #include "javastoragenames.h" |
|
29 |
|
30 using namespace java::backup; |
|
31 using namespace java::storage; |
|
32 using namespace java::util; |
|
33 |
|
34 |
|
35 MediaIdUpdater::MediaIdUpdater() : mStorage(0) |
|
36 { |
|
37 } |
|
38 |
|
39 MediaIdUpdater::~MediaIdUpdater() |
|
40 { |
|
41 close(); |
|
42 } |
|
43 |
|
44 /** |
|
45 * Ensures that java storage does not contain media ids that are not valid any more. |
|
46 * Media id updating is done in following steps: |
|
47 * - get media ids from storage |
|
48 * - get present (valid) media ids |
|
49 * - get difference between storage and present ids (result is invalid ids that need to be updated) |
|
50 * - update invalid media ids with valid ids |
|
51 * @param - |
|
52 * @return - |
|
53 */ |
|
54 void MediaIdUpdater::update() |
|
55 { |
|
56 try |
|
57 { |
|
58 open(); |
|
59 std::set<std::wstring> storageIds = getMediaIdsFromStorage(); |
|
60 std::set<std::wstring> presentIds = getPresentMediaIds(); |
|
61 |
|
62 std::set<std::wstring> invalidIds = removePresentMediaIds(storageIds, presentIds); |
|
63 std::set<std::wstring> validIds = getPresentRemovableDriveMediaIds(); |
|
64 |
|
65 if (isMediaIdUpdateNeeded(invalidIds, validIds)) |
|
66 { |
|
67 updateMediaIds(invalidIds, validIds); |
|
68 } |
|
69 close(); |
|
70 } |
|
71 catch (JavaStorageException jse) |
|
72 { |
|
73 ELOG1(EBackup, "MediaIdUpdater::update() failed: %S", jse.toString().c_str()); |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 * Gets present media ids. Result includes phone, internal and mass memory drive ids if any |
|
79 * @param - |
|
80 * @return present media ids |
|
81 */ |
|
82 std::set<std::wstring> MediaIdUpdater::getPresentMediaIds() |
|
83 { |
|
84 std::set<std::wstring> result; |
|
85 |
|
86 java::fileutils::driveInfos drives; |
|
87 java::fileutils::DriveUtilities::getAccesibleDrives(drives); |
|
88 |
|
89 PLOG1(EBackup, "getPresentMediaIds(count=%d)", drives.size()); |
|
90 for (java::fileutils::driveInfos::const_iterator iter = drives.begin(); iter != drives.end(); iter++) |
|
91 { |
|
92 PLOG2(EBackup, " %S - id=%d", (*iter).iRootPath.c_str(), (*iter).iId); |
|
93 std::wstring mediaId = JavaCommonUtils::intToWstring((*iter).iId); |
|
94 result.insert(mediaId); |
|
95 } |
|
96 |
|
97 result.insert(L"0"); // default set by javainstaller for internal drives |
|
98 return result; |
|
99 } |
|
100 |
|
101 /** |
|
102 * Gets present removable media ids. Result mass memory drive ids if any |
|
103 * @param - |
|
104 * @return present removable media ids |
|
105 */ |
|
106 std::set<std::wstring> MediaIdUpdater::getPresentRemovableDriveMediaIds() |
|
107 { |
|
108 std::set<std::wstring> result; |
|
109 |
|
110 java::fileutils::driveInfos drives; |
|
111 java::fileutils::DriveUtilities::getAccesibleDrives(drives); |
|
112 |
|
113 PLOG1(EBackup, "getPresentRemovableDriveMediaIds(count=%d)", drives.size()); |
|
114 for (java::fileutils::driveInfos::const_iterator iter = drives.begin(); iter != drives.end(); iter++) |
|
115 { |
|
116 if ((*iter).iIsRemovable) |
|
117 { |
|
118 PLOG2(EBackup, " %S - id=%d", (*iter).iRootPath.c_str(), (*iter).iId); |
|
119 std::wstring mediaId = JavaCommonUtils::intToWstring((*iter).iId); |
|
120 result.insert(mediaId); |
|
121 } |
|
122 } |
|
123 |
|
124 return result; |
|
125 } |
|
126 |
|
127 /** |
|
128 * Filters present media ids from given set. |
|
129 * @param[in] aStorageIds original media ids |
|
130 * @param[in] aPresentIds present media ids |
|
131 * @return set of media ids that are invalid i.e. not present at the moment |
|
132 */ |
|
133 std::set<std::wstring> MediaIdUpdater::removePresentMediaIds(const std::set<std::wstring>& aStorageIds, |
|
134 const std::set<std::wstring>& aPresentIds) |
|
135 { |
|
136 std::set<std::wstring> result; |
|
137 |
|
138 std::set_difference(aStorageIds.begin(), aStorageIds.end(), |
|
139 aPresentIds.begin(), aPresentIds.end(), |
|
140 std::inserter(result, result.end())); |
|
141 |
|
142 return result; |
|
143 } |
|
144 |
|
145 /** |
|
146 * Checks if there is a need for updating given media ids |
|
147 * @param[in] aInvalidIds media ids to be updated |
|
148 * @param[in] aValidIds valid ids used during update |
|
149 * @return true, if media ids need to be updated, false otherwise |
|
150 */ |
|
151 bool MediaIdUpdater::isMediaIdUpdateNeeded(const std::set<std::wstring>& aInvalidIds, |
|
152 const std::set<std::wstring>& aValidIds) |
|
153 { |
|
154 if(aInvalidIds.size() > 0 && aValidIds.size() > 0) |
|
155 { |
|
156 return true; |
|
157 } |
|
158 return false; |
|
159 } |
|
160 |
|
161 /** |
|
162 * Gets media ids that can be found from java storage. |
|
163 * @param - |
|
164 * @return media ids found from java storage |
|
165 */ |
|
166 std::set<std::wstring> MediaIdUpdater::getMediaIdsFromStorage() |
|
167 { |
|
168 std::set<std::wstring> result; |
|
169 |
|
170 JavaStorageEntry attribute; |
|
171 attribute.setEntry(MEDIA_ID, L""); |
|
172 |
|
173 JavaStorageApplicationEntry_t findQuery; |
|
174 findQuery.insert(attribute); |
|
175 |
|
176 JavaStorageApplicationList_t foundEntries; |
|
177 |
|
178 mStorage->search(APPLICATION_PACKAGE_TABLE, findQuery, foundEntries); |
|
179 PLOG1(EBackup, "getMediaIdsFromStorage(count=%d)", foundEntries.size()); |
|
180 for (JavaStorageApplicationList_t::const_iterator iter = foundEntries.begin(); iter != foundEntries.end(); iter++) |
|
181 { |
|
182 std::wstring id = (*iter).begin()->entryValue(); |
|
183 PLOG1(EBackup, " id=%S", id.c_str()); |
|
184 result.insert(id); |
|
185 } |
|
186 |
|
187 return result; |
|
188 } |
|
189 |
|
190 /** |
|
191 * Updates given media ids with new values and saves the result in java storage. |
|
192 * @param[in] aInvalidIds media ids to be updated |
|
193 * @param[in] aValidIds valid ids used during update |
|
194 * @return - |
|
195 */ |
|
196 void MediaIdUpdater::updateMediaIds(const std::set<std::wstring>& aInvalidIds, |
|
197 const std::set<std::wstring>& aValidIds) |
|
198 { |
|
199 if (aValidIds.size() == 0) |
|
200 { |
|
201 return; |
|
202 } |
|
203 // use the first present media id for all |
|
204 std::wstring newId = *(aValidIds.begin()); |
|
205 |
|
206 mStorage->startTransaction(); |
|
207 |
|
208 PLOG1(EBackup, "updateMediaIds(count=%d)", aInvalidIds.size()); |
|
209 for (std::set<std::wstring>::const_iterator iter = aInvalidIds.begin(); iter != aInvalidIds.end(); ++iter) |
|
210 { |
|
211 std::wstring origId = (*iter).c_str(); |
|
212 PLOG2(EBackup, " origId=%S - newId=%S", origId.c_str(), newId.c_str()); |
|
213 |
|
214 JavaStorageEntry attribute; |
|
215 attribute.setEntry(MEDIA_ID, newId); |
|
216 |
|
217 JavaStorageApplicationEntry_t updateEntry; |
|
218 updateEntry.insert(attribute); |
|
219 |
|
220 JavaStorageApplicationEntry_t matchEntry; |
|
221 attribute.setEntry(MEDIA_ID, origId); |
|
222 matchEntry.insert(attribute); |
|
223 |
|
224 mStorage->update(APPLICATION_PACKAGE_TABLE, updateEntry, matchEntry); |
|
225 } |
|
226 |
|
227 mStorage->commitTransaction(); |
|
228 } |
|
229 |
|
230 /** |
|
231 * Opens java storage connection |
|
232 * @param - |
|
233 * @return - |
|
234 */ |
|
235 void MediaIdUpdater::open() |
|
236 { |
|
237 close(); |
|
238 mStorage = JavaStorage::createInstance(); |
|
239 mStorage->open(JAVA_DATABASE_NAME); |
|
240 } |
|
241 |
|
242 /** |
|
243 * Closes java storage connection |
|
244 * @param - |
|
245 * @return - |
|
246 */ |
|
247 void MediaIdUpdater::close() |
|
248 { |
|
249 try |
|
250 { |
|
251 if (mStorage) |
|
252 { |
|
253 mStorage->close(); |
|
254 delete mStorage; |
|
255 mStorage = 0; |
|
256 } |
|
257 } |
|
258 catch (JavaStorageException jse) |
|
259 { |
|
260 ELOG1(EBackup, "MediaIdUpdater::close() failed: %S", jse.toString().c_str()); |
|
261 } |
|
262 } |
|
263 |