screenshot-application.c 27.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* gnome-screenshot.c - Take a screenshot of the desktop
 *
 * Copyright (C) 2001 Jonathan Blandford <jrb@alum.mit.edu>
 * Copyright (C) 2006 Emmanuele Bassi <ebassi@gnome.org>
 * Copyright (C) 2008-2012 Cosimo Cecchi <cosimoc@gnome.org>
 *
 * 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 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 21 22
 * USA
 */

23 24
#include "config.h"

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <gdk/gdkx.h>
#include <gdk/gdkkeysyms.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <glib/gi18n.h>
#include <gio/gio.h>

#include "screenshot-application.h"
#include "screenshot-area-selection.h"
#include "screenshot-config.h"
#include "screenshot-filename-builder.h"
#include "screenshot-interactive-dialog.h"
#include "screenshot-shadow.h"
#include "screenshot-utils.h"
#include "screenshot-dialog.h"

#define LAST_SAVE_DIRECTORY_KEY "last-save-directory"

G_DEFINE_TYPE (ScreenshotApplication, screenshot_application, GTK_TYPE_APPLICATION);

47
static void screenshot_save_to_file (ScreenshotApplication *self);
48
static void screenshot_show_interactive_dialog (ScreenshotApplication *self);
49

50 51 52 53
struct _ScreenshotApplicationPriv {
  gchar *icc_profile_base64;
  GdkPixbuf *screenshot;

54
  gchar *save_uri;
55
  gboolean should_overwrite;
56

57 58 59 60
  ScreenshotDialog *dialog;
};

static void
61
save_folder_to_settings (ScreenshotApplication *self)
62 63 64
{
  char *folder;

65
  folder = screenshot_dialog_get_folder (self->priv->dialog);
66 67 68 69 70 71 72
  g_settings_set_string (screenshot_config->settings,
                         LAST_SAVE_DIRECTORY_KEY, folder);

  g_free (folder);
}

static void
73
set_recent_entry (ScreenshotApplication *self)
74
{
75
  char *app_exec = NULL;
76 77 78 79 80 81 82 83 84 85 86 87 88 89
  GtkRecentManager *recent;
  GtkRecentData recent_data;
  GAppInfo *app;
  const char *exec_name = NULL;
  static char * groups[2] = { "Graphics", NULL };

  app = g_app_info_get_default_for_type ("image/png", TRUE);

  if (!app) {
    /* return early, as this would be an useless recent entry anyway. */
    return;
  }

  recent = gtk_recent_manager_get_default ();
90

91 92 93 94 95 96 97 98 99 100 101
  exec_name = g_app_info_get_executable (app);
  app_exec = g_strjoin (" ", exec_name, "%u", NULL);

  recent_data.display_name = NULL;
  recent_data.description = NULL;
  recent_data.mime_type = "image/png";
  recent_data.app_name = "GNOME Screenshot";
  recent_data.app_exec = app_exec;
  recent_data.groups = groups;
  recent_data.is_private = FALSE;

102
  gtk_recent_manager_add_full (recent, self->priv->save_uri, &recent_data);
103 104 105 106 107

  g_object_unref (app);
  g_free (app_exec);
}

108 109 110 111 112 113 114 115 116
static void
screenshot_close_interactive_dialog (ScreenshotApplication *self)
{
  ScreenshotDialog *dialog = self->priv->dialog;
  save_folder_to_settings (self);
  gtk_widget_destroy (dialog->dialog);
  g_free (dialog);
}

117
static void
118
save_pixbuf_handle_success (ScreenshotApplication *self)
119
{
120
  set_recent_entry (self);
121

122 123
  if (screenshot_config->interactive)
    {
124
      screenshot_close_interactive_dialog (self);
125 126 127 128 129
    }
  else
    {
      g_application_release (G_APPLICATION (self));
    }
130 131 132
}

static void
133 134
save_pixbuf_handle_error (ScreenshotApplication *self,
                          GError *error)
135
{
136 137 138
  if (screenshot_config->interactive)
    {
      ScreenshotDialog *dialog = self->priv->dialog;
139

140
      screenshot_dialog_set_busy (dialog, FALSE);
141 142 143 144 145

      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS) &&
          !self->priv->should_overwrite)
        {
          gchar *folder = screenshot_dialog_get_folder (dialog);
146 147
          gchar *folder_uri = g_path_get_basename (folder);
          gchar *folder_name = g_uri_unescape_string (folder_uri, NULL);
148
          gchar *file_name = screenshot_dialog_get_filename (dialog);
149
          gchar *detail = g_strdup_printf (_("A file named “%s” already exists in “%s”"),
150 151
                                           file_name, folder_name);
          gint response;
152

153
          response = screenshot_show_dialog (GTK_WINDOW (dialog->dialog),
154 155 156 157 158 159
                                             GTK_MESSAGE_WARNING,
                                             GTK_BUTTONS_YES_NO,
                                             _("Overwrite existing file?"),
                                             detail);

          g_free (folder);
160
          g_free (folder_uri);
161 162 163 164 165 166 167 168 169 170 171 172 173 174
          g_free (folder_name);
          g_free (file_name);
          g_free (detail);

          if (response == GTK_RESPONSE_YES)
            {
              self->priv->should_overwrite = TRUE;
              screenshot_save_to_file (self);

              return;
            }
        }
      else
        {
175
          screenshot_show_dialog (GTK_WINDOW (dialog->dialog),
176 177 178 179 180 181
                                  GTK_MESSAGE_ERROR,
                                  GTK_BUTTONS_OK,
                                  _("Unable to capture a screenshot"),
                                  _("Error creating file. Please choose another location and retry."));
        }

182
      gtk_widget_grab_focus (dialog->filename_entry);
183 184 185
    }
  else
    {
186
      g_critical ("Unable to save the screenshot: %s", error->message);
187 188
      screenshot_play_sound_effect ("dialog-error", _("Unable to capture a screenshot"));
      g_application_release (G_APPLICATION (self));
189 190
      if (screenshot_config->file != NULL)
        exit (EXIT_FAILURE);
191
    }
192 193 194 195 196 197 198 199
}

static void
save_pixbuf_ready_cb (GObject *source,
                      GAsyncResult *res,
                      gpointer user_data)
{
  GError *error = NULL;
200
  ScreenshotApplication *self = user_data;
201 202 203 204 205

  gdk_pixbuf_save_to_stream_finish (res, &error);

  if (error != NULL)
    {
206
      save_pixbuf_handle_error (self, error);
207 208 209 210
      g_error_free (error);
      return;
    }

211
  save_pixbuf_handle_success (self);
212 213
}

214 215
static void
find_out_writable_format_by_extension (gpointer data,
216
                                       gpointer user_data)
217 218 219 220 221 222 223 224 225 226 227 228 229
{
  GdkPixbufFormat *format     = (GdkPixbufFormat*) data;
  gchar          **name       = (gchar **) user_data;
  gchar          **extensions = gdk_pixbuf_format_get_extensions (format);
  gchar          **ptr        = extensions;
  gboolean         found      = FALSE;

  while (*ptr != NULL)
    {
      if (g_strcmp0 (*ptr, *name) == 0 &&
          gdk_pixbuf_format_is_writable (format) == TRUE)
        {
          *name = gdk_pixbuf_format_get_name (format);
230
          found = TRUE;
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
          break;
        }
      ptr++;
    }

  g_strfreev (extensions);

  /* Needing to duplicate string here because
   * gdk_pixbuf_format_get_name will return a duplicated string.
   */
  if (!found)
    *name = g_strdup (*name);
}

static gboolean
is_png (gchar *format)
{
  if (g_strcmp0 (format, "png") == 0)
    return TRUE;
  else
    return FALSE;
}

static gboolean
has_profile (ScreenshotApplication *self)
{
  if (self->priv->icc_profile_base64 != NULL)
    return TRUE;
  else
    return FALSE;
}

static void
save_with_description_and_profile (ScreenshotApplication *self,
                                   GFileOutputStream     *os,
                                   gchar                 *format)
{
  gdk_pixbuf_save_to_stream_async (self->priv->screenshot,
                                   G_OUTPUT_STREAM (os),
                                   format, NULL,
                                   save_pixbuf_ready_cb, self,
                                   "icc-profile", self->priv->icc_profile_base64,
                                   "tEXt::Software", "gnome-screenshot",
                                   NULL);
}
static void
save_with_description (ScreenshotApplication *self,
                       GFileOutputStream     *os,
                       gchar                 *format)
{
  gdk_pixbuf_save_to_stream_async (self->priv->screenshot,
                                   G_OUTPUT_STREAM (os),
                                   format, NULL,
                                   save_pixbuf_ready_cb, self,
                                   "tEXt::Software", "gnome-screenshot",
                                   NULL);
}

static void
save_with_no_profile_or_description (ScreenshotApplication *self,
                                     GFileOutputStream     *os,
                                     gchar                 *format)
{
  gdk_pixbuf_save_to_stream_async (self->priv->screenshot,
                                   G_OUTPUT_STREAM (os),
                                   format, NULL,
                                   save_pixbuf_ready_cb, self,
                                   NULL);
}

301 302 303 304 305 306 307 308
static void
save_file_create_ready_cb (GObject *source,
                           GAsyncResult *res,
                           gpointer user_data)
{
  ScreenshotApplication *self = user_data;
  GFileOutputStream *os;
  GError *error = NULL;
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  gchar *basename = g_file_get_basename (G_FILE (source));
  gchar *extension = g_strrstr (basename, ".");
  gchar *format = NULL;
  GSList *formats = NULL;

  if (extension == NULL)
    extension = "png";
  else
    extension++;

  format = extension;

  formats = gdk_pixbuf_get_formats();
  g_slist_foreach (formats,
                   find_out_writable_format_by_extension,
                   (gpointer) &format);
  g_slist_free (formats);
  g_free (basename);
327

328 329 330 331
  if (self->priv->should_overwrite)
    os = g_file_replace_finish (G_FILE (source), res, &error);
  else
    os = g_file_create_finish (G_FILE (source), res, &error);
332 333 334

  if (error != NULL)
    {
335
      save_pixbuf_handle_error (self, error);
336 337 338 339
      g_error_free (error);
      return;
    }

340 341 342 343 344 345 346
  if (is_png (format))
    {
      if (has_profile (self))
        save_with_description_and_profile (self, os, format);
      else
        save_with_description (self, os, format);
    }
347
  else
348 349 350
    {
      save_with_no_profile_or_description (self, os, format);
    }
351 352

  g_object_unref (os);
353
  g_free (format);
354 355 356
}

static void
357
screenshot_save_to_file (ScreenshotApplication *self)
358 359 360
{
  GFile *target_file;

361 362
  if (self->priv->dialog != NULL)
    screenshot_dialog_set_busy (self->priv->dialog, TRUE);
363

364
  target_file = g_file_new_for_uri (self->priv->save_uri);
365 366 367 368 369 370 371

  if (self->priv->should_overwrite)
    {
      g_file_replace_async (target_file,
                            NULL, FALSE,
                            G_FILE_CREATE_NONE,
                            G_PRIORITY_DEFAULT,
372
                            NULL,
373 374 375 376 377 378 379 380 381 382
                            save_file_create_ready_cb, self);
    }
  else
    {
      g_file_create_async (target_file,
                           G_FILE_CREATE_NONE,
                           G_PRIORITY_DEFAULT,
                           NULL,
                           save_file_create_ready_cb, self);
    }
383 384 385 386

  g_object_unref (target_file);
}

387 388 389 390 391 392 393
static void
screenshot_back (ScreenshotApplication *self)
{
  screenshot_close_interactive_dialog (self);
  screenshot_show_interactive_dialog (self);
}

394 395 396 397 398 399 400 401 402 403 404
static void
screenshot_save_to_clipboard (ScreenshotApplication *self)
{
  GtkClipboard *clipboard;

  clipboard = gtk_clipboard_get_for_display (gdk_display_get_default (),
                                             GDK_SELECTION_CLIPBOARD);
  gtk_clipboard_set_image (clipboard, self->priv->screenshot);
}

static void
405 406
screenshot_dialog_response_cb (ScreenshotResponse response,
                               ScreenshotApplication *self)
407
{
408
  switch (response)
409
    {
410
    case SCREENSHOT_RESPONSE_SAVE:
411 412 413 414
      /* update to the new URI */
      g_free (self->priv->save_uri);
      self->priv->save_uri = screenshot_dialog_get_uri (self->priv->dialog);
      screenshot_save_to_file (self);
415 416 417 418
      break;
    case SCREENSHOT_RESPONSE_COPY:
      screenshot_save_to_clipboard (self);
      break;
419 420 421
    case SCREENSHOT_RESPONSE_BACK:
      screenshot_back (self);
      break;
422
    default:
423
      g_assert_not_reached ();
424 425 426 427 428 429 430 431 432 433 434
      break;
    }
}

static void
build_filename_ready_cb (GObject *source,
                         GAsyncResult *res,
                         gpointer user_data)
{
  ScreenshotApplication *self = user_data;
  GError *error = NULL;
435
  char *save_path;
436

437 438 439 440 441 442 443 444 445 446 447 448 449
  save_path = screenshot_build_filename_finish (res, &error);
  if (save_path != NULL)
    {
      GFile *file;

      file = g_file_new_for_path (save_path);
      g_free (save_path);

      self->priv->save_uri = g_file_get_uri (file);
      g_object_unref (file);
    }
  else
    self->priv->save_uri = NULL;
450

451 452 453
  /* now release the application */
  g_application_release (G_APPLICATION (self));

454 455 456 457 458 459
  if (error != NULL)
    {
      g_critical ("Impossible to find a valid location to save the screenshot: %s",
                  error->message);
      g_error_free (error);

460
      if (screenshot_config->interactive)
461 462 463 464 465
        screenshot_show_dialog (NULL,
                                GTK_MESSAGE_ERROR,
                                GTK_BUTTONS_OK,
                                _("Unable to capture a screenshot"),
                                _("Error creating file"));
466
      else
467 468 469 470 471
        {
          screenshot_play_sound_effect ("dialog-error", _("Unable to capture a screenshot"));
          if (screenshot_config->file != NULL)
            exit (EXIT_FAILURE);
        }
472

473 474
      return;
    }
475

476 477
  screenshot_play_sound_effect ("screen-capture", _("Screenshot taken"));

478 479
  if (screenshot_config->interactive)
    {
480 481 482 483
      self->priv->dialog = screenshot_dialog_new (self->priv->screenshot,
                                                  self->priv->save_uri,
                                                  (SaveScreenshotCallback)screenshot_dialog_response_cb,
                                                  self);
484 485 486 487 488 489
    }
  else
    {
      g_application_hold (G_APPLICATION (self));
      screenshot_save_to_file (self);
    }
490 491 492 493 494 495 496 497 498 499 500 501
}

static void
finish_prepare_screenshot (ScreenshotApplication *self,
                           GdkRectangle *rectangle)
{
  GdkPixbuf *screenshot;

  screenshot = screenshot_get_pixbuf (rectangle);

  if (screenshot == NULL)
    {
502 503 504
      g_critical ("Unable to capture a screenshot of any window");

      if (screenshot_config->interactive)
505 506 507 508 509
        screenshot_show_dialog (NULL,
                                GTK_MESSAGE_ERROR,
                                GTK_BUTTONS_OK,
                                _("Unable to capture a screenshot"),
                                _("All possible methods failed"));
510 511 512 513
      else
        screenshot_play_sound_effect ("dialog-error", _("Unable to capture a screenshot"));

      g_application_release (G_APPLICATION (self));
514 515
      if (screenshot_config->file != NULL)
        exit (EXIT_FAILURE);
516 517

      return;
518 519 520 521 522 523 524 525 526 527 528 529
    }

  if (screenshot_config->take_window_shot)
    {
      switch (screenshot_config->border_effect[0])
        {
        case 's': /* shadow */
          screenshot_add_shadow (&screenshot);
          break;
        case 'b': /* border */
          screenshot_add_border (&screenshot);
          break;
530 531 532
        case 'v': /* vintage */
          screenshot_add_vintage (&screenshot);
          break;
533 534 535 536 537 538 539 540 541 542 543
        case 'n': /* none */
        default:
          break;
        }
    }

  self->priv->screenshot = screenshot;

  if (screenshot_config->copy_to_clipboard)
    {
      screenshot_save_to_clipboard (self);
544
      screenshot_play_sound_effect ("screen-capture", _("Screenshot taken"));
545 546 547 548 549 550 551
      
      if (screenshot_config->file == NULL)
        {
          g_application_release (G_APPLICATION (self));
          
          return;
        }
552 553 554 555 556 557 558 559 560
    }

  /* FIXME: apply the ICC profile according to the preferences.
   * org.gnome.ColorManager.GetProfileForWindow() does not exist anymore,
   * so we probably need to fetch the color profile of the screen where
   * the area/window was.
   *
   * screenshot_ensure_icc_profile (window);
   */
561 562 563 564 565 566 567
  if (screenshot_config->file != NULL)
    {
      self->priv->save_uri = g_file_get_uri (screenshot_config->file);
      self->priv->should_overwrite = TRUE;
      screenshot_save_to_file (self);
    }
  else
568
    screenshot_build_filename_async (screenshot_config->save_dir, NULL, build_filename_ready_cb, self);
569 570 571 572 573 574 575 576 577
}

static void
rectangle_found_cb (GdkRectangle *rectangle,
                    gpointer user_data)
{
  ScreenshotApplication *self = user_data;

  if (rectangle != NULL)
578 579 580
    {
      finish_prepare_screenshot (self, rectangle);
    }
581
  else
582 583 584 585 586 587 588
    {
      /* user dismissed the area selection, possibly show the dialog again */
      g_application_release (G_APPLICATION (self));

      if (screenshot_config->interactive)
        screenshot_show_interactive_dialog (self);
    }
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
}

static gboolean
prepare_screenshot_timeout (gpointer user_data)
{
  ScreenshotApplication *self = user_data;

  if (screenshot_config->take_area_shot)
    screenshot_select_area_async (rectangle_found_cb, self);
  else
    finish_prepare_screenshot (self, NULL);

  screenshot_save_config ();

  return FALSE;
}

static void
screenshot_start (ScreenshotApplication *self)
{
  guint delay = screenshot_config->delay * 1000;

  /* hold the GApplication while doing the async screenshot op */
  g_application_hold (G_APPLICATION (self));

614 615 616
  if (screenshot_config->take_area_shot)
    delay = 0;

617
  /* HACK: give time to the dialog to actually disappear.
618
   * We don't have any way to tell when the compositor has finished
619 620 621 622 623 624 625 626 627 628 629 630 631
   * re-drawing.
   */
  if (delay == 0 && screenshot_config->interactive)
    delay = 200;

  if (delay > 0)
    g_timeout_add (delay,
                   prepare_screenshot_timeout,
                   self);
  else
    g_idle_add (prepare_screenshot_timeout, self);
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
static gboolean version_arg = FALSE;

static const GOptionEntry entries[] = {
  { "clipboard", 'c', 0, G_OPTION_ARG_NONE, NULL, N_("Send the grab directly to the clipboard"), NULL },
  { "window", 'w', 0, G_OPTION_ARG_NONE, NULL, N_("Grab a window instead of the entire screen"), NULL },
  { "area", 'a', 0, G_OPTION_ARG_NONE, NULL, N_("Grab an area of the screen instead of the entire screen"), NULL },
  { "include-border", 'b', 0, G_OPTION_ARG_NONE, NULL, N_("Include the window border with the screenshot"), NULL },
  { "remove-border", 'B', 0, G_OPTION_ARG_NONE, NULL, N_("Remove the window border from the screenshot"), NULL },
  { "include-pointer", 'p', 0, G_OPTION_ARG_NONE, NULL, N_("Include the pointer with the screenshot"), NULL },
  { "delay", 'd', 0, G_OPTION_ARG_INT, NULL, N_("Take screenshot after specified delay [in seconds]"), N_("seconds") },
  { "border-effect", 'e', 0, G_OPTION_ARG_STRING, NULL, N_("Effect to add to the border (shadow, border, vintage or none)"), N_("effect") },
  { "interactive", 'i', 0, G_OPTION_ARG_NONE, NULL, N_("Interactively set options"), NULL },
  { "file", 'f', 0, G_OPTION_ARG_FILENAME, NULL, N_("Save screenshot directly to this file"), N_("filename") },
  { "version", 0, 0, G_OPTION_ARG_NONE, &version_arg, N_("Print version information and exit"), NULL },
  { NULL },
};

static gint
screenshot_application_handle_local_options (GApplication *app,
                                             GVariantDict *options)
{
  if (version_arg)
    {
      g_print ("%s %s\n", g_get_application_name (), VERSION);
      exit (EXIT_SUCCESS);
    }

659 660 661 662 663 664 665 666 667 668 669 670
  /* Start headless instances in non-unique mode */
  if (!g_variant_dict_contains (options, "interactive"))
    {
      GApplicationFlags old_flags;

      old_flags = g_application_get_flags (app);
      if ((old_flags & G_APPLICATION_IS_SERVICE) == 0)
        {
          g_application_set_flags (app, old_flags | G_APPLICATION_NON_UNIQUE);
        }
    }

671 672 673 674
  return -1;
}

static gint
Matthias Clasen's avatar
Matthias Clasen committed
675
screenshot_application_command_line (GApplication            *app,
676
                                     GApplicationCommandLine *command_line)
677
{
Matthias Clasen's avatar
Matthias Clasen committed
678
  ScreenshotApplication *self = SCREENSHOT_APPLICATION (app);
679 680 681 682 683
  gboolean clipboard_arg = FALSE;
  gboolean window_arg = FALSE;
  gboolean area_arg = FALSE;
  gboolean include_border_arg = FALSE;
  gboolean disable_border_arg = FALSE;
684
  gboolean include_pointer_arg = FALSE;
685 686 687
  gboolean interactive_arg = FALSE;
  gchar *border_effect_arg = NULL;
  guint delay_arg = 0;
688
  gchar *file_arg = NULL;
689 690
  GVariantDict *options;
  gint exit_status = EXIT_SUCCESS;
691 692
  gboolean res;

693 694 695 696 697 698 699 700 701
  options = g_application_command_line_get_options_dict (command_line);
  g_variant_dict_lookup (options, "clipboard", "b", &clipboard_arg);
  g_variant_dict_lookup (options, "window", "b", &window_arg);
  g_variant_dict_lookup (options, "area", "b", &area_arg);
  g_variant_dict_lookup (options, "include-border", "b", &include_border_arg);
  g_variant_dict_lookup (options, "remove-border", "b", &disable_border_arg);
  g_variant_dict_lookup (options, "include-pointer", "b", &include_pointer_arg);
  g_variant_dict_lookup (options, "interactive", "b", &interactive_arg);
  g_variant_dict_lookup (options, "border-effect", "&s", &border_effect_arg);
702
  g_variant_dict_lookup (options, "delay", "i", &delay_arg);
703
  g_variant_dict_lookup (options, "file", "^&ay", &file_arg);
704

705 706 707 708 709 710 711 712 713 714
  res = screenshot_config_parse_command_line (clipboard_arg,
                                              window_arg,
                                              area_arg,
                                              include_border_arg,
                                              disable_border_arg,
                                              include_pointer_arg,
                                              border_effect_arg,
                                              delay_arg,
                                              interactive_arg,
                                              file_arg);
715 716
  if (!res)
    {
717
      exit_status = EXIT_FAILURE;
718 719 720
      goto out;
    }

721 722
  /* interactive mode: trigger the dialog and wait for the response */
  if (screenshot_config->interactive)
723
    g_application_activate (app);
724
  else
Matthias Clasen's avatar
Matthias Clasen committed
725
    screenshot_start (self);
726 727

 out:
728
  return exit_status;
729 730
}

731 732 733
static void
screenshot_show_interactive_dialog (ScreenshotApplication *self)
{
734
  screenshot_interactive_dialog_new ((CaptureClickedCallback) screenshot_start, self);
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 760 761 762 763 764 765 766 767 768 769 770 771 772
static void
action_quit (GSimpleAction *action,
             GVariant *parameter,
             gpointer user_data)
{
  GList *windows = gtk_application_get_windows (GTK_APPLICATION (user_data));
  gtk_widget_destroy (g_list_nth_data (windows, 0));
}

static void
action_help (GSimpleAction *action,
             GVariant *parameter,
             gpointer user_data)
{
  GList *windows = gtk_application_get_windows (GTK_APPLICATION (user_data));
  screenshot_display_help (g_list_nth_data (windows, 0));
}

static void
action_about (GSimpleAction *action,
              GVariant *parameter,
              gpointer user_data)
{
  const gchar *authors[] = {
    "Emmanuele Bassi",
    "Jonathan Blandford",
    "Cosimo Cecchi",
    NULL
  };

  GList *windows = gtk_application_get_windows (GTK_APPLICATION (user_data));
  gtk_show_about_dialog (GTK_WINDOW (g_list_nth_data (windows, 0)),
                         "version", VERSION,
                         "authors", authors,
                         "program-name", _("Screenshot"),
                         "comments", _("Save images of your screen or individual windows"),
773
                         "logo-icon-name", SCREENSHOT_ICON_NAME,
774 775 776 777 778 779
                         "translator-credits", _("translator-credits"),
                         "license-type", GTK_LICENSE_GPL_2_0,
                         "wrap-license", TRUE,
                         NULL);
}

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
static void
action_screen_shot (GSimpleAction *action,
                    GVariant *parameter,
                    gpointer user_data)
{
  ScreenshotApplication *self = SCREENSHOT_APPLICATION (user_data);

  screenshot_config_parse_command_line (FALSE, /* clipboard */
                                        FALSE,  /* window */
                                        FALSE, /* area */
                                        FALSE, /* include border */
                                        FALSE, /* disable border */
                                        FALSE, /* include pointer */
                                        NULL,  /* border effect */
                                        0,     /* delay */
                                        FALSE, /* interactive */
                                        NULL); /* file */
  screenshot_start (self);
}

static void
action_window_shot (GSimpleAction *action,
                    GVariant *parameter,
                    gpointer user_data)
{
  ScreenshotApplication *self = SCREENSHOT_APPLICATION (user_data);

  screenshot_config_parse_command_line (FALSE, /* clipboard */
                                        TRUE,  /* window */
                                        FALSE, /* area */
                                        FALSE, /* include border */
                                        FALSE, /* disable border */
                                        FALSE, /* include pointer */
                                        NULL,  /* border effect */
                                        0,     /* delay */
                                        FALSE, /* interactive */
                                        NULL); /* file */
  screenshot_start (self);
}

820 821 822
static GActionEntry action_entries[] = {
  { "about", action_about, NULL, NULL, NULL },
  { "help", action_help, NULL, NULL, NULL },
823 824 825
  { "quit", action_quit, NULL, NULL, NULL },
  { "screen-shot", action_screen_shot, NULL, NULL, NULL },
  { "window-shot", action_window_shot, NULL, NULL, NULL }
826 827
};

828 829 830
static void
screenshot_application_startup (GApplication *app)
{
831 832
  GMenuModel *menu;
  GtkBuilder *builder;
833 834 835 836
  ScreenshotApplication *self = SCREENSHOT_APPLICATION (app);

  G_APPLICATION_CLASS (screenshot_application_parent_class)->startup (app);

837 838
  screenshot_load_config ();

839
  g_set_application_name (_("Screenshot"));
840
  gtk_window_set_default_icon_name (SCREENSHOT_ICON_NAME);
841

842 843
  g_action_map_add_action_entries (G_ACTION_MAP (self), action_entries,
                                   G_N_ELEMENTS (action_entries), self);
844

845 846 847
  builder = gtk_builder_new ();
  gtk_builder_add_from_resource (builder, "/org/gnome/screenshot/screenshot-app-menu.ui", NULL);
  menu = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
848
  gtk_application_set_app_menu (GTK_APPLICATION (app), menu);
849

850 851
  g_object_unref (builder);
  g_object_unref (menu);
852 853
}

854 855 856
static void
screenshot_application_activate (GApplication *app)
{
857 858 859 860 861 862 863 864 865
  GtkWindow *window;

  window = gtk_application_get_active_window (GTK_APPLICATION (app));
  if (window != NULL)
    {
      gtk_window_present (GTK_WINDOW (window));
      return;
    }

866 867 868 869
  screenshot_config->interactive = TRUE;
  screenshot_show_interactive_dialog (SCREENSHOT_APPLICATION (app));
}

870 871 872 873 874 875 876
static void
screenshot_application_finalize (GObject *object)
{
  ScreenshotApplication *self = SCREENSHOT_APPLICATION (object);

  g_clear_object (&self->priv->screenshot);
  g_free (self->priv->icc_profile_base64);
877
  g_free (self->priv->save_uri);
878 879 880 881 882 883 884 885 886 887 888 889

  G_OBJECT_CLASS (screenshot_application_parent_class)->finalize (object);
}

static void
screenshot_application_class_init (ScreenshotApplicationClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
  GApplicationClass *aclass = G_APPLICATION_CLASS (klass);

  oclass->finalize = screenshot_application_finalize;

890 891
  aclass->handle_local_options = screenshot_application_handle_local_options;
  aclass->command_line = screenshot_application_command_line;
892
  aclass->startup = screenshot_application_startup;
893
  aclass->activate = screenshot_application_activate;
894 895 896 897 898 899 900 901 902

  g_type_class_add_private (klass, sizeof (ScreenshotApplicationPriv));
}

static void
screenshot_application_init (ScreenshotApplication *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, SCREENSHOT_TYPE_APPLICATION,
                                            ScreenshotApplicationPriv);
903 904

  g_application_add_main_option_entries (G_APPLICATION (self), entries);
905 906 907
}

ScreenshotApplication *
908
screenshot_application_new (void)
909
{
910
  return g_object_new (SCREENSHOT_TYPE_APPLICATION,
911
                       "application-id", "org.gnome.Screenshot",
912
                       "flags", G_APPLICATION_HANDLES_COMMAND_LINE,
913
                       NULL);
914
}