Bug 226941 - sorting files by date does not work correctly
Summary: sorting files by date does not work correctly
Status: RESOLVED FIXED
Alias: None
Product: amarok
Classification: Applications
Component: File Browser (show other bugs)
Version: 2.3-GIT
Platform: openSUSE Unspecified
: HI normal
Target Milestone: 2.3.1
Assignee: Amarok Developers
URL:
Keywords:
: 231384 234125 236968 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-02-15 09:10 UTC by Silver Salonen
Modified: 2010-05-18 12:00 UTC (History)
8 users (show)

See Also:
Latest Commit:
Version Fixed In: 2.3.1


Attachments
incorrect sorting by date (17.93 KB, image/png)
2010-02-15 10:13 UTC, Silver Salonen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Silver Salonen 2010-02-15 09:10:38 UTC
Version:           2.2.90 (using KDE 4.4.0)
Installed from:    openSUSE RPMs

Sorting by date in file browser does not work correctly - files are sorted by smth else.
Comment 1 Myriam Schweingruber 2010-02-15 09:30:59 UTC
Could you please specify what you did exactly? A precise description would be helpful.
Comment 2 Silver Salonen 2010-02-15 09:37:00 UTC
I went to file browser, set files to be listed as "Detailed view" and clicked on date's column. I expected the files to be sorted by date now, but they didn't.

PS. Note that it's with version 2.2.90, 2.2.2 is OK.
Comment 3 Myriam Schweingruber 2010-02-15 09:50:37 UTC
Well, it definitely works here, you maybe just didn't click twice. A file browser does take the focus from the name column on first click and sets it to the other column on second click only.
Mind you, this only sorts the file date which is only the date you added a file, and it will only display one folder. Sorting files by play date is usually done in the CollectionBrowser search field...
Comment 4 Silver Salonen 2010-02-15 10:11:29 UTC
Are you sure you are talking about file browser? I'll upload an image to illustrate what I'm talking about..
Comment 5 Silver Salonen 2010-02-15 10:13:16 UTC
Created attachment 40791 [details]
incorrect sorting by date
Comment 6 Myriam Schweingruber 2010-02-15 11:16:50 UTC
Yes, this is definitely the file browser, there are no columns in the CollectionBrowser.
Please do not reopen this bug
Comment 7 Silver Salonen 2010-02-15 11:36:27 UTC
Did you look at the image? Does it look like OK?
Comment 8 Myriam Schweingruber 2010-02-15 13:40:02 UTC
Hm, I tried in another folder now, and you are right, the sorting is indeed wrong, it sorts only by day, months and years (and times) are completely wrong.
Comment 9 Myriam Schweingruber 2010-02-15 13:40:23 UTC
Changing status.
Comment 10 Nikolaj Hald Nielsen 2010-03-04 16:58:35 UTC
The issue seems to be that the dates are sorted as strings and not as dates. On my system it works, but that is because the date format is arranged in order of importance (for instance, "2010-01-23 16:34" ) which when sorted as a string gives the correct result.

According to the specs, the QSortFilterProxyModel used _should_ know how to correctly sort dates, but apparently not...
Comment 11 Silver Salonen 2010-03-05 14:05:34 UTC
So it's Qt's fault?
Comment 12 Nikolaj Hald Nielsen 2010-03-15 10:36:35 UTC
As far as I can tell, yes
Comment 13 Myriam Schweingruber 2010-03-16 01:45:44 UTC
Nikolaj, did you report this upstream already?
Comment 14 Sven Krohlas 2010-03-24 09:29:07 UTC
*** Bug 231384 has been marked as a duplicate of this bug. ***
Comment 15 Mikko C. 2010-04-06 09:20:42 UTC
confirmed in git master.
Comment 16 Sven Krohlas 2010-04-12 08:57:49 UTC
*** Bug 234125 has been marked as a duplicate of this bug. ***
Comment 17 Mikko C. 2010-05-09 11:52:38 UTC
*** Bug 236968 has been marked as a duplicate of this bug. ***
Comment 18 Rick W. Chen 2010-05-16 17:24:29 UTC
commit 0a6d4a465b3e13edc55450c110c1de23bca379db
Author: Rick W. Chen <stuffcorpse@archlinux.us>
Date:   Mon May 17 03:19:29 2010 +1200

    Fix file browser sorting using headers
    
    BUG:226941

diff --git a/ChangeLog b/ChangeLog
index ce726c4..b80d003 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -11,6 +11,7 @@ VERSION 2.3.1
       Amarok. Batch scan users can invoke it with the --idlepriority flag.
 
   BUGFIXES:
+    * File browser: fixed sorting files by date. (BR 226941)
     * Fixed issue with file browser bookmarks not working when the file browser
       was showing "places".
     * Fixed crash when right clicking in file browser while it is showing 
diff --git a/src/browsers/filebrowser/FileBrowser.cpp b/src/browsers/filebrowser/FileBrowser.cpp
index 0a210e6..7a3b675 100644
--- a/src/browsers/filebrowser/FileBrowser.cpp
+++ b/src/browsers/filebrowser/FileBrowser.cpp
@@ -74,7 +74,7 @@ FileBrowser::FileBrowser( const char * name, QWidget *parent )
     m_mimeFilterProxyModel->setSourceModel( m_kdirModel );
     m_mimeFilterProxyModel->setSortCaseSensitivity( Qt::CaseInsensitive );
     m_mimeFilterProxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
-    m_mimeFilterProxyModel->sort( 0 );
+    m_mimeFilterProxyModel->setDynamicSortFilter( true );
 
     debug() << "home path: " <<  QDir::homePath();
 
diff --git a/src/browsers/filebrowser/MimeTypeFilterProxyModel.cpp b/src/browsers/filebrowser/MimeTypeFilterProxyModel.cpp
index 92ae889..309d7d4 100644
--- a/src/browsers/filebrowser/MimeTypeFilterProxyModel.cpp
+++ b/src/browsers/filebrowser/MimeTypeFilterProxyModel.cpp
@@ -18,7 +18,7 @@
 
 #include <KDirModel>
 #include <KFileItem>
-#include <kfile.h>
+#include <KFile>
 
 MimeTypeFilterProxyModel::MimeTypeFilterProxyModel( QStringList mimeList, QObject *parent )
     : QSortFilterProxyModel( parent )
@@ -49,24 +49,39 @@ MimeTypeFilterProxyModel::filterAcceptsRow( int source_row, const QModelIndex& s
 bool
 MimeTypeFilterProxyModel::lessThan( const QModelIndex& left, const QModelIndex& right ) const
 {
-    // get the KFileItems
-    QVariant qvarLeft = left.data( KDirModel::FileItemRole );
-    QVariant qvarRight = right.data( KDirModel::FileItemRole );
+    const QVariant qvarLeft   = left.data( KDirModel::FileItemRole );
+    const QVariant qvarRight  = right.data( KDirModel::FileItemRole );
+    const KFileItem itemLeft  = qvarLeft.value<KFileItem>();
+    const KFileItem itemRight = qvarRight.value<KFileItem>();
 
-    KFileItem itemLeft = qvarLeft.value<KFileItem>();
-    KFileItem itemRight = qvarRight.value<KFileItem>();
+    switch( left.column() )
+    {
+    case KDirModel::Name:
+        // if both items are directories or files, then compare (case insensitive) on name
+        if( itemLeft.isDir() == itemRight.isDir() )
+            return itemLeft.name( true ) < itemRight.name( true );
+        // directories come before files
+        if( itemLeft.isDir() )
+            return true;
+        break;
 
-    // if both items are directories or files, then compare (case insensitive) on name
-    if( itemLeft.isDir() == itemRight.isDir()) {
-        return itemLeft.name(true) < itemRight.name(true);
-    }
+    case KDirModel::Size:
+        return itemLeft.size() < itemRight.size();
+        break;
 
-    // directories come before files
-    if (itemLeft.isDir()) {
-        return true;
-    } else {
-        return false;
+    case KDirModel::ModifiedTime:
+        return itemLeft.time( KFileItem::ModificationTime ) < itemRight.time( KFileItem::ModificationTime );
+        break;
+
+    case KDirModel::Permissions:
+    case KDirModel::Owner:
+    case KDirModel::Group:
+    case KDirModel::Type:
+    default:
+        return QString::localeAwareCompare( itemLeft.text(), itemRight.text() ) < 0;
+        break;
     }
+    return false;
 }
Comment 19 Rick W. Chen 2010-05-16 17:31:07 UTC
According to the specs, the QSortFilterProxyModel used _should_ know how to
correctly sort dates, but apparently not...(In reply to comment #10 / Nikolaj)

It should. But from what I understand KDirModel provides all those data as QStrings, as opposed to burying their respectively registered metatypes in the QVariants themselves. So the only thing you can do is find out what column they came from and match what the types used by KDirModel before they were converted to QStrings. That's what my patch does.