$darkmode
Libical API Documentation 4.0 STABLE VERSION Visit the v3.0 documentation
icalcluster.c
Go to the documentation of this file.
1 /*======================================================================
2  FILE: icalcluster.c
3  CREATOR: acampi 13 March 2002
4 
5  SPDX-FileCopyrightText: 2002 Andrea Campi <a.campi@inet.it>
6  SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7 ======================================================================*/
8 
14 #ifdef HAVE_CONFIG_H
15 #include <config.h>
16 #endif
17 
18 #include "icalcluster.h"
19 #include "icalclusterimpl.h"
20 #include "icalerror_p.h"
21 
22 #include <stdlib.h>
23 
24 static icalcluster *icalcluster_new_impl(void)
25 {
26  struct icalcluster_impl *cluster;
27 
28  if ((cluster = (struct icalcluster_impl *)malloc(sizeof(struct icalcluster_impl))) == 0) {
30  return 0;
31  }
32 
33  memset(cluster, 0, sizeof(struct icalcluster_impl));
34  strcpy(cluster->id, ICALCLUSTER_ID);
35 
36  return cluster;
37 }
38 
39 icalcluster *icalcluster_new(const char *key, icalcomponent *data)
40 {
41  struct icalcluster_impl *cluster = icalcluster_new_impl();
42 
43  if (!cluster) {
44  return NULL;
45  }
46  assert(cluster->data == 0);
47 
48  cluster->key = strdup(key);
49  cluster->changed = 0;
50  cluster->data = 0;
51 
52  if (data != NULL) {
54  cluster->data = icalcomponent_new(ICAL_XROOT_COMPONENT);
55  icalcomponent_add_component(cluster->data, data);
56  } else {
57  cluster->data = icalcomponent_clone(data);
58  }
59  } else {
60  cluster->data = icalcomponent_new(ICAL_XROOT_COMPONENT);
61  }
62 
63  return cluster;
64 }
65 
70 icalcluster *icalcluster_clone(const icalcluster *old)
71 {
72  struct icalcluster_impl *old_impl = (struct icalcluster_impl *)old;
73  struct icalcluster_impl *cluster = icalcluster_new_impl();
74 
75  if (cluster) {
76  cluster->key = strdup(old_impl->key);
77  cluster->data = icalcomponent_clone(old_impl->data);
78  cluster->changed = 0;
79  }
80 
81  return cluster;
82 }
83 
84 void icalcluster_free(icalcluster *cluster)
85 {
86  if (!cluster) {
87  return;
88  }
89 
90  if (cluster->key != 0) {
91  free(cluster->key);
92  cluster->key = 0;
93  }
94 
95  if (cluster->data != 0) {
96  icalcomponent_free(cluster->data);
97  cluster->data = 0;
98  }
99 
100  free(cluster);
101 }
102 
103 const char *icalcluster_key(const icalcluster *cluster)
104 {
105  icalerror_check_arg_rz((cluster != 0), "cluster");
106 
107  return cluster->key;
108 }
109 
110 int icalcluster_is_changed(const icalcluster *cluster)
111 {
112  icalerror_check_arg_rz((cluster != 0), "cluster");
113 
114  return cluster->changed;
115 }
116 
117 void icalcluster_mark(icalcluster *cluster)
118 {
119  icalerror_check_arg_rv((cluster != 0), "cluster");
120 
121  cluster->changed = 1;
122 }
123 
124 void icalcluster_commit(icalcluster *cluster)
125 {
126  icalerror_check_arg_rv((cluster != 0), "cluster");
127 
128  cluster->changed = 0;
129 }
130 
131 icalcomponent *icalcluster_get_component(const icalcluster *cluster)
132 {
133  icalerror_check_arg_rz((cluster != 0), "cluster");
134 
135  if (icalcomponent_isa(cluster->data) != ICAL_XROOT_COMPONENT) {
136  char *obj;
137 
138  icalerror_warn("The top component is not an XROOT");
139  obj = icalcomponent_as_ical_string_r(cluster->data);
140  fprintf(stderr, "%s\n", obj);
141  free(obj);
142  abort();
143  }
144 
145  return cluster->data;
146 }
147 
148 icalerrorenum icalcluster_add_component(icalcluster *cluster, icalcomponent *child)
149 {
150  icalerror_check_arg_re((cluster != 0), "cluster", ICAL_BADARG_ERROR);
151  icalerror_check_arg_re((child != 0), "child", ICAL_BADARG_ERROR);
152 
153  icalcomponent_add_component(cluster->data, child);
154  icalcluster_mark(cluster);
155 
156  return ICAL_NO_ERROR;
157 }
158 
159 icalerrorenum icalcluster_remove_component(icalcluster *cluster, icalcomponent *child)
160 {
161  icalerror_check_arg_re((cluster != 0), "cluster", ICAL_BADARG_ERROR);
162  icalerror_check_arg_re((child != 0), "child", ICAL_BADARG_ERROR);
163 
164  icalcomponent_remove_component(cluster->data, child);
165  icalcluster_mark(cluster);
166 
167  return ICAL_NO_ERROR;
168 }
169 
170 int icalcluster_count_components(icalcluster *cluster, icalcomponent_kind kind)
171 {
172  icalerror_check_arg_re((cluster != 0), "cluster", ICAL_BADARG_ERROR);
173 
174  return icalcomponent_count_components(cluster->data, kind);
175 }
176 
179 icalcomponent *icalcluster_get_current_component(icalcluster *cluster)
180 {
181  icalerror_check_arg_rz((cluster != 0), "cluster");
182 
183  return icalcomponent_get_current_component(cluster->data);
184 }
185 
186 icalcomponent *icalcluster_get_first_component(icalcluster *cluster)
187 {
188  icalerror_check_arg_rz((cluster != 0), "cluster");
189 
191 }
192 
193 icalcomponent *icalcluster_get_next_component(icalcluster *cluster)
194 {
195  icalerror_check_arg_rz((cluster != 0), "cluster");
196 
198 }
icalcomponent * icalcomponent_get_first_component(icalcomponent *c, icalcomponent_kind kind)
icalcomponent * icalcomponent_get_current_component(icalcomponent *component)
int icalcomponent_count_components(icalcomponent *component, icalcomponent_kind kind)
void icalcomponent_remove_component(icalcomponent *parent, icalcomponent *child)
void icalcomponent_add_component(icalcomponent *parent, icalcomponent *child)
icalcluster * icalcluster_clone(const icalcluster *old)
Deep clone an icalcluster to a new one.
Definition: icalcluster.c:70
icalcomponent * icalcomponent_clone(const icalcomponent *old)
Defines the data structure for calendar clusters.
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition: icalerror.c:90
icalerrorenum
Represents the different types of errors that can be triggered in libical.
Definition: icalerror.h:41
char * icalcomponent_as_ical_string_r(const icalcomponent *component)
icalcomponent * icalcomponent_get_next_component(icalcomponent *c, icalcomponent_kind kind)
icalcluster * icalcluster_new(const char *key, icalcomponent *data)
Create a cluster with a key/value pair.
Definition: icalcluster.c:39
icalcomponent * icalcomponent_new(icalcomponent_kind kind)
icalcomponent_kind
Definition: icalenums.h:28
icalcomponent_kind icalcomponent_isa(const icalcomponent *component)
void icalcomponent_free(icalcomponent *c)
icalcomponent * icalcluster_get_current_component(icalcluster *cluster)
Iterate through components.
Definition: icalcluster.c:179