1 /*************************************************************************** 2 krenamefile.h - description 3 ------------------- 4 begin : Wed Apr 18 2007 5 copyright : (C) 2007 by Dominik Seichter 6 email : domseichter@web.de 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 18 #ifndef KRENAME_FILE_H 19 #define KRENAME_FILE_H 20 21 #include <QVector> 22 23 #include <kfileitem.h> 24 #include <QUrl> 25 #include <QPixmap> 26 27 #include <kio/pixmaploader.h> 28 29 class KFileItem; 30 31 /** An enum to describe the mode to split 32 * filename and extension. 33 */ 34 enum ESplitMode { 35 eSplitMode_FirstDot, ///< Extension starts at the first dot found in the filename 36 eSplitMode_LastDot, ///< Extension starts at the last dot found in the filename 37 eSplitMode_NoExtension, ///< Do not use file extension handling 38 eSplitMode_CustomDot ///< Extension starts at a user defined dot in the filename 39 }; 40 41 /** 42 * Type of manual change made. 43 * Specifies on what kind of filename, the manual changes are based. 44 */ 45 enum EManualChangeMode { 46 eManualChangeMode_None, ///< Use filename created by KRename 47 eManualChangeMode_Input, ///< Use custom filename, based on input filename 48 eManualChangeMode_Custom ///< Use custom filename 49 }; 50 51 class KRenameFile 52 { 53 struct TFileDescription { 54 QString filename; 55 QString extension; 56 QString directory; 57 58 QUrl url; 59 60 const TFileDescription &operator=(const TFileDescription &rhs) 61 { 62 filename = rhs.filename; 63 extension = rhs.extension; 64 directory = rhs.directory; 65 url = rhs.url; 66 67 return *this; 68 } 69 }; 70 71 public: 72 73 /** A list of KRenameFile objects 74 */ 75 typedef QVector<KRenameFile> List; 76 77 /** Empty default constructor 78 * which creates an invalid KRenameFile. 79 * 80 * \see isValid 81 */ KRenameFile()82 KRenameFile() 83 : m_bValid(false) 84 { 85 } 86 87 /** Construct a new KRenameFile from an url. 88 * 89 * The url is expected to exist and is not 90 * tested for existence. This is much faster than 91 * the other constructor. 92 * 93 * \param src an url of a file or directory 94 * \param directory must be true if the url referes 95 * to a directory. 96 * \param eSplitMode splitmode which is used to separate 97 * filename and extension 98 * \param dot dot to use as separator for eSplitMode_CustomDot 99 */ 100 KRenameFile(const QUrl &src, bool directory, ESplitMode eSplitMode, unsigned int dot); 101 102 /** Construct a new KRenameFile form a KFileItem which is faster 103 * than construction from an URL. 104 * 105 * \param item a KFileItem 106 * \param eSplitMode splitmode which is used to separate 107 * filename and extension 108 * \param dot dot to use as separator for eSplitMode_CustomDot 109 */ 110 111 KRenameFile(const QUrl &src, ESplitMode eSplitMode, unsigned int dot); 112 113 KRenameFile(const KFileItem &item, ESplitMode eSplitMode, unsigned int dot); 114 115 /** Copy constructor 116 * \param rhs KRenameFile to copy 117 */ 118 KRenameFile(const KRenameFile &rhs); 119 120 /** Set the splitmode to separate filename from fileextension 121 * 122 * \param eSplitMode splitmode which is used to separate 123 * filename and extension 124 * \param dot dot to use as separator for eSplitMode_CustomDot 125 * 126 * \see srcFilename() 127 * \see srcExtension() 128 */ 129 void setCurrentSplitMode(ESplitMode eSplitMode, unsigned int dot = 1); 130 131 /** 132 * \returns the number of dots in this filename that can be used to separate filename and extension 133 */ 134 int dots() const; 135 136 /** Convert the KRenameFile into a string 137 * that can be displayed to the user. 138 * 139 * \returns original source url as string representation 140 */ toString()141 inline const QString toString() const 142 { 143 return m_src.url.toDisplayString(QUrl::PreferLocalFile); 144 } 145 146 /** Get a preview icon of the KRenameFile 147 * 148 * @returns a QPixmap containing a preview of this KRenameFile. 149 * This might be only a mimetype icon depending on the current KDE settings. 150 */ icon()151 inline const QPixmap &icon() const 152 { 153 if (m_icon.isNull()) { 154 const_cast<KRenameFile *>(this)->loadPreviewIcon(); 155 } 156 157 return m_icon; 158 } 159 160 /** Set the preview icon of the KRenameFile 161 * 162 * @param icon a preview icon. 163 * 164 * This method is only here for performance reason 165 * to easily and fast set the icon from one 166 * KRenameFile onto another. 167 * 168 * Normally KRenameFile knows how to load the icon itself. 169 */ setIcon(const QPixmap & icon)170 inline void setIcon(const QPixmap &icon) 171 { 172 m_icon = icon; 173 } 174 175 /** Set this error code to value != 0 176 * if an error has occurred during renaming this 177 * particular file. 178 * 179 * @param error an error code (0 means no error) 180 */ setError(int error)181 inline void setError(int error) 182 { 183 m_error = error;; 184 } 185 186 /** 187 * @returns true if an error code was set for this KRenameFile 188 */ hasError()189 inline bool hasError() const 190 { 191 return m_error != 0; 192 } 193 194 /** Assigns another KRenameFile to this KRenameFile 195 * \param rhs object to assign 196 */ 197 const KRenameFile &operator=(const KRenameFile &rhs); 198 199 /** Compare a KRenameFile object to a KFileItem 200 * 201 * \returns true if the file item of this KRenameFile 202 * is identical to the parameter 203 */ 204 bool operator==(const KFileItem &item) const; 205 206 /** 207 * \returns true if this file references 208 * an existing file or directory 209 */ isValid()210 inline bool isValid() const 211 { 212 return m_bValid; 213 } 214 215 /** 216 * \returns manualChanges the user has made to the filename 217 */ manualChanges()218 inline const QString &manualChanges() const 219 { 220 return m_manual; 221 } 222 223 /** 224 * Sets manual changes made by the user 225 * 226 * \param manual manual changes for filename and extension 227 * \param mode mode of change 228 */ setManualChanges(const QString & manual,EManualChangeMode mode)229 inline void setManualChanges(const QString &manual, EManualChangeMode mode) 230 { 231 m_manual = manual; 232 m_manualMode = mode; 233 } 234 235 /** 236 * \returns the change mode 237 */ manualChangeMode()238 inline EManualChangeMode manualChangeMode() const 239 { 240 return m_manualMode; 241 } 242 243 /** 244 * \returns always the original source directory 245 */ realSrcDirectory()246 inline const QString &realSrcDirectory() const 247 { 248 return m_src.directory; 249 } 250 setOverrideSrcDirectory(const QString & dir)251 inline void setOverrideSrcDirectory(const QString &dir) 252 { 253 m_overrideDir = dir; 254 } 255 srcFilename()256 inline const QString &srcFilename() const 257 { 258 return m_src.filename; 259 } 260 srcExtension()261 inline const QString &srcExtension() const 262 { 263 return m_src.extension; 264 } 265 srcDirectory()266 inline const QString &srcDirectory() const 267 { 268 return (m_overrideDir.isNull() ? m_src.directory : m_overrideDir); 269 } 270 271 const QUrl srcUrl() const; 272 setDstFilename(const QString & filename)273 inline void setDstFilename(const QString &filename) 274 { 275 m_dst.filename = filename; 276 } 277 dstFilename()278 inline const QString &dstFilename() const 279 { 280 return m_dst.filename; 281 } 282 setDstExtension(const QString & extension)283 inline void setDstExtension(const QString &extension) 284 { 285 m_dst.extension = extension; 286 } 287 dstExtension()288 inline const QString &dstExtension() const 289 { 290 return m_dst.extension; 291 } 292 setDstDirectory(const QString & directory)293 inline void setDstDirectory(const QString &directory) 294 { 295 m_dst.directory = directory; 296 } 297 dstDirectory()298 inline const QString &dstDirectory() const 299 { 300 return m_dst.directory; 301 } 302 setDstUrl(const QUrl & url)303 inline void setDstUrl(const QUrl &url) 304 { 305 m_dst.url = url; 306 } 307 dstUrl()308 inline const QUrl &dstUrl() const 309 { 310 return m_dst.url; 311 } 312 isDirectory()313 inline bool isDirectory() const 314 { 315 return m_bDirectory; 316 } 317 318 /** 319 * Get access to the internal file item 320 * 321 * @returns a KFileItem 322 */ 323 const KFileItem &fileItem() const; 324 325 /** 326 * Set the icon (preview) size 327 * 328 * @param size the icon size 329 */ 330 static void setIconSize(int size); 331 /** 332 * @returns the icon size 333 */ 334 static int iconSize(); 335 /** 336 * @returns the default icon size 337 */ 338 static int getDefaultIconSize(); 339 340 private: 341 void initFileDescription(TFileDescription &rDescription, const QUrl &url, ESplitMode eSplitMode, unsigned int dot) const; 342 343 /** Load a preview icon for this KRenameFile object 344 * using KDEs preview loading mechanism. 345 */ 346 void loadPreviewIcon(); 347 348 static const int DEFAULT_ICON_SIZE; 349 static const char *EXTRA_DATA_KEY; 350 351 private: 352 KFileItem m_fileItem; 353 static int m_iconSize; 354 355 TFileDescription m_src; 356 TFileDescription m_dst; 357 358 QString m_overrideDir; ///< A changed sourcedirectory (required when renaming directories) 359 360 bool m_bDirectory; ///< If true this is a directory 361 bool m_bValid; ///< If true this item is valid 362 363 QPixmap m_icon; ///< This is the file preview icon 364 365 int m_error; ///< This value is set to != 0 if an error occurred during renaming 366 QString m_manual; ///< Manual changes to the filename+extension by the user are stored here 367 EManualChangeMode m_manualMode; 368 }; 369 370 #endif // KRENAME_FILE_H 371