// Returns true if this object is a child of parent, either direct or indirect
bool CFileSystemObject::isChildOf(const CFileSystemObject &parent) const
{
	if (!isValid() || !parent.isValid())
		return false;

	if (fullAbsolutePath().startsWith(parent.fullAbsolutePath(), caseSensitiveFilesystem() ? Qt::CaseSensitive : Qt::CaseInsensitive))
		return true;

	if (!isSymLink() && !parent.isSymLink())
		return false;

	const auto resolvedChildLink = isSymLink() ? symLinkTarget() : fullAbsolutePath();
	const auto resolvedParentLink = parent.isSymLink() ? parent.symLinkTarget() : parent.fullAbsolutePath();

	assert_and_return_r(!resolvedChildLink.isEmpty() && !resolvedParentLink.isEmpty(), false);
	return resolvedChildLink.startsWith(resolvedParentLink, caseSensitiveFilesystem() ? Qt::CaseSensitive : Qt::CaseInsensitive);
}
Ejemplo n.º 2
0
// Indicates that an item was activated and appropriate action should be taken. Returns error message, if any
FileOperationResultCode CController::itemActivated(qulonglong itemHash, Panel p)
{
	const auto item = panel(p).itemByHash(itemHash);
	if (item.isDir())
	{
		// Attempting to enter this dir
		const FileOperationResultCode result = setPath(p, item.fullAbsolutePath(), item.isCdUp() ? refreshCauseCdUp : refreshCauseForwardNavigation);
		return result;
	}
	else if (item.isFile())
	{
		if (item.isExecutable())
			// Attempting to launch this exe from the current directory
			return QProcess::startDetached(toPosixSeparators(item.fullAbsolutePath()), QStringList(), toPosixSeparators(item.parentDirPath())) ? rcOk : rcFail;
		else
			// It's probably not a binary file, try opening with openUrl
			return QDesktopServices::openUrl(QUrl::fromLocalFile(toPosixSeparators(item.fullAbsolutePath()))) ? rcOk : rcFail;
	}

	return rcFail;
}
void CFileSystemObject::refreshInfo()
{
	_properties.exists = _fileInfo.exists();
	_properties.fullPath = _fileInfo.absoluteFilePath();

	if (_fileInfo.isFile())
		_properties.type = File;
	else if (_fileInfo.isDir())
	{
		// Normalization - very important for hash calculation and equality checking
		// C:/1/ must be equal to C:/1
		if (!_properties.fullPath.endsWith('/'))
			_properties.fullPath.append('/');

		_properties.type = _fileInfo.isBundle() ? Bundle : Directory;
	}
	else if (_properties.exists)
	{
#ifdef _WIN32
		qInfo() << _properties.fullPath << " is neither a file nor a dir";
#endif
	}
	else if (_properties.fullPath.endsWith('/'))
		_properties.type = Directory;

	_properties.hash = fasthash64(_properties.fullPath.constData(), _properties.fullPath.size() * sizeof(QChar), 0);


	if (_properties.type == File)
	{
		_properties.extension = _fileInfo.suffix();
		_properties.completeBaseName = _fileInfo.completeBaseName();
	}
	else if (_properties.type == Directory)
	{
		_properties.completeBaseName = _fileInfo.baseName();
		const QString suffix = _fileInfo.completeSuffix();
		if (!suffix.isEmpty())
			_properties.completeBaseName = _properties.completeBaseName % '.' % suffix;

		// Ugly temporary bug fix for #141
		if (_properties.completeBaseName.isEmpty() && _properties.fullPath.endsWith('/'))
		{
			const QFileInfo tmpInfo = QFileInfo(_properties.fullPath.left(_properties.fullPath.length() - 1));
			_properties.completeBaseName = tmpInfo.baseName();
			const QString sfx = tmpInfo.completeSuffix();
			if (!sfx.isEmpty())
				_properties.completeBaseName = _properties.completeBaseName % '.' % sfx;
		}
	}
	else if (_properties.type == Bundle)
	{
		_properties.extension = _fileInfo.suffix();
		_properties.completeBaseName = _fileInfo.completeBaseName();
	}

	_properties.fullName = _properties.type == Directory ? _properties.completeBaseName : _fileInfo.fileName();
	_properties.isCdUp = _properties.fullName == QLatin1String("..");
	// QFileInfo::canonicalPath() / QFileInfo::absolutePath are undefined for non-files
	_properties.parentFolder = parentForAbsolutePath(_properties.fullPath);

	if (!_properties.exists)
		return;

	_properties.creationDate = (time_t) _fileInfo.created().toTime_t();
	_properties.modificationDate = _fileInfo.lastModified().toTime_t();
	_properties.size = _properties.type == File ? _fileInfo.size() : 0;

	if (isDir())
		_dir.setPath(fullAbsolutePath());
	else
		_dir = QDir();
}
bool CFileSystemObject::isEmptyDir() const
{
	return isDir() && QDir(fullAbsolutePath()).entryList(QDir::NoDotAndDotDot | QDir::Hidden | QDir::System).empty();
}