Sort headers using clang-tidy
[geeqie.git] / src / remote.cc
index 70a6561..4ece932 100644 (file)
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "main.h"
 #include "remote.h"
 
+#include <config.h>
+
 #include "cache-maint.h"
-#include "collect.h"
 #include "collect-io.h"
+#include "collect.h"
+#include "compat.h"
+#include "debug.h"
 #include "exif.h"
 #include "filedata.h"
 #include "filefilter.h"
 #include "image.h"
 #include "img-view.h"
+#include "intl.h"
 #include "layout-image.h"
 #include "layout-util.h"
+#include "main-defines.h"
+#include "main.h"
 #include "misc.h"
 #include "pixbuf-renderer.h"
+#include "rcfile.h"
 #include "slideshow.h"
 #include "ui-fileops.h"
+#include "ui-misc.h"
 #include "utilops.h"
-#include "rcfile.h"
 #include "view-file.h"
 
 #include <csignal>
 
 #include "glua.h"
 
-#define SERVER_MAX_CLIENTS 8
+enum {
+       SERVER_MAX_CLIENTS = 8
+};
 
-#define REMOTE_SERVER_BACKLOG 4
+enum {
+       REMOTE_SERVER_BACKLOG = 4
+};
 
 
 #ifndef UNIX_PATH_MAX
@@ -245,10 +256,15 @@ static RemoteConnection *remote_server_open(const gchar *path)
 {
        RemoteConnection *rc;
        struct sockaddr_un addr;
-       gint sun_path_len;
        gint fd;
        GIOChannel *channel;
 
+       if (strlen(path) >= sizeof(addr.sun_path))
+               {
+               log_printf("Address is too long: %s\n", path);
+               return nullptr;
+               }
+
        if (remote_server_exists(path))
                {
                log_printf("Address already in use: %s\n", path);
@@ -259,8 +275,10 @@ static RemoteConnection *remote_server_open(const gchar *path)
        if (fd == -1) return nullptr;
 
        addr.sun_family = AF_UNIX;
-       sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
-       strncpy(addr.sun_path, path, sun_path_len);
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+       strncpy(addr.sun_path, path, sizeof(addr.sun_path));
+#pragma GCC diagnostic pop
        if (bind(fd, reinterpret_cast<const struct sockaddr*>(&addr), sizeof(addr)) == -1 ||
            listen(fd, REMOTE_SERVER_BACKLOG) == -1)
                {
@@ -285,7 +303,7 @@ static RemoteConnection *remote_server_open(const gchar *path)
        return rc;
 }
 
-static void remote_server_subscribe(RemoteConnection *rc, RemoteReadFunc *func, gpointer data)
+static void remote_server_subscribe(RemoteConnection *rc, RemoteConnection::ReadFunc *func, gpointer data)
 {
        if (!rc || !rc->server) return;
 
@@ -299,17 +317,20 @@ static RemoteConnection *remote_client_open(const gchar *path)
        RemoteConnection *rc;
        struct stat st;
        struct sockaddr_un addr;
-       gint sun_path_len;
        gint fd;
 
+       if (strlen(path) >= sizeof(addr.sun_path)) return nullptr;
+
        if (stat(path, &st) != 0 || !S_ISSOCK(st.st_mode)) return nullptr;
 
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
        if (fd == -1) return nullptr;
 
        addr.sun_family = AF_UNIX;
-       sun_path_len = MIN(strlen(path) + 1, UNIX_PATH_MAX);
-       strncpy(addr.sun_path, path, sun_path_len);
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+       strncpy(addr.sun_path, path, sizeof(addr.sun_path));
+#pragma GCC diagnostic pop
        if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == -1)
                {
                DEBUG_1("error connecting to socket: %s", strerror(errno));
@@ -334,7 +355,8 @@ static void sighandler_sigpipe(gint)
 
 static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
 {
-       struct sigaction new_action, old_action;
+       struct sigaction new_action;
+       struct sigaction old_action;
        gboolean ret = FALSE;
        GError *error = nullptr;
        GIOChannel *channel;
@@ -603,7 +625,10 @@ static void gr_slideshow_stop(const gchar *, GIOChannel *, gpointer)
 
 static void gr_slideshow_delay(const gchar *text, GIOChannel *, gpointer)
 {
-       gdouble t1, t2, t3, n;
+       gdouble t1;
+       gdouble t2;
+       gdouble t3;
+       gdouble n;
        gint res;
 
        res = sscanf(text, "%lf:%lf:%lf", &t1, &t2, &t3);
@@ -730,9 +755,13 @@ static void gr_file_load(const gchar *text, GIOChannel *channel, gpointer data)
 static void gr_pixel_info(const gchar *, GIOChannel *channel, gpointer)
 {
        gchar *pixel_info;
-       gint x_pixel, y_pixel;
-       gint width, height;
-       gint r_mouse, g_mouse, b_mouse;
+       gint x_pixel;
+       gint y_pixel;
+       gint width;
+       gint height;
+       gint r_mouse;
+       gint g_mouse;
+       gint b_mouse;
        PixbufRenderer *pr;
 
        if (!layout_valid(&lw_id)) return;
@@ -775,7 +804,10 @@ static void gr_rectangle(const gchar *, GIOChannel *channel, gpointer)
 {
        gchar *rectangle_info;
        PixbufRenderer *pr;
-       gint x1, y1, x2, y2;
+       gint x1;
+       gint y1;
+       gint x2;
+       gint y2;
 
        if (!options->draw_rectangle) return;
        if (!layout_valid(&lw_id)) return;
@@ -876,7 +908,7 @@ static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurs
        while (work)
                {
                fd = static_cast<FileData *>(work->data);
-               g_string_append_printf(out_string, "%s", fd->path);
+               g_string_append(out_string, fd->path);
                format_class = filter_file_get_class(fd->path);
 
                switch (format_class)
@@ -1147,7 +1179,7 @@ static void gr_collection_list(const gchar *, GIOChannel *channel, gpointer)
        while (work)
                {
                auto collection_name = static_cast<const gchar *>(work->data);
-               out_string = g_string_append(out_string, g_strdup(collection_name));
+               out_string = g_string_append(out_string, collection_name);
                out_string = g_string_append(out_string, "\n");
 
                work = work->next;
@@ -1166,7 +1198,7 @@ static gboolean wait_cb(gpointer data)
        gint x = position >> 16;
        gint y = position - (x << 16);
 
-       gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
+       gq_gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
 
        return G_SOURCE_REMOVE;
 }
@@ -1185,7 +1217,7 @@ static void gr_geometry(const gchar *text, GIOChannel *, gpointer)
                geometry = g_strsplit_set(text, "+", 3);
                if (geometry[1] != nullptr && geometry[2] != nullptr )
                        {
-                       gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
+                       gq_gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
                        }
                }
        else
@@ -1556,113 +1588,39 @@ static void gr_action(const gchar *text, GIOChannel *, gpointer)
                }
 }
 
-static gint simple_sort(gconstpointer a, gconstpointer b)
-{
-       return g_strcmp0((gchar *)a, (gchar *)b);
-}
-
 static void gr_action_list(const gchar *, GIOChannel *channel, gpointer)
 {
-       const gchar *accel_path;
-       gchar *action_list;
-       gchar *action_name;
-       gchar *comment_list;
-       gchar *label;
-       gchar *tooltip;
+       ActionItem *action_item;
        gint max_length = 0;
-       GList *actions;
-       GList *groups;
-       GList *list_final = nullptr;
        GList *list = nullptr;
-       GList *work;
        GString *out_string = g_string_new(nullptr);
-       GtkAction *action;
 
        if (!layout_valid(&lw_id))
                {
                return;
                }
 
-       groups = gtk_ui_manager_get_action_groups(lw_id->ui_manager);
-       while (groups)
-               {
-               actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
-               while (actions)
-                       {
-                       action = GTK_ACTION(actions->data);
-                       accel_path = gtk_action_get_accel_path(action);
-
-                               if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
-                                       {
-                                       g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
-
-                                       action_name = g_path_get_basename(accel_path);
+       list = get_action_items();
 
-                                       /* Used for output column padding */
-                                       if (g_utf8_strlen(action_name, -1) > max_length)
-                                               {
-                                               max_length = g_utf8_strlen(action_name, -1);
-                                               }
-
-                                       /* Tooltips with newlines affect output format */
-                                       if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
-                                               {
-                                               list = g_list_prepend(list, g_strdup(tooltip));
-                                               }
-                                       else
-                                               {
-                                               list = g_list_prepend(list, g_strdup(label));
-                                               }
-
-                                       list = g_list_prepend(list, g_strdup(action_name));
-
-                                       g_free(action_name);
-                                       g_free(label);
-                                       g_free(tooltip);
-                                       }
-
-                       actions = actions->next;
-                       }
-
-               groups = groups->next;
+       /* Get the length required for padding */
+       for (GList *work = list; work; work = work->next)
+               {
+               action_item = static_cast<ActionItem *>(work->data);
+               const auto length = g_utf8_strlen(action_item->name, -1);
+               max_length = MAX(length, max_length);
                }
 
        /* Pad the action names to the same column for readable output */
-       work = list;
-       while (work)
+       for (GList *work = list; work; work = work->next)
                {
-               /* Menu actions are irrelevant */
-               if (g_strstr_len(static_cast<gchar *>(work->data), -1, "Menu") == nullptr)
-                       {
-                       action_list = g_strdup_printf("%-*s", max_length + 4, static_cast<gchar *>(work->data));
-
-                       work=work->next;
-
-                       comment_list = static_cast<gchar *>(work->data);
-                       list_final = g_list_prepend(list_final, g_strconcat(action_list, comment_list, nullptr));
-
-                       g_free(action_list);
-                       }
-               else
-                       {
-                       work = work->next;
-                       }
-               work = work->next;
-               }
-
-       string_list_free(list);
-
-       list_final = g_list_sort(list_final, simple_sort);
+               action_item = static_cast<ActionItem *>(work->data);
 
-       work = list_final;
-       while (work)
-               {
-               out_string = g_string_append(out_string, static_cast<gchar *>(work->data) );
+               g_string_append_printf(out_string, "%-*s", max_length + 4, action_item->name);
+               out_string = g_string_append(out_string, action_item->label);
                out_string = g_string_append(out_string, "\n");
-               work = work->next;
                }
 
-       string_list_free(list_final);
+       action_items_free(list);
 
        g_io_channel_write_chars(channel, out_string->str, -1, nullptr, nullptr);
        g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, nullptr, nullptr);
@@ -1818,7 +1776,8 @@ static RemoteCommandEntry *remote_command_find(const gchar *text, const gchar **
                                if (offset) *offset = text + strlen(remote_commands[i].opt_s);
                                return &remote_commands[i];
                                }
-                       else if (remote_commands[i].opt_l &&
+
+                       if (remote_commands[i].opt_l &&
                                 strncmp(remote_commands[i].opt_l, text, strlen(remote_commands[i].opt_l)) == 0)
                                {
                                if (offset) *offset = text + strlen(remote_commands[i].opt_l);
@@ -1872,10 +1831,11 @@ void remote_help()
                        {
                        s_opt_param = g_strdup(remote_commands[i].opt_s  ? remote_commands[i].opt_s : "" );
                        l_opt_param = g_strconcat(remote_commands[i].opt_l, remote_commands[i].parameter, NULL);
-                       printf_term(FALSE, "  %-4s %-40s%-s\n",
-                                       s_opt_param,
-                                       l_opt_param,
-                                       _(remote_commands[i].description));
+
+                       if (g_str_has_prefix(l_opt_param, "--"))
+                               {
+                               printf_term(FALSE, "  %-4s %-40s%-s\n", s_opt_param, l_opt_param, _(remote_commands[i].description));
+                               }
                        g_free(s_opt_param);
                        g_free(l_opt_param);
                        }