|
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 using System; |
|
18 using System.Collections.Generic; |
|
19 |
|
20 namespace SymbianUtils.Settings |
|
21 { |
|
22 public class XmlSettingsBoundedList |
|
23 { |
|
24 #region Constructors |
|
25 public XmlSettingsBoundedList( string aCategory, XmlSettings aSettings ) |
|
26 { |
|
27 // Since the max capacity was not supplied, we can attempt to read |
|
28 // it from the settings or then if not found, we default to 10. |
|
29 iSettings = aSettings; |
|
30 iCategory = aCategory; |
|
31 iMaxCapacity = SettingsMaxCapacity; |
|
32 // |
|
33 Restore(); |
|
34 } |
|
35 |
|
36 public XmlSettingsBoundedList( string aCategory, XmlSettings aSettings, int aMaxCapacity ) |
|
37 { |
|
38 iSettings = aSettings; |
|
39 iCategory = aCategory; |
|
40 iMaxCapacity = aMaxCapacity; |
|
41 // |
|
42 Restore(); |
|
43 } |
|
44 #endregion |
|
45 |
|
46 #region API |
|
47 public void Restore() |
|
48 { |
|
49 // Newest additions are stored at the front of the list. |
|
50 // Oldest items are therefore at the end |
|
51 iList.Clear(); |
|
52 // |
|
53 int settingsCount = SettingsCount; |
|
54 for ( int i = 0; i < settingsCount && i < MaxCapacity; i++ ) |
|
55 { |
|
56 string item = SettingItem(i); |
|
57 iList.Add( item ); |
|
58 } |
|
59 } |
|
60 |
|
61 public void Store() |
|
62 { |
|
63 // Remove all entries that already exist |
|
64 // within the settings object |
|
65 RemoveAllExistingSettings(); |
|
66 |
|
67 // Now add the replacement items |
|
68 int settingsCount = iList.Count; |
|
69 SettingsCount = settingsCount; |
|
70 for(int i=0; i<settingsCount; i++) |
|
71 { |
|
72 string itemKey = MakeSettingsKey( i ); |
|
73 string itemValue = iList[ i ]; |
|
74 // |
|
75 iSettings[ Category, itemKey ] = itemValue; |
|
76 } |
|
77 } |
|
78 |
|
79 public void Add( string aValue ) |
|
80 { |
|
81 // First, add the item |
|
82 iList.Insert( 0, aValue ); |
|
83 |
|
84 // If the capacity has reached the limit, we remove the last (oldest) item |
|
85 if ( iList.Count > MaxCapacity ) |
|
86 { |
|
87 iList.RemoveAt( MaxCapacity ); |
|
88 } |
|
89 } |
|
90 #endregion |
|
91 |
|
92 #region Properties |
|
93 public int MaxCapacity |
|
94 { |
|
95 get { return iMaxCapacity; } |
|
96 } |
|
97 |
|
98 public string Category |
|
99 { |
|
100 get { return iCategory; } |
|
101 } |
|
102 |
|
103 public int Count |
|
104 { |
|
105 get { return iList.Count; } |
|
106 } |
|
107 #endregion |
|
108 |
|
109 #region Internal properties |
|
110 private int SettingsCount |
|
111 { |
|
112 get |
|
113 { |
|
114 int count = 0; |
|
115 string countAsString = iSettings[Category, "_NumberOfItems"]; |
|
116 if (countAsString != null) |
|
117 { |
|
118 count = System.Convert.ToInt32(countAsString); |
|
119 } |
|
120 return count; |
|
121 } |
|
122 set |
|
123 { |
|
124 iSettings[Category, "_NumberOfItems"] = value.ToString(); |
|
125 } |
|
126 } |
|
127 |
|
128 private int SettingsMaxCapacity |
|
129 { |
|
130 get |
|
131 { |
|
132 int capacity = 0; |
|
133 string capacityAsString = iSettings[Category, "_MaxCapacity"]; |
|
134 if (capacityAsString != null) |
|
135 { |
|
136 capacity = System.Convert.ToInt32(capacityAsString); |
|
137 } |
|
138 return capacity; |
|
139 } |
|
140 set |
|
141 { |
|
142 iSettings[Category, "_MaxCapacity"] = value.ToString(); |
|
143 } |
|
144 } |
|
145 #endregion |
|
146 |
|
147 #region Internal methods |
|
148 private string MakeSettingsKey( int aIndex ) |
|
149 { |
|
150 return "_Entry_Id_" + aIndex.ToString( "0000" ); |
|
151 } |
|
152 |
|
153 private string SettingItem( int aIndex ) |
|
154 { |
|
155 string key = MakeSettingsKey( aIndex ); |
|
156 string itemValue = iSettings[ Category, key ]; |
|
157 // |
|
158 return itemValue; |
|
159 } |
|
160 |
|
161 private void RemoveAllExistingSettings() |
|
162 { |
|
163 iSettings.Remove( Category ); |
|
164 } |
|
165 #endregion |
|
166 |
|
167 #region Data members |
|
168 int iMaxCapacity = 100; |
|
169 readonly string iCategory; |
|
170 readonly XmlSettings iSettings; |
|
171 List<string> iList = new List<string>(10); |
|
172 #endregion |
|
173 } |
|
174 } |