xref: /haiku/src/apps/mediaplayer/settings/Settings.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
1 /*
2  * Copyright 2008, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Fredrik Modéen <fredrik@modeen.se>
7  */
8 
9 #include "Settings.h"
10 
11 #include <stdio.h>
12 
13 #include <String.h>
14 #include <FindDirectory.h>
15 #include <File.h>
16 
17 #include "TPreferences.h"
18 
19 Settings::Settings(const char *filename)
20 	: fTPreferences(TPreferences(B_USER_CONFIG_DIRECTORY, filename))
21 {
22 }
23 
24 
25 void
26 Settings::LoadSettings(mpSettings &settings)
27 {
28 	BPath path;
29 
30 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)
31 		return;
32 
33 	path.Append(SETTINGSFILENAME);
34 
35 	BFile settingsFile(path.Path(), B_READ_ONLY);
36 
37 	if (settingsFile.InitCheck() != B_OK) {
38 		_SetDefault(settings);
39 		return;
40 	}
41 
42 	fTPreferences.Unflatten(&settingsFile);
43 	memset(&settings, 0, sizeof(settings));
44 
45 	fTPreferences.FindInt8("autostart", (int8 *)&settings.autostart);
46 	fTPreferences.FindInt8("closeWhenDonePlayingMovie",
47 		(int8 *)&settings.closeWhenDonePlayingMovie);
48 	fTPreferences.FindInt8("closeWhenDonePlayingSound",
49 		(int8 *)&settings.closeWhenDonePlayingSound);
50 	fTPreferences.FindInt8("loopMovie", (int8 *)&settings.loopMovie);
51 	fTPreferences.FindInt8("loopSound", (int8 *)&settings.loopSound);
52 	fTPreferences.FindInt8("fullVolume", (int8 *)&settings.fullVolume);
53 	fTPreferences.FindInt8("halfVolume", (int8 *)&settings.halfVolume);
54 	fTPreferences.FindInt8("mute", (int8 *)&settings.mute);
55 
56 	fTPreferences.Flatten(&settingsFile);
57 }
58 
59 
60 void
61 Settings::SaveSettings(const mpSettings &settings)
62 {
63 	BPath path;
64 
65 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)
66 		return;
67 
68 	path.Append(SETTINGSFILENAME);
69 
70 	BFile settingsFile(path.Path(), B_READ_WRITE | B_CREATE_FILE);
71 
72 	fTPreferences.Unflatten(&settingsFile);
73 
74 	fTPreferences.SetInt8("autostart", settings.autostart);
75 	fTPreferences.SetInt8("closeWhenDonePlayingMovie",
76 		settings.closeWhenDonePlayingMovie);
77 	fTPreferences.SetInt8("closeWhenDonePlayingSound",
78 		settings.closeWhenDonePlayingSound);
79 	fTPreferences.SetInt8("loopMovie", settings.loopMovie);
80 	fTPreferences.SetInt8("loopSound", settings.loopSound);
81 	fTPreferences.SetInt8("fullVolume", settings.fullVolume);
82 	fTPreferences.SetInt8("halfVolume", settings.halfVolume);
83 	fTPreferences.SetInt8("mute", settings.mute);
84 
85 	settingsFile.SetSize(0);
86 	settingsFile.Seek(0, SEEK_SET);
87 
88 	fTPreferences.Flatten(&settingsFile);
89 }
90 
91 
92 void
93 Settings::_SetDefault(mpSettings &settings)
94 {
95 	memset(&settings, 0, sizeof(settings));
96 
97 	settings.autostart = 0;
98 	settings.closeWhenDonePlayingMovie = 0;
99 	settings.closeWhenDonePlayingSound = 0;
100 	settings.loopMovie = 0;
101 	settings.loopSound = 0;
102 	settings.fullVolume = 0;
103 	settings.halfVolume = 0;
104 	settings.mute = 1;
105 }
106 
107 
108