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

0001 /*
0002     SPDX-FileCopyrightText: 2007 Henrique Pinto <henrique.pinto@kdemail.net>
0003     SPDX-FileCopyrightText: 2008-2009 Harald Hvaal <haraldhv@stud.ntnu.no>
0004     SPDX-FileCopyrightText: 2009 Raphael Kubo da Costa <rakuco@FreeBSD.org>
0005     SPDX-FileCopyrightText: 2016 Vladyslav Batyrenko <mvlabat@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #ifndef PART_H
0010 #define PART_H
0011 
0012 #include "archiveentry.h"
0013 #include "interface.h"
0014 
0015 #include <KMessageWidget>
0016 #include <KParts/Part>
0017 #include <KParts/ReadWritePart>
0018 #include <KParts/StatusBarExtension>
0019 
0020 #include <QModelIndex>
0021 #include <QTimer>
0022 
0023 class ArchiveModel;
0024 class ArchiveSortFilterModel;
0025 class ArchiveView;
0026 class InfoPanel;
0027 
0028 class KAbstractWidgetJobTracker;
0029 class KJob;
0030 class KToggleAction;
0031 
0032 class QAction;
0033 class QLineEdit;
0034 class QSplitter;
0035 class QTemporaryDir;
0036 class QVBoxLayout;
0037 class QFileSystemWatcher;
0038 class QGroupBox;
0039 class QPlainTextEdit;
0040 class QPushButton;
0041 
0042 namespace Ark
0043 {
0044 class Part : public KParts::ReadWritePart, public Interface
0045 {
0046     Q_OBJECT
0047     Q_INTERFACES(Interface)
0048 public:
0049     enum OpenFileMode { Preview, OpenFile, OpenFileWith };
0050 
0051     enum OverwriteBehaviour { ShowOverwriteDialog, DoNotShowOverwriteDialog };
0052 
0053     Part(QWidget *parentWidget, QObject *parent, const KPluginMetaData &metaData, const QVariantList &);
0054     ~Part() override;
0055 
0056     bool openFile() override;
0057     bool saveFile() override;
0058 
0059     bool isBusy() const override;
0060     KConfigSkeleton *config() const override;
0061     QList<Kerfuffle::SettingsPage *> settingsPages(QWidget *parent) const override;
0062     QWidget *infoPanel() const override;
0063 
0064     bool eventFilter(QObject *target, QEvent *event) override;
0065 
0066     /**
0067      * Return custom compnentName for KXMLGUIClient, as by history not the plugin id is used
0068      */
0069     QString componentName() const override;
0070 
0071     /**
0072      * Validate the localFilePath() associated to this part.
0073      * If the file is not valid, an error message is displayed to the user.
0074      * @return Whether the localFilePath() can be loaded by the part.
0075      */
0076     bool isLocalFileValid();
0077 
0078     /**
0079      * Ask the user whether to overwrite @p targetFile, when creating a new archive with the same path.
0080      * @return True if the file has been successfully removed upon user's will. False otherwise.
0081      */
0082     bool confirmAndDelete(const QString &targetFile);
0083 
0084 public Q_SLOTS:
0085     // Extracts selected files to @p localPath when drag and drop'ing from Ark to e.g. Dolphin
0086     void extractSelectedFilesTo(const QString &localPath);
0087 
0088 protected:
0089     void guiActivateEvent(KParts::GUIActivateEvent *event) override;
0090 
0091 private Q_SLOTS:
0092     void slotCompleted();
0093     void slotLoadingStarted();
0094     void slotLoadingFinished(KJob *job);
0095     void slotOpenExtractedEntry(KJob *);
0096     void slotPreviewExtractedEntry(KJob *job);
0097     void slotOpenEntry(int mode);
0098     void slotError(const QString &errorMessage, const QString &details);
0099     void slotExtractArchive();
0100     void slotShowExtractionDialog();
0101     void slotExtractionDone(KJob *);
0102     void slotQuickExtractFiles(QAction *);
0103 
0104     /**
0105      * Creates and starts AddJob.
0106      *
0107      * @param files Files to add.
0108      * @param destination Destination path within the archive to which entries have to be added. Is used on addto action
0109      * or drag'n'drop event, for adding a watched file it has empty.
0110      * @param relPath Relative path of watched entry inside the archive. Is used only for adding temporarily extracted
0111      * watched file.
0112      * @param onOverwrite Whether to show a confirmation dialog when files will be overwritten
0113      */
0114     void slotAddFiles(const QStringList &files,
0115                       const Kerfuffle::Archive::Entry *destination,
0116                       const QString &relPath,
0117                       Ark::Part::OverwriteBehaviour onOverwrite = ShowOverwriteDialog);
0118     void slotDroppedFiles(const QStringList &files, const Kerfuffle::Archive::Entry *destination);
0119 
0120     /**
0121      * Creates and starts MoveJob or CopyJob.
0122      *
0123      * @param files Files to paste.
0124      * @param destination Destination path within the archive to which entries have to be added. For renaming an entry
0125      * the path has to contain a new filename too.
0126      * @param entriesWithoutChildren Entries count, excluding their children. For CopyJob 0 MUST be passed.
0127      */
0128     void slotPasteFiles(QVector<Kerfuffle::Archive::Entry *> &files, Kerfuffle::Archive::Entry *destination, int entriesWithoutChildren);
0129 
0130     void slotAddFiles();
0131     void slotCutFiles();
0132     void slotCopyFiles();
0133     void slotRenameFile(const QString &name);
0134     void slotPasteFiles();
0135     void slotAddFilesDone(KJob *);
0136     void slotPasteFilesDone(KJob *);
0137     void slotTestingDone(KJob *);
0138     void slotDeleteFiles();
0139     void slotDeleteFilesDone(KJob *);
0140     void slotShowProperties();
0141     void slotShowContextMenu();
0142     void slotActivated(const QModelIndex &index);
0143     void slotToggleInfoPanel(bool);
0144     void slotSaveAs();
0145     void updateActions();
0146     void updateQuickExtractMenu(QAction *extractAction);
0147     void selectionChanged();
0148     void setBusyGui();
0149     void setReadyGui();
0150     void setFileNameFromArchive();
0151     void slotResetFileChangeTimer(const QString &file);
0152     void slotWatchedFileModified(const QString &file);
0153     void slotShowComment();
0154     void slotAddComment();
0155     void slotCommentChanged();
0156     void slotTestArchive();
0157     void slotShowFind();
0158     void displayMsgWidget(KMessageWidget::MessageType type, const QString &msg);
0159     void searchEdited(const QString &text);
0160 
0161 Q_SIGNALS:
0162     void busy();
0163     void ready();
0164     void quit();
0165 
0166 private:
0167     /**
0168      * @return true if both the current archive and the part are read-write, false otherwise.
0169      */
0170     bool isArchiveWritable() const;
0171 
0172     /**
0173      * @return Whether the part has been told to create a new archive.
0174      */
0175     bool isCreatingNewArchive() const;
0176 
0177     void createArchive();
0178     void loadArchive();
0179     void resetArchive();
0180     void resetGui();
0181     void setupView();
0182     void setupActions();
0183     QString detectSubfolder() const;
0184     QVector<Kerfuffle::Archive::Entry *> filesForIndexes(const QModelIndexList &list) const;
0185     QVector<Kerfuffle::Archive::Entry *> filesAndRootNodesForIndexes(const QModelIndexList &list) const;
0186     QModelIndexList addChildren(const QModelIndexList &list) const;
0187     void registerJob(KJob *job);
0188     QModelIndexList getSelectedIndexes();
0189     void readCompressionOptions();
0190 
0191     ArchiveModel *m_model;
0192     ArchiveView *m_view;
0193     QAction *m_previewAction;
0194     QAction *m_openFileAction;
0195     QAction *m_openFileWithAction;
0196     QAction *m_extractArchiveAction;
0197     QAction *m_extractAction;
0198     QAction *m_addFilesAction;
0199     QAction *m_renameFileAction;
0200     QAction *m_deleteFilesAction;
0201     QAction *m_cutFilesAction;
0202     QAction *m_copyFilesAction;
0203     QAction *m_pasteFilesAction;
0204     QAction *m_saveAsAction;
0205     QAction *m_propertiesAction;
0206     QAction *m_editCommentAction;
0207     QAction *m_testArchiveAction;
0208     QAction *m_searchAction;
0209     KToggleAction *m_showInfoPanelAction;
0210     InfoPanel *m_infoPanel;
0211     QSplitter *m_splitter;
0212     QList<QTemporaryDir *> m_tmpExtractDirList;
0213     bool m_busy;
0214 
0215     OpenFileMode m_openFileMode;
0216     QUrl m_lastUsedAddPath;
0217     QVector<Kerfuffle::Archive::Entry *> m_jobTempEntries;
0218     Kerfuffle::Archive::Entry *m_destination;
0219     QModelIndexList m_cutIndexes;
0220 
0221     QTimer m_watchedFileChangeTimer;
0222     QString m_lastChangedFilename;
0223 
0224     KAbstractWidgetJobTracker *m_jobTracker;
0225     KParts::StatusBarExtension *m_statusBarExtension;
0226     QVBoxLayout *m_vlayout;
0227     std::unique_ptr<QFileSystemWatcher> m_fileWatcher;
0228     QSplitter *m_commentSplitter;
0229     QGroupBox *m_commentBox;
0230     QPlainTextEdit *m_commentView;
0231     KMessageWidget *m_commentMsgWidget;
0232     KMessageWidget *m_messageWidget;
0233     Kerfuffle::CompressionOptions m_compressionOptions;
0234     ArchiveSortFilterModel *m_filterModel;
0235     QWidget *m_searchWidget;
0236     QLineEdit *m_searchLineEdit;
0237     QPushButton *m_searchCloseButton;
0238 };
0239 
0240 } // namespace Ark
0241 
0242 #endif // PART_H