dashboard-commands.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "actions-types.h"

27 28
#include "core/gimp.h"

29 30
#include "widgets/gimpdashboard.h"
#include "widgets/gimphelp-ids.h"
31 32 33
#include "widgets/gimpuimanager.h"

#include "dialogs/dialogs.h"
34 35 36 37 38 39

#include "dashboard-commands.h"

#include "gimp-intl.h"


40 41 42 43 44 45 46
typedef struct
{
  GFile                  *folder;
  GimpDashboardLogParams  params;
} DashboardLogDialogInfo;


47 48
/*  local function prototypes  */

49 50 51 52 53 54 55
static void                     dashboard_log_record_response      (GtkWidget              *dialog,
                                                                    int                     response_id,
                                                                    GimpDashboard          *dashboard);

static void                     dashboard_log_add_marker_response  (GtkWidget              *dialog,
                                                                    const gchar            *description,
                                                                    GimpDashboard          *dashboard);
56

57 58
static DashboardLogDialogInfo * dashboard_log_dialog_info_new      (GimpDashboard          *dashboard);
static void                     dashboard_log_dialog_info_free     (DashboardLogDialogInfo *info);
59 60


61
/*  public functions */
62 63 64


void
65
dashboard_update_interval_cmd_callback (GimpAction *action,
66
                                        GVariant   *value,
67
                                        gpointer    data)
68 69 70 71
{
  GimpDashboard              *dashboard = GIMP_DASHBOARD (data);
  GimpDashboardUpdateInteval  update_interval;

72
  update_interval = g_variant_get_int32 (value);
73 74 75 76 77

  gimp_dashboard_set_update_interval (dashboard, update_interval);
}

void
78
dashboard_history_duration_cmd_callback (GimpAction *action,
79
                                         GVariant   *value,
80
                                         gpointer    data)
81 82 83 84
{
  GimpDashboard                *dashboard = GIMP_DASHBOARD (data);
  GimpDashboardHistoryDuration  history_duration;

85
  history_duration = g_variant_get_int32 (value);
86 87 88 89

  gimp_dashboard_set_history_duration (dashboard, history_duration);
}

90
void
91
dashboard_log_record_cmd_callback (GimpAction *action,
92
                                   GVariant   *value,
93
                                   gpointer    data)
94 95 96 97 98 99 100 101 102 103 104 105 106
{
  GimpDashboard *dashboard = GIMP_DASHBOARD (data);

  if (! gimp_dashboard_log_is_recording (dashboard))
    {
      GtkWidget *dialog;

      #define LOG_RECORD_KEY "gimp-dashboard-log-record-dialog"

      dialog = dialogs_get_dialog (G_OBJECT (dashboard), LOG_RECORD_KEY);

      if (! dialog)
        {
107 108 109 110 111 112 113
          GtkFileFilter          *filter;
          DashboardLogDialogInfo *info;
          GtkWidget              *hbox;
          GtkWidget              *hbox2;
          GtkWidget              *label;
          GtkWidget              *spinbutton;
          GtkWidget              *toggle;
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

          dialog = gtk_file_chooser_dialog_new (
            "Record Performance Log", NULL, GTK_FILE_CHOOSER_ACTION_SAVE,

            _("_Cancel"), GTK_RESPONSE_CANCEL,
            _("_Record"), GTK_RESPONSE_OK,

            NULL);

          gtk_dialog_set_default_response (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK);
          gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                                   GTK_RESPONSE_OK,
                                                   GTK_RESPONSE_CANCEL,
                                                   -1);

          gtk_window_set_screen (
            GTK_WINDOW (dialog),
            gtk_widget_get_screen (GTK_WIDGET (dashboard)));
          gtk_window_set_role (GTK_WINDOW (dialog),
                               "gimp-dashboard-log-record");
          gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);

          gtk_file_chooser_set_do_overwrite_confirmation (
            GTK_FILE_CHOOSER (dialog), TRUE);

          filter = gtk_file_filter_new ();
          gtk_file_filter_set_name (filter, _("All Files"));
          gtk_file_filter_add_pattern (filter, "*");
          gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);

          filter = gtk_file_filter_new ();
          gtk_file_filter_set_name (filter, _("Log Files (*.log)"));
          gtk_file_filter_add_pattern (filter, "*.log");
          gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);

          gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);

152 153 154 155 156 157 158 159 160 161 162 163
          info = g_object_get_data (G_OBJECT (dashboard),
                                    "gimp-dashboard-log-dialog-info");

          if (! info)
            {
              info = dashboard_log_dialog_info_new (dashboard);

              g_object_set_data_full (
                G_OBJECT (dashboard),
                "gimp-dashboard-log-dialog-info", info,
                (GDestroyNotify) dashboard_log_dialog_info_free);
            }
164

165
          if (info->folder)
166 167
            {
              gtk_file_chooser_set_current_folder_file (
168
                GTK_FILE_CHOOSER (dialog), info->folder, NULL);
169 170 171 172 173
            }

          gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog),
                                             "gimp-performance.log");

174
          hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
175 176 177
          gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), hbox);
          gtk_widget_show (hbox);

178
          hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
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
          gimp_help_set_help_data (hbox2, _("Log samples per second"), NULL);
          gtk_box_pack_start (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
          gtk_widget_show (hbox2);

          label = gtk_label_new_with_mnemonic (_("Sample fre_quency:"));
          gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
          gtk_widget_show (label);

          spinbutton = gimp_spin_button_new_with_range (1, 1000, 1);
          gtk_box_pack_start (GTK_BOX (hbox2), spinbutton, FALSE, FALSE, 0);
          gtk_widget_show (spinbutton);

          gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton),
                                     info->params.sample_frequency);

          g_signal_connect (gtk_spin_button_get_adjustment (
                              GTK_SPIN_BUTTON (spinbutton)),
                            "value-changed",
                            G_CALLBACK (gimp_int_adjustment_update),
                            &info->params.sample_frequency);

          gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinbutton);

          toggle = gtk_check_button_new_with_mnemonic (_("_Backtrace"));
          gimp_help_set_help_data (toggle, _("Include backtraces in log"),
                                   NULL);
          gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
          gtk_widget_show (toggle);

          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
                                        info->params.backtrace);

          g_signal_connect (toggle, "toggled",
                            G_CALLBACK (gimp_toggle_button_update),
                            &info->params.backtrace);

Ell's avatar
Ell committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228
          toggle = gtk_check_button_new_with_mnemonic (_("Progressi_ve"));
          gimp_help_set_help_data (toggle, _("Produce complete log "
                                             "even if not properly terminated"),
                                   NULL);
          gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0);
          gtk_widget_show (toggle);

          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
                                        info->params.progressive);

          g_signal_connect (toggle, "toggled",
                            G_CALLBACK (gimp_toggle_button_update),
                            &info->params.progressive);

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
          g_signal_connect (dialog, "response",
                            G_CALLBACK (dashboard_log_record_response),
                            dashboard);
          g_signal_connect (dialog, "delete-event",
                            G_CALLBACK (gtk_true),
                            NULL);

          gimp_help_connect (dialog, gimp_standard_help_func,
                             GIMP_HELP_DASHBOARD_LOG_RECORD, NULL);

          dialogs_attach_dialog (G_OBJECT (dashboard), LOG_RECORD_KEY, dialog);

          g_signal_connect_object (dashboard, "destroy",
                                   G_CALLBACK (gtk_widget_destroy),
                                   dialog,
                                   G_CONNECT_SWAPPED);

          #undef LOG_RECORD_KEY
        }

      gtk_window_present (GTK_WINDOW (dialog));
    }
  else
    {
      GError *error = NULL;

      if (! gimp_dashboard_log_stop_recording (dashboard, &error))
        {
          gimp_message_literal (
            gimp_editor_get_ui_manager (GIMP_EDITOR (dashboard))->gimp,
            NULL, GIMP_MESSAGE_ERROR, error->message);
        }
    }
}

void
265
dashboard_log_add_marker_cmd_callback (GimpAction *action,
266
                                       GVariant   *value,
267
                                       gpointer    data)
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
{
  GimpDashboard *dashboard = GIMP_DASHBOARD (data);
  GtkWidget     *dialog;

  #define LOG_ADD_MARKER_KEY "gimp-dashboard-log-add-marker-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (dashboard), LOG_ADD_MARKER_KEY);

  if (! dialog)
    {
      dialog = gimp_query_string_box (
        _("Add Marker"), GTK_WIDGET (dashboard),
        gimp_standard_help_func, GIMP_HELP_DASHBOARD_LOG_ADD_MARKER,
        _("Enter a description for the marker"),
        NULL,
        G_OBJECT (dashboard), "destroy",
        (GimpQueryStringCallback) dashboard_log_add_marker_response,
        dashboard);

      dialogs_attach_dialog (G_OBJECT (dashboard), LOG_ADD_MARKER_KEY, dialog);

      #undef LOG_ADD_MARKER_KEY
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
296
dashboard_log_add_empty_marker_cmd_callback (GimpAction *action,
297
                                             GVariant   *value,
298
                                             gpointer    data)
299 300 301 302 303 304
{
  GimpDashboard *dashboard = GIMP_DASHBOARD (data);

  gimp_dashboard_log_add_marker (dashboard, NULL);
}

Ell's avatar
Ell committed
305
void
306
dashboard_reset_cmd_callback (GimpAction *action,
307
                              GVariant   *value,
308
                              gpointer    data)
Ell's avatar
Ell committed
309 310 311 312 313 314
{
  GimpDashboard *dashboard = GIMP_DASHBOARD (data);

  gimp_dashboard_reset (dashboard);
}

315
void
316
dashboard_low_swap_space_warning_cmd_callback (GimpAction *action,
317
                                               GVariant   *value,
318
                                               gpointer    data)
319
{
320 321
  GimpDashboard *dashboard              = GIMP_DASHBOARD (data);
  gboolean       low_swap_space_warning = g_variant_get_boolean (value);
322 323 324

  gimp_dashboard_set_low_swap_space_warning (dashboard, low_swap_space_warning);
}
325 326 327 328 329 330 331 332 333 334 335


/*  private functions  */

static void
dashboard_log_record_response (GtkWidget     *dialog,
                               int            response_id,
                               GimpDashboard *dashboard)
{
  if (response_id == GTK_RESPONSE_OK)
    {
336 337 338 339 340
      GFile                  *file;
      DashboardLogDialogInfo *info;
      GError                 *error = NULL;

      file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
341

342 343
      info = g_object_get_data (G_OBJECT (dashboard),
                                "gimp-dashboard-log-dialog-info");
344

345 346 347 348 349 350 351
      g_return_if_fail (info != NULL);

      g_set_object (&info->folder, g_file_get_parent (file));

      if (! gimp_dashboard_log_start_recording (dashboard,
                                                file, &info->params,
                                                &error))
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        {
          gimp_message_literal (
            gimp_editor_get_ui_manager (GIMP_EDITOR (dashboard))->gimp,
            NULL, GIMP_MESSAGE_ERROR, error->message);

          g_clear_error (&error);
        }

      g_object_unref (file);
    }

  gtk_widget_destroy (dialog);
}

static void
dashboard_log_add_marker_response (GtkWidget     *dialog,
                                   const gchar   *description,
                                   GimpDashboard *dashboard)
{
  gimp_dashboard_log_add_marker (dashboard, description);
}
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

static DashboardLogDialogInfo *
dashboard_log_dialog_info_new (GimpDashboard *dashboard)
{
  DashboardLogDialogInfo *info = g_slice_new (DashboardLogDialogInfo);

  info->folder = NULL;
  info->params = *gimp_dashboard_log_get_default_params (dashboard);

  return info;
}

static void
dashboard_log_dialog_info_free (DashboardLogDialogInfo *info)
{
  g_clear_object (&info->folder);

  g_slice_free (DashboardLogDialogInfo, info);
}