Added a 3 seconds delayed save to disk

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5349 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval 2003-11-13 09:13:08 +00:00
parent 5480b4590e
commit 36a4855350
3 changed files with 42 additions and 16 deletions

View File

@ -2,6 +2,7 @@
* Copyright 2003, Jérôme Duval. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include <Application.h>
#include <Autolock.h>
#include <string.h>
#include <storage/FindDirectory.h>
@ -12,7 +13,8 @@
MMediaFilesManager::MMediaFilesManager()
: fLocker(new BLocker("media files manager locker")),
fRegistryMap(new Map<BString, Map<BString, entry_ref> >)
fRegistryMap(new Map<BString, Map<BString, entry_ref> >),
fRunner(NULL)
{
CALLED();
LoadState();
@ -24,6 +26,7 @@ MMediaFilesManager::MMediaFilesManager()
MMediaFilesManager::~MMediaFilesManager()
{
CALLED();
delete fRunner;
SaveState();
delete fRegistryMap;
delete fLocker;
@ -120,7 +123,7 @@ MMediaFilesManager::LoadState()
BEntry entry(val);
entry.GetRef(&ref);
}
SetRefFor(str, key, ref);
SetRefFor(str, key, ref, false);
free(key);
free(val);
@ -276,7 +279,7 @@ MMediaFilesManager::GetRefFor(const char *type,
status_t
MMediaFilesManager::SetRefFor(const char *type,
const char *item,
const entry_ref &ref)
const entry_ref &ref, bool save)
{
CALLED();
TRACE("MMediaFilesManager::SetRefFor %s %s\n", type, item);
@ -294,6 +297,8 @@ MMediaFilesManager::SetRefFor(const char *type,
if(map->Has(itemString))
map->Remove(itemString);
map->Insert(itemString, ref);
if(save)
LaunchTimer();
return B_OK;
}
@ -308,12 +313,11 @@ MMediaFilesManager::RemoveRefFor(const char *type,
BString itemString(item);
BString typeString(type);
Map <BString, entry_ref> *map;
if(!fRegistryMap->Get(typeString, &map))
return B_OK;
map->Remove(itemString);
map->Insert(itemString, *(new entry_ref));
if(fRegistryMap->Get(typeString, &map)) {
map->Remove(itemString);
map->Insert(itemString, *(new entry_ref));
LaunchTimer();
}
return B_OK;
}
@ -326,11 +330,25 @@ MMediaFilesManager::RemoveItem(const char *type,
BString itemString(item);
BString typeString(type);
Map <BString, entry_ref> *map;
if(!fRegistryMap->Get(typeString, &map))
return B_OK;
map->Remove(itemString);
if(fRegistryMap->Get(typeString, &map)) {
map->Remove(itemString);
LaunchTimer();
}
return B_OK;
}
void
MMediaFilesManager::LaunchTimer()
{
if(!fRunner)
fRunner = new BMessageRunner(be_app,
new BMessage(MMEDIAFILESMANAGER_SAVE_TIMER), 3 * 1000000, 1);
}
void
MMediaFilesManager::TimerMessage()
{
SaveState();
delete fRunner;
fRunner = NULL;
}

View File

@ -6,10 +6,13 @@
#include <Entry.h>
#include <File.h>
#include <Locker.h>
#include <MessageRunner.h>
#include <String.h>
#include "TMap.h"
#include "DataExchange.h"
#define MMEDIAFILESMANAGER_SAVE_TIMER 'mmst'
class MMediaFilesManager
{
public:
@ -35,7 +38,8 @@ public:
status_t SetRefFor(
const char * type,
const char * item,
const entry_ref & ref);
const entry_ref & ref,
bool save = true);
status_t RemoveRefFor( // This might better be called "ClearRefFor"
const char * type, // but it's too late now...
const char * item,
@ -45,15 +49,18 @@ public:
const char * type,
const char * item);
void TimerMessage();
private:
static int32 ReadPascalString(BFile &file, char **str);
static int32 WritePascalString(BFile &file, const char *str);
void LaunchTimer();
private:
BLocker *fLocker;
Map<BString, Map<BString, entry_ref> > * fRegistryMap;
uint32 header[3];
BMessageRunner *fRunner;
};

View File

@ -698,6 +698,7 @@ void ServerApp::MessageReceived(BMessage *msg)
case MEDIA_SERVER_REQUEST_NOTIFICATIONS: gNotificationManager->EnqueueMessage(msg); break;
case MEDIA_SERVER_CANCEL_NOTIFICATIONS: gNotificationManager->EnqueueMessage(msg); break;
case MEDIA_SERVER_SEND_NOTIFICATIONS: gNotificationManager->EnqueueMessage(msg); break;
case MMEDIAFILESMANAGER_SAVE_TIMER: gMMediaFilesManager->TimerMessage(); break;
default:
printf("\nnew media server: unknown message received\n");
msg->PrintToStream();