File indexing completed on 2024-04-28 05:45:03

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "kfileitemlisttostring.h"
0009 
0010 #include <KFileItem>
0011 #include <KFileItemListProperties>
0012 #include <KLocalizedString>
0013 
0014 #include <QFontMetrics>
0015 #include <QString>
0016 
0017 QString fileItemListToString(KFileItemList items, int maximumTextWidth, const QFontMetrics &fontMetrics, ItemsState itemsState)
0018 {
0019     QString text;
0020     switch (items.count()) {
0021     case 1:
0022         text = i18nc("Textual representation of a file. %1 is the name of the file/folder.", "\"%1\"", items.first().name());
0023         break;
0024     case 2:
0025         text =
0026             i18nc("Textual representation of two files. %1 and %2 are names of files/folders.", "\"%1\" and \"%2\"", items.first().name(), items.last().name());
0027         break;
0028     case 3:
0029         text = i18nc("Textual representation of three files. %1, %2 and %3 are names of files/folders.",
0030                      "\"%1\", \"%2\" and \"%3\"",
0031                      items.first().name(),
0032                      items.at(1).name(),
0033                      items.last().name());
0034         break;
0035     case 4:
0036         text = i18nc("Textual representation of four files. %1, %2, %3 and %4 are names of files/folders.",
0037                      "\"%1\", \"%2\", \"%3\" and \"%4\"",
0038                      items.first().name(),
0039                      items.at(1).name(),
0040                      items.at(2).name(),
0041                      items.last().name());
0042         break;
0043     case 5:
0044         text = i18nc("Textual representation of five files. %1, %2, %3, %4 and %5 are names of files/folders.",
0045                      "\"%1\", \"%2\", \"%3\", \"%4\" and \"%5\"",
0046                      items.first().name(),
0047                      items.at(1).name(),
0048                      items.at(2).name(),
0049                      items.at(3).name(),
0050                      items.last().name());
0051         break;
0052     default:
0053         text = QString();
0054         break;
0055     }
0056 
0057     // At some point the added clarity from the text starts being less important than the text width.
0058     if (!text.isEmpty() && fontMetrics.horizontalAdvance(text) <= maximumTextWidth) {
0059         return text;
0060     }
0061 
0062     const KFileItemListProperties properties(items);
0063     if (itemsState == Selected) {
0064         if (properties.isFile()) {
0065             text = i18ncp("Textual representation of selected files. %1 is the number of files.", "One Selected File", "%1 Selected Files", items.count());
0066         } else if (properties.isDirectory()) {
0067             text =
0068                 i18ncp("Textual representation of selected folders. %1 is the number of folders.", "One Selected Folder", "%1 Selected Folders", items.count());
0069         } else {
0070             text = i18ncp("Textual representation of selected fileitems. %1 is the number of files/folders.",
0071                           "One Selected Item",
0072                           "%1 Selected Items",
0073                           items.count());
0074         }
0075 
0076         if (fontMetrics.horizontalAdvance(text) <= maximumTextWidth) {
0077             return text;
0078         }
0079     }
0080 
0081     if (properties.isFile()) {
0082         return i18ncp("Textual representation of files. %1 is the number of files.", "One File", "%1 Files", items.count());
0083     } else if (properties.isDirectory()) {
0084         return i18ncp("Textual representation of folders. %1 is the number of folders.", "One Folder", "%1 Folders", items.count());
0085     } else {
0086         return i18ncp("Textual representation of fileitems. %1 is the number of files/folders.", "One Item", "%1 Items", items.count());
0087     }
0088 }