1 /* Demo Recorder for MAEMO 5
2 * Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2,
5 * or (at your option) any later version, as published by the Free
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 public class ApplicationSettings {
22 public enum OpenAction {
28 public static OpenAction from_label(string val) {
30 case "Last open project":
34 case "Recent projects list":
35 return RECENT_PROJECTS;
36 case "Specified project":
42 public static OpenAction from_name(string val) {
48 case "RECENT_PROJECTS":
49 return RECENT_PROJECTS;
56 public static string to_label(OpenAction val) {
59 return "Last open project";
63 return "Recent projects list";
65 return "Specified project";
67 return "Last open project";
70 public static string to_name(OpenAction val) {
73 return "LAST_PROJECT";
77 return "RECENT_PROJECTS";
79 return "NAMED_PROJECT";
81 return "LAST_PROJECT";
85 public enum CloseAction {
90 public static CloseAction from_label(string val) {
92 case "Auto-save the project":
93 return AUTO_SAVE_PROJECT;
94 case "Prompt to save the project":
95 return PROMPT_SAVE_PROJECT;
99 return AUTO_SAVE_PROJECT;
102 public static CloseAction from_name(string val) {
104 case "AUTO_SAVE_PROJECT":
105 return AUTO_SAVE_PROJECT;
106 case "PROMPT_SAVE_PROJECT":
107 return PROMPT_SAVE_PROJECT;
111 return PROMPT_SAVE_PROJECT;
114 public static string to_label(CloseAction val) {
116 case AUTO_SAVE_PROJECT:
117 return "Auto-save the project";
118 case PROMPT_SAVE_PROJECT:
119 return "Prompt to save the project";
123 return "Auto-save the project";
126 public static string to_name(CloseAction val) {
128 case AUTO_SAVE_PROJECT:
129 return "AUTO_SAVE_PROJECT";
130 case PROMPT_SAVE_PROJECT:
131 return "PROMPT_SAVE_PROJECT";
135 return "PROMPT_SAVE_PROJECT";
139 public enum AddTrackAction {
145 public static AddTrackAction from_label(string val) {
151 case "Disable track":
159 public static AddTrackAction from_name(string val) {
173 public static string to_label(AddTrackAction val) {
176 return "Enable track";
180 return "Disable track";
184 return "Enable track";
187 public static string to_name(AddTrackAction val) {
194 return "SET_INACTIVE";
202 public enum BounceTracksAction {
209 public static BounceTracksAction from_label(string val) {
211 case "Disable source tracks":
212 return SET_SOURCE_INACTIVE;
213 case "Mute source tracks":
214 return SET_SOURCE_MUTE;
215 case "Solo new track":
216 return SET_TARGET_SOLO;
217 case "Disable new track":
218 return SET_TARGET_INACTIVE;
219 case "Mute new track":
220 return SET_TARGET_MUTE;
222 return SET_SOURCE_INACTIVE;
225 public static BounceTracksAction from_name(string val) {
227 case "SET_SOURCE_INACTIVE":
228 return SET_SOURCE_INACTIVE;
229 case "SET_SOURCE_MUTE":
230 return SET_SOURCE_MUTE;
231 case "SET_TARGET_SOLO":
232 return SET_TARGET_SOLO;
233 case "SET_TARGET_INACTIVE":
234 return SET_TARGET_INACTIVE;
235 case "SET_TARGET_MUTE":
236 return SET_TARGET_MUTE;
238 return SET_SOURCE_INACTIVE;
241 public static string to_label(BounceTracksAction val) {
243 case SET_SOURCE_INACTIVE:
244 return "Disable source tracks";
245 case SET_SOURCE_MUTE:
246 return "Mute source tracks";
247 case SET_TARGET_SOLO:
248 return "Solo new track";
249 case SET_TARGET_INACTIVE:
250 return "Disable new track";
251 case SET_TARGET_MUTE:
252 return "Mute new track";
254 return "Disable source tracks";
257 public static string to_name(BounceTracksAction val) {
259 case SET_SOURCE_INACTIVE:
260 return "SET_SOURCE_INACTIVE";
261 case SET_SOURCE_MUTE:
262 return "SET_SOURCE_MUTE";
263 case SET_TARGET_SOLO:
264 return "SET_TARGET_SOLO";
265 case SET_TARGET_INACTIVE:
266 return "SET_TARGET_INACTIVE";
267 case SET_TARGET_MUTE:
268 return "SET_TARGET_MUTE";
270 return "SET_SOURCE_INACTIVE";
274 public enum RemoveTrackAction {
279 public static RemoveTrackAction from_label(string val) {
281 case "Prompt with choices":
283 case "Delete the file":
285 case "Preserve the file":
286 return PRESERVE_FILE;
291 public static RemoveTrackAction from_name(string val) {
297 case "PRESERVE_FILE":
298 return PRESERVE_FILE;
303 public static string to_label(RemoveTrackAction val) {
306 return "Prompt with choices";
308 return "Delete the file";
310 return "Preserve the file";
312 return "Prompt with choices";
315 public static string to_name(RemoveTrackAction val) {
320 return "DELETE_FILE";
322 return "PRESERVE_FILE";
328 public enum MixdownAction {
332 public static MixdownAction from_label(string val) {
342 public static MixdownAction from_name(string val) {
352 public static string to_label(MixdownAction val) {
362 public static string to_name(MixdownAction val) {
367 return "PLAY_TARGET";
374 public string working_directory {public get; public set;}
375 public string last_project_name {public get; public set;}
376 public string last_project_location {public get; public set;}
377 public string named_project_name {public get; public set;}
378 public string named_project_location {public get; public set;}
379 public string[] recent_project_names {public get; public set;}
380 public string[] recent_project_locations {public get; public set;}
381 public uint64 delay_max_delay {public get; public set;}
382 public bool mixdown_enable_lamenc {public get; public set;}
383 public bool mixdown_enable_mpegaudio {public get; public set;}
384 public OpenAction open_action {public get; public set;}
385 public CloseAction close_action {public get; public set;}
386 public AddTrackAction add_track_action {public get; public set;}
387 public BounceTracksAction bounce_tracks_action {public get; public set;}
388 public RemoveTrackAction remove_track_action {public get; public set;}
389 public MixdownAction mixdown_action {public get; public set;}
391 public ApplicationSettings() {
392 working_directory = Environment.get_home_dir() + "/DemoRecordings";
393 last_project_name = "";
394 last_project_location = "";
395 named_project_name = "";
396 named_project_location = "";
397 recent_project_names = new string[]{"","","","","","","","","",""};
398 recent_project_locations = new string[]{"","","","","","","","","",""};
399 delay_max_delay = Time.Nanoseconds.SECOND / 2;
400 mixdown_enable_lamenc = false;
401 mixdown_enable_mpegaudio = false;
402 open_action = OpenAction.LAST_PROJECT;
403 close_action = CloseAction.PROMPT_SAVE_PROJECT;
404 add_track_action = AddTrackAction.SET_ACTIVE;
405 bounce_tracks_action = BounceTracksAction.SET_SOURCE_INACTIVE;
406 remove_track_action = RemoveTrackAction.PROMPT;
409 public static ApplicationSettings load_settings(string filename, string version, out string errors) {
411 ApplicationSettings settings = new ApplicationSettings();
412 bool success = false;
413 // prepare the Xml Parser
416 Xml.Doc* doc = Xml.Parser.parse_file(filename);
418 // load the root node
419 Xml.Node* root = doc->get_root_element();
421 // check element is ProjectSettings
422 if ("ApplicationSettings" == root->name) {
424 string? doc_version = XmlHelpers.get_node_attribute(root, "version");
425 if (null != doc_version && version == doc_version) {
426 // looking good let's get parsing.
428 settings.from_xml_node(root);
431 catch (GLib.Error ex) {
432 errors = "ApplicationSettings parser threw an error.";
439 errors = "Incorrect version or unversioned document.";
443 errors = "Not a valid settings file.";
447 errors = "No root node found.";
453 errors = "Couldn't parse file as valid XML.";
455 // clean up the parser
456 Xml.Parser.cleanup();
457 return (success) ? settings : null;
460 public static bool save_settings(string filename, ApplicationSettings settings, ref string errors) {
461 //stdout.printf("save settings called\n");
463 string contents = settings.to_xml_string();
465 if (0 < contents.length) {
466 contents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + contents;
467 //stdout.printf(contents);
468 //stdout.printf("\n" + filename + "\n");
471 if (FileUtils.set_contents(filename, contents, contents.length)) {
472 //stdout.printf("save settings worked\n");
477 errors = "Couldn't save the settings file!\n%s".printf("FILE ERROR");
480 catch (FileError ex) {
481 errors = "Couldn't save the settings file!\n%s".printf("FILE ERROR");
485 errors = "Couldn't serialize the settings!.";
487 //stdout.printf("save settings failed\n");
492 private string to_xml_string() {
493 return ("<ApplicationSettings version=\"1.0\">\n" +
494 "<working_directory type=\"string\"><![CDATA[%s]]></working_directory>\n" +
495 "<last_project_name type=\"string\"><![CDATA[%s]]></last_project_name>\n" +
496 "<last_project_location type=\"string\"><![CDATA[%s]]></last_project_location>\n" +
497 "<named_project_name type=\"string\"><![CDATA[%s]]></named_project_name>\n" +
498 "<named_project_location type=\"string\"><![CDATA[%s]]></named_project_location>\n" +
499 "<recent_project_names type=\"CDL\"><![CDATA[%s]]></recent_project_names>\n" +
500 "<recent_project_locations type=\"CDL\"><![CDATA[%s]]></recent_project_locations>\n" +
501 "<delay_max_delay type=\"uint64\">%llu</delay_max_delay>\n" +
502 "<mixdown_enable_lamenc type=\"bool\">%s</mixdown_enable_lamenc>\n" +
503 "<mixdown_enable_mpegaudio type=\"bool\">%s</mixdown_enable_mpegaudio>\n" +
504 "<open_action type=\"OpenAction\">%s</open_action>\n" +
505 "<close_action type=\"CloseAction\">%s</close_action>\n" +
506 "<add_track_action type=\"AddTrackAction\">%s</add_track_action>\n" +
507 "<bounce_tracks_action type=\"BounceTracksAction\">%s</bounce_tracks_action>\n" +
508 "<remove_track_action type=\"RemoveTrackAction\">%s</remove_track_action>\n" +
509 "</ApplicationSettings>\n").printf(
512 last_project_location,
518 mixdown_enable_lamenc.to_string(),
519 mixdown_enable_mpegaudio.to_string(),
520 OpenAction.to_name(open_action),
521 CloseAction.to_name(close_action),
522 AddTrackAction.to_name(add_track_action),
523 BounceTracksAction.to_name(bounce_tracks_action),
524 RemoveTrackAction.to_name(remove_track_action)
528 private void from_xml_node(Xml.Node* node) {
529 // Loop over the passed node's children
530 for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
531 // Spaces between tags are also nodes, discard them
532 if (iter->type != Xml.ElementType.ELEMENT_NODE) {
535 // Get the node's name
536 string node_name = iter->name;
537 // Get the nodes type
538 string? type = XmlHelpers.get_node_attribute(iter, "type");
539 // Get the node's content with <tags> stripped
545 case "working_directory":
546 this.working_directory = iter->get_content();
548 case "last_project_name":
549 this.last_project_name = iter->get_content();
551 case "last_project_location":
552 this.last_project_location = iter->get_content();
554 case "named_project_name":
555 this.named_project_name = iter->get_content();
557 case "named_project_location":
558 this.named_project_location = iter->get_content();
566 case "mixdown_enable_lamenc":
567 this.mixdown_enable_lamenc = iter->get_content().to_bool();
569 case "mixdown_enable_mpegaudio":
570 this.mixdown_enable_mpegaudio = iter->get_content().to_bool();
578 case "delay_max_delay":
579 this.delay_max_delay = iter->get_content().to_uint64();
587 case "recent_project_names":
588 /// TODO this.recent_project_names = iter->get_content().to_uint64();
590 case "recent_project_locations":
591 /// TODO this.recent_project_locations = iter->get_content().to_uint64();
600 this.open_action = OpenAction.from_name(iter->get_content());
609 this.close_action = CloseAction.from_name(iter->get_content());
615 case "AddTrackAction":
617 case "add_track_action":
618 this.add_track_action = AddTrackAction.from_name(iter->get_content());
624 case "BounceTracksAction":
626 case "bounce_tracks_action":
627 this.bounce_tracks_action = BounceTracksAction.from_name(iter->get_content());
633 case "RemoveTrackAction":
635 case "remove_track_action":
636 this.remove_track_action = RemoveTrackAction.from_name(iter->get_content());