summaryrefslogtreecommitdiffstats
path: root/krename/batchrenamer.h
blob: d188b51c8ba598e6e0ce0640fdad2678350a2b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***************************************************************************
                          batchrenamer.h  -  description
                             -------------------
    begin                : Sat Aug 18 2001
    copyright            : (C) 2001 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef BATCHRENAMER_H
#define BATCHRENAMER_H

#include <tqdatetime.h>
#include <tqvaluelist.h>
#include <kurl.h>

class TQFile;
class TQProgressDialog;
class TQString;

/* How many diferrent users and groups
 * KRename supports. Values over 1000
 * make KRename slow, but it may be
 * necessary on bigger systems to
 * increase this value.
 * MAXENTRIES must be < sizeof(int)
 */
#define MAXENTRIES 1000

/* May Value for SpinBoxes
 *
 */
#define SPINMAX 100000

enum {
    COPY, MOVE, RENAME, PREVIEW, LINK
};

struct fileentry {
    TQString name;       // filename
    TQString directory;  // directory
    TQString extension;  // extension

    KURL url;
};

struct data {
    fileentry src;
    fileentry dst;

    bool dir;
};

/*
 * Changes made by hand by the user
 * in the preview list view are
 * stored here.
 */
struct manualchanges {
    KURL url;      // input filename
    TQString user;  // name the user wants
};

struct replacestrings {
    TQString find;      // Text to replace
    TQString replace;   // Replace with
    bool reg;          // is it a reg expression ?
};

struct tCounterValues {
    int value;  // current value of this counter
    int start;  // start value of this counter (for findResetCounter)
    int step;   // stepping value of this counter;
};

/**
  *@author Dominik Seichter
  */

class TQObject;
class TQListView;
class ProgressDialog;
class PluginLoader;
class BatchRenamer {
    public:
        BatchRenamer();
        ~BatchRenamer();
        void processFiles( ProgressDialog* p, TQObject* object );
        void createPreview( TQListView* list );

        inline void setText( const TQString & t ) { text = t; doEscape( text, false ); }
        inline void setExText( const TQString & t ) { extext = t; }
        inline void setDirname( const KURL & url ) { dirname = url; }
        inline void setUndoScript( const TQString & t ) { m_undoScript = t; }
        inline void setUndo( bool b ) { undo = b; }
        inline void setOverwrite( bool b ) { overwrite = b; }
        inline void setIndex( int i ) { m_index = i; }
        inline void setStep( int s ) { m_step = s; }
	inline void setResetCounter( bool r ) { m_reset = r; }
        inline void setSkipList( const TQValueList<int> & s ) { m_skip = s; }
        inline void setReplaceList( const TQValueList<replacestrings> & r ) { m_replace = r; }
        
        inline void setFiles( const TQValueList<data> & f ) { m_files = f; } //TODO: use a pointer for more speed
        inline TQValueList<data> files() const { return m_files; }
        inline void setChanges( const TQValueList<manualchanges> & m ) { m_changes = m; }

        inline void setMode( int m) { m_mode = m; }
        inline int mode() const { return m_mode; }

        // Since 2.1 public, because plugins may want to access them to:
        TQString findAndProcess( const TQString & token, TQString text, const TQString & replace );

        TQString findNumbers( TQString text, int count, int i );
        TQString findStar( const TQString & oldname, TQString text );
        TQString findBrackets( TQString oldname, TQString text, int i );
        TQString findToken( TQString oldname, TQString token, int i );
        TQString processToken( TQString token, TQString oldname, int i );
        TQString findPartStrings( TQString oldname, TQString token );
        static TQString findDirName( TQString token, TQString path );
        TQString findLength( const TQString & token, const TQString & name );
        TQString findReplace( TQString text );  // text is here already the new filename !
        TQString doReplace( TQString text, TQString find, TQString replace, bool reg ); // text is here already the new filename !

        TQString processString( TQString text, TQString oldname, int i );

        static TQString & doEscape( TQString & text, bool filename = true );
        static TQString & unEscape( TQString & text );
        static void escape( TQString & text, const TQString & token, const TQString & sequence );

        static TQString buildFilename( fileentry* entry, bool dir = true );

    private:
        /** 
         * Returns the length of the string when int n is converted to
         * a string.
         * @param n   a number whose length as string is needed
         * @returns stringlength of n converted to a string 
         */
        int getCharacters( int n ) ;

        void work( ProgressDialog* p );
        void writeUndoScript( TQTextStream* t );
        void parseSubdirs( data* f );
        void findNumberAppendix( TQString & text, int pos, int* start, int* step );
	/** resets all counters to there start value if the directory name at @p i
	 *  in m_files changes.
	 *  The caller has to check m_reset before calling this function.
	 */
        void findCounterReset( int i );
        TQString parsePlugins( int i, const TQString & text, int type );
        bool applyManualChanges( int i );

        TQString text;           // template
        KURL dirname;           // destination dir
        TQString extext;         // Extension template
        TQString m_undoScript;     // Filename of undoscript
        bool undo;              // create an undo script
        int m_index;              // index for numbers
        int m_step;               // step for numbers
	bool m_reset;             // reset counter on new directories
        bool overwrite;         // overwrite existing files
        int m_mode;             // renaming mode
        TQValueList<int> m_skip; // Numbers to skip
        TQValueList<replacestrings> m_replace; // Replace strings
        TQValueList<data> m_files;
        TQValueList<manualchanges> m_changes;  // User made changes
        PluginLoader* plug;

        // a is used in find number and
        // required for skipping.
        int m_counter_index;
	TQValueList<tCounterValues> m_counters;
        
    protected:
        TQFile* f;
        TQTime t;
        TQProgressDialog* progress;
};

#endif