Option to select menu style
[geeqie.git] / src / collect-io.cc
index db16195..e0c1933 100644 (file)
@@ -35,7 +35,7 @@
 #define GQ_COLLECTION_FAIL_PERCENT 98
 #define GQ_COLLECTION_READ_BUFSIZE 4096
 
-typedef struct _CollectManagerEntry CollectManagerEntry;
+struct CollectManagerEntry;
 
 static void collection_load_thumb_step(CollectionData *cd);
 static gboolean collection_save_private(CollectionData *cd, const gchar *path);
@@ -44,21 +44,25 @@ static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
 static void collect_manager_entry_reset(CollectManagerEntry *entry);
 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
 
+namespace
+{
 
-static gboolean scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h)
+gboolean scan_geometry(gchar *buffer, GdkRectangle &window)
 {
        gint nx, ny, nw, nh;
 
        if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
 
-       *x = nx;
-       *y = ny;
-       *w = nw;
-       *h = nh;
+       window.x = nx;
+       window.y = ny;
+       window.width = nw;
+       window.height = nh;
 
        return TRUE;
 }
 
+}
+
 static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
 {
        gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
@@ -73,12 +77,11 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
        guint total = 0;
        guint fail = 0;
        gboolean changed = FALSE;
-       CollectManagerEntry *entry = NULL;
+       CollectManagerEntry *entry = nullptr;
        guint flush = !!(flags & COLLECTION_LOAD_FLUSH);
        guint append = !!(flags & COLLECTION_LOAD_APPEND);
        guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY);
        gboolean reading_extended_filename = FALSE;
-       GString *extended_filename_buffer = g_string_new(NULL);
        gchar *buffer2;
 
        if (!only_geometry)
@@ -92,8 +95,8 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
 
                if (!append)
                        {
-                       collection_list_free(cd->list);
-                       cd->list = NULL;
+                       g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
+                       cd->list = nullptr;
                        }
                }
 
@@ -115,6 +118,7 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
                return FALSE;
                }
 
+       GString *extended_filename_buffer = g_string_new(nullptr);
        while (fgets(s_buf, sizeof(s_buf), f))
                {
                gchar *buf;
@@ -141,7 +145,7 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
                                        limit_failures = FALSE;
                                        }
                                else if (strncmp(p, "#geometry:", 10 ) == 0 &&
-                                        scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h))
+                                        scan_geometry(p + 10, cd->window))
                                        {
                                        has_geometry_header = TRUE;
                                        cd->window_read = TRUE;
@@ -198,7 +202,7 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
                                }
                        }
 
-               if (strlen(extended_filename_buffer->str) > 0)
+               if (extended_filename_buffer->len > 0)
                        {
                        buffer2 = g_strdup(extended_filename_buffer->str);
                        g_string_erase(extended_filename_buffer, 0, -1);
@@ -246,13 +250,13 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
 
        if (!flush)
                {
-               gchar *buf = NULL;
+               gchar *buf = nullptr;
                while (collect_manager_process_action(entry, &buf))
                        {
                        collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
                        changed = TRUE;
                        g_free(buf);
-                       buf = NULL;
+                       buf = nullptr;
                        }
                }
 
@@ -293,17 +297,17 @@ static void collection_load_thumb_do(CollectionData *cd)
        if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
 }
 
-static void collection_load_thumb_error_cb(ThumbLoader *UNUSED(tl), gpointer data)
+static void collection_load_thumb_error_cb(ThumbLoader *, gpointer data)
 {
-       CollectionData *cd = static_cast<CollectionData *>(data);
+       auto cd = static_cast<CollectionData *>(data);
 
        collection_load_thumb_do(cd);
        collection_load_thumb_step(cd);
 }
 
-static void collection_load_thumb_done_cb(ThumbLoader *UNUSED(tl), gpointer data)
+static void collection_load_thumb_done_cb(ThumbLoader *, gpointer data)
 {
-       CollectionData *cd = static_cast<CollectionData *>(data);
+       auto cd = static_cast<CollectionData *>(data);
 
        collection_load_thumb_do(cd);
        collection_load_thumb_step(cd);
@@ -336,7 +340,7 @@ static void collection_load_thumb_step(CollectionData *cd)
                collection_load_stop(cd);
 
                /* send a NULL CollectInfo to notify end */
-               if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data);
+               if (cd->info_updated_func) cd->info_updated_func(cd, nullptr, cd->info_updated_data);
 
                return;
                }
@@ -348,7 +352,7 @@ static void collection_load_thumb_step(CollectionData *cd)
        thumb_loader_set_callbacks(cd->thumb_loader,
                                   collection_load_thumb_done_cb,
                                   collection_load_thumb_error_cb,
-                                  NULL,
+                                  nullptr,
                                   cd);
 
        /* start it */
@@ -380,7 +384,7 @@ void collection_load_stop(CollectionData *cd)
        if (!cd->thumb_loader) return;
 
        thumb_loader_free(cd->thumb_loader);
-       cd->thumb_loader = NULL;
+       cd->thumb_loader = nullptr;
 }
 
 static gboolean collection_save_private(CollectionData *cd, const gchar *path)
@@ -412,13 +416,13 @@ static gboolean collection_save_private(CollectionData *cd, const gchar *path)
        collection_update_geometry(cd);
        if (cd->window_read)
                {
-               secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
+               secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window.x, cd->window.y, cd->window.width, cd->window.height);
                }
 
        work = cd->list;
        while (work && secsave_errno == SS_ERR_NONE)
                {
-               CollectInfo *ci = static_cast<CollectInfo *>(work->data);
+               auto ci = static_cast<CollectInfo *>(work->data);
                secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
                work = work->next;
                }
@@ -476,7 +480,7 @@ gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
 #define COLLECT_MANAGER_FLUSH_DELAY      10000
 
-struct _CollectManagerEntry
+struct CollectManagerEntry
 {
        gchar *path;
        GList *add_list;
@@ -485,14 +489,13 @@ struct _CollectManagerEntry
        gboolean empty;
 };
 
-typedef enum {
+enum CollectManagerType {
        COLLECTION_MANAGER_UPDATE,
        COLLECTION_MANAGER_ADD,
        COLLECTION_MANAGER_REMOVE
-} CollectManagerType;
+};
 
-typedef struct _CollectManagerAction CollectManagerAction;
-struct _CollectManagerAction
+struct CollectManagerAction
 {
        gchar *oldpath;
        gchar *newpath;
@@ -503,9 +506,9 @@ struct _CollectManagerAction
 };
 
 
-static GList *collection_manager_entry_list = NULL;
-static GList *collection_manager_action_list = NULL;
-static GList *collection_manager_action_tail = NULL;
+static GList *collection_manager_entry_list = nullptr;
+static GList *collection_manager_action_list = nullptr;
+static GList *collection_manager_action_tail = nullptr;
 static guint collection_manager_timer_id = 0; /* event source id */
 
 
@@ -543,19 +546,7 @@ static void collect_manager_action_unref(CollectManagerAction *action)
 
 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
 {
-       GList *work;
-
-       work = entry->add_list;
-       while (work)
-               {
-               CollectManagerAction *action;
-
-               action = static_cast<CollectManagerAction *>(work->data);
-               work = work->next;
-
-               collect_manager_action_unref(action);
-               }
-       g_list_free(entry->add_list);
+       g_list_free_full(entry->add_list, reinterpret_cast<GDestroyNotify>(collect_manager_action_unref));
        if (g_hash_table_size(entry->oldpath_hash) > 0)
                g_hash_table_destroy(entry->oldpath_hash);
        else
@@ -568,8 +559,8 @@ static void collect_manager_entry_free_data(CollectManagerEntry *entry)
 
 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
 {
-       entry->add_list = NULL;
-       entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref);
+       entry->add_list = nullptr;
+       entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, reinterpret_cast<GDestroyNotify>(collect_manager_action_unref));
        entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
        entry->empty = TRUE;
 
@@ -621,7 +612,7 @@ static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
                        return entry;
                        }
                }
-       return NULL;
+       return nullptr;
 
 }
 
@@ -632,10 +623,10 @@ static void collect_manager_entry_add_action(CollectManagerEntry *entry, Collect
 
        entry->empty = FALSE;
 
-       if (action->oldpath == NULL)
+       if (action->oldpath == nullptr)
                {
                /* add file */
-               if (action->newpath == NULL)
+               if (action->newpath == nullptr)
                        {
                        return;
                        }
@@ -703,20 +694,20 @@ static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar
        gchar *path = *path_ptr;
        CollectManagerAction *action;
 
-       if (path == NULL)
+       if (path == nullptr)
                {
                /* get new files */
                if (entry->add_list)
                        {
                        action = static_cast<CollectManagerAction *>(entry->add_list->data);
-                       g_assert(action->oldpath == NULL);
+                       g_assert(action->oldpath == nullptr);
                        entry->add_list = g_list_remove(entry->add_list, action);
                        path = g_strdup(action->newpath);
                        g_hash_table_remove(entry->newpath_hash, path);
                        collect_manager_action_unref(action);
                        }
                *path_ptr = path;
-               return (path != NULL);
+               return (path != nullptr);
                }
 
        action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->oldpath_hash, path));
@@ -730,14 +721,14 @@ static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar
        return FALSE; /* no change */
 }
 
-static void collect_manager_refresh(void)
+static void collect_manager_refresh()
 {
        GList *list;
        GList *work;
        FileData *dir_fd;
 
        dir_fd = file_data_new_dir(get_collections_dir());
-       filelist_read(dir_fd, &list, NULL);
+       filelist_read(dir_fd, &list, nullptr);
        file_data_unref(dir_fd);
 
        work = collection_manager_entry_list;
@@ -762,13 +753,13 @@ static void collect_manager_refresh(void)
                                list = g_list_remove(list, fd);
                                file_data_unref(fd);
 
-                               entry = NULL;
+                               entry = nullptr;
                                }
                        else
                                {
                                collect_manager_entry_free(entry);
 
-                               entry = NULL;
+                               entry = nullptr;
                                }
                        }
                }
@@ -791,7 +782,7 @@ static void collect_manager_process_actions(gint max)
 {
        if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
 
-       while (collection_manager_action_list != NULL && max > 0)
+       while (collection_manager_action_list != nullptr && max > 0)
                {
                CollectManagerAction *action;
                GList *work;
@@ -817,11 +808,11 @@ static void collect_manager_process_actions(gint max)
                                if (action->type == COLLECTION_MANAGER_ADD)
                                        {
                                        action->newpath = action->oldpath;
-                                       action->oldpath = NULL;
+                                       action->oldpath = nullptr;
                                        }
                                else if (action->type == COLLECTION_MANAGER_REMOVE)
                                        {
-                                       action->newpath = NULL;
+                                       action->newpath = nullptr;
                                        }
                                collect_manager_entry_add_action(entry, action);
                                }
@@ -839,7 +830,7 @@ static void collect_manager_process_actions(gint max)
 
                if (collection_manager_action_tail == collection_manager_action_list)
                        {
-                       collection_manager_action_tail = NULL;
+                       collection_manager_action_tail = nullptr;
                        }
                collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
                collect_manager_action_unref(action);
@@ -860,7 +851,7 @@ static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
        return TRUE;
 }
 
-static gboolean collect_manager_process_entry_list(void)
+static gboolean collect_manager_process_entry_list()
 {
        GList *work;
 
@@ -879,7 +870,7 @@ static gboolean collect_manager_process_entry_list(void)
 
 
 
-static gboolean collect_manager_process_cb(gpointer UNUSED(data))
+static gboolean collect_manager_process_cb(gpointer)
 {
        if (collection_manager_action_list) collect_manager_refresh();
        collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
@@ -891,11 +882,11 @@ static gboolean collect_manager_process_cb(gpointer UNUSED(data))
        return G_SOURCE_REMOVE;
 }
 
-static gboolean collect_manager_timer_cb(gpointer UNUSED(data))
+static gboolean collect_manager_timer_cb(gpointer)
 {
        DEBUG_1("collection manager timer expired");
 
-       g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL);
+       g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, nullptr, nullptr);
 
        collection_manager_timer_id = 0;
        return FALSE;
@@ -914,7 +905,7 @@ static void collect_manager_timer_push(gint stop)
        if (!stop)
                {
                collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
-                                                           collect_manager_timer_cb, NULL);
+                                                           collect_manager_timer_cb, nullptr);
                DEBUG_1("collection manager timer started");
                }
 }
@@ -959,7 +950,7 @@ void collect_manager_add(FileData *fd, const gchar *collection)
        cw = collection_window_find_by_path(collection);
        if (cw)
                {
-               if (collection_list_find_fd(cw->cd->list, fd) == NULL)
+               if (collection_list_find_fd(cw->cd->list, fd) == nullptr)
                        {
                        collection_add(cw->cd, fd, FALSE);
                        }
@@ -988,15 +979,15 @@ void collect_manager_remove(FileData *fd, const gchar *collection)
        collect_manager_add_action(action);
 }
 
-void collect_manager_flush(void)
+void collect_manager_flush()
 {
        collect_manager_timer_push(TRUE);
 
        DEBUG_1("collection manager flushing");
-       while (collect_manager_process_cb(NULL));
+       while (collect_manager_process_cb(nullptr));
 }
 
-void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer UNUSED(data))
+void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer)
 {
        if (!(type & NOTIFY_CHANGE) || !fd->change) return;
 
@@ -1020,62 +1011,62 @@ void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer UNUSED(da
 
 static gint collection_manager_sort_cb(gconstpointer a, gconstpointer b)
 {
-       const gchar *char_a = static_cast<const gchar *>(a);
-       const gchar *char_b = static_cast<const gchar *>(b);
+       auto char_a = static_cast<const gchar *>(a);
+       auto char_b = static_cast<const gchar *>(b);
 
        return g_strcmp0(char_a, char_b);
 }
+
 /**
  * @brief Creates sorted list of collections
  * @param[out] names_exc sorted list of collections names excluding extension
  * @param[out] names_inc sorted list of collections names including extension
  * @param[out] paths sorted list of collection paths
- * 
+ *
  * Lists of type gchar.
  * Used lists must be freed with string_list_free()
  */
 void collect_manager_list(GList **names_exc, GList **names_inc, GList **paths)
 {
        FileData *dir_fd;
-       GList *list = NULL;
+       GList *list = nullptr;
        gchar *name;
        FileData *fd;
        gchar *filename;
 
-       if (names_exc == NULL && names_inc == NULL && paths == NULL)
+       if (names_exc == nullptr && names_inc == nullptr && paths == nullptr)
                {
                return;
                }
 
        dir_fd = file_data_new_dir((get_collections_dir()));
 
-       filelist_read(dir_fd, &list, NULL);
+       filelist_read(dir_fd, &list, nullptr);
 
        while (list)
                {
                fd = static_cast<FileData *>(list->data);
-               filename = g_strdup(filename_from_path((gchar *)fd->path));
+               filename = g_strdup(filename_from_path(fd->path));
 
                if (file_extension_match(filename, GQ_COLLECTION_EXT))
                        {
                        name = remove_extension_from_path(filename);
 
-                       if (names_exc != NULL)
+                       if (names_exc != nullptr)
                                {
                                *names_exc = g_list_insert_sorted(*names_exc, g_strdup(name),
                                                                                        collection_manager_sort_cb);
                                *names_exc = g_list_first(*names_exc);
                                }
-                       if (names_inc != NULL)
+                       if (names_inc != nullptr)
                                {
                                *names_inc = g_list_insert_sorted(*names_inc,filename,
                                                                                        collection_manager_sort_cb);
                                *names_inc = g_list_first(*names_inc);
                                }
-                       if (paths != NULL)
+                       if (paths != nullptr)
                                {
-                               *paths = g_list_insert_sorted(*paths,fd->path,
+                               *paths = g_list_insert_sorted(*paths, g_strdup(fd->path),
                                                                                        collection_manager_sort_cb);
                                *paths = g_list_first(*paths);
                                }