Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / client-api / src / c++ / windows / vocl / WinNote.cpp
1 /*
2  * Funambol is a mobile platform developed by Funambol, Inc. 
3  * Copyright (C) 2003 - 2007 Funambol, Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by
7  * the Free Software Foundation with the addition of the following permission 
8  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
9  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
10  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License 
18  * along with this program; if not, see http://www.gnu.org/licenses or write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301 USA.
21  * 
22  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
23  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License
30  * version 3, these Appropriate Legal Notices must retain the display of the
31  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
32  * feasible for technical reasons, the Appropriate Legal Notices must display
33  * the words "Powered by Funambol".
34  */
35
36 #include "base/util/utils.h"
37 #include "vocl/WinNote.h"
38 #include "vocl/VConverter.h"
39 #include "base/stringUtils.h"
40 #include "base/globalsdef.h"
41
42 USE_NAMESPACE
43
44 using namespace std;
45
46
47 /**
48  * Following are the possible values for WinNote property "X-FUNAMBOL-COLOR".
49  * @see enum WinNoteColor
50  */
51 static WCHAR* colorName[] = 
52 {
53         {L"BLUE"   },
54         {L"GREEN"  },
55         {L"PINK"   },
56         {L"YELLOW" },
57         {L"WHITE"  },
58     {NULL}
59 };
60
61
62 // Constructor
63 WinNote::WinNote() {
64     vNote = L"";
65 }
66
67 // Constructor: fills propertyMap parsing the passed data
68 WinNote::WinNote(const wstring dataString) {
69     vNote = L"";
70     parse(dataString);
71 }
72
73 // Destructor
74 WinNote::~WinNote() {
75 }
76
77
78
79
80 // Format and return a vNote string from the propertyMap.
81 wstring& WinNote::toString() {
82
83     vNote = L"";
84
85     //
86     // Conversion: WinNote -> vObject.
87     // -------------------------------
88     //
89     VObject* vo = new VObject();
90     VProperty* vp  = NULL;
91     wstring element;
92
93     vp = new VProperty(TEXT("BEGIN"), TEXT("VNOTE"));
94     vo->addProperty(vp);
95     delete vp; vp = NULL;
96
97     vp = new VProperty(TEXT("VERSION"), VNOTE_VERSION);
98     vo->addProperty(vp);
99     delete vp; vp = NULL;
100
101
102     // Folder path.
103     if (getProperty(L"Folder", element)) {
104         vp = new VProperty(L"X-FUNAMBOL-FOLDER");
105         vp->addValue(element.c_str());
106         vo->addProperty(vp);
107         delete vp; vp = NULL;
108     }
109
110     if (getProperty(L"Subject", element)) {
111         vp = new VProperty(TEXT("SUMMARY"), element.c_str());
112         vo->addProperty(vp);
113         delete vp; vp = NULL;
114     }
115     if (getProperty(L"Body", element)) {
116         vp = new VProperty(TEXT("BODY"), element.c_str());
117         vo->addProperty(vp);
118         delete vp; vp = NULL;
119     }
120     if (getProperty(L"Categories", element)) {
121         vp = new VProperty(TEXT("CATEGORIES"), element.c_str());
122         vo->addProperty(vp);
123         delete vp; vp = NULL;
124     }
125
126
127     //
128     // ---- Other Funambol defined properties ----
129     // Support for other fields that don't have a
130     // specific correspondence in vCalendar.
131     if (getProperty(L"Color", element)) {
132         long color = _wtoi(element.c_str());
133         vp = new VProperty(TEXT("X-FUNAMBOL-COLOR"));
134
135         if (color < 0 || color >= NUM_NOTE_COLOR) {
136             color = 1;  // Default = yellow.
137         }
138         vp->addValue(colorName[color]);
139
140         vo->addProperty(vp);
141         delete vp; vp = NULL;
142     }
143
144
145     bool found = false;
146     vp = new VProperty(TEXT("X-FUNAMBOL-POSITION"));
147     if (getProperty(L"Top", element)) {
148         found = true;
149         vp->addValue(element.c_str());
150     }
151     if (getProperty(L"Left", element)) {
152         found = true;
153         vp->addValue(element.c_str());
154     }
155     if (getProperty(L"Height", element)) {
156         found = true;
157         vp->addValue(element.c_str());
158     }
159     if (getProperty(L"Width", element)) {
160         found = true;
161         vp->addValue(element.c_str());
162     }
163     if (found) {
164         vo->addProperty(vp);
165     }
166     delete vp; vp = NULL;
167
168
169     vp = new VProperty(TEXT("END"), TEXT("VNOTE"));
170     vo->addProperty(vp);
171     delete vp; vp = NULL;
172
173
174     //
175     // Format the vNote.
176     // -----------------
177     //
178     WCHAR* tmp = vo->toString();
179     if (tmp) {
180         vNote = tmp;
181         delete [] tmp;
182     }
183     return vNote;
184 }
185
186
187
188
189 //
190 // Parse a vNote string and fills the propertyMap.
191 //
192 int WinNote::parse(const wstring dataString) {
193
194     WCHAR* element = NULL;
195
196     //
197     // Parse the vNote and fill the VObject.
198     // -----------------------------------------
199     //
200     VObject* vo = VConverter::parse(dataString.c_str());
201     if (!vo) {
202         //sprintf(lastErrorMsg, ERR_ITEM_VOBJ_PARSE);
203         setError(1, ERR_ITEM_VOBJ_PARSE);
204         LOG.error(getLastErrorMsg());
205         return 1;
206     }
207     // Check if VObject type and version are the correct ones.
208     if (!checkVNoteTypeAndVersion(vo)) {
209         if (vo) delete vo;
210         return 1;
211     }
212
213
214     //
215     // Conversion: vObject -> WinNote.
216     // -------------------------------
217     // Note: properties found are added to the propertyMap, so that the 
218     //       map will contain only parsed properties after this process.
219     //
220     if (element = getVObjectPropertyValue(vo, L"SUMMARY")) {
221         setProperty(L"Subject", element);
222     }
223     if (element = getVObjectPropertyValue(vo, L"BODY")) {
224         setProperty(L"Body", element);
225     }
226     if (element = getVObjectPropertyValue(vo, L"CATEGORIES")) {
227         setProperty(L"Categories", element);
228     }
229     if (element = getVObjectPropertyValue(vo, L"X-FUNAMBOL-FOLDER")) {
230         setProperty(L"Folder", element);
231     }
232
233
234     //
235     // ---- Other Funambol defined properties ----
236     // Support for other fields that don't have a
237     // specific correspondence in vNote.
238     if (element = getVObjectPropertyValue(vo, L"X-FUNAMBOL-COLOR")) {
239         WCHAR tmp[10];
240         int i=0;
241         for (i=0; i<NUM_NOTE_COLOR; i++) {
242             if (!wcscmp(colorName[i], element)) { break; }
243         }
244         if (i == NUM_NOTE_COLOR) { 
245             i = 1;      // Default = yellow
246         }
247         wsprintf(tmp, TEXT("%i"), i);
248         setProperty(L"Color", tmp);
249     }
250
251     if (element = getVObjectPropertyValue(vo, L"X-FUNAMBOL-POSITION")) {
252         VProperty* vp = vo->getProperty(TEXT("X-FUNAMBOL-POSITION"));
253         element = vp->getPropComponent(1);  
254         if (element && wcslen(element)>0)  { setProperty(L"Top", element);    }
255         element = vp->getPropComponent(2);  
256         if (element && wcslen(element)>0)  { setProperty(L"Left", element);   }
257         element = vp->getPropComponent(3);  
258         if (element && wcslen(element)>0)  { setProperty(L"Height", element); }
259         element = vp->getPropComponent(4);  
260         if (element && wcslen(element)>0)  { setProperty(L"Width", element);  }
261     }
262
263     return 0;
264 }
265
266
267 // Utility to check the productID and version of VObject passed.
268 bool WinNote::checkVNoteTypeAndVersion(VObject* vo) {
269
270     WCHAR* prodID  = vo->getProdID();
271     WCHAR* version = vo->getVersion();
272     
273     if (!prodID) {
274         LOG.error(ERR_ITEM_VOBJ_TYPE_NOTFOUND, L"VNOTE");
275         return false;
276     }
277     if (wcscmp(prodID, L"VNOTE")) {
278         LOG.error(ERR_ITEM_VOBJ_WRONG_TYPE, prodID, L"VNOTE");
279         return false;
280     }
281
282     if (!version) {
283         // Just log a warning...
284         LOG.info(INFO_ITEM_VOBJ_VERSION_NOTFOUND, VNOTE_VERSION);
285     }
286     else if (wcscmp(version, VNOTE_VERSION)) {
287         // Just log a warning...
288         LOG.info(INFO_ITEM_VOBJ_WRONG_VERSION, version, VNOTE_VERSION);
289     }
290     return true;
291 }
292