-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slideshow.py
executable file
·175 lines (128 loc) · 5.35 KB
/
Slideshow.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
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python
import os
import gtk
import webkit
import ConfigParser
import subprocess
import sys
import locale
from optparse import OptionParser
import gobject
'''
A basic GTK widget (webkit.WebView) which displays a slideshow in the
ubiquity-slideshow format. Feel free to copy and paste this to your application
and customize it as needed.
'''
class SlideshowViewer(webkit.WebView):
'''
@param path Path to the slideshow, in which the slideshow.conf file is stored.
@param locale Ideal locale to use for the slideshow
@param rtl True if the given locale should be displayed right-to-left
'''
def __init__(self, path, locale='c', rtl=False, controls=False):
self.path = path
config = ConfigParser.ConfigParser()
config.read(os.path.join(self.path,'slideshow.conf'))
self.locale = self._find_available_locale(locale)
slideshow_main = 'file://' + os.path.join(self.path, 'slides', 'index.html')
parameters = ''
if controls:
parameters += "?controls"
if self.locale != 'c': #slideshow will use default automatically
parameters += '?locale=' + self.locale
if rtl:
parameters += '?rtl'
webkit.WebView.__init__(self)
self.open(slideshow_main+'#'+parameters)
settings = self.get_settings()
settings.set_property("enable-default-context-menu", False)
#Recent webkit feature. See <http://trac.webkit.org/changeset/52087>.
settings.set_property("enable-file-access-from-file-uris", True)
config_width = int(config.get('Slideshow','width'))
config_height = int(config.get('Slideshow','height'))
self.set_size_request(config_width,config_height)
self.connect('navigation-policy-decision-requested', self._on_navigate_decision)
self.connect('navigation-requested', self._on_navigate)
self.connect('new-window-policy-decision-requested', self._on_new_window_decision)
self.connect('create-web-view', self._on_new_window)
'''
Determines the ideal locale for the slideshow, based on the given locale,
or 'c' if an ideal one is not available.
@param locale The full locale string, for example en_AU.UTF8
@return The available locale which best matches the input.
'''
def _find_available_locale(self, locale):
slides_dir = os.path.join(self.path, "slides")
locale_choice = 'c'
if os.path.exists( os.path.join(slides_dir, "loc."+locale) ):
locale_choice = locale
else:
ll_cc = locale.split('.')[0]
ll = ll_cc.split('_')[0]
if os.path.exists('%s/loc.%s' % (slides_dir,ll_cc)):
locale_choice = ll_cc
elif os.path.exists('%s/loc.%s' % (slides_dir,ll)):
locale_choice = ll
return locale_choice
def _new_browser_window(self, uri):
subprocess.Popen(['xdg-open', uri], close_fds=True)
def _on_navigate_decision(self, view, frame, req, action, decision):
reason = action.get_reason()
print(reason)
if reason == "link-clicked":
decision.use()
return False
decision.ignore()
return True
def _on_navigate(self, view, frame, req):
uri = req.get_uri()
print(uri)
self.new_browser_window(uri)
return True
def _on_new_window_decision(self, view, frame, req, action, decision):
uri = req.get_uri()
decision.ignore()
self.new_browser_window(uri)
return True
def _on_new_window(self, view, frame):
return True
def progress_increment(progressbar, fraction):
new_fraction = progressbar.get_fraction() + fraction
if new_fraction > 1:
progressbar.set_fraction(1.0)
install_progressbar.set_text("Finished pretending to install.")
return False
progressbar.set_fraction(new_fraction)
install_progressbar.set_text("Pretending to install... %d%%" % (new_fraction * 100))
return True
#Main program
default_path = os.path.join( os.path.abspath(os.path.dirname(sys.argv[0])) , 'build', 'ubuntu' )
default_locale = locale.getlocale()[0]
parser = OptionParser(usage="usage: %prog [options] [slideshow]")
parser.add_option("-l", "--locale", help="LOCALE to use for the slideshow", metavar="LOCALE", default=default_locale)
parser.add_option("-r", "--rtl", action="store_true", help="use output in right-to-left format")
parser.add_option("-c", "--controls", action="store_true", help="Enable controls in the slideshow (you may need to resize the window)")
parser.add_option("-p", "--path", help="path to the SLIDESHOW which will be presented", metavar="SLIDESHOW", default=default_path)
(options, args) = parser.parse_args()
options.path = os.path.abspath(options.path)
if os.path.exists(options.path) == False:
print("\033[91m * Please build the slideshow content first by using the make command * \033[0m")
sys.exit()
gtk.gdk.threads_init()
slideshow_window = gtk.Window()
slideshow_window.set_title("Ubiquity Slideshow with Webkit")
slideshow_window.connect('destroy',gtk.main_quit)
slideshow_container = gtk.VBox()
slideshow_container.set_spacing(8)
slideshow_window.add(slideshow_container)
slideshow = SlideshowViewer(options.path, locale=options.locale, rtl=options.rtl, controls=options.controls)
install_progressbar = gtk.ProgressBar()
install_progressbar.set_size_request(-1,30)
install_progressbar.set_text("Pretending to install. Please wait...")
install_progressbar.set_fraction(0)
slideshow_container.add(slideshow)
slideshow_container.add(install_progressbar)
slideshow_container.set_child_packing(install_progressbar, True, False, 0, 0)
slideshow_window.show_all()
install_timer = gobject.timeout_add_seconds(2, progress_increment, install_progressbar, 0.01)
gtk.main()