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
115
116
117
118
119
120
121
|
# Copyright (C) 2008 One Laptop Per Child
# Copyright (C) 2009 Simon Schampijer, James Zaki
#
# 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
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import tempfile
from gettext import gettext as _
import StringIO
import cairo
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GConf
import dbus
from sugar3.datastore import datastore
from sugar3.graphics import style
from sugar3 import env
from jarabe.model import shell
BOUND_KEYS = ['<alt>1', 'Print']
def handle_key_press(key):
tmp_dir = os.path.join(env.get_profile_path(), 'data')
fd, file_path = tempfile.mkstemp(dir=tmp_dir)
os.close(fd)
window = Gdk.get_default_root_window()
width, height = window.get_width(), window.get_height()
window_cr = Gdk.cairo_create(window)
window_surface = window_cr.get_target()
screenshot_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(screenshot_surface)
cr.set_source_surface(window_surface)
cr.paint()
screenshot_surface.write_to_png(file_path)
client = GConf.Client.get_default()
color = client.get_string('/desktop/sugar/user/color')
content_title = None
shell_model = shell.get_model()
zoom_level = shell_model.zoom_level
# TRANS: Nouns of what a screenshot contains
if zoom_level == shell_model.ZOOM_MESH:
content_title = _('Mesh')
elif zoom_level == shell_model.ZOOM_GROUP:
content_title = _('Group')
elif zoom_level == shell_model.ZOOM_HOME:
content_title = _('Home')
elif zoom_level == shell_model.ZOOM_ACTIVITY:
activity = shell_model.get_active_activity()
if activity != None:
content_title = activity.get_title()
if content_title == None:
content_title = _('Activity')
if content_title is None:
title = _('Screenshot')
else:
title = _('Screenshot of \"%s\"') % content_title
jobject = datastore.create()
try:
jobject.metadata['title'] = title
jobject.metadata['keep'] = '0'
jobject.metadata['buddies'] = ''
jobject.metadata['preview'] = _get_preview_data(screenshot_surface)
jobject.metadata['icon-color'] = color
jobject.metadata['mime_type'] = 'image/png'
jobject.file_path = file_path
datastore.write(jobject, transfer_ownership=True)
finally:
jobject.destroy()
del jobject
def _get_preview_data(screenshot_surface):
screenshot_width = screenshot_surface.get_width()
screenshot_height = screenshot_surface.get_height()
preview_width, preview_height = style.zoom(300), style.zoom(225)
preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
preview_width, preview_height)
cr = cairo.Context(preview_surface)
scale_w = preview_width * 1.0 / screenshot_width
scale_h = preview_height * 1.0 / screenshot_height
scale = min(scale_w, scale_h)
translate_x = int((preview_width - (screenshot_width * scale)) / 2)
translate_y = int((preview_height - (screenshot_height * scale)) / 2)
cr.translate(translate_x, translate_y)
cr.scale(scale, scale)
cr.set_source_rgba(1, 1, 1, 0)
cr.set_operator(cairo.OPERATOR_SOURCE)
cr.paint()
cr.set_source_surface(screenshot_surface)
cr.paint()
preview_str = StringIO.StringIO()
preview_surface.write_to_png(preview_str)
return dbus.ByteArray(preview_str.getvalue())
|