File indexing completed on 2022-11-29 20:25:13

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