1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Copyright 2016, Rene Gollent, rene@gollent.com. 4 * Distributed under the terms of the MIT License. 5 */ 6 7 #include "LocatableFile.h" 8 9 #include <AutoLocker.h> 10 11 #include "LocatableDirectory.h" 12 13 14 // #pragma mark - LocatableFile 15 16 17 LocatableFile::LocatableFile(LocatableEntryOwner* owner, 18 LocatableDirectory* directory, const BString& name) 19 : 20 LocatableEntry(owner, directory), 21 fName(name), 22 fLocatedPath(), 23 fListeners(8) 24 { 25 } 26 27 28 LocatableFile::~LocatableFile() 29 { 30 } 31 32 33 const char* 34 LocatableFile::Name() const 35 { 36 return fName.String(); 37 } 38 39 40 void 41 LocatableFile::GetPath(BString& _path) const 42 { 43 fParent->GetPath(_path); 44 if (_path.Length() != 0) 45 _path << '/'; 46 _path << fName; 47 } 48 49 50 bool 51 LocatableFile::GetLocatedPath(BString& _path) const 52 { 53 AutoLocker<LocatableEntryOwner> locker(fOwner); 54 55 if (fLocatedPath.Length() > 0) { 56 _path = fLocatedPath; 57 return true; 58 } 59 60 if (!fParent->GetLocatedPath(_path)) 61 return false; 62 63 _path << '/' << fName; 64 return true; 65 } 66 67 68 void 69 LocatableFile::SetLocatedPath(const BString& path, bool implicit) 70 { 71 // called with owner already locked 72 73 if (implicit) { 74 fLocatedPath = (const char*)NULL; 75 fState = LOCATABLE_ENTRY_LOCATED_IMPLICITLY; 76 } else { 77 fLocatedPath = path; 78 fState = LOCATABLE_ENTRY_LOCATED_EXPLICITLY; 79 } 80 81 _NotifyListeners(); 82 } 83 84 85 bool 86 LocatableFile::AddListener(Listener* listener) 87 { 88 AutoLocker<LocatableEntryOwner> locker(fOwner); 89 return fListeners.AddItem(listener); 90 } 91 92 93 void 94 LocatableFile::RemoveListener(Listener* listener) 95 { 96 AutoLocker<LocatableEntryOwner> locker(fOwner); 97 fListeners.RemoveItem(listener); 98 } 99 100 101 void 102 LocatableFile::_NotifyListeners() 103 { 104 for (int32 i = fListeners.CountItems() - 1; i >= 0; i--) 105 fListeners.ItemAt(i)->LocatableFileChanged(this); 106 } 107 108 109 // #pragma mark - Listener 110 111 112 LocatableFile::Listener::~Listener() 113 { 114 } 115