File indexing completed on 2024-05-19 05:49:14

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 "fileListHelper.h"
0008 
0009 #include <QAction>
0010 #include <QFileDialog>
0011 #include <QObject>
0012 #include <QPushButton>
0013 
0014 #include <KLocalizedString>
0015 #include <KMessageBox>
0016 
0017 #include "defaults.h"
0018 
0019 #include "ksystemlog_debug.h"
0020 
0021 FileListHelper::FileListHelper(QWidget *p)
0022     : mParent(p)
0023 {
0024 }
0025 
0026 FileListHelper::~FileListHelper()
0027 {
0028 }
0029 
0030 QAction *FileListHelper::prepareButtonAndAction(QPushButton *button, const QIcon &icon)
0031 {
0032     // Initialize action
0033     auto action = new QAction(button);
0034     action->setIcon(icon);
0035     action->setText(button->text());
0036 
0037     // Initialize button
0038     button->setIcon(icon);
0039     // Allow the disabling of the matching action when disabling this button
0040     button->addAction(action);
0041 
0042     // Assert that when an action is triggered, the related button sends a clicked() event
0043     //(the button is the QObject which is connected to custom slots)
0044     connect(action, &QAction::triggered, button, &QAbstractButton::click);
0045 
0046     return action;
0047 }
0048 
0049 QAction *FileListHelper::prepareButtonAndAction(QPushButton *button, const QIcon &icon, const QObject *receiver, const char *member)
0050 {
0051     QAction *action = prepareButtonAndAction(button, icon);
0052     connect(button, SIGNAL(clicked(bool)), receiver, member);
0053 
0054     return action;
0055 }
0056 
0057 void FileListHelper::prepareButton(QPushButton *button, const QIcon &icon, const QObject *receiver, const char *member, QWidget *fileList)
0058 {
0059     // Initialize action
0060     QAction *action = prepareButtonAndAction(button, icon, receiver, member);
0061     fileList->addAction(action);
0062 }
0063 
0064 QStringList FileListHelper::findPaths(const QList<QUrl> &urls)
0065 {
0066     QStringList paths;
0067 
0068     for (QList<QUrl>::ConstIterator it = urls.constBegin(), total = urls.constEnd(); it != total; ++it) {
0069         QUrl const url(*it);
0070 
0071         if (isValidFile(url)) {
0072             // If this Url uses a joker (i.e. : "/var/log/apache2/*")
0073             if (url.fileName().contains(QLatin1Char('*'))) {
0074                 const QStringList foundPaths = expandJoker(url);
0075                 qCDebug(KSYSTEMLOG) << "Found paths of " << url.path() << ":" << foundPaths;
0076                 for (const QString &foundPath : foundPaths) {
0077                     paths.append(foundPath);
0078                 }
0079             } else {
0080                 paths.append(url.toLocalFile());
0081             }
0082         }
0083     }
0084 
0085     return paths;
0086 }
0087 
0088 bool FileListHelper::isValidFile(const QUrl &url)
0089 {
0090     QString message;
0091 
0092     // If it is not valid
0093     if (!url.isValid()) {
0094         return false;
0095     }
0096 
0097     // If it is not a local file
0098     if (!url.isLocalFile()) {
0099         message = i18n("'%1' is not a local file.", url.path());
0100         KMessageBox::error(mParent, message, i18n("File selection failed"), KMessageBox::Notify);
0101         return false;
0102     }
0103 
0104     // If it's a directory, it's not valid
0105     if (QDir(url.toLocalFile()).exists()) {
0106         return false;
0107     }
0108 
0109     return true;
0110 }
0111 
0112 QList<QUrl> FileListHelper::openUrls() const
0113 {
0114     QFileDialog fileDialog(mParent, QString(), QStringLiteral(DEFAULT_LOG_FOLDER), i18n("All Files (*)") + QLatin1String(";;") + i18n("Log Files (*.log)"));
0115     fileDialog.setWindowTitle(i18n("Choose Log File"));
0116     fileDialog.setFileMode(QFileDialog::ExistingFiles);
0117 
0118     fileDialog.exec();
0119     return fileDialog.selectedUrls();
0120 }
0121 
0122 QUrl FileListHelper::openUrl(const QString &originPath) const
0123 {
0124     QFileDialog fileDialog(mParent, QString(), originPath, i18n("All Files (*)") + QLatin1String(";;") + i18n("Log Files (*.log)"));
0125     fileDialog.setWindowTitle(i18n("Choose Log File"));
0126     fileDialog.setFileMode(QFileDialog::AnyFile);
0127 
0128     fileDialog.exec();
0129     const QList<QUrl> urls = fileDialog.selectedUrls();
0130     if (!urls.isEmpty()) {
0131         return fileDialog.selectedUrls().at(0);
0132     } else {
0133         return QUrl();
0134     }
0135 }
0136 
0137 QStringList FileListHelper::expandJoker(const QUrl &url)
0138 {
0139     const QFileInfo info(url.toLocalFile());
0140 
0141     qCDebug(KSYSTEMLOG) << "Dir " << info.dir().path();
0142     const QString filename = info.fileName();
0143 
0144     if (filename.isEmpty()) {
0145         return QStringList();
0146     }
0147 
0148     QStringList foundPaths;
0149     const QStringList files = info.dir().entryList(QStringList(filename), QDir::Files | QDir::NoSymLinks);
0150     foundPaths.reserve(files.count());
0151     for (const QString &file : files) {
0152         foundPaths.append(info.dir().absoluteFilePath(file));
0153     }
0154 
0155     return foundPaths;
0156 }
0157 
0158 void FileListHelper::setEnabledAction(QPushButton *button, bool enabled)
0159 {
0160     button->setEnabled(enabled);
0161 
0162     const QList<QAction *> actions = button->actions();
0163     for (QAction *action : actions) {
0164         action->setEnabled(enabled);
0165     }
0166 }
0167 
0168 #include "moc_fileListHelper.cpp"