* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "ui-fileops.h"
+
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <memory>
+
#include <unistd.h>
#include <utime.h>
-#include "main.h"
-#include "ui-fileops.h"
+#include <config.h>
-#include "md5-util.h"
+#include "compat.h"
+#include "debug.h"
#include "filefilter.h"
+#include "intl.h"
#include "layout.h"
-#include "utilops.h"
+#include "main-defines.h"
+#include "md5-util.h"
#include "secure-save.h"
+#include "utilops.h"
/*
*-----------------------------------------------------------------------------
{
gchar *text_l;
- text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
+ text_l = g_locale_from_utf8(text_utf8, -1, nullptr, nullptr, nullptr);
if (err)
{
fputs((text_l) ? text_l : text_utf8, stderr);
lc = getenv("LANG");
bf = getenv("G_BROKEN_FILENAMES");
- string = g_string_new("");
- g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
+ string = g_string_new(_("One or more filenames are not encoded with the preferred locale character set.\n"));
g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
g_string_append(string, "\n");
g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
{
gchar *name;
- name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
+ name = g_convert(path, -1, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
- if (g_utf8_validate(path, -1, NULL))
+ if (g_utf8_validate(path, -1, nullptr))
g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
else
g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
}
gd = generic_dialog_new(_("Filename encoding locale mismatch"),
- "locale warning", NULL, TRUE, NULL, NULL);
- generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
+ "locale warning", nullptr, TRUE, nullptr, nullptr);
+ generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), nullptr, TRUE);
- generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
+ generic_dialog_add_message(gd, GQ_ICON_DIALOG_WARNING,
_("Filename encoding locale mismatch"), string->str, TRUE);
gtk_widget_show(gd->dialog);
#endif
{
gchar *utf8;
- GError *error = NULL;
+ GError *error = nullptr;
- if (!path) return NULL;
+ if (!path) return nullptr;
- utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
+ utf8 = g_filename_to_utf8(path, -1, nullptr, nullptr, &error);
if (error)
{
#if GQ_DEBUG_PATH_UTF8
#endif
{
gchar *path;
- GError *error = NULL;
+ GError *error = nullptr;
- if (!utf8) return NULL;
+ if (!utf8) return nullptr;
- path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
+ path = g_filename_from_utf8(utf8, -1, nullptr, nullptr, &error);
if (error)
{
#if GQ_DEBUG_PATH_UTF8
}
/* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
-const gchar *homedir(void)
+const gchar *homedir()
{
- static gchar *home = NULL;
+ static gchar *home = nullptr;
if (!home)
home = path_to_utf8(getenv("HOME"));
return path_to_utf8(dir);
}
-const gchar *xdg_data_home_get(void)
+const gchar *xdg_data_home_get()
{
- static const gchar *xdg_data_home = NULL;
+ static const gchar *xdg_data_home = nullptr;
if (xdg_data_home) return xdg_data_home;
return xdg_data_home;
}
-const gchar *xdg_config_home_get(void)
+const gchar *xdg_config_home_get()
{
- static const gchar *xdg_config_home = NULL;
+ static const gchar *xdg_config_home = nullptr;
if (xdg_config_home) return xdg_config_home;
return xdg_config_home;
}
-const gchar *xdg_cache_home_get(void)
+const gchar *xdg_cache_home_get()
{
- static const gchar *xdg_cache_home = NULL;
+ static const gchar *xdg_cache_home = nullptr;
if (xdg_cache_home) return xdg_cache_home;
return xdg_cache_home;
}
-const gchar *get_rc_dir(void)
+const gchar *get_rc_dir()
{
- static gchar *rc_dir = NULL;
+ static gchar *rc_dir = nullptr;
if (rc_dir) return rc_dir;
return rc_dir;
}
-const gchar *get_collections_dir(void)
+const gchar *get_collections_dir()
{
- static gchar *collections_dir = NULL;
+ static gchar *collections_dir = nullptr;
if (collections_dir) return collections_dir;
return collections_dir;
}
-const gchar *get_trash_dir(void)
+const gchar *get_trash_dir()
{
- static gchar *trash_dir = NULL;
+ static gchar *trash_dir = nullptr;
if (trash_dir) return trash_dir;
return trash_dir;
}
-const gchar *get_window_layouts_dir(void)
+const gchar *get_window_layouts_dir()
{
- static gchar *window_layouts_dir = NULL;
+ static gchar *window_layouts_dir = nullptr;
if (window_layouts_dir) return window_layouts_dir;
return ret;
}
-gboolean symlink_utf8(const gchar *source, const gchar *target)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+gboolean symlink_utf8_unused(const gchar *source, const gchar *target)
{
gchar *sl;
gchar *tl;
return ret;
}
+#pragma GCC diagnostic pop
gboolean mkdir_utf8(const gchar *s, gint mode)
{
gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
{
struct stat st;
- gchar *sl, *tl;
+ gchar *sl;
+ gchar *tl;
gboolean ret = FALSE;
if (!s || !t) return FALSE;
sta.st_ino == stb.st_ino);
}
+static void fclose_safe(FILE *fp)
+{
+ if (fp) fclose(fp);
+}
+
gboolean copy_file(const gchar *s, const gchar *t)
{
- FILE *fi = NULL;
- FILE *fo = NULL;
- gchar *sl = NULL;
- gchar *tl = NULL;
- gchar *randname = NULL;
gchar buf[16384];
size_t b;
- gint ret = FALSE;
gint fd = -1;
- sl = path_from_utf8(s);
- tl = path_from_utf8(t);
+ std::unique_ptr<gchar, decltype(&g_free)> sl{path_from_utf8(s), g_free};
+ std::unique_ptr<gchar, decltype(&g_free)> tl{path_from_utf8(t), g_free};
- if (hard_linked(sl, tl))
+ if (hard_linked(sl.get(), tl.get()))
{
- ret = TRUE;
- goto end;
+ return TRUE;
}
/* Do not dereference absolute symlinks, but copy them "as is".
* copied/moved to another dir to keep pointing it to same target as
* a relative symlink, so we turn it into absolute symlink using
* realpath() instead. */
- struct stat st;
- if (lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+ auto copy_symlink = [](const gchar *sl, const gchar *tl) -> gboolean
{
+ struct stat st;
+ if (!lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+ {
+ return FALSE;
+ }
+
gchar *link_target;
ssize_t i;
- link_target = g_malloc(st.st_size + 1);
+ link_target = static_cast<gchar *>(g_malloc(st.st_size + 1));
i = readlink(sl, link_target, st.st_size);
if (i<0)
{
g_free(link_target);
- goto orig_copy; // try a "normal" copy
+ return FALSE; // try a "normal" copy
}
link_target[st.st_size] = '\0';
{
gchar *absolute;
- gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
+ const gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
gint len = lastslash - sl + 1;
- absolute = g_malloc(len + st.st_size + 1);
+ absolute = static_cast<gchar *>(g_malloc(len + st.st_size + 1));
strncpy(absolute, sl, len);
strcpy(absolute + len, link_target);
g_free(link_target);
link_target = absolute;
gchar *realPath;
- realPath = realpath(link_target, NULL);
+ realPath = realpath(link_target, nullptr);
- if (realPath != NULL) // successfully resolved into an absolute path
+ if (realPath != nullptr) // successfully resolved into an absolute path
{
g_free(link_target);
link_target = g_strdup(realPath);
else // could not get absolute path, got some error instead
{
g_free(link_target);
- goto orig_copy; // so try a "normal" copy
+ return FALSE; // so try a "normal" copy
}
}
gint success = (symlink(link_target, tl) == 0);
g_free(link_target);
- if (success)
- {
- ret = TRUE;
- goto end;
- }
- } // if symlink did not succeed, continue on to try a copy procedure
- orig_copy:
+ return success;
+ };
- fi = fopen(sl, "rb");
- if (!fi) goto end;
+ if (copy_symlink(sl.get(), tl.get()))
+ {
+ return TRUE;
+ }
+
+ // if symlink did not succeed, continue on to try a copy procedure
+ std::unique_ptr<FILE, decltype(&fclose_safe)> fi{fopen(sl.get(), "rb"), fclose_safe};
+ if (!fi)
+ {
+ return FALSE;
+ }
/* First we write to a temporary file, then we rename it on success,
and attributes from original file are copied */
- randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
- if (!randname) goto end;
+ std::unique_ptr<gchar, decltype(&g_free)> randname{g_strconcat(tl.get(), ".tmp_XXXXXX", NULL), g_free};
+ if (!randname)
+ {
+ return FALSE;
+ }
- fd = g_mkstemp(randname);
- if (fd == -1) goto end;
+ fd = g_mkstemp(randname.get());
+ if (fd == -1)
+ {
+ return FALSE;
+ }
- fo = fdopen(fd, "wb");
+ std::unique_ptr<FILE, decltype(&fclose_safe)> fo{fdopen(fd, "wb"), fclose_safe};
if (!fo) {
close(fd);
- goto end;
+ return FALSE;
}
- while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
+ while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi.get())) && b != 0)
{
- if (fwrite(buf, sizeof(gchar), b, fo) != b)
+ if (fwrite(buf, sizeof(gchar), b, fo.get()) != b)
{
- unlink(randname);
- goto end;
+ unlink(randname.get());
+ return FALSE;
}
}
- fclose(fi); fi = NULL;
- fclose(fo); fo = NULL;
-
- if (rename(randname, tl) < 0) {
- unlink(randname);
- goto end;
+ if (rename(randname.get(), tl.get()) < 0) {
+ unlink(randname.get());
+ return FALSE;
}
- ret = copy_file_attributes(s, t, TRUE, TRUE);
-
-end:
- if (fi) fclose(fi);
- if (fo) fclose(fo);
- if (sl) g_free(sl);
- if (tl) g_free(tl);
- if (randname) g_free(randname);
- return ret;
+ return copy_file_attributes(s, t, TRUE, TRUE);
}
gboolean move_file(const gchar *s, const gchar *t)
{
- gchar *sl, *tl;
+ gchar *sl;
+ gchar *tl;
gboolean ret = TRUE;
if (!s || !t) return FALSE;
gboolean rename_file(const gchar *s, const gchar *t)
{
- gchar *sl, *tl;
+ gchar *sl;
+ gchar *tl;
gboolean ret;
if (!s || !t) return FALSE;
return ret;
}
-gchar *get_current_dir(void)
+gchar *get_current_dir()
{
gchar *pathl;
gchar *path8;
return path8;
}
-void list_free_wrapper(void *data, void *UNUSED(userdata))
-{
- g_free(data);
-}
-
void string_list_free(GList *list)
{
- g_list_foreach(list, (GFunc)list_free_wrapper, NULL);
- g_list_free(list);
+ g_list_free_full(list, g_free);
}
GList *string_list_copy(const GList *list)
{
- GList *new_list = NULL;
- GList *work = (GList *) list;
+ GList *new_list = nullptr;
+ auto work = const_cast<GList *>(list);
while (work)
{
gchar *path;
- path = work->data;
+ path = static_cast<gchar *>(work->data);
work = work->next;
new_list = g_list_prepend(new_list, g_strdup(path));
{
/* well, we tried */
g_free(unique);
- return NULL;
+ return nullptr;
}
}
return unique;
}
-gchar *unique_filename_simple(const gchar *path)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-function"
+gchar *unique_filename_simple_unused(const gchar *path)
{
gchar *unique;
const gchar *name;
const gchar *ext;
- if (!path) return NULL;
+ if (!path) return nullptr;
name = filename_from_path(path);
- if (!name) return NULL;
+ if (!name) return nullptr;
ext = registered_extension_from_path(name);
if (!ext)
{
- unique = unique_filename(path, NULL, "_", TRUE);
+ unique = unique_filename(path, nullptr, "_", TRUE);
}
else
{
return unique;
}
+#pragma GCC diagnostic pop
const gchar *filename_from_path(const gchar *path)
{
const gchar *base;
- if (!path) return NULL;
+ if (!path) return nullptr;
base = strrchr(path, G_DIR_SEPARATOR);
if (base) return base + 1;
{
const gchar *reg_ext;
- if (!path) return NULL;
+ if (!path) return nullptr;
reg_ext = registered_extension_from_path(path);
- return g_strndup(path, strlen(path) - (reg_ext == NULL ? 0 : strlen(reg_ext)));
+ return g_strndup(path, strlen(path) - (reg_ext == nullptr ? 0 : strlen(reg_ext)));
}
void parse_out_relatives(gchar *path)
{
- gint s, t;
+ gint s;
+ gint t;
if (!path) return;
s = p;
continue;
}
- else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
+
+ if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
{
/* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
s = p + 1;
{
gchar *path;
gchar *namel;
- gint p, l;
+ gint p;
+ gint l;
gboolean ret = FALSE;
if (!name) return FALSE;
gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
{
- guchar digest[16];
+ std::unique_ptr<gchar, decltype(&g_free)> pathl{path_from_utf8(path), g_free};
- if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
+ auto md5_text = md5_get_string_from_file(pathl.get());
- return md5_digest_to_text(digest);
+ return md5_text ? md5_text : g_strdup(error_text);
}
/* Download web file
*/
-typedef struct _WebData WebData;
-struct _WebData
+struct WebData
{
GenericDialog *gd;
GCancellable *cancellable;
static void web_file_async_ready_cb(GObject *source_object, GAsyncResult *res, gpointer data)
{
- GError *error = NULL;
- WebData* web = data;
+ GError *error = nullptr;
+ auto web = static_cast<WebData *>(data);
gchar *tmp_filename;
if (!g_cancellable_is_cancelled(web->cancellable))
}
else
{
- file_util_warning_dialog(_("Web file download failed"), error->message, GTK_STOCK_DIALOG_ERROR, NULL);
+ file_util_warning_dialog(_("Web file download failed"), error->message, GQ_ICON_DIALOG_ERROR, nullptr);
}
g_object_unref(web->tmp_g_file);
- web->tmp_g_file = NULL;
+ web->tmp_g_file = nullptr;
g_object_unref(web->cancellable);
g_object_unref(web->web_file);
}
static void web_file_progress_cb(goffset current_num_bytes, goffset total_num_bytes, gpointer data)
{
- WebData* web = data;
+ auto web = static_cast<WebData *>(data);
if (!g_cancellable_is_cancelled(web->cancellable))
{
- gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(web->progress), (gdouble)current_num_bytes / total_num_bytes);
+ gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(web->progress), static_cast<gdouble>(current_num_bytes) / total_num_bytes);
}
}
-static void download_web_file_cancel_button_cb(GenericDialog *UNUSED(gd), gpointer data)
+static void download_web_file_cancel_button_cb(GenericDialog *, gpointer data)
{
- WebData* web = data;
+ auto web = static_cast<WebData *>(data);
g_cancellable_cancel(web->cancellable);
}
gboolean download_web_file(const gchar *text, gboolean minimized, gpointer data)
{
gchar *scheme;
- LayoutWindow *lw = data;
+ auto lw = static_cast<LayoutWindow *>(data);
gchar *tmp_dir;
- GError *error = NULL;
+ GError *error = nullptr;
WebData *web;
gchar *base;
gboolean ret = FALSE;
{
log_printf("Error: could not create temporary file n%s\n", error->message);
g_error_free(error);
- error = NULL;
+ error = nullptr;
ret = TRUE;
}
else
base = g_strdup(g_file_get_basename(web->web_file));
web->tmp_g_file = g_file_new_for_path(g_build_filename(tmp_dir, base, NULL));
- web->gd = generic_dialog_new(_("Download web file"), "download_web_file", NULL, TRUE, download_web_file_cancel_button_cb, web);
+ web->gd = generic_dialog_new(_("Download web file"), "download_web_file", nullptr, TRUE, download_web_file_cancel_button_cb, web);
message = g_strconcat(_("Downloading "), base, NULL);
- generic_dialog_add_message(web->gd, GTK_STOCK_DIALOG_INFO, message, NULL, FALSE);
+ generic_dialog_add_message(web->gd, GQ_ICON_DIALOG_INFO, message, nullptr, FALSE);
web->progress = gtk_progress_bar_new();
- gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
+ gq_gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
gtk_widget_show(web->progress);
if (minimized)
{
gboolean rmdir_recursive(GFile *file, GCancellable *cancellable, GError **error)
{
- g_autoptr(GFileEnumerator) enumerator = NULL;
+ g_autoptr(GFileEnumerator) enumerator = nullptr;
- enumerator = g_file_enumerate_children(file, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, NULL);
+ enumerator = g_file_enumerate_children(file, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, cancellable, nullptr);
- while (enumerator != NULL)
+ while (enumerator != nullptr)
{
GFile *child;
- if (!g_file_enumerator_iterate(enumerator, NULL, &child, cancellable, error))
+ if (!g_file_enumerator_iterate(enumerator, nullptr, &child, cancellable, error))
return FALSE;
- if (child == NULL)
+ if (child == nullptr)
break;
if (!rmdir_recursive(child, cancellable, error))
return FALSE;
* @brief Retrieves the internal scale factor that maps from window coordinates to the actual device pixels
* @param -
* @returns scale factor
- *
- *
+ *
+ *
*/
gint scale_factor()
{
- LayoutWindow *lw = NULL;
+ LayoutWindow *lw = nullptr;
layout_valid(&lw);
return gtk_widget_get_scale_factor(lw->window);