|
1 /* |
|
2 * Copyright (c) 2002-2004 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: Implements the definition of CICalContentLineWriter.It writes the ICAL content. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // Class include. |
|
21 #include "ICalContentLineWriter.h" // CICalContentLineWriter |
|
22 |
|
23 //debug |
|
24 #include "calendarengines_debug.h" |
|
25 |
|
26 // System includes. |
|
27 #include <s32strm.h> // RWriteStream |
|
28 #include <utf.h> // CnvUtfConverter |
|
29 |
|
30 // Constants. |
|
31 const TInt KICalMaxLineLength = 75; // The length of a line allowed by RFC 2445. |
|
32 const TInt KICalMaxFoldedLineLength = KICalMaxLineLength - 1; // Allows for the extra space at the start of the line. |
|
33 |
|
34 // Line folding literals. |
|
35 _LIT8(KICalCRLF,"\r\n"); |
|
36 _LIT8(KICalFoldLine,"\r\n "); |
|
37 |
|
38 /** |
|
39 Allocates and constructs a new CICalContentLineWriter. |
|
40 @return The newly constructed CICalContentLineWriter |
|
41 @internalTechnology |
|
42 */ |
|
43 CICalContentLineWriter* CICalContentLineWriter::NewL(RWriteStream& aStream) |
|
44 { |
|
45 TRACE_ENTRY_POINT; |
|
46 |
|
47 CICalContentLineWriter* self = CICalContentLineWriter::NewLC(aStream); |
|
48 CleanupStack::Pop(self); |
|
49 |
|
50 TRACE_EXIT_POINT; |
|
51 return self; |
|
52 } |
|
53 |
|
54 /** |
|
55 Allocates and constructs a new CICalContentLineWriter. |
|
56 The pointer to the new object is left on the Cleanup Stack. |
|
57 @return The newly constructed CICalContentLineWriter. |
|
58 @internalTechnology |
|
59 */ |
|
60 CICalContentLineWriter* CICalContentLineWriter::NewLC(RWriteStream& aStream) |
|
61 { |
|
62 TRACE_ENTRY_POINT; |
|
63 |
|
64 CICalContentLineWriter* self = new (ELeave) CICalContentLineWriter(aStream); |
|
65 CleanupStack::PushL(self); |
|
66 self->ConstructL(); |
|
67 |
|
68 TRACE_EXIT_POINT; |
|
69 return self; |
|
70 } |
|
71 |
|
72 /** |
|
73 Destructor. |
|
74 @internalTechnology |
|
75 */ |
|
76 CICalContentLineWriter::~CICalContentLineWriter() |
|
77 { |
|
78 TRACE_ENTRY_POINT; |
|
79 |
|
80 delete iCurrentLine; |
|
81 |
|
82 TRACE_EXIT_POINT; |
|
83 } |
|
84 |
|
85 /** |
|
86 Writes the current content line to the stream after conversion to UTF-8. |
|
87 Performs any necessary folding. |
|
88 @internalTechnology |
|
89 */ |
|
90 void CICalContentLineWriter::WriteContentLineL() |
|
91 { |
|
92 TRACE_ENTRY_POINT; |
|
93 |
|
94 // Convert to UTF-8 for writing |
|
95 HBufC8* tmpLine = CnvUtfConverter::ConvertFromUnicodeToUtf8L(*iCurrentLine); |
|
96 CleanupStack::PushL(tmpLine); |
|
97 |
|
98 TInt pos(0); |
|
99 TInt remaining(tmpLine->Length()); |
|
100 |
|
101 // Fold the line if longer than 75 octets |
|
102 TInt lineLength(KICalMaxLineLength); |
|
103 |
|
104 while (remaining > lineLength) |
|
105 { |
|
106 iWriteStream.WriteL(tmpLine->Mid(pos), lineLength); |
|
107 iWriteStream.WriteL(KICalFoldLine); |
|
108 pos += lineLength; |
|
109 remaining -= lineLength; |
|
110 lineLength = KICalMaxFoldedLineLength; |
|
111 } |
|
112 |
|
113 // Complete the line |
|
114 iWriteStream.WriteL(tmpLine->Mid(pos)); |
|
115 iWriteStream.WriteL(KICalCRLF); |
|
116 |
|
117 CleanupStack::PopAndDestroy(tmpLine); |
|
118 |
|
119 iCurrentLine->Des().SetLength(0); |
|
120 |
|
121 TRACE_EXIT_POINT; |
|
122 } |
|
123 |
|
124 /** |
|
125 Append the contents of aLine to the current content line being formed. |
|
126 iCurrentLine will be reallocated if not enough storage is available |
|
127 @param aLine Descriptor to append. |
|
128 @internalTechnology |
|
129 */ |
|
130 void CICalContentLineWriter::AppendL(const TDesC& aLine) |
|
131 { |
|
132 TRACE_ENTRY_POINT; |
|
133 |
|
134 if (!iCurrentLine) |
|
135 { |
|
136 iCurrentLine = HBufC::NewL(KICalMaxLineLength); |
|
137 } |
|
138 |
|
139 if ((iCurrentLine->Length() + aLine.Length()) > iCurrentLine->Des().MaxLength()) |
|
140 { |
|
141 // Reallocate the buffer |
|
142 iCurrentLine = iCurrentLine->ReAllocL(iCurrentLine->Des().MaxLength() + aLine.Length()); |
|
143 } |
|
144 |
|
145 iCurrentLine->Des().Append(aLine); |
|
146 TRACE_EXIT_POINT; |
|
147 } |
|
148 |
|
149 /** |
|
150 Append a character to the current content line being formed. |
|
151 iCurrentLine will be reallocated if not enough storage is available. |
|
152 @param aCharacter Character to append. |
|
153 @internalTechnology |
|
154 */ |
|
155 void CICalContentLineWriter::AppendL(const TChar& aCharacter) |
|
156 { |
|
157 TRACE_ENTRY_POINT; |
|
158 |
|
159 if (!iCurrentLine) |
|
160 { |
|
161 iCurrentLine = HBufC::NewL(KICalMaxLineLength); |
|
162 } |
|
163 |
|
164 if ((iCurrentLine->Length() + 1) > iCurrentLine->Des().MaxLength()) |
|
165 { |
|
166 // Reallocate the buffer - expand by KICalMaxLineLength rather than '1' |
|
167 // to allow space for further expansion without further reallocation. |
|
168 iCurrentLine = iCurrentLine->ReAllocL(iCurrentLine->Des().MaxLength() + KICalMaxLineLength); |
|
169 } |
|
170 |
|
171 iCurrentLine->Des().Append(aCharacter); |
|
172 |
|
173 TRACE_EXIT_POINT; |
|
174 } |
|
175 |
|
176 /** |
|
177 Default constructor. |
|
178 @internalTechnology |
|
179 @param aStream The stream to write to. |
|
180 @internalTechnology |
|
181 */ |
|
182 CICalContentLineWriter::CICalContentLineWriter(RWriteStream& aStream) |
|
183 : iWriteStream(aStream) |
|
184 { |
|
185 TRACE_ENTRY_POINT; |
|
186 } |
|
187 |
|
188 /** |
|
189 Second phase construction. |
|
190 @internalTechnology |
|
191 */ |
|
192 void CICalContentLineWriter::ConstructL() |
|
193 { |
|
194 TRACE_ENTRY_POINT; |
|
195 iCurrentLine = HBufC::NewL(KICalMaxLineLength); |
|
196 TRACE_EXIT_POINT; |
|
197 } |
|
198 |
|
199 // End of File |