ide-build-log-panel.c 23 KB
Newer Older
1
/* ide-build-log-panel.c
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Copyright (C) 2015 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/>.
 */

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

27 28 29 30 31 32 33 34 35 36 37 38 39
typedef struct _ColorCodeState
{
  /* A value of -1 is used to specify a default foreground orr background */
  gint16 foreground;
  gint16 background;

  guint  bold       : 1;
  guint  dim        : 1;
  guint  underlined : 1;
  guint  reverse    : 1;
  guint  hidden     : 1;
} ColorCodeState;

40
struct _IdeBuildLogPanel
41
{
42
  DzlDockWidget      parent_instance;
43

44
  IdeBuildPipeline  *pipeline;
45 46 47
  GtkCssProvider    *css;
  GSettings         *settings;
  GtkTextBuffer     *buffer;
48

49 50
  GtkScrolledWindow *scroller;
  GtkTextView       *text_view;
51

52
  GtkTextTag        *stderr_tag;
53 54 55 56 57 58
  GPtrArray         *color_codes_foreground_tags;
  GPtrArray         *color_codes_background_tags;
  GtkTextTag        *color_codes_bold_tag;
  GtkTextTag        *color_codes_underlined_tag;
  ColorCodeState     color_codes_state;
  ColorCodeState     current_color_codes_state;
59 60

  guint              log_observer;
61 62 63 64
};

enum {
  PROP_0,
65
  PROP_PIPELINE,
66 67 68
  LAST_PROP
};

69
G_DEFINE_TYPE (IdeBuildLogPanel, ide_build_log_panel, DZL_TYPE_DOCK_WIDGET)
70 71 72

static GParamSpec *properties [LAST_PROP];

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 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 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/* TODO: Same hard-coded palette as terminal-view
 * till we have code for custom palettes
 */
#define COLOR_PALETTE_NB_COLORS 16

static const GdkRGBA solarized_palette[] =
{
  /*
   * Solarized palette (1.0.0beta2):
   * http://ethanschoonover.com/solarized
   */
  { 0.02745,  0.211764, 0.258823, 1 },
  { 0.862745, 0.196078, 0.184313, 1 },
  { 0.521568, 0.6,      0,        1 },
  { 0.709803, 0.537254, 0,        1 },
  { 0.149019, 0.545098, 0.823529, 1 },
  { 0.82745,  0.211764, 0.509803, 1 },
  { 0.164705, 0.631372, 0.596078, 1 },
  { 0.933333, 0.909803, 0.835294, 1 },
  { 0,        0.168627, 0.211764, 1 },
  { 0.796078, 0.294117, 0.086274, 1 },
  { 0.345098, 0.431372, 0.458823, 1 },
  { 0.396078, 0.482352, 0.513725, 1 },
  { 0.513725, 0.580392, 0.588235, 1 },
  { 0.423529, 0.443137, 0.768627, 1 },
  { 0.57647,  0.631372, 0.631372, 1 },
  { 0.992156, 0.964705, 0.890196, 1 },
};

typedef enum
{
  COLOR_CODE_NONE,
  COLOR_CODE_TAG,
  COLOR_CODE_INVALID,
  COLOR_CODE_SKIP,
} ColorCodeType;

static void
init_color_tags_from_palette (IdeBuildLogPanel *self)
{
  GtkTextTag *tag;
  GdkRGBA rgba;

  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
  g_assert (self->buffer != NULL);

  g_clear_pointer (&self->color_codes_foreground_tags, g_ptr_array_unref);
  g_clear_pointer (&self->color_codes_background_tags, g_ptr_array_unref);
  g_clear_object (&self->color_codes_bold_tag);
  g_clear_object (&self->color_codes_underlined_tag);

  self->color_codes_foreground_tags = g_ptr_array_new ();
  for (gint i = 0; i < COLOR_PALETTE_NB_COLORS; ++i)
    {
      rgba = solarized_palette [i];
      tag = gtk_text_buffer_create_tag (self->buffer, NULL, "foreground-rgba", &rgba, NULL);
      g_ptr_array_add (self->color_codes_foreground_tags, tag);
    }

  self->color_codes_background_tags = g_ptr_array_new ();
  for (gint i = 0; i < COLOR_PALETTE_NB_COLORS; ++i)
    {
      rgba = solarized_palette [i];
      tag = gtk_text_buffer_create_tag (self->buffer, NULL, "background-rgba", &rgba, NULL);
      g_ptr_array_add (self->color_codes_background_tags, tag);
    }

  self->color_codes_bold_tag =
    g_object_ref (gtk_text_buffer_create_tag (self->buffer, NULL,
                                              "weight", PANGO_WEIGHT_BOLD,
                                              NULL));
  self->color_codes_underlined_tag =
    g_object_ref (gtk_text_buffer_create_tag (self->buffer, NULL,
                                              "underline", PANGO_UNDERLINE_SINGLE,
                                              NULL));
}

static inline gboolean
is_foreground_color_value (gint value)
{
  return ((value >= 30 && value <= 37) || (value >= 90 && value <= 97));
}

static inline gboolean
is_background_color_value (gint value)
{
  return ((value >= 40 && value <= 47) || (value >= 100 && value <= 107));
}

static inline gboolean
is_format_color_value (gint value)
{
  return (value == 1 || value == 2 || value == 4 || value == 5 || value == 7 || value == 8);
}

static inline gboolean
is_reset_format_color_value (gint value)
{
  return (value == 21 || value == 22 || value == 24 || value == 25 || value == 27 || value == 28);
}

static inline gboolean
is_reset_all_color_value (gint value)
{
  return (value == 0);
}

/* Return -1 if not valid.
 * Cursor is updated in every cases.
 */
static gint
str_to_int (const gchar **cursor_ptr)
{
  gint value = 0;

  g_assert (cursor_ptr != NULL && *cursor_ptr != NULL);

  if (**cursor_ptr == 'm')
    return 0;

  while (**cursor_ptr >= '0' && **cursor_ptr <= '9')
    {
      value *= 10;
      value += **cursor_ptr - '0';

      ++(*cursor_ptr);
    }

  if (is_foreground_color_value (value) ||
      is_background_color_value (value) ||
      is_format_color_value (value) ||
      is_reset_format_color_value (value) ||
      value == 0 || value == 39 || value == 49)
    return value;
  else
    return -1;
}

static gint
color_code_value_to_tag_index (gint value)
{
  if (value >=30 && value <= 37)
    return value - 30;

  if (value >=90 && value <= 97)
    return value - 82;

  return -1;
}

static void
color_codes_state_reset (ColorCodeState *color_codes_state)
{
  g_assert (color_codes_state != NULL);

  color_codes_state->foreground = -1;
  color_codes_state->background = -1;

  color_codes_state->bold = FALSE;
  color_codes_state->dim = FALSE;
  color_codes_state->reverse = FALSE;
  color_codes_state->underlined = FALSE;
  color_codes_state->hidden = FALSE;
}

static void
color_codes_state_update (IdeBuildLogPanel *self,
                          ColorCodeState   *color_codes_state,
                          gint              value)
{
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
  g_assert (color_codes_state != NULL);

  if (value == 0)
    color_codes_state_reset (color_codes_state);
  else if (value == 39)
    color_codes_state->foreground = -1;
  else if (value == 49)
    color_codes_state->background = -1;
  else if (is_foreground_color_value (value))
    color_codes_state->foreground = value;
  else if (is_background_color_value (value))
    color_codes_state->background = value;
  else if (is_format_color_value (value))
    {
      if (value == 1)
        color_codes_state->bold = TRUE;
      else if (value == 4)
        color_codes_state->underlined = TRUE;
    }
  else if (is_reset_format_color_value (value))
    {
      if (value == 21)
        color_codes_state->bold = FALSE;
      else if (value == 24)
        color_codes_state->underlined = FALSE;
    }
}

static void
color_codes_state_apply (IdeBuildLogPanel *self,
                         ColorCodeState   *color_codes_state,
                         GtkTextIter      *begin,
                         GtkTextIter      *end)
{
  GtkTextTag *tag;
  gint tag_index;

  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
  g_assert (color_codes_state != NULL);
  g_assert (begin != NULL);
  g_assert (end != NULL);

  if (color_codes_state->foreground != -1)
    {
      tag_index = color_code_value_to_tag_index (color_codes_state->foreground);
      g_assert (tag_index != -1);

      tag = g_ptr_array_index (self->color_codes_foreground_tags, tag_index);
      gtk_text_buffer_apply_tag (self->buffer, tag, begin, end);
    }

  if (color_codes_state->background != -1)
    {
      tag_index = color_code_value_to_tag_index (color_codes_state->background);
      g_assert (tag_index != -1);

      tag = g_ptr_array_index (self->color_codes_background_tags, tag_index);
      gtk_text_buffer_apply_tag (self->buffer, tag, begin, end);
    }

  if (color_codes_state->bold ==  TRUE)
    gtk_text_buffer_apply_tag (self->buffer, self->color_codes_bold_tag, begin, end);

  if (color_codes_state->underlined ==  TRUE)
    gtk_text_buffer_apply_tag (self->buffer, self->color_codes_underlined_tag, begin, end);
}

static ColorCodeType
fetch_color_codes_tags (IdeBuildLogPanel  *self,
                        const gchar      **cursor,
                        ColorCodeState    *color_codes_state)
{
  gint value;
  ColorCodeType ret = COLOR_CODE_NONE;
  ColorCodeState tmp_color_codes_state = *color_codes_state;

  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
  g_assert (cursor != NULL && *cursor != NULL);
  g_assert (color_codes_state  != NULL);

  while (**cursor != '\0')
    {
      value = str_to_int (cursor);
      if (value != -1)
        {
          if (is_foreground_color_value (value) ||
              is_background_color_value (value) ||
              is_format_color_value (value) ||
              is_reset_format_color_value (value) ||
              is_reset_all_color_value (value))
            {
              color_codes_state_update (self, &tmp_color_codes_state, value);
              ret = COLOR_CODE_TAG;
            }
        }
      else if (ret == COLOR_CODE_NONE)
        ret = COLOR_CODE_INVALID;

      if (**cursor == 'm')
      {
        if (ret != COLOR_CODE_INVALID)
          *color_codes_state = tmp_color_codes_state;

        ++(*cursor);
        return ret;
      }

      if (**cursor != ';')
        break;

      ++(*cursor);
    }

  return COLOR_CODE_INVALID;
}

/**
 * find_color_code:
 * @self: a #GbpBuildLogPanel
 * @msg: text to search in
 * @color_codes_state: (inout) : if a color code is found, the state is updated
 * @start: (out) point to the first char of a found code
 * @end: (out) point to the last char + 1 of a found code
 *
 * If no color code is found, start and end point to the string's end.
 *
 * Returns: a #ColorCodeType indicating the state of the search.
 */

static ColorCodeType
find_color_code (IdeBuildLogPanel  *self,
                 const gchar       *msg,
                 ColorCodeState    *color_codes_state,
                 const gchar      **start,
                 const gchar      **end)
{
  const gchar *cursor = msg;
  ColorCodeType ret;

  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
  g_assert (!ide_str_empty0 (msg));
  g_assert (color_codes_state != NULL);
  g_assert (start != NULL);
  g_assert (end != NULL);

  while (*cursor != '\0')
    {
      if (*cursor == '\\' && *(cursor + 1) == 'e')
        {
          *start = cursor;
          cursor += 2;
        }
396
      else if (*cursor == '\033')
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
        {
          *start = cursor;
          ++cursor;
        }
      else
        goto next;

      if (*cursor == '[')
        {
          ++cursor;
          if (*cursor == '\0')
            goto end;

          if (*cursor == 'K')
            {
              *end = cursor + 1;
              return COLOR_CODE_SKIP;
            }

          ret = fetch_color_codes_tags (self, &cursor, color_codes_state);
          *end = cursor;

          return ret;
        }

      if (*cursor == '\0')
        goto end;

next:
      /* TODO: skip a possible escaped char */
      cursor = g_utf8_next_char (cursor);
    }

end:
  *start = *end = cursor;
  return COLOR_CODE_NONE;
}

/* Transform VT color codes into tags before inserting the text */
static void
ide_build_log_panel_insert_text (IdeBuildLogPanel  *self,
                                 const gchar       *message,
                                 GtkTextIter       *iter,
                                 IdeBuildLogStream  stream)
{
  ColorCodeType tag_type;
  ColorCodeType current_tag_type = COLOR_CODE_NONE;
  GtkTextIter pos = *iter;
  const gchar *cursor = message;
  const gchar *tag_start;
  const gchar *tag_end;
  gsize len;

  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
  g_assert (iter != NULL);

  if (ide_str_empty0 (message))
    return;

  while (*cursor != '\0')
    {
      tag_type = find_color_code (self, cursor, &self->color_codes_state, &tag_start, &tag_end);
      len = tag_start - cursor;
      if (len > 0)
        {
          GtkTextIter begin;
          guint offset;

          offset = gtk_text_iter_get_offset (&pos);
          gtk_text_buffer_insert (self->buffer, &pos, cursor, len);
          gtk_text_buffer_get_iter_at_offset (self->buffer, &begin, offset);

          if (current_tag_type == COLOR_CODE_TAG || current_tag_type == COLOR_CODE_SKIP)
            color_codes_state_apply (self, &self->current_color_codes_state, &begin, &pos);

          if (G_LIKELY (stream != IDE_BUILD_LOG_STDOUT))
            gtk_text_buffer_apply_tag (self->buffer, self->stderr_tag, &begin, &pos);
        }

      current_tag_type = tag_type;
      self->current_color_codes_state = self->color_codes_state;

      if (tag_type == COLOR_CODE_NONE)
        break;

      cursor = tag_end;
    }

  gtk_text_buffer_insert (self->buffer, &pos, "\n", 1);
}

488
static void
489
ide_build_log_panel_reset_view (IdeBuildLogPanel *self)
490
{
491 492
  GtkStyleContext *context;

493
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
494 495 496

  g_clear_object (&self->buffer);

497 498 499
  if (self->text_view != NULL)
    gtk_widget_destroy (GTK_WIDGET (self->text_view));

500 501 502 503
  self->buffer = gtk_text_buffer_new (NULL);
  self->stderr_tag = gtk_text_buffer_create_tag (self->buffer,
                                                 "stderr-tag",
                                                 "foreground", "#ff0000",
504
                                                 "weight", PANGO_WEIGHT_NORMAL,
505
                                                 NULL);
506

507 508 509 510 511
  /* We set VT color codes tags after stderr_tag so that they have higher priority */
  init_color_tags_from_palette (self);
  color_codes_state_reset (&self->current_color_codes_state);
  color_codes_state_reset (&self->color_codes_state);

512
  self->text_view = g_object_new (GTK_TYPE_TEXT_VIEW,
513
                                  "bottom-margin", 3,
514
                                  "buffer", self->buffer,
515
                                  "cursor-visible", FALSE,
516
                                  "editable", FALSE,
517
                                  "left-margin", 3,
518
                                  "monospace", TRUE,
519 520
                                  "right-margin", 3,
                                  "top-margin", 3,
521 522 523 524 525 526 527
                                  "visible", TRUE,
                                  NULL);
  context = gtk_widget_get_style_context (GTK_WIDGET (self->text_view));
  gtk_style_context_add_provider (context,
                                  GTK_STYLE_PROVIDER (self->css),
                                  GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  gtk_container_add (GTK_CONTAINER (self->scroller), GTK_WIDGET (self->text_view));
528 529
}

530
static void
531
ide_build_log_panel_log_observer (IdeBuildLogStream  stream,
532 533 534
                                  const gchar       *message,
                                  gssize             message_len,
                                  gpointer           user_data)
535
{
536
  IdeBuildLogPanel *self = user_data;
537
  GtkTextMark *insert;
538
  GtkTextIter iter, enditer;
539

540
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
541
  g_assert (message != NULL);
542 543
  g_assert (message_len >= 0);
  g_assert (message[message_len] == '\0');
544 545

  gtk_text_buffer_get_end_iter (self->buffer, &iter);
546
  ide_build_log_panel_insert_text (self, message, &iter, stream);
547

548
  insert = gtk_text_buffer_get_insert (self->buffer);
549
  gtk_text_view_scroll_to_mark (self->text_view, insert, 0.0, TRUE, 1.0, 0.0);
550 551 552

  gtk_text_buffer_get_end_iter (self->buffer, &enditer);
  gtk_text_buffer_place_cursor (self->buffer, &enditer);
553 554 555
}

void
556
ide_build_log_panel_set_pipeline (IdeBuildLogPanel *self,
557
                                  IdeBuildPipeline *pipeline)
558
{
559
  g_return_if_fail (IDE_IS_BUILD_LOG_PANEL (self));
560
  g_return_if_fail (!pipeline || IDE_IS_BUILD_PIPELINE (pipeline));
561

562
  if (pipeline != self->pipeline)
563
    {
564 565 566 567 568 569 570 571 572 573 574 575
      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,
576
                                                 ide_build_log_panel_log_observer,
577 578 579
                                                 self,
                                                 NULL);
        }
580 581 582
    }
}

583
static void
584
ide_build_log_panel_changed_font_name (IdeBuildLogPanel *self,
585 586 587 588 589 590
                                       const gchar      *key,
                                       GSettings        *settings)
{
  gchar *font_name;
  PangoFontDescription *font_desc;

591
  g_assert (IDE_IS_BUILD_LOG_PANEL (self));
592 593 594 595 596 597 598 599 600 601 602
  g_assert (g_strcmp0 (key, "font-name") == 0);
  g_assert (G_IS_SETTINGS (settings));

  font_name = g_settings_get_string (settings, key);
  font_desc = pango_font_description_from_string (font_name);

  if (font_desc != NULL)
    {
      gchar *fragment;
      gchar *css;

603
      fragment = dzl_pango_font_description_to_css (font_desc);
604 605 606 607 608 609 610 611 612 613 614 615
      css = g_strdup_printf ("textview { %s }", fragment);

      gtk_css_provider_load_from_data (self->css, css, -1, NULL);

      pango_font_description_free (font_desc);
      g_free (fragment);
      g_free (css);
    }

  g_free (font_name);
}

616
static void
617
ide_build_log_panel_finalize (GObject *object)
618
{
619
  IdeBuildLogPanel *self = (IdeBuildLogPanel *)object;
620

621 622
  self->stderr_tag = NULL;

623
  g_clear_object (&self->pipeline);
624 625
  g_clear_object (&self->css);
  g_clear_object (&self->settings);
626

627 628 629 630 631
  g_clear_pointer (&self->color_codes_foreground_tags, g_ptr_array_unref);
  g_clear_pointer (&self->color_codes_background_tags, g_ptr_array_unref);
  g_clear_object (&self->color_codes_bold_tag);
  g_clear_object (&self->color_codes_underlined_tag);

632
  G_OBJECT_CLASS (ide_build_log_panel_parent_class)->finalize (object);
633 634
}

635
static void
636
ide_build_log_panel_dispose (GObject *object)
637
{
638
  IdeBuildLogPanel *self = (IdeBuildLogPanel *)object;
639

640
  ide_build_log_panel_set_pipeline (self, NULL);
641

642
  G_OBJECT_CLASS (ide_build_log_panel_parent_class)->dispose (object);
643 644
}

645
static void
646
ide_build_log_panel_get_property (GObject    *object,
647 648 649 650
                                  guint       prop_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
{
651
  IdeBuildLogPanel *self = IDE_BUILD_LOG_PANEL (object);
652 653 654

  switch (prop_id)
    {
655 656
    case PROP_PIPELINE:
      g_value_set_object (value, self->pipeline);
657 658 659 660 661 662 663 664
      break;

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

static void
665
ide_build_log_panel_set_property (GObject      *object,
666 667 668 669
                                  guint         prop_id,
                                  const GValue *value,
                                  GParamSpec   *pspec)
{
670
  IdeBuildLogPanel *self = IDE_BUILD_LOG_PANEL (object);
671 672 673

  switch (prop_id)
    {
674
    case PROP_PIPELINE:
675
      ide_build_log_panel_set_pipeline (self, g_value_get_object (value));
676 677 678 679 680 681 682 683
      break;

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

static void
684
ide_build_log_panel_class_init (IdeBuildLogPanelClass *klass)
685 686 687 688
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

689 690 691 692
  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;
693 694

  gtk_widget_class_set_css_name (widget_class, "buildlogpanel");
695 696
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/builder/plugins/buildui/ide-build-log-panel.ui");
  gtk_widget_class_bind_template_child (widget_class, IdeBuildLogPanel, scroller);
697

698 699
  properties [PROP_PIPELINE] =
    g_param_spec_object ("pipeline",
700 701
                         "Result",
                         "Result",
702
                         IDE_TYPE_BUILD_PIPELINE,
703 704 705 706 707
                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_properties (object_class, LAST_PROP, properties);
}

708 709 710 711 712 713 714 715 716 717 718 719 720
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));

  gtk_text_buffer_set_text (self->buffer, "", 0);
}

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
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)
    {
      g_autofree gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (native));
      g_autofree gchar *text = NULL;
      GtkTextIter begin;
      GtkTextIter end;

      gtk_text_buffer_get_bounds (self->buffer, &begin, &end);
      text = gtk_text_buffer_get_text (self->buffer, &begin, &end, FALSE);
      g_file_set_contents (filename, text, -1, NULL);
    }

  IDE_EXIT;
}

760
static void
761
ide_build_log_panel_init (IdeBuildLogPanel *self)
762
{
763 764
  static GActionEntry entries[] = {
    { "clear", ide_build_log_panel_clear_activate },
765
    { "save", ide_build_log_panel_save_in_file },
766 767 768
  };
  g_autoptr(GSimpleActionGroup) actions = NULL;

769
  self->css = gtk_css_provider_new ();
770

771 772
  gtk_widget_init_template (GTK_WIDGET (self));

773 774
  g_object_set (self, "title", _("Build Output"), NULL);

775
  ide_build_log_panel_reset_view (self);
776

777 778 779
  self->settings = g_settings_new ("org.gnome.builder.terminal");
  g_signal_connect_object (self->settings,
                           "changed::font-name",
780
                           G_CALLBACK (ide_build_log_panel_changed_font_name),
781 782
                           self,
                           G_CONNECT_SWAPPED);
783
  ide_build_log_panel_changed_font_name (self, "font-name", self->settings);
784 785 786 787

  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));
788
}