
--[[
=head1 NAME

applets.JogglerInstallers.JogglerInstallersApplet

=head1 DESCRIPTION

JogglerInstallers v1.01 (12th April 2012)

This applet is used to install or remove software on the O2 Joggler.

=cut
--]]


-- Things we might be using.
local setmetatable, tonumber, tostring = setmetatable, tonumber, tostring

local os                     = require("os")
local io                     = require("io")
local oo                     = require("loop.simple")
local math                   = require("math")
local string                 = require("string")
local table                  = require("jive.utils.table")
local debug                  = require("jive.utils.debug")

local Applet                 = require("jive.Applet")
local System                 = require("jive.System")
local Checkbox               = require("jive.ui.Checkbox")
local Choice                 = require("jive.ui.Choice")
local Framework              = require("jive.ui.Framework")
local Event                  = require("jive.ui.Event")
local Icon                   = require("jive.ui.Icon")
local Label                  = require("jive.ui.Label")
local Button                 = require("jive.ui.Button")
local Popup                  = require("jive.ui.Popup")
local Group                  = require("jive.ui.Group")
local RadioButton            = require("jive.ui.RadioButton")
local RadioGroup             = require("jive.ui.RadioGroup")
local SimpleMenu             = require("jive.ui.SimpleMenu")
local Slider                 = require("jive.ui.Slider")
local Surface                = require("jive.ui.Surface")
local Textarea               = require("jive.ui.Textarea")
local Textinput              = require("jive.ui.Textinput")
local Window                 = require("jive.ui.Window")
local ContextMenuWindow      = require("jive.ui.ContextMenuWindow")
local Timer                  = require("jive.ui.Timer")
local Keyboard               = require("jive.ui.Keyboard")

local appletManager = appletManager
local jiveMain		= jiveMain

module(..., Framework.constants)
oo.class(_M, Applet)


-- Define the initial menu.
function menu(self, menuItem)

	local window = Window("text_list", self:string("TITLE"))
	local textarea = Textarea("help_text", self:string("INSTALLWARN"))

	local menu = SimpleMenu("menu", {
	
			{
				text = self:string("SSH"),
				sound = "WINDOWSHOW",
				callback = function(event, menuItem)
					self:installssh(menuItem)
				end
			},
		--	{
		--		text = self:string("LMS"),
		--		sound = "WINDOWSHOW",
		--		callback = function(event, menuItem)
		--			self:installlms(menuItem)
		--		end
		--	},
			
	})

	window:addWidget(menu)
	menu:setHeaderWidget(textarea)

    -- for reference - the first menu doesn't save the settings, this is done as the button is pressed now.
	--window:addListener(EVENT_WINDOW_POP,
	--	function()
	--		self:storeSettings()
	--	end
	--)

	self:tieAndShowWindow(window)
	return window
	
end


-- Define the SSH submenu
function installssh(self, menuItem)
	local window = Window("text_list", menuItem.text)
	local textarea = Textarea("help_text", self:string("SSHINFO"))
	

	local ssh = self:getSettings()["ssh"]

	local group = RadioGroup()
	
	local menu = SimpleMenu("menu", {
		{
			text = self:string("SSHINSTALL"),
			style = 'item_choice',
			check = RadioButton(
				"radio", group,
				function(event, menuItem, statusfile)
					self:popupscreen('SSHINSTALLING','REBOOTREQ')
					os.execute("sqp_JogglerInstallers.sh ssh install &")
					self:getSettings()["ssh"] = "enabled"
					self:storeSettings()
				end,
			ssh == "enabled")
		},
		{
			text = self:string("SSHREMOVE"),
			style = 'item_choice',
			check = RadioButton(
				"radio", group,
				function(event, menuItem)
					self:popupscreen('SSHREMOVING','REBOOTREQ')		
					os.execute("sqp_JogglerInstallers.sh ssh uninstall &")
					self:getSettings()["ssh"] = "disabled"
					self:storeSettings()
				end,
			ssh == "disabled")
		},
	})

	window:addWidget(menu)
	menu:setHeaderWidget(textarea)
	self:tieAndShowWindow(window)
	return window;
end


-- This should check an installation's status, but it doesn't work in the intended way.
--function checkinstatus()
--	log:info("Install Status: Checking...")
--	instatusfile = io.open('/tmp/sqp_installstatus', "r")
--	instatus = instatusfile:read()
--	instatusfile:close()
--	log:info("Install Status: " .. instatus)
--end


-- This is a simple on-screen message.
function popupmsg(self, warning)

       log:info(self:string(warning))
 
       -- create a Popup object, using already established 'toast_popup_text' skin style
       local popupmessage = Popup('toast_popup_text')
 
       -- add message to popup
       local popupmessageMessage = Group("group", {
                       text = Textarea('toast_popup_textarea',self:string(warning)),
             })
       popupmessage:addWidget(popupmessageMessage)
 
       -- display the message for 4 seconds
       popupmessage:showBriefly(4000, nil, Window.transitionPushPopupUp, Window.transitionPushPopupDown)		

end


-- This is a full screen message. Use self.popup:hide() to get rid.
function popupscreen(self, warning, subwarning)

	log:info(self:string(warning))

    self.popup = Popup("waiting_popup")

	-- prevent exiting from the spinny
	self.popup:setAllowScreensaver(false)
	self.popup:setAlwaysOnTop(true)
	self.popup:setAutoHide(false)
	self.popup:ignoreAllInputExcept()

    self.popup:addWidget(Icon("icon_connecting"))
    self.popup:addWidget(Label("text", self:string(warning)))
    self.popup:addWidget(Label("subtext", self:string(subwarning)))
    self:tieAndShowWindow(self.popup)

end



--[[

=head1 LICENSE

Created by Andy Davison
birdslikewires.co.uk

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

=cut
--]]

