summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/inc/configmodule/screensaver.inc.php
blob: 88e0c968dde89c438fcf139650e5189cc8ab11c7 (plain) (blame)
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
<?php

ConfigModule::registerModule(
	ConfigModule_Screensaver::MODID, // ID
	Dictionary::translateFileModule('sysconfig', 'config-module', 'screensaver_title'), // Title
	Dictionary::translateFileModule('sysconfig', 'config-module', 'screensaver_description'), // Description
	Dictionary::translateFileModule('sysconfig', 'config-module', 'group_generic'), // Group
	true, // Only one per config?
	100 // Sort order
);

class ConfigModule_Screensaver extends ConfigModule
{
	const MODID = 'Screensaver';
	const VERSION = 1;

	protected function generateInternal($tgz, $parent)
	{
		/* Validate if all data are available */
		if (!$this->validateConfig())
			return false;

		/* Give the Taskmanager the job and create the tgz */
		$taskId = 'xscreensaver' . mt_rand() . '-' . microtime(true);

		$task = Taskmanager::submit('MakeTarball', array(
			'id' => $taskId,
			'files' => $this->getFileArray(),
			'destination' => $tgz,
		), false);

		return $task;
	}

	protected function moduleVersion()
	{
		return self::VERSION;
	}

	protected function validateConfig()
	{
		return isset($this->moduleData['qss']) &&
				 isset($this->moduleData['texts']) &&
			    isset($this->moduleData['texts']['text-idle-kill']) &&
				 isset($this->moduleData['texts']['text-no-timeout']) &&
				 isset($this->moduleData['texts']['text-shutdown']);
	}

	public function setData($key, $value)
	{
		switch ($key) {
		case 'qss':
		case 'texts':
		case 'messages':
			break;
		default:
			return false;
		}
		$this->moduleData[$key] = $value;
		return true;
	}

	public function allowDownload()
	{
		return false;
	}

	/**
	 * Creates a map with filepath => file content
	 *
	 * @return array in the form of Map<String, byte[]>
	 */
	private function getFileArray()
	{
		$files = array(
			'/opt/openslx/xscreensaver/style.qss' => $this->moduleData['qss'],
			'/opt/openslx/xscreensaver/text-idle-kill' => $this->wrapHtmlTags('text-idle-kill'),
			'/opt/openslx/xscreensaver/text-no-timeout' => $this->wrapHtmlTags('text-no-timeout'),
			'/opt/openslx/xscreensaver/text-shutdown' => $this->wrapHtmlTags('text-shutdown'),
		);

		/* Create the message.ini from the messages array */
		$messages = '';
		foreach ($this->moduleData['messages'] as $category => $array) {
			$messages .= '[' . $category . ']' . "\n";
			foreach ($array as $key => $message) {
				$messages .= $key . '="' . str_replace(['\\', '"', "\n", "\r"], '-', $message) . '"' . "\n";
			}
		}
		$files['/opt/openslx/xscreensaver/messages.ini'] = $messages;

		/* Add locked files if there are any */
		if (isset($this->moduleData['texts']['text-idle-kill-locked']))
			$files['/opt/openslx/xscreensaver/text-idle-kill-locked'] = $this->wrapHtmlTags('text-idle-kill-locked');
		if (isset($this->moduleData['texts']['text-no-timeout-locked']))
			$files['/opt/openslx/xscreensaver/text-no-timeout-locked'] = $this->wrapHtmlTags('text-no-timeout-locked');
		if (isset($this->moduleData['texts']['text-shutdown-locked']))
			$files['/opt/openslx/xscreensaver/text-shutdown-locked'] = $this->wrapHtmlTags('text-shutdown-locked');

		return $files;
	}

	private function wrapHtmlTags($text_name)
	{
		return '<html><body>' . $this->moduleData['texts'][$text_name] . '</body></html>';
	}
}