KRename – powerful batch renamer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

batchrenamer.h 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /***************************************************************************
  2. batchrenamer.h - description
  3. -------------------
  4. begin : Sat Aug 18 2001
  5. copyright : (C) 2001 by Dominik Seichter
  6. email : domseichter@web.de
  7. ***************************************************************************/
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #ifndef BATCHRENAMER_H
  17. #define BATCHRENAMER_H
  18. #include <tqdatetime.h>
  19. #include <tqvaluelist.h>
  20. #include <kurl.h>
  21. class TQFile;
  22. class TQProgressDialog;
  23. class TQString;
  24. /* How many diferrent users and groups
  25. * KRename supports. Values over 1000
  26. * make KRename slow, but it may be
  27. * necessary on bigger systems to
  28. * increase this value.
  29. * MAXENTRIES must be < sizeof(int)
  30. */
  31. #define MAXENTRIES 1000
  32. /* May Value for SpinBoxes
  33. *
  34. */
  35. #define SPINMAX 100000
  36. enum {
  37. COPY, MOVE, RENAME, PREVIEW, LINK
  38. };
  39. typedef struct fileentry {
  40. TQString name; // filename
  41. TQString directory; // directory
  42. TQString extension; // extension
  43. KURL url;
  44. };
  45. typedef struct data {
  46. fileentry src;
  47. fileentry dst;
  48. bool dir;
  49. };
  50. /*
  51. * Changes made by hand by the user
  52. * in the preview list view are
  53. * stored here.
  54. */
  55. typedef struct manualchanges {
  56. KURL url; // input filename
  57. TQString user; // name the user wants
  58. };
  59. typedef struct replacestrings {
  60. TQString tqfind; // Text to tqreplace
  61. TQString tqreplace; // Replace with
  62. bool reg; // is it a reg expression ?
  63. };
  64. typedef struct tCounterValues {
  65. int value; // current value of this counter
  66. int start; // start value of this counter (for findResetCounter)
  67. int step; // stepping value of this counter;
  68. };
  69. /**
  70. *@author Dominik Seichter
  71. */
  72. class TQObject;
  73. class TQListView;
  74. class ProgressDialog;
  75. class PluginLoader;
  76. class BatchRenamer {
  77. public:
  78. BatchRenamer();
  79. ~BatchRenamer();
  80. void processFiles( ProgressDialog* p, TQObject* object );
  81. void createPreview( TQListView* list );
  82. inline void setText( const TQString & t ) { text = t; doEscape( text, false ); }
  83. inline void setExText( const TQString & t ) { extext = t; }
  84. inline void setDirname( const KURL & url ) { dirname = url; }
  85. inline void setUndoScript( const TQString & t ) { m_undoScript = t; }
  86. inline void setUndo( bool b ) { undo = b; }
  87. inline void setOverwrite( bool b ) { overwrite = b; }
  88. inline void setIndex( int i ) { m_index = i; }
  89. inline void setStep( int s ) { m_step = s; }
  90. inline void setResetCounter( bool r ) { m_reset = r; }
  91. inline void setSkipList( const TQValueList<int> & s ) { m_skip = s; }
  92. inline void setReplaceList( const TQValueList<replacestrings> & r ) { m_tqreplace = r; }
  93. inline void setFiles( const TQValueList<data> & f ) { m_files = f; } //TODO: use a pointer for more speed
  94. inline TQValueList<data> files() const { return m_files; }
  95. inline void setChanges( const TQValueList<manualchanges> & m ) { m_changes = m; }
  96. inline void setMode( int m) { m_mode = m; }
  97. inline int mode() const { return m_mode; }
  98. // Since 2.1 public, because plugins may want to access them to:
  99. TQString findAndProcess( const TQString & token, TQString text, const TQString & tqreplace );
  100. TQString findNumbers( TQString text, int count, int i );
  101. TQString findStar( const TQString & oldname, TQString text );
  102. TQString findBrackets( TQString oldname, TQString text, int i );
  103. TQString findToken( TQString oldname, TQString token, int i );
  104. TQString processToken( TQString token, TQString oldname, int i );
  105. TQString findPartStrings( TQString oldname, TQString token );
  106. static TQString findDirName( TQString token, TQString path );
  107. TQString findLength( const TQString & token, const TQString & name );
  108. TQString findReplace( TQString text ); // text is here already the new filename !
  109. TQString doReplace( TQString text, TQString tqfind, TQString tqreplace, bool reg ); // text is here already the new filename !
  110. TQString processString( TQString text, TQString oldname, int i );
  111. static TQString & doEscape( TQString text, bool filename = true );
  112. static TQString & unEscape( TQString & text );
  113. static void escape( TQString & text, const TQString & token, const TQString & sequence );
  114. static TQString buildFilename( fileentry* entry, bool dir = true );
  115. private:
  116. /**
  117. * Returns the length of the string when int n is converted to
  118. * a string.
  119. * @param n a number whose length as string is needed
  120. * @returns stringlength of n converted to a string
  121. */
  122. int getCharacters( int n ) ;
  123. void work( ProgressDialog* p );
  124. void writeUndoScript( TQTextStream* t );
  125. void parseSubdirs( data* f );
  126. void findNumberAppendix( TQString & text, int pos, int* start, int* step );
  127. /** resets all counters to there start value if the directory name at @p i
  128. * in m_files changes.
  129. * The caller has to check m_reset before calling this function.
  130. */
  131. void findCounterReset( int i );
  132. TQString parsePlugins( int i, const TQString & text, int type );
  133. bool applyManualChanges( int i );
  134. TQString text; // template
  135. KURL dirname; // destination dir
  136. TQString extext; // Extension template
  137. TQString m_undoScript; // Filename of undoscript
  138. bool undo; // create an undo script
  139. int m_index; // index for numbers
  140. int m_step; // step for numbers
  141. bool m_reset; // reset counter on new directories
  142. bool overwrite; // overwrite existing files
  143. int m_mode; // renaming mode
  144. TQValueList<int> m_skip; // Numbers to skip
  145. TQValueList<replacestrings> m_tqreplace; // Replace strings
  146. TQValueList<data> m_files;
  147. TQValueList<manualchanges> m_changes; // User made changes
  148. PluginLoader* plug;
  149. // a is used in find number and
  150. // required for skipping.
  151. int m_counter_index;
  152. TQValueList<tCounterValues> m_counters;
  153. protected:
  154. TQFile* f;
  155. TQTime t;
  156. TQProgressDialog* progress;
  157. };
  158. #endif