13c5dbb46SIngo Weinhold/*
23c5dbb46SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3584fd961SRene Gollent * Copyright 2016, Rene Gollent, rene@gollent.com.
43c5dbb46SIngo Weinhold * Distributed under the terms of the MIT License.
53c5dbb46SIngo Weinhold */
63c5dbb46SIngo Weinhold
73c5dbb46SIngo Weinhold#include "LocatableFile.h"
83c5dbb46SIngo Weinhold
93c5dbb46SIngo Weinhold#include <AutoLocker.h>
103c5dbb46SIngo Weinhold
113c5dbb46SIngo Weinhold#include "LocatableDirectory.h"
123c5dbb46SIngo Weinhold
133c5dbb46SIngo Weinhold
143c5dbb46SIngo Weinhold// #pragma mark - LocatableFile
153c5dbb46SIngo Weinhold
163c5dbb46SIngo Weinhold
173c5dbb46SIngo WeinholdLocatableFile::LocatableFile(LocatableEntryOwner* owner,
183c5dbb46SIngo Weinhold	LocatableDirectory* directory, const BString& name)
193c5dbb46SIngo Weinhold	:
203c5dbb46SIngo Weinhold	LocatableEntry(owner, directory),
213c5dbb46SIngo Weinhold	fName(name),
223c5dbb46SIngo Weinhold	fLocatedPath(),
233c5dbb46SIngo Weinhold	fListeners(8)
243c5dbb46SIngo Weinhold{
253c5dbb46SIngo Weinhold}
263c5dbb46SIngo Weinhold
273c5dbb46SIngo Weinhold
283c5dbb46SIngo WeinholdLocatableFile::~LocatableFile()
293c5dbb46SIngo Weinhold{
303c5dbb46SIngo Weinhold}
313c5dbb46SIngo Weinhold
323c5dbb46SIngo Weinhold
333c5dbb46SIngo Weinholdconst char*
343c5dbb46SIngo WeinholdLocatableFile::Name() const
353c5dbb46SIngo Weinhold{
363c5dbb46SIngo Weinhold	return fName.String();
373c5dbb46SIngo Weinhold}
383c5dbb46SIngo Weinhold
393c5dbb46SIngo Weinhold
403c5dbb46SIngo Weinholdvoid
413c5dbb46SIngo WeinholdLocatableFile::GetPath(BString& _path) const
423c5dbb46SIngo Weinhold{
433c5dbb46SIngo Weinhold	fParent->GetPath(_path);
44584fd961SRene Gollent	if (_path.Length() != 0)
45584fd961SRene Gollent		_path << '/';
46584fd961SRene Gollent	_path << fName;
473c5dbb46SIngo Weinhold}
483c5dbb46SIngo Weinhold
493c5dbb46SIngo Weinhold
503c5dbb46SIngo Weinholdbool
513c5dbb46SIngo WeinholdLocatableFile::GetLocatedPath(BString& _path) const
523c5dbb46SIngo Weinhold{
533c5dbb46SIngo Weinhold	AutoLocker<LocatableEntryOwner> locker(fOwner);
543c5dbb46SIngo Weinhold
553c5dbb46SIngo Weinhold	if (fLocatedPath.Length() > 0) {
563c5dbb46SIngo Weinhold		_path = fLocatedPath;
573c5dbb46SIngo Weinhold		return true;
583c5dbb46SIngo Weinhold	}
593c5dbb46SIngo Weinhold
603c5dbb46SIngo Weinhold	if (!fParent->GetLocatedPath(_path))
613c5dbb46SIngo Weinhold		return false;
623c5dbb46SIngo Weinhold
633c5dbb46SIngo Weinhold	_path << '/' << fName;
643c5dbb46SIngo Weinhold	return true;
653c5dbb46SIngo Weinhold}
663c5dbb46SIngo Weinhold
673c5dbb46SIngo Weinhold
683c5dbb46SIngo Weinholdvoid
693c5dbb46SIngo WeinholdLocatableFile::SetLocatedPath(const BString& path, bool implicit)
703c5dbb46SIngo Weinhold{
713c5dbb46SIngo Weinhold	// called with owner already locked
723c5dbb46SIngo Weinhold
733c5dbb46SIngo Weinhold	if (implicit) {
743c5dbb46SIngo Weinhold		fLocatedPath = (const char*)NULL;
753c5dbb46SIngo Weinhold		fState = LOCATABLE_ENTRY_LOCATED_IMPLICITLY;
763c5dbb46SIngo Weinhold	} else {
773c5dbb46SIngo Weinhold		fLocatedPath = path;
783c5dbb46SIngo Weinhold		fState = LOCATABLE_ENTRY_LOCATED_EXPLICITLY;
793c5dbb46SIngo Weinhold	}
803c5dbb46SIngo Weinhold
813c5dbb46SIngo Weinhold	_NotifyListeners();
823c5dbb46SIngo Weinhold}
833c5dbb46SIngo Weinhold
843c5dbb46SIngo Weinhold
853c5dbb46SIngo Weinholdbool
863c5dbb46SIngo WeinholdLocatableFile::AddListener(Listener* listener)
873c5dbb46SIngo Weinhold{
883c5dbb46SIngo Weinhold	AutoLocker<LocatableEntryOwner> locker(fOwner);
893c5dbb46SIngo Weinhold	return fListeners.AddItem(listener);
903c5dbb46SIngo Weinhold}
913c5dbb46SIngo Weinhold
923c5dbb46SIngo Weinhold
933c5dbb46SIngo Weinholdvoid
943c5dbb46SIngo WeinholdLocatableFile::RemoveListener(Listener* listener)
953c5dbb46SIngo Weinhold{
963c5dbb46SIngo Weinhold	AutoLocker<LocatableEntryOwner> locker(fOwner);
973c5dbb46SIngo Weinhold	fListeners.RemoveItem(listener);
983c5dbb46SIngo Weinhold}
993c5dbb46SIngo Weinhold
1003c5dbb46SIngo Weinhold
1013c5dbb46SIngo Weinholdvoid
1023c5dbb46SIngo WeinholdLocatableFile::_NotifyListeners()
1033c5dbb46SIngo Weinhold{
1043c5dbb46SIngo Weinhold	for (int32 i = fListeners.CountItems() - 1; i >= 0; i--)
1053c5dbb46SIngo Weinhold		fListeners.ItemAt(i)->LocatableFileChanged(this);
1063c5dbb46SIngo Weinhold}
1073c5dbb46SIngo Weinhold
1083c5dbb46SIngo Weinhold
1093c5dbb46SIngo Weinhold// #pragma mark - Listener
1103c5dbb46SIngo Weinhold
1113c5dbb46SIngo Weinhold
1123c5dbb46SIngo WeinholdLocatableFile::Listener::~Listener()
1133c5dbb46SIngo Weinhold{
1143c5dbb46SIngo Weinhold}
115