File indexing completed on 2022-10-04 17:30:16

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2003 Dominik Seichter <domseichter@web.de>
0003 
0004 #ifndef KRENAMEIMPL_H
0005 #define KRENAMEIMPL_H
0006 
0007 #include <QObject>
0008 
0009 #include <QUrl>
0010 
0011 #include <KSharedConfig>
0012 
0013 #include "batchrenamer.h"
0014 #include "krenamefile.h"
0015 
0016 class KRenameModel;
0017 class KRenamePreviewModel;
0018 class KRenameWindow;
0019 class ThreadedLister;
0020 class PluginLoader;
0021 class QLineEdit;
0022 class QCommandLineParser;
0023 
0024 class KRenameImpl : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     ~KRenameImpl() override;
0030 
0031     static QWidget *launch(const QRect &rect, const KRenameFile::List &list, QCommandLineParser *commandLine = nullptr);
0032 
0033 public Q_SLOTS:
0034     /** Updates the preview of the filenames after renaming
0035      */
0036     void slotUpdatePreview();
0037 
0038 private:
0039     KRenameImpl(KRenameWindow *window, const KRenameFile::List &list , QCommandLineParser *commandLine = nullptr);
0040 
0041     /** Connect all gui components with their slots
0042      */
0043     void setupSlots();
0044 
0045     /** Adds a single url to the list of files which will
0046      *  be renamed.
0047      *
0048      *  If you have a QList<QUrl> use addFilesOrDirs which is
0049      *  faster.
0050      *
0051      *  \param url must be an existing file or directory.
0052      *
0053      *  \see addFilesOrDirs
0054      */
0055     void addFileOrDir(const QUrl &url);
0056 
0057     /** Adds a list of urls to the list of files which will
0058      *  be renamed.
0059      *
0060      *  Adding a whole list is much faster than adding single
0061      *  files.
0062      *
0063      *  \param list of existing files or directories
0064      *  \param filter only add files or directories matching this filter
0065      *  \param recursively if true all directories will be added recursively
0066      *  \param dirsWithFiles add directory names along with their contents
0067      *  \param dirsOnly add only directories and no files
0068      *  \param hidden add also hidden files and directories
0069      *
0070      *  \see addFileOrDir
0071      */
0072     void addFilesOrDirs(const QList<QUrl> &list, const QString &filter = "*",
0073                         bool recursively = false, bool dirsWithFiles = false,
0074                         bool dirsOnly = false, bool hidden = false);
0075 
0076     /** Parses commandline options
0077      */
0078     void parseCmdLineOptions(QCommandLineParser *parser);
0079 
0080     /** Load configuration
0081      */
0082     void loadConfig();
0083 
0084 private Q_SLOTS:
0085     /** Save configuration
0086      */
0087     void saveConfig();
0088 
0089     /** Start KRename's selftest:
0090      *  I.e several unit tests.
0091      */
0092     void selfTest();
0093 
0094     /** Called when the user clicks the "Add..." button.
0095      *  open a dialog to select files for adding to KRename.
0096      */
0097     void slotAddFiles();
0098 
0099     /** Called when the user clicks the "Remove" buttons.
0100      *  Remove selected files from the list.
0101      */
0102     void slotRemoveFiles();
0103 
0104     /** Called when the user clicks the "Remove All..." buttons.
0105      *  Remove all files from the list.
0106      */
0107     void slotRemoveAllFiles();
0108 
0109     /** Updates the count of files
0110      *  in the file list of KRename
0111      */
0112     void slotUpdateCount();
0113 
0114     /** This slot shows a dialog with advanced numbering settings
0115      *  - start index
0116      *  - stepping
0117      *  - reset counter on each new directory
0118      *  - skip numbers list
0119      *
0120      */
0121     void slotAdvancedNumberingDlg();
0122 
0123     /** This slots shows a dialog which allows the user
0124      *  to confortably insert a part of an oldfilename into the
0125      *  new filename (part is to be understood as "substring")
0126      */
0127     void slotInsertPartFilenameDlg();
0128 
0129     /** This slot shows a dialog which allows the user to configure find and replace
0130      */
0131     void slotFindReplaceDlg();
0132 
0133     /** Start the actual renaming
0134      *  with the current settings
0135      */
0136     void slotStart();
0137 
0138     /** Clean's up and deletes the lister after he has done
0139      *  all his work.
0140      *
0141      *  \param lister a ThreadedLister which has done his work
0142      */
0143     void slotListerDone(ThreadedLister *lister);
0144 
0145     /** Show a TokenHelpDialog that works on a QLineEdit
0146      *
0147      *  \param edit insert user selected tokens into this linedit
0148      */
0149     void slotTokenHelpDialog(QLineEdit *edit);
0150 
0151     /** Called when the user selects another extension split mode
0152      *
0153      *  @param splitMode the mode which is used to split filename and extension
0154      *  @param dot the n-th dot to use for splitting if splitMode = eSplitMode_CustomDot
0155      */
0156     void slotExtensionSplitModeChanged(ESplitMode splitMode, int dot);
0157 
0158 private:
0159     KRenameWindow        *m_window;
0160     KRenameModel         *m_model;
0161     KRenamePreviewModel  *m_previewModel;
0162 
0163     KRenameFile::List     m_vector;
0164 
0165     BatchRenamer          m_renamer;
0166 
0167     ESplitMode            m_lastSplitMode; ///< The last used split mode
0168     int                   m_lastDot;       ///< The last used dot value for splitmode
0169 
0170     PluginLoader         *m_pluginLoader;  ///< Global plugin loader instance
0171 
0172     int                   m_runningThreadedListersCount; ///< Count of running threaded listers
0173 };
0174 
0175 #if 0
0176 // Own includes
0177 /*
0178 #include "batchrenamer.h"
0179 #include "krenamedcop.h"
0180 */
0181 class HelpDialog;
0182 class HelpDialogData;
0183 class KAction;
0184 class KComboBox;
0185 class KJanusWidget;
0186 class KMyHistoryCombo;
0187 class KMyListBox;
0188 class KMyListView;
0189 class KMenuBar;
0190 class KPopupMenu;
0191 class QPushButton;
0192 class KURL;
0193 class KURLRequester;
0194 class KIntNumInput;
0195 class MyHelpCombo;
0196 class Plugin;
0197 class PluginLoader;
0198 class QButtonGroup;
0199 class QCheckBox;
0200 class QGroupBox;
0201 class QFileInfo;
0202 class QHBoxLayout;
0203 class QLabel;
0204 class QLineEdit;
0205 class QPushButton;
0206 class QRect;
0207 class QWidgetStack;
0208 class QRadioButton;
0209 class QVBoxLayout;
0210 class QVGroupBox;
0211 class QWidget;
0212 
0213 #include <kguiitem.h>
0214 QPushButton *createButton(KGuiItem item, QWidget *parent);
0215 
0216 class KRenameImpl : public QObject, public KRenameDCOP
0217 {
0218     Q_OBJECT
0219 
0220     friend class ProfileManager;
0221     friend class ProfileDlg;
0222     friend class tabs;
0223 
0224 public:
0225     KRenameImpl(QWidget *p, KMenuBar *m, QPushButton *finish);
0226     ~KRenameImpl();
0227 
0228     void setWizardMode(bool mode);
0229 
0230     /*
0231      * public because both get called from KRenameImpl::launch()
0232      */
0233     void updatePre();
0234     void addFileOrDir(const KURL &name);
0235 
0236     void setup(bool wizardmode);
0237     void changeParent(QWidget *p, KMenuBar *m, QPushButton *finish, QRect r);
0238     static QWidget *launch(QRect rect, const KURL::List &list, KRenameImpl *impl = 0, bool loadprofile = true);
0239 
0240     /** DCOP functions we have to implement
0241       */
0242     void addFileOrDir(const QString &name);
0243     const QString fileNameTemplate() const;
0244     inline int counterStart() const;
0245 
0246     void setExtensionTemplate(const QString &t);
0247     const QString extensionTemplate() const;
0248 
0249     void setUseExtension(bool b);
0250     bool useExtension() const;
0251 
0252     QStringList tokens() const;
0253 
0254     QString parseString(const QString &token, const QString &string);
0255     void addDir(const QString &name, const QString &filter, bool recursive, bool hidden, bool dirnames);
0256 
0257     const QString title(int index) const;
0258 
0259     /** @returns true if the user has specified a profile on the commandline
0260      *           other wise false is returned
0261      */
0262     inline bool hasCommandlineProfile() const;
0263 
0264 public Q_SLOTS:
0265     void setFileNameTemplate(const QString &t);
0266     void setCounterStart(int index);
0267 
0268 Q_SIGNALS:
0269     void pageDone(QWidget *page, const QString &title);
0270     void showPage(int page);
0271     void enableFinish(bool b);
0272 
0273 private Q_SLOTS:
0274     void about();
0275     void addFile();
0276     bool checkErrors();
0277     void clearList();
0278     void enableControls();
0279     void toggleImagePreview();
0280     void moveUp();
0281     void moveDown();
0282     void help();
0283     void removeFile();
0284     void removeFile(int index);
0285     void preferences();
0286     void replace();
0287     void saveConfig();
0288     void start();
0289     void updateCount();
0290     void undo();
0291     void changeUndoScriptName();
0292     void pluginHelpChanged();
0293     void getCoordinates();
0294     void changeNumbers();
0295     void updateDots();
0296     void updatePreview();
0297     void showTokenHelp();
0298     void toggleName();
0299     void changed();
0300     void loadFilePlugins();
0301     void reloadFilePluginData();
0302     void manageProfiles();
0303     QString easyOptions(KComboBox *combo, KMyHistoryCombo *custom);
0304     void slotEasy1();
0305     void slotEasy2();
0306     void slotEasy3();
0307     void slotEasy4();
0308 
0309     /** Update the preview only if the passed
0310      *  plugin is enabled for use.
0311      */
0312     void updatePluginPreview(Plugin *p);
0313 
0314 private:
0315     static int numRealTimePreview;
0316 
0317     /** Change the GUI mode according to the current setting of m_wizard
0318       */
0319     void changeGUIMode();
0320 
0321     /** Returns COPY if optionCopy is checked, RENAME if optionRename is checked
0322       * ...
0323       */
0324     int currentRenameMode();
0325 
0326     void loadConfig();
0327     void fillStructures(BatchRenamer *b, bool preview);
0328     bool setupBatchRenamer(BatchRenamer *b, bool preview);
0329     void splitFilename(QFileInfo *fi, QString *base, QString *extension);
0330 
0331     void setupPage1();
0332     void setupPage2();
0333     void setupPage3();
0334     void setupPage4();
0335     void setupTab1();
0336     void setupFileTab1();
0337     void setupFileTab2();
0338     void setupPages();
0339     void updateHist();
0340     void parseCommandline();
0341     void addTitle(QWidget *p, QVBoxLayout *layout, QString title);
0342 
0343     QValueList<manualchanges> changes;
0344     void refreshColumnMode();
0345     void parseWizardMode();
0346     void getHelpDialogString(QLineEdit *edit);
0347 
0348 protected:
0349     QWidget *parent;
0350     KMenuBar *menuBar;
0351     KPopupMenu *mnuSort;
0352     KAction *loadPlugins;
0353 
0354     QPushButton *finishButton;
0355 
0356     PluginLoader *plugin;
0357 
0358     QWidgetStack *fileTab;
0359 
0360     QWidget *page_1;
0361     QWidget *page_2;
0362     KJanusWidget *page_3;
0363     QWidget *page_4;
0364 
0365     bool m_wizard;
0366     bool m_loadplugins;
0367     bool m_switching;
0368     bool m_autosize;
0369     int m_hist;
0370     int m_index;
0371     int m_step;
0372     bool m_reset;
0373 
0374     bool m_hasCommandlineProfile;
0375 
0376     QPushButton *buttonUp;
0377     QPushButton *buttonDown;
0378     QPushButton *buttonUp2;
0379     QPushButton *buttonDown2;
0380     QPushButton *buttonAdd;
0381     QPushButton *buttonRemove;
0382     QPushButton *buttonRemoveAll;
0383     QPushButton *buttonReplace;
0384     QPushButton *buttonEasyReplace;
0385     QPushButton *buttonHelp;
0386     QPushButton *buttonMove;
0387     QPushButton *buttonCoord;
0388     QPushButton *buttonNumber;
0389 
0390     QPushButton *buttonEasy1;
0391     QPushButton *buttonEasy2;
0392     QPushButton *buttonEasy3;
0393     QPushButton *buttonEasy4;
0394     KComboBox *comboSort;
0395 
0396     QLabel *description;
0397     QLabel *description2;
0398     QLabel *description3;
0399     QLabel *description4;
0400 
0401     QLabel *labelTemplate;
0402     QLabel *labelHelp;
0403     QLabel *labelCount;
0404     QLabel *labelPoint;
0405 
0406     KMyListBox *fileList;
0407     KMyListView *preview;
0408     QButtonGroup *groupOptions;
0409 
0410     QRadioButton *optionCopy;
0411     QRadioButton *optionMove;
0412     QRadioButton *optionRename;
0413     QRadioButton *optionLink;
0414 
0415     QGroupBox *groupExtension;
0416     QVGroupBox *groupUndo;
0417 
0418     KMyHistoryCombo *dirname;
0419     KURLRequester *urlrequester;
0420     KURLRequester *undorequester;
0421     KMyHistoryCombo *filename;
0422     KMyHistoryCombo *extemplate;
0423 
0424     QCheckBox *checkName;
0425     QCheckBox *checkExtension;
0426     QCheckBox *checkOverwrite;
0427     QCheckBox *checkPreview;
0428     QCheckBox *checkUndoScript;
0429 
0430     KComboBox *comboExtension;
0431 
0432     MyHelpCombo *comboHelp;
0433     HelpDialog *helpDialog;
0434     HelpDialogData *helpDialogData;
0435 
0436     BatchRenamer *b;
0437     QValueList<int> skip;
0438     QValueList<replacestrings> rep;
0439 
0440     // ==========
0441     // Easy mode:
0442     // ==========
0443 
0444     KComboBox *comboKRenamePrefix;
0445     KComboBox *comboKRenameSuffix;
0446     KComboBox *comboKRenameFilename;
0447     KComboBox *comboKRenameExtension;
0448 
0449     KMyHistoryCombo *comboPrefix;
0450     KMyHistoryCombo *comboSuffix;
0451     KMyHistoryCombo *comboCustom;
0452     KMyHistoryCombo *comboCustomExtension;
0453 
0454     KIntNumInput *spinStart;
0455     KIntNumInput *spinNull;
0456 
0457     // ===========
0458     // Layout:
0459     // ===========
0460 
0461     QHBoxLayout *pageLayout;
0462     QVBoxLayout *pageLayout_2;
0463     QVBoxLayout *pageLayout_3;
0464     QVBoxLayout *pageLayout_4;
0465 
0466     QHBoxLayout *tabLayout_0;
0467     QHBoxLayout *tabLayout_1;
0468     QHBoxLayout *tabLayout_2;
0469     QVBoxLayout *tabLayout_3;
0470     QVBoxLayout *groupAdvancedExtensionLayout;
0471     QVBoxLayout *groupOptionsLayout;
0472     QHBoxLayout *groupDirLayout;
0473     QVBoxLayout *groupNumberLayout;
0474     QHBoxLayout *groupExtensionLayout;
0475 
0476     // page1
0477     QHBoxLayout *Layout2;
0478     QVBoxLayout *Layout3;
0479     QHBoxLayout *Layout4;
0480     QVBoxLayout *Layout5;
0481 
0482     // page4
0483     QVBoxLayout *Layout10;
0484     QHBoxLayout *Layout15;
0485     QVBoxLayout *Layout16;
0486     QHBoxLayout *Layout22;
0487     QVBoxLayout *Layout23;
0488 
0489     // tab
0490     QHBoxLayout *Layout100;
0491     QHBoxLayout *Layout101;
0492 };
0493 
0494 int KRenameImpl::counterStart() const
0495 {
0496     return m_index;
0497 }
0498 
0499 bool KRenameImpl::hasCommandlineProfile() const
0500 {
0501     return m_hasCommandlineProfile;
0502 }
0503 #endif // 0
0504 #endif