[Libguestfs] [p2v PATCH 2/2] remove old GLIB2 / GTK3 compat code

Laszlo Ersek lersek at redhat.com
Wed Sep 28 12:06:50 UTC 2022


Open-code conditional compilation directives that evaluate to constant
values when virt-p2v is built with GLIB >= 2.56 and GTK >= 3.22.

Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 gui-gtk3-compat.h | 46 -------------
 gui.c             | 69 ++------------------
 2 files changed, 4 insertions(+), 111 deletions(-)

diff --git a/gui-gtk3-compat.h b/gui-gtk3-compat.h
index e26506469eea..147abf9f4f54 100644
--- a/gui-gtk3-compat.h
+++ b/gui-gtk3-compat.h
@@ -5,135 +5,89 @@
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 /* Backwards compatibility for some deprecated functions in Gtk 3. */
-#if !GTK_CHECK_VERSION(3,2,0)   /* gtk < 3.2 */
-static gboolean
-gdk_event_get_button (const GdkEvent *event, guint *button)
-{
-  if (event->type != GDK_BUTTON_PRESS)
-    return FALSE;
-
-  *button = ((const GdkEventButton *) event)->button;
-  return TRUE;
-}
-#endif
-
-#if GTK_CHECK_VERSION(3,2,0)   /* gtk >= 3.2 */
 #define hbox_new(box, homogeneous, spacing)                    \
   do {                                                         \
     (box) = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, spacing); \
     if (homogeneous)                                           \
       gtk_box_set_homogeneous (GTK_BOX (box), TRUE);           \
   } while (0)
 #define vbox_new(box, homogeneous, spacing)                    \
   do {                                                         \
     (box) = gtk_box_new (GTK_ORIENTATION_VERTICAL, spacing);   \
     if (homogeneous)                                           \
       gtk_box_set_homogeneous (GTK_BOX (box), TRUE);           \
   } while (0)
-#else /* gtk < 3.2 */
-#define hbox_new(box, homogeneous, spacing)             \
-  (box) = gtk_hbox_new ((homogeneous), (spacing))
-#define vbox_new(box, homogeneous, spacing)             \
-  (box) = gtk_vbox_new ((homogeneous), (spacing))
-#endif
 
-#if GTK_CHECK_VERSION(3,4,0)   /* gtk >= 3.4 */
 /* Copy this enum from GtkTable, as when building without deprecated
  * functions this is not automatically pulled in.
  */
 typedef enum
 {
   GTK_EXPAND = 1 << 0,
   GTK_SHRINK = 1 << 1,
   GTK_FILL   = 1 << 2
 } GtkAttachOptions;
 /* GtkGrid is sufficiently similar to GtkTable that we can just
  * redefine these functions.
  */
 #define table_new(grid, rows, columns)          \
   (grid) = gtk_grid_new ()
 #define table_attach(grid, child, left, right, top, xoptions, yoptions, xpadding, ypadding) \
   do {                                                                  \
     if (((xoptions) & GTK_EXPAND) != 0)                                 \
       gtk_widget_set_hexpand ((child), TRUE);                           \
     if (((xoptions) & GTK_FILL) != 0)                                   \
       gtk_widget_set_halign ((child), GTK_ALIGN_FILL);                  \
     if (((yoptions) & GTK_EXPAND) != 0)                                 \
       gtk_widget_set_vexpand ((child), TRUE);                           \
     if (((yoptions) & GTK_FILL) != 0)                                   \
       gtk_widget_set_valign ((child), GTK_ALIGN_FILL);                  \
     set_padding ((child), (xpadding), (ypadding));                      \
     gtk_grid_attach (GTK_GRID (grid), (child),                          \
                      (left), (top), (right)-(left), 1);    \
   } while (0)
-#else
-#define table_new(table, rows, columns)                 \
-  (table) = gtk_table_new ((rows), (columns), FALSE)
-#define table_attach(table, child, left, right,top, xoptions, yoptions, xpadding, ypadding) \
-  gtk_table_attach (GTK_TABLE (table), (child),                         \
-                    (left), (right), (top), (top + 1),                   \
-                    (xoptions), (yoptions), (xpadding), (ypadding))
-#endif
 
-#if GTK_CHECK_VERSION(3,8,0)   /* gtk >= 3.8 */
 #define scrolled_window_add_with_viewport(container, child)     \
   gtk_container_add (GTK_CONTAINER (container), child)
-#else
-#define scrolled_window_add_with_viewport(container, child)             \
-  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (container), child)
-#endif
 
-#if !GTK_CHECK_VERSION(3,10,0)   /* gtk < 3.10 */
-#define gdk_event_get_event_type(event) ((event)->type)
-#endif
-
-#if GTK_CHECK_VERSION(3,10,0)   /* gtk >= 3.10 */
 #undef GTK_STOCK_DIALOG_WARNING
 #define GTK_STOCK_DIALOG_WARNING "dialog-warning"
 #define gtk_image_new_from_stock gtk_image_new_from_icon_name
-#endif
 
-#if GTK_CHECK_VERSION(3,14,0)   /* gtk >= 3.14 */
 #define set_padding(widget, xpad, ypad)                               \
   do {                                                                \
     if ((xpad) != 0) {                                                \
       gtk_widget_set_margin_start ((widget), (xpad));                 \
       gtk_widget_set_margin_end ((widget), (xpad));                   \
     }                                                                 \
     if ((ypad) != 0) {                                                \
       gtk_widget_set_margin_top ((widget), (ypad));                   \
       gtk_widget_set_margin_bottom ((widget), (ypad));                \
     }                                                                 \
   } while (0)
 #define set_alignment(widget, xalign, yalign)                   \
   do {                                                          \
     if ((xalign) == 0.)                                         \
       gtk_widget_set_halign ((widget), GTK_ALIGN_START);        \
     else if ((xalign) == 1.)                                    \
       gtk_widget_set_halign ((widget), GTK_ALIGN_END);          \
     else                                                        \
       gtk_widget_set_halign ((widget), GTK_ALIGN_CENTER);       \
     if ((yalign) == 0.)                                         \
       gtk_widget_set_valign ((widget), GTK_ALIGN_START);        \
     else if ((xalign) == 1.)                                    \
       gtk_widget_set_valign ((widget), GTK_ALIGN_END);          \
     else                                                        \
       gtk_widget_set_valign ((widget), GTK_ALIGN_CENTER);       \
   } while (0)
-#else  /* gtk < 3.14 */
-#define set_padding(widget, xpad, ypad)                 \
-  gtk_misc_set_padding(GTK_MISC(widget),(xpad),(ypad))
-#define set_alignment(widget, xalign, yalign)                   \
-  gtk_misc_set_alignment(GTK_MISC(widget),(xalign),(yalign))
-#endif
diff --git a/gui.c b/gui.c
index d391305f4f51..3068c97c3ba3 100644
--- a/gui.c
+++ b/gui.c
@@ -36,31 +36,32 @@
  * (eg. which network interfaces should be copied and which should be
  * ignored).
  *
  * =item Running dialog
  *
  * The running dialog is displayed when the P2V process is underway.
  * It mainly displays the virt-v2v debug messages.
  *
  * =back
  *
  * Note that the other major dialog (C<"Configure network ...">) is
  * handled entirely by NetworkManager's L<nm-connection-editor(1)>
  * program and has nothing to do with this code.
  *
- * This file is written in a kind of "pseudo-Gtk" which is backwards compatible
- * from Gtk 3.0 through at least Gtk 3.22.  This is done using a few macros to
- * implement old C<gtk_*> functions or map them to newer functions.
+ * This file is written in a kind of "pseudo-Gtk" that currently targets Gtk
+ * 3.22 exclusively, but may later be extended to a broader Gtk version range.
+ * This is done using a few macros to implement old C<gtk_*> functions or map
+ * them to newer functions.
  */
 
 #include <config.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
 #include <inttypes.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <error.h>
 #include <locale.h>
@@ -81,32 +82,28 @@
 #include "ignore-value.h"
 
 #include "p2v.h"
 
 /* See note about "pseudo-Gtk" above. */
 #include "gui-gtk3-compat.h"
 
 /* Maximum vCPUs and guest memory that we will allow users to set.
  * These limits come from
  * https://access.redhat.com/articles/rhel-kvm-limits
  */
 #define MAX_SUPPORTED_VCPUS 160
 #define MAX_SUPPORTED_MEMORY_MB (UINT64_C (4000 * 1024))
 
-#if GLIB_CHECK_VERSION(2,32,0) && GTK_CHECK_VERSION(3,12,0)   /* glib >= 2.32 && gtk >= 3.12 */
-#define USE_POPOVERS
-#endif
-
 static void create_connection_dialog (struct config *);
 static void create_conversion_dialog (struct config *config,
                                       const char * const *disks,
                                       const char * const *removable);
 static void create_running_dialog (void);
 static void show_connection_dialog (void);
 static void show_conversion_dialog (void);
 static void show_running_dialog (void);
 
 static void set_info_label (void);
 
 /* The connection dialog. */
 static GtkWidget *conn_dlg,
   *server_entry, *port_entry,
@@ -236,32 +233,30 @@ create_connection_dialog (struct config *config)
     gtk_entry_set_text (GTK_ENTRY (username_entry), config->auth.username);
   else
     gtk_entry_set_text (GTK_ENTRY (username_entry), "root");
   table_attach (table, username_entry,
                 1, 2, row, GTK_EXPAND|GTK_FILL, GTK_FILL, 4, 4);
 
   row++;
   password_label = gtk_label_new_with_mnemonic (_("_Password:"));
   table_attach (table, password_label,
                 0, 1, row, GTK_FILL, GTK_FILL, 4, 4);
   set_alignment (password_label, 1., 0.5);
   password_entry = gtk_entry_new ();
   gtk_label_set_mnemonic_widget (GTK_LABEL (password_label), password_entry);
   gtk_entry_set_visibility (GTK_ENTRY (password_entry), FALSE);
-#ifdef GTK_INPUT_PURPOSE_PASSWORD /* guaranteed if gtk >= 3.5.12 */
   gtk_entry_set_input_purpose (GTK_ENTRY (password_entry),
                                GTK_INPUT_PURPOSE_PASSWORD);
-#endif
   if (config->auth.password != NULL)
     gtk_entry_set_text (GTK_ENTRY (password_entry), config->auth.password);
   table_attach (table, password_entry,
                 1, 2, row, GTK_EXPAND|GTK_FILL, GTK_FILL, 4, 4);
 
   row++;
   identity_label = gtk_label_new_with_mnemonic (_("SSH _Identity URL:"));
   table_attach (table, identity_label,
                 0, 1, row, GTK_FILL, GTK_FILL, 4, 4);
   set_alignment (identity_label, 1., 0.5);
   identity_entry = gtk_entry_new ();
   gtk_label_set_mnemonic_widget (GTK_LABEL (identity_label), identity_entry);
   if (config->auth.identity.url != NULL)
     gtk_entry_set_text (GTK_ENTRY (identity_entry), config->auth.identity.url);
@@ -1736,186 +1731,141 @@ get_memory_from_conv_dlg (void)
   else
     return UINT64_C (1024) * 1024 * 1024;
 }
 
 /*----------------------------------------------------------------------*/
 /* Running dialog. */
 
 static gboolean set_log_dir (gpointer remote_dir);
 static gboolean set_status (gpointer msg);
 static gboolean add_v2v_output (gpointer msg);
 static void *start_conversion_thread (void *data);
 static gboolean conversion_error (gpointer user_data);
 static gboolean conversion_finished (gpointer user_data);
 static void cancel_conversion_dialog (GtkWidget *w, gpointer data);
-#ifdef USE_POPOVERS
 static void activate_action (GSimpleAction *action, GVariant *parameter, gpointer user_data);
-#else
-static void shutdown_button_clicked (GtkToolButton *w, gpointer data);
-#endif
 static void shutdown_clicked (GtkWidget *w, gpointer data);
 static void reboot_clicked (GtkWidget *w, gpointer data);
 static gboolean close_running_dialog (GtkWidget *w, GdkEvent *event, gpointer data);
 
-#ifdef USE_POPOVERS
 static const GActionEntry shutdown_actions[] = {
   { .name = "shutdown", .activate = activate_action },
   { .name = "reboot", .activate = activate_action },
 };
-#endif
 
 /**
  * Create the running dialog.
  *
  * This creates the dialog, but it is not displayed.  See
  * C<show_running_dialog>.
  */
 static void
 create_running_dialog (void)
 {
   size_t i;
   static const char *tags[16] =
     { "black", "maroon", "green", "olive", "navy", "purple", "teal", "silver",
       "gray", "red", "lime", "yellow", "blue", "fuchsia", "cyan", "white" };
   GtkTextBuffer *buf;
-#ifdef USE_POPOVERS
   GMenu *shutdown_menu;
   GSimpleActionGroup *shutdown_group;
-#else
-  GtkWidget *shutdown_menu;
-  GtkWidget *shutdown_menu_item;
-  GtkWidget *reboot_menu_item;
-#endif
 
   run_dlg = gtk_dialog_new ();
   gtk_window_set_title (GTK_WINDOW (run_dlg), g_get_prgname ());
   gtk_window_set_resizable (GTK_WINDOW (run_dlg), FALSE);
 
   /* The main dialog area. */
   v2v_output_sw = gtk_scrolled_window_new (NULL, NULL);
   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (v2v_output_sw),
                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
   gtk_widget_set_size_request (v2v_output_sw, 700, 400);
 
   v2v_output = gtk_text_view_new ();
   gtk_text_view_set_editable (GTK_TEXT_VIEW (v2v_output), FALSE);
   gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (v2v_output), GTK_WRAP_CHAR);
 
   buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (v2v_output));
   for (i = 0; i < 16; ++i) {
     CLEANUP_FREE char *tag_name;
 
     if (asprintf (&tag_name, "tag_%s", tags[i]) == -1)
       error (EXIT_FAILURE, errno, "asprintf");
     v2v_output_tags[i] =
       gtk_text_buffer_create_tag (buf, tag_name, "foreground", tags[i], NULL);
   }
 
-#if GTK_CHECK_VERSION(3,16,0)   /* gtk >= 3.16 */
   /* XXX This only sets the "CSS" style.  It's not clear how to set
    * the particular font.  However (by accident) this does at least
    * set the widget to use a monospace font.
    */
   GtkStyleContext *context = gtk_widget_get_style_context (v2v_output);
   gtk_style_context_add_class (context, "monospace");
-#else
-  PangoFontDescription *font;
-  font = pango_font_description_from_string ("Monospace 11");
-  gtk_widget_override_font (v2v_output, font);
-  pango_font_description_free (font);
-#endif
 
   log_label = gtk_label_new (NULL);
   set_alignment (log_label, 0., 0.5);
   set_padding (log_label, 10, 10);
   set_log_dir (NULL);
   status_label = gtk_label_new (NULL);
   set_alignment (status_label, 0., 0.5);
   set_padding (status_label, 10, 10);
 
   gtk_container_add (GTK_CONTAINER (v2v_output_sw), v2v_output);
 
   gtk_box_pack_start
     (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (run_dlg))),
      v2v_output_sw, TRUE, TRUE, 0);
   gtk_box_pack_start
     (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (run_dlg))),
      log_label, TRUE, TRUE, 0);
   gtk_box_pack_start
     (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (run_dlg))),
      status_label, TRUE, TRUE, 0);
 
   /* Shutdown popup menu. */
-#ifdef USE_POPOVERS
   shutdown_menu = g_menu_new ();
   g_menu_append (shutdown_menu, _("_Shutdown"), "shutdown.shutdown");
   g_menu_append (shutdown_menu, _("_Reboot"), "shutdown.reboot");
 
   shutdown_group = g_simple_action_group_new ();
   g_action_map_add_action_entries (G_ACTION_MAP (shutdown_group),
                                    shutdown_actions,
                                    G_N_ELEMENTS (shutdown_actions), NULL);
-#else
-  shutdown_menu = gtk_menu_new ();
-  shutdown_menu_item = gtk_menu_item_new_with_mnemonic (_("_Shutdown"));
-  gtk_menu_shell_append (GTK_MENU_SHELL (shutdown_menu), shutdown_menu_item);
-  gtk_widget_show (shutdown_menu_item);
-  reboot_menu_item = gtk_menu_item_new_with_mnemonic (_("_Reboot"));
-  gtk_menu_shell_append (GTK_MENU_SHELL (shutdown_menu), reboot_menu_item);
-  gtk_widget_show (reboot_menu_item);
-#endif
 
   /* Buttons. */
   gtk_dialog_add_buttons (GTK_DIALOG (run_dlg),
                           _("_Cancel conversion ..."), 1,
                           NULL);
   cancel_button = gtk_dialog_get_widget_for_response (GTK_DIALOG (run_dlg), 1);
   gtk_widget_set_sensitive (cancel_button, FALSE);
-#ifdef USE_POPOVERS
   shutdown_button = gtk_menu_button_new ();
   gtk_button_set_use_underline (GTK_BUTTON (shutdown_button), TRUE);
   gtk_button_set_label (GTK_BUTTON (shutdown_button), _("_Shutdown ..."));
   gtk_button_set_always_show_image (GTK_BUTTON (shutdown_button), TRUE);
   gtk_widget_insert_action_group (shutdown_button, "shutdown",
                                   G_ACTION_GROUP (shutdown_group));
   gtk_menu_button_set_use_popover (GTK_MENU_BUTTON (shutdown_button), TRUE);
   gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (shutdown_button),
                                   G_MENU_MODEL (shutdown_menu));
-#else
-  shutdown_button = GTK_WIDGET (gtk_menu_tool_button_new (NULL,
-                                                          _("_Shutdown ...")));
-  gtk_tool_button_set_use_underline (GTK_TOOL_BUTTON (shutdown_button), TRUE);
-  gtk_menu_tool_button_set_menu (GTK_MENU_TOOL_BUTTON (shutdown_button),
-                                 shutdown_menu);
-#endif
   gtk_widget_set_sensitive (shutdown_button, FALSE);
   gtk_dialog_add_action_widget (GTK_DIALOG (run_dlg), shutdown_button, 2);
 
   /* Signals. */
   g_signal_connect_swapped (G_OBJECT (run_dlg), "delete_event",
                             G_CALLBACK (close_running_dialog), NULL);
   g_signal_connect_swapped (G_OBJECT (run_dlg), "destroy",
                             G_CALLBACK (gtk_main_quit), NULL);
   g_signal_connect (G_OBJECT (cancel_button), "clicked",
                     G_CALLBACK (cancel_conversion_dialog), NULL);
-#ifndef USE_POPOVERS
-  g_signal_connect (G_OBJECT (shutdown_button), "clicked",
-                    G_CALLBACK (shutdown_button_clicked), shutdown_menu);
-  g_signal_connect (G_OBJECT (shutdown_menu_item), "activate",
-                    G_CALLBACK (shutdown_clicked), NULL);
-  g_signal_connect (G_OBJECT (reboot_menu_item), "activate",
-                    G_CALLBACK (reboot_clicked), NULL);
-#endif
 }
 
 /**
  * Hide all other dialogs and show the running dialog.
  */
 static void
 show_running_dialog (void)
 {
   /* Hide the other dialogs. */
   gtk_widget_hide (conn_dlg);
   gtk_widget_hide (conv_dlg);
 
   /* Show the running dialog. */
   gtk_widget_show_all (run_dlg);
@@ -2356,48 +2306,37 @@ cancel_conversion_dialog (GtkWidget *w, gpointer data)
                                 GTK_DIALOG_DESTROY_WITH_PARENT,
                                 GTK_MESSAGE_QUESTION,
                                 GTK_BUTTONS_YES_NO,
                                 _("Really cancel the conversion? "
                                   "To convert this machine you will need to "
                                   "re-run the conversion from the beginning."));
   gtk_window_set_title (GTK_WINDOW (dlg), _("Cancel the conversion"));
   if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_YES)
     /* This makes start_conversion return an error (eventually). */
     cancel_conversion ();
 
   gtk_widget_destroy (dlg);
 }
 
-#ifdef USE_POPOVERS
 static void
 activate_action (GSimpleAction *action, GVariant *parameter, gpointer user_data)
 {
   const char *action_name = g_action_get_name (G_ACTION (action));
   if (STREQ (action_name, "shutdown"))
     shutdown_clicked (NULL, user_data);
   else if (STREQ (action_name, "reboot"))
     reboot_clicked (NULL, user_data);
 }
-#else
-static void
-shutdown_button_clicked (GtkToolButton *w, gpointer data)
-{
-  GtkMenu *menu = data;
-
-  gtk_menu_popup (menu, NULL, NULL, NULL, NULL, 1,
-                  gtk_get_current_event_time ());
-}
-#endif
 
 static void
 shutdown_clicked (GtkWidget *w, gpointer data)
 {
   if (!is_iso_environment)
     return;
 
   sync ();
   sleep (2);
   ignore_value (system ("/sbin/poweroff"));
 }
 
 static void
 reboot_clicked (GtkWidget *w, gpointer data)


More information about the Libguestfs mailing list