From: Olaf Wintermann Date: Sun, 12 Jan 2025 20:28:18 +0000 (+0100) Subject: add parser for xdg user-dirs.dirs X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=fbc53511422e792708e70f6884a4c7b1b2821e97;p=mizunara.git add parser for xdg user-dirs.dirs --- diff --git a/mizunara/Makefile b/mizunara/Makefile index 8fa6d75..ac23d64 100644 --- a/mizunara/Makefile +++ b/mizunara/Makefile @@ -37,6 +37,7 @@ SRC += menu.c SRC += window.c SRC += filebrowser.c SRC += gtk-filesview.c +SRC += bookmarks.c SRC += $(MZUI) @@ -45,7 +46,7 @@ OBJ = $(SRC:%.c=$(BUILD_ROOT)/build/mizunara/%.$(OBJ_EXT)) all: $(BUILD_ROOT)/build/bin/mizunara $(BUILD_ROOT)/build/bin/mizunara: $(OBJ) $(BUILD_ROOT)/build/lib/libuitk.a - $(CC) -o $(BUILD_ROOT)/build/bin/mizunara$(APP_EXT) $(OBJ) -L$(BUILD_ROOT)/build/lib -luitk -lucx $(LDFLAGS) $(TK_LDFLAGS) + $(CC) -o $(BUILD_ROOT)/build/bin/mizunara$(APP_EXT) $(OBJ) -L$(BUILD_ROOT)/build/lib -luitk -lidav -lucx $(LDFLAGS) $(TK_LDFLAGS) $(DAV_LDFLAGS) $(BUILD_ROOT)/build/mizunara/%.$(OBJ_EXT): %.c $(CC) $(CFLAGS) $(TK_CFLAGS) -o $@ -c $< diff --git a/mizunara/application.c b/mizunara/application.c index f0a29da..b66a02d 100644 --- a/mizunara/application.c +++ b/mizunara/application.c @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2024 Olaf Wintermann. All rights reserved. + * Copyright 2025 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -30,9 +30,11 @@ #include "menu.h" #include "window.h" +#include "bookmarks.h" void application_init(void) { menu_init(); + bookmarks_init(); } void application_startup(UiEvent* event, void* data) { diff --git a/mizunara/application.h b/mizunara/application.h index 9b10531..dfa9c89 100644 --- a/mizunara/application.h +++ b/mizunara/application.h @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright 2024 Olaf Wintermann. All rights reserved. + * Copyright 2025 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: diff --git a/mizunara/bookmarks.c b/mizunara/bookmarks.c new file mode 100644 index 0000000..8e8b059 --- /dev/null +++ b/mizunara/bookmarks.c @@ -0,0 +1,127 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "bookmarks.h" +#include "application.h" + +#include +#include +#include +#include +#include +#include +#include + +static CxList *bookmarks; + +void bookmarks_init(void) { + bookmarks = cxArrayListCreateSimple(sizeof(MZBookmark), 8); + + bookmarks_import_xdg_user_dirs(bookmarks, NULL); +} + +void bookmarks_import_xdg_user_dirs(CxList *list, const char *optional_path) { + char *path = NULL; + if(!optional_path) { + char *home = getenv("HOME"); + path = util_concat_path(home, ".config/user-dirs.dirs"); + optional_path = path; + } + + int fd = open(optional_path, O_RDONLY); + free(path); + if(fd == -1) { + return; + } + + // we can parse the xdg user-dirs.dirs file with CxProperties + // format: + // key="value" + CxProperties parser; + cxPropertiesInit(&parser, cx_properties_config_default); + + char buf[2048]; + ssize_t r; + int i = 0; + while((r = read(fd, buf, 2048)) > 0) { + cxPropertiesFilln(&parser, buf, r); + cxstring key; + cxstring value; + CxPropertiesStatus status; + while((status=cxPropertiesNext(&parser, &key, &value)) == CX_PROPERTIES_NO_ERROR) { + // remove string quotes from value + if(value.length >= 2 && value.ptr[0] == '"' && value.ptr[value.length-1] == '"') { + value.ptr++; + value.length-=2; + } + + cxmutstr bookmark_path; + if(cx_strprefix(value, CX_STR("$HOME/"))) { + bookmark_path = cx_strcat(2, cx_str(getenv("HOME")), cx_strsubs(value, 5)); + } else { + bookmark_path = cx_strdup(value); + } + + const char *icon = "folder-symbolic"; + if(!cx_strcmp(key, CX_STR("XDG_DOWNLOAD_DIR"))) { + icon = "folder-download-symbolic"; + } else if(!cx_strcmp(key, CX_STR("XDG_DOCUMENTS_DIR"))) { + icon = "folder-documents-symbolic"; + } else if(!cx_strcmp(key, CX_STR("XDG_MUSIC_DIR"))) { + icon = "folder-music-symbolic"; + } else if(!cx_strcmp(key, CX_STR("XDG_PICTURES_DIR"))) { + icon = "folder-pictures-symbolic"; + } else if(!cx_strcmp(key, CX_STR("XDG_VIDEOS_DIR"))) { + icon = "folder-videos-symbolic"; + } else if(!cx_strcmp(key, CX_STR("XDG_PUBLICSHARE_DIR"))) { + icon = "folder-publicshare-symbolic"; + } else if(!cx_strcmp(key, CX_STR("XDG_DESKTOP_DIR"))) { + icon = "user-desktop-symbolic"; + } + + MZBookmark bookmark; + bookmark.icon = icon; + bookmark.name = util_resource_name(bookmark_path.ptr); + bookmark.path = bookmark_path.ptr; + bookmark.sort = i++; + + cxListAdd(list, &bookmark); + } + if(status > CX_PROPERTIES_OK) { + break; + } + } + close(fd); +} + +void bookmarks_init_windowdata(UiList *list) { + CxIterator i = cxListIterator(bookmarks); + cx_foreach(MZBookmark *, bookmark, i) { + ui_list_append(list, bookmark); + } +} diff --git a/mizunara/bookmarks.h b/mizunara/bookmarks.h new file mode 100644 index 0000000..f304a4a --- /dev/null +++ b/mizunara/bookmarks.h @@ -0,0 +1,57 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2025 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MZ_BOOKMARKS_H +#define MZ_BOOKMARKS_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct MZBookmark { + const char *icon; + const char *name; + char *path; + int sort; +} MZBookmark; + +void bookmarks_init(void); + +void bookmarks_import_xdg_user_dirs(CxList *list, const char *optional_path); + +void bookmarks_init_windowdata(UiList *list); + +#ifdef __cplusplus +} +#endif + +#endif /* MZ_BOOKMARKS_H */ + diff --git a/mizunara/window.c b/mizunara/window.c index e115c46..8293e5d 100644 --- a/mizunara/window.c +++ b/mizunara/window.c @@ -29,9 +29,11 @@ #include "window.h" #include "filebrowser.h" +#include "bookmarks.h" #ifdef GTK_MAJOR_VERSION #include "gtk-filesview.h" +#include "bookmarks.h" #endif UiObject* window_create(const char *url) { @@ -96,9 +98,10 @@ void windowdata_init(UiContext *ctx, MainWindow *win) { win->default_dirs = ui_list_new(ctx, NULL); win->user_dirs = ui_list_new(ctx, NULL); + bookmarks_init_windowdata(win->user_dirs); + // TODO: init lists ui_list_append(win->default_dirs, "Test"); - ui_list_append(win->user_dirs, "Test"); } /* @@ -117,6 +120,7 @@ void window_sidebar_default_dirs_item(void *sublist_userdata, void *rowdata, int } void window_sidebar_user_dirs_item(void *sublist_userdata, void *rowdata, int index, UiSubListItem *item) { - item->icon = strdup("folder-documents-symbolic"); - item->label = strdup("Documents"); + MZBookmark *bookmark = rowdata; + item->icon = strdup(bookmark->icon); + item->label = strdup(bookmark->name); }