14cf3529cSIngo Weinhold/*
26077cad8SIngo Weinhold * Copyright 2013-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
34cf3529cSIngo Weinhold * Distributed under the terms of the MIT License.
44cf3529cSIngo Weinhold */
54cf3529cSIngo Weinhold
64cf3529cSIngo Weinhold
74cf3529cSIngo Weinhold#include <EntryOperationEngineBase.h>
84cf3529cSIngo Weinhold
94cf3529cSIngo Weinhold#include <Directory.h>
104cf3529cSIngo Weinhold#include <Entry.h>
114cf3529cSIngo Weinhold#include <Path.h>
124cf3529cSIngo Weinhold
134cf3529cSIngo Weinhold
144cf3529cSIngo Weinholdnamespace BPrivate {
154cf3529cSIngo Weinhold
164cf3529cSIngo Weinhold
174cf3529cSIngo WeinholdBEntryOperationEngineBase::Entry::Entry(const char* path)
184cf3529cSIngo Weinhold	:
194cf3529cSIngo Weinhold	fDirectory(NULL),
204cf3529cSIngo Weinhold	fPath(path),
214cf3529cSIngo Weinhold	fEntry(NULL),
229680cf0bSIngo Weinhold	fEntryRef(NULL),
239680cf0bSIngo Weinhold	fDirectoryRef(NULL)
244cf3529cSIngo Weinhold{
254cf3529cSIngo Weinhold}
264cf3529cSIngo Weinhold
274cf3529cSIngo Weinhold
289680cf0bSIngo WeinholdBEntryOperationEngineBase::Entry::Entry(const BDirectory& directory,
299680cf0bSIngo Weinhold	const char* path)
304cf3529cSIngo Weinhold	:
314cf3529cSIngo Weinhold	fDirectory(&directory),
324cf3529cSIngo Weinhold	fPath(path),
334cf3529cSIngo Weinhold	fEntry(NULL),
349680cf0bSIngo Weinhold	fEntryRef(NULL),
359680cf0bSIngo Weinhold	fDirectoryRef(NULL)
364cf3529cSIngo Weinhold{
374cf3529cSIngo Weinhold}
384cf3529cSIngo Weinhold
394cf3529cSIngo Weinhold
404cf3529cSIngo WeinholdBEntryOperationEngineBase::Entry::Entry(const BEntry& entry)
414cf3529cSIngo Weinhold	:
424cf3529cSIngo Weinhold	fDirectory(NULL),
434cf3529cSIngo Weinhold	fPath(NULL),
444cf3529cSIngo Weinhold	fEntry(&entry),
459680cf0bSIngo Weinhold	fEntryRef(NULL),
469680cf0bSIngo Weinhold	fDirectoryRef(NULL)
474cf3529cSIngo Weinhold{
484cf3529cSIngo Weinhold}
494cf3529cSIngo Weinhold
504cf3529cSIngo Weinhold
514cf3529cSIngo WeinholdBEntryOperationEngineBase::Entry::Entry(const entry_ref& entryRef)
524cf3529cSIngo Weinhold	:
534cf3529cSIngo Weinhold	fDirectory(NULL),
544cf3529cSIngo Weinhold	fPath(NULL),
554cf3529cSIngo Weinhold	fEntry(NULL),
569680cf0bSIngo Weinhold	fEntryRef(&entryRef),
579680cf0bSIngo Weinhold	fDirectoryRef(NULL)
589680cf0bSIngo Weinhold{
599680cf0bSIngo Weinhold}
609680cf0bSIngo Weinhold
619680cf0bSIngo Weinhold
629680cf0bSIngo WeinholdBEntryOperationEngineBase::Entry::Entry(const node_ref& directoryRef,
639680cf0bSIngo Weinhold	const char* path)
649680cf0bSIngo Weinhold	:
659680cf0bSIngo Weinhold	fDirectory(NULL),
669680cf0bSIngo Weinhold	fPath(path),
679680cf0bSIngo Weinhold	fEntry(NULL),
689680cf0bSIngo Weinhold	fEntryRef(NULL),
699680cf0bSIngo Weinhold	fDirectoryRef(&directoryRef)
704cf3529cSIngo Weinhold{
714cf3529cSIngo Weinhold}
724cf3529cSIngo Weinhold
734cf3529cSIngo Weinhold
744cf3529cSIngo WeinholdBEntryOperationEngineBase::Entry::~Entry()
754cf3529cSIngo Weinhold{
764cf3529cSIngo Weinhold}
774cf3529cSIngo Weinhold
784cf3529cSIngo Weinhold
794cf3529cSIngo Weinholdstatus_t
804cf3529cSIngo WeinholdBEntryOperationEngineBase::Entry::GetPath(BPath& buffer, const char*& _path)
814cf3529cSIngo Weinhold	const
824cf3529cSIngo Weinhold{
83c8f47aeaSRene Gollent	status_t error = B_OK;
844cf3529cSIngo Weinhold
854cf3529cSIngo Weinhold	if (fEntry != NULL) {
864cf3529cSIngo Weinhold		error = buffer.SetTo(fEntry);
874cf3529cSIngo Weinhold	} else if (fDirectory != NULL) {
884cf3529cSIngo Weinhold		error = buffer.SetTo(fDirectory, fPath);
894cf3529cSIngo Weinhold	} else if (fEntryRef != NULL) {
904cf3529cSIngo Weinhold		error = buffer.SetTo(fEntryRef);
919680cf0bSIngo Weinhold	} else if (fDirectoryRef != NULL) {
929680cf0bSIngo Weinhold		BDirectory directory;
939680cf0bSIngo Weinhold		error = directory.SetTo(fDirectoryRef);
949680cf0bSIngo Weinhold		if (error == B_OK)
959680cf0bSIngo Weinhold			error = buffer.SetTo(&directory, fPath);
964cf3529cSIngo Weinhold	} else if (fPath != NULL) {
974cf3529cSIngo Weinhold		_path = fPath;
984cf3529cSIngo Weinhold		return B_OK;
994cf3529cSIngo Weinhold	}
1004cf3529cSIngo Weinhold
1014cf3529cSIngo Weinhold	if (error != B_OK)
1024cf3529cSIngo Weinhold		return error;
1034cf3529cSIngo Weinhold
1044cf3529cSIngo Weinhold	_path = buffer.Path();
1054cf3529cSIngo Weinhold	return B_OK;
1064cf3529cSIngo Weinhold}
1074cf3529cSIngo Weinhold
1084cf3529cSIngo Weinhold
1094cf3529cSIngo WeinholdBString
1104cf3529cSIngo WeinholdBEntryOperationEngineBase::Entry::Path() const
1114cf3529cSIngo Weinhold{
1124cf3529cSIngo Weinhold	BPath pathBuffer;
1134cf3529cSIngo Weinhold	const char* path;
1144cf3529cSIngo Weinhold	if (GetPath(pathBuffer, path) == B_OK)
1154cf3529cSIngo Weinhold		return BString(path);
1164cf3529cSIngo Weinhold	return BString();
1174cf3529cSIngo Weinhold}
1184cf3529cSIngo Weinhold
1194cf3529cSIngo Weinhold
1206077cad8SIngo Weinholdstatus_t
1216077cad8SIngo WeinholdBEntryOperationEngineBase::Entry::GetPathOrName(BString& _path) const
1226077cad8SIngo Weinhold{
1236077cad8SIngo Weinhold	_path.Truncate(0);
1246077cad8SIngo Weinhold
1256077cad8SIngo Weinhold	BPath buffer;
1266077cad8SIngo Weinhold	const char* path;
1276077cad8SIngo Weinhold	status_t error = GetPath(buffer, path);
1286077cad8SIngo Weinhold	if (error == B_NO_MEMORY)
1296077cad8SIngo Weinhold		return error;
1306077cad8SIngo Weinhold
1316077cad8SIngo Weinhold	if (error == B_OK) {
1326077cad8SIngo Weinhold		_path = path;
1336077cad8SIngo Weinhold	} else if (fEntry != NULL) {
1346077cad8SIngo Weinhold		// GetPath() apparently failed, so just return the entry name.
1356077cad8SIngo Weinhold		_path = fEntry->Name();
1366077cad8SIngo Weinhold	} else if (fDirectory != NULL || fDirectoryRef != NULL) {
1376077cad8SIngo Weinhold		if (fPath != NULL && fPath[0] == '/') {
1386077cad8SIngo Weinhold			// absolute path -- just return it
1396077cad8SIngo Weinhold			_path = fPath;
1406077cad8SIngo Weinhold		} else {
1416077cad8SIngo Weinhold			// get the directory path
1426077cad8SIngo Weinhold			BEntry entry;
1436077cad8SIngo Weinhold			if (fDirectory != NULL) {
1446077cad8SIngo Weinhold				error = fDirectory->GetEntry(&entry);
1456077cad8SIngo Weinhold			} else {
1466077cad8SIngo Weinhold				BDirectory directory;
1476077cad8SIngo Weinhold				error = directory.SetTo(fDirectoryRef);
1486077cad8SIngo Weinhold				if (error == B_OK)
1496077cad8SIngo Weinhold					error = directory.GetEntry(&entry);
1506077cad8SIngo Weinhold			}
1516077cad8SIngo Weinhold
1526077cad8SIngo Weinhold			if (error != B_OK || (error = entry.GetPath(&buffer)) != B_OK)
1536077cad8SIngo Weinhold				return error;
1546077cad8SIngo Weinhold
1556077cad8SIngo Weinhold			_path = buffer.Path();
1566077cad8SIngo Weinhold
1576077cad8SIngo Weinhold			// If we additionally have a relative path, append it.
1586077cad8SIngo Weinhold			if (!_path.IsEmpty() && fPath != NULL) {
1596077cad8SIngo Weinhold				int32 length = _path.Length();
1606077cad8SIngo Weinhold				_path << '/' << fPath;
1616077cad8SIngo Weinhold				if (_path.Length() < length + 2)
1626077cad8SIngo Weinhold					return B_NO_MEMORY;
1636077cad8SIngo Weinhold			}
1646077cad8SIngo Weinhold		}
1656077cad8SIngo Weinhold	} else if (fEntryRef != NULL) {
1666077cad8SIngo Weinhold		// Getting the actual path apparently failed, so just return the entry
1676077cad8SIngo Weinhold		// name.
1686077cad8SIngo Weinhold		_path = fEntryRef->name;
1696077cad8SIngo Weinhold	} else if (fPath != NULL)
1706077cad8SIngo Weinhold		_path = fPath;
1716077cad8SIngo Weinhold
1726077cad8SIngo Weinhold	return _path.IsEmpty() ? B_NO_MEMORY : B_OK;
1736077cad8SIngo Weinhold}
1746077cad8SIngo Weinhold
1756077cad8SIngo Weinhold
1766077cad8SIngo WeinholdBString
1776077cad8SIngo WeinholdBEntryOperationEngineBase::Entry::PathOrName() const
1786077cad8SIngo Weinhold{
1796077cad8SIngo Weinhold	BString path;
1806077cad8SIngo Weinhold	return GetPathOrName(path) == B_OK ? path : BString();
1816077cad8SIngo Weinhold}
1826077cad8SIngo Weinhold
1836077cad8SIngo Weinhold
1844cf3529cSIngo Weinhold} // namespace BPrivate
185