|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #ifndef __TZLOCALIZATIONDB_H__ |
|
17 #define __TZLOCALIZATIONDB_H__ |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <d32dbms.h> |
|
21 #include <tzlocalizationdatatypes.h> |
|
22 #include <tz.h> |
|
23 #include "tzsystemdata.h" |
|
24 #include "tzuserdata.h" |
|
25 #include "mtimezonedataaccess.h" |
|
26 |
|
27 class CTzServer; |
|
28 |
|
29 /** |
|
30 This class provides access to a database containing the localization information. |
|
31 |
|
32 The database contains two tables : one called "FrequentlyUsedZones" containing time zones |
|
33 and one called "UserCities" containing cities. |
|
34 |
|
35 The "FrequentlyUsedZones" table contains one entry (row) for each of the frequently used zones |
|
36 as defined by the CTzLocalizedTimeZone::TTzFrequentlyUsedZone enum (i.e. 5 rows at the time of |
|
37 writing). The table always contains this number of rows. When the database is created all the |
|
38 rows are initialized to a "null" value. |
|
39 |
|
40 The table contains 9 columns containing the following information: |
|
41 Column 1: The time zone id (note this column is a TUint16 while CTzId uses a TUint32) |
|
42 Column 2: The standard name of the time zone |
|
43 Column 3: The DST name of the time zone |
|
44 Column 4: The short standard name of the time zone |
|
45 Column 5: The short DST name of the time zone |
|
46 Column 6: The name of the city that is representative of this time zone |
|
47 Column 7: The group id of the city that is representative of this time zone |
|
48 Column 8: The resource id associated with the time zone id. |
|
49 Column 9: The index of the city in the resource file (note this is not the resource id but the |
|
50 position of this city in the list of cities in the resource file) |
|
51 |
|
52 In other words columns 1 and 8 store the values of CTzLocalizedTimeZone::iTzLocalizedId columns 2 to 5 |
|
53 store the other members of CTzLocalizedTimeZone. |
|
54 |
|
55 As we mentioned above this table always has the same number of rows but they may have been initialized to |
|
56 a "null". The code in CTzLocalizer::PrepareFrequentlyUsedZonesL() checks if this is the case and replaces |
|
57 the "null" value with the correct data. |
|
58 |
|
59 Column 9 holds an index that is used when changing language. This is used in the |
|
60 CTzLocalizer::UpdateFrequentlyUsedZonesL() function. When changing language the UpdateFrequentlyUsedZonesL() |
|
61 code first tries to find a city with the same name in the new resource file. If it can't find a city with |
|
62 the same name it will use the city that has the same index. The index is simply the position of the city in |
|
63 the resource file so this code assumes that cities are in the same order in the different resource files. See |
|
64 CTzLocalizationResourceReader::AddCityArrayFromResourceL() for the code that initializes the index. |
|
65 |
|
66 Note that this table used to have only 8 columns. Column 9 was added as part of a defect fix in |
|
67 October 2006. The CTzLocalizer class will check whether the database has 8 or 9 columns on construction |
|
68 and upgrade the database format if needed. However because of this the CTzLocalizationDb code must cope |
|
69 with the absence of the 9th column. |
|
70 |
|
71 The "UserCities" table contains the list of cities that have been added by the user using the |
|
72 @see CTzLocalizer::AddCityL() function. The table contains 4 columns containing the following |
|
73 information: |
|
74 Column 1: The id of the time zone in which the city is |
|
75 Column 2: The name of the city |
|
76 Column 3: The id of the group this city is part of or 0 if the city doesn't belong to a group |
|
77 Column 4: The resource id associated with the time zone id. |
|
78 |
|
79 In other words columns 1 and 2 store the values of CTzLocalizedCity::iTzLocalizedId, column 2 stores |
|
80 the value of CTzLocalizedCity::iName and column stores the value of CTzLocalizedCity::iGroupId. |
|
81 |
|
82 The columns can be addressed using the TTzCityColumn enum. |
|
83 |
|
84 @internalComponent |
|
85 */ |
|
86 class CTzLocalizationDb : public CBase, public MTzUserDataChangeObserver, public MTzDataObserver |
|
87 { |
|
88 public: |
|
89 static CTzLocalizationDb* NewL(CTzServer& aServer); |
|
90 ~CTzLocalizationDb(); |
|
91 |
|
92 private: |
|
93 CTzLocalizationDb(CTzServer& aServer); |
|
94 void ConstructL(); |
|
95 |
|
96 public: |
|
97 void OpenDbL(); |
|
98 void CloseDb(); |
|
99 |
|
100 private: |
|
101 void CreateBlankDatabaseL(); |
|
102 void CreateBlankFrequentlyUsedZoneTableL(); |
|
103 void CreateBlankUserCityTableL(); |
|
104 TBool IsInvalidL(); |
|
105 void RecreateBlankDatabaseL(); |
|
106 TBool NeedsUpdatingL(); |
|
107 |
|
108 public: // from MTzUserDataChangeObserver |
|
109 void NotifyUserTzRulesChange(TTzUserDataChange aChange); |
|
110 void NotifyUserTzNamesChange(TTzUserDataChange aChange); |
|
111 |
|
112 // from MTzDataObserver |
|
113 void NotifyTZDataStatusChangeL(RTz::TTzChanges); |
|
114 |
|
115 public: |
|
116 void ReadCitiesL(RPointerArray<CTzLocalizedCityRecord>& aCities); |
|
117 void ReadCitiesL(RPointerArray<CTzLocalizedCityRecord>& aCities, TInt aTimeZoneId); |
|
118 void ReadCitiesInGroupL(RPointerArray<CTzLocalizedCityRecord>& aCities, TUint8 aGroupId); |
|
119 CTzLocalizedTimeZoneRecord* ReadFrequentlyUsedZoneL(TInt aFrequentlyUsedZone); |
|
120 CTzLocalizedCityRecord* ReadCachedTimeZoneCityL(TInt aFrequentlyUsedZone); |
|
121 void WriteCityL(const TDesC& aCityName, TUint16 aCityTzId, TUint8 aCityGroupId, TUint aCityTzResourceId); |
|
122 void DeleteCityL(const TDesC& aCityName, TUint16 aCityTzId); |
|
123 void WriteFrequentlyUsedZoneL(const CTzLocalizedTimeZoneRecord& aTimeZone, |
|
124 const CTzLocalizedCityRecord& aCity, TInt aFrequentlyUsedZone); |
|
125 void WriteAllFrequentlyUsedZonesL(const RPointerArray<CTzLocalizedTimeZoneRecord>& aTimeZones, |
|
126 const RPointerArray<CTzLocalizedCityRecord>& aCities); |
|
127 |
|
128 // methods to handle backup-restore |
|
129 void BackupBeginningL(); |
|
130 void BackupCompletedL(); |
|
131 void RestoreBeginningL(); |
|
132 void RestoreCompletedL(); |
|
133 |
|
134 private: |
|
135 void PrepareCityViewL(RDbView& aCityView, const TDesC& aSqlQuery); |
|
136 void PrepareZoneViewL(RDbView& aZoneView); |
|
137 HBufC* GetCityQueryLC(const TDesC& aCityName, TInt aCityTimeZoneId); |
|
138 HBufC* GetCityQueryLC(TInt aCityTimeZoneId); |
|
139 void DoReadCitiesL(RPointerArray<CTzLocalizedCityRecord>& aCities, const TDesC& aSqlString); |
|
140 void DoWriteFrequentlyUsedZoneL(RDbView& aZoneView, const CTzLocalizedTimeZoneRecord& aTimeZone, |
|
141 const CTzLocalizedCityRecord& aCity); |
|
142 TInt GetTimeZoneIdFromTzServerL(CTzSystemDataDb& aSystemDataDb); |
|
143 void NotifyUserTzNamesChangeL(TTzUserDataChange aChange); |
|
144 TBool ExistsL(TUint aTzId); |
|
145 static void ReleaseMutex(TAny* iTarget); |
|
146 void DeleteCitiesL(TTzUserDataChange aChange); |
|
147 void UpdateTimeZoneAndCityRecordL(RPointerArray<CTzLocalizedTimeZoneRecord>& aFrequentlyUsedZones, |
|
148 RPointerArray<CTzLocalizedCityRecord>& aCachedCities, TInt aTzId); |
|
149 static void CleanupTimeZonePointerArray(TAny* aArray); |
|
150 static void CleanupCityPointerArray(TAny* aArray); |
|
151 TBool ExistsInSystemDbL(TUint aTzId, CTzSystemDataDb& aSystemDataDb); |
|
152 void UpdateTimeZoneAndCityRecordForSystemDataL(RPointerArray<CTzLocalizedTimeZoneRecord>& aFrequentlyUsedZones, |
|
153 RPointerArray<CTzLocalizedCityRecord>& aCachedCities, TInt aTzId, CTzSystemDataDb& aSystemDataDb); |
|
154 void RestoreDbL(); |
|
155 TUint32 GetFrequentlyUsedDefaultZoneCenRepKeyL(CTzLocalizedTimeZone::TTzFrequentlyUsedZone aFreqUsedZone); |
|
156 CTzLocalizedTimeZoneRecord* DoReadFrequentlyUsedZoneL(TInt aFrequentlyUsedZone); |
|
157 void DoWriteAllFrequentlyUsedZonesL(const RPointerArray<CTzLocalizedTimeZoneRecord>& aTimeZones, |
|
158 const RPointerArray<CTzLocalizedCityRecord>& aCities); |
|
159 TInt GetFrequentlyUsedDefaultZoneIdL(CTzLocalizedTimeZone::TTzFrequentlyUsedZone aFreqUsedZone, CTzSystemDataDb& aSystemDataDb); |
|
160 |
|
161 private: |
|
162 enum TTzCityColumn |
|
163 { |
|
164 // Enum will used as a DB column index, so it must start at 1 |
|
165 ETzCityId =1, |
|
166 ETzCityCity, |
|
167 ETzCityGroup, |
|
168 ETzCityResourceId, |
|
169 }; |
|
170 |
|
171 // enum that specifies the maximum length of the strings corresponding |
|
172 // to time zone IDs and group IDs |
|
173 enum TTzStringLength |
|
174 { |
|
175 // Group ID is an unsigned 8-bit integer |
|
176 EMaxGroupIdStringLength = 3, |
|
177 // Time Zone ID is an unsigned 16-bit integer |
|
178 EMaxTimeZoneIdStringLength = 5 |
|
179 }; |
|
180 |
|
181 private: |
|
182 CTzServer& iServer; |
|
183 RDbs iDbsSession; |
|
184 RDbNamedDatabase iLocalizedTimeZoneDb; |
|
185 /** |
|
186 This is used to forbid clients to access the database when a backup/restore |
|
187 is taking place. The database can also be locked permanently if there has been |
|
188 an unrecoverable error during an update, in which case only a server reboot |
|
189 will unlock it. |
|
190 */ |
|
191 TBool iLocked; |
|
192 // Protecting iZoneView changes |
|
193 RMutex iZoneMutex; |
|
194 }; |
|
195 |
|
196 #endif |