File: settings.vala

package info (click to toggle)
listaller 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,812 kB
  • sloc: ansic: 2,152; sh: 1,735; perl: 1,452; xml: 874; cpp: 765; java: 157; makefile: 150; cs: 48; python: 24
file content (327 lines) | stat: -rw-r--r-- 8,152 bytes parent folder | download
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
/* settings.vala -- Store global settings which affect all Listaller modules
 *
 * Copyright (C) 2009-2012 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 3
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

using Config;
using GLib;
using Listaller;

namespace Listaller {

private static const string tmpdir = "/var/tmp";
private static const string tmpdir_small = "/tmp";

/**
 * Provides Listaller's settings
 *
 * Provides settings for all other classes.
 */
public class Settings : Object {

	const string confdir = "/etc/listaller";
	const string sudbdir = "/var/lib/listaller";
	const string su_instroot = "/opt";
	const string su_desktopdir = Config.PREFIXDIR + "/share/applications";
	const string su_icondir = "/usr/share/icons/hicolor";
	const string su_pixdir = "/usr/share/pixmaps";
	private string[] lib_paths = { Config.PREFIXDIR + "/lib",
					   Config.PREFIXDIR + "/lib64",
					   "/lib"};

	private bool _sumode;
	private bool _testmode;
	private bool _locked;
	private string uinsttmp = "";


	// System directories
	internal const string sys_libdir = Config.LIBDIR;
	internal const string sys_bindir = Config.PREFIXDIR + "/bin";
	internal const string sys_sharedir = Config.PREFIXDIR + "/share";
	internal const string sys_etcdir = "/etc";

	public bool locked {
		get { return _locked; }
	}

	public bool sumode {
		get { return _sumode; }
		set { if (can_change ()) _sumode = value; }
	}

	public bool testmode {
		get { return _testmode; }
		set { if (can_change ()) _testmode = value; }
	}

	public Settings (bool root = false) {
		sumode = root;
		testmode = false;

		// Append some Debian-specific multiarch paths
		string tripel = "%s-%s-gnu".printf (Utils.system_machine (), Utils.system_os ());
		lib_paths += Path.build_filename ("/lib", tripel);
		lib_paths += Path.build_filename ("/usr", "lib", tripel);
		lib_paths += Path.build_filename ("/usr", "lib", tripel, "mesa");
		// null-terminate paths list
		lib_paths += null;

		unlock (); // Be unlocked by default
	}

	/*
	 * Lock the settings, so no changes can be made anymore
	 */
	public void lock () {
		_locked = true;
	}

	/*
	 * Allow changing the settings again
	 */
	public void unlock () {
		_locked = false;
	}

	/*
	 * @returns: True if we can change values
	 */
	private bool can_change () {
		if (locked) {
			warning ("Tried to write on locked settings object! (Don't try this!)");
			return false;
		} else {
			return true;
		}
	}

	public string database_file () {
		return appregister_dir () + "/installed.db";
	}

	[CCode (array_length = false, array_null_terminated = true)]
	public string[] library_paths () {
		return lib_paths;
	}

	private void touch_dir (string dirname, string warnmsg = "Error: %s") {
		if ((sumode && Utils.is_root ()) || (!sumode)) {
			File d = File.new_for_path (dirname);
			try {
				if (!d.query_exists ()) {
					d.make_directory_with_parents ();
				}
			} catch (Error e) {
				warning (warnmsg, e.message);
			}
		}
	}

	public string appregister_dir () {
		string regdir;
		if (sumode) {
			regdir = sudbdir;
		} else {
			regdir = Path.build_filename (Environment.get_user_data_dir (), "software", null);
		}
		if (testmode) {
			// Point to our temporary dir
			regdir = Path.build_filename (get_unique_install_tmp_dir (), "appreg", null);
		}

		touch_dir (regdir, "Unable to create application database directory: %s");

		return regdir;
	}

	public string appdata_dir () {
		string addir;

		if (sumode) {
			addir = Path.build_filename (su_instroot, "apps", null);
		} else {
			addir = Path.build_filename (Environment.get_home_dir (), ".appdata", "apps", null);
		}
		if (testmode) {
			// Point to our temporary dir
			addir = Path.build_filename (get_unique_install_tmp_dir (), "apps", null);
		}

		touch_dir (addir, "Unable to create application data directory: %s");

		return addir;
	}

	public string depdata_dir () {
		string depdir;

		if (sumode) {
			depdir = Path.build_filename (su_instroot, "deps", null);
		} else {
			depdir = Path.build_filename (Environment.get_home_dir (), ".appdata", "deps", null);
		}
		if (testmode) {
			// Point to our temporary dir
			depdir = Path.build_filename (get_unique_install_tmp_dir (), "deps", null);
		}

		touch_dir (depdir, "Unable to create application data directory: %s");

		return depdir;
	}

	public string icon_base_dir () {
		string icodir;

		if (sumode) {
			icodir = su_icondir;
		} else {
			icodir = Path.build_filename (Environment.get_user_data_dir (), "icons", "hicolor", null);
		}
		if (testmode) {
			// Point to our temporary dir
			icodir = Path.build_filename (get_unique_install_tmp_dir (), "icons", null);
		}

		touch_dir (icodir, "Unable to create icon directory: %s");

		return icodir;
	}

	public string icon_size_dir (int size = 0) {
		string icodir;

		bool size_valid = false;
		switch (size) {
			case 16: size_valid = true;
				break;
			case 24: size_valid = true;
				break;
			case 32: size_valid = true;
				break;
			case 48: size_valid = true;
				break;
			case 64: size_valid = true;
				break;
			case 128: size_valid = true;
				break;
			case 265: size_valid = true;
				break;
			default: size_valid = false;
				break;
		}

		if (!size_valid)
			size = 0;

		if (size == 0) {
			if (sumode)
				icodir = su_pixdir;
			else
				icodir = Path.build_filename (Environment.get_user_data_dir (), "icons", null);
		} else {
			icodir = Path.build_filename (icon_base_dir (), size.to_string () + "x" + size.to_string (), "apps", null);
		}

		touch_dir (icodir, "Unable to create icon directory: %s");

		return icodir;
	}

	public string desktop_dir () {
		string dskdir;

		if (sumode) {
			dskdir = su_desktopdir;
		} else {
			dskdir = Path.build_filename (Environment.get_home_dir (), ".local", "share", "applications", null);
		}
		if (testmode) {
			// Point to our temporary dir
			dskdir = Path.build_filename (get_unique_install_tmp_dir (), "app-desktop", null);
		}

		touch_dir (dskdir, "Unable to create application data directory: %s");

		return dskdir;
	}

	public string conf_dir () {
		return confdir;
	}

	public string tmp_dir () {
		string ret;
		//! ret = Environment.get_tmp_dir ();
		ret = tmpdir;
		return ret;
	}

	public string get_unique_tmp_dir (string prefix = "listaller") {
		string template = Path.build_filename (tmp_dir (), prefix + "-XXXXXX", null);

		string res = DirUtils.mkdtemp (template);
		if (res == null)
			res = tmp_dir ();
		return res;
	}

	internal void invalidate_tmp_dir () {
		if (uinsttmp.has_prefix (tmpdir))
			Utils.delete_dir_recursive (uinsttmp);
		uinsttmp = "";
	}

	public string get_unique_install_tmp_dir () {
		if (uinsttmp != "")
			return uinsttmp;

		string template = Path.build_filename (tmp_dir (), "litest-XXXXXX", null);

		string res = DirUtils.mkdtemp (template);
		if (res == null)
			res = tmp_dir ();
		uinsttmp = res;
		return uinsttmp;
	}
}

private bool find_library (string libname, Listaller.Settings conf) {
	Posix.Stat? s = null;
	string[] paths = conf.library_paths ();
	for (uint i = 0; paths[i] != null; i++) {
		s = null;
		string path = Path.build_filename (paths[i], libname);
		Posix.stat (path, out s);
		if (s.st_size != 0)
			return true;
	}

	return false;
}

} // End of namespace

public void li_enable_translation ()
{
	// Initialize localisation
	Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
	Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
	Intl.textdomain (Config.GETTEXT_PACKAGE);
}