forked from exebetche/vlsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlsub.lua
2042 lines (1777 loc) · 53.1 KB
/
vlsub.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
VLSub Extension for VLC media player 1.1 and 2.0
Copyright 2013 Guillaume Le Maout
Authors: Guillaume Le Maout
Contact: http://addons.videolan.org/messages/?action=newmessage&username=exebetche
Bug report: http://addons.videolan.org/content/show.php/?content=148752
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 Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]
--[[ Global var ]]--
-- You can set here your default language by replacing nil with your language code (see below)
-- Example:
-- language = "fre",
-- language = "ger",
-- language = "eng",
-- ...
local options = {
language = nil,
downloadBehaviour = 'save',
langExt = false,
removeTag = false,
showMediaInformation = true,
progressBarSize = 80,
intLang = 'eng',
translations_avail = {
eng = 'English',
cze = 'Czech',
dan = 'Danish',
dut = 'Nederlands',
fre = 'Français',
ell = 'Greek',
baq = 'Basque',
pob = 'Brazilian Portuguese',
por = 'Portuguese (Portugal)',
rum = 'Romanian',
slo = 'Slovak',
spa = 'Spanish',
swe = 'Swedish',
ukr = 'Ukrainian',
hun = 'Hungarian'
},
translation = {
int_all = 'All',
int_descr = 'Download subtitles from OpenSubtitles.org',
int_research = 'Research',
int_config = 'Config',
int_configuration = 'Configuration',
int_help = 'Help',
int_search_hash = 'Search by hash',
int_search_name = 'Search by name',
int_title = 'Title',
int_season = 'Season (series)',
int_episode = 'Episode (series)',
int_show_help = 'Show help',
int_show_conf = 'Show config',
int_dowload_sel = 'Download selection',
int_close = 'Close',
int_ok = 'Ok',
int_save = 'Save',
int_cancel = 'Cancel',
int_bool_true = 'Yes',
int_bool_false = 'No',
int_search_transl = 'Search translations',
int_searching_transl = 'Searching translations ...',
int_int_lang = 'Interface language',
int_default_lang = 'Subtitles language',
int_dowload_behav = 'What to do with subtitles',
int_dowload_save = 'Load and save',
int_dowload_load = 'Load only',
int_dowload_manual = 'Manual download',
int_display_code = 'Display language code in file name',
int_remove_tag = 'Remove tags',
int_vlsub_work_dir = 'VLSub working directory',
int_os_username = 'Username',
int_os_password = 'Password',
int_help_mess = " Download subtitles from <a href='http://www.opensubtitles.org/'>opensubtitles.org</a> and display them while watching a video.<br>"..
" <br>"..
" <b><u>Usage:</u></b><br>"..
" <br>"..
" VLSub is meant to be used while you are watching the video, so start it first (if nothing is playing you will get a link to download the subtitles in your browser).<br>"..
" <br>"..
" Choose the language for your subtitles and click on the button corresponding to one of the two research methods provided by VLSub:<br>"..
" <br>"..
" <b>Method 1: Search by hash</b><br>"..
" It is recommended to try this method first, because it performs a research based on the video file print, so you can find subtitles synchronized with your video.<br>"..
" <br>"..
" <b>Method 2: Search by name</b><br>"..
" If you have no luck with the first method, just check the title is correct before clicking. If you search subtitles for a series, you can also provide a season and episode number.<br>"..
" <br>"..
" <b>Downloading Subtitles</b><br>"..
" Select one subtitle in the list and click on 'Download'.<br>"..
" It will be put in the same directory that your video, with the same name (different extension)"..
" so VLC will load them automatically the next time you'll start the video.<br>"..
" <br>"..
" <b>/!\\ Beware :</b> Existing subtitles are overwritten without asking confirmation, so put them elsewhere if they're important.<br>"..
" <br>"..
" Find more VLC extensions at <a href='http://addons.videolan.org'>addons.videolan.org</a>.",
int_no_support_mess = [[
<strong>VLSub is not working with Vlc 2.1.x on any platform</strong>
because the lua "net" module needed to interact with opensubtitles has been
removed in this release for the extensions.
<br>
<strong>Works with Vlc 2.2 on mac and linux.</strong>
<br>
<strong>On windows you have to install an older version of Vlc (2.0.8 for example)</strong>
to use Vlsub:
<br>
<a target="_blank" rel="nofollow" href="http://download.videolan.org/pub/videolan/vlc/2.0.8/">http://download.videola...pub/videolan/vlc/2.0.8/</a>
<br>
]],
action_login = 'Logging in',
action_logout = 'Logging out',
action_noop = 'Checking session',
action_search = 'Searching subtitles',
action_hash = 'Calculating movie hash',
mess_success = 'Success',
mess_error = 'Error',
mess_no_response = 'Server not responding',
mess_unauthorized = 'Request unauthorized',
mess_expired = 'Session expired, retrying',
mess_overloaded = 'Server overloaded, please retry later',
mess_no_input = 'Please use this method during playing',
mess_not_local = 'This method works with local file only (for now)',
mess_not_found = 'File not found',
mess_not_found2 = 'File not found (illegal character?)',
mess_no_selection = 'No subtitles selected',
mess_save_fail = 'Unable to save subtitles',
mess_click_link = 'Click here to open the file',
mess_complete = 'Research complete',
mess_no_res = 'No result',
mess_res = 'result(s)',
mess_loaded = 'Subtitles loaded',
mess_not_load = 'Unable to load subtitles',
mess_downloading = 'Downloading subtitle',
mess_dowload_link = 'Download link',
mess_err_conf_access ='Can\'t find a suitable path to save config, please set it manually',
mess_err_wrong_path ='the path contains illegal character, please correct it'
}
}
local languages = {
{'alb', 'Albanian'},
{'ara', 'Arabic'},
{'arm', 'Armenian'},
{'baq', 'Basque'},
{'ben', 'Bengali'},
{'bos', 'Bosnian'},
{'bre', 'Breton'},
{'bul', 'Bulgarian'},
{'bur', 'Burmese'},
{'cat', 'Catalan'},
{'chi', 'Chinese'},
{'hrv', 'Croatian'},
{'cze', 'Czech'},
{'dan', 'Danish'},
{'dut', 'Dutch'},
{'eng', 'English'},
{'epo', 'Esperanto'},
{'est', 'Estonian'},
{'fin', 'Finnish'},
{'fre', 'French'},
{'glg', 'Galician'},
{'geo', 'Georgian'},
{'ger', 'German'},
{'ell', 'Greek'},
{'heb', 'Hebrew'},
{'hin', 'Hindi'},
{'hun', 'Hungarian'},
{'ice', 'Icelandic'},
{'ind', 'Indonesian'},
{'ita', 'Italian'},
{'jpn', 'Japanese'},
{'kaz', 'Kazakh'},
{'khm', 'Khmer'},
{'kor', 'Korean'},
{'lav', 'Latvian'},
{'lit', 'Lithuanian'},
{'ltz', 'Luxembourgish'},
{'mac', 'Macedonian'},
{'may', 'Malay'},
{'mal', 'Malayalam'},
{'mon', 'Mongolian'},
{'nor', 'Norwegian'},
{'oci', 'Occitan'},
{'per', 'Persian'},
{'pol', 'Polish'},
{'por', 'Portuguese'},
{'pob', 'Brazilian Portuguese'},
{'rum', 'Romanian'},
{'rus', 'Russian'},
{'scc', 'Serbian'},
{'sin', 'Sinhalese'},
{'slo', 'Slovak'},
{'slv', 'Slovenian'},
{'spa', 'Spanish'},
{'swa', 'Swahili'},
{'swe', 'Swedish'},
{'syr', 'Syriac'},
{'tgl', 'Tagalog'},
{'tel', 'Telugu'},
{'tha', 'Thai'},
{'tur', 'Turkish'},
{'ukr', 'Ukrainian'},
{'urd', 'Urdu'},
{'vie', 'Vietnamese'}
}
-- Languages code conversion table: iso-639-1 to iso-639-3
-- See https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
local lang_os_to_iso = {
sq = "alb",
ar = "ara",
hy = "arm",
eu = "baq",
bn = "ben",
bs = "bos",
br = "bre",
bg = "bul",
my = "bur",
ca = "cat",
zh = "chi",
hr = "hrv",
cs = "cze",
da = "dan",
nl = "dut",
en = "eng",
eo = "epo",
et = "est",
fi = "fin",
fr = "fre",
gl = "glg",
ka = "geo",
de = "ger",
el = "ell",
he = "heb",
hi = "hin",
hu = "hun",
is = "ice",
id = "ind",
it = "ita",
ja = "jpn",
kk = "kaz",
km = "khm",
ko = "kor",
lv = "lav",
lt = "lit",
lb = "ltz",
mk = "mac",
ms = "may",
ml = "mal",
mn = "mon",
no = "nor",
oc = "oci",
fa = "per",
pl = "pol",
pt = "por",
po = "pob",
ro = "rum",
ru = "rus",
sr = "scc",
si = "sin",
sk = "slo",
sl = "slv",
es = "spa",
sw = "swa",
sv = "swe",
tl = "tgl",
te = "tel",
th = "tha",
tr = "tur",
uk = "ukr",
ur = "urd",
vi = "vie"
}
local dlg = nil
local input_table = {} -- General widget id reference
local select_conf = {} -- Drop down widget / option table association
--[[ VLC extension stuff ]]--
function descriptor()
return {
title = "VLsub 0.9.11",
version = "0.9.11",
author = "exebetche",
url = 'http://www.opensubtitles.org/',
shortdesc = "VLsub";
description = options.translation.int_descr,
capabilities = {"menu", "input-listener" }
}
end
function activate()
vlc.msg.dbg("[VLsub] Welcome")
if not check_config() then
vlc.msg.err("[VLsub] Unsupported VLC version")
return false
end
if vlc.input.item() then
openSub.getFileInfo()
openSub.getMovieInfo()
end
show_main()
end
function close()
deactivate()
end
function deactivate()
vlc.msg.dbg("[VLsub] Bye bye!")
if dlg then
dlg:hide()
end
if openSub.session.token and openSub.session.token ~= "" then
openSub.request("LogOut")
end
vlc.deactivate()
end
function menu()
return {
lang.int_research,
lang.int_config,
lang.int_help
}
end
function meta_changed()
return false
end
function input_changed()
collectgarbage()
set_interface_main()
collectgarbage()
end
--[[ Interface data ]]--
function interface_main()
dlg:add_label(lang["int_default_lang"]..':', 1, 1, 1, 1)
input_table['language'] = dlg:add_dropdown(2, 1, 2, 1)
dlg:add_button(lang["int_search_hash"], searchHash, 4, 1, 1, 1)
dlg:add_label(lang["int_title"]..':', 1, 2, 1, 1)
input_table['title'] = dlg:add_text_input(openSub.movie.title or "", 2, 2, 2, 1)
dlg:add_button(lang["int_search_name"], searchIMBD, 4, 2, 1, 1)
dlg:add_label(lang["int_season"]..':', 1, 3, 1, 1)
input_table['seasonNumber'] = dlg:add_text_input(openSub.movie.seasonNumber or "", 2, 3, 2, 1)
dlg:add_label(lang["int_episode"]..':', 1, 4, 1, 1)
input_table['episodeNumber'] = dlg:add_text_input(openSub.movie.episodeNumber or "", 2, 4, 2, 1)
input_table['mainlist'] = dlg:add_list(1, 5, 4, 1)
input_table['message'] = nil
input_table['message'] = dlg:add_label(' ', 1, 6, 4, 1)
dlg:add_button(lang["int_show_help"], show_help, 1, 7, 1, 1)
dlg:add_button(' '..lang["int_show_conf"]..' ', show_conf, 2, 7, 1, 1)
dlg:add_button(lang["int_dowload_sel"], download_subtitles, 3, 7, 1, 1)
dlg:add_button(lang["int_close"], deactivate, 4, 7, 1, 1)
assoc_select_conf('language', 'language', openSub.conf.languages, 2, lang["int_all"])
display_subtitles()
end
function set_interface_main()
-- Update movie title and co. if video input change
if not type(input_table['title']) == 'userdata' then return false end
openSub.getFileInfo()
openSub.getMovieInfo()
input_table['title']:set_text(openSub.movie.title or "")
input_table['episodeNumber']:set_text(openSub.movie.episodeNumber or "")
input_table['seasonNumber']:set_text(openSub.movie.seasonNumber or "")
end
function interface_config()
input_table['intLangLab'] = dlg:add_label(lang["int_int_lang"]..':', 1, 1, 1, 1)
input_table['intLangBut'] = dlg:add_button(lang["int_search_transl"], get_available_translations, 2, 1, 1, 1)
input_table['intLang'] = dlg:add_dropdown(3, 1, 1, 1)
dlg:add_label(lang["int_default_lang"]..':', 1, 2, 2, 1)
input_table['default_language'] = dlg:add_dropdown(3, 2, 1, 1)
dlg:add_label(lang["int_dowload_behav"]..':', 1, 3, 2, 1)
input_table['downloadBehaviour'] = dlg:add_dropdown(3, 3, 1, 1)
dlg:add_label(lang["int_display_code"]..':', 1, 4, 0, 1)
input_table['langExt'] = dlg:add_dropdown(3, 4, 1, 1)
dlg:add_label(lang["int_remove_tag"]..':', 1, 5, 0, 1)
input_table['removeTag'] = dlg:add_dropdown(3, 5, 1, 1)
if openSub.conf.dirPath then
if openSub.conf.os == "win" then
dlg :add_label("<a href='file:///"..openSub.conf.dirPath.."'>"..lang["int_vlsub_work_dir"].."</a>", 1, 6, 2, 1)
else
dlg :add_label("<a href='"..openSub.conf.dirPath.."'>"..lang["int_vlsub_work_dir"].."</a>", 1, 6, 2, 1)
end
else
dlg :add_label(lang["int_vlsub_work_dir"], 1, 6, 2, 1)
end
input_table['dir_path'] = dlg:add_text_input(openSub.conf.dirPath, 2, 6, 2, 1)
dlg:add_label(lang["int_os_username"]..':', 1, 7, 0, 1)
input_table['os_username'] = dlg:add_text_input(type(openSub.option.os_username) == "string" and openSub.option.os_username or "", 2, 7, 2, 1)
dlg:add_label(lang["int_os_password"]..':', 1, 8, 0, 1)
input_table['os_password'] = dlg:add_text_input(type(openSub.option.os_password) == "string" and openSub.option.os_password or "", 2, 8, 2, 1)
input_table['message'] = nil
input_table['message'] = dlg:add_label(' ', 1, 9, 3, 1)
dlg:add_button(lang["int_cancel"], show_main, 2, 10, 1, 1)
dlg:add_button(lang["int_save"], apply_config, 3, 10, 1, 1)
input_table['langExt']:add_value(lang["int_bool_"..tostring(openSub.option.langExt)], 1)
input_table['langExt']:add_value(lang["int_bool_"..tostring(not openSub.option.langExt)], 2)
input_table['removeTag']:add_value(lang["int_bool_"..tostring(openSub.option.removeTag)], 1)
input_table['removeTag']:add_value(lang["int_bool_"..tostring(not openSub.option.removeTag)], 2)
assoc_select_conf('intLang', 'intLang', openSub.conf.translations_avail, 2)
assoc_select_conf('default_language', 'language', openSub.conf.languages, 2, lang["int_all"])
assoc_select_conf('downloadBehaviour', 'downloadBehaviour', openSub.conf.downloadBehaviours, 1)
end
function interface_help()
local help_html = lang["int_help_mess"]
input_table['help'] = dlg:add_html(help_html, 1, 1, 4, 1)
dlg:add_label(string.rep (" ", 100), 1, 2, 3, 1)
dlg:add_button(lang["int_ok"], show_main, 4, 2, 1, 1)
end
function interface_no_support()
local no_support_html = lang["int_no_support_mess"]
input_table['no_support'] = dlg:add_html(no_support_html, 1, 1, 4, 1)
dlg:add_label(string.rep (" ", 100), 1, 2, 3, 1)
end
function trigger_menu(dlg_id)
if dlg_id == 1 then
close_dlg()
dlg = vlc.dialog(openSub.conf.useragent)
interface_main()
elseif dlg_id == 2 then
close_dlg()
dlg = vlc.dialog(openSub.conf.useragent..': '..lang["int_configuration"])
interface_config()
elseif dlg_id == 3 then
close_dlg()
dlg = vlc.dialog(openSub.conf.useragent..': '..lang["int_help"])
interface_help()
end
collectgarbage() --~ !important
end
function show_main()
trigger_menu(1)
end
function show_conf()
trigger_menu(2)
end
function show_help()
trigger_menu(3)
end
function close_dlg()
vlc.msg.dbg("[VLSub] Closing dialog")
if dlg ~= nil then
--~ dlg:delete() -- Throw an error
dlg:hide()
end
dlg = nil
input_table = nil
input_table = {}
collectgarbage() --~ !important
end
--[[ Drop down / config association]]--
function assoc_select_conf(select_id, option, conf, ind, default)
-- Helper for i/o interaction between drop down and option list (lang...)
select_conf[select_id] = {cf = conf, opt = option, dflt = default, ind = ind}
set_default_option(select_id)
display_select(select_id)
end
function set_default_option(select_id)
-- Put the selected option of a list in first place of the associated table
local opt = select_conf[select_id].opt
local cfg = select_conf[select_id].cf
local ind = select_conf[select_id].ind
if openSub.option[opt] then
table.sort(cfg, function(a, b)
if a[1] == openSub.option[opt] then
return true
elseif b[1] == openSub.option[opt] then
return false
else
return a[ind] < b[ind]
end
end)
end
end
function display_select(select_id)
-- Display the drop down values with an optional default value at the top
local conf = select_conf[select_id].cf
local opt = select_conf[select_id].opt
local option = openSub.option[opt]
local default = select_conf[select_id].dflt
local default_isset = false
if not default then
default_isset = true
end
for k, l in ipairs(conf) do
if default_isset then
input_table[select_id]:add_value(l[2], k)
else
if option then
input_table[select_id]:add_value(l[2], k)
input_table[select_id]:add_value(default, 0)
else
input_table[select_id]:add_value(default, 0)
input_table[select_id]:add_value(l[2], k)
end
default_isset = true
end
end
end
--[[ Config & interface localization]]--
function check_config()
-- Make a copy of english translation to use it as default
-- in case some element aren't translated in other translations
eng_translation = {}
for k, v in pairs(openSub.option.translation) do
eng_translation[k] = v
end
-- Get available translation full name from code
trsl_names = {}
for i, lg in ipairs(languages) do
trsl_names[lg[1]] = lg[2]
end
if is_window_path(vlc.config.datadir()) then
openSub.conf.os = "win"
slash = "\\"
else
openSub.conf.os = "lin"
slash = "/"
end
local path_generic = {"lua", "extensions", "userdata", "vlsub"}
local dirPath = slash..table.concat(path_generic, slash)
local filePath = slash.."vlsub_conf.xml"
local config_saved = false
sub_dir = slash.."vlsub_subtitles"
-- Check if config file path is stored in vlc config
local other_dirs = {}
for path in vlc.config.get("sub-autodetect-path"):gmatch("[^,]+") do
if path:match(".*"..sub_dir.."$") then
openSub.conf.dirPath = path:gsub("%s*(.*)"..sub_dir.."%s*$", "%1")
config_saved = true
end
table.insert(other_dirs, path)
end
-- if not stored in vlc config
-- try to find a suitable config file path
if openSub.conf.dirPath then
if not is_dir(openSub.conf.dirPath) and
(openSub.conf.os == "lin" or
is_win_safe(openSub.conf.dirPath)) then
mkdir_p(openSub.conf.dirPath)
end
else
local userdatadir = vlc.config.userdatadir()
local datadir = vlc.config.datadir()
-- check if the config already exist
if file_exist(userdatadir..dirPath..filePath) then
-- in vlc.config.userdatadir()
openSub.conf.dirPath = userdatadir..dirPath
config_saved = true
elseif file_exist(datadir..dirPath..filePath) then
-- in vlc.config.datadir()
openSub.conf.dirPath = datadir..dirPath
config_saved = true
else
-- if not found determine an accessible path
local extension_path = slash..path_generic[1]
..slash..path_generic[2]
-- use the same folder as the extension if accessible
if is_dir(userdatadir..extension_path)
and file_touch(userdatadir..dirPath..filePath) then
openSub.conf.dirPath = userdatadir..dirPath
elseif file_touch(datadir..dirPath..filePath) then
openSub.conf.dirPath = datadir..dirPath
end
-- try to create working dir in user folder
if not openSub.conf.dirPath
and is_dir(userdatadir) then
if not is_dir(userdatadir..dirPath) then
mkdir_p(userdatadir..dirPath)
end
if is_dir(userdatadir..dirPath) and
file_touch(userdatadir..dirPath..filePath) then
openSub.conf.dirPath = userdatadir..dirPath
end
end
-- try to create working dir in vlc folder
if not openSub.conf.dirPath and
is_dir(datadir) then
if not is_dir(datadir..dirPath) then
mkdir_p(datadir..dirPath)
end
if file_touch(datadir..dirPath..filePath) then
openSub.conf.dirPath = datadir..dirPath
end
end
end
end
if openSub.conf.dirPath then
vlc.msg.dbg("[VLSub] Working directory: " ..
(openSub.conf.dirPath or "not found"))
openSub.conf.filePath = openSub.conf.dirPath..filePath
openSub.conf.localePath = openSub.conf.dirPath..slash.."locale"
if config_saved
and file_exist(openSub.conf.filePath) then
vlc.msg.dbg("[VLSub] Loading config file: "..openSub.conf.filePath)
load_config()
else
vlc.msg.dbg("[VLSub] No config file")
getenv_lang()
config_saved = save_config()
if not config_saved then
vlc.msg.dbg("[VLSub] Unable to save config")
end
end
-- Check presence of a translation file in "%vlsub_directory%/locale"
-- Add translation files to available translation list
local file_list = list_dir(openSub.conf.localePath)
local translations_avail = openSub.conf.translations_avail
if file_list then
for i, file_name in ipairs(file_list) do
local lg = string.gsub(file_name, "^(%w%w%w).xml$", "%1")
if lg and not openSub.option.translations_avail[lg] then
table.insert(translations_avail, {lg, trsl_names[lg]})
end
end
end
-- Load selected translation from file
if openSub.option.intLang ~= "eng"
and not openSub.conf.translated
then
local transl_file_path = openSub.conf.localePath..slash..openSub.option.intLang..".xml"
if file_exist(transl_file_path) then
vlc.msg.dbg("[VLSub] Loading translation from file: " .. transl_file_path)
load_transl(transl_file_path)
end
end
else
vlc.msg.dbg("[VLSub] Unable to find a suitable path to save config, please set it manually")
end
lang = nil
lang = options.translation -- just a short cut
if not vlc.net or not vlc.net.poll then
dlg = vlc.dialog(openSub.conf.useragent..': '..lang["mess_error"])
interface_no_support()
dlg:show()
return false
end
SetDownloadBehaviours()
if not openSub.conf.dirPath then
setError(lang["mess_err_conf_access"])
end
-- Set table list of available translations from assoc. array
-- so it is sortable
for k, l in pairs(openSub.option.translations_avail) do
if k == openSub.option.int_research then
table.insert(openSub.conf.translations_avail, 1, {k, l})
else
table.insert(openSub.conf.translations_avail, {k, l})
end
end
collectgarbage()
return true
end
function load_config()
-- Overwrite default conf with loaded conf
local tmpFile = io.open(openSub.conf.filePath, "rb")
if not tmpFile then return false end
local resp = tmpFile:read("*all")
tmpFile:flush()
tmpFile:close()
local option = parse_xml(resp)
for key, value in pairs(option) do
if type(value) == "table" then
if key == "translation" then
openSub.conf.translated = true
for k, v in pairs(value) do
openSub.option.translation[k] = v
end
else
openSub.option[key] = value
end
else
if value == "true" then
openSub.option[key] = true
elseif value == "false" then
openSub.option[key] = false
else
openSub.option[key] = value
end
end
end
collectgarbage()
end
function load_transl(path)
-- Overwrite default conf with loaded conf
local tmpFile = assert(io.open(path, "rb"))
local resp = tmpFile:read("*all")
tmpFile:flush()
tmpFile:close()
openSub.option.translation = nil
openSub.option.translation = parse_xml(resp)
collectgarbage()
end
function apply_translation()
-- Overwrite default conf with loaded conf
for k, v in pairs(eng_translation) do
if not openSub.option.translation[k] then
openSub.option.translation[k] = eng_translation[k]
end
end
end
function getenv_lang()
-- Retrieve the user OS language
local os_lang = os.getenv("LANG")
if os_lang then -- unix, mac
os_lang = string.sub(os_lang, 0, 2)
if type(lang_os_to_iso[os_lang]) then
openSub.option.language = lang_os_to_iso[os_lang]
end
else -- Windows
local lang_w = string.match(os.setlocale("", "collate"), "^[^_]+")
for i, v in ipairs(openSub.conf.languages) do
if v[2] == lang_w then
openSub.option.language = v[1]
end
end
end
end
function apply_config()
-- Apply user config selection to local config
local lg_sel = input_table['intLang']:get_value()
local sel_val
local opt
if lg_sel and lg_sel ~= 1
and openSub.conf.translations_avail[lg_sel] then
local lg = openSub.conf.translations_avail[lg_sel][1]
set_translation(lg)
SetDownloadBehaviours()
end
for select_id, v in pairs(select_conf) do
if input_table[select_id] and select_conf[select_id] then
sel_val = input_table[select_id]:get_value()
opt = select_conf[select_id].opt
if sel_val == 0 then
openSub.option[opt] = nil
else
openSub.option[opt] = select_conf[select_id].cf[sel_val][1]
end
set_default_option(select_id)
end
end
openSub.option.os_username = input_table['os_username']:get_text()
openSub.option.os_password = input_table['os_password']:get_text()
if input_table["langExt"]:get_value() == 2 then
openSub.option.langExt = not openSub.option.langExt
end
if input_table["removeTag"]:get_value() == 2 then
openSub.option.removeTag = not openSub.option.removeTag
end
-- Set a custom working directory
local dir_path = input_table['dir_path']:get_text()
local dir_path_err = false
if trim(dir_path) == "" then dir_path = nil end
if dir_path ~= openSub.conf.dirPath then
if openSub.conf.os == "lin"
or is_win_safe(dir_path)
or not dir_path then
local other_dirs = {}
for path in vlc.config.get("sub-autodetect-path"):gmatch("[^,]+") do
path = trim(path)
if path ~= (openSub.conf.dirPath or "")..sub_dir then
table.insert(other_dirs, path)
end
end
openSub.conf.dirPath = dir_path
if dir_path then
table.insert(other_dirs,
string.gsub(dir_path, "^(.-)[\\/]?$", "%1")..sub_dir)
if not is_dir(dir_path) then
mkdir_p(dir_path)
end
openSub.conf.filePath = openSub.conf.dirPath..slash.."vlsub_conf.xml"
openSub.conf.localePath = openSub.conf.dirPath..slash.."locale"
else
openSub.conf.filePath = nil
openSub.conf.localePath = nil
end
vlc.config.set("sub-autodetect-path", table.concat(other_dirs, ", "))
else
dir_path_err = true
setError(lang["mess_err_wrong_path"].."<br><b>"..string.gsub(dir_path, "[^%:%w%p%s§¤]+", "<span style='color:#B23'>%1</span>").."</b>")
end
end
if openSub.conf.dirPath and
not dir_path_err then
local config_saved = save_config()
trigger_menu(1)
if not config_saved then
setError(lang["mess_err_conf_access"])
end
else
setError(lang["mess_err_conf_access"])
end
end
function save_config()
-- Dump local config into config file
if openSub.conf.dirPath
and openSub.conf.filePath then
vlc.msg.dbg("[VLSub] Saving config file: " .. openSub.conf.filePath)
if file_touch(openSub.conf.filePath) then
local tmpFile = assert(io.open(openSub.conf.filePath, "wb"))
local resp = dump_xml(openSub.option)
tmpFile:write(resp)
tmpFile:flush()
tmpFile:close()
tmpFile = nil
else
return false
end
collectgarbage()
return true
else
vlc.msg.dbg("[VLSub] Unable fount a suitable path to save config, please set it manually")
setError(lang["mess_err_conf_access"])
return false
end
end
function SetDownloadBehaviours()
openSub.conf.downloadBehaviours = nil
openSub.conf.downloadBehaviours = {
{'save', lang["int_dowload_save"]},
{'load', lang["int_dowload_load"]},
{'manual', lang["int_dowload_manual"]}
}
end
function get_available_translations()
-- Get all available translation files from the internet
-- (drop previous direct download from github repo because of problem with github https CA certficate on OS X an XP)
-- https://github.com/exebetche/vlsub/tree/master/locale
local translations_url = "http://addons.videolan.org/CONTENT/content-files/148752-vlsub_translations.xml"
if input_table['intLangBut']:get_text() == lang["int_search_transl"] then
openSub.actionLabel = lang["int_searching_transl"]
local translations_content, lol = get(translations_url)
all_trsl = parse_xml(translations_content)
local lg, trsl
for lg, trsl in pairs(all_trsl) do
if lg ~= options.intLang[1] and not openSub.option.translations_avail[lg] then
openSub.option.translations_avail[lg] = trsl_names[lg] or ""
table.insert(openSub.conf.translations_avail, {lg, trsl_names[lg]})
input_table['intLang']:add_value(trsl_names[lg], #openSub.conf.translations_avail)
end
end
setMessage(success_tag(lang["mess_complete"]))
collectgarbage()
end
end
function set_translation(lg)
openSub.option.translation = nil
openSub.option.translation = {}
if lg == 'eng' then
for k, v in pairs(eng_translation) do
openSub.option.translation[k] = v
end
else
-- If translation file exists in /locale directory load it
if openSub.conf.localePath
and file_exist(openSub.conf.localePath..slash..lg..".xml") then
local transl_file_path = openSub.conf.localePath..slash..lg..".xml"
vlc.msg.dbg("[VLSub] Loading translation from file: " .. transl_file_path)
load_transl(transl_file_path)
apply_translation()
else
-- Load translation file from internet
if not all_trsl then
get_available_translations()
end
if not all_trsl or not all_trsl[lg] then
vlc.msg.dbg("[VLSub] Error, translation not found")
return false
end
openSub.option.translation = all_trsl[lg]
apply_translation()
all_trsl = nil
end
end
lang = nil
lang = openSub.option.translation
collectgarbage()
end