upload tizen1.0 source
[pkgs/o/oma-ds-service.git] / Common / Common_Define.c
1 /*
2  * oma-ds-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JuHak Park <juhaki.park@samsung.com>,
7  *          JuneHyuk Lee <junhyuk7.lee@samsung.com>,
8  *          SunBong Ha <sunbong.ha@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24
25
26
27 /*
28  * For any sort of issue you concern as to this software,
29  * you may use following point of contact.
30  * All resources contributed on this software
31  * are orinigally written by S-Core Inc., a member of Samsung Group.
32  *
33  * SeongWon Shim <seongwon.shim@samsung.com>
34  */
35
36 /**
37  *   @Common_Define_Internal.c
38  *   @version                                                                   0.1
39  *   @brief                                                                             This file is the source file of implementation of defined Common structure
40  */
41
42 #include "agent-framework/Utility/fw_log.h"
43 #include "Common/Common_Define.h"
44
45 #define LOG_TAG "OMA_DS_COMMON"
46
47 Datastore *datastoreinfo_per_content_type[4];
48 AlertType syncType;
49
50 DatastoreInfo *create_datastoreinfo(char *target, char *source)
51 {
52         FW_LOGV("start");
53
54         DatastoreInfo *datastoreInfo = (DatastoreInfo *)calloc(1, sizeof(DatastoreInfo));
55         if (datastoreInfo == NULL) {
56                 FW_LOGV("datastoreInfo is NULL");
57                 return NULL;
58         }
59
60         if (target != NULL)
61                 datastoreInfo->target = strdup(target);
62         if (source != NULL)
63                 datastoreInfo->source = strdup(source);
64
65         FW_LOGV("end");
66
67         return datastoreInfo;
68 }
69
70 void free_datastoreinfos(GList *pDatastoreInfos)
71 {
72         FW_LOGV("start");
73
74         if (!pDatastoreInfos) {
75                 FW_LOGV("List is null");
76                 return;
77         }
78
79         GList *iter = NULL;
80         for (iter = pDatastoreInfos; iter != NULL; iter = g_list_next(iter))
81                 free_datastoreinfo(iter->data);
82
83         g_list_free(pDatastoreInfos);
84
85         FW_LOGV("end");
86 }
87
88 void free_datastoreinfo(DatastoreInfo *pDatastoreInfo)
89 {
90         FW_LOGV("start");
91
92         if (!pDatastoreInfo) {
93                 FW_LOGV("pDatastoreInfo is NULL");
94                 return;
95         }
96
97         if (pDatastoreInfo->target) {
98                 free(pDatastoreInfo->target);
99                 pDatastoreInfo->target = NULL;
100         }
101
102         if (pDatastoreInfo->source) {
103                 free(pDatastoreInfo->source);
104                 pDatastoreInfo->source = NULL;
105         }
106
107         if (pDatastoreInfo->lastAnchor) {
108                 free(pDatastoreInfo->lastAnchor);
109                 pDatastoreInfo->lastAnchor = NULL;
110         }
111
112         if (pDatastoreInfo->nextAnchor) {
113                 free(pDatastoreInfo->nextAnchor);
114                 pDatastoreInfo->nextAnchor = NULL;
115         }
116
117         free(pDatastoreInfo);
118
119         FW_LOGV("end");
120 }
121
122 void set_datastoreInfo_synctype(DatastoreInfo *datastoreInfo, AlertType syncType)
123 {
124         if (0 < syncType)
125                 datastoreInfo->syncType = syncType;
126 }
127
128 void set_datastoreinfo_lastanchor(DatastoreInfo *datastoreInfo, char *lastAnchor)
129 {
130         if (lastAnchor)
131                 datastoreInfo->lastAnchor = strdup(lastAnchor);
132 }
133
134 void set_datastoreinfo_nextanchor(DatastoreInfo *datastoreInfo, char *nextAnchor)
135 {
136         if (nextAnchor)
137                 datastoreInfo->nextAnchor = strdup(nextAnchor);
138 }
139
140 void set_datastoreinfo_maxobjsize(DatastoreInfo *datastoreInfo, unsigned int maxObjSize)
141 {
142         if (maxObjSize)
143                 datastoreInfo->maxObjSize = maxObjSize;
144 }
145
146 ChangedItem *create_changeditem(ChangeType type, char *luid)
147 {
148         FW_LOGV("start");
149         ChangedItem *changedItem = (ChangedItem *)calloc(1, sizeof(ChangedItem));
150         if (changedItem == NULL) {
151                 FW_LOGV("changedItem is NULL");
152                 return NULL;
153         }
154
155         changedItem->changeType = type;
156
157         if (luid != NULL)
158                 changedItem->luid = strdup(luid);
159
160         changedItem->content_type = NULL;
161
162         FW_LOGV("end");
163
164         return changedItem;
165 }
166
167 void set_changeditem_contenttype(ChangedItem *pChangedItem, char *content_type)
168 {
169         FW_LOGV("start content_type = %s", content_type);
170
171         if (!pChangedItem) {
172                 FW_LOGV("pChangedItem is NULL");
173                 return;
174         }
175
176         if (content_type != NULL)
177                 pChangedItem->content_type = strdup(content_type);
178 }
179
180 void free_changeditem(ChangedItem *pChangedItem)
181 {
182         FW_LOGV("start");
183
184         if (!pChangedItem) {
185                 FW_LOGV("pChangedItem is NULL");
186                 return;
187         }
188
189         if (pChangedItem->luid) {
190                 free(pChangedItem->luid);
191                 pChangedItem->luid = NULL;
192         }
193
194         if (pChangedItem->content_type) {
195                 free(pChangedItem->content_type);
196                 pChangedItem->content_type = NULL;
197         }
198
199         if (pChangedItem->data) {
200                 free(pChangedItem->data);
201                 pChangedItem->data = NULL;
202         }
203
204         if (pChangedItem)
205                 free(pChangedItem);
206
207         FW_LOGV("end");
208 }
209
210 void set_changeditem_data(ChangedItem *changedItem, char *data)
211 {
212         if (!changedItem) {
213                 FW_LOGV("changedItem is NULL");
214                 return;
215         }
216
217         if (data != NULL)
218                 changedItem->data = strdup(data);
219 }
220
221 void set_changedItem_indexofdatastore(ChangedItem *changedItem, unsigned int indexOfDatastore)
222 {
223         if (!changedItem) {
224                 FW_LOGV("changedItem is NULL");
225                 return;
226         }
227
228         changedItem->indexOfDatastore = indexOfDatastore;
229 }
230
231 ChangedDatastore *create_changeddatastore(char *source, char *target, int hasNumberOfChanges, unsigned int numberOfChanges)
232 {
233         FW_LOGV("start");
234
235         ChangedDatastore *changedDatastore = (ChangedDatastore *)calloc(1, sizeof(ChangedDatastore));
236
237         if (changedDatastore == NULL) {
238                 FW_LOGV("changedDatastore is NULL");
239                 return NULL;
240         }
241
242         if (source != NULL)
243                 changedDatastore->source = strdup(source);
244         if (target != NULL)
245                 changedDatastore->target = strdup(target);
246
247         changedDatastore->needSyncCommand = 1;
248         changedDatastore->numberOfChanges = numberOfChanges;
249         changedDatastore->hasNumberOfChanges = hasNumberOfChanges;
250
251         FW_LOGV("end");
252
253         return changedDatastore;
254 }
255
256 void free_changeddatastores(GList *pChangedDatastores)
257 {
258         FW_LOGV("start");
259
260         if (!pChangedDatastores) {
261                 FW_LOGV("List is NULL");
262                 return;
263         }
264
265         GList *iter = NULL;
266         for (iter = pChangedDatastores; iter != NULL; iter = g_list_next(iter))
267                 free_changeddatastore(iter->data);
268
269         g_list_free(pChangedDatastores);
270
271         FW_LOGV("end");
272 }
273
274 void free_changeddatastore(ChangedDatastore *pChangedDatastore)
275 {
276         FW_LOGV("pChangedDatastore = %p", pChangedDatastore);
277         FW_LOGV("start");
278
279         if (!pChangedDatastore) {
280                 FW_LOGV("pChangedDatastore is NULL");
281                 return;
282         }
283
284         if (pChangedDatastore->source) {
285                 free(pChangedDatastore->source);
286                 pChangedDatastore->source = NULL;
287         }
288
289         if (pChangedDatastore->target) {
290                 free(pChangedDatastore->target);
291                 pChangedDatastore->target = NULL;
292         }
293
294         GList *iter = NULL;
295         for (iter = pChangedDatastore->changeItem; iter != NULL; iter = g_list_next(iter))
296                 free_changeditem(iter->data);
297
298         g_list_free(pChangedDatastore->changeItem);
299         pChangedDatastore->changeItem = NULL;
300
301         for (iter = pChangedDatastore->sentItem; iter != NULL; iter = g_list_next(iter))
302                 free_changeditem(iter->data);
303
304         g_list_free(pChangedDatastore->sentItem);
305         pChangedDatastore->sentItem = NULL;
306
307         if (pChangedDatastore)
308                 free(pChangedDatastore);
309
310         FW_LOGV("end");
311 }
312
313 void set_changeddatastore_changeditem(ChangedDatastore *changedDatastore, GList *changeItem)
314 {
315         if (changedDatastore)
316                 changedDatastore->changeItem = changeItem;
317 }
318
319 void add_changeddatastore_changeditem(ChangedDatastore *changedDatastore, ChangedItem *changedItem)
320 {
321         if (!changedDatastore) {
322                 FW_LOGV("changedDatastore is NULL");
323                 return;
324         }
325
326         changedDatastore->changeItem = g_list_append(changedDatastore->changeItem, changedItem);
327 }
328
329 AppliedStatus *create_appliedstatus(char *luid, ChangeType changeType, int status)
330 {
331         FW_LOGV("start");
332
333         AppliedStatus *appliedStatus = (AppliedStatus *)calloc(1, sizeof(AppliedStatus));
334         if (appliedStatus == NULL) {
335                 FW_LOGV("appliedStatus is NULL");
336                 return NULL;
337         }
338
339         if (luid != NULL)
340                 appliedStatus->luid = strdup(luid);
341
342         appliedStatus->changeType = changeType;
343         appliedStatus->status = status;
344
345         FW_LOGV("end");
346
347         return appliedStatus;
348 }
349
350 void free_appliedstatuses(GList *pAppliedStatuses)
351 {
352         FW_LOGV("start");
353
354         if (!pAppliedStatuses) {
355                 FW_LOGV("List is NULL");
356                 return;
357         }
358
359         GList *iter = NULL;
360         for (iter = pAppliedStatuses; iter != NULL; iter = g_list_next(iter))
361                 free_appliedstatus(iter->data);
362
363         g_list_free(pAppliedStatuses);
364
365         FW_LOGV("start");
366 }
367
368 void free_appliedstatus(AppliedStatus *pAppliedStatus)
369 {
370         FW_LOGV("start");
371
372         if (!pAppliedStatus) {
373                 FW_LOGV("pAppliedStatus is NULL");
374                 return;
375         }
376
377         if (pAppliedStatus->luid) {
378                 free(pAppliedStatus->luid);
379                 pAppliedStatus->luid = NULL;
380         }
381
382         free(pAppliedStatus);
383
384         FW_LOGV("end");
385 }
386
387 SendingStatus *create_sendingstatus(char *source, char *target)
388 {
389         FW_LOGV("start");
390
391         SendingStatus *sendingStatus = (SendingStatus *)calloc(1, sizeof(SendingStatus));
392         if (sendingStatus == NULL) {
393                 FW_LOGV("sendingStatus is NULL");
394                 return NULL;
395         }
396
397         if (source != NULL)
398                 sendingStatus->source = strdup(source);
399
400         if (target != NULL)
401                 sendingStatus->target = strdup(target);
402
403         FW_LOGV("end");
404
405         return sendingStatus;
406 }
407
408 void free_sendingstatuses(GList *pSendingStatuses)
409 {
410         FW_LOGV("start");
411
412         if (!pSendingStatuses) {
413                 FW_LOGV("List is NULL");
414                 return;
415         }
416
417         GList *iter = NULL;
418         for (iter = pSendingStatuses; iter != NULL; iter = g_list_next(iter))
419                 free_sendingstatus(iter->data);
420
421         g_list_free(pSendingStatuses);
422
423         FW_LOGV("end");
424 }
425
426 void free_sendingstatus(SendingStatus *pSendingStatus)
427 {
428         FW_LOGV("start");
429
430         if (!pSendingStatus) {
431                 FW_LOGV("pAppliedStatus is NULL");
432                 return;
433         }
434
435         if (pSendingStatus->source) {
436                 free(pSendingStatus->source);
437                 pSendingStatus->source = NULL;
438         }
439
440         if (pSendingStatus->target) {
441                 free(pSendingStatus->target);
442                 pSendingStatus->target = NULL;
443         }
444
445         GList *iter = NULL;
446         for (iter = pSendingStatus->items; iter != NULL; iter = g_list_next(iter))
447                 free_appliedstatus(iter->data);
448
449         g_list_free(pSendingStatus->items);
450
451         free(pSendingStatus);
452
453         FW_LOGV("end");
454 }
455 void add_sendingstatus_appliedstatus(SendingStatus *sendingStatus, AppliedStatus *appliedStatus)
456 {
457         if (!sendingStatus) {
458                 FW_LOGV("sendingStatus is NULL");
459                 return;
460         }
461
462         sendingStatus->items = g_list_append(sendingStatus->items, appliedStatus);
463 }
464
465 Datastore *create_datastore(char *target, char *source)
466 {
467         FW_LOGV("start");
468
469         Datastore *datastore = (Datastore *)calloc(1, sizeof(Datastore));
470         if (datastore == NULL) {
471                 FW_LOGV("datastore is NULL");
472                 return NULL;
473         }
474
475         if (target)
476                 datastore->target = strdup(target);
477
478         if (source)
479                 datastore->source = strdup(source);
480
481         FW_LOGV("end");
482
483         return datastore;
484 }
485
486 void set_datastore_contenttype_info(Datastore *datastore, int datastore_id, int folder_type_id)
487 {
488         if (datastore_id)
489                 datastore->datastore_id = datastore_id;
490         if (folder_type_id)
491                 datastore->folder_type_id = folder_type_id;
492 }
493
494 void set_datastore_account_info(Datastore *datastore, char *account_id, char *account_pw)
495 {
496         if (account_id)
497                 datastore->id = strdup(account_id);
498         if (account_pw)
499                 datastore->pw = strdup(account_pw);
500 }
501
502 void set_datastore_client_sync_type(Datastore *datastore, AlertType syncType)
503 {
504         if (syncType)
505                 datastore->clientSyncType = syncType ;
506 }
507
508 void set_datastore_server_sync_type(Datastore *datastore, AlertType syncType)
509 {
510         if (syncType)
511                 datastore->serverSyncType = syncType ;
512 }
513
514 void set_datastore_client_anchor(Datastore *datastore, char *lastAnchor, char *nextAnchor)
515 {
516         if (lastAnchor)
517                 datastore->lastAnchorClient = strdup(lastAnchor);
518         if (nextAnchor)
519                 datastore->nextAnchorClient = strdup(nextAnchor);
520 }
521
522 void set_datastore_server_anchor(Datastore *datastore, char *lastAnchor, char *nextAnchor)
523 {
524         if (lastAnchor)
525                 datastore->lastAnchorServer = strdup(lastAnchor);
526         if (nextAnchor)
527                 datastore->nextAnchorServer = strdup(nextAnchor);
528 }
529
530 SyncResult *create_sync_result()
531 {
532         FW_LOGV("start");
533
534         SyncResult *syncResult = calloc(1, sizeof(SyncResult));
535         if (syncResult == NULL) {
536                 FW_LOGV("syncResult is NULL");
537                 return NULL;
538         }
539
540         FW_LOGV("end");
541
542         return syncResult;
543 }
544
545 SyncResult *dup_syncresult(SyncResult *orgSyncResult)
546 {
547         FW_LOGV("start");
548
549         SyncResult *syncResult = calloc(1, sizeof(SyncResult));
550         if (syncResult == NULL) {
551                 FW_LOGV("syncResult is NULL");
552                 return NULL;
553         }
554
555         syncResult->sessionResult = orgSyncResult->sessionResult;
556         syncResult->numberOfChange = orgSyncResult->numberOfChange;
557         syncResult->received_count = orgSyncResult->received_count;
558         syncResult->add_count = orgSyncResult->add_count;
559         syncResult->replace_count = orgSyncResult->replace_count;
560         syncResult->delete_count = orgSyncResult->delete_count;
561
562         FW_LOGV("end");
563
564         return syncResult;
565
566 }
567
568 void set_numberofchange(SyncResult *syncResult, unsigned int numberOfChange)
569 {
570         if (syncResult)
571                 syncResult->numberOfChange = numberOfChange;
572 }
573
574 void add_add_count(SyncResult *syncResult, unsigned int add_cnt)
575 {
576         if (syncResult)
577                 syncResult->add_count += add_cnt;
578 }
579
580 void add_receive_count(SyncResult *syncResult, unsigned int received_count)
581 {
582         if (syncResult)
583                 syncResult->received_count += received_count;
584 }
585
586 void add_replace_count(SyncResult *syncResult, unsigned int replace_cnt)
587 {
588         if (syncResult)
589                 syncResult->replace_count += replace_cnt;
590 }
591
592 void add_delete_count(SyncResult *syncResult, unsigned int delete_cnt)
593 {
594         if (syncResult)
595                 syncResult->delete_count += delete_cnt;
596 }
597
598 void free_presyncreturnobj(PreSyncReturnObj *pPreSyncReturnObj)
599 {
600         FW_LOGV("start");
601
602         if (pPreSyncReturnObj == NULL) {
603                 FW_LOGV("pPreSyncReturnObj is NULL");
604                 return;
605         }
606
607         GList *iter = NULL;
608         for (iter = pPreSyncReturnObj->datastoreInfo; iter != NULL; iter = g_list_next(iter))
609                 free_datastoreinfo(iter->data);
610         g_list_free(pPreSyncReturnObj->datastoreInfo);
611         pPreSyncReturnObj->datastoreInfo = NULL;
612
613         if (pPreSyncReturnObj->devID != NULL) {
614                 free(pPreSyncReturnObj->devID);
615                 pPreSyncReturnObj->devID = NULL;
616         }
617
618         free(pPreSyncReturnObj);
619         pPreSyncReturnObj = NULL;
620
621         FW_LOGV("end");
622 }
623
624 void free_syncobj(SyncObj *pSyncObj)
625 {
626         FW_LOGV("start");
627
628         if (pSyncObj == NULL) {
629                 FW_LOGV("pSyncObj is NULL");
630                 return;
631         }
632
633         GList *iter = NULL;
634         for (iter = pSyncObj->changedDatastore; iter != NULL; iter = g_list_next(iter))
635                 free_changeddatastore(iter->data);
636         g_list_free(pSyncObj->changedDatastore);
637         pSyncObj->changedDatastore = NULL;
638
639         for (iter = pSyncObj->sendingStatus; iter != NULL; iter = g_list_next(iter))
640                 free_sendingstatus(iter->data);
641         g_list_free(pSyncObj->sendingStatus);
642         pSyncObj->sendingStatus = NULL;
643
644         free(pSyncObj);
645         pSyncObj = NULL;
646
647         FW_LOGV("end");
648 }
649
650 void free_syncreturnobj(SyncReturnObj *pSyncReturnObj)
651 {
652         FW_LOGV("start");
653
654         if (pSyncReturnObj == NULL) {
655                 FW_LOGV("pSyncReturnObj is NULL");
656                 return;
657         }
658
659         GList *iter = NULL;
660         for (iter = pSyncReturnObj->changedDatastore; iter != NULL; iter = g_list_next(iter))
661                 free_changeddatastore(iter->data);
662         g_list_free(pSyncReturnObj->changedDatastore);
663         pSyncReturnObj->changedDatastore = NULL;
664
665         for (iter = pSyncReturnObj->status; iter != NULL; iter = g_list_next(iter))
666                 free_appliedstatus(iter->data);
667         g_list_free(pSyncReturnObj->status);
668         pSyncReturnObj->status = NULL;
669
670         free(pSyncReturnObj);
671         pSyncReturnObj = NULL;
672
673         FW_LOGV("end");
674 }
675
676 void free_datastore(Datastore *pDatastore)
677 {
678         FW_LOGV("start");
679
680         if (pDatastore == NULL)
681                 return;
682
683         if (pDatastore->target != NULL) {
684                 free(pDatastore->target);
685                 pDatastore->target = NULL;
686         }
687
688         if (pDatastore->source != NULL) {
689                 free(pDatastore->source);
690                 pDatastore->source = NULL;
691         }
692
693         if (pDatastore->id != NULL) {
694                 free(pDatastore->id);
695                 pDatastore->id = NULL;
696         }
697
698         if (pDatastore->pw != NULL) {
699                 free(pDatastore->pw);
700                 pDatastore->pw = NULL;
701         }
702
703         if (pDatastore->lastAnchorClient != NULL) {
704                 free(pDatastore->lastAnchorClient);
705                 pDatastore->lastAnchorClient = NULL;
706         }
707
708         if (pDatastore->nextAnchorClient != NULL) {
709                 free(pDatastore->nextAnchorClient);
710                 pDatastore->nextAnchorClient = NULL;
711         }
712
713         if (pDatastore->lastAnchorServer != NULL) {
714                 free(pDatastore->lastAnchorServer);
715                 pDatastore->lastAnchorServer = NULL;
716         }
717
718         if (pDatastore->nextAnchorServer != NULL) {
719                 free(pDatastore->nextAnchorServer);
720                 pDatastore->nextAnchorServer = NULL;
721         }
722
723         if (pDatastore->clientSyncResult != NULL)
724                 free(pDatastore->clientSyncResult);
725
726         if (pDatastore->serverSyncResult != NULL)
727                 free(pDatastore->serverSyncResult);
728
729         free(pDatastore);
730
731         FW_LOGV("end");
732 }