forked from sugarlabs/tuxpaint-wrapper-activity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.py
executable file
·76 lines (61 loc) · 2.65 KB
/
activity.py
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
# Copyright (C) 2012-2018 One Laptop per Child
# 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 subprocess
import os
import errno
import logging
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import GLib
from gi.repository import Gtk
from sugar3.activity.activity import Activity
class TuxPaintLauncher(Activity):
def __init__(self, handle):
# Initialize the parent
Activity.__init__(self, handle)
self.max_participants = 1
hbox = Gtk.HBox()
self.set_canvas(hbox)
self.show_all()
options = [
'tuxpaint', '--nolockfile', '--fullscreen=native', '--noprint']
doc_path = self.get_documents_path()
if doc_path is not None:
options.extend(('--savedir', doc_path))
proc = subprocess.Popen(options)
# Stay alive with a blank window mapped for at least 60 seconds
# so that the shell knows that we launched
GLib.timeout_add_seconds(60, Gtk.main_quit)
# but get rid of that window if the child exits beforehand
GLib.child_watch_add(proc.pid, Gtk.main_quit)
def get_documents_path(self):
"""Gets the path of the DOCUMENTS folder
If xdg-user-dir can not find the DOCUMENTS folder it returns
$HOME, which we omit. xdg-user-dir handles localization
(i.e. translation) of the filenames.
Returns: Path to $HOME/DOCUMENTS or None if an error occurs
Code from src/jarabe/journal/model.py
"""
try:
pipe = subprocess.Popen(['xdg-user-dir', 'DOCUMENTS'],
stdout=subprocess.PIPE)
documents_path = os.path.normpath(pipe.communicate()[0].strip())
if os.path.exists(documents_path) and \
os.environ.get('HOME') != documents_path:
return documents_path
except OSError as exception:
if exception.errno != errno.ENOENT:
logging.exception('Could not run xdg-user-dir')
return None