Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / client-api / src / c++ / common / spds / SyncSourceReport.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
37 #include "spds/SyncSourceReport.h"
38 #include "spds/SyncReport.h"
39 #include "spds/ItemReport.h"
40 #include "base/globalsdef.h"
41
42 USE_NAMESPACE
43
44 const char* const SyncSourceReport::targets[] = {
45     CLIENT,
46     SERVER,
47     NULL
48 };
49
50 const char* const SyncSourceReport::commands[] = {
51     COMMAND_ADD,
52     COMMAND_REPLACE,
53     COMMAND_DELETE,
54     NULL
55 };
56
57
58
59 //--------------------------------------------------- Constructor & Destructor
60 SyncSourceReport::SyncSourceReport(const char* name) {
61
62     initialize();
63
64     if (name) {
65         setSourceName(name);
66     }
67
68     clientAddItems = new ArrayList();
69     clientModItems = new ArrayList();
70     clientDelItems = new ArrayList();
71
72     serverAddItems = new ArrayList();
73     serverModItems = new ArrayList();
74     serverDelItems = new ArrayList();
75 }
76
77 SyncSourceReport::SyncSourceReport(SyncSourceReport& ssr) {
78     initialize();
79     assign(ssr);
80 }
81
82 SyncSourceReport::~SyncSourceReport() {
83
84     if (lastErrorMsg) {
85         delete [] lastErrorMsg;
86         lastErrorMsg = NULL;
87     }
88     if (sourceName) {
89         delete [] sourceName;
90         sourceName = NULL;
91     }
92
93     //clientAddItems->clear();
94     //clientModItems->clear();
95     //clientDelItems->clear();
96
97     //serverAddItems->clear();
98     //serverModItems->clear();
99     //serverDelItems->clear();
100     if (clientAddItems){
101         delete clientAddItems;
102         clientAddItems = NULL;
103     }
104     if (clientModItems){
105         delete clientModItems;
106         clientModItems = NULL;
107     }
108     if (clientDelItems){
109         delete clientDelItems;
110         clientDelItems = NULL;
111     }
112     if (serverAddItems){
113         delete serverAddItems;
114         serverAddItems = NULL;
115     }
116     if (serverModItems){
117         delete serverModItems;
118         serverModItems = NULL;
119     }
120     if (serverDelItems){
121         delete serverDelItems;
122         serverDelItems = NULL;
123     }
124 }
125
126
127
128 //------------------------------------------------------------- Public Methods
129
130 const int SyncSourceReport::getLastErrorCode() const {
131     return lastErrorCode;
132 }
133 void SyncSourceReport::setLastErrorCode(const int code) {
134     lastErrorCode = code;
135 }
136
137 const SourceState SyncSourceReport::getState() const {
138     return state;
139 }
140 void SyncSourceReport::setState(const SourceState s) {
141     state = s;
142 }
143
144 const char* SyncSourceReport::getLastErrorMsg() const {
145     return lastErrorMsg;
146 }
147 void SyncSourceReport::setLastErrorMsg(const char* msg) {
148     if (lastErrorMsg) {
149         delete [] lastErrorMsg;
150         lastErrorMsg = NULL;
151     }
152     lastErrorMsg = stringdup(msg);
153 }
154
155 const char* SyncSourceReport::getSourceName() const {
156     return sourceName;
157 }
158 void SyncSourceReport::setSourceName(const char* name) {
159     if (sourceName) {
160         delete [] sourceName;
161         sourceName = NULL;
162     }
163     sourceName = stringdup(name);
164 }
165
166
167 bool SyncSourceReport::checkState() {
168     if (state == SOURCE_ACTIVE) {
169         return true;
170     }
171     return false;
172 }
173
174
175 ItemReport* SyncSourceReport::getItemReport(const char* target, const char* command, int index) {
176
177     ArrayList* list = getList(target, command);
178
179     if (index<0 || index >= list->size()) {
180         return NULL;
181     }
182     return (ItemReport*)list->get(index);
183 }
184
185
186 void SyncSourceReport::addItem(const char* target, const char* command, const WCHAR* ID,
187                                const int status, const WCHAR* statusMessage) {
188
189     // Create the ItemReport element
190     ItemReport element(ID, status, statusMessage);
191
192     // Add element in the corresponding list
193     ArrayList* list = getList(target, command);
194
195
196     // If the element is already present -> no add, only replace status with the new one.
197   /*  ItemReport* ie = NULL;
198     for (int i=0; i<list->size(); i++) {
199         ie = getItemReport(target, command, i);
200         if ( !wcscmp(element.getId(), ie->getId()) ) {
201             ie->setStatus(status);
202             return;
203         }
204     }*/
205
206     // If here, element is new -> add.
207     list->add(element);
208 }
209
210
211 int SyncSourceReport::getItemReportCount(const char* target, const char* command) {
212     ArrayList* list = getList(target, command);
213     return list->size();
214 }
215
216 int SyncSourceReport::getItemReportSuccessfulCount(const char* target, const char* command) {
217
218     ArrayList* list = getList(target, command);
219     ItemReport* e;
220
221     // Scan for succesful codes
222     int good = 0;
223     if (list->size() > 0) {
224         e = (ItemReport*)list->front();
225         if ( isSuccessful(e->getStatus()) ) good++;
226         for (int i=1; i<list->size(); i++) {
227             e = (ItemReport*)list->next();
228             if ( isSuccessful(e->getStatus()) ) good++;
229         }
230     }
231     return good;
232 }
233
234
235 int SyncSourceReport::getItemReportFailedCount(const char* target, const char* command) {
236
237     ArrayList* list = getList(target, command);
238     if (list->size() == 0) {
239         return 0;
240     }
241     int good = getItemReportSuccessfulCount(target, command);
242     return (list->size() - good);
243 }
244
245
246 int SyncSourceReport::getItemReportAlreadyExistCount(const char* target, const char* command) {
247
248     ArrayList* list = getList(target, command);
249     ItemReport* e;
250
251     // Scan for code 418 = ALREADY_EXISTS
252     int found = 0;
253     if (list->size() > 0) {
254         e = (ItemReport*)list->front();
255         if (e->getStatus() == ALREADY_EXISTS) found++;
256         for (int i=1; i<list->size(); i++) {
257             e = (ItemReport*)list->next();
258             if (e->getStatus() == ALREADY_EXISTS) found++;
259         }
260     }
261     return found;
262 }
263
264
265
266 ArrayList* SyncSourceReport::getList(const char* target, const char* command) const {
267
268     ArrayList* ret = NULL;
269
270     if (!strcmp(target, CLIENT)) {
271         if (!strcmp(command, COMMAND_ADD)) {
272             ret = clientAddItems;
273         }
274         else if (!strcmp(command, COMMAND_REPLACE)) {
275             ret = clientModItems;
276         }
277         else if (!strcmp(command, COMMAND_DELETE)) {
278             ret = clientDelItems;
279         }
280         else {
281             // error
282         }
283     }
284     else if (!strcmp(target, SERVER)) {
285         if (!strcmp(command, COMMAND_ADD)) {
286             ret = serverAddItems;
287         }
288         else if (!strcmp(command, COMMAND_REPLACE)) {
289             ret = serverModItems;
290         }
291         else if (!strcmp(command, COMMAND_DELETE)) {
292             ret = serverDelItems;
293         }
294         else {
295             // error
296         }
297     }
298     else {
299         // error
300     }
301
302     return ret;
303 }
304
305
306 //------------------------------------------------------------- Private Methods
307
308 bool SyncSourceReport::isSuccessful(const int status) {
309     if (status >= 200 && status < 500)
310         return true;
311     else
312         return false;
313 }
314
315 void SyncSourceReport::initialize() {
316     lastErrorCode  = ERR_NONE;
317     lastErrorMsg   = NULL;
318     sourceName     = NULL;
319     state          = SOURCE_INACTIVE;
320     clientAddItems = NULL;
321     clientModItems = NULL;
322     clientDelItems = NULL;
323     serverAddItems = NULL;
324     serverModItems = NULL;
325     serverDelItems = NULL;
326 }
327
328 void SyncSourceReport::assign(const SyncSourceReport& ssr) {
329
330     setLastErrorCode(ssr.getLastErrorCode());
331     setLastErrorMsg (ssr.getLastErrorMsg ());
332     setSourceName   (ssr.getSourceName   ());
333     setState        (ssr.getState        ());
334
335     clientAddItems = ssr.getList(CLIENT, COMMAND_ADD)->clone();
336     clientModItems = ssr.getList(CLIENT, COMMAND_REPLACE)->clone();
337     clientDelItems = ssr.getList(CLIENT, COMMAND_DELETE)->clone();
338
339     serverAddItems = ssr.getList(SERVER, COMMAND_ADD)->clone();
340     serverModItems = ssr.getList(SERVER, COMMAND_REPLACE)->clone();
341     serverDelItems = ssr.getList(SERVER, COMMAND_DELETE)->clone();
342 }