File indexing completed on 2024-04-28 05:47:44

0001 /*
0002     SPDX-FileCopyrightText: 2007 Henrique Pinto <henrique.pinto@kdemail.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "infopanel.h"
0008 #include "archiveentry.h"
0009 
0010 #include <KIO/Global>
0011 #include <KLocalizedString>
0012 
0013 #include <QFileInfo>
0014 #include <QIcon>
0015 #include <QMimeDatabase>
0016 
0017 using namespace Kerfuffle;
0018 
0019 QPixmap InfoPanel::getPixmap(const QString &name)
0020 {
0021     return QIcon::fromTheme(name).pixmap(48);
0022 }
0023 
0024 InfoPanel::InfoPanel(ArchiveModel *model, QWidget *parent)
0025     : QFrame(parent)
0026     , m_model(model)
0027 {
0028     setupUi(this);
0029 
0030     // Make the file name font bigger than the rest
0031     QFont fnt = fileName->font();
0032     if (fnt.pointSize() > -1) {
0033         fnt.setPointSize(fnt.pointSize() + 1);
0034     } else {
0035         fnt.setPixelSize(fnt.pixelSize() + 3);
0036     }
0037     fileName->setFont(fnt);
0038 
0039     updateWithDefaults();
0040 }
0041 
0042 InfoPanel::~InfoPanel()
0043 {
0044 }
0045 
0046 void InfoPanel::updateWithDefaults()
0047 {
0048     iconLabel->setPixmap(getPixmap(QStringLiteral("utilities-file-archiver")));
0049 
0050     const QString currentFileName = prettyFileName();
0051 
0052     if (currentFileName.isEmpty()) {
0053         fileName->setText(i18n("No archive loaded"));
0054     } else {
0055         fileName->setText(currentFileName);
0056     }
0057 
0058     additionalInfo->setText(QString());
0059     hideMetaData();
0060 }
0061 
0062 QString InfoPanel::prettyFileName() const
0063 {
0064     if (m_prettyFileName.isEmpty()) {
0065         if (m_model->archive()) {
0066             QFileInfo fileInfo(m_model->archive()->fileName());
0067             return fileInfo.fileName();
0068         }
0069     }
0070 
0071     return m_prettyFileName;
0072 }
0073 
0074 void InfoPanel::setPrettyFileName(const QString &fileName)
0075 {
0076     m_prettyFileName = fileName;
0077 }
0078 
0079 void InfoPanel::setIndex(const QModelIndex &index)
0080 {
0081     if (!index.isValid()) {
0082         updateWithDefaults();
0083     } else {
0084         const Archive::Entry *entry = m_model->entryForIndex(index);
0085         if (!entry) {
0086             return;
0087         }
0088 
0089         QMimeDatabase db;
0090         QMimeType mimeType;
0091         if (entry->isDir()) {
0092             mimeType = db.mimeTypeForName(QStringLiteral("inode/directory"));
0093         } else {
0094             mimeType = db.mimeTypeForFile(entry->displayName(), QMimeDatabase::MatchExtension);
0095         }
0096 
0097         iconLabel->setPixmap(getPixmap(mimeType.iconName()));
0098         if (entry->isDir()) {
0099             uint dirs;
0100             uint files;
0101             entry->countChildren(dirs, files);
0102             additionalInfo->setText(KIO::itemsSummaryString(dirs + files, files, dirs, entry->property("size").toULongLong(), true));
0103         } else if (!entry->property("link").toString().isEmpty()) {
0104             additionalInfo->setText(i18n("Symbolic Link"));
0105         } else {
0106             if (entry->property("size") != 0) {
0107                 additionalInfo->setText(KIO::convertSize(entry->property("size").toULongLong()));
0108             } else {
0109                 additionalInfo->setText(i18n("Unknown size"));
0110             }
0111         }
0112 
0113         fileName->setText(entry->displayName());
0114         showMetaDataFor(index);
0115     }
0116 }
0117 
0118 void InfoPanel::setIndexes(const QModelIndexList &list)
0119 {
0120     if (list.size() == 0) {
0121         setIndex(QModelIndex());
0122     } else if (list.size() == 1) {
0123         setIndex(list[0]);
0124     } else {
0125         iconLabel->setPixmap(getPixmap(QStringLiteral("utilities-file-archiver")));
0126         fileName->setText(i18np("One file selected", "%1 files selected", list.size()));
0127         quint64 totalSize = 0;
0128         for (const QModelIndex &index : list) {
0129             const Archive::Entry *entry = m_model->entryForIndex(index);
0130             totalSize += entry->property("size").toULongLong();
0131         }
0132         additionalInfo->setText(KIO::convertSize(totalSize));
0133         hideMetaData();
0134     }
0135 }
0136 
0137 void InfoPanel::showMetaData()
0138 {
0139     m_separator->show();
0140     m_metaDataWidget->show();
0141 }
0142 
0143 void InfoPanel::hideMetaData()
0144 {
0145     m_separator->hide();
0146     m_metaDataWidget->hide();
0147 }
0148 
0149 void InfoPanel::showMetaDataFor(const QModelIndex &index)
0150 {
0151     showMetaData();
0152 
0153     const Archive::Entry *entry = m_model->entryForIndex(index);
0154 
0155     QMimeDatabase db;
0156     QMimeType mimeType;
0157 
0158     if (entry->isDir()) {
0159         mimeType = db.mimeTypeForName(QStringLiteral("inode/directory"));
0160     } else {
0161         mimeType = db.mimeTypeForFile(entry->displayName(), QMimeDatabase::MatchExtension);
0162     }
0163 
0164     if (entry->isExecutable() && mimeType.isDefault()) {
0165         m_typeValueLabel->setText(db.mimeTypeForName(QStringLiteral("application/x-executable")).comment());
0166     } else {
0167         m_typeValueLabel->setText(mimeType.comment());
0168     }
0169 
0170     if (!entry->property("owner").toString().isEmpty()) {
0171         m_ownerLabel->show();
0172         m_ownerValueLabel->show();
0173         m_ownerValueLabel->setText(entry->property("owner").toString());
0174     } else {
0175         m_ownerLabel->hide();
0176         m_ownerValueLabel->hide();
0177     }
0178 
0179     if (!entry->property("group").toString().isEmpty()) {
0180         m_groupLabel->show();
0181         m_groupValueLabel->show();
0182         m_groupValueLabel->setText(entry->property("group").toString());
0183     } else {
0184         m_groupLabel->hide();
0185         m_groupValueLabel->hide();
0186     }
0187 
0188     if (!entry->property("link").toString().isEmpty()) {
0189         m_targetLabel->show();
0190         m_targetValueLabel->show();
0191         m_targetValueLabel->setText(entry->property("link").toString());
0192     } else {
0193         m_targetLabel->hide();
0194         m_targetValueLabel->hide();
0195     }
0196 
0197     if (entry->property("isPasswordProtected").toBool()) {
0198         m_passwordLabel->show();
0199         m_passwordValueLabel->show();
0200     } else {
0201         m_passwordLabel->hide();
0202         m_passwordValueLabel->hide();
0203     }
0204 }
0205 
0206 #include "moc_infopanel.cpp"