2 * Funambol is a mobile platform developed by Funambol, Inc.
3 * Copyright (C) 2003 - 2007 Funambol, Inc.
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.
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
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,
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.
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.
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".
37 #include "spds/SyncSourceReport.h"
38 #include "spds/SyncReport.h"
39 #include "spds/ItemReport.h"
40 #include "base/globalsdef.h"
44 const char* const SyncSourceReport::targets[] = {
50 const char* const SyncSourceReport::commands[] = {
59 //--------------------------------------------------- Constructor & Destructor
60 SyncSourceReport::SyncSourceReport(const char* name) {
68 clientAddItems = new ArrayList();
69 clientModItems = new ArrayList();
70 clientDelItems = new ArrayList();
72 serverAddItems = new ArrayList();
73 serverModItems = new ArrayList();
74 serverDelItems = new ArrayList();
77 SyncSourceReport::SyncSourceReport(SyncSourceReport& ssr) {
82 SyncSourceReport::~SyncSourceReport() {
85 delete [] lastErrorMsg;
93 //clientAddItems->clear();
94 //clientModItems->clear();
95 //clientDelItems->clear();
97 //serverAddItems->clear();
98 //serverModItems->clear();
99 //serverDelItems->clear();
101 delete clientAddItems;
102 clientAddItems = NULL;
105 delete clientModItems;
106 clientModItems = NULL;
109 delete clientDelItems;
110 clientDelItems = NULL;
113 delete serverAddItems;
114 serverAddItems = NULL;
117 delete serverModItems;
118 serverModItems = NULL;
121 delete serverDelItems;
122 serverDelItems = NULL;
128 //------------------------------------------------------------- Public Methods
130 const int SyncSourceReport::getLastErrorCode() const {
131 return lastErrorCode;
133 void SyncSourceReport::setLastErrorCode(const int code) {
134 lastErrorCode = code;
137 const SourceState SyncSourceReport::getState() const {
140 void SyncSourceReport::setState(const SourceState s) {
144 const char* SyncSourceReport::getLastErrorMsg() const {
147 void SyncSourceReport::setLastErrorMsg(const char* msg) {
149 delete [] lastErrorMsg;
152 lastErrorMsg = stringdup(msg);
155 const char* SyncSourceReport::getSourceName() const {
158 void SyncSourceReport::setSourceName(const char* name) {
160 delete [] sourceName;
163 sourceName = stringdup(name);
167 bool SyncSourceReport::checkState() {
168 if (state == SOURCE_ACTIVE) {
175 ItemReport* SyncSourceReport::getItemReport(const char* target, const char* command, int index) {
177 ArrayList* list = getList(target, command);
179 if (index<0 || index >= list->size()) {
182 return (ItemReport*)list->get(index);
186 void SyncSourceReport::addItem(const char* target, const char* command, const WCHAR* ID,
187 const int status, const WCHAR* statusMessage) {
189 // Create the ItemReport element
190 ItemReport element(ID, status, statusMessage);
192 // Add element in the corresponding list
193 ArrayList* list = getList(target, command);
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);
206 // If here, element is new -> add.
211 int SyncSourceReport::getItemReportCount(const char* target, const char* command) {
212 ArrayList* list = getList(target, command);
216 int SyncSourceReport::getItemReportSuccessfulCount(const char* target, const char* command) {
218 ArrayList* list = getList(target, command);
221 // Scan for succesful codes
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++;
235 int SyncSourceReport::getItemReportFailedCount(const char* target, const char* command) {
237 ArrayList* list = getList(target, command);
238 if (list->size() == 0) {
241 int good = getItemReportSuccessfulCount(target, command);
242 return (list->size() - good);
246 int SyncSourceReport::getItemReportAlreadyExistCount(const char* target, const char* command) {
248 ArrayList* list = getList(target, command);
251 // Scan for code 418 = ALREADY_EXISTS
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++;
266 ArrayList* SyncSourceReport::getList(const char* target, const char* command) const {
268 ArrayList* ret = NULL;
270 if (!strcmp(target, CLIENT)) {
271 if (!strcmp(command, COMMAND_ADD)) {
272 ret = clientAddItems;
274 else if (!strcmp(command, COMMAND_REPLACE)) {
275 ret = clientModItems;
277 else if (!strcmp(command, COMMAND_DELETE)) {
278 ret = clientDelItems;
284 else if (!strcmp(target, SERVER)) {
285 if (!strcmp(command, COMMAND_ADD)) {
286 ret = serverAddItems;
288 else if (!strcmp(command, COMMAND_REPLACE)) {
289 ret = serverModItems;
291 else if (!strcmp(command, COMMAND_DELETE)) {
292 ret = serverDelItems;
306 //------------------------------------------------------------- Private Methods
308 bool SyncSourceReport::isSuccessful(const int status) {
309 if (status >= 200 && status < 500)
315 void SyncSourceReport::initialize() {
316 lastErrorCode = ERR_NONE;
319 state = SOURCE_INACTIVE;
320 clientAddItems = NULL;
321 clientModItems = NULL;
322 clientDelItems = NULL;
323 serverAddItems = NULL;
324 serverModItems = NULL;
325 serverDelItems = NULL;
328 void SyncSourceReport::assign(const SyncSourceReport& ssr) {
330 setLastErrorCode(ssr.getLastErrorCode());
331 setLastErrorMsg (ssr.getLastErrorMsg ());
332 setSourceName (ssr.getSourceName ());
333 setState (ssr.getState ());
335 clientAddItems = ssr.getList(CLIENT, COMMAND_ADD)->clone();
336 clientModItems = ssr.getList(CLIENT, COMMAND_REPLACE)->clone();
337 clientDelItems = ssr.getList(CLIENT, COMMAND_DELETE)->clone();
339 serverAddItems = ssr.getList(SERVER, COMMAND_ADD)->clone();
340 serverModItems = ssr.getList(SERVER, COMMAND_REPLACE)->clone();
341 serverDelItems = ssr.getList(SERVER, COMMAND_DELETE)->clone();