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

0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 // SPDX-FileCopyrightText: 2007 Dominik Seichter <domseichter@web.de>
0003 
0004 #ifndef KRENAME_FILE_H
0005 #define KRENAME_FILE_H
0006 
0007 #include <QVector>
0008 
0009 #include <kfileitem.h>
0010 #include <QUrl>
0011 #include <QPixmap>
0012 
0013 #include <kio/pixmaploader.h>
0014 
0015 class KFileItem;
0016 
0017 /** An enum to describe the mode to split
0018  *  filename and extension.
0019  */
0020 enum ESplitMode {
0021     eSplitMode_FirstDot,    ///< Extension starts at the first dot found in the filename
0022     eSplitMode_LastDot,     ///< Extension starts at the last dot found in the filename
0023     eSplitMode_NoExtension, ///< Do not use file extension handling
0024     eSplitMode_CustomDot    ///< Extension starts at a user defined dot in the filename
0025 };
0026 
0027 /**
0028  * Type of manual change made.
0029  * Specifies on what kind of filename, the manual changes are based.
0030  */
0031 enum EManualChangeMode {
0032     eManualChangeMode_None, ///< Use filename created by KRename
0033     eManualChangeMode_Input, ///< Use custom filename, based on input filename
0034     eManualChangeMode_Custom ///< Use custom filename
0035 };
0036 
0037 class KRenameFile
0038 {
0039     struct TFileDescription {
0040         QString filename;
0041         QString extension;
0042         QString directory;
0043 
0044         QUrl    url;
0045 
0046         const TFileDescription &operator=(const TFileDescription &rhs)
0047         {
0048             filename  = rhs.filename;
0049             extension = rhs.extension;
0050             directory = rhs.directory;
0051             url       = rhs.url;
0052 
0053             return *this;
0054         }
0055     };
0056 
0057 public:
0058 
0059     /** A list of KRenameFile objects
0060      */
0061     typedef QVector<KRenameFile> List;
0062 
0063     /** Empty default constructor
0064      *  which creates an invalid KRenameFile.
0065      *
0066      *  \see isValid
0067      */
0068     KRenameFile()
0069         : m_bValid(false)
0070     {
0071     }
0072 
0073     /** Construct a new KRenameFile from an url.
0074      *
0075      *  The url is expected to exist and is not
0076      *  tested for existence. This is much faster than
0077      *  the other constructor.
0078      *
0079      *  \param src an url of a file or directory
0080      *  \param directory must be true if the url referes
0081      *                   to a directory.
0082      *  \param eSplitMode splitmode which is used to separate
0083      *                    filename and extension
0084      *  \param dot dot to use as separator for eSplitMode_CustomDot
0085      */
0086     KRenameFile(const QUrl &src, bool directory, ESplitMode eSplitMode, unsigned int dot);
0087 
0088     /** Construct a new KRenameFile form a KFileItem which is faster
0089      *  than construction from an URL.
0090      *
0091      *  \param item a KFileItem
0092      *  \param eSplitMode splitmode which is used to separate
0093      *                    filename and extension
0094      *  \param dot dot to use as separator for eSplitMode_CustomDot
0095      */
0096 
0097     KRenameFile(const QUrl &src, ESplitMode eSplitMode, unsigned int dot);
0098 
0099     KRenameFile(const KFileItem &item, ESplitMode eSplitMode, unsigned int dot);
0100 
0101     /** Copy constructor
0102      *  \param rhs KRenameFile to copy
0103      */
0104     KRenameFile(const KRenameFile &rhs);
0105 
0106     /** Set the splitmode to separate filename from fileextension
0107      *
0108      *  \param eSplitMode splitmode which is used to separate
0109      *                    filename and extension
0110      *  \param dot dot to use as separator for eSplitMode_CustomDot
0111      *
0112      *  \see srcFilename()
0113      *  \see srcExtension()
0114      */
0115     void setCurrentSplitMode(ESplitMode eSplitMode, unsigned int dot = 1);
0116 
0117     /**
0118      * \returns the number of dots in this filename that can be used to separate filename and extension
0119      */
0120     int dots() const;
0121 
0122     /** Convert the KRenameFile into a string
0123      *  that can be displayed to the user.
0124      *
0125      *  \returns original source url as string representation
0126      */
0127     inline const QString toString() const
0128     {
0129         return m_src.url.toDisplayString(QUrl::PreferLocalFile);
0130     }
0131 
0132     /** Get a preview icon of the KRenameFile
0133      *
0134      *  @returns a QPixmap containing a preview of this KRenameFile.
0135      *           This might be only a mimetype icon depending on the current KDE settings.
0136      */
0137     inline const QPixmap &icon() const
0138     {
0139         if (m_icon.isNull()) {
0140             const_cast<KRenameFile *>(this)->loadPreviewIcon();
0141         }
0142 
0143         return m_icon;
0144     }
0145 
0146     /** Set the preview icon of the KRenameFile
0147      *
0148      *  @param icon a preview icon.
0149      *
0150      *  This method is only here for performance reason
0151      *  to easily and fast set the icon from one
0152      *  KRenameFile onto another.
0153      *
0154      *  Normally KRenameFile knows how to load the icon itself.
0155      */
0156     inline void setIcon(const QPixmap &icon)
0157     {
0158         m_icon = icon;
0159     }
0160 
0161     /** Set this error code to value != 0
0162      *  if an error has occurred during renaming this
0163      *  particular file.
0164      *
0165      *  @param error an error code (0 means no error)
0166      */
0167     inline void setError(int error)
0168     {
0169         m_error = error;;
0170     }
0171 
0172     /**
0173      * @returns true if an error code was set for this KRenameFile
0174      */
0175     inline bool hasError() const
0176     {
0177         return m_error != 0;
0178     }
0179 
0180     /** Assigns another KRenameFile to this KRenameFile
0181      *  \param rhs object to assign
0182      */
0183     const KRenameFile &operator=(const KRenameFile &rhs);
0184 
0185     /** Compare a KRenameFile object to a KFileItem
0186      *
0187      *  \returns true if the file item of this KRenameFile
0188      *           is identical to the parameter
0189      */
0190     bool operator==(const KFileItem &item) const;
0191 
0192     /**
0193      * \returns true if this file references
0194      *               an existing file or directory
0195      */
0196     inline bool isValid() const
0197     {
0198         return m_bValid;
0199     }
0200 
0201     /**
0202      * \returns manualChanges the user has made to the filename
0203      */
0204     inline const QString &manualChanges() const
0205     {
0206         return m_manual;
0207     }
0208 
0209     /**
0210      * Sets manual changes made by the user
0211      *
0212      * \param manual manual changes for filename and extension
0213      * \param mode mode of change
0214      */
0215     inline void setManualChanges(const QString &manual, EManualChangeMode mode)
0216     {
0217         m_manual = manual;
0218         m_manualMode = mode;
0219     }
0220 
0221     /**
0222      * \returns the change mode
0223      */
0224     inline EManualChangeMode manualChangeMode() const
0225     {
0226         return m_manualMode;
0227     }
0228 
0229     /**
0230      * \returns always the original source directory
0231      */
0232     inline const QString &realSrcDirectory() const
0233     {
0234         return m_src.directory;
0235     }
0236 
0237     inline void setOverrideSrcDirectory(const QString &dir)
0238     {
0239         m_overrideDir = dir;
0240     }
0241 
0242     inline const QString &srcFilename() const
0243     {
0244         return m_src.filename;
0245     }
0246 
0247     inline const QString &srcExtension() const
0248     {
0249         return m_src.extension;
0250     }
0251 
0252     inline const QString &srcDirectory() const
0253     {
0254         return (m_overrideDir.isNull() ? m_src.directory : m_overrideDir);
0255     }
0256 
0257     const QUrl srcUrl() const;
0258 
0259     inline void setDstFilename(const QString &filename)
0260     {
0261         m_dst.filename = filename;
0262     }
0263 
0264     inline const QString &dstFilename() const
0265     {
0266         return m_dst.filename;
0267     }
0268 
0269     inline void setDstExtension(const QString &extension)
0270     {
0271         m_dst.extension = extension;
0272     }
0273 
0274     inline const QString &dstExtension() const
0275     {
0276         return m_dst.extension;
0277     }
0278 
0279     inline void setDstDirectory(const QString &directory)
0280     {
0281         m_dst.directory = directory;
0282     }
0283 
0284     inline const QString &dstDirectory() const
0285     {
0286         return m_dst.directory;
0287     }
0288 
0289     inline void setDstUrl(const QUrl &url)
0290     {
0291         m_dst.url = url;
0292     }
0293 
0294     inline const QUrl &dstUrl() const
0295     {
0296         return m_dst.url;
0297     }
0298 
0299     inline bool isDirectory() const
0300     {
0301         return m_bDirectory;
0302     }
0303 
0304     /**
0305      * Get access to the internal file item
0306      *
0307      * @returns a KFileItem
0308      */
0309     const KFileItem &fileItem() const;
0310 
0311     /**
0312      * Set the icon (preview) size
0313      *
0314      * @param size the icon size
0315      */
0316     static void setIconSize(int size);
0317     /**
0318      * @returns the icon size
0319      */
0320     static int iconSize();
0321     /**
0322      * @returns the default icon size
0323      */
0324     static int getDefaultIconSize();
0325 
0326 private:
0327     void initFileDescription(TFileDescription &rDescription, const QUrl &url, ESplitMode eSplitMode, unsigned int dot) const;
0328 
0329     /** Load a preview icon for this KRenameFile object
0330      *  using KDEs preview loading mechanism.
0331      */
0332     void loadPreviewIcon();
0333 
0334     static const int DEFAULT_ICON_SIZE;
0335     static const char *EXTRA_DATA_KEY;
0336 
0337 private:
0338     KFileItem        m_fileItem;
0339     static int       m_iconSize;
0340 
0341     TFileDescription m_src;
0342     TFileDescription m_dst;
0343 
0344     QString          m_overrideDir;  ///< A changed sourcedirectory (required when renaming directories)
0345 
0346     bool             m_bDirectory;   ///< If true this is a directory
0347     bool             m_bValid;       ///< If true this item is valid
0348 
0349     QPixmap          m_icon;         ///< This is the file preview icon
0350 
0351     int              m_error;        ///< This value is set to != 0 if an error occurred during renaming
0352     QString          m_manual;       ///< Manual changes to the filename+extension by the user are stored here
0353     EManualChangeMode m_manualMode;
0354 };
0355 
0356 #endif // KRENAME_FILE_H