-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.lua
executable file
·551 lines (523 loc) · 20.8 KB
/
app.lua
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#!/usr/bin/env luajit
do -- try to load a config file if it exists
local ok, res = pcall(function()
local file = debug.getinfo(1, 'S').short_src:gsub('([\\/])[^\\/]*$', '%1')..'config.lua'
assert(loadfile(file))()
end)
if not ok then print(res) end
end
local lgi = require 'lgi'
local LuaOpenFing = lgi.package 'LuaOpenFing' -- package for custom widgets
local GLib = lgi.require 'GLib'
local Gtk = lgi.require 'Gtk'
local GObject = lgi.require 'GObject'
local Soup = lgi.require 'Soup'
local Gst = lgi.require 'Gst'
local GstVideo = lgi.require 'GstVideo'
local json = require 'json'
local unpack = unpack or table.unpack
-------------------------------------------------------------------------------
local Dao = {}
Dao.__index = Dao
function Dao:new(params)
local file = params.file
or (os.getenv 'HOME' and os.getenv 'HOME':gsub('/*$', '') .. '/.lua-openfing.json')
local objs = {}
local fd = io.open(file)
if fd then
objs = json.decode(fd:read '*all')
fd:close()
end
return setmetatable({objs=objs, file=file}, self)
end
function Dao:save(id, object)
self.objs[id] = object
local fd = assert(io.open(self.file, 'w'))
fd:write(json.encode(self.objs))
fd:close()
end
function Dao:load(id)
return self.objs[id]
end
local dao = Dao:new{}
-- Objects in dao:
-- courses = list of {code:string, eva=url:string, name=description:string}
-------------------------------------------------------------------------------
local function versionCompare(a, b) -- similar to the '<' string operator
local ai, bi = 1, 1
while true do
local as, bs = a:match('^[^%d]*', ai), b:match('^[^%d]*', bi)
if as ~= bs then return as < bs end
ai, bi = ai + #as, bi + #bs
as, bs = a:match('^%d*', ai), b:match('^%d*', bi)
if as == '' or bs == '' then return bs ~= '' end
local an, bn = tonumber(as), tonumber(bs)
if an ~= bn then return an < bn end
ai, bi = ai + #as, bi + #bs
end
end
local function timeFormat(seconds)
if seconds < 0 then return '-'..timeFormat(-seconds) end
return ('%d:%02d'):format(math.floor(seconds / 60), math.floor(seconds) % 60)
end
local function guiPlayer(app, course, classesList, i, choose)
local closed = false
local dao_id = ('class/%s/%s'):format(course.id, classesList[i].id)
local status = dao:load(dao_id) or {progress=''}
status.minute = 0
local vbox = Gtk.Box {orientation = 'VERTICAL', margin = 10, spacing = 10}
local video = Gtk.DrawingArea {expand = true}
local controls = Gtk.Box {orientation = 'HORIZONTAL'}
local currentFormat, ignoreNextStreamError = 'MP4', false
local back = Gtk.Button {
label = 'Atrás',
image = Gtk.Image { stock = Gtk.STOCK_GO_BACK, icon_size = Gtk.IconSize.BUTTON },
margin_right = 10
}
local playPause = Gtk.Button {
image = Gtk.Image { stock = Gtk.STOCK_MEDIA_PLAY, icon_size = Gtk.IconSize.BUTTON },
}
local stop = Gtk.Button {
image = Gtk.Image { stock = Gtk.STOCK_MEDIA_STOP, icon_size = Gtk.IconSize.BUTTON },
}
local time = Gtk.Label {
margin_left = 5
}
local slider = Gtk.Scale {
orientation = 'HORIZONTAL', draw_value = false,
adjustment = Gtk.Adjustment {
value = 0, lower = 0, upper = status.duration and status.duration / Gst.SECOND or 100,
step_increment = 1, page_increment = 10 },
hexpand = true, margin_left = 5
}
local remtime = Gtk.Label {
margin_left = 5, margin_right = 5
}
local speed
local menub = Gtk.MenuButton {
direction = 'UP', halign = 'END',
popup = Gtk.Menu {
Gtk.MenuItem { label = 'Velocidad', visible = true, submenu = Gtk.Menu {
Gtk.MenuItem { label = '0.5x', visible = true, on_activate = function()speed(0.5)end },
Gtk.MenuItem { label = '0.75x', visible = true, on_activate = function()speed(0.75)end },
Gtk.MenuItem { label = 'Normal', visible = true, on_activate = function()speed(1)end },
Gtk.MenuItem { label = '1.25x', visible = true, on_activate = function()speed(1.25)end },
Gtk.MenuItem { label = '1.5x', visible = true, on_activate = function()speed(1.5)end },
Gtk.MenuItem { label = '2x', visible = true, on_activate = function()speed(2)end },
}}
}
}
local playbin = Gst.ElementFactory.make('playbin', 'playbin')
vbox:add(video)
vbox:add(controls)
controls:add(back)
controls:add(playPause)
controls:add(stop)
controls:add(time)
controls:add(slider)
controls:add(remtime)
controls:add(menub)
function vbox:on_realize()
app:title(('OpenFING - %s: %s (%s)'):format(course.id, classesList[i].id, classesList[i].description))
end
function video:on_realize()
local id
id = lgi.require 'GdkX11'.X11Window.get_xid(self:get_window())
GstVideo.VideoOverlay.set_window_handle(playbin, id)
playbin:set_state 'PLAYING'
end
function video:on_draw(cr)
if not ({PLAYING=1, PAUSED=1})[playbin.current_state] then
-- draw a black rectangle to avoid garbage
local rec = self:get_allocation()
cr:set_source_rgb(0, 0, 0)
cr:rectangle(0, 0, rec.width, rec.height)
cr:fill()
end
return false
end
function back:on_clicked()
if closed then return end
closed = true
playbin:set_state 'NULL'
return app:pop()
end
function playPause:on_clicked()
local state = playbin.pending_state == 'VOID_PENDING' and playbin.current_state or playbin.pending_state
playbin:set_state(state == 'PLAYING' and 'PAUSED' or 'PLAYING')
end
function stop:on_clicked()
playbin:set_state 'NULL'
playPause.image = Gtk.Image { stock = Gtk.STOCK_MEDIA_PLAY, icon_size = Gtk.IconSize.BUTTON }
video:queue_draw()
end
function slider:on_value_changed()
local value = slider:get_value()
local oldValue = status.position and status.position / Gst.SECOND or 0
if math.abs(value - oldValue) > 1 then
playbin:seek_simple('TIME', Gst.SeekFlags.FLUSH + Gst.SeekFlags.KEY_UNIT, value * Gst.SECOND)
time.label = timeFormat(value)
if value < oldValue and math.floor(value / 60) == status.minute then
-- exception: scrolling back in the same minute!
else
status.minute = math.ceil(value / 60)
end
end
end
playbin:get_bus().on_message = function(bus, message)
local t = message.type
if t.STATE_CHANGED then
if message.src == playbin then
local oldState, newState, pending = message:parse_state_changed()
playPause.image = Gtk.Image { stock = newState=='PLAYING' and Gtk.STOCK_MEDIA_PAUSE or Gtk.STOCK_MEDIA_PLAY, icon_size = Gtk.IconSize.BUTTON }
end
elseif t.ERROR then
local err, deb = message:parse_error()
if err.code == 'NOT_FOUND' and err.domain == Gst.ResourceError and currentFormat == 'MP4' then
GLib.idle_add(GLib.PRIORITY_LOW, function()
playbin.uri = ('http://openfing-video.fing.edu.uy/media/%s/%s_%02d.webm'):format(
course.id, course.id, tonumber(classesList[i].id))
playbin:set_state('PLAYING')
currentFormat = 'WEBM'
end)
ignoreNextStreamError = true
return
elseif err.code == 'FAILED' and err.domain == Gst.StreamError and ignoreNextStreamError then
print'ignoring'
ignoreNextStreamError = false
return
end
print(err, deb)
Gtk.MessageDialog {
text = ('Error code: %s (code=%s, domain=%s)\nwhile loading video\n%s'):format(
err.message, err.code, err.domain, deb),
message_type = 'ERROR', buttons = 'CLOSE',
on_response = Gtk.Widget.destroy
}:run()
--elseif t.BUFFERING or t.TAG then
else
--local types = {} for k, v in pairs(t) do table.insert(types, tostring(k)..'='..tostring(v)) end print('otra cosa?', message, table.concat(types,' '))
end
end
speed = function(s)
playbin:seek(s, 'TIME', Gst.SeekFlags.FLUSH + Gst.SeekFlags.ACCURATE,
'SET', playbin:query_position 'TIME', 'NONE', 0)
end
GLib.timeout_add(GLib.PRIORITY_LOW, 100, function()
if closed then return false end
local duration = playbin:query_duration 'TIME'
local position = playbin:query_position 'TIME'
if duration and duration ~= status.duration then
slider:set_range(0, duration / Gst.SECOND)
status.duration = duration
local minutes = math.floor(duration / Gst.SECOND / 60)
status.progress = (status.progress .. ('0'):rep(minutes)):sub(1, minutes + 1)
dao:save(dao_id, status)
end
if position and position ~= status.position then
status.position = position
-- Range.set_value triggers a value-changed callback, that's why we update status.position
-- before the signal is sent.
local seconds, rem = position / Gst.SECOND, (position - (status.duration or 0)) / Gst.SECOND
slider:set_value(seconds)
time.label = timeFormat(seconds)
remtime.label = timeFormat(rem)
if seconds and math.floor(seconds / 60) == status.minute + 1 then
status.progress = status.progress:sub(1, status.minute) .. '1' .. status.progress:sub(status.minute + 2)
status.minute = status.minute + 1
dao:save(dao_id, status)
end
end
return true
end)
playbin.uri = ('http://openfing-video.fing.edu.uy/media/%s/%s_%02d.mp4'):format(
course.id, course.id, tonumber(classesList[i].id))
playbin:get_bus():add_signal_watch()
return vbox
end
LuaOpenFing:class('ProgressCellRenderer', Gtk.CellRendererText)
function LuaOpenFing.ProgressCellRenderer:do_render(cr, widget, background_area, cell_area, flags)
cr:new_sub_path()
local text = self.text
local degrees = math.pi / 180
local x, y = cell_area.x + 1, cell_area.y + cell_area.height * .2
local radius = cell_area.height / 3
local width, height = radius * 2 + #self.text, math.min(cell_area.height * .6, 30)
cr:arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees)
cr:arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees)
cr:arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees)
cr:arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees)
cr:close_path()
cr:stroke()
for k,v in pairs(self.priv) do
print('foo',k,v)
end
cr:set_source_rgb(1, 0, 0)
cr:new_path()
local from, to = text:find '1+'
while from do
cr:move_to(cell_area.x + 1 + radius + from - 1, cell_area.y + cell_area.height / 2)
cr:line_to(cell_area.x + 1 + radius + to, cell_area.y + cell_area.height / 2)
from, to = text:find('1+', to+1)
end
cr:stroke()
end
function LuaOpenFing.ProgressCellRenderer:do_get_preferred_width(...)
local m = 22 + #self.text
return m, m
end
function LuaOpenFing.ProgressCellRenderer:do_get_preferred_height_for_width(...)
return 10, 10
end
function LuaOpenFing.ProgressCellRenderer:do_get_preferred_height(...)
return 10, 10
end
local function guiClasses(app, course)
local grid = Gtk.Grid {
margin = 10
}
local label = Gtk.Label {
label = 'Elija clase del curso ' .. course.description .. ':'
}
local classesScroll = Gtk.ScrolledWindow {
expand = true, margin_top = 10
}
local classesModel = Gtk.ListStore.new {
GObject.Type.STRING,
GObject.Type.STRING,
GObject.Type.STRING
}
local classes = Gtk.TreeView {
expand = true,
model = classesModel,
Gtk.TreeViewColumn {title = "Id", sort_column_id = 0, { Gtk.CellRendererText {}, { text = 1 } }},
Gtk.TreeViewColumn {title = "Nombre", sort_column_id = 1, { Gtk.CellRendererText {}, { markup = 2 } }},
Gtk.TreeViewColumn {title = "Progreso", sort_column_id = 1, { LuaOpenFing.ProgressCellRenderer {}, { text = 3 } }},
}
local back = Gtk.Button {
label = 'Atrás',
image = Gtk.Image { stock = Gtk.STOCK_GO_BACK, icon_size = Gtk.IconSize.BUTTON },
hexpand = true, halign = 'START', margin_top = 10
}
local eva = Gtk.Button {
label = 'Web del curso',
image = Gtk.Image { stock = Gtk.STOCK_INFO, icon_size = Gtk.IconSize.BUTTON },
margin_top = 10,
sensitive = false
}
local go = Gtk.Button {
label = 'Ver video',
image = Gtk.Image { stock = Gtk.STOCK_GO_FORWARD, icon_size = Gtk.IconSize.BUTTON },
halign = 'END', margin_left = 10, margin_top = 10,
sensitive = false
}
local markVideo
local menu = Gtk.Menu {
Gtk.MenuItem { label = 'Marcar video como visto.', visible = true, on_activate = function()markVideo(true)end },
Gtk.MenuItem { label = 'Marcar video como no visto.', visible = true, on_activate = function()markVideo(false)end }
}
grid:attach(label, 0, 0, 3, 1)
grid:attach(classesScroll, 0, 1, 3, 1)
classesScroll:add(classes)
grid:attach(back, 0, 2, 1, 1)
grid:attach(eva, 1, 2, 1, 1)
grid:attach(go, 2, 2, 1, 1)
menu:attach_to_widget(classes)
local info = {}
classes:get_selection().on_changed = function()
go.sensitive = true
end
function back.on_clicked()
return app:pop()
end
eva.on_clicked = function()
os.execute("xdg-open '" .. evaLink:gsub("'", "'\\''") .. "'")
end
function markVideo(viewed)
local row = classes:get_selection():get_selected_rows()[1]
if not row then return end
local dao_id = ('class/%s/%s'):format(course.id, classesModel[row][1])
local cls = dao:load(dao_id)
if not cls then return end
cls.progress = cls.progress:gsub('.', viewed and '1' or '0')
dao:save(dao_id, cls)
classesModel[row][3] = cls.progress
end
local function selectClass()
local currentClassId = classesModel[classes:get_selection():get_selected_rows()[1]][1]
local classesList = {}
local currentI
for i, classId in ipairs(info.classesKeys) do
classesList[i] = {id=classId, description=info.classes[classId]}
if classId == currentClassId then currentI = i end
end
app:push(guiPlayer(app, course, classesList, currentI, function(i)
local rowNumber
for j, row in ipairs(classesModel) do
if row[1] == classesList[i].id then rowNumber = j - 1 end
end
classes:set_cursor(Gtk.TreePath.new_from_string(tostring(rowNumber)))
end), function() -- called on player's pop
local idx = classes:get_selection():get_selected_rows()[1]
local classId = classesModel[idx][1]
local progress = (dao:load(('class/%s/%s'):format(course.id, classId)) or {progress=''}).progress
classesModel[idx][3] = progress
end)
end
go.on_clicked = selectClass
function classes:on_button_press_event(event)
if event.type == 'DOUBLE_BUTTON_PRESS' then selectClass() end
if event.type == 'BUTTON_PRESS' and event.button == 3 then
menu:popup(nil, nil, nil, event.button, event.time)
end
end
local msg = Soup.Message.new('GET', 'https://open.fing.edu.uy/data/' .. course.id .. '.json')
local result = Soup.SessionSync {}:send_message(msg)
function grid.on_realize()
app:title(('OpenFING - %s (%s)'):format(course.id, course.description))
end
if result ~= 200 then
Gtk.MessageDialog {
text = 'Error code: ' .. result .. '\nwhile requesting course info',
message_type = 'ERROR', buttons = 'CLOSE',
on_response = Gtk.Widget.destroy
}:run()
else
dao:save('course/'..course.id, json.decode(msg.response_body.data))
end
info = dao:load('course/'..course.id)
if info.eva and info.eva:match '^https?:' then
eva.sensitive = true
end
info.classesKeys = {}
for classId, _name in pairs(info.classes) do
table.insert(info.classesKeys, classId)
end
table.sort(info.classesKeys, versionCompare)
for i, classId in ipairs(info.classesKeys) do
local progress = (dao:load(('class/%s/%s'):format(course.id, classId)) or {progress=''}).progress
classesModel:append{classId, info.classes[classId], progress}
end
return grid
end
local function guiCourses(app)
local grid = Gtk.Grid {
margin = 10
}
local label = Gtk.Label {
label = 'Elija curso:'
}
local coursesScroll = Gtk.ScrolledWindow {
expand = true, margin_top = 10
}
local coursesModel = Gtk.ListStore.new {
GObject.Type.STRING,
GObject.Type.STRING
}
local courses = Gtk.TreeView {
expand = true,
model = coursesModel,
Gtk.TreeViewColumn {title = "Id", sort_column_id = 0, { Gtk.CellRendererText {}, { text = 1 } }},
Gtk.TreeViewColumn {title = "Nombre", sort_column_id = 1, { Gtk.CellRendererText {}, { markup = 2 } }},
}
local eva = Gtk.Button {
label = 'Web del curso',
image = Gtk.Image { stock = Gtk.STOCK_INFO, icon_size = Gtk.IconSize.BUTTON },
hexpand = true, halign = 'END', margin_top = 10,
sensitive = false
}
local go = Gtk.Button {
label = 'Siguiente',
image = Gtk.Image { stock = Gtk.STOCK_GO_FORWARD, icon_size = Gtk.IconSize.BUTTON },
halign = 'END', margin_left = 10, margin_top = 10,
sensitive = false
}
grid:attach(label, 0, 0, 2, 1)
grid:attach(coursesScroll, 0, 1, 2, 1)
coursesScroll:add(courses)
grid:attach(eva, 0, 2, 1, 1)
grid:attach(go, 1, 2, 1, 1)
courses:get_selection().on_changed = function()
eva.sensitive = true
go.sensitive = true
end
local evaLinks = {}
local function selectCourse()
local row = coursesModel[courses:get_selection():get_selected_rows()[1]]
app:push(guiClasses(app, {id=row[1], description=row[2]}))
end
function courses:on_button_press_event(event)
if event.type == 'DOUBLE_BUTTON_PRESS' then selectCourse() end
end
function eva:on_clicked()
for i, row in ipairs(courses:get_selection():get_selected_rows()) do
local url = evaLinks[coursesModel[row][1]]
if url:match '^https?:' then
os.execute("xdg-open '" .. evaLinks[coursesModel[row][1]]:gsub("'", "'\\''") .. "'")
end
end
end
go.on_clicked = selectCourse
grid.on_realize = function()
app:title 'OpenFING'
end
local msg = Soup.Message.new('GET', 'https://open.fing.edu.uy/data/courses.json')
local result = Soup.SessionSync {}:send_message(msg)
if result ~= 200 then
Gtk.MessageDialog {
text = 'Error code: ' .. result .. '\nwhile requesting courses',
message_type = 'ERROR', buttons = 'CLOSE',
on_response = Gtk.Widget.destroy
}:run()
else
dao:save('courses', json.decode(msg.response_body.data).courses)
end
for i, course in ipairs(dao:load 'courses') do
evaLinks[course.code] = course.eva
coursesModel:append{course.code, course.name}
end
return grid
end
local App = {}
App.__index = App
function App:title(text)
if text then
self.window.title = text
end
return self.window.title
end
-- gui objects never push themselves, instead they return the widget to be pushed
function App:push(gui, cb_on_pop)
if #self.children > 0 then
self.window:remove(self.children[#self.children].gui)
end
self.children[#self.children + 1] = {gui = gui, cb_on_pop = cb_on_pop}
self.window:add(gui)
self.window:show_all()
end
-- however they do pop by themselves.
function App:pop()
assert(#self.children > 0)
local cb_on_pop = self.children[#self.children].cb_on_pop
self.window:remove(self.children[#self.children].gui)
self.children[#self.children] = nil
if #self.children > 0 then
self.window:add(self.children[#self.children].gui)
end
if cb_on_pop then cb_on_pop() end
end
function App:run()
local res = setmetatable({children={}, titles={}}, self)
res.window = Gtk.Window {
default_width = 640, default_height = 480,
has_resize_grip = true
}
function res.window:on_destroy()
if self == res.window then Gtk.main_quit() end
end
res.window:show_all()
res:push(guiCourses(res))
Gtk.main()
end
App:run()
-- vi: et sw=4