xref: /haiku/src/add-ons/index_server/IndexServerAddOn.cpp (revision 3a88830daabc2ac9037143a49605ead5844db823)
1 /*
2  * Copyright 2010, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Clemens Zeidler <haiku@clemens-zeidler.de>
7  */
8 
9 #include "IndexServerAddOn.h"
10 
11 #include <Debug.h>
12 #include <Directory.h>
13 #include <File.h>
14 #include <Path.h>
15 
16 #include "IndexServerPrivate.h"
17 
18 
analyser_settings()19 analyser_settings::analyser_settings()
20 	:
21 	catchUpEnabled(true),
22 
23 	syncPosition(0),
24 	watchingStart(0),
25 	watchingPosition(0)
26 {
27 
28 }
29 
30 
31 const char* kAnalyserStatusFile = "AnalyserStatus";
32 
33 const char* kCatchUpEnabledAttr = "CatchUpEnabled";
34 const char* kSyncPositionAttr = "SyncPosition";
35 const char* kWatchingStartAttr = "WatchingStart";
36 const char* kWatchingPositionAttr = "WatchingPosition";
37 
38 
AnalyserSettings(const BString & name,const BVolume & volume)39 AnalyserSettings::AnalyserSettings(const BString& name, const BVolume& volume)
40 	:
41 	fName(name),
42 	fVolume(volume)
43 {
44 	ReadSettings();
45 }
46 
47 
48 bool
ReadSettings()49 AnalyserSettings::ReadSettings()
50 {
51 	BAutolock _(fSettingsLock);
52 
53 	BDirectory rootDir;
54 	fVolume.GetRootDirectory(&rootDir);
55 	BPath path(&rootDir);
56 	path.Append(kIndexServerDirectory);
57 	path.Append(fName);
58 	path.Append(kAnalyserStatusFile);
59 
60 	BFile file(path.Path(), B_READ_ONLY);
61 	if (file.InitCheck() != B_OK)
62 		return false;
63 
64 	uint32 value;
65 	file.ReadAttr(kCatchUpEnabledAttr, B_UINT32_TYPE, 0, &value,
66 		sizeof(uint32));
67 	fAnalyserSettings.catchUpEnabled = value != 0 ? true : false;
68 	file.ReadAttr(kSyncPositionAttr, B_INT64_TYPE, 0,
69 		&fAnalyserSettings.syncPosition, sizeof(int64));
70 	file.ReadAttr(kWatchingStartAttr, B_INT64_TYPE, 0,
71 		&fAnalyserSettings.watchingStart, sizeof(int64));
72 	file.ReadAttr(kWatchingPositionAttr, B_INT64_TYPE, 0,
73 		&fAnalyserSettings.watchingPosition, sizeof(int64));
74 
75 	return true;
76 }
77 
78 
79 bool
WriteSettings()80 AnalyserSettings::WriteSettings()
81 {
82 	BAutolock _(fSettingsLock);
83 
84 	BDirectory rootDir;
85 	fVolume.GetRootDirectory(&rootDir);
86 	BPath path(&rootDir);
87 	path.Append(kIndexServerDirectory);
88 	path.Append(fName);
89 	if (create_directory(path.Path(), 777) != B_OK)
90 		return false;
91 	path.Append(kAnalyserStatusFile);
92 
93 	BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
94 	if (file.InitCheck() != B_OK)
95 		return false;
96 
97 	uint32 value = fAnalyserSettings.catchUpEnabled ? 1 : 0;
98 	file.WriteAttr(kCatchUpEnabledAttr, B_UINT32_TYPE, 0, &value,
99 		sizeof(uint32));
100 	file.WriteAttr(kSyncPositionAttr, B_INT64_TYPE, 0,
101 		&fAnalyserSettings.syncPosition, sizeof(int64));
102 	file.WriteAttr(kWatchingStartAttr, B_INT64_TYPE, 0,
103 		&fAnalyserSettings.watchingStart, sizeof(int64));
104 	file.WriteAttr(kWatchingPositionAttr, B_INT64_TYPE, 0,
105 		&fAnalyserSettings.watchingPosition, sizeof(int64));
106 
107 	return true;
108 }
109 
110 
111 analyser_settings
RawSettings()112 AnalyserSettings::RawSettings()
113 {
114 	BAutolock _(fSettingsLock);
115 
116 	return fAnalyserSettings;
117 }
118 
119 
120 void
SetCatchUpEnabled(bool enabled)121 AnalyserSettings::SetCatchUpEnabled(bool enabled)
122 {
123 	BAutolock _(fSettingsLock);
124 
125 	fAnalyserSettings.catchUpEnabled = enabled;
126 }
127 
128 
129 void
SetSyncPosition(bigtime_t time)130 AnalyserSettings::SetSyncPosition(bigtime_t time)
131 {
132 	BAutolock _(fSettingsLock);
133 
134 	fAnalyserSettings.syncPosition = time;
135 }
136 
137 
138 void
SetWatchingStart(bigtime_t time)139 AnalyserSettings::SetWatchingStart(bigtime_t time)
140 {
141 	BAutolock _(fSettingsLock);
142 
143 	fAnalyserSettings.watchingStart = time;
144 }
145 
146 
147 void
SetWatchingPosition(bigtime_t time)148 AnalyserSettings::SetWatchingPosition(bigtime_t time)
149 {
150 	BAutolock _(fSettingsLock);
151 
152 	fAnalyserSettings.watchingPosition = time;
153 }
154 
155 
156 bool
CatchUpEnabled()157 AnalyserSettings::CatchUpEnabled()
158 {
159 	BAutolock _(fSettingsLock);
160 
161 	return fAnalyserSettings.catchUpEnabled;
162 }
163 
164 
165 bigtime_t
SyncPosition()166 AnalyserSettings::SyncPosition()
167 {
168 	BAutolock _(fSettingsLock);
169 
170 	return fAnalyserSettings.syncPosition;
171 }
172 
173 
174 bigtime_t
WatchingStart()175 AnalyserSettings::WatchingStart()
176 {
177 	BAutolock _(fSettingsLock);
178 
179 	return fAnalyserSettings.watchingStart;
180 }
181 
182 
183 bigtime_t
WatchingPosition()184 AnalyserSettings::WatchingPosition()
185 {
186 	BAutolock _(fSettingsLock);
187 
188 	return fAnalyserSettings.watchingPosition;
189 }
190 
191 
FileAnalyser(const BString & name,const BVolume & volume)192 FileAnalyser::FileAnalyser(const BString& name, const BVolume& volume)
193 	:
194 	fVolume(volume),
195 	fName(name)
196 {
197 
198 }
199 
200 
201 void
SetSettings(AnalyserSettings * settings)202 FileAnalyser::SetSettings(AnalyserSettings* settings)
203 {
204 	ASSERT(fName == settings->Name() && fVolume == settings->Volume());
205 
206 	fAnalyserSettings = settings;
207 	ASSERT(fAnalyserSettings.Get());
208 	UpdateSettingsCache();
209 }
210 
211 
212 AnalyserSettings*
Settings() const213 FileAnalyser::Settings() const
214 {
215 	return fAnalyserSettings;
216 }
217 
218 
219 const analyser_settings&
CachedSettings() const220 FileAnalyser::CachedSettings() const
221 {
222 	return fCachedSettings;
223 }
224 
225 
226 void
UpdateSettingsCache()227 FileAnalyser::UpdateSettingsCache()
228 {
229 	fCachedSettings = fAnalyserSettings->RawSettings();
230 }
231