-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRadioSettings.cpp
141 lines (117 loc) · 3.18 KB
/
RadioSettings.cpp
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
/*
* File: RadioSettings.cpp
* Author: user
*
* Created on 26. Februar 2013, 04:01
*/
#include <Path.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <File.h>
#include "RadioSettings.h"
const char* SettingsFileName = "Radio.settings";
StationsList::StationsList()
: BObjectList<Station>()
{
}
StationsList::~StationsList() {
for (int i = CountItems() - 1; i >= 0; i--) {
BObjectList<Station>::RemoveItem(ItemAt(i), true);
}
}
bool
StationsList::AddItem(Station* station) {
if (FindItem(station->Name())) return false;
return BObjectList<Station>::AddItem(station);
}
bool
StationsList::RemoveItem(BString* stationName) {
Station* s = FindItem(stationName);
if (s)
return BObjectList<Station>::RemoveItem(s, true);
else
return false;
}
bool
StationsList::RemoveItem(Station* station) {
return BObjectList<Station>::RemoveItem(station, false);
}
Station*
StationsList::FindItem(BString* stationName) {
for (int i = 0; i < CountItems(); i++) {
Station* item = ItemAt(i);
if (stationName->Compare(item->Name()->String()) == 0)
return item;
}
return NULL;
}
status_t
StationsList::Load() {
status_t status;
BDirectory* stationsDir = Station::StationDirectory();
BEntry stationEntry;
while ((status = stationsDir->GetNextEntry(&stationEntry)) == B_OK) {
Station* station = Station::LoadFromPlsFile(stationEntry.Name());
if (station && FindItem(station->Name()) == NULL)
AddItem(station);
}
}
status_t
StationsList::Save() {
BEntry stationEntry;
BDirectory* stationsDir = Station::StationDirectory();
while ((stationsDir->GetNextEntry(&stationEntry)) == B_OK) {
stationEntry.Remove();
}
EachListItemIgnoreResult<Station, status_t>(this, &Station::Save);
}
RadioSettings::RadioSettings()
: BMessage()
{
Stations = new StationsList();
Load();
}
RadioSettings::RadioSettings(const RadioSettings& orig) : BMessage(orig) {
Stations = new StationsList();
Stations->AddList(orig.Stations);
}
RadioSettings::~RadioSettings() {
delete Stations;
}
const char*
RadioSettings::StationFinderName() {
return GetString("stationfinder", NULL);
}
void
RadioSettings::SetStationFinderName(const char* name) {
RemoveName("stationfinder");
AddString("stationfinder", name);
}
status_t
RadioSettings::Load() {
status_t status;
BPath configPath;
BDirectory configDir;
BFile configFile;
status = find_directory(B_USER_SETTINGS_DIRECTORY, &configPath);
if (status!=B_OK) return status;
configDir.SetTo(configPath.Path());
status = configFile.SetTo(&configDir, SettingsFileName, B_READ_ONLY);
status = Unflatten(&configFile);
status = Stations->Load();
return status;
}
status_t
RadioSettings::Save() {
status_t status;
BPath configPath;
BDirectory configDir;
BFile configFile;
status = find_directory(B_USER_SETTINGS_DIRECTORY, &configPath);
if (status!=B_OK) return status;
configDir.SetTo(configPath.Path());
status = configDir.CreateFile(SettingsFileName, &configFile);
Flatten(&configFile);
Stations->Save();
return B_OK;
}