summaryrefslogtreecommitdiff
path: root/src/contacts-crop-cheese-dialog.vala
blob: cc912c28ca2147a4b2c48f1da689172b1d755022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/*
 * Copyright (C) 2018 Elias Entrup <elias-git@flump.de>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

using Gtk;

[GtkTemplate (ui = "/org/gnome/Contacts/ui/contacts-crop-cheese-dialog.ui")]
public class Contacts.CropCheeseDialog : Gtk.Window {
  [GtkChild]
  private Stack stack;
  [GtkChild]
  private Button take_another_button;

  private Cc.CropArea crop_area;
  private const string STACK_NAME_CROP = "crop";
  private const string STACK_NAME_CHEESE = "cheese";

#if HAVE_CHEESE
  private Cheese.Flash flash;
  private Cheese.Widget cheese;
#endif

  public signal void picture_selected (Gdk.Pixbuf buf);

  public CropCheeseDialog.for_cheese (Gtk.Window parent) {
#if HAVE_CHEESE
    setup_widget (parent);
    this.flash = new Cheese.Flash (this);
    this.cheese = new Cheese.Widget ();
    this.cheese.show ();
    this.stack.add_named (this.cheese, STACK_NAME_CHEESE);
    this.stack.set_visible_child_name (STACK_NAME_CHEESE);
#endif
  }

  public CropCheeseDialog.for_crop (Gtk.Window parent, Gdk.Pixbuf pixbuf) {
    setup_widget (parent);
    this.take_another_button.visible = false;
    this.crop_area.set_picture (pixbuf);
  }

  /* this function is called from both constructors */
  private void setup_widget (Gtk.Window parent) {
    this.set_transient_for (parent);

    this.crop_area = new Cc.CropArea ();
    this.crop_area.set_vexpand (true);
    this.crop_area.set_hexpand (true);
    this.crop_area.set_min_size (48, 48);
    this.crop_area.set_constrain_aspect (true);
    this.stack.add_named (this.crop_area, STACK_NAME_CROP);
  }

  [GtkCallback]
  private void on_cancel_clicked (Button button) {
    this.destroy ();
  }

  [GtkCallback]
  private void on_take_another_clicked (Button button) {
#if HAVE_CHEESE
    this.stack.set_visible_child_name (STACK_NAME_CHEESE);
#endif
  }

  [GtkCallback]
  private void on_take_pic_clicked (Button button) {
#if HAVE_CHEESE
    var camera = this.cheese.get_camera () as Cheese.Camera;
    this.flash.fire ();
    camera.photo_taken.connect ( (pix) => {
        this.stack.set_visible_child_name (STACK_NAME_CROP);
        this.crop_area.set_picture(pix);
    });

    if (!camera.take_photo_pixbuf ()) {
      Utils.show_error_dialog (_("Unable to take photo."),
                               this as Gtk.Window);
    }
#endif
  }

  [GtkCallback]
  private void on_done_clicked (Button button) {
    picture_selected (this.crop_area.get_picture ());
    destroy();
  }
  
  [GtkCallback]
  private void on_destroy () {
#if HAVE_CHEESE
    /* Ensure the Vala garbage collector disposes of the Cheese widget.
     * This prevents the 'Device or resource busy' warnings, see:
     *   https://bugzilla.gnome.org/show_bug.cgi?id=700959
     */
    this.cheese = null;
#endif
  }

}