|
1 /* |
|
2 * Copyright (c) 2002 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 * Stores and handles text labels for note dialogs. Each note dialog |
|
16 * can have two different labels, one singular and one plural labels. |
|
17 * For example:- |
|
18 * "You have 1 new message" is the singular label and |
|
19 * "You have 2 new messages" is the plural label. |
|
20 * This class supports the ability to change number inside the label, e.g.:- |
|
21 * "You have 100 new messages" is also supported. |
|
22 * |
|
23 */ |
|
24 |
|
25 |
|
26 #include <StringLoader.h> |
|
27 #include "AknPanic.h" |
|
28 #include "akntext.h" |
|
29 #include "AknUtils.h" |
|
30 |
|
31 |
|
32 _LIT(KNumberId, "%N"); |
|
33 _LIT(KDigitId, "%d"); |
|
34 _LIT(KUnsignedId, "%U"); |
|
35 |
|
36 CAknText::CAknText() |
|
37 { |
|
38 iPlurality = ENotSpecified; |
|
39 iType = ENotFormatted; |
|
40 } |
|
41 |
|
42 CAknText::CAknText(const TType& aType) : iType(aType) |
|
43 { |
|
44 iPlurality = ENotSpecified; |
|
45 } |
|
46 |
|
47 CAknText::~CAknText() |
|
48 { |
|
49 delete iUnformattedSingularText; |
|
50 delete iUnformattedPluralText; |
|
51 delete iText; |
|
52 delete iTextCopy; |
|
53 } |
|
54 |
|
55 |
|
56 void CAknText::ConstructFromResourceL(TResourceReader& aRes) |
|
57 { |
|
58 if (iType == ENotFormatted) |
|
59 { |
|
60 iUnformattedSingularText = aRes.ReadHBufCL(); |
|
61 iUnformattedPluralText = aRes.ReadHBufCL(); |
|
62 |
|
63 if (iUnformattedSingularText) |
|
64 iPlurality = ESingular; |
|
65 |
|
66 if (iUnformattedPluralText && !iUnformattedSingularText) |
|
67 iPlurality = EPlural; |
|
68 |
|
69 FormatL(); |
|
70 } |
|
71 else if (iType == EFormatted) |
|
72 { |
|
73 iText = aRes.ReadHBufCL(); |
|
74 if (iText) |
|
75 iTextCopy = iText->Des().AllocL(); |
|
76 } |
|
77 } |
|
78 |
|
79 CAknText& CAknText::operator=(CAknText& aNoteText) |
|
80 { |
|
81 if (&aNoteText == this) |
|
82 return *this; |
|
83 |
|
84 iType = aNoteText.iType; |
|
85 |
|
86 if (aNoteText.iPlurality != ENotSpecified) |
|
87 iPlurality = aNoteText.iPlurality; |
|
88 |
|
89 if (aNoteText.iNumberHasBeenSet) |
|
90 { |
|
91 iNumber = aNoteText.iNumber; |
|
92 iNumberHasBeenSet = aNoteText.iNumberHasBeenSet; |
|
93 } |
|
94 |
|
95 #define __TRANSFER_PTR_(P1,P2) if (P1) { delete P2; P2 = P1; P1 = NULL; } |
|
96 |
|
97 __TRANSFER_PTR_(aNoteText.iUnformattedSingularText,iUnformattedSingularText); |
|
98 __TRANSFER_PTR_(aNoteText.iUnformattedPluralText,iUnformattedPluralText); |
|
99 __TRANSFER_PTR_(aNoteText.iText,iText); |
|
100 __TRANSFER_PTR_(aNoteText.iTextCopy,iTextCopy); |
|
101 |
|
102 #undef __TRASFER_PTR_ |
|
103 |
|
104 TRAP_IGNORE(FormatL()); |
|
105 return *this; |
|
106 } |
|
107 |
|
108 |
|
109 void CAknText::SetPluralityL(TBool aIsPlural) |
|
110 { |
|
111 iType = ENotFormatted; |
|
112 if (aIsPlural) |
|
113 iPlurality = EPlural; |
|
114 else |
|
115 iPlurality = ESingular; |
|
116 FormatL(); |
|
117 } |
|
118 |
|
119 void CAknText::SetNumberL(TInt aNumber) |
|
120 { |
|
121 iType = ENotFormatted; |
|
122 iNumberHasBeenSet = ETrue; |
|
123 iNumber = aNumber; |
|
124 SetPluralityL(iNumber > 1); |
|
125 } |
|
126 |
|
127 void CAknText::DoSetTextL(HBufC*& aBuffer, const TDesC& aText) |
|
128 { |
|
129 if (!aBuffer || aBuffer->Des().MaxLength() < aText.Length() ) |
|
130 { |
|
131 delete aBuffer; |
|
132 aBuffer = NULL; |
|
133 aBuffer = aText.AllocL(); |
|
134 } |
|
135 else |
|
136 { |
|
137 *aBuffer = aText; |
|
138 } |
|
139 } |
|
140 |
|
141 void CAknText::SetL(const TDesC& aText) |
|
142 { |
|
143 if (TextIsNotFormatted(aText)) |
|
144 { |
|
145 if (iPlurality == ESingular) |
|
146 { |
|
147 DoSetTextL(iUnformattedSingularText,aText); |
|
148 } |
|
149 else |
|
150 { |
|
151 DoSetTextL(iUnformattedPluralText,aText); |
|
152 } |
|
153 FormatL(); |
|
154 } |
|
155 else |
|
156 { |
|
157 DoSetTextL(iText,aText); |
|
158 DoSetTextL(iTextCopy,aText); |
|
159 iType = EFormatted; |
|
160 } |
|
161 } |
|
162 |
|
163 /** |
|
164 * Return a copy of the text, that the client can modify |
|
165 */ |
|
166 TPtr CAknText::Get() const |
|
167 { |
|
168 if (iText && iTextCopy) |
|
169 { |
|
170 if (iTextCopy->Des().MaxLength() >= iText->Des().Length()) |
|
171 { |
|
172 *iTextCopy = *iText; |
|
173 return iTextCopy->Des(); |
|
174 } |
|
175 } |
|
176 return TPtr(0,0); |
|
177 } |
|
178 |
|
179 void CAknText::FormatL() |
|
180 { |
|
181 if (iType == EFormatted) |
|
182 return; |
|
183 |
|
184 if (iPlurality == ENotSpecified) |
|
185 return; |
|
186 |
|
187 if (iPlurality == ESingular && iUnformattedSingularText) |
|
188 DoFormatTextL(iUnformattedSingularText); |
|
189 |
|
190 else if (iPlurality == EPlural && iUnformattedPluralText) |
|
191 DoFormatTextL(iUnformattedPluralText); |
|
192 } |
|
193 |
|
194 void CAknText::DoFormatTextL(HBufC* aUnformattedText) |
|
195 { |
|
196 __ASSERT_DEBUG(aUnformattedText,Panic(EAknPanicNullPointer)); |
|
197 |
|
198 TPtr unformattedText = TranslateDintoN(aUnformattedText); |
|
199 |
|
200 TInt len = aUnformattedText->Length() + 10; //10 is because biggest int32 has 10 digits at most |
|
201 if (!iText || iText->Des().MaxLength() < len) |
|
202 { |
|
203 delete iText; |
|
204 iText = NULL; |
|
205 iText = HBufC::NewL(len); |
|
206 } |
|
207 |
|
208 TPtr formattedText = iText->Des(); |
|
209 |
|
210 |
|
211 if ( unformattedText.Find(KNumberId) != KErrNotFound) // threre is a number key |
|
212 { |
|
213 StringLoader::Format( formattedText, unformattedText, -1, iNumber); |
|
214 } |
|
215 else |
|
216 { |
|
217 formattedText.Zero(); |
|
218 formattedText.Append(unformattedText); |
|
219 } |
|
220 |
|
221 |
|
222 if (iText && (!iTextCopy || iTextCopy->Des().MaxLength() < iText->Des().Length())) |
|
223 { |
|
224 delete iTextCopy; |
|
225 iTextCopy = NULL; |
|
226 iTextCopy = iText->Des().AllocL(); |
|
227 } |
|
228 } |
|
229 |
|
230 // Translate %d and %U into %N |
|
231 TPtr CAknText::TranslateDintoN(HBufC* aUnformattedText) |
|
232 { |
|
233 TPtr text = aUnformattedText->Des(); |
|
234 |
|
235 TInt pos = text.Find(KDigitId); |
|
236 if ( pos == KErrNotFound ) |
|
237 { |
|
238 pos = text.Find(KUnsignedId); |
|
239 } |
|
240 if (pos != KErrNotFound) |
|
241 { |
|
242 text[pos+1] = 'N'; |
|
243 } |
|
244 |
|
245 return text; |
|
246 } |
|
247 |
|
248 TBool CAknText::TextIsNotFormatted(const TDesC& aText) |
|
249 { |
|
250 return ( ( (aText.Find(KNumberId) != KErrNotFound) || |
|
251 (aText.Find(KDigitId) != KErrNotFound) ) && iType == ENotFormatted); |
|
252 |
|
253 } |
|
254 |
|
255 // End of File |