ide-build-log-panel.c 9.81 KB
Newer Older
1
/* ide-build-log-panel.c
2
 *
3
 * Copyright © 2015 Christian Hergert <chergert@redhat.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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/>.
 */

19 20
#define G_LOG_DOMAIN "ide-build-log-panel"

21
#include <dazzle.h>
22 23 24
#include <glib/gi18n.h>
#include <ide.h>

25
#include "buildui/ide-build-log-panel.h"
26
#include "terminal/ide-terminal.h"
27

28
struct _IdeBuildLogPanel
29
{
30
  DzlDockWidget      parent_instance;
31

32
  IdeBuildPipeline  *pipeline;
33
  GSettings         *settings;
34

35
  IdeTerminal       *terminal;
36 37

  guint              log_observer;
38 39 40 41
};

enum {
  PROP_0,
42
  PROP_PIPELINE,
43
  N_PROPS
44 45
};

46
G_DEFINE_TYPE (IdeBuildLogPanel, ide_build_log_panel, DZL_TYPE_DOCK_WIDGET)
47

48
static GParamSpec *properties [N_PROPS];
49

50
static void
51
ide_build_log_panel_reset_view (IdeBuildLogPanel *self)
52
{
53
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
54

55
  vte_terminal_reset (VTE_TERMINAL (self->terminal), TRUE, TRUE);
56 57
}

58
static void
59
ide_build_log_panel_log_observer (IdeBuildLogStream  stream,
60 61 62
                                  const gchar       *message,
                                  gssize             message_len,
                                  gpointer           user_data)
63
{
64
  IdeBuildLogPanel *self = user_data;
65

66
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
67
  g_assert (message != NULL);
68 69
  g_assert (message_len >= 0);
  g_assert (message[message_len] == '\0');
70

71 72
  vte_terminal_feed (VTE_TERMINAL (self->terminal), message, -1);
  vte_terminal_feed (VTE_TERMINAL (self->terminal), "\r\n", -1);
73 74 75
}

void
76
ide_build_log_panel_set_pipeline (IdeBuildLogPanel *self,
77
                                  IdeBuildPipeline *pipeline)
78
{
79
  g_return_if_fail (IDE_IS_BUILD_LOG_PANEL (self));
80
  g_return_if_fail (!pipeline || IDE_IS_BUILD_PIPELINE (pipeline));
81

82
  if (pipeline != self->pipeline)
83
    {
84 85 86 87 88 89 90 91 92 93 94 95
      if (self->pipeline != NULL)
        {
          ide_build_pipeline_remove_log_observer (self->pipeline, self->log_observer);
          self->log_observer = 0;
          g_clear_object (&self->pipeline);
        }

      if (pipeline != NULL)
        {
          self->pipeline = g_object_ref (pipeline);
          self->log_observer =
            ide_build_pipeline_add_log_observer (self->pipeline,
96
                                                 ide_build_log_panel_log_observer,
97 98 99
                                                 self,
                                                 NULL);
        }
100 101 102
    }
}

103
static void
104
ide_build_log_panel_changed_font_name (IdeBuildLogPanel *self,
105 106 107
                                       const gchar      *key,
                                       GSettings        *settings)
{
108
  g_autofree gchar *font_name= NULL;
109 110
  PangoFontDescription *font_desc;

111
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
112 113 114
  g_assert (g_strcmp0 (key, "font-name") == 0);
  g_assert (G_IS_SETTINGS (settings));

115 116
  /* TODO: This probably can all go in IdeTerminal directly */

117 118 119 120 121 122 123 124
  font_name = g_settings_get_string (settings, key);
  font_desc = pango_font_description_from_string (font_name);

  if (font_desc != NULL)
    {

    }

125
  g_clear_pointer (&font_desc, pango_font_description_free);
126 127
}

128
static void
129
ide_build_log_panel_finalize (GObject *object)
130
{
131
  IdeBuildLogPanel *self = (IdeBuildLogPanel *)object;
132

133
  g_clear_object (&self->pipeline);
134
  g_clear_object (&self->settings);
135

136
  G_OBJECT_CLASS (ide_build_log_panel_parent_class)->finalize (object);
137 138
}

139
static void
140
ide_build_log_panel_dispose (GObject *object)
141
{
142
  IdeBuildLogPanel *self = (IdeBuildLogPanel *)object;
143

144
  ide_build_log_panel_set_pipeline (self, NULL);
145

146
  G_OBJECT_CLASS (ide_build_log_panel_parent_class)->dispose (object);
147 148
}

149
static void
150
ide_build_log_panel_get_property (GObject    *object,
151 152 153 154
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
155
  IdeBuildLogPanel *self = IDE_BUILD_LOG_PANEL (object);
156 157 158

  switch (prop_id)
    {
159 160
    case PROP_PIPELINE:
      g_value_set_object (value, self->pipeline);
161 162 163 164 165 166 167 168
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
169
ide_build_log_panel_set_property (GObject      *object,
170 171 172 173
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
174
  IdeBuildLogPanel *self = IDE_BUILD_LOG_PANEL (object);
175 176 177

  switch (prop_id)
    {
178
    case PROP_PIPELINE:
179
      ide_build_log_panel_set_pipeline (self, g_value_get_object (value));
180 181 182 183 184 185 186 187
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
188
ide_build_log_panel_class_init (IdeBuildLogPanelClass *klass)
189 190 191 192
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

193 194 195 196
  object_class->dispose = ide_build_log_panel_dispose;
  object_class->finalize = ide_build_log_panel_finalize;
  object_class->get_property = ide_build_log_panel_get_property;
  object_class->set_property = ide_build_log_panel_set_property;
197 198

  gtk_widget_class_set_css_name (widget_class, "buildlogpanel");
199
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/builder/plugins/buildui/ide-build-log-panel.ui");
200
  gtk_widget_class_bind_template_child (widget_class, IdeBuildLogPanel, terminal);
201

202 203
  properties [PROP_PIPELINE] =
    g_param_spec_object ("pipeline",
204 205
                         "Result",
                         "Result",
206
                         IDE_TYPE_BUILD_PIPELINE,
207 208
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

209
  g_object_class_install_properties (object_class, N_PROPS, properties);
210 211
}

212 213 214 215 216 217 218 219 220 221
static void
ide_build_log_panel_clear_activate (GSimpleAction *action,
                                    GVariant      *param,
                                    gpointer       user_data)
{
  IdeBuildLogPanel *self = user_data;

  g_assert (G_IS_SIMPLE_ACTION (action));
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));

222
  ide_build_log_panel_reset_view (self);
223 224
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
static void
ide_build_log_panel_save_in_file (GSimpleAction *action,
                                  GVariant      *param,
                                  gpointer       user_data)
{
  IdeBuildLogPanel *self = user_data;
  g_autoptr(GtkFileChooserNative) native = NULL;
  GtkWidget *window;
  gint res;

  IDE_ENTRY;

  g_assert (G_IS_SIMPLE_ACTION (action));
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));

  window = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_WINDOW);
  native = gtk_file_chooser_native_new (_("Save File"),
                                        GTK_WINDOW (window),
                                        GTK_FILE_CHOOSER_ACTION_SAVE,
                                        _("_Save"),
                                        _("_Cancel"));

  res = gtk_native_dialog_run (GTK_NATIVE_DIALOG (native));

  if (res == GTK_RESPONSE_ACCEPT)
    {
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
      g_autoptr(GFile) file = NULL;

      file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (native));

      if (file != NULL)
        {
          g_autoptr(GFileOutputStream) stream = NULL;
          g_autoptr(GError) error = NULL;

          stream = g_file_replace (file,
                                   NULL,
                                   FALSE,
                                   G_FILE_CREATE_REPLACE_DESTINATION,
                                   NULL,
                                   &error);

          if (stream != NULL)
            {
              vte_terminal_write_contents_sync (VTE_TERMINAL (self->terminal),
                                                G_OUTPUT_STREAM (stream),
                                                VTE_WRITE_DEFAULT,
                                                NULL,
                                                &error);
              g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL);
            }

          if (error != NULL)
            g_warning ("Failed to write contents: %s", error->message);
        }
280 281 282 283 284
    }

  IDE_EXIT;
}

285
static void
286
ide_build_log_panel_init (IdeBuildLogPanel *self)
287
{
288 289
  g_autoptr(GSimpleActionGroup) actions = NULL;
  static const GActionEntry entries[] = {
290
    { "clear", ide_build_log_panel_clear_activate },
291
    { "save", ide_build_log_panel_save_in_file },
292
  };
293

294 295
  gtk_widget_init_template (GTK_WIDGET (self));

296 297 298 299 300 301 302 303 304 305 306 307 308
#if 0
  {
    VtePty *pty = vte_pty_new_sync (VTE_PTY_DEFAULT, NULL, NULL);
    vte_terminal_set_pty (VTE_TERMINAL (self->terminal), pty);
    g_object_unref (pty);
  }
#endif

  vte_terminal_set_scrollback_lines (VTE_TERMINAL (self->terminal), 1000);
  vte_terminal_set_scroll_on_output (VTE_TERMINAL (self->terminal), FALSE);
  vte_terminal_set_scroll_on_keystroke (VTE_TERMINAL (self->terminal), TRUE);

  dzl_dock_widget_set_title (DZL_DOCK_WIDGET (self), _("Build Output"));
309

310
  ide_build_log_panel_reset_view (self);
311

312 313 314
  self->settings = g_settings_new ("org.gnome.builder.terminal");
  g_signal_connect_object (self->settings,
                           "changed::font-name",
315
                           G_CALLBACK (ide_build_log_panel_changed_font_name),
316 317
                           self,
                           G_CONNECT_SWAPPED);
318
  ide_build_log_panel_changed_font_name (self, "font-name", self->settings);
319 320 321 322

  actions = g_simple_action_group_new ();
  g_action_map_add_action_entries (G_ACTION_MAP (actions), entries, G_N_ELEMENTS (entries), self);
  gtk_widget_insert_action_group (GTK_WIDGET (self), "build-log", G_ACTION_GROUP (actions));
323
}