agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs
commit c1dcdcd95e6dc9f86520e18e2ff5d83be9de958e
parent 61b12603b64daed3a98eba7af8916189e43ffcef
Author: Marc Coquand <marc@coquand.email>
Date:   Mon, 28 Jul 2025 16:36:58 +0200

Cleanup

Diffstat:
Mical_extra.c | 108+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 56 insertions(+), 52 deletions(-)
diff --git a/ical_extra.c b/ical_extra.c
@@ -6,6 +6,28 @@
 const char *IS_DIRECTORY_PROPERTY = "X-CALDAVFS-ISDIRECTORY";
 const char *FILE_EXTENSION_PROPERTY = "X-CALDAVFS-FILEEXT";
 
+const char *
+icalcomponent_get_uniq_x_value(icalcomponent *component, const char *key)
+{
+	icalcomponent *inner =
+	    icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
+	if (!inner) {
+		return NULL;
+	}
+
+	for (icalproperty *prop =
+		 icalcomponent_get_first_property(inner, ICAL_X_PROPERTY);
+	     prop != NULL;
+	     prop = icalcomponent_get_next_property(inner, ICAL_X_PROPERTY)) {
+
+		const char *prop_name = icalproperty_get_x_name(prop);
+		if (prop_name && strcmp(prop_name, key) == 0) {
+			return icalproperty_get_value_as_string(prop);
+		}
+	}
+	return NULL;
+}
+
 icaltimetype
 get_last_modified(icalcomponent *component)
 {
@@ -69,25 +91,11 @@ is_directory_component(icalcomponent *component)
 			return true;
 		}
 	}
-	for (icalproperty *prop =
-		 icalcomponent_get_first_property(journal, ICAL_X_PROPERTY);
-	     prop != NULL;
-	     prop = icalcomponent_get_next_property(journal, ICAL_X_PROPERTY)) {
-		LOG("FOUND X PROPERTY");
-
-		const char *prop_name = icalproperty_get_x_name(prop);
-		if (prop_name &&
-		    strcmp(prop_name, IS_DIRECTORY_PROPERTY) == 0) {
-			const char *value =
-			    icalproperty_get_value_as_string(prop);
-			LOG("FOUND DIRECTORY");
-			return value && strcmp(value, "YES") == 0;
-		}
-	}
-	return false;
+	const char *is_directory =
+	    icalcomponent_get_uniq_x_value(component, IS_DIRECTORY_PROPERTY);
+	return is_directory && strcmp(is_directory, "YES") == 0;
 }
 
-// Owner: ctx
 icalcomponent *
 parse_ics_file(memory_region *mreg, const char *filename)
 {
@@ -173,16 +181,6 @@ create_vjournal_directory(memory_region *mreg, const char *summary)
 	return calendar;
 }
 
-void
-icalcomponent_mark_as_directory(icalcomponent *ic)
-{
-	icalcomponent *inner = icalcomponent_get_inner(ic);
-	icalproperty *is_directory = icalproperty_new_x(IS_DIRECTORY_PROPERTY);
-	icalproperty_set_x_name(is_directory, IS_DIRECTORY_PROPERTY);
-	icalproperty_set_x(is_directory, "YES");
-	icalcomponent_add_property(inner, is_directory);
-}
-
 icalcomponent *
 create_vjournal_entry(memory_region *mreg, const char *summary)
 {
@@ -292,8 +290,7 @@ icalcomponent_insert_description(memory_region *mreg, icalcomponent *ic,
 }
 
 void
-icalcomponent_set_file_extension(icalcomponent *component,
-				 const char *extension)
+icalcomponent_remove_x_prop(icalcomponent *component, const char *key)
 {
 	icalcomponent *inner =
 	    icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
@@ -305,44 +302,51 @@ icalcomponent_set_file_extension(icalcomponent *component,
 		icalproperty *next_prop =
 		    icalcomponent_get_next_property(inner, ICAL_X_PROPERTY);
 		const char *prop_name = icalproperty_get_x_name(prop_iter);
-		if (prop_name &&
-		    strcmp(prop_name, FILE_EXTENSION_PROPERTY) == 0) {
+		if (prop_name && strcmp(prop_name, key) == 0) {
 			icalcomponent_remove_property(inner, prop_iter);
 			icalproperty_free(prop_iter);
 			break;
 		}
 		prop_iter = next_prop;
 	}
+}
 
-	icalproperty *fileext_prop =
-	    icalproperty_new_x(FILE_EXTENSION_PROPERTY);
+void
+icalcomponent_set_unique_x_value(icalcomponent *component, const char *key,
+				 const char *value)
+{
+	icalcomponent_remove_x_prop(component, key);
+	icalcomponent *inner =
+	    icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
+	assert(inner);
 
-	icalproperty_set_x_name(fileext_prop, FILE_EXTENSION_PROPERTY);
-	icalproperty_set_x(fileext_prop, extension);
+	icalproperty *fileext_prop = icalproperty_new_x(key);
+
+	icalproperty_set_x_name(fileext_prop, key);
+	icalproperty_set_x(fileext_prop, value);
 
 	icalcomponent_add_property(inner, fileext_prop);
+}
+
+void
+icalcomponent_set_file_extension(icalcomponent *component,
+				 const char *extension)
+{
+
+	icalcomponent_set_unique_x_value(component, FILE_EXTENSION_PROPERTY,
+					 extension);
 	LOG("Set X-CALDAVFS-FILEEXT to '%s' on component.", extension);
 }
 
 const char *
 icalcomponent_get_file_extension(icalcomponent *component)
 {
-	icalcomponent *inner =
-	    icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
-	if (!inner) {
-		return NULL;
-	}
-
-	for (icalproperty *prop =
-		 icalcomponent_get_first_property(inner, ICAL_X_PROPERTY);
-	     prop != NULL;
-	     prop = icalcomponent_get_next_property(inner, ICAL_X_PROPERTY)) {
+	return icalcomponent_get_uniq_x_value(component,
+					      FILE_EXTENSION_PROPERTY);
+}
 
-		const char *prop_name = icalproperty_get_x_name(prop);
-		if (prop_name &&
-		    strcmp(prop_name, FILE_EXTENSION_PROPERTY) == 0) {
-			return icalproperty_get_value_as_string(prop);
-		}
-	}
-	return NULL;
+void
+icalcomponent_mark_as_directory(icalcomponent *ic)
+{
+	icalcomponent_set_unique_x_value(ic, IS_DIRECTORY_PROPERTY, "YES");
 }