-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript_manager.lua
961 lines (816 loc) · 29 KB
/
script_manager.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
--[[
This file is part of darktable,
copyright (c) 2018 Bill Ferguson <[email protected]>
darktable 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 3 of the License, or
(at your option) any later version.
darktable 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 darktable. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
script_manager.lua - a tool for managing the darktable lua scripts
script_manager is designed to run as a standalone script so that it
may be used as a drop in luarc file in the user's $HOME/.config/darktable
($HOME/AppData/Local/darktable on windows) directory. It may also be
required from a luarc file.
On startup script_manager checks to see if there is an existing scripts directory.
If there is an existing lua scripts directory then it is read to see what scripts are present.
Scripts are sorted by "category" based on what subdirectory they are found in, thus with a lua
scripts directory that matched the current repository the categories would be contrib, examples,
offical, and tools. Each script has an Enable/Disable button to enable or disable the script.
A link is created to the user's Downloads directory on linux, unix and MacOS. Windows users must create the
link manually using mklink.exe. Additional "un-official" scripts may be downloaded
from other sources and placed in the users Downloads directory. These scripts all fall in a downloads category.
They also each have an Enable/Disable button.
]]
local dt = require "darktable"
local gettext = dt.gettext
-- Tell gettext where to find the .mo file translating messages for a particular domain
gettext.bindtextdomain("script_manager",dt.configuration.config_dir.."/lua/locale/")
local function _(msgid)
return gettext.dgettext("script_manager", msgid)
end
collectgarbage("stop")
-- - - - - - - - - - - - - - - - - - - - - - - -
-- C O N S T A N T S
-- - - - - - - - - - - - - - - - - - - - - - - -
-- path separator
local PS = dt.configuration.running_os == "windows" and "\\" or "/"
-- command separator
local CS = dt.configuration.running_os == "windows" and "&" or ";"
local LUA_DIR = dt.configuration.config_dir .. PS .. "lua"
local LUA_SCRIPT_REPO = "https://github.com/darktable-org/lua-scripts.git"
dt.print_log("LUA_DIR is " .. LUA_DIR)
-- - - - - - - - - - - - - - - - - - - - - - - -
-- N A M E S P A C E
-- - - - - - - - - - - - - - - - - - - - - - - -
local script_manager = {}
local sm = script_manager
-- - - - - - - - - - - - - - - - - - - - - - - - - -
-- C O P I E D L I B R A R Y F U N C T I O N S
-- - - - - - - - - - - - - - - - - - - - - - - - - -
--[[
We can't rely on the libraries of existing functions in order for this script to run standalone.
Therefore the required library functions are copied here. In the lua scripts distribution version
this code will be removed and the library functions used.
]]
-- define the library stubs so that we don't have to
-- modify all the subroutine calls
local df = {} -- corresponds to lib/dtutils.file
function df.get_executable_path_preference(executable)
return dt.preferences.read("executable_paths", executable, "string")
end
function df.set_executable_path_preference(executable, path)
dt.preferences.write("executable_paths", executable, "string", path)
end
function df.executable_path_widget(executables)
local box_widgets = {}
table.insert(box_widgets, dt.new_widget("section_label"){label = "select executable(s)"})
for _, executable in pairs(executables) do
table.insert(box_widgets, dt.new_widget("label"){label = "select " .. executable .. " executable"})
local path = df.get_executable_path_preference(executable)
if not path then
path = ""
end
table.insert(box_widgets, dt.new_widget("file_chooser_button"){
title = "select " .. executable .. " executable",
value = path,
is_directory = false,
changed_callback = function(self)
if df.check_if_bin_exists(self.value) then
df.set_executable_path_preference(executable, self.value)
end
end}
)
end
local box = dt.new_widget("box"){
orientation = "vertical",
table.unpack(box_widgets)
}
return box
end
function df.check_if_file_exists(filepath)
local result
if (dt.configuration.running_os == 'windows') then
filepath = string.gsub(filepath, '[\\/]+', '\\')
result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)')
if not result then
result = false
end
elseif (dt.configuration.running_os == "linux") then
result = os.execute('test -e ' .. "\"" .. filepath .. "\"")
if not result then
result = false
end
else
local file = io.open(filepath, "r")
if file then
result = true
file:close()
else
result = false
end
end
return result
end
function df.check_if_bin_exists(bin)
local result = false
local path = nil
if string.match(bin, "/") or string.match(bin, "\\") then
path = bin
else
path = df.get_executable_path_preference(bin)
end
if string.len(path) > 0 then
if df.check_if_file_exists(path) then
if (string.match(path, ".exe$") or string.match(path, ".EXE$")) and dt.configuration.running_os ~= "windows" then
result = "wine " .. "\"" .. path .. "\""
else
result = "\"" .. path .. "\""
end
end
elseif dt.configuration.running_os == "linux" then
local p = io.popen("which " .. bin)
local output = p:read("*a")
p:close()
if string.len(output) > 0 then
result = output:sub(1,-2)
end
end
return result
end
function df.file_copy(fromFile, toFile)
local result = nil
-- if cp exists, use it
if df.check_if_bin_exists("cp") then
result = os.execute("cp '" .. fromFile .. "' '" .. toFile .. "'")
end
-- if cp was not present, or if cp failed, then a pure lua solution
if not result then
local fileIn, err = io.open(fromFile, 'rb')
if fileIn then
local fileOut, errr = io.open(toFile, 'w')
if fileOut then
local content = fileIn:read(4096)
while content do
fileOut:write(content)
content = fileIn:read(4096)
end
result = true
fileIn:close()
fileOut:close()
else
log.msg(log.error, errr)
end
else
log.msg(log.error, err)
end
end
return result
end
function df.file_move(fromFile, toFile)
local success = os.rename(fromFile, toFile)
if not success then
-- an error occurred, so let's try using the operating system function
if df.check_if_bin_exists("mv") then
success = os.execute("mv '" .. fromFile .. "' '" .. toFile .. "'")
end
-- if the mv didn't exist or succeed, then...
if not success then
-- pure lua solution
success = df.file_copy(fromFile, toFile)
if success then
os.remove(fromFile)
else
log.msg(log.error, "Unable to move " .. fromFile .. " to " .. toFile .. ". Leaving " .. fromFile .. " in place.")
end
end
end
return success -- nil on error, some value if success
end
local du = {} -- corresponds to lib/dutils
function du.join(tabl, pat)
returnstr = ""
for i,str in pairs(tabl) do
returnstr = returnstr .. str .. pat
end
return string.sub(returnstr, 1, -(pat:len() + 1))
end
local dtsys = {} -- corresponds to lib/dtutils.system
function dtsys.windows_command(command)
local result = 1
local fname = dt.configuration.tmp_dir .. "/run_command.bat"
local file = io.open(fname, "w")
if file then
dt.print_log("opened file")
command = string.gsub(command, "%%", "%%%%") -- escape % from windows shell
file:write(command)
file:close()
result = dt.control.execute(fname)
dt.print_log("result from windows command was " .. result)
os.remove(fname)
else
dt.print_error("Windows command failed: unable to create batch file")
end
return result
end
-- - - - - - - - - - - - - - - - - - - - - - - -
-- F U N C T I O N S
-- - - - - - - - - - - - - - - - - - - - - - - -
local function prequire(script)
dt.print_log("Loading " .. script)
local status, lib = pcall(require, script)
if status then
dt.print_log("Loaded " .. script)
else
dt.print_error("Error loading " .. script)
dt.print_error(lib)
end
return status, lib
end
local function update_combobox_choices(combobox, choice_table, selected)
local items = #combobox
local choices = #choice_table
for i, name in ipairs(choice_table) do
combobox[i] = name
end
if choices < items then
for j = items, choices + 1, -1 do
combobox[j] = nil
end
end
combobox.value = selected
end
local function install_scripts()
local result = false
if df.check_if_file_exists(LUA_DIR) then
if df.check_if_file_exists(LUA_DIR .. ".orig") then
if dt.configuration.running_os == "windows" then
os.execute("rmdir /s " .. LUA_DIR .. ".orig")
else
os.execute("rm -rf " .. LUA_DIR .. ".orig")
end
end
os.rename(LUA_DIR, LUA_DIR .. ".orig")
end
local git = df.check_if_bin_exists("git")
if not git then
dt.print("ERROR: git not found. Install or specify the location of the git executable.")
return
end
local git_command = git .. " clone " .. sm.lua_repository .. " " .. LUA_DIR
dt.print_log("install git command is " .. git_command)
result = os.execute(git_command)
if not df.check_if_file_exists(LUA_DIR .. PS .. "downloads") then
os.execute("mkdir " .. LUA_DIR .. PS .. "downloads")
end
return result
end
local function update_scripts()
local result = false
local git = df.check_if_bin_exists("git")
if not git then
dt.print("ERROR: git not found. Install or specify the location of the git executable.")
return
end
local git_command = "cd " .. LUA_DIR .. " " .. CS .. " " .. git .. " pull"
dt.print_log("update git command is " .. git_command)
if dt.configuration.running_os == "windows" then
result = dtsys.windows_command(git_command)
else
result = os.execute(git_command)
end
if dt.preferences.read("script_manager", "use_lua_scripts_version", "bool") and
dt.configuration.running_os == "windows" then
use_lua_scripts_version()
end
return result
end
local function add_script_data(script_file)
-- the script file supplied is category/filename.filetype
-- the following pattern splits the string into category, path, name, fileename, and filetype
-- for example contrib/gimp.lua becomes
-- category - contrib
-- path -
-- name - gimp.lua
-- filename - gimp
-- filetype - lua
-- Thanks Tobias Jakobs for the awesome regulary expression
local pattern = "(.-)/(.-)(([^\\/]-)%.?([^%.\\/]*))$"
if dt.configuration.running_os == "windows" then
-- change the path separator from / to \ for windows
pattern = "(.-)\\(.-)(([^\\]-)%.?([^%.\\]*))$"
end
dt.print_log("processing " .. script_file)
-- add the script data
local category,path,name,filename,filetype = string.match(script_file, pattern)
dt.print_log("category is " .. category)
dt.print_log("name is " .. name)
if #sm.script_categories == 0 or not string.match(du.join(sm.script_categories, " "), category) then
sm.script_categories[#sm.script_categories + 1] = category
sm.script_names[category] = {}
end
if name then
dt.print_log("category is " .. category)
dt.print_log("name is " .. name)
if not string.match(du.join(sm.script_names[category], " "), name) then
sm.script_names[category][#sm.script_names[category] + 1] = name
sm.script_paths[category .. "/" .. name] = category .. "/" .. path .. name
if category == "downloads" then
sm.have_downloads = true
end
end
end
end
local function scan_scripts()
local find_cmd = "find -L " .. LUA_DIR .. " -name \\*.lua -print | sort"
if dt.configuration.running_os == "windows" then
find_cmd = "dir /b/s " .. LUA_DIR .. "\\*.lua | sort"
end
-- scan the scripts
local output = io.popen(find_cmd)
for line in output:lines() do
local l = string.gsub(line, LUA_DIR .. PS, "") -- strip the lua dir off
local script_file = l:sub(1,-5)
if not string.match(script_file, "script_manager") then -- let's not include ourself
if not string.match(script_file, "plugins") then -- skip plugins
if not string.match(script_file, "lib" .. PS) then -- let's not try and run libraries
if not string.match(script_file, "include_all") then -- skip include_all.lua
if not string.match(script_file, "yield") then -- special case, because everything needs this
add_script_data(script_file)
else
prequire(script_file) -- load yield.lua
end
end
end
end
end
end
-- work around because we can't dynamically add a new stack child. We create an empty child that will be
-- populated with downloads as they occur. If there are already downloads then this is just ignored
add_script_data("downloads" .. PS)
end
-- get the script documentation, with some assumptions
local function get_script_doc(script)
local description = nil
f = io.open(LUA_DIR .. PS .. script .. ".lua")
if f then
-- slurp the file
local content = f:read("*all")
f:close()
-- assume that the second block comment is the documentation
description = string.match(content, "%-%-%[%[.-%]%].-%-%-%[%[(.-)%]%]")
else
dt.print_error("Cant read from " .. script)
end
if description then
return description
else
return "No documentation available"
end
end
local function activate(script, scriptname)
dt.print_log("activating " .. scriptname)
local status, err = prequire(sm.script_paths[script])
if status then
dt.preferences.write("script_manager", script, "bool", true)
dt.print("Loaded " .. scriptname)
else
dt.print(scriptname .. " failed to load")
dt.print_error("Error loading " .. scriptname)
dt.print_error("Error message: " .. err)
end
return status
end
local function deactivate(script, scriptname)
-- presently the lua api doesn't support unloading gui elements therefore
-- we just mark then inactive for the next time darktable starts
-- deactivate it....
dt.preferences.write("script_manager", script, "bool", false)
dt.print_log("setting " .. scriptname .. " to not start")
dt.print(scriptname .. " will not be active when darktable is restarted")
end
local function create_enable_disable_button(btext, sname, req)
return dt.new_widget("button")
{
label = btext .. sname,
tooltip = get_script_doc(req),
clicked_callback = function (self)
-- split the label into action and target
local action, target = string.match(self.label, "(.+) (.+)")
-- load the script if it's not loaded
local scat = sm.category_selector.value
local starget = du.join({scat, target}, "/")
dt.print_log("starget to activate is " .. starget)
dt.print_log("target to activate is " .. target)
if action == "Enable" then
local status = activate(starget, target)
if status then
self.label = "Disable " .. target
end
else
deactivate(starget, target)
self.label = "Enable " .. target
end
end
}
end
local function load_script_stack()
-- load the scripts
table.sort(sm.script_categories)
for _,cat in ipairs(sm.script_categories) do
local tmp = {}
table.sort(sm.script_names[cat])
if not sm.script_widgets[cat] then
for _,sname in ipairs(sm.script_names[cat]) do
local req = du.join({cat, sname}, "/")
local btext = "Enable "
if dt.preferences.read("script_manager", req, "bool") then
local status, err = prequire(sm.script_paths[req])
if status then
btext = "Disable "
else
dt.print_error("Error loading " .. sname)
dt.print_error("Error message: " .. err)
end
else
dt.preferences.write("script_manager", req, "bool", false)
end
tmp[#tmp + 1] = create_enable_disable_button(btext, sname, req)
end
sm.script_widgets[cat] = dt.new_widget("box")
{
orientation = "vertical",
table.unpack(tmp),
}
elseif #sm.script_widgets[cat] ~= #sm.script_names[cat] then
for index,sname in ipairs(sm.script_names[cat]) do
local req = du.join({cat, sname}, "/")
dt.print_error("script is " .. sname .. " and index is " .. index)
if sm.script_widgets[cat][index] then
sm.script_widgets[cat][index] = nil
end
sm.script_widgets[cat][index] = create_enable_disable_button("Enable ", sname, req)
end
end
end
if not sm.script_stack then
sm.script_stack = dt.new_widget("stack"){}
for i,cat in ipairs(sm.script_categories) do
sm.script_stack[i] = sm.script_widgets[cat]
end
sm.script_stack.active = 1
end
end
local function update_stack_choices(combobox, choice_table)
sm.have_downloads = true
local items = #combobox
local choices = #choice_table
if #sm.script_widgets["downloads"] == 0 then
choices = choices - 1
sm.have_downloads = false
end
cnt = 1
for i, name in ipairs(choice_table) do
if (name == "downloads" and sm.have_downloads) or name ~= "downloads" then
combobox[cnt] = name
cnt = cnt + 1
end
end
if choices < items then
for j = items, choices + 1, -1 do
combobox[j] = nil
end
end
combobox.value = 1
end
local function build_scripts_block()
-- build the whole script block
scan_scripts()
-- set up the stack for the choices
load_script_stack()
if not sm.category_selector then
-- set up the combobox for the categories
sm.category_selector = dt.new_widget("combobox"){
label = "Category",
tooltip = "Select the script category",
value = 1, "placeholder",
changed_callback = function(self)
local cnt = 1
for i,cat in ipairs(sm.script_categories) do
if cat == self.value then
sm.script_stack.active = i
end
end
end
}
end
update_stack_choices(sm.category_selector, sm.script_categories)
if not sm.scripts then
sm.scripts = dt.new_widget("box"){
orientation = "vertical",
dt.new_widget("label"){ label = "Scripts" },
sm.category_selector,
sm.script_stack,
}
end
end
local function insert_scripts_block()
table.insert(sm.main_menu_choices, "Enable/Disable Scripts")
update_combobox_choices(sm.main_menu, sm.main_menu_choices, 1)
sm.main_stack[#sm.main_stack + 1] = sm.scripts
end
local function use_lua_scripts_version()
if dt.configuration.running_os == "windows" then
-- copy tools\script_manger.lua to luarc
df.file_copy(LUA_DIR .. PS .. "tools\\script_manager.lua", dt.configuration.config_dir .. PS .. "luarc")
else
-- create a symbolic link from luarc to tools/script_manager.lua
if df.check_if_file_exists(dt.configuration.config_dir .. "/luarc") then
os.remove(dt.configuration.config_dir .. "/luarc")
end
os.execute("ln -s " .. LUA_DIR .. "/tools/script_manager.lua " .. dt.configuration.config_dir .. "/luarc")
end
end
local function link_downloads_directory()
if not df.check_if_file_exists("$HOME/Downloads") then
os.execute("mkdir $HOME/Downloads")
end
if df.check_if_file_exists(LUA_DIR .. "/downloads") then
os.remove(LUA_DIR .. "/downloads")
end
os.execute("ln -s " .. "$HOME/Downloads " .. LUA_DIR .. "/downloads")
end
-- - - - - - - - - - - - - - - - - - - - - - - -
-- M A I N P R O G R A M
-- - - - - - - - - - - - - - - - - - - - - - - -
-- api check
dt.configuration.check_version(...,{5,0,0})
-- set up tables to contain all the widgets and choices
sm.script_widgets = {}
sm.script_categories = {}
sm.script_names = {}
sm.script_paths = {}
sm.main_menu_choices = {}
sm.main_stack_items = {}
-- see if we've run this before
sm.initialized = dt.preferences.read("script_manager", "initialized", "bool")
if not sm.initialized then
-- write out preferences
dt.preferences.write("script_manager", "lua_repository", "string", LUA_SCRIPT_REPO)
dt.preferences.write("script_manager", "use_distro_version", "bool", false)
dt.preferences.write("script_manager", "link_downloads_directory", "bool", false)
dt.preferences.write("script_manager", "initialized", "bool", true)
end
sm.have_scripts = df.check_if_file_exists(LUA_DIR)
sm.git_managed = df.check_if_file_exists(LUA_DIR .. PS .. ".git")
if sm.have_scripts then
dt.print_log("found lua scripts directory")
else
dt.print_log("lua scripts directory not found")
end
if sm.git_managed then
dt.print_log("scripts managed by git")
else
dt.print_log("scripts not managed")
end
local git = df.check_if_bin_exists("git")
if git then
dt.print_log("git found at " .. git)
sm.need_git = false
else
dt.print_log("git not found")
sm.need_git = true
end
sm.repository = dt.new_widget("entry")
{
text = dt.preferences.read("script_manager", "lua_repository", "string"),
editable = true,
}
sm.need_install = false
local install_update_text = _("install")
if sm.have_scripts and sm.git_managed then
install_update_text = _("update")
else
sm.need_install = true
end
sm.install_update_button = dt.new_widget("button"){
label = install_update_text .. _(" scripts"),
clicked_callback = function(self)
sm.lua_repository = sm.repository.text
dt.preferences.write("script_manager", "lua_repository", "string", sm.repository.text)
if sm.need_install then
local result = install_scripts()
if result then
build_scripts_block()
insert_scripts_block()
sm.have_scripts = true
sm.git_managed = true
sm.need_install = false
self.label = _("update scripts")
dt.print(_("installed scripts from " .. sm.repository.text))
else
dt.print(_("Error installing scripts from " .. sm.repository.text))
end
else
local result = update_scripts()
if result then
--build_scripts_block()
dt.print(_("updated scripts from " .. sm.repository.text))
else
dt.print(_("Error updating scripts from " .. sm.repository.text))
end
end
end
}
if not sm.need_install then
sm.reinstall_button = dt.new_widget("button"){
label = "reinstall scripts",
clicked_callback = function(self)
sm.lua_repository = sm.repository.text
dt.preferences.write("script_manager", "lua_repository", "string", sm.repository.text)
local result = install_scripts()
if result then
build_scripts_block()
insert_scripts_block()
sm.have_scripts = true
sm.git_managed = true
sm.need_install = false
sm.install_update_button.label = "update scripts"
dt.print(_("reinstalled scripts from " .. sm.repository.text))
dt.print_log(_("scripts reinstalled"))
else
dt.print(_("ERROR: script reinstallation failed"))
dt.print_error(_("script reinstall failed"))
end
end
}
sm.install_update_widgets = {
sm.install_update_button,
sm.reinstall_button,
}
else
sm.install_update_widgets = {
sm.install_update_button,
}
end
sm.install_update_box = dt.new_widget("box"){
orientation = "vertical",
dt.new_widget("label"){ label = "Install/Update scripts" },
table.unpack(sm.install_update_widgets),
}
table.insert(sm.main_menu_choices, "Install/Update Scripts")
table.insert(sm.main_stack_items, sm.install_update_box)
-- configuration items
sm.repository_update = dt.new_widget("button"){
label = "update",
clicked_callback = function()
dt.preferences.write("script_manager", "lua_repository", "string", sm.repository.text)
sm.install_update_button.label = "install"
sm.install_update_button.sensitive = true
sm.reinstall_button.sensitive = false
sm.need_install = true
end
}
sm.repository_reset = dt.new_widget("button"){
label = "reset",
clicked_callback = function()
sm.repository.text = LUA_SCRIPT_REPO
dt.preferences.write("script_manager", "lua_repository", "string", LUA_SCRIPT_REPO)
sm.install_update_button.label = "install"
sm.install_update_button.sensitive = true
sm.reinstall_button.sensitive = false
sm.need_install = true
end
}
sm.update_reset = dt.new_widget("box"){
orientation = "horizontal",
sm.repository_update,
sm.repository_reset,
}
-- replace standalone version of script_manager with distributed version, i.e. tools/script_manager.lua
-- link on linux/MacOS, copy on windows
-- this option won't be active until the repository version of this script is accepted
sm.use_lua_scripts_version = dt.new_widget("check_button"){
label = "Use lua scripts distributed version",
tooltip = "Use the standalone version (false) or the distributed version (true)",
value = dt.preferences.read("script_manager", "use_distro_version", "bool"),
clicked_callback = function(self)
if dt.preferences.read("script_manager", "use_distro_version", "bool") == self.value then
-- do nothing
else
sm.apply_configuration.sensitive = true
end
end
}
-- link downloads to $HOME/downloads on linux and MacOS
sm.link_downloads_directory = dt.new_widget("check_button"){
label = "Link lua/downloads to $HOME/Downloads",
tooltip = "Linking the directories enables dropping a script in $HOME/downloads\nand having it recognized the next time darktable starts",
value = dt.preferences.read("script_manager", "link_downloads_directory", "bool"),
clicked_callback = function(self)
if dt.preferences.read("script_manager", "link_downloads_directory", "bool") == self.value then
-- do nothing
else
sm.apply_configuration.sensitive = true
end
end
}
sm.apply_configuration = dt.new_widget("button"){
label = "Apply",
sensitive = false,
clicked_callback = function(self)
dt.preferences.write("script_manager", "use_distro_version", "bool", sm.use_lua_scripts_version.value)
if sm.use_lua_scripts_version.value then
use_lua_scripts_version()
end
if dt.configuration.running_os ~= "windows" then
dt.preferences.write("script_manager", "link_downloads_directory", "bool", sm.link_downloads_directory.value)
if sm.link_downloads_directory.value then
link_downloads_directory()
end
end
end
}
-- get git location on windows
sm.git_location = df.executable_path_widget({"git"})
sm.configuration_widgets = {
sm.repository,
sm.update_reset,
dt.new_widget("separator"){},
dt.new_widget("separator"){},
}
if dt.configuration.running_os == "windows" or sm.need_git then
sm.configuration_widgets[#sm.configuration_widgets + 1] = sm.git_location
end
if dt.configuration.running_os ~= "windows" then
sm.configuration_widgets[#sm.configuration_widgets + 1] = sm.use_lua_scripts_version
sm.configuration_widgets[#sm.configuration_widgets + 1] = sm.link_downloads_directory
end
sm.configuration_widgets[#sm.configuration_widgets + 1] = sm.apply_configuration
sm.config_box = dt.new_widget("box"){
orientation = "vertical",
dt.new_widget("label") { label = "Configuration" },
table.unpack(sm.configuration_widgets),
}
table.insert(sm.main_menu_choices, "Configure")
table.insert(sm.main_stack_items, sm.config_box)
-- set up the outside stack for config, install/update, and download
-- make a stack for the choices
sm.main_stack = dt.new_widget("stack"){
table.unpack(sm.main_stack_items),
}
-- make a combobox for the selector
sm.main_menu = dt.new_widget("combobox"){
label = "Action",
tooltip = "Select the action you want to perform",
value = 1, "No actions available",
changed_callback = function(self)
for pos,str in ipairs(sm.main_menu_choices) do
if self.value == str then
sm.main_stack.active = pos
dt.preferences.write("script_manager", "sm_main_menu_value", "integer", pos)
end
end
end
}
if #sm.main_menu_choices > 0 then
update_combobox_choices(sm.main_menu, sm.main_menu_choices, 1)
end
sm.main_box = dt.new_widget("box"){
orientation = "vertical",
sm.main_menu,
sm.main_stack,
}
-- - - - - - - - - - - - - - - - - - - - - - - -
-- D A R K T A B L E I N T E G R A T I O N
-- - - - - - - - - - - - - - - - - - - - - - - -
-- register the module
dt.register_lib(
"script_manager", -- Module name
"script manager", -- Visible name
true, -- expandable
false, -- resetable
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_LEFT_BOTTOM", 100}}, -- containers
dt.new_widget("box") -- widget
{
orientation = "vertical",
sm.main_box,
},
nil,-- view_enter
nil -- view_leave
)
-- set up the scripts block if we have them otherwise we'll wait until we download them
if sm.have_scripts then
-- scan for scripts and populate the categories
build_scripts_block()
-- add the widgets to the lib
insert_scripts_block()
sm.main_menu.selected = 3
end
collectgarbage("restart")