[gnome-builder/wip/chergert/shellcmd] shellcmd: add command editing to preferences



commit 0eee5981ce6b8524de57819af3c2bb9d0ffea19e
Author: Christian Hergert <chergert redhat com>
Date:   Fri Aug 9 17:39:03 2019 -0700

    shellcmd: add command editing to preferences

 src/libide/themes/themes/shared.css                |   4 +
 src/plugins/shellcmd/gbp-shellcmd-command-editor.c | 171 +++++++++++++++++++
 src/plugins/shellcmd/gbp-shellcmd-command-editor.h |  37 +++++
 .../shellcmd/gbp-shellcmd-command-editor.ui        | 110 ++++++++++++
 src/plugins/shellcmd/gbp-shellcmd-command-model.c  |  14 ++
 src/plugins/shellcmd/gbp-shellcmd-command-model.h  |   2 +
 src/plugins/shellcmd/gbp-shellcmd-command-row.c    |  93 +++++++++++
 src/plugins/shellcmd/gbp-shellcmd-command-row.h    |  36 ++++
 src/plugins/shellcmd/gbp-shellcmd-command-row.ui   |  31 ++++
 src/plugins/shellcmd/gbp-shellcmd-list.c           | 185 +++++++++++++++++++++
 src/plugins/shellcmd/gbp-shellcmd-list.h           |  35 ++++
 .../shellcmd/gbp-shellcmd-preferences-addin.c      | 173 +++++++++++++++++++
 .../shellcmd/gbp-shellcmd-preferences-addin.h      |  31 ++++
 src/plugins/shellcmd/meson.build                   |   4 +
 src/plugins/shellcmd/shellcmd-plugin.c             |   4 +
 src/plugins/shellcmd/shellcmd.gresource.xml        |   2 +
 16 files changed, 932 insertions(+)
---
diff --git a/src/libide/themes/themes/shared.css b/src/libide/themes/themes/shared.css
index fcf9ea9c9..c28b2d76d 100644
--- a/src/libide/themes/themes/shared.css
+++ b/src/libide/themes/themes/shared.css
@@ -149,3 +149,7 @@ dzlstacklist .stack-header {
   background-color: @content_view_bg;
   background-image: none;
 }
+
+row:selected label.keycap {
+  color: @theme_fg_color;
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-editor.c 
b/src/plugins/shellcmd/gbp-shellcmd-command-editor.c
new file mode 100644
index 000000000..17c32c6a7
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-editor.c
@@ -0,0 +1,171 @@
+/* gbp-shellcmd-command-editor.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-shellcmd-command-editor"
+
+#include "config.h"
+
+#include <dazzle.h>
+#include <glib/gi18n.h>
+#include <libide-gui.h>
+
+#include "gbp-shellcmd-command-editor.h"
+
+struct _GbpShellcmdCommandEditor
+{
+  GtkBin                parent_instance;
+
+  DzlBindingGroup      *bindings;
+
+  IdeEnvironmentEditor *environment;
+  DzlShortcutLabel     *shortcut;
+  GtkEntry             *title;
+  GtkEntry             *command;
+  GtkButton            *change;
+};
+
+G_DEFINE_TYPE (GbpShellcmdCommandEditor, gbp_shellcmd_command_editor, GTK_TYPE_BIN)
+
+static void
+on_dialog_response_cb (GbpShellcmdCommandEditor *self,
+                       gint                      response,
+                       DzlShortcutAccelDialog   *dialog)
+{
+  g_assert (GBP_IS_SHELLCMD_COMMAND_EDITOR (self));
+  g_assert (DZL_IS_SHORTCUT_ACCEL_DIALOG (dialog));
+
+
+  if (response == GTK_RESPONSE_ACCEPT)
+    {
+      GbpShellcmdCommand *command = GBP_SHELLCMD_COMMAND (dzl_binding_group_get_source (self->bindings));
+
+      if (command != NULL)
+        {
+          g_autofree gchar *accel = dzl_shortcut_accel_dialog_get_accelerator (dialog);
+          gbp_shellcmd_command_set_shortcut (command, accel);
+        }
+    }
+
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+
+static void
+on_chagne_shortcut_cb (GbpShellcmdCommandEditor *self,
+                       GtkButton                *button)
+{
+  GbpShellcmdCommand *command;
+  g_autofree gchar *title = NULL;
+  GtkWidget *dialog;
+
+  g_assert (GBP_IS_SHELLCMD_COMMAND_EDITOR (self));
+  g_assert (GTK_IS_BUTTON (button));
+
+  command = GBP_SHELLCMD_COMMAND (dzl_binding_group_get_source (self->bindings));
+
+  if (command == NULL)
+    return;
+
+  title = ide_command_get_title (IDE_COMMAND (command));
+
+  dialog = g_object_new (DZL_TYPE_SHORTCUT_ACCEL_DIALOG,
+                         "modal", TRUE,
+                         "shortcut-title", title,
+                         "title", _("Change Shortcut"),
+                         "transient-for", gtk_widget_get_toplevel (GTK_WIDGET (self)),
+                         "use-header-bar", TRUE,
+                         NULL);
+
+  g_signal_connect_object (dialog,
+                           "response",
+                           G_CALLBACK (on_dialog_response_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  gtk_window_present (GTK_WINDOW (dialog));
+}
+
+static void
+gbp_shellcmd_command_editor_destroy (GtkWidget *widget)
+{
+  GbpShellcmdCommandEditor *self = (GbpShellcmdCommandEditor *)widget;
+
+  if (self->bindings != NULL)
+    {
+      dzl_binding_group_set_source (self->bindings, NULL);
+      g_clear_object (&self->bindings);
+    }
+
+  GTK_WIDGET_CLASS (gbp_shellcmd_command_editor_parent_class)->destroy (widget);
+}
+
+static void
+gbp_shellcmd_command_editor_class_init (GbpShellcmdCommandEditorClass *klass)
+{
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  widget_class->destroy = gbp_shellcmd_command_editor_destroy;
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/plugins/shellcmd/gbp-shellcmd-command-editor.ui");
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandEditor, change);
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandEditor, command);
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandEditor, environment);
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandEditor, shortcut);
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandEditor, title);
+
+  g_type_ensure (IDE_TYPE_ENVIRONMENT_EDITOR);
+}
+
+static void
+gbp_shellcmd_command_editor_init (GbpShellcmdCommandEditor *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  self->bindings = dzl_binding_group_new ();
+
+  dzl_binding_group_bind (self->bindings, "title", self->title, "text", G_BINDING_BIDIRECTIONAL);
+  dzl_binding_group_bind (self->bindings, "command", self->command, "text", G_BINDING_BIDIRECTIONAL);
+  dzl_binding_group_bind (self->bindings, "shortcut", self->shortcut, "accelerator", 
G_BINDING_BIDIRECTIONAL);
+  dzl_binding_group_bind (self->bindings, "environment", self->environment, "environment", 
G_BINDING_SYNC_CREATE);
+
+  g_signal_connect_object (self->change,
+                           "clicked",
+                           G_CALLBACK (on_chagne_shortcut_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+}
+
+void
+gbp_shellcmd_command_editor_set_command (GbpShellcmdCommandEditor *self,
+                                         GbpShellcmdCommand       *command)
+{
+  g_return_if_fail (GBP_IS_SHELLCMD_COMMAND_EDITOR (self));
+  g_return_if_fail (!command || GBP_IS_SHELLCMD_COMMAND (command));
+
+  dzl_binding_group_set_source (self->bindings, command);
+
+  if (command != NULL)
+    gtk_widget_grab_focus (GTK_WIDGET (self->title));
+}
+
+GtkWidget *
+gbp_shellcmd_command_editor_new (void)
+{
+  return g_object_new (GBP_TYPE_SHELLCMD_COMMAND_EDITOR, NULL);
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-editor.h 
b/src/plugins/shellcmd/gbp-shellcmd-command-editor.h
new file mode 100644
index 000000000..4492ebf17
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-editor.h
@@ -0,0 +1,37 @@
+/* gbp-shellcmd-command-editor.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "gbp-shellcmd-command.h"
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SHELLCMD_COMMAND_EDITOR (gbp_shellcmd_command_editor_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpShellcmdCommandEditor, gbp_shellcmd_command_editor, GBP, SHELLCMD_COMMAND_EDITOR, 
GtkBin)
+
+GtkWidget *gbp_shellcmd_command_editor_new         (void);
+void       gbp_shellcmd_command_editor_set_command (GbpShellcmdCommandEditor *self,
+                                                    GbpShellcmdCommand       *command);
+
+G_END_DECLS
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-editor.ui 
b/src/plugins/shellcmd/gbp-shellcmd-command-editor.ui
new file mode 100644
index 000000000..7216916dc
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-editor.ui
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GbpShellcmdCommandEditor" parent="GtkBin">
+    <child>
+      <object class="GtkBox">
+        <property name="orientation">vertical</property>
+        <property name="spacing">6</property>
+        <property name="visible">true</property>
+        <child>
+          <object class="GtkLabel">
+            <property name="label" translatable="yes">Title</property>
+            <property name="visible">true</property>
+            <property name="xalign">0</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkEntry" id="title">
+            <property name="visible">true</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel">
+            <property name="label" translatable="yes">Shell Command</property>
+            <property name="margin-top">10</property>
+            <property name="visible">true</property>
+            <property name="xalign">0</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkEntry" id="command">
+            <property name="visible">true</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel">
+            <property name="label" translatable="yes">The command will be executed using a shell like 
“/bin/sh -c". You may use variable expansion like “$SHELL”.</property>
+            <property name="visible">true</property>
+            <property name="wrap">true</property>
+            <property name="width-chars">10</property>
+            <property name="max-width-chars">10</property>
+            <property name="xalign">0</property>
+            <attributes>
+              <attribute name="scale" value="0.8333"/>
+            </attributes>
+            <style>
+              <class name="dim-label"/>
+            </style>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel">
+            <property name="label" translatable="yes">Keyboard Shortcut</property>
+            <property name="margin-top">10</property>
+            <property name="visible">true</property>
+            <property name="xalign">0</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="orientation">horizontal</property>
+            <property name="spacing">12</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkButton" id="change">
+                <property name="label" translatable="yes">Change</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+            <child>
+              <object class="DzlShortcutLabel" id="shortcut">
+                <property name="valign">start</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel">
+            <property name="label" translatable="yes">Environment</property>
+            <property name="margin-top">10</property>
+            <property name="visible">true</property>
+            <property name="xalign">0</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkFrame">
+            <property name="shadow-type">in</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="IdeEnvironmentEditor" id="environment">
+                <property name="visible">true</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkButton">
+            <property name="halign">end</property>
+            <property name="label" translatable="yes">Delete Command</property>
+            <property name="visible">true</property>
+            <property name="margin-top">12</property>
+            <style>
+              <class name="destructive-action"/>
+            </style>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-model.c 
b/src/plugins/shellcmd/gbp-shellcmd-command-model.c
index 9ff5e0d0d..637f19329 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-command-model.c
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-model.c
@@ -249,3 +249,17 @@ gbp_shellcmd_command_model_query (GbpShellcmdCommandModel *self,
         }
     }
 }
+
+void
+gbp_shellcmd_command_model_add (GbpShellcmdCommandModel *self,
+                                GbpShellcmdCommand      *command)
+{
+  guint position;
+
+  g_return_if_fail (GBP_IS_SHELLCMD_COMMAND_MODEL (self));
+  g_return_if_fail (GBP_IS_SHELLCMD_COMMAND (command));
+
+  position = self->items->len;
+  g_ptr_array_add (self->items, g_object_ref (command));
+  g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-model.h 
b/src/plugins/shellcmd/gbp-shellcmd-command-model.h
index 897cfd424..8ed718dee 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-command-model.h
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-model.h
@@ -33,6 +33,8 @@ G_DECLARE_FINAL_TYPE (GbpShellcmdCommandModel, gbp_shellcmd_command_model, GBP,
 GbpShellcmdCommandModel *gbp_shellcmd_command_model_new         (void);
 GbpShellcmdCommand      *gbp_shellcmd_command_model_get_command (GbpShellcmdCommandModel  *self,
                                                                  const gchar              *command_id);
+void                     gbp_shellcmd_command_model_add         (GbpShellcmdCommandModel  *self,
+                                                                 GbpShellcmdCommand       *model);
 void                     gbp_shellcmd_command_model_query       (GbpShellcmdCommandModel  *self,
                                                                  GPtrArray                *items,
                                                                  const gchar              *typed_text);
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-row.c b/src/plugins/shellcmd/gbp-shellcmd-command-row.c
new file mode 100644
index 000000000..fd125c80e
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-row.c
@@ -0,0 +1,93 @@
+/* gbp-shellcmd-command-row.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-shellcmd-command-row"
+
+#include "config.h"
+
+#include "gbp-shellcmd-command-row.h"
+
+struct _GbpShellcmdCommandRow
+{
+  GtkListBoxRow       parent_instance;
+
+  gchar              *id;
+  GbpShellcmdCommand *command;
+
+  GtkLabel           *title;
+  DzlShortcutLabel   *chord;
+};
+
+G_DEFINE_TYPE (GbpShellcmdCommandRow, gbp_shellcmd_command_row, GTK_TYPE_LIST_BOX_ROW)
+
+static void
+gbp_shellcmd_command_row_finalize (GObject *object)
+{
+  GbpShellcmdCommandRow *self = (GbpShellcmdCommandRow *)object;
+
+  g_clear_pointer (&self->id, g_free);
+  g_clear_object (&self->command);
+
+  G_OBJECT_CLASS (gbp_shellcmd_command_row_parent_class)->finalize (object);
+}
+
+static void
+gbp_shellcmd_command_row_class_init (GbpShellcmdCommandRowClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->finalize = gbp_shellcmd_command_row_finalize;
+
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/plugins/shellcmd/gbp-shellcmd-command-row.ui");
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandRow, chord);
+  gtk_widget_class_bind_template_child (widget_class, GbpShellcmdCommandRow, title);
+}
+
+static void
+gbp_shellcmd_command_row_init (GbpShellcmdCommandRow *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+GtkWidget *
+gbp_shellcmd_command_row_new (GbpShellcmdCommand *command)
+{
+  GbpShellcmdCommandRow *self;
+
+  self = g_object_new (GBP_TYPE_SHELLCMD_COMMAND_ROW,
+                       "visible", TRUE,
+                       NULL);
+  self->id = g_strdup (gbp_shellcmd_command_get_id (command));
+  g_set_object (&self->command, command);
+
+  g_object_bind_property (command, "title", self->title, "label", G_BINDING_SYNC_CREATE);
+  g_object_bind_property (command, "shortcut", self->chord, "accelerator", G_BINDING_SYNC_CREATE);
+
+  return GTK_WIDGET (self);
+}
+
+GbpShellcmdCommand *
+gbp_shellcmd_command_row_get_command (GbpShellcmdCommandRow *self)
+{
+  g_return_val_if_fail (GBP_IS_SHELLCMD_COMMAND_ROW (self), NULL);
+
+  return self->command;
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-row.h b/src/plugins/shellcmd/gbp-shellcmd-command-row.h
new file mode 100644
index 000000000..7120d1543
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-row.h
@@ -0,0 +1,36 @@
+/* gbp-shellcmd-command-row.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "gbp-shellcmd-command.h"
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SHELLCMD_COMMAND_ROW (gbp_shellcmd_command_row_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpShellcmdCommandRow, gbp_shellcmd_command_row, GBP, SHELLCMD_COMMAND_ROW, 
GtkListBoxRow)
+
+GtkWidget          *gbp_shellcmd_command_row_new         (GbpShellcmdCommand    *commad);
+GbpShellcmdCommand *gbp_shellcmd_command_row_get_command (GbpShellcmdCommandRow *self);
+
+G_END_DECLS
diff --git a/src/plugins/shellcmd/gbp-shellcmd-command-row.ui 
b/src/plugins/shellcmd/gbp-shellcmd-command-row.ui
new file mode 100644
index 000000000..3d192465d
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-command-row.ui
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GbpShellcmdCommandRow" parent="GtkListBoxRow">
+    <child>
+      <object class="GtkBox">
+        <property name="orientation">horizontal</property>
+        <property name="spacing">12</property>
+        <property name="visible">true</property>
+        <child>
+          <object class="GtkLabel" id="title">
+            <property name="ellipsize">end</property>
+            <property name="visible">true</property>
+            <property name="hexpand">true</property>
+            <property name="xalign">0.0</property>
+          </object>
+        </child>
+        <child>
+          <object class="DzlShortcutLabel" id="chord">
+            <property name="visible">true</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkImage">
+            <property name="icon-name">pan-end-symbolic</property>
+            <property name="visible">true</property>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/plugins/shellcmd/gbp-shellcmd-list.c b/src/plugins/shellcmd/gbp-shellcmd-list.c
new file mode 100644
index 000000000..f3b73e7d4
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-list.c
@@ -0,0 +1,185 @@
+/* gbp-shellcmd-list.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-shellcmd-list"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include "gbp-shellcmd-command.h"
+#include "gbp-shellcmd-command-row.h"
+#include "gbp-shellcmd-list.h"
+
+struct _GbpShellcmdList
+{
+  GtkFrame                 parent_instance;
+
+  GtkListBox              *list;
+  GtkBox                  *box;
+  GtkListBoxRow           *add_row;
+
+  GbpShellcmdCommandModel *model;
+};
+
+enum {
+  COMMAND_SELECTED,
+  N_SIGNALS
+};
+
+static guint signals [N_SIGNALS];
+
+G_DEFINE_TYPE (GbpShellcmdList, gbp_shellcmd_list, GTK_TYPE_FRAME)
+
+static void
+gbp_shellcmd_list_class_init (GbpShellcmdListClass *klass)
+{
+  signals [COMMAND_SELECTED] =
+    g_signal_new ("command-selected",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0,
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE, 1, GBP_TYPE_SHELLCMD_COMMAND);
+}
+
+static void
+gbp_shellcmd_list_init (GbpShellcmdList *self)
+{
+}
+
+static void
+on_row_selected_cb (GbpShellcmdList       *self,
+                    GbpShellcmdCommandRow *row,
+                    GtkListBox            *list_box)
+{
+  GbpShellcmdCommand *command;
+
+  g_assert (GBP_IS_SHELLCMD_LIST (self));
+  g_assert (GBP_IS_SHELLCMD_COMMAND_ROW (row));
+  g_assert (GTK_IS_LIST_BOX (list_box));
+
+  command = gbp_shellcmd_command_row_get_command (row);
+
+  if (command != NULL)
+    g_signal_emit (self, signals [COMMAND_SELECTED], 0, command);
+}
+
+static GtkWidget *
+create_row_func (gpointer item,
+                 gpointer user_data)
+{
+  return gbp_shellcmd_command_row_new (item);
+}
+
+static void
+on_add_new_row_cb (GbpShellcmdList *self,
+                   GtkListBoxRow   *row,
+                   GtkListBox      *list_box)
+{
+  g_autoptr(GbpShellcmdCommand) command = NULL;
+  g_autofree gchar *id = NULL;
+  guint nth;
+
+  g_assert (GBP_IS_SHELLCMD_LIST (self));
+  g_assert (GTK_IS_LIST_BOX_ROW (row));
+  g_assert (GTK_IS_LIST_BOX (list_box));
+
+  if (self->model == NULL)
+    return;
+
+  id = g_uuid_string_random ();
+  nth = g_list_model_get_n_items (G_LIST_MODEL (self->model));
+
+  command = g_object_new (GBP_TYPE_SHELLCMD_COMMAND,
+                          "id", id,
+                          "title", _("New command"),
+                          "command", "",
+                          NULL);
+  gbp_shellcmd_command_model_add (self->model, command);
+
+  /* Now select the new row */
+  row = gtk_list_box_get_row_at_index (self->list, nth);
+  gtk_list_box_select_row (self->list, row);
+}
+
+GtkWidget *
+gbp_shellcmd_list_new (GbpShellcmdCommandModel *model)
+{
+  GbpShellcmdList *self;
+  GtkWidget *list2;
+  GtkWidget *placeholder;
+
+  g_return_val_if_fail (GBP_IS_SHELLCMD_COMMAND_MODEL (model), NULL);
+
+  placeholder = g_object_new (GTK_TYPE_LABEL,
+                              "margin", 12,
+                              "label", _("Click + to add an external command"),
+                              "visible", TRUE,
+                              NULL);
+
+  self = g_object_new (GBP_TYPE_SHELLCMD_LIST,
+                       "shadow-type", GTK_SHADOW_IN,
+                       "visible", TRUE,
+                       NULL);
+  self->model = g_object_ref (model);
+
+  self->box = g_object_new (GTK_TYPE_BOX,
+                            "orientation", GTK_ORIENTATION_VERTICAL,
+                            "visible", TRUE,
+                            NULL);
+  gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->box));
+
+  self->list = g_object_new (GTK_TYPE_LIST_BOX,
+                             "visible", TRUE,
+                             NULL);
+  gtk_list_box_set_placeholder (self->list, placeholder);
+  gtk_list_box_bind_model (self->list,
+                           G_LIST_MODEL (model),
+                           create_row_func, NULL, NULL);
+  g_signal_connect_object (self->list,
+                           "row-selected",
+                           G_CALLBACK (on_row_selected_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+  gtk_container_add (GTK_CONTAINER (self->box), GTK_WIDGET (self->list));
+
+  list2 = g_object_new (GTK_TYPE_LIST_BOX,
+                        "selection-mode", GTK_SELECTION_NONE,
+                        "visible", TRUE,
+                        NULL);
+  g_signal_connect_object (list2,
+                           "row-activated",
+                           G_CALLBACK (on_add_new_row_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+  gtk_container_add (GTK_CONTAINER (self->box), GTK_WIDGET (list2));
+
+  self->add_row = g_object_new (GTK_TYPE_LIST_BOX_ROW,
+                                "child", g_object_new (GTK_TYPE_IMAGE,
+                                                       "icon-name", "list-add-symbolic",
+                                                       "visible", TRUE,
+                                                       NULL),
+                                "visible", TRUE,
+                                NULL);
+  gtk_container_add (GTK_CONTAINER (list2), GTK_WIDGET (self->add_row));
+
+  return GTK_WIDGET (g_steal_pointer (&self));
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-list.h b/src/plugins/shellcmd/gbp-shellcmd-list.h
new file mode 100644
index 000000000..2ac4cf91c
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-list.h
@@ -0,0 +1,35 @@
+/* gbp-shellcmd-list.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+#include "gbp-shellcmd-command-model.h"
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SHELLCMD_LIST (gbp_shellcmd_list_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpShellcmdList, gbp_shellcmd_list, GBP, SHELLCMD_LIST, GtkFrame)
+
+GtkWidget *gbp_shellcmd_list_new (GbpShellcmdCommandModel *model);
+
+G_END_DECLS
diff --git a/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c 
b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
new file mode 100644
index 000000000..1f5d744b2
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.c
@@ -0,0 +1,173 @@
+/* gbp-shellcmd-preferences-addin.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-shellcmd-preferences-addin"
+
+#include "config.h"
+
+#include <dazzle.h>
+#include <glib/gi18n.h>
+#include <libide-gui.h>
+
+#include "gbp-shellcmd-application-addin.h"
+#include "gbp-shellcmd-command-editor.h"
+#include "gbp-shellcmd-command-model.h"
+#include "gbp-shellcmd-command-row.h"
+#include "gbp-shellcmd-list.h"
+#include "gbp-shellcmd-preferences-addin.h"
+
+struct _GbpShellcmdPreferencesAddin
+{
+  GObject parent_instance;
+
+  GbpShellcmdCommandEditor *editor;
+};
+
+static GbpShellcmdCommandModel *
+get_model (void)
+{
+  GbpShellcmdApplicationAddin *app_addin;
+  GbpShellcmdCommandModel *model;
+
+  app_addin = ide_application_find_addin_by_module_name (NULL, "shellcmd");
+  g_assert (GBP_IS_SHELLCMD_APPLICATION_ADDIN (app_addin));
+
+  model = gbp_shellcmd_application_addin_get_model (app_addin);
+  g_assert (GBP_IS_SHELLCMD_COMMAND_MODEL (model));
+
+  return model;
+}
+
+static void
+set_editor_command (GbpShellcmdCommandRow    *row,
+                    GbpShellcmdCommandEditor *editor)
+{
+  GbpShellcmdCommand *command;
+
+  g_assert (GBP_IS_SHELLCMD_COMMAND_ROW (row));
+  g_assert (GBP_IS_SHELLCMD_COMMAND_EDITOR (editor));
+
+  command = gbp_shellcmd_command_row_get_command (row);
+  gbp_shellcmd_command_editor_set_command (editor, command);
+}
+
+static void
+add_new_command_cb (GbpShellcmdPreferencesAddin *self,
+                    GtkListBoxRow               *row)
+{
+  g_assert (GBP_IS_SHELLCMD_PREFERENCES_ADDIN (self));
+
+}
+
+static void
+on_command_selected_cb (GbpShellcmdPreferencesAddin *self,
+                        GbpShellcmdCommand          *command,
+                        GbpShellcmdList             *list)
+{
+  g_autoptr(GHashTable) map = NULL;
+  GtkWidget *preferences;
+
+  g_assert (GBP_IS_SHELLCMD_PREFERENCES_ADDIN (self));
+  g_assert (GBP_IS_SHELLCMD_COMMAND (command));
+  g_assert (GBP_IS_SHELLCMD_LIST (list));
+
+  if (!(preferences = gtk_widget_get_ancestor (GTK_WIDGET (list), DZL_TYPE_PREFERENCES)))
+    return;
+
+  map = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, g_free);
+  g_hash_table_insert (map, (gchar *)"{id}", g_strdup (gbp_shellcmd_command_get_id (command)));
+  dzl_preferences_set_page (DZL_PREFERENCES (preferences), "shellcmd.id", map);
+
+  gbp_shellcmd_command_editor_set_command (self->editor, command);
+}
+
+static void
+gbp_shellcmd_preferences_addin_load (IdePreferencesAddin *addin,
+                                     DzlPreferences      *prefs)
+{
+  GbpShellcmdPreferencesAddin *self = (GbpShellcmdPreferencesAddin *)addin;
+  GbpShellcmdCommandModel *model;
+  GtkWidget *list;
+  GtkWidget *parent;
+  GtkWidget *box;
+  GtkWidget *editor;
+  guint n_items;
+
+  g_assert (GBP_IS_SHELLCMD_PREFERENCES_ADDIN (self));
+  g_assert (DZL_IS_PREFERENCES (prefs));
+
+  dzl_preferences_add_page (prefs, "shellcmd", _("External Commands"), 650);
+  dzl_preferences_add_group (prefs, "shellcmd", "commands", _("External Commands"), 0);
+
+  list = gbp_shellcmd_list_new (get_model ());
+  g_signal_connect_object (list,
+                           "command-selected",
+                           G_CALLBACK (on_command_selected_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+  dzl_preferences_add_custom (prefs, "shellcmd", "commands", list, NULL, 0);
+
+  dzl_preferences_add_page (prefs, "shellcmd.id", NULL, 0);
+  dzl_preferences_add_group (prefs, "shellcmd.id", "basic", _("Command"), 0);
+
+  self->editor = g_object_new (GBP_TYPE_SHELLCMD_COMMAND_EDITOR,
+                               "visible", TRUE,
+                               NULL);
+  g_signal_connect (self->editor,
+                    "destroy",
+                    G_CALLBACK (gtk_widget_destroyed),
+                    &self->editor);
+  dzl_preferences_add_custom (prefs, "shellcmd.id", "basic", GTK_WIDGET (self->editor), NULL, 0);
+}
+
+static void
+gbp_shellcmd_preferences_addin_unload (IdePreferencesAddin *addin,
+                                       DzlPreferences      *prefs)
+{
+  GbpShellcmdPreferencesAddin *self = (GbpShellcmdPreferencesAddin *)addin;
+
+  g_assert (GBP_IS_SHELLCMD_PREFERENCES_ADDIN (self));
+  g_assert (DZL_IS_PREFERENCES (prefs));
+
+  if (self->editor != NULL)
+    gtk_widget_destroy (GTK_WIDGET (self->editor));
+
+  g_assert (self->editor == NULL);
+}
+
+static void
+preferences_addin_iface_init (IdePreferencesAddinInterface *iface)
+{
+  iface->load = gbp_shellcmd_preferences_addin_load;
+  iface->unload = gbp_shellcmd_preferences_addin_unload;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpShellcmdPreferencesAddin, gbp_shellcmd_preferences_addin, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_PREFERENCES_ADDIN, preferences_addin_iface_init))
+
+static void
+gbp_shellcmd_preferences_addin_class_init (GbpShellcmdPreferencesAddinClass *klass)
+{
+}
+
+static void
+gbp_shellcmd_preferences_addin_init (GbpShellcmdPreferencesAddin *self)
+{
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.h 
b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.h
new file mode 100644
index 000000000..6041fa0c1
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-preferences-addin.h
@@ -0,0 +1,31 @@
+/* gbp-shellcmd-preferences-addin.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SHELLCMD_PREFERENCES_ADDIN (gbp_shellcmd_preferences_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpShellcmdPreferencesAddin, gbp_shellcmd_preferences_addin, GBP, 
SHELLCMD_PREFERENCES_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/shellcmd/meson.build b/src/plugins/shellcmd/meson.build
index a00a0e8ca..e6d03e030 100644
--- a/src/plugins/shellcmd/meson.build
+++ b/src/plugins/shellcmd/meson.build
@@ -4,8 +4,12 @@ plugins_sources += files([
   'shellcmd-plugin.c',
   'gbp-shellcmd-application-addin.c',
   'gbp-shellcmd-command.c',
+  'gbp-shellcmd-command-editor.c',
   'gbp-shellcmd-command-model.c',
   'gbp-shellcmd-command-provider.c',
+  'gbp-shellcmd-command-row.c',
+  'gbp-shellcmd-list.c',
+  'gbp-shellcmd-preferences-addin.c',
 ])
 
 plugin_shellcmd_enum_headers = [
diff --git a/src/plugins/shellcmd/shellcmd-plugin.c b/src/plugins/shellcmd/shellcmd-plugin.c
index 0f316a265..26b841cc8 100644
--- a/src/plugins/shellcmd/shellcmd-plugin.c
+++ b/src/plugins/shellcmd/shellcmd-plugin.c
@@ -25,6 +25,7 @@
 
 #include "gbp-shellcmd-application-addin.h"
 #include "gbp-shellcmd-command-provider.h"
+#include "gbp-shellcmd-preferences-addin.h"
 
 _IDE_EXTERN void
 _gbp_shellcmd_register_types (PeasObjectModule *module)
@@ -35,4 +36,7 @@ _gbp_shellcmd_register_types (PeasObjectModule *module)
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_COMMAND_PROVIDER,
                                               GBP_TYPE_SHELLCMD_COMMAND_PROVIDER);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_PREFERENCES_ADDIN,
+                                              GBP_TYPE_SHELLCMD_PREFERENCES_ADDIN);
 }
diff --git a/src/plugins/shellcmd/shellcmd.gresource.xml b/src/plugins/shellcmd/shellcmd.gresource.xml
index 4af547c31..47849fb90 100644
--- a/src/plugins/shellcmd/shellcmd.gresource.xml
+++ b/src/plugins/shellcmd/shellcmd.gresource.xml
@@ -2,5 +2,7 @@
 <gresources>
   <gresource prefix="/plugins/shellcmd">
     <file>shellcmd.plugin</file>
+    <file preprocess="xml-stripblanks">gbp-shellcmd-command-editor.ui</file>
+    <file preprocess="xml-stripblanks">gbp-shellcmd-command-row.ui</file>
   </gresource>
 </gresources>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]