2010-11-25 01:23:48 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2018-01-17 21:18:10 +01:00
|
|
|
// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
|
2010-11-25 01:23:48 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2010-11-25 01:23:48 +01:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Model_FileSystem.h"
|
2014-11-25 02:14:42 +01:00
|
|
|
|
|
|
|
//Internal
|
2011-06-04 19:15:32 +02:00
|
|
|
#include "Global.h"
|
2010-11-25 01:23:48 +01:00
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
2014-11-25 17:33:27 +01:00
|
|
|
#include <MUtils/OSSupport.h>
|
2014-11-25 02:14:42 +01:00
|
|
|
|
|
|
|
//Qt
|
2010-11-25 01:23:48 +01:00
|
|
|
#include <QApplication>
|
2011-06-04 19:15:32 +02:00
|
|
|
#include <QFileIconProvider>
|
2011-06-05 14:52:45 +02:00
|
|
|
#include <QDesktopServices>
|
2011-06-04 19:15:32 +02:00
|
|
|
|
2013-10-06 19:28:12 +02:00
|
|
|
//Windows includes
|
|
|
|
#define NOMINMAX
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
|
2012-03-30 03:35:43 +02:00
|
|
|
#define IS_DIR(ATTR) (((ATTR) & FILE_ATTRIBUTE_DIRECTORY) && (!((ATTR) & FILE_ATTRIBUTE_HIDDEN)))
|
|
|
|
#define NO_DOT_OR_DOTDOT(STR) (wcscmp((STR), L".") && wcscmp((STR), L".."))
|
|
|
|
|
2011-06-04 19:15:32 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Dummy QFileIconProvider class
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class QFileIconProviderEx : public QFileIconProvider
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QFileIconProviderEx();
|
|
|
|
virtual QIcon icon(IconType type) const { return (type == Drive) ? m_driveIcon : m_folderIcon; }
|
|
|
|
virtual QIcon icon(const QFileInfo &info) const;
|
|
|
|
virtual QString type (const QFileInfo &info) const { return info.isDir() ? m_folderType : m_emptyType; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const QIcon m_driveIcon;
|
|
|
|
const QIcon m_cdromIcon;
|
|
|
|
const QIcon m_networkIcon;
|
|
|
|
const QIcon m_floppyIcon;
|
|
|
|
const QIcon m_folderIcon;
|
2011-06-05 14:52:45 +02:00
|
|
|
const QIcon m_homeIcon;
|
|
|
|
const QIcon m_desktopIcon;
|
|
|
|
const QIcon m_musicIcon;
|
|
|
|
const QIcon m_moviesIcon;
|
|
|
|
const QIcon m_picturesIcon;
|
2012-04-10 01:25:34 +02:00
|
|
|
const QIcon m_heartIcon;
|
2011-06-04 19:15:32 +02:00
|
|
|
const QIcon m_emptyIcon;
|
|
|
|
const QString m_folderType;
|
|
|
|
const QString m_emptyType;
|
2011-06-05 14:52:45 +02:00
|
|
|
const QString m_homeDir;
|
|
|
|
const QString m_desktopDir;
|
|
|
|
const QString m_musicDir;
|
|
|
|
const QString m_moviesDir;
|
|
|
|
const QString m_picturesDir;
|
2012-04-10 01:25:34 +02:00
|
|
|
const QString m_installDir;
|
2011-06-04 19:15:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
QFileIconProviderEx::QFileIconProviderEx()
|
|
|
|
:
|
|
|
|
m_folderIcon(":/icons/folder.png"),
|
|
|
|
m_driveIcon(":/icons/drive.png"),
|
|
|
|
m_cdromIcon(":/icons/drive_cd.png"),
|
|
|
|
m_networkIcon(":/icons/drive_link.png"),
|
|
|
|
m_floppyIcon(":/icons/drive_disk.png"),
|
2011-06-05 14:52:45 +02:00
|
|
|
m_homeIcon(":/icons/house.png"),
|
|
|
|
m_desktopIcon(":/icons/monitor.png"),
|
|
|
|
m_musicIcon(":/icons/music.png"),
|
|
|
|
m_moviesIcon(":/icons/film.png"),
|
|
|
|
m_picturesIcon(":/icons/picture.png"),
|
2012-04-10 01:25:34 +02:00
|
|
|
m_heartIcon(":/icons/heart.png"),
|
2011-06-05 14:52:45 +02:00
|
|
|
m_homeDir(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))),
|
|
|
|
m_desktopDir(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))),
|
|
|
|
m_musicDir(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MusicLocation))),
|
|
|
|
m_moviesDir(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation))),
|
|
|
|
m_picturesDir(QDir::fromNativeSeparators(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation))),
|
2012-04-10 01:25:34 +02:00
|
|
|
m_installDir(QDir::fromNativeSeparators(qApp->applicationDirPath())),
|
2011-06-04 19:15:32 +02:00
|
|
|
m_folderType("Folder")
|
|
|
|
{
|
2013-10-10 01:46:30 +02:00
|
|
|
/*nothing to do*/
|
2011-06-04 19:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QIcon QFileIconProviderEx::icon(const QFileInfo &info) const
|
|
|
|
{
|
2011-06-05 14:52:45 +02:00
|
|
|
if(info.isFile())
|
|
|
|
{
|
|
|
|
return m_emptyIcon;
|
|
|
|
}
|
|
|
|
else if(info.isRoot())
|
2011-06-04 19:15:32 +02:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
switch(GetDriveType(MUTILS_WCHR(QDir::toNativeSeparators(info.absoluteFilePath()))))
|
2011-06-04 19:15:32 +02:00
|
|
|
{
|
|
|
|
case DRIVE_CDROM:
|
|
|
|
return m_cdromIcon;
|
|
|
|
break;
|
|
|
|
case DRIVE_REMOVABLE:
|
|
|
|
return m_floppyIcon;
|
|
|
|
break;
|
|
|
|
case DRIVE_REMOTE:
|
|
|
|
return m_networkIcon;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return m_driveIcon;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-05 14:52:45 +02:00
|
|
|
else
|
|
|
|
{
|
2012-04-10 01:25:34 +02:00
|
|
|
const QString filePath = info.filePath();
|
|
|
|
if(m_homeDir.compare(filePath, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m_homeIcon;
|
|
|
|
}
|
|
|
|
else if(m_desktopDir.compare(filePath, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m_desktopIcon;
|
|
|
|
}
|
|
|
|
else if(m_musicDir.compare(filePath, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m_musicIcon;
|
|
|
|
}
|
|
|
|
else if(m_moviesDir.compare(filePath, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m_moviesIcon;
|
|
|
|
}
|
|
|
|
else if(m_picturesDir.compare(filePath, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m_picturesIcon;
|
|
|
|
}
|
|
|
|
else if(m_installDir.compare(filePath, Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m_heartIcon;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_folderIcon;
|
|
|
|
}
|
2011-06-05 14:52:45 +02:00
|
|
|
}
|
2011-06-04 19:15:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modified QFileSystemModel class
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2010-11-25 01:23:48 +01:00
|
|
|
|
|
|
|
QFileSystemModelEx::QFileSystemModelEx()
|
|
|
|
:
|
|
|
|
QFileSystemModel()
|
|
|
|
{
|
2011-06-04 19:15:32 +02:00
|
|
|
this->m_myIconProvider = new QFileIconProviderEx();
|
|
|
|
this->setIconProvider(m_myIconProvider);
|
2010-11-25 01:23:48 +01:00
|
|
|
this->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
|
2011-06-04 19:15:32 +02:00
|
|
|
this->setNameFilterDisables(false);
|
2010-11-25 01:23:48 +01:00
|
|
|
}
|
|
|
|
|
2011-06-04 19:15:32 +02:00
|
|
|
QFileSystemModelEx::~QFileSystemModelEx()
|
2010-11-25 01:23:48 +01:00
|
|
|
{
|
2012-04-05 15:24:45 +02:00
|
|
|
removeAllFromCache();
|
2014-11-25 02:14:42 +01:00
|
|
|
MUTILS_DELETE(m_myIconProvider);
|
2011-06-04 19:15:32 +02:00
|
|
|
}
|
2010-11-25 01:23:48 +01:00
|
|
|
|
2011-06-04 19:15:32 +02:00
|
|
|
bool QFileSystemModelEx::hasChildren(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if(parent.isValid())
|
2010-11-25 01:23:48 +01:00
|
|
|
{
|
2012-03-31 22:20:07 +02:00
|
|
|
return hasSubfoldersCached(filePath(parent).toLower()); //return (QDir(QFileSystemModel::filePath(parent)).entryList(QDir::Dirs | QDir::NoDotAndDotDot).count() > 0);
|
2010-11-25 01:23:48 +01:00
|
|
|
}
|
2011-06-04 19:15:32 +02:00
|
|
|
return true;
|
2010-11-25 01:23:48 +01:00
|
|
|
}
|
2012-03-30 03:35:43 +02:00
|
|
|
|
|
|
|
void QFileSystemModelEx::fetchMore(const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
if(parent.isValid())
|
|
|
|
{
|
2012-03-31 22:20:07 +02:00
|
|
|
removeFromCache(filePath(parent).toLower());
|
2012-03-30 03:35:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QFileSystemModel::fetchMore(parent);
|
|
|
|
}
|
|
|
|
|
2012-03-30 15:28:47 +02:00
|
|
|
QModelIndex QFileSystemModelEx::index(const QString &path, int column) const
|
|
|
|
{
|
|
|
|
QFileInfo info(path);
|
|
|
|
if(info.exists() && info.isDir())
|
|
|
|
{
|
2012-03-31 22:20:07 +02:00
|
|
|
QString fullPath = QDir::fromNativeSeparators(info.canonicalFilePath());
|
|
|
|
QStringList parts = fullPath.split('/', QString::SkipEmptyParts);
|
2012-03-30 15:28:47 +02:00
|
|
|
for(int i = 2; i <= parts.count(); i++)
|
|
|
|
{
|
|
|
|
QFileInfo currentPath(((QStringList) parts.mid(0, i)).join("/"));
|
|
|
|
if((!currentPath.exists()) || (!currentPath.isDir()) || currentPath.isHidden())
|
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
}
|
2012-03-31 22:20:07 +02:00
|
|
|
QModelIndex index = QFileSystemModel::index(fullPath, column);
|
|
|
|
if(index.isValid())
|
|
|
|
{
|
|
|
|
QModelIndex temp = index;
|
|
|
|
while(temp.isValid())
|
|
|
|
{
|
|
|
|
removeFromCache(filePath(temp).toLower());
|
|
|
|
temp = temp.parent();
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
2012-03-30 15:28:47 +02:00
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2012-03-30 03:35:43 +02:00
|
|
|
|
2012-04-05 15:24:45 +02:00
|
|
|
void QFileSystemModelEx::flushCache(void)
|
|
|
|
{
|
|
|
|
removeAllFromCache();
|
|
|
|
}
|
|
|
|
|
2012-03-30 03:35:43 +02:00
|
|
|
/* ------------------------ */
|
|
|
|
/* STATIC FUNCTIONS BELOW */
|
|
|
|
/* ------------------------ */
|
|
|
|
|
2012-03-30 15:28:47 +02:00
|
|
|
QHash<const QString, bool> QFileSystemModelEx::s_hasSubfolderCache;
|
|
|
|
QMutex QFileSystemModelEx::s_hasSubfolderMutex;
|
2013-10-10 01:46:30 +02:00
|
|
|
int QFileSystemModelEx::s_findFirstFileExInfoLevel = INT_MAX;
|
2012-03-31 16:27:37 +02:00
|
|
|
|
2012-03-30 03:35:43 +02:00
|
|
|
bool QFileSystemModelEx::hasSubfoldersCached(const QString &path)
|
|
|
|
{
|
2012-03-30 15:28:47 +02:00
|
|
|
QMutexLocker lock(&s_hasSubfolderMutex);
|
2012-03-31 22:20:07 +02:00
|
|
|
|
2012-03-30 15:28:47 +02:00
|
|
|
if(s_hasSubfolderCache.contains(path))
|
2012-03-30 03:35:43 +02:00
|
|
|
{
|
2012-03-30 15:28:47 +02:00
|
|
|
return s_hasSubfolderCache.value(path);
|
2012-03-30 03:35:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool bChildren = hasSubfolders(path);
|
2012-03-30 15:28:47 +02:00
|
|
|
s_hasSubfolderCache.insert(path, bChildren);
|
2012-03-30 03:35:43 +02:00
|
|
|
return bChildren;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QFileSystemModelEx::removeFromCache(const QString &path)
|
|
|
|
{
|
2012-03-30 15:28:47 +02:00
|
|
|
QMutexLocker lock(&s_hasSubfolderMutex);
|
|
|
|
s_hasSubfolderCache.remove(path);
|
2012-03-30 03:35:43 +02:00
|
|
|
}
|
|
|
|
|
2012-04-05 15:24:45 +02:00
|
|
|
void QFileSystemModelEx::removeAllFromCache(void)
|
|
|
|
{
|
|
|
|
QMutexLocker lock(&s_hasSubfolderMutex);
|
|
|
|
s_hasSubfolderCache.clear();
|
|
|
|
}
|
|
|
|
|
2012-03-30 03:35:43 +02:00
|
|
|
bool QFileSystemModelEx::hasSubfolders(const QString &path)
|
|
|
|
{
|
2013-10-10 01:46:30 +02:00
|
|
|
if(s_findFirstFileExInfoLevel == INT_MAX)
|
2012-03-30 15:28:47 +02:00
|
|
|
{
|
2014-11-25 17:33:27 +01:00
|
|
|
const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
|
|
|
|
s_findFirstFileExInfoLevel = (osVersion >= MUtils::OS::Version::WINDOWS_WIN70) ? FindExInfoBasic : FindExInfoStandard;
|
2012-03-30 15:28:47 +02:00
|
|
|
}
|
2012-03-31 16:27:37 +02:00
|
|
|
|
2012-03-30 15:28:47 +02:00
|
|
|
WIN32_FIND_DATAW findData;
|
|
|
|
bool bChildren = false;
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
HANDLE h = FindFirstFileEx(MUTILS_WCHR(QDir::toNativeSeparators(path + "/*")), ((FINDEX_INFO_LEVELS)s_findFirstFileExInfoLevel), &findData, FindExSearchLimitToDirectories, NULL, 0);
|
2012-03-31 16:27:37 +02:00
|
|
|
|
2012-03-30 03:35:43 +02:00
|
|
|
if(h != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
if(NO_DOT_OR_DOTDOT(findData.cFileName))
|
|
|
|
{
|
|
|
|
bChildren = IS_DIR(findData.dwFileAttributes);
|
|
|
|
}
|
|
|
|
while((!bChildren) && FindNextFile(h, &findData))
|
|
|
|
{
|
|
|
|
if(NO_DOT_OR_DOTDOT(findData.cFileName))
|
|
|
|
{
|
|
|
|
bChildren = IS_DIR(findData.dwFileAttributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FindClose(h);
|
|
|
|
}
|
2012-03-31 16:27:37 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
if((err == ERROR_NOT_SUPPORTED) || (err == ERROR_INVALID_PARAMETER))
|
|
|
|
{
|
2013-10-10 01:46:30 +02:00
|
|
|
qWarning("FindFirstFileEx failed with error code #%u", err);
|
2012-03-31 16:27:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 03:35:43 +02:00
|
|
|
return bChildren;
|
|
|
|
}
|