File indexing completed on 2024-04-21 16:32:32

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