Sort headers using clang-tidy
[geeqie.git] / src / misc.cc
index 27525b6..3d80891 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "main.h"
 #include "misc.h"
 
+#include <clocale>
+#include <memory>
+
+#include <config.h>
+
+#include "debug.h"
+#include "filedata.h"
+#include "intl.h"
+#include "main-defines.h"
+#include "main.h"
+#include "options.h"
 #include "ui-fileops.h"
 
 #include <langinfo.h>
+#include <pwd.h>
 
-gdouble get_zoom_increment(void)
+gdouble get_zoom_increment()
 {
        return ((options->image.zoom_increment != 0) ? static_cast<gdouble>(options->image.zoom_increment) / 100.0 : 1.0);
 }
@@ -34,23 +45,25 @@ gchar *utf8_validate_or_convert(const gchar *text)
 {
        gint len;
 
-       if (!text) return NULL;
+       if (!text) return nullptr;
 
        len = strlen(text);
-       if (!g_utf8_validate(text, len, NULL))
-               return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
+       if (!g_utf8_validate(text, len, nullptr))
+               return g_convert(text, len, "UTF-8", "ISO-8859-1", nullptr, nullptr, nullptr);
 
        return g_strdup(text);
 }
 
 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
 {
-       gchar *s1_key, *s2_key;
-       gchar *s1_t, *s2_t;
+       gchar *s1_key;
+       gchar *s2_key;
+       gchar *s1_t;
+       gchar *s2_t;
        gint ret;
 
-       g_assert(g_utf8_validate(s1, -1, NULL));
-       g_assert(g_utf8_validate(s2, -1, NULL));
+       g_assert(g_utf8_validate(s1, -1, nullptr));
+       g_assert(g_utf8_validate(s2, -1, nullptr));
 
        if (!case_sensitive)
                {
@@ -122,8 +135,8 @@ gchar *expand_tilde(const gchar *filename)
 
        if (slash)
                return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
-       else
-               return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
+
+       return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
 #endif
 }
 
@@ -131,11 +144,13 @@ gchar *expand_tilde(const gchar *filename)
  */
 
 #define GEOCODE_NAME "geocode-parameters.awk"
-#define BUFSIZE 128
+enum {
+       BUFSIZE = 128
+};
 
 gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
 {
-       gchar *message;
+       std::unique_ptr<gchar, decltype(&g_free)> message{nullptr, g_free};
        gchar *path = g_build_filename(path_dir, GEOCODE_NAME, NULL);
        gchar *cmd = g_strconcat("echo \'", input_text, "\'  | awk -f ", path, NULL);
 
@@ -144,9 +159,9 @@ gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
                gchar buf[BUFSIZE];
                FILE *fp;
 
-               if ((fp = popen(cmd, "r")) == NULL)
+               if ((fp = popen(cmd, "r")) == nullptr)
                        {
-                       message = g_strconcat("Error: opening pipe\n", input_text, NULL);
+                       message.reset(g_strconcat("Error: opening pipe\n", input_text, NULL));
                        }
                else
                        {
@@ -155,22 +170,22 @@ gchar *decode_geo_script(const gchar *path_dir, const gchar *input_text)
                                DEBUG_1("Output: %s", buf);
                                }
 
-                       message = g_strconcat(buf, NULL);
+                       message.reset(g_strconcat(buf, NULL));
 
                        if(pclose(fp))
                                {
-                               message = g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL);
+                               message.reset(g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL));
                                }
                        }
                }
        else
                {
-               message = g_strconcat(input_text, NULL);
+               message.reset(g_strconcat(input_text, NULL));
                }
 
        g_free(path);
        g_free(cmd);
-       return message;
+       return message.release();
 }
 
 gchar *decode_geo_parameters(const gchar *input_text)
@@ -284,7 +299,7 @@ gint date_get_first_day_of_week()
  */
 gchar *date_get_abbreviated_day_name(gint day)
 {
-       gchar *abday = NULL;
+       gchar *abday = nullptr;
 
        switch (day)
                {
@@ -316,31 +331,24 @@ gchar *date_get_abbreviated_day_name(gint day)
 
 gchar *convert_rating_to_stars(gint rating)
 {
-       gchar *ret;
-       GString *str = g_string_new(NULL);
+       GString *str = g_string_new(nullptr);
 
        if (rating == -1)
                {
                str = g_string_append_unichar(str, options->star_rating.rejected);
-               ret = g_strdup(str->str);
-               g_string_free(str, TRUE);
+               return g_string_free(str, FALSE);
                }
-       else if (rating > 0 && rating < 6)
+
+       if (rating > 0 && rating < 6)
                {
-               while (rating > 0)
+               for (; rating > 0; --rating)
                        {
                        str = g_string_append_unichar(str, options->star_rating.star);
-                       rating = rating - 1;
                        }
-               ret = g_strdup(str->str);
-               g_string_free(str, TRUE);
-               }
-       else
-               {
-               ret = g_strdup("");
+               return g_string_free(str, FALSE);
                }
 
-       return ret;
+       return g_strdup("");
 }
 
 gchar *get_symbolic_link(const gchar *path_utf8)
@@ -376,20 +384,58 @@ gchar *get_symbolic_link(const gchar *path_utf8)
        return ret;
 }
 
-gint get_cpu_cores(void)
+gint get_cpu_cores()
 {
     return sysconf(_SC_NPROCESSORS_ONLN);
 }
 
-void tree_path_free_wrapper(void *data, void *UNUSED(useradata))
+#ifdef HAVE_GTK4
+void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
+{
+/* @FIXME GTK4 stub */
+}
+#else
+void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba)
+{
+       auto gdk_color = static_cast<GdkColor *>(data);
+
+       gdk_rgba->red = CLAMP((double)gdk_color->red / 65535.0, 0.0, 1.0);
+       gdk_rgba->green = CLAMP((double)gdk_color->green / 65535.0, 0.0, 1.0);
+       gdk_rgba->blue = CLAMP((double)gdk_color->blue / 65535.0, 0.0, 1.0);
+       gdk_rgba->alpha = 1.0;
+}
+#endif
+
+void gq_gtk_entry_set_text(GtkEntry *entry, const gchar *text)
+{
+       GtkEntryBuffer *buffer;
+
+       buffer = gtk_entry_get_buffer(entry);
+       gtk_entry_buffer_set_text(buffer, text, static_cast<gint>(g_utf8_strlen(text, -1)));
+}
+
+const gchar *gq_gtk_entry_get_text(GtkEntry *entry)
+{
+       GtkEntryBuffer *buffer;
+
+       buffer = gtk_entry_get_buffer(entry);
+       return gtk_entry_buffer_get_text(buffer);
+}
+
+void gq_gtk_grid_attach(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach, GtkAttachOptions, GtkAttachOptions, guint, guint)
+{
+       gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
+}
+
+void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_attach, guint right_attach, guint top_attach, guint bottom_attach )
 {
-       gtk_tree_path_free(static_cast<GtkTreePath *>(data));
+       gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach);
 }
 
 /* Copied from the libarchive .repo. examples */
 
 #ifndef HAVE_ARCHIVE
-gchar *open_archive(FileData *UNUSED(fd))
+gchar *open_archive(FileData *)
 {
        log_printf("%s", _("Warning: libarchive not installed"));
        return NULL;
@@ -420,7 +466,7 @@ gchar *open_archive(FileData *fd)
                {
                log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n");
                g_free(destination_dir);
-               return NULL;
+               return nullptr;
                }
 
        current_dir = g_get_current_dir();
@@ -430,7 +476,7 @@ gchar *open_archive(FileData *fd)
                log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n  Error code: "), strerror(errno), "\n");
                g_free(destination_dir);
                g_free(current_dir);
-               return NULL;
+               return nullptr;
                }
 
        flags = ARCHIVE_EXTRACT_TIME;
@@ -442,14 +488,14 @@ gchar *open_archive(FileData *fd)
                log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n  Error code: "), strerror(errno), "\n");
                g_free(destination_dir);
                g_free(current_dir);
-               return NULL;
+               return nullptr;
                }
        g_free(current_dir);
 
        if (!success)
                {
                g_free(destination_dir);
-               destination_dir = NULL;
+               destination_dir = nullptr;
                }
 
        return destination_dir;
@@ -469,9 +515,9 @@ static gboolean extract(const char *filename, int do_extract, int flags)
        archive_read_support_filter_all(a);
        archive_read_support_format_all(a);
 
-       if (filename != NULL && strcmp(filename, "-") == 0)
+       if (filename != nullptr && strcmp(filename, "-") == 0)
                {
-               filename = NULL;
+               filename = nullptr;
                }
        if ((r = archive_read_open_filename(a, filename, 10240)))
                {
@@ -567,7 +613,7 @@ static void msg(const char *m)
 
 static void errmsg(const char *m)
 {
-       if (m == NULL)
+       if (m == nullptr)
                {
                m = "Error: No error description provided.\n";
                }