summaryrefslogtreecommitdiffstats
path: root/lbu-controller.lua
blob: 060a7b8aa20b991db9435a4450e384d225bfcb3c (plain)
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
module(..., package.seeall)

-- ################################################################################
-- PUBLIC FUNCTIONS

mvc={}
function mvc.on_load (self, parent)
	self.conf.default_action = "status"
end

function status (self)
	local cmd = self.clientdata.cmd
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	local status, errors = self.model:getstatus()
	return ( {status = status,
		errors = errors,
		lbustatus = self.model:list(),
		url = url, } )
end

function config (self)
	local errors = {}
	local cmdresult
	if (self.clientdata.cmd_delete_excluded) and (self.clientdata.lbu_excluded) then
		cmdresult = self.model:lbuincexcl("exclude", self.clientdata.lbu_excluded, "remove")
	end
	if (self.clientdata.cmd_delete_included) and (self.clientdata.lbu_included) then
		cmdresult = self.model:lbuincexcl("include", self.clientdata.lbu_included, "remove")
	end
	if (self.clientdata.config_submit) then
		local variables="ENCRYPTION LBU_MEDIA DEFAULT_CIPHER PASSWORD"
		cmdresult = {}
		for var in string.gmatch(variables, "%S+") do
			cmdresult[var] = self.model:editconfig(var, self.clientdata[var], "change_value")
		end
	end
	if (self.clientdata.cmd_add_included) and (self.clientdata.item_add_included) then
		cmdresult = self.model:lbuincexcl("include", self.clientdata.item_add_included, "add")
	end
	if (self.clientdata.cmd_add_excluded) and (self.clientdata.item_add_excluded) then
		cmdresult = self.model:lbuincexcl("exclude", self.clientdata.item_add_excluded, "add")
	end

	local config = self.model:getconfig()
	-- This needs to be done /after/ the editing of the config (done earlier in this code)
	local status = self.model:getstatus()

	-- Add submit and other buttons
	config["cmd_delete_included"] = cfe({
		name="cmd_delete_included",
		value="Delete",
		label="Delete selected included-item",
		type="submit",
		descr="Mark a item in '" .. config.lbu_included.label .. "'-list before pressing [Delete]",
		})

	config["item_add_included"] = cfe({
		name="item_add_included",
		label="Item to add to included-list",
		})

	config["cmd_add_included"] = cfe({
		name="cmd_add_included",
		value="Add",
		label="Add the item into the included-list",
		type="submit",
		descr="Enter a value in '" .. config.item_add_included.label .. "'-input before pressing [Add]",
		})

	config["cmd_delete_excluded"] = cfe({
		name="cmd_delete_excluded",
		value="Delete",
		label="Delete selected excluded-item",
		type="submit",
		descr="Mark a item in '" .. config.lbu_excluded.label .. "'-list before pressing [Delete]",
		})

	config["item_add_excluded"] = cfe({
		name="item_add_excluded",
		label="Item to add to excluded-list",
		})

	config["cmd_add_excluded"] = cfe({
		name="cmd_add_excluded",
		value="Add",
		label="Add the item into the excluded-list",
		type="submit",
		descr="Enter a value in '" .. config.item_add_excluded.label .. "'-input before pressing [Add]",
		})

	config["config_submit"] = cfe({
		name="config_submit",
		value="Apply",
		label="Apply above settings",
		type="submit",
		})
	if (self.clientdata.config_submit) then
		config.config_submit.descr="* Changes has been saved!"
	end	


	-- Disable buttons/commands that is usable for som reason
	if (config.lbu_included.option) and (#config.lbu_included.option == 0) then
		config.cmd_delete_included.disabled= "yes"
		config.cmd_delete_included.descr= ""
	end
	if (config.lbu_excluded.option) and (#config.lbu_excluded.option == 0) then
		config.cmd_delete_excluded.disabled= "yes"
		config.cmd_delete_excluded.descr= ""
	end


	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	return ( {status = status, 
		cmdresult = cmdresult,
		errors = errors,
		clientdata = self.clientdata,
		config = config,
		url = url, } )
end

function commit(self)
	local cmdresult, cmderror
	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller
	local cmdflag = nil
	if (self.clientdata.lbucleanmedia) then cmdflag = "-d" else cmdflag = "" end
	if (self.clientdata.lbusimulate) then
		cmdresult, cmderror = self.model:getsimulate(cmdflag)
	end
	if (self.clientdata.lbucommit) then
		cmdresult, cmderror = self.model:getcommit(cmdflag)
	end

	-- If no clientdata then do a dryrun and see if it's going to work else report
	if not (cmdresult) then
		tmp_cmdresult, cmderror = self.model:getsimulate()
	end

	local status, errors = self.model:getstatus()

	-- Add buttons
	status["lbusimulate"] = cfe({
		name="lbusimulate",
		value="Simulate",
		label="Simulate/Test a commit",
		type="submit",
		})

	status["lbucommit"] = cfe({
		name="lbucommit",
		value="Commit",
		label="Actually commit and save changes",
		type="submit",
		})

	if (cmderror["-d"]) then
	status["lbucleanmedia"] = cfe({
		name="lbucleanmedia",
		value="lbucleanmedia",
		label="Remove existing apk.ovls from media",
		type="checkbox",
		})
	end

	return ( {status = status,
		cmdresult = cmdresult,
		cmdflag = cmdflag,
		cmderror = cmderror,
		clientdata = self.clientdata,
		url = url, } )
end
function expert (self)
	local modifications = self.clientdata.filecontent or ""
	if ( self.clientdata.cmdsave) then
		modifications = self.model:update_filecontent(modifications)
	end

	local status = self.model.getstatus()
	local file = self.model:get_filedetails()
	-- Add submit and other buttons
	file["cmdsave"] = cfe({
		name="cmdsave",
		value="Apply",
		label="Apply settings",
		type="submit",
		})
	if (self.clientdata.cmdsave) then
		file.cmdsave.descr="* Changes has been saved!"
	end	

	local url = ENV["SCRIPT_NAME"] .. self.conf.prefix .. self.conf.controller

	return { 
		file = file, 
		status = status,
		clientdata = self.clientdata,
		url = url, }
end