* Now uses a flattened BMessage as settings file format instead of a proprietary

one.
* Also renamed the settings file to "MediaFiles" which I had previously already
  renamed accidently.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34562 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-12-08 18:08:35 +00:00
parent 9abc1e0049
commit 9dc3c57ac7
2 changed files with 79 additions and 139 deletions
+77 -133
View File
@@ -19,10 +19,9 @@
#include <MediaSounds.h> #include <MediaSounds.h>
const char* kMediaFilesManagerSettingsDirectory = "Media"; static const char* kSettingsDirectory = "Media";
const char* kMediaFilesManagerSettingsFile = "MediaFilesManager"; static const char* kSettingsFile = "MediaFiles";
static const uint32 kSettingsWhat = 'mfil';
const uint32 kHeader[] = {0xac00150c, 0x18723462, 0x00000001};
MediaFilesManager::MediaFilesManager() MediaFilesManager::MediaFilesManager()
@@ -46,7 +45,7 @@ MediaFilesManager::MediaFilesManager()
SetRefFor(MEDIA_TYPE_SOUNDS, MEDIA_SOUNDS_WINDOW_RESTORED, ref, false); SetRefFor(MEDIA_TYPE_SOUNDS, MEDIA_SOUNDS_WINDOW_RESTORED, ref, false);
SetRefFor(MEDIA_TYPE_SOUNDS, MEDIA_SOUNDS_WINDOW_ZOOMED, ref, false); SetRefFor(MEDIA_TYPE_SOUNDS, MEDIA_SOUNDS_WINDOW_ZOOMED, ref, false);
LoadState(); _LoadState();
#if DEBUG >=3 #if DEBUG >=3
Dump(); Dump();
#endif #endif
@@ -60,124 +59,49 @@ MediaFilesManager::~MediaFilesManager()
} }
//! This is called by the media_server *before* any add-ons have been loaded.
status_t
MediaFilesManager::LoadState()
{
CALLED();
BPath path;
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
if (status == B_OK)
status = path.Append(kMediaFilesManagerSettingsDirectory);
if (status == B_OK)
status = path.Append(kMediaFilesManagerSettingsFile);
if (status != B_OK)
return status;
BFile file;
status = file.SetTo(path.Path(), B_READ_ONLY);
if (status != B_OK)
return status;
uint32 header[3];
if (file.Read(header, sizeof(header)) < (int32)sizeof(header))
return B_ERROR;
if (memcmp(header, kHeader, sizeof(kHeader)) != 0)
return B_ERROR;
uint32 categoryCount;
if (file.Read(&categoryCount, sizeof(uint32)) < (int32)sizeof(uint32))
return B_ERROR;
while (categoryCount--) {
BString type;
ssize_t length = _ReadSettingsString(file, type);
if (length < 0)
return length;
if (length == 0)
break;
TRACE("%s {\n", type.String());
do {
BString key;
length = _ReadSettingsString(file, key);
if (length < 0)
return length;
if (length == 0)
break;
BString value;
length = _ReadSettingsString(file, value);
entry_ref ref;
if (length > 1)
get_ref_for_path(value.String(), &ref);
SetRefFor(type, key.String(), ref, false);
} while (true);
TRACE("}\n");
}
return B_OK;
}
status_t status_t
MediaFilesManager::SaveState() MediaFilesManager::SaveState()
{ {
CALLED(); CALLED();
BPath path; BMessage settings(kSettingsWhat);
status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); status_t status;
if (status == B_OK)
status = path.Append(kMediaFilesManagerSettingsDirectory);
if (status == B_OK) {
status = create_directory(path.Path(),
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
if (status == B_OK)
status = path.Append(kMediaFilesManagerSettingsFile);
if (status != B_OK)
return status;
BFile file;
status = file.SetTo(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
if (status != B_OK)
return status;
if (file.Write(kHeader, sizeof(kHeader)) < (ssize_t)sizeof(kHeader))
return B_ERROR;
BAutolock _(this);
uint32 categoryCount = fMap.size();
if (file.Write(&categoryCount, sizeof(uint32)) < (ssize_t)sizeof(uint32))
return B_ERROR;
uint32 zero = 0;
TypeMap::iterator iterator = fMap.begin(); TypeMap::iterator iterator = fMap.begin();
for (; iterator != fMap.end(); iterator++) { for (; iterator != fMap.end(); iterator++) {
const BString& type = iterator->first; const BString& type = iterator->first;
FileMap& fileMap = iterator->second; FileMap& fileMap = iterator->second;
_WriteSettingsString(file, type.String()); BMessage items;
status = items.AddString("type", type.String());
if (status != B_OK)
return status;
FileMap::iterator fileIterator = fileMap.begin(); FileMap::iterator fileIterator = fileMap.begin();
for (; fileIterator != fileMap.end(); fileIterator++) { for (; fileIterator != fileMap.end(); fileIterator++) {
const BString& item = fileIterator->first; const BString& item = fileIterator->first;
BPath path(&fileIterator->second); BPath path(&fileIterator->second);
_WriteSettingsString(file, item.String()); status = items.AddString("item", item.String());
_WriteSettingsString(file, path.Path() ? path.Path() : ""); if (status == B_OK) {
status = items.AddString("path",
path.Path() ? path.Path() : "");
}
if (status != B_OK)
return status;
} }
file.Write(&zero, sizeof(uint32)); status = settings.AddMessage("type items", &items);
if (status != B_OK)
return status;
} }
file.Write(&zero, sizeof(uint32));
return B_OK; BFile file;
status = _OpenSettingsFile(file,
B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
if (status != B_OK)
return status;
return settings.Flatten(&file);
} }
@@ -419,47 +343,67 @@ MediaFilesManager::_LaunchTimer()
} }
/*static*/ int32 status_t
MediaFilesManager::_ReadSettingsString(BFile& file, BString& string) MediaFilesManager::_OpenSettingsFile(BFile& file, int mode)
{ {
uint32 length; bool createFile = (mode & O_ACCMODE) != O_RDONLY;
if (file.Read(&length, 4) < 4) BPath path;
return -1; status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path,
createFile);
if (length == 0) { if (status == B_OK)
string.Truncate(0); status = path.Append(kSettingsDirectory);
return 0; if (status == B_OK && createFile) {
status = create_directory(path.Path(),
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
} }
if (status == B_OK)
status = path.Append(kSettingsFile);
if (status != B_OK)
return status;
char* buffer = string.LockBuffer(length); return file.SetTo(path.Path(), mode);
if (buffer == NULL)
return -1;
ssize_t bytesRead = file.Read(buffer, length);
string.UnlockBuffer(length);
if (bytesRead < (ssize_t)length)
return -1;
return length;
} }
//! This is called by the media_server *before* any add-ons have been loaded.
status_t status_t
MediaFilesManager::_WriteSettingsString(BFile& file, const char* string) MediaFilesManager::_LoadState()
{ {
if (string == NULL) CALLED();
return B_BAD_VALUE;
uint32 length = strlen(string); BFile file;
if (file.Write(&length, 4) < 4) status_t status = _OpenSettingsFile(file, B_READ_ONLY);
return B_ERROR; if (status != B_OK)
return status;
if (length == 0) BMessage settings;
return B_OK; status = settings.Unflatten(&file);
if (status != B_OK)
return status;
if (file.Write(string, length) < (ssize_t)length) if (settings.what != kSettingsWhat)
return B_ERROR; return B_BAD_TYPE;
BMessage items;
for (int32 i = 0; settings.FindMessage("type items", i, &items) == B_OK;
i++) {
const char* type;
if (items.FindString("type", &type) != B_OK)
continue;
const char* item;
for (int32 j = 0; items.FindString("item", j, &item) == B_OK; j++) {
const char* path;
if (items.FindString("path", j, &path) != B_OK)
return B_BAD_DATA;
entry_ref ref;
get_ref_for_path(path, &ref);
// it's okay for this to fail
SetRefFor(type, item, ref, false);
}
}
return B_OK; return B_OK;
} }
+2 -6
View File
@@ -23,7 +23,6 @@ public:
MediaFilesManager(); MediaFilesManager();
~MediaFilesManager(); ~MediaFilesManager();
status_t LoadState();
status_t SaveState(); status_t SaveState();
void Dump(); void Dump();
@@ -45,11 +44,8 @@ public:
private: private:
void _LaunchTimer(); void _LaunchTimer();
status_t _OpenSettingsFile(BFile& file, int mode);
static ssize_t _ReadSettingsString(BFile& file, status_t _LoadState();
BString& string);
static status_t _WriteSettingsString(BFile& file,
const char* string);
private: private:
// for each type, the map contains a map of item/entry_ref // for each type, the map contains a map of item/entry_ref