From f748e2d460fd3b179aecd7092fb46ec5daf450f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Thu, 12 Feb 2004 03:00:56 +0000 Subject: [PATCH] Extended watching mechanism (it now works more like the BHandler watching mechanism) - it's not really used yet, though. Fixed some variable initialization bugs and missing return values. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6566 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/diskprobe/DataEditor.cpp | 81 +++++++++++++++++++++++++++++-- src/apps/diskprobe/DataEditor.h | 13 +++++ 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/apps/diskprobe/DataEditor.cpp b/src/apps/diskprobe/DataEditor.cpp index 5a38ec8fbf..a6b748ce7f 100644 --- a/src/apps/diskprobe/DataEditor.cpp +++ b/src/apps/diskprobe/DataEditor.cpp @@ -9,6 +9,8 @@ #include #include +#include + class DataChange { public: @@ -170,7 +172,7 @@ status_t DataEditor::SetTo(const char *path, const char *attribute) { BEntry entry(path); - SetTo(entry, attribute); + return SetTo(entry, attribute); } @@ -178,7 +180,7 @@ status_t DataEditor::SetTo(entry_ref &ref, const char *attribute) { BEntry entry(&ref); - SetTo(entry, attribute); + return SetTo(entry, attribute); } @@ -195,18 +197,32 @@ DataEditor::SetTo(BEntry &entry, const char *attribute) fIsReadOnly = true; } + struct stat stat; + stat.st_mode = 0; + + fFile.GetStat(&stat); + fIsDevice = (stat.st_mode & (S_IFBLK | S_IFCHR)) != 0; + + // ToDo: add support for devices and attributes! status = fFile.GetSize(&fSize); if (status < B_OK) { fFile.Unset(); return status; } + if (attribute != NULL) + fAttribute = strdup(attribute); + else + fAttribute = NULL; + + fView = NULL; fBlockSize = 512; - fAttribute = attribute; - fRealViewOffset = -1; + fRealViewOffset = 0; fViewOffset = 0; fRealViewSize = fViewSize = fBlockSize; fNeedsUpdate = true; + + return B_OK; } @@ -384,6 +400,7 @@ status_t DataEditor::SetFileSize(off_t size) { fSize = size; + return B_OK; } @@ -399,6 +416,8 @@ DataEditor::SetViewOffset(off_t offset) fRealViewOffset = (offset / fBlockSize) * fBlockSize; fViewOffset = offset; fNeedsUpdate = true; + + return B_OK; } @@ -483,21 +502,75 @@ DataEditor::Unlock() } +void +DataEditor::SendNotices(uint32 what, BMessage *message) +{ + if (fObservers.CountItems() == 0) + return; + + BMessage *notice; + if (message) { + notice = new BMessage(*message); + notice->what = B_OBSERVER_NOTICE_CHANGE; + notice->AddInt32(B_OBSERVE_ORIGINAL_WHAT, message->what); + } else + notice = new BMessage(B_OBSERVER_NOTICE_CHANGE); + + notice->AddInt32(B_OBSERVE_WHAT_CHANGE, what); + + for (int32 i = fObservers.CountItems(); i-- > 0;) { + BMessenger *messenger = fObservers.ItemAt(i); + messenger->SendMessage(notice); + } + + delete notice; +} + + status_t DataEditor::StartWatching(BMessenger target) { + BAutolock locker(fLock); + node_ref node; status_t status = fFile.GetNodeRef(&node); if (status < B_OK) return status; + fObservers.AddItem(new BMessenger(target)); + return watch_node(&node, B_WATCH_STAT | B_WATCH_ATTR, target); } +status_t +DataEditor::StartWatching(BHandler *handler, BLooper *looper) +{ + return StartWatching(BMessenger(handler, looper)); +} + + void DataEditor::StopWatching(BMessenger target) { + BAutolock locker(fLock); + + for (int32 i = fObservers.CountItems(); i-- > 0;) { + BMessenger *messenger = fObservers.ItemAt(i); + if (*messenger == target) { + fObservers.RemoveItemAt(i); + delete messenger; + break; + } + } + stop_watching(target); } + +void +DataEditor::StopWatching(BHandler *handler, BLooper *looper) +{ + StopWatching(BMessenger(handler, looper)); +} + diff --git a/src/apps/diskprobe/DataEditor.h b/src/apps/diskprobe/DataEditor.h index 13021d9ab9..e84968bcd9 100644 --- a/src/apps/diskprobe/DataEditor.h +++ b/src/apps/diskprobe/DataEditor.h @@ -29,8 +29,11 @@ class DataEditor { bool IsReadOnly() const { return fIsReadOnly; } bool IsDevice() const { return fIsDevice; } + bool IsAttribute() const { return fAttribute != NULL; } //bool IsModified() const { return fIsModified; } + const char *Attribute() const { return fAttribute; } + status_t InitCheck(); status_t Replace(off_t offset, const uint8 *data, size_t length); @@ -65,16 +68,21 @@ class DataEditor { void Unlock(); status_t StartWatching(BMessenger target); + status_t StartWatching(BHandler *handler, BLooper *looper = NULL); void StopWatching(BMessenger target); + void StopWatching(BHandler *handler, BLooper *looper = NULL); BFile &File() { return fFile; } private: + void SendNotices(uint32 what, BMessage *message = NULL); status_t Update(); void AddChange(DataChange *change); void ApplyChanges(); void RemoveRedos(); + BObjectList fObservers; + BFile fFile; const char *fAttribute; bool fIsDevice, fIsReadOnly; @@ -93,4 +101,9 @@ class DataEditor { size_t fBlockSize; }; +static const uint32 kMsgDataEditorUndoState = 'deUS'; +static const uint32 kMsgDataEditorRedoState = 'deRS'; +static const uint32 kMsgDataEditorModifiedState = 'deMS'; +static const uint32 kMsgDataEditorUpdate = 'deUp'; + #endif /* DATA_EDITOR_H */