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