From 4f13e86cb875221d628da6e59d6e65643e0782fe Mon Sep 17 00:00:00 2001 From: Stefan Hundhammer Date: Fri, 12 Jan 2024 16:50:23 +0100 Subject: [PATCH] Artificial file type for non-suffix rules (Fixes #241) --- src/FileTypeStats.cpp | 35 ++++++++++++++++++++-------- src/FileTypeStats.h | 19 ++++++++++++++- src/FileTypeStatsWindow.cpp | 46 +++++++++++++++++++++++++++++++------ 3 files changed, 83 insertions(+), 17 deletions(-) diff --git a/src/FileTypeStats.cpp b/src/FileTypeStats.cpp index 23bcfe9d..8af8b414 100644 --- a/src/FileTypeStats.cpp +++ b/src/FileTypeStats.cpp @@ -71,6 +71,19 @@ FileSize FileTypeStats::categorySum( MimeCategory * category ) const } +int FileTypeStats::categoryNonSuffixRuleCount( MimeCategory * category ) const +{ + return _categoryNonSuffixRuleCount.value( category, 0 ); + +} + + +FileSize FileTypeStats::categoryNonSuffixRuleSum( MimeCategory * category ) const +{ + return _categoryNonSuffixRuleSum.value( category, 0LL ); +} + + MimeCategory * FileTypeStats::category( const QString & suffix ) const { return _mimeCategorizer->category( "x." + suffix ); @@ -144,6 +157,17 @@ void FileTypeStats::collect( FileInfo * dir ) { _categorySum[ category ] += item->size(); ++_categoryCount[ category ]; + + if ( suffix.isEmpty() ) + { + _categoryNonSuffixRuleSum[ category ] += item->size(); + ++_categoryNonSuffixRuleCount[ category ]; + } + else + { + _suffixSum[ suffix ] += item->size(); + ++_suffixCount[ suffix ]; + } } else // ! category { @@ -172,18 +196,11 @@ void FileTypeStats::collect( FileInfo * dir ) if ( suffix.isEmpty() ) suffix = NO_SUFFIX; - } - - if ( ! suffix.isEmpty() ) - { - // The suffix may stil be empty if this was found via a - // non-suffix rule by the MimeCategorizer. - _suffixSum[ suffix ] += item->size(); ++_suffixCount[ suffix ]; } - } - // Disregard symlinks, block devices and other special files + // Disregard symlinks, block devices and other special files + } ++it; } diff --git a/src/FileTypeStats.h b/src/FileTypeStats.h index 142273ea..ad31a790 100644 --- a/src/FileTypeStats.h +++ b/src/FileTypeStats.h @@ -16,7 +16,10 @@ #include "ui_file-type-stats-window.h" #include "DirInfo.h" -#define NO_SUFFIX "//" // A slash is illegal in Linux/Unix filenames +// Using a suffix that can never occur: A slash is illegal in Linux/Unix +// filenames. +#define NO_SUFFIX "//" +#define NON_SUFFIX_RULE "//" namespace QDirStat @@ -101,6 +104,18 @@ namespace QDirStat **/ FileSize categorySum( MimeCategory * category ) const; + /** + * Return the number of files in the tree matched by a non-suffix rule + * with the specified category. + **/ + int categoryNonSuffixRuleCount( MimeCategory * category ) const; + + /** + * Return the total file size of files in the tree matched by a + * non-suffix rule with the specified category. + **/ + FileSize categoryNonSuffixRuleSum( MimeCategory * category ) const; + /** * Return the category for the specified suffix or 0 if there is none. **/ @@ -188,6 +203,8 @@ namespace QDirStat StringIntMap _suffixCount; CategoryFileSizeMap _categorySum; CategoryIntMap _categoryCount; + CategoryFileSizeMap _categoryNonSuffixRuleSum; + CategoryIntMap _categoryNonSuffixRuleCount; FileSize _totalSize; }; diff --git a/src/FileTypeStatsWindow.cpp b/src/FileTypeStatsWindow.cpp index 1a6d39b8..56f585e4 100644 --- a/src/FileTypeStatsWindow.cpp +++ b/src/FileTypeStatsWindow.cpp @@ -162,20 +162,42 @@ void FileTypeStatsWindow::populate( FileInfo * newSubtree ) if ( category ) { + // + // Add a category item + // + FileSize sum = it.value(); int count = _stats->categoryCount( category ); double percentage = _stats->percentage( sum ); - CategoryFileTypeItem * item = + CategoryFileTypeItem * catItem = new CategoryFileTypeItem( category, count, sum, percentage ); - CHECK_NEW( item ); + CHECK_NEW( catItem ); - _ui->treeWidget->addTopLevelItem( item ); - item->setBold(); - categoryItem[ category ] = item; + _ui->treeWidget->addTopLevelItem( catItem ); + catItem->setBold(); + categoryItem[ category ] = catItem; if ( category == _stats->otherCategory() ) - otherCategoryItem = item; + otherCategoryItem = catItem; + else + { + if ( _stats->categoryNonSuffixRuleCount( category ) > 0 ) + { + // Add an item below the category for files + // matching any non-suffix rules + + QString suffix = NON_SUFFIX_RULE; + FileSize sum = _stats->categoryNonSuffixRuleSum ( category ); + int count = _stats->categoryNonSuffixRuleCount( category ); + double percentage = _stats->percentage( sum ); + + SuffixFileTypeItem * item = new SuffixFileTypeItem( suffix, count, sum, percentage ); + CHECK_NEW( item ); + + catItem->addChild( item ); + } + } } } @@ -347,6 +369,12 @@ QString FileTypeStatsWindow::currentSuffix() const return QString(); } + if ( current->suffix() == NON_SUFFIX_RULE ) + { + logWarning() << "NON_SUFFIX_RULE selected" << endl; + + return QString(); + } return current->suffix(); } @@ -361,7 +389,9 @@ void FileTypeStatsWindow::enableActions( QTreeWidgetItem * currentItem ) SuffixFileTypeItem * suffixItem = dynamic_cast( currentItem ); - enabled = suffixItem && suffixItem->suffix() != NO_SUFFIX; + enabled = suffixItem && + suffixItem->suffix() != NO_SUFFIX && + suffixItem->suffix() != NON_SUFFIX_RULE; } _ui->actionLocate->setEnabled( enabled ); @@ -404,6 +434,8 @@ SuffixFileTypeItem::SuffixFileTypeItem( const QString & suffix, { if ( suffix == NO_SUFFIX ) setText( FT_NameCol, QObject::tr( "" ) ); + else if ( suffix == NON_SUFFIX_RULE ) + setText( FT_NameCol, QObject::tr( "" ) ); else _suffix = "*." + suffix; }