qulogic/libgnt

Merged in default (pull request #80)

2019-05-21, Gary Kramlich
9f83881789ea
Merged in default (pull request #80)

Hide GntFile entirely, plus related cleanup

Approved-by: Gary Kramlich
  • +61 -149
    gntfilesel.c
  • +0 -78
    gntfilesel.h
  • --- a/gntfilesel.c Tue May 21 01:25:14 2019 +0000
    +++ b/gntfilesel.c Tue May 21 01:33:29 2019 +0000
    @@ -59,8 +59,6 @@
    gboolean dirsonly; /* Show only directories */
    gboolean multiselect;
    GList *tags; /* List of tagged files when multiselect is set */
    -
    - gboolean (*read_fn)(const char *path, GList **files, GError **error);
    } GntFileSelPrivate;
    enum
    @@ -158,79 +156,12 @@
    return find;
    }
    -GntFile* gnt_file_new_dir(const char *name)
    -{
    - GntFile *file = g_new0(GntFile, 1);
    - file->basename = g_strdup(name);
    - file->type = GNT_FILE_DIR;
    - return file;
    -}
    -
    -GntFile* gnt_file_new(const char *name, unsigned long size)
    -{
    - GntFile *file = g_new0(GntFile, 1);
    - file->basename = g_strdup(name);
    - file->type = GNT_FILE_REGULAR;
    - file->size = size;
    - return file;
    -}
    -
    -static gboolean
    -local_read_fn(const char *path, GList **files, GError **error)
    -{
    - GDir *dir;
    - GntFile *file;
    - const char *str;
    -
    - dir = g_dir_open(path, 0, error);
    - if (dir == NULL || (error && *error)) {
    - return FALSE;
    - }
    -
    - *files = NULL;
    - if (*path != '\0' && strcmp(path, G_DIR_SEPARATOR_S)) {
    - file = gnt_file_new_dir("..");
    - *files = g_list_prepend(*files, file);
    - }
    -
    - while ((str = g_dir_read_name(dir)) != NULL) {
    - char *fp = g_build_filename(path, str, NULL);
    - GStatBuf st;
    -
    - if (g_stat(fp, &st)) {
    - gnt_warning("Error stating location %s", fp);
    - } else {
    - if (S_ISDIR(st.st_mode)) {
    - file = gnt_file_new_dir(str);
    - } else {
    - file = gnt_file_new(str, (long)st.st_size);
    - }
    - *files = g_list_prepend(*files, file);
    - }
    - g_free(fp);
    - }
    - g_dir_close(dir);
    -
    - *files = g_list_reverse(*files);
    - return TRUE;
    -}
    -
    -static void
    -gnt_file_free(GntFile *file)
    -{
    - g_return_if_fail(file != NULL);
    -
    - g_free(file->fullpath);
    - g_free(file->basename);
    - g_free(file);
    -}
    -
    static gboolean
    location_changed(GntFileSel *sel, GError **err)
    {
    GntFileSelPrivate *priv = gnt_file_sel_get_instance_private(sel);
    - GList *files, *iter;
    - gboolean success;
    + GDir *dir;
    + const gchar *str;
    if (!priv->dirs) {
    return TRUE;
    @@ -252,50 +183,74 @@
    * XXX: This is blocking.
    * XXX:/
    */
    - files = NULL;
    - if (priv->read_fn) {
    - success = priv->read_fn(priv->current, &files, err);
    - } else {
    - success = local_read_fn(priv->current, &files, err);
    - }
    -
    - if (!success || *err) {
    + dir = g_dir_open(priv->current, 0, err);
    + if (dir == NULL || *err) {
    gnt_warning("error opening location %s (%s)", priv->current,
    *err ? (*err)->message : "reason unknown");
    return FALSE;
    }
    - for (iter = files; iter; iter = iter->next) {
    - GntFile *file = iter->data;
    - char *str = file->basename;
    - if (file->type == GNT_FILE_DIR) {
    - gnt_tree_add_row_after(
    - GNT_TREE(priv->dirs), g_strdup(str),
    - gnt_tree_create_row(GNT_TREE(priv->dirs), str),
    - NULL, NULL);
    - if (priv->multiselect && priv->dirsonly &&
    - is_tagged(sel, str)) {
    - gnt_tree_set_row_flags(GNT_TREE(priv->dirs),
    - (gpointer)str,
    - GNT_TEXT_FLAG_BOLD);
    - }
    - } else if (!priv->dirsonly) {
    - char size[128];
    - snprintf(size, sizeof(size), "%ld", file->size);
    + if (*priv->current != '\0' &&
    + g_path_is_absolute(priv->current) &&
    + *g_path_skip_root(priv->current) != '\0') {
    + gnt_tree_add_row_after(
    + GNT_TREE(priv->dirs), g_strdup(".."),
    + gnt_tree_create_row(GNT_TREE(priv->dirs), ".."), NULL,
    + NULL);
    + if (priv->multiselect && priv->dirsonly &&
    + is_tagged(sel, "..")) {
    + gnt_tree_set_row_flags(GNT_TREE(priv->dirs), "..",
    + GNT_TEXT_FLAG_BOLD);
    + }
    + }
    +
    + while ((str = g_dir_read_name(dir)) != NULL) {
    + gchar *fp = g_build_filename(priv->current, str, NULL);
    + GStatBuf st;
    +
    + if (g_stat(fp, &st)) {
    + gnt_warning("Error stating location %s", fp);
    + } else {
    + /* Just there to silence const warnings; the key is
    + * suitably copied when necessary. */
    + gchar *key = (gchar *)str;
    - gnt_tree_add_row_after(
    - GNT_TREE(priv->files), g_strdup(str),
    - gnt_tree_create_row(GNT_TREE(priv->files), str,
    - size, ""),
    - NULL, NULL);
    - if (priv->multiselect && is_tagged(sel, str)) {
    - gnt_tree_set_row_flags(GNT_TREE(priv->files),
    - (gpointer)str,
    - GNT_TEXT_FLAG_BOLD);
    + if (S_ISDIR(st.st_mode)) {
    + gnt_tree_add_row_after(
    + GNT_TREE(priv->dirs), g_strdup(str),
    + gnt_tree_create_row(
    + GNT_TREE(priv->dirs), key),
    + NULL, NULL);
    + if (priv->multiselect && priv->dirsonly &&
    + is_tagged(sel, str)) {
    + gnt_tree_set_row_flags(
    + GNT_TREE(priv->dirs), key,
    + GNT_TEXT_FLAG_BOLD);
    + }
    + } else {
    + gchar *size;
    +
    + size = g_format_size((guint64)st.st_size);
    +
    + gnt_tree_add_row_after(
    + GNT_TREE(priv->files), g_strdup(str),
    + gnt_tree_create_row(
    + GNT_TREE(priv->files), key,
    + size, ""),
    + NULL, NULL);
    + if (priv->multiselect && is_tagged(sel, str)) {
    + gnt_tree_set_row_flags(
    + GNT_TREE(priv->files), key,
    + GNT_TEXT_FLAG_BOLD);
    + }
    +
    + g_free(size);
    }
    }
    + g_free(fp);
    }
    - g_list_free_full(files, (GDestroyNotify)gnt_file_free);
    + g_dir_close(dir);
    +
    if (gnt_widget_get_mapped(GNT_WIDGET(sel))) {
    gnt_widget_draw(GNT_WIDGET(sel));
    }
    @@ -786,46 +741,3 @@
    list = g_list_reverse(list);
    return list;
    }
    -
    -void gnt_file_sel_set_read_fn(GntFileSel *sel, gboolean (*read_fn)(const char *path, GList **files, GError **error))
    -{
    - GntFileSelPrivate *priv = NULL;
    -
    - g_return_if_fail(GNT_IS_FILE_SEL(sel));
    - priv = gnt_file_sel_get_instance_private(sel);
    -
    - priv->read_fn = read_fn;
    -}
    -
    -/**************************************************************************
    - * GntFile GBoxed API
    - **************************************************************************/
    -static GntFile *
    -gnt_file_copy(GntFile *file)
    -{
    - GntFile *file_new;
    -
    - g_return_val_if_fail(file != NULL, NULL);
    -
    - file_new = g_new(GntFile, 1);
    - *file_new = *file;
    -
    - file_new->fullpath = g_strdup(file->fullpath);
    - file_new->basename = g_strdup(file->basename);
    -
    - return file_new;
    -}
    -
    -GType
    -gnt_file_get_type(void)
    -{
    - static GType type = 0;
    -
    - if (type == 0) {
    - type = g_boxed_type_register_static("GntFile",
    - (GBoxedCopyFunc)gnt_file_copy,
    - (GBoxedFreeFunc)gnt_file_free);
    - }
    -
    - return type;
    -}
    --- a/gntfilesel.h Tue May 21 01:25:14 2019 +0000
    +++ b/gntfilesel.h Tue May 21 01:33:29 2019 +0000
    @@ -35,35 +35,6 @@
    #include "gntwindow.h"
    #define GNT_TYPE_FILE_SEL gnt_file_sel_get_type()
    -#define GNT_TYPE_FILE gnt_file_get_type()
    -
    -#ifndef GNT_DISABLE_DEPRECATED
    -/**
    - * GntFileType:
    - *
    - * Deprecated: 2.14.0: This is an internal implementation detail. Use #GFile
    - * from GIO for a similar abstraction.
    - */
    -typedef enum
    -{
    - GNT_FILE_REGULAR,
    - GNT_FILE_DIR
    -} GntFileType;
    -
    -/**
    - * GntFile:
    - *
    - * Deprecated: 2.14.0: This is an internal implementation detail. Use #GFile
    - * from GIO for a similar abstraction.
    - */
    -typedef struct
    -{
    - char *fullpath;
    - char *basename;
    - GntFileType type;
    - unsigned long size;
    -} GntFile;
    -#endif
    G_BEGIN_DECLS
    @@ -89,17 +60,6 @@
    void (*gnt_reserved4)(void);
    };
    -#ifndef GNT_DISABLE_DEPRECATED
    -/**
    - * gnt_file_get_type:
    - *
    - * Returns: The #GType for the #GntFile boxed structure.
    - *
    - * Deprecated: 2.14.0: This is an internal implementation detail.
    - */
    -GType gnt_file_get_type(void);
    -#endif
    -
    /**
    * gnt_file_sel_new:
    *
    @@ -201,44 +161,6 @@
    */
    void gnt_file_sel_set_suggested_filename(GntFileSel *sel, const char *suggest);
    -#ifndef GNT_DISABLE_DEPRECATED
    -/**
    - * gnt_file_sel_set_read_fn:
    - * @sel: The file selector.
    - * @read_fn: The custom read function.
    - *
    - * Set custom functions to read the names of files.
    - *
    - * Deprecated: 2.14.0: This is an internal implementation detail.
    - */
    -void gnt_file_sel_set_read_fn(GntFileSel *sel, gboolean (*read_fn)(const char *path, GList **files, GError **error));
    -
    -/**
    - * gnt_file_new:
    - * @name: The name of the file.
    - * @size: The size of the file.
    - *
    - * Create a new GntFile.
    - *
    - * Returns: The newly created GntFile.
    - *
    - * Deprecated: 2.14.0: This is an internal implementation detail.
    - */
    -GntFile* gnt_file_new(const char *name, unsigned long size);
    -
    -/**
    - * gnt_file_new_dir:
    - * @name: The name of the directory.
    - *
    - * Create a new GntFile for a directory.
    - *
    - * Returns: The newly created GntFile.
    - *
    - * Deprecated: 2.14.0: This is an internal implementation detail.
    - */
    -GntFile* gnt_file_new_dir(const char *name);
    -#endif
    -
    G_END_DECLS
    #endif /* GNT_FILE_SEL_H */