]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlisttostring.cpp
GIT_SILENT Update Appstream for new release
[dolphin.git] / src / kitemviews / kfileitemlisttostring.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2022 Felix Ernst <felixernst@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "kfileitemlisttostring.h"
9
10 #include <KFileItem>
11 #include <KFileItemListProperties>
12 #include <KLocalizedString>
13
14 #include <QFontMetrics>
15 #include <QString>
16
17 QString fileItemListToString(KFileItemList items, int maximumTextWidth, const QFontMetrics &fontMetrics, ItemsState itemsState)
18 {
19 QString text;
20 switch (items.count()) {
21 case 1:
22 text = i18nc("Textual representation of a file. %1 is the name of the file/folder.", "\"%1\"", items.first().name().replace("&", "&&"));
23 break;
24 case 2:
25 text =
26 i18nc("Textual representation of two files. %1 and %2 are names of files/folders.", "\"%1\" and \"%2\"", items.first().name().replace("&", "&&"), items.last().name().replace("&", "&&"));
27 break;
28 case 3:
29 text = i18nc("Textual representation of three files. %1, %2 and %3 are names of files/folders.",
30 "\"%1\", \"%2\" and \"%3\"",
31 items.first().name().replace("&", "&&"),
32 items.at(1).name().replace("&", "&&"),
33 items.last().name().replace("&", "&&"));
34 break;
35 case 4:
36 text = i18nc("Textual representation of four files. %1, %2, %3 and %4 are names of files/folders.",
37 "\"%1\", \"%2\", \"%3\" and \"%4\"",
38 items.first().name().replace("&", "&&"),
39 items.at(1).name().replace("&", "&&"),
40 items.at(2).name().replace("&", "&&"),
41 items.last().name().replace("&", "&&"));
42 break;
43 case 5:
44 text = i18nc("Textual representation of five files. %1, %2, %3, %4 and %5 are names of files/folders.",
45 "\"%1\", \"%2\", \"%3\", \"%4\" and \"%5\"",
46 items.first().name().replace("&", "&&"),
47 items.at(1).name().replace("&", "&&"),
48 items.at(2).name().replace("&", "&&"),
49 items.at(3).name().replace("&", "&&"),
50 items.last().name().replace("&", "&&"));
51 break;
52 default:
53 text = QString();
54 break;
55 }
56
57 // At some point the added clarity from the text starts being less important than the text width.
58 if (!text.isEmpty() && fontMetrics.horizontalAdvance(text) <= maximumTextWidth) {
59 return text;
60 }
61
62 const KFileItemListProperties properties(items);
63 if (itemsState == Selected) {
64 if (properties.isFile()) {
65 text = i18ncp("Textual representation of selected files. %1 is the number of files.", "One Selected File", "%1 Selected Files", items.count());
66 } else if (properties.isDirectory()) {
67 text =
68 i18ncp("Textual representation of selected folders. %1 is the number of folders.", "One Selected Folder", "%1 Selected Folders", items.count());
69 } else {
70 text = i18ncp("Textual representation of selected fileitems. %1 is the number of files/folders.",
71 "One Selected Item",
72 "%1 Selected Items",
73 items.count());
74 }
75
76 if (fontMetrics.horizontalAdvance(text) <= maximumTextWidth) {
77 return text;
78 }
79 }
80
81 if (properties.isFile()) {
82 return i18ncp("Textual representation of files. %1 is the number of files.", "One File", "%1 Files", items.count());
83 } else if (properties.isDirectory()) {
84 return i18ncp("Textual representation of folders. %1 is the number of folders.", "One Folder", "%1 Folders", items.count());
85 } else {
86 return i18ncp("Textual representation of fileitems. %1 is the number of files/folders.", "One Item", "%1 Items", items.count());
87 }
88 }