File indexing completed on 2024-05-12 05:48:36

0001 /*
0002     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "fileList.h"
0008 
0009 #include <QListWidget>
0010 
0011 #include <QDesktopServices>
0012 #include <QPushButton>
0013 
0014 #include <QFileInfo>
0015 #include <QIcon>
0016 
0017 #include "defaults.h"
0018 
0019 #include "ksystemlog_debug.h"
0020 
0021 FileList::FileList(QWidget *parent, const QString &descriptionText)
0022     : QWidget(parent)
0023     , mFileListHelper(this)
0024 {
0025     qCDebug(KSYSTEMLOG) << "Initializing file list...";
0026 
0027     setupUi(this);
0028 
0029     mWarningBox = new KMessageWidget(this);
0030     mWarningBox->setVisible(false);
0031     mWarningBox->setMessageType(KMessageWidget::Warning);
0032     mWarningBox->setText(
0033         i18n("Some log files do not exist.\n"
0034              "If all log files are missing, this mode will be unavailable."));
0035     mWarningBox->setCloseButtonVisible(false);
0036     mWarningBox->setIcon(QIcon::fromTheme(QStringLiteral("dialog-warning")));
0037     vboxLayout->insertWidget(1, mWarningBox);
0038 
0039     description->setText(descriptionText);
0040     connect(description, &QLabel::linkActivated, this, &FileList::slotLinkClicked);
0041 
0042     mFileListHelper.prepareButton(add, QIcon::fromTheme(QStringLiteral("document-new")), this, SLOT(addItem()), fileList);
0043 
0044     mFileListHelper.prepareButton(modify, QIcon::fromTheme(QStringLiteral("document-open")), this, SLOT(modifyItem()), fileList);
0045 
0046     // Add a separator in the FileList
0047     auto separator = new QAction(this);
0048     separator->setSeparator(true);
0049     fileList->addAction(separator);
0050 
0051     mFileListHelper.prepareButton(remove, QIcon::fromTheme(QStringLiteral("list-remove")), this, SLOT(removeSelectedItem()), fileList);
0052 
0053     mFileListHelper.prepareButton(up, QIcon::fromTheme(QStringLiteral("go-up")), this, SLOT(moveUpItem()), fileList);
0054 
0055     mFileListHelper.prepareButton(down, QIcon::fromTheme(QStringLiteral("go-down")), this, SLOT(moveDownItem()), fileList);
0056 
0057     mFileListHelper.prepareButton(removeAll, QIcon::fromTheme(QStringLiteral("trash-empty")), this, SLOT(removeAllItems()), fileList);
0058 
0059     connect(fileList, &QListWidget::itemSelectionChanged, this, &FileList::updateButtons);
0060     connect(fileList, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(modifyItem(QListWidgetItem *)));
0061     connect(this, &FileList::fileListChanged, this, &FileList::updateButtons);
0062 
0063     updateButtons();
0064 
0065     qCDebug(KSYSTEMLOG) << "File list initialized";
0066 }
0067 
0068 FileList::~FileList()
0069 {
0070 }
0071 
0072 int FileList::count() const
0073 {
0074     return fileList->count();
0075 }
0076 
0077 bool FileList::isEmpty() const
0078 {
0079     return fileList->count() == 0;
0080 }
0081 
0082 void FileList::slotLinkClicked(const QString &link)
0083 {
0084     QDesktopServices::openUrl(QUrl::fromUserInput(link));
0085 }
0086 
0087 void FileList::addItem()
0088 {
0089     // Open a standard Filedialog
0090     const QList<QUrl> urls = mFileListHelper.openUrls();
0091 
0092     const QStringList paths = mFileListHelper.findPaths(urls);
0093     for (const QString &path : paths) {
0094         fileList->addItem(path);
0095     }
0096 
0097     Q_EMIT fileListChanged();
0098 }
0099 
0100 void FileList::modifyItem()
0101 {
0102     modifyItem(fileList->item(fileList->currentRow()));
0103 }
0104 
0105 void FileList::modifyItem(QListWidgetItem *item)
0106 {
0107     const QString previousPath = item->text();
0108 
0109     // Open a standard Filedialog
0110     const QUrl url = mFileListHelper.openUrl(previousPath);
0111 
0112     const QList<QUrl> urls{url};
0113     const QStringList paths = mFileListHelper.findPaths(urls);
0114 
0115     // We only take the first path
0116     if (!paths.isEmpty()) {
0117         item->setText(paths.at(0));
0118     }
0119 
0120     Q_EMIT fileListChanged();
0121 }
0122 
0123 void FileList::removeSelectedItem()
0124 {
0125     const QList<QListWidgetItem *> selectedItems = fileList->selectedItems();
0126 
0127     for (QListWidgetItem *item : selectedItems) {
0128         delete fileList->takeItem(fileList->row(item));
0129     }
0130 
0131     // fileList->setCurrentRow(fileList->count()-1);
0132 
0133     Q_EMIT fileListChanged();
0134 }
0135 
0136 void FileList::unselectAllItems()
0137 {
0138     const QList<QListWidgetItem *> selectedItems = fileList->selectedItems();
0139     for (QListWidgetItem *item : selectedItems) {
0140         item->setSelected(false);
0141     }
0142 }
0143 
0144 void FileList::moveItem(int direction)
0145 {
0146     const QList<QListWidgetItem *> selectedItems = fileList->selectedItems();
0147 
0148     QListWidgetItem *item = selectedItems.at(0);
0149 
0150     const int itemIndex = fileList->row(item);
0151 
0152     fileList->takeItem(itemIndex);
0153 
0154     unselectAllItems();
0155 
0156     fileList->insertItem(itemIndex + direction, item);
0157 
0158     fileList->setCurrentRow(fileList->row(item));
0159 
0160     Q_EMIT fileListChanged();
0161 }
0162 
0163 void FileList::moveUpItem()
0164 {
0165     moveItem(-1);
0166 }
0167 
0168 void FileList::moveDownItem()
0169 {
0170     moveItem(+1);
0171 }
0172 
0173 void FileList::removeAllItems()
0174 {
0175     fileList->clear();
0176 
0177     Q_EMIT fileListChanged();
0178 }
0179 
0180 void FileList::updateButtons()
0181 {
0182     if (fileList->count() == 0) {
0183         mFileListHelper.setEnabledAction(removeAll, false);
0184     } else {
0185         mFileListHelper.setEnabledAction(removeAll, true);
0186     }
0187 
0188     const QList<QListWidgetItem *> selectedItems = fileList->selectedItems();
0189     if (!selectedItems.isEmpty()) {
0190         mFileListHelper.setEnabledAction(remove, true);
0191         mFileListHelper.setEnabledAction(modify, true);
0192 
0193         QListWidgetItem *selection = selectedItems.at(0);
0194 
0195         // If the item is at the top of the list, it could not be upped anymore
0196         if (fileList->row(selection) == 0) {
0197             mFileListHelper.setEnabledAction(up, false);
0198         } else {
0199             mFileListHelper.setEnabledAction(up, true);
0200         }
0201 
0202         // If the item is at bottom of the list, it could not be downed anymore
0203         if (fileList->row(selection) == fileList->count() - 1) {
0204             mFileListHelper.setEnabledAction(down, false);
0205         } else {
0206             mFileListHelper.setEnabledAction(down, true);
0207         }
0208     }
0209     // If nothing is selected, disabled special buttons
0210     else {
0211         mFileListHelper.setEnabledAction(remove, false);
0212         mFileListHelper.setEnabledAction(modify, false);
0213         mFileListHelper.setEnabledAction(up, false);
0214         mFileListHelper.setEnabledAction(down, false);
0215     }
0216 }
0217 
0218 QVBoxLayout *FileList::buttonsLayout()
0219 {
0220     return vboxLayout1;
0221 }
0222 
0223 void FileList::addPaths(const QStringList &paths)
0224 {
0225     bool missingFiles = false;
0226     for (const QString &path : paths) {
0227         auto item = new QListWidgetItem(path);
0228         const QFileInfo checkFile(path);
0229         if (!checkFile.exists()) {
0230             item->setForeground(Qt::red);
0231             missingFiles = true;
0232         }
0233         fileList->addItem(item);
0234     }
0235     mWarningBox->setVisible(missingFiles);
0236 
0237     updateButtons();
0238 }
0239 
0240 QStringList FileList::paths() const
0241 {
0242     QStringList paths;
0243     paths.reserve(fileList->count());
0244     for (int i = 0; i < fileList->count(); i++) {
0245         paths.append(fileList->item(i)->text());
0246     }
0247 
0248     return paths;
0249 }
0250 
0251 #include "moc_fileList.cpp"