File indexing completed on 2024-04-28 17:05:53

0001 /*
0002     SPDX-FileCopyrightText: 2005 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2007-2008 Csaba Karai <cskarai@freemail.hu>
0004     SPDX-FileCopyrightText: 2008 Jonas Bähr <jonas.baehr@web.de>
0005     SPDX-FileCopyrightText: 2005-2022 Krusader Krew <https://krusader.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef CHECKSUMDLG_H
0011 #define CHECKSUMDLG_H
0012 
0013 // QtCore
0014 #include <QFutureWatcher>
0015 #include <QString>
0016 #include <QTemporaryFile>
0017 // QtWidgets
0018 #include <QCheckBox>
0019 #include <QLabel>
0020 #include <QWizard>
0021 
0022 #include <KCompletion/KComboBox>
0023 #include <KCoreAddons/KProcess>
0024 
0025 class KrListWidget;
0026 class KrTreeWidget;
0027 
0028 /**
0029  * Perform checksum operations: Creation of checksums or verifying files with a checksum file.
0030  *
0031  * The dialogs are not modal. The used checksum tools only support local files which are expected to
0032  * be in one directory (specified by 'path').
0033  */
0034 class Checksum
0035 {
0036 public:
0037     static void startCreationWizard(const QString &path, const QStringList &fileNames);
0038     static void startVerifyWizard(const QString &path, const QString &checksumFile = QString());
0039 };
0040 
0041 namespace CHECKSUM_
0042 { // private namespace
0043 
0044 /** Wrapper for KProcess to handle errors and output. */
0045 class ChecksumProcess : public KProcess
0046 {
0047     Q_OBJECT
0048 public:
0049     ChecksumProcess(QObject *parent, const QString &path);
0050     ~ChecksumProcess() override;
0051 
0052     QStringList stdOutput() const
0053     {
0054         return m_outputLines;
0055     }
0056     QStringList errOutput() const
0057     {
0058         return m_errorLines;
0059     }
0060 
0061 signals:
0062     void resultReady();
0063 
0064 private slots:
0065     void slotError(QProcess::ProcessError error);
0066     void slotFinished(int, QProcess::ExitStatus exitStatus);
0067 
0068 private:
0069     QStringList m_outputLines;
0070     QStringList m_errorLines;
0071     QTemporaryFile m_tmpOutFile;
0072     QTemporaryFile m_tmpErrFile;
0073 };
0074 
0075 /** Base class for common code in creation and verify wizard. */
0076 class ChecksumWizard : public QWizard
0077 {
0078     Q_OBJECT
0079 public:
0080     explicit ChecksumWizard(const QString &path);
0081     ~ChecksumWizard() override;
0082 
0083 private slots:
0084     void slotCurrentIdChanged(int id);
0085 
0086 protected:
0087     virtual QWizardPage *createIntroPage() = 0;
0088     virtual QWizardPage *createResultPage() = 0;
0089 
0090     virtual void onIntroPage() = 0;
0091     virtual void onProgressPage() = 0;
0092     virtual void onResultPage() = 0;
0093 
0094     QWizardPage *createProgressPage(const QString &title);
0095 
0096     bool checkExists(const QString &type);
0097     void runProcess(const QString &type, const QStringList &args);
0098     void addChecksumLine(KrTreeWidget *tree, const QString &line);
0099 
0100     const QString m_path;
0101     ChecksumProcess *m_process;
0102 
0103     QMap<QString, QString> m_checksumTools; // extension/typ-name -> binary name
0104 
0105     int m_introId, m_progressId, m_resultId;
0106 };
0107 
0108 class CreateWizard : public ChecksumWizard
0109 {
0110     Q_OBJECT
0111 public:
0112     CreateWizard(const QString &path, const QStringList &_files);
0113 
0114 public slots:
0115     void accept() override;
0116 
0117 private:
0118     QWizardPage *createIntroPage() override;
0119     QWizardPage *createResultPage() override;
0120 
0121     void onIntroPage() override;
0122     void onProgressPage() override;
0123     void onResultPage() override;
0124 
0125     void createChecksums();
0126     bool savePerFile();
0127     bool saveChecksumFile(const QStringList &data, const QString &filename = QString());
0128 
0129     const QStringList m_fileNames;
0130 
0131     QFutureWatcher<QStringList> m_listFilesWatcher;
0132 
0133     QString m_suggestedFilePath;
0134 
0135     // intro page
0136     KComboBox *m_methodBox;
0137     // result page
0138     KrTreeWidget *m_hashesTreeWidget;
0139     QLabel *m_errorLabel;
0140     KrListWidget *m_errorListWidget;
0141     QCheckBox *m_onePerFileBox;
0142 };
0143 
0144 class VerifyWizard : public ChecksumWizard
0145 {
0146     Q_OBJECT
0147 public:
0148     VerifyWizard(const QString &path, const QString &inputFile);
0149 
0150 private slots:
0151     void slotChecksumPathChanged(const QString &path);
0152 
0153 private:
0154     QWizardPage *createIntroPage() override;
0155     QWizardPage *createResultPage() override;
0156 
0157     void onIntroPage() override;
0158     void onProgressPage() override;
0159     void onResultPage() override;
0160 
0161     bool isSupported(const QString &path);
0162 
0163     QString m_checksumFile;
0164 
0165     // intro page
0166     KrTreeWidget *m_hashesTreeWidget;
0167     // result page
0168     QLabel *m_outputLabel;
0169     KrListWidget *m_outputListWidget;
0170 };
0171 
0172 } // NAMESPACE CHECKSUM_
0173 
0174 #endif // CHECKSUMDLG_H