File indexing completed on 2022-09-27 16:33:53

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2010 Dominik Seichter <domseichter@web.de>
0003 
0004 #include "krenametokensorter.h"
0005 
0006 #include "plugin.h"
0007 #include "pluginloader.h"
0008 
0009 #include <krandom.h>
0010 
0011 // Helper functions for sorting
0012 static const QString findNumInString(unsigned int pos, const QString &s)
0013 {
0014     QString num;
0015 
0016     for (int i = static_cast<int>(pos); i >= 0; i--)
0017         if (s[i].isDigit()) {
0018             num.prepend(s[i]);
0019         } else {
0020             break;
0021         }
0022 
0023     for (int i = pos + 1; i < s.length(); i++)
0024         if (s[i].isDigit()) {
0025             num.append(s[i]);
0026         } else {
0027             break;
0028         }
0029 
0030     return num;
0031 }
0032 
0033 static int compareNummeric(const QString &s1, const QString &s2)
0034 {
0035     int z = 0;
0036     int max = (s1.length() > s2.length() ? s1.length() : s2.length());
0037 
0038     QString num1;
0039     QString num2;
0040     for (z = 0; z < max; z++) {
0041         //if( z >= s1.length() || z >= s2.length() )
0042         //    break;
0043 
0044         if ((z < s1.length() && z < s2.length() && s1[z] != s2[z])) {
0045             if (z < s1.length() && s1[z].isDigit()) {
0046                 num1 = findNumInString(z, s1);
0047             }
0048 
0049             if (z < s2.length() && s2[z].isDigit()) {
0050                 num2 = findNumInString(z, s2);
0051             }
0052 
0053             if (num1.isNull() && num2.isNull()) {
0054                 break;
0055             }
0056 
0057             int a = num1.toInt();
0058             int b = num2.toInt();
0059             if (a == b) {
0060                 return s1.compare(s2);
0061             } else {
0062                 return (a > b) ? 1 : -1;
0063             }
0064         }
0065     }
0066 
0067     return s1.compare(s2);
0068 }
0069 
0070 // Less than functions for sorting
0071 bool ascendingKRenameFileLessThan(const KRenameFile &file1, const KRenameFile &file2)
0072 {
0073     return file1.srcUrl() < file2.srcUrl();
0074 }
0075 
0076 bool descendingKRenameFileLessThan(const KRenameFile &file1, const KRenameFile &file2)
0077 {
0078     return !(file1.srcUrl() < file2.srcUrl());
0079 }
0080 
0081 bool numericKRenameFileLessThan(const KRenameFile &file1, const KRenameFile &file2)
0082 {
0083     QUrl url1 = file1.srcUrl();
0084     QUrl url2 = file2.srcUrl();
0085     QString directory1 = url1.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash).path();
0086     QString directory2 = url2.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash).path();
0087     if (directory1 != directory2) {
0088         // not in the same directory so do lexical comparison
0089         return url1 < url2;
0090     } else {
0091         return (compareNummeric(file1.srcFilename(), file2.srcFilename()) < 0);
0092     }
0093 
0094     return false;
0095 }
0096 
0097 bool randomKRenameFileLessThan(const KRenameFile &, const KRenameFile &)
0098 {
0099     return QRandomGenerator::global()->bounded(1.0) < 0.5;
0100 }
0101 
0102 KRenameTokenSorter::KRenameTokenSorter(BatchRenamer *renamer, const QString &token,
0103                                        const KRenameFile::List &list, ESimpleSortMode eSortMode)
0104     : m_renamer(renamer), m_token(token), m_list(list), m_eSortMode(eSortMode)
0105 {
0106     m_plugin = PluginLoader::Instance()->findPlugin(token);
0107 
0108     KRenameFile::List::ConstIterator it = list.begin();
0109     int index = 0;
0110     while (it != list.end()) {
0111         QString value = processString(index++);
0112         m_values.insert((*it).srcUrl(), value);
0113 
0114         ++it;
0115     }
0116 }
0117 
0118 bool KRenameTokenSorter::operator()(const KRenameFile &file1, const KRenameFile &file2)
0119 {
0120     QString str1, str2;
0121 
0122     str1 = m_values.value(file1.srcUrl());
0123     str2 = m_values.value(file2.srcUrl());
0124 
0125     if (m_eSortMode == eSimpleSortMode_Ascending) {
0126         return str1 < str2;
0127     } else if (m_eSortMode == eSimpleSortMode_Descending) {
0128         return !(str1 < str2);
0129     } else if (m_eSortMode == eSimpleSortMode_Numeric) {
0130         return compareNummeric(str1, str2) < 0;
0131     }
0132 
0133     // Default, should never be reached
0134     return (str1 < str2);
0135 }
0136 
0137 QString KRenameTokenSorter::processString(int index) const
0138 {
0139     QString ret = m_token;
0140     if (m_plugin != nullptr) {
0141         ret = m_plugin->processFile(m_renamer, index, ret, ePluginType_Token);
0142     }
0143     return ret;
0144 }
0145