Skip to content

Commit

Permalink
Artificial <other> file type for non-suffix rules (Fixes #241)
Browse files Browse the repository at this point in the history
  • Loading branch information
shundhammer committed Jan 12, 2024
1 parent 62cf73f commit 4f13e86
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 17 deletions.
35 changes: 26 additions & 9 deletions src/FileTypeStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
19 changes: 18 additions & 1 deletion src/FileTypeStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
#include "ui_file-type-stats-window.h"
#include "DirInfo.h"

#define NO_SUFFIX "//<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 "//<No Suffix>"
#define NON_SUFFIX_RULE "//<Other>"


namespace QDirStat
Expand Down Expand Up @@ -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.
**/
Expand Down Expand Up @@ -188,6 +203,8 @@ namespace QDirStat
StringIntMap _suffixCount;
CategoryFileSizeMap _categorySum;
CategoryIntMap _categoryCount;
CategoryFileSizeMap _categoryNonSuffixRuleSum;
CategoryIntMap _categoryNonSuffixRuleCount;

FileSize _totalSize;
};
Expand Down
46 changes: 39 additions & 7 deletions src/FileTypeStatsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Other> 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 );
}
}
}
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -361,7 +389,9 @@ void FileTypeStatsWindow::enableActions( QTreeWidgetItem * currentItem )
SuffixFileTypeItem * suffixItem =
dynamic_cast<SuffixFileTypeItem *>( currentItem );

enabled = suffixItem && suffixItem->suffix() != NO_SUFFIX;
enabled = suffixItem &&
suffixItem->suffix() != NO_SUFFIX &&
suffixItem->suffix() != NON_SUFFIX_RULE;
}

_ui->actionLocate->setEnabled( enabled );
Expand Down Expand Up @@ -404,6 +434,8 @@ SuffixFileTypeItem::SuffixFileTypeItem( const QString & suffix,
{
if ( suffix == NO_SUFFIX )
setText( FT_NameCol, QObject::tr( "<No Extension>" ) );
else if ( suffix == NON_SUFFIX_RULE )
setText( FT_NameCol, QObject::tr( "<Other>" ) );
else
_suffix = "*." + suffix;
}
Expand Down

0 comments on commit 4f13e86

Please sign in to comment.