File indexing completed on 2025-01-26 05:06:21
0001 /* 0002 SPDX-FileCopyrightText: 2008, 2009 Fredrik Höglund <fredrik@kde.org> 0003 SPDX-FileCopyrightText: 2013-2014 Eike Hein <hein@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "labelgenerator.h" 0009 0010 #include <KFilePlacesModel> 0011 #include <KShell> 0012 0013 #include "foldermodel.h" 0014 0015 int LabelGenerator::s_instanceCount = 0; 0016 KFilePlacesModel *LabelGenerator::s_placesModel = nullptr; 0017 0018 LabelGenerator::LabelGenerator(QObject *parent) 0019 : QObject(parent) 0020 , m_rtl(false) 0021 , m_labelMode(1) 0022 { 0023 ++s_instanceCount; 0024 } 0025 0026 LabelGenerator::~LabelGenerator() 0027 { 0028 --s_instanceCount; 0029 if (!s_instanceCount) { 0030 delete s_placesModel; 0031 s_placesModel = nullptr; 0032 } 0033 } 0034 0035 FolderModel *LabelGenerator::folderModel() const 0036 { 0037 return m_folderModel.data(); 0038 } 0039 0040 void LabelGenerator::setFolderModel(FolderModel *folderModel) 0041 { 0042 if (m_folderModel.data() != folderModel) { 0043 if (m_folderModel.data()) { 0044 disconnect(m_folderModel.data(), nullptr, this, nullptr); 0045 } 0046 0047 m_folderModel = folderModel; 0048 0049 connect(m_folderModel.data(), &FolderModel::listingCompleted, this, &LabelGenerator::updateDisplayLabel); 0050 connect(m_folderModel.data(), &FolderModel::listingCanceled, this, &LabelGenerator::updateDisplayLabel); 0051 0052 Q_EMIT folderModelChanged(); 0053 updateDisplayLabel(); 0054 } 0055 } 0056 0057 bool LabelGenerator::rtl() const 0058 { 0059 return m_rtl; 0060 } 0061 0062 void LabelGenerator::setRtl(bool rtl) 0063 { 0064 if (rtl != m_rtl) { 0065 m_rtl = rtl; 0066 Q_EMIT rtlChanged(); 0067 updateDisplayLabel(); 0068 } 0069 } 0070 0071 int LabelGenerator::labelMode() const 0072 { 0073 return m_labelMode; 0074 } 0075 0076 void LabelGenerator::setLabelMode(int mode) 0077 { 0078 if (mode != m_labelMode) { 0079 m_labelMode = mode; 0080 Q_EMIT labelModeChanged(); 0081 updateDisplayLabel(); 0082 } 0083 } 0084 0085 QString LabelGenerator::labelText() const 0086 { 0087 return m_labelText; 0088 } 0089 0090 void LabelGenerator::setLabelText(const QString &text) 0091 { 0092 if (text != m_labelText) { 0093 m_labelText = text; 0094 Q_EMIT labelTextChanged(); 0095 updateDisplayLabel(); 0096 } 0097 } 0098 0099 QString LabelGenerator::displayLabel() const 0100 { 0101 return m_displayLabel; 0102 } 0103 0104 void LabelGenerator::updateDisplayLabel() 0105 { 0106 const QString displayLabel = generatedDisplayLabel(); 0107 if (m_displayLabel != displayLabel) { 0108 m_displayLabel = displayLabel; 0109 Q_EMIT displayLabelChanged(); 0110 } 0111 } 0112 0113 QString LabelGenerator::generatedDisplayLabel() 0114 { 0115 if (!m_folderModel) { 0116 return QString(); 0117 } 0118 0119 QUrl url = m_folderModel->resolvedUrl(); 0120 0121 if (m_labelMode == 1 /* Default */) { 0122 if (url.path().length() <= 1) { 0123 const KFileItem &rootItem = m_folderModel->rootItem(); 0124 0125 if (rootItem.text() != QLatin1String(".")) { 0126 return rootItem.text(); 0127 } 0128 } 0129 0130 QString label(url.toDisplayString(QUrl::PreferLocalFile | QUrl::StripTrailingSlash)); 0131 0132 if (!s_placesModel) { 0133 s_placesModel = new KFilePlacesModel(); 0134 } 0135 0136 connectPlacesModel(); 0137 0138 const QModelIndex index = s_placesModel->closestItem(url); 0139 0140 if (index.isValid()) { 0141 QString root = s_placesModel->url(index).toDisplayString(QUrl::PreferLocalFile | QUrl::StripTrailingSlash); 0142 0143 label = label.right(label.length() - root.length()); 0144 0145 if (!label.isEmpty()) { 0146 if (label.at(0) == QLatin1Char('/')) { 0147 label.remove(0, 1); 0148 } 0149 0150 if (m_rtl) { 0151 label.prepend(QLatin1String(" < ")); 0152 } else { 0153 label.prepend(QLatin1String(" > ")); 0154 } 0155 } 0156 0157 label.prepend(s_placesModel->text(index)); 0158 } 0159 0160 return label; 0161 } else if (m_labelMode == 2 /* Full path */) { 0162 return QUrl(url).toDisplayString(QUrl::PreferLocalFile | QUrl::StripTrailingSlash); 0163 } else if (m_labelMode == 3 /* Custom title */) { 0164 return m_labelText; 0165 } 0166 0167 return QString(); 0168 } 0169 0170 void LabelGenerator::connectPlacesModel() 0171 { 0172 connect(s_placesModel, &KFilePlacesModel::rowsInserted, this, &LabelGenerator::updateDisplayLabel, Qt::UniqueConnection); 0173 connect(s_placesModel, &KFilePlacesModel::rowsRemoved, this, &LabelGenerator::updateDisplayLabel, Qt::UniqueConnection); 0174 connect(s_placesModel, &KFilePlacesModel::dataChanged, this, &LabelGenerator::updateDisplayLabel, Qt::UniqueConnection); 0175 }