Skip to content

Commit

Permalink
Simplify some file list usage using filters new to QT4
Browse files Browse the repository at this point in the history
QT4 introduced a 'QDir::NoDotAndDotDot' filter for use with QDir::setFilter(),
QDir::entryList() and QDir::entryInfoList(). This allows us to
simplify a few places in the code by removing our own . and .. checks.

Thanks to Rolf Eike Beer <eike@sf-mail.de> for spotting the problem
and providing an example patch. Since there were many more examples in
the code than the one he highlighted I decided to fix them all in one
commit instead of applying his patch.
  • Loading branch information
stuartm committed Feb 14, 2013
1 parent efc1e46 commit a05b4d4
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 90 deletions.
5 changes: 2 additions & 3 deletions mythplugins/mythgallery/mythgallery/galleryfilter.cpp
Expand Up @@ -73,7 +73,8 @@ bool GalleryFilter::TestFilter(const QString& dir, const GalleryFilter& flt,
QDir d(dir);
QString currDir = d.absolutePath();
QFileInfoList list = d.entryInfoList(GalleryUtil::GetMediaFilter(),
QDir::Files | QDir::AllDirs,
QDir::Files | QDir::AllDirs |
QDir::NoDotAndDotDot,
(QDir::SortFlag)flt.getSort());

if (list.isEmpty())
Expand All @@ -86,8 +87,6 @@ bool GalleryFilter::TestFilter(const QString& dir, const GalleryFilter& flt,
it != list.end(); ++it)
{
fi = &(*it);
if (fi->fileName() == "." || fi->fileName() == "..")
continue;

// remove these already-resized pictures.
if ((fi->fileName().indexOf(".thumb.") > 0) ||
Expand Down
27 changes: 11 additions & 16 deletions mythplugins/mythgallery/mythgallery/galleryutil.cpp
Expand Up @@ -260,9 +260,10 @@ bool GalleryUtil::LoadDirectory(ThumbList& itemList, const QString& dir,
// Create .thumbcache dir if neccesary
if (thumbGen)
thumbGen->getThumbcacheDir(currDir);

QFileInfoList list = d.entryInfoList(GetMediaFilter(),
QDir::Files | QDir::AllDirs,
QDir::Files | QDir::AllDirs |
QDir::NoDotAndDotDot,
(QDir::SortFlag)flt.getSort());

if (list.isEmpty())
Expand All @@ -286,8 +287,6 @@ bool GalleryUtil::LoadDirectory(ThumbList& itemList, const QString& dir,
{
fi = &(*it);
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
continue;

// remove these already-resized pictures.
if (isGallery && (
Expand Down Expand Up @@ -585,16 +584,14 @@ bool GalleryUtil::CopyDirectory(const QFileInfo src, QFileInfo &dst)

bool ok = true;
QDir dstDir(dst.absoluteFilePath());
srcDir.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = srcDir.entryInfoList();
QFileInfoList::const_iterator it = list.begin();
for (; it != list.end(); ++it)
{
const QString fn = it->fileName();
if (fn != "." && fn != "..")
{
QFileInfo dfi(dstDir, fn);
ok &= Copy(*it, dfi);
}
QFileInfo dfi(dstDir, fn);
ok &= Copy(*it, dfi);
}

return ok;
Expand All @@ -616,16 +613,14 @@ bool GalleryUtil::MoveDirectory(const QFileInfo src, QFileInfo &dst)

bool ok = true;
QDir dstDir(dst.absoluteFilePath());
srcDir.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = srcDir.entryInfoList();
QFileInfoList::const_iterator it = list.begin();
for (; it != list.end(); ++it)
{
const QString fn = it->fileName();
if (fn != "." && fn != "..")
{
QFileInfo dfi(dstDir, fn);
ok &= Move(*it, dfi);
}
QFileInfo dfi(dstDir, fn);
ok &= Move(*it, dfi);
}

return ok && FileDelete(src);
Expand All @@ -637,13 +632,13 @@ bool GalleryUtil::DeleteDirectory(const QFileInfo &dir)
return false;

QDir srcDir(dir.absoluteFilePath());
srcDir.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = srcDir.entryInfoList();
QFileInfoList::const_iterator it = list.begin();
for (; it != list.end(); ++it)
{
const QString fn = it->fileName();
if (fn != "." && fn != "..")
Delete(*it);
Delete(*it);
}

return FileDelete(dir);
Expand Down
10 changes: 4 additions & 6 deletions mythplugins/mythgallery/mythgallery/iconview.cpp
Expand Up @@ -1388,7 +1388,8 @@ void IconView::ImportFromDir(const QString &fromDir, const QString &toDir)
d.setNameFilters(GalleryUtil::GetMediaFilter());
d.setSorting((QDir::SortFlag)m_sortorder);
d.setFilter(QDir::Files | QDir::AllDirs |
QDir::NoSymLinks | QDir::Readable);
QDir::NoSymLinks | QDir::Readable |
QDir::NoDotAndDotDot);
QFileInfoList list = d.entryInfoList();
QFileInfoList::const_iterator it = list.begin();
const QFileInfo *fi;
Expand All @@ -1397,8 +1398,6 @@ void IconView::ImportFromDir(const QString &fromDir, const QString &toDir)
{
fi = &(*it);
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
continue;

if (fi->isDir())
{
Expand Down Expand Up @@ -1566,7 +1565,8 @@ int ChildCountThread::getChildCount(const QString &filepath)
isGallery = (gList.count() != 0);

QFileInfoList list = d.entryInfoList(GalleryUtil::GetMediaFilter(),
QDir::Files | QDir::AllDirs);
QDir::Files | QDir::AllDirs |
QDir::NoDotAndDotDot);

if (list.isEmpty())
return 0;
Expand All @@ -1579,8 +1579,6 @@ int ChildCountThread::getChildCount(const QString &filepath)
{
fi = &(*it);
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
continue;

// remove these already-resized pictures.
if (isGallery && (
Expand Down
6 changes: 1 addition & 5 deletions mythplugins/mythgallery/mythgallery/thumbgenerator.cpp
Expand Up @@ -262,7 +262,7 @@ void ThumbGenerator::loadDir(QImage& image, const QFileInfo& fi)

// if we didn't find the image yet
// go into subdirs and keep looking
dir.setFilter(QDir::Dirs);
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QFileInfoList dirlist = dir.entryInfoList();
if (dirlist.isEmpty())
return;
Expand All @@ -271,10 +271,6 @@ void ThumbGenerator::loadDir(QImage& image, const QFileInfo& fi)
it != dirlist.end() && image.isNull() && !m_cancel; ++it)
{
const QFileInfo *f = &(*it);

if (f->fileName() == "." || f->fileName() == "..")
continue;

loadDir(image, *f);
}
}
Expand Down
14 changes: 2 additions & 12 deletions mythplugins/mythgame/mythgame/gamehandler.cpp
Expand Up @@ -561,19 +561,14 @@ int GameHandler::buildFileCount(QString directory, GameHandler *handler)
if (!RomDir.isReadable())
return 0;

RomDir.setFilter(QDir::NoDotAndDotDot);
QFileInfoList List = RomDir.entryInfoList();
for (QFileInfoList::const_iterator it = List.begin();
it != List.end(); ++it)
{
QFileInfo Info = *it;
QString RomName = Info.fileName();

if (RomName == "." ||
RomName == "..")
{
continue;
}

if (Info.isDir())
{
filecount += buildFileCount(Info.filePath(), handler);
Expand Down Expand Up @@ -633,6 +628,7 @@ void GameHandler::buildFileList(QString directory, GameHandler *handler,
return;

RomDir.setSorting( QDir:: DirsFirst | QDir::Name );
RomDir.setFilter(QDir::NoDotAndDotDot);
QFileInfoList List = RomDir.entryInfoList();
for (QFileInfoList::const_iterator it = List.begin();
it != List.end(); ++it)
Expand All @@ -641,12 +637,6 @@ void GameHandler::buildFileList(QString directory, GameHandler *handler,
QString RomName = Info.fileName();
QString GameName = Info.completeBaseName();

if (RomName == "." ||
RomName == "..")
{
continue;
}

if (Info.isDir())
{
buildFileList(Info.filePath(), handler, filecount);
Expand Down
3 changes: 1 addition & 2 deletions mythplugins/mythmusic/mythmusic/filescanner.cpp
Expand Up @@ -87,6 +87,7 @@ void FileScanner::BuildFileList(QString &directory, MusicLoadedMap &music_files,
if (!d.exists())
return;

d.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = d.entryInfoList();
if (list.isEmpty())
return;
Expand All @@ -102,8 +103,6 @@ void FileScanner::BuildFileList(QString &directory, MusicLoadedMap &music_files,
{
fi = &(*it);
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
continue;
QString filename = fi->absoluteFilePath();
if (fi->isDir())
{
Expand Down
8 changes: 3 additions & 5 deletions mythplugins/mythmusic/mythmusic/importmusic.cpp
Expand Up @@ -554,6 +554,7 @@ void ImportMusicDialog::scanDirectory(QString &directory, vector<TrackInfo*> *tr
if (!d.exists())
return;

d.setFilter(QDir::NoDotAndDotDot);
const QFileInfoList list = d.entryInfoList();
if (list.isEmpty())
return;
Expand All @@ -565,8 +566,6 @@ void ImportMusicDialog::scanDirectory(QString &directory, vector<TrackInfo*> *tr
{
fi = &(*it);
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
continue;
QString filename = fi->absoluteFilePath();
if (fi->isDir())
scanDirectory(filename, tracks);
Expand Down Expand Up @@ -1028,7 +1027,8 @@ void ImportCoverArtDialog::scanDirectory()
QString nameFilter = gCoreContext->GetSetting("AlbumArtFilter",
"*.png;*.jpg;*.jpeg;*.gif;*.bmp");

QFileInfoList list = d.entryInfoList(nameFilter.split(";"));
QFileInfoList list = d.entryInfoList(nameFilter.split(";"),
QDir::NoDotAndDotDot);
if (list.isEmpty())
return;

Expand All @@ -1039,8 +1039,6 @@ void ImportCoverArtDialog::scanDirectory()
{
fi = &(*it);
++it;
if (fi->fileName() == "." || fi->fileName() == "..")
continue;
QString filename = fi->absoluteFilePath();
if (!fi->isDir())
{
Expand Down
5 changes: 2 additions & 3 deletions mythplugins/mythweather/mythweather/sourceManager.cpp
Expand Up @@ -375,7 +375,8 @@ void SourceManager::recurseDirs( QDir dir )
if (!dir.exists())
return;

dir.setFilter(QDir::Executable | QDir::Files | QDir::Dirs);
dir.setFilter(QDir::Executable | QDir::Files | QDir::Dirs |
QDir::NoDotAndDotDot);
QFileInfoList files = dir.entryInfoList();
QFileInfo file;

Expand All @@ -385,8 +386,6 @@ void SourceManager::recurseDirs( QDir dir )
file = files.at(x);
if (file.isDir())
{
if (file.fileName() == QString("..")) continue;
if (file.fileName() == QString(".")) continue;
QDir recurseTo(file.filePath());
recurseDirs(recurseTo);
}
Expand Down
10 changes: 2 additions & 8 deletions mythtv/libs/libmyth/mediamonitor-unix.cpp
Expand Up @@ -256,15 +256,12 @@ bool MediaMonitorUnix::CheckMountable(void)
m_fifo = open(kUDEV_FIFO, O_RDONLY | O_NONBLOCK);

QDir sysfs("/sys/block");
sysfs.setFilter(QDir::Dirs);
sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);

QStringList devices = sysfs.entryList();

for (QStringList::iterator it = devices.begin(); it != devices.end(); ++it)
{
if (*it == "." || *it == "..")
continue;

// ignore floppies, too slow
if ((*it).startsWith("fd"))
continue;
Expand Down Expand Up @@ -772,16 +769,13 @@ bool MediaMonitorUnix::FindPartitions(const QString &dev, bool checkPartitions)
{
// check for partitions
QDir sysfs(dev);
sysfs.setFilter(QDir::Dirs);
sysfs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);

bool found_partitions = false;
QStringList parts = sysfs.entryList();
for (QStringList::iterator pit = parts.begin();
pit != parts.end(); ++pit)
{
if (*pit == "." || *pit == "..")
continue;

// skip some sysfs dirs that are _not_ sub-partitions
if (*pit == "device" || *pit == "holders" || *pit == "queue"
|| *pit == "slaves" || *pit == "subsystem"
Expand Down
5 changes: 1 addition & 4 deletions mythtv/libs/libmythbase/mythmedia.cpp
Expand Up @@ -249,7 +249,7 @@ bool MythMediaDevice::ScanMediaType(const QString &directory, ext_cnt_t &cnt)
if (!d.exists())
return false;


d.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = d.entryInfoList();

for( QFileInfoList::iterator it = list.begin();
Expand All @@ -258,9 +258,6 @@ bool MythMediaDevice::ScanMediaType(const QString &directory, ext_cnt_t &cnt)
{
QFileInfo &fi = *it;

if (("." == fi.fileName()) || (".." == fi.fileName()))
continue;

if (fi.isSymLink())
continue;

Expand Down
7 changes: 2 additions & 5 deletions mythtv/libs/libmythbase/storagegroup.cpp
Expand Up @@ -300,18 +300,15 @@ QStringList StorageGroup::GetFileInfoList(QString Path)
if (!d.exists())
return files;

d.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = d.entryInfoList();
if (!list.size())
return files;

for (QFileInfoList::iterator p = list.begin(); p != list.end(); ++p)
{
if (p->fileName() == "." ||
p->fileName() == ".." ||
p->fileName() == "Thumbs.db")
{
if (p->fileName() == "Thumbs.db")
continue;
}

QString tmp;

Expand Down
9 changes: 3 additions & 6 deletions mythtv/libs/libmythmetadata/dirscan.cpp
Expand Up @@ -54,7 +54,8 @@ namespace
// Return a fail if directory doesn't exist.
if (!d.exists())
return false;


d.setFilter(QDir::NoDotAndDotDot);
QFileInfoList list = d.entryInfoList();
// An empty directory is fine
if (!list.size())
Expand All @@ -64,12 +65,8 @@ namespace

for (QFileInfoList::iterator p = list.begin(); p != list.end(); ++p)
{
if (p->fileName() == "." ||
p->fileName() == ".." ||
p->fileName() == "Thumbs.db")
{
if (p->fileName() == "Thumbs.db")
continue;
}

if (!p->isDir() &&
ext_settings.extension_ignored(p->suffix())) continue;
Expand Down
8 changes: 2 additions & 6 deletions mythtv/libs/libmythmetadata/videometadata.cpp
Expand Up @@ -414,7 +414,8 @@ class VideoMetadataImp
bool VideoMetadataImp::removeDir(const QString &dirName)
{
QDir d(dirName);


d.setFilter(QDir::NoDotAndDotDot);
QFileInfoList contents = d.entryInfoList();
if (!contents.size())
{
Expand All @@ -423,11 +424,6 @@ bool VideoMetadataImp::removeDir(const QString &dirName)

for (QFileInfoList::iterator p = contents.begin(); p != contents.end(); ++p)
{
if (p->fileName() == "." ||
p->fileName() == "..")
{
continue;
}
if (p->isDir())
{
QString fileName = p->fileName();
Expand Down

0 comments on commit a05b4d4

Please sign in to comment.