summaryrefslogtreecommitdiffstats
path: root/src/convert.h
blob: cd4fc5eaa62274fa98dc5b8749f963abeaf57810 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282


#ifndef CONVERT_H
#define CONVERT_H

#include <tdeio/jobclasses.h>

#include <tqobject.h>
#include <tqvaluelist.h>

#include <kprocess.h>

class Config;
class TagEngine;
class CDManager;
class FileList;
class FileListItem;
class ReplayGain;
class Logger;
class KTempFile;
//class TDEProcess;

/**
 * @short The items for the conversion (for every active file)
 * @author Daniel Faust <hessijames@gmail.com>
 * @version 0.3
 */
class ConvertItem
{
public:
    /**
     * A list of flags for knowing what to do
     */
    enum Mode {
        get               = 0x0001, // Copy the file to tmp
        get_correction    = 0x0002, // Copy the correction file to tmp
        rip               = 0x0004, // Rip the file
        decode            = 0x0008, // Decode the file
        encode            = 0x0010, // Encode the file
        replaygain        = 0x0020, // Apply replaygain
        write_tags        = 0x0040, // Write the tags to the file
        put               = 0x0080, // Move the file to the output directory
        put_correction    = 0x0100, // Move the correction file to the output directory
        execute_userscript= 0x0200  // Run the user script
    };

    /**
     * Default Constructor
     */
    ConvertItem();

    /**
     * Constructor
     * @p item A pointer to the file list item
     */
    ConvertItem( FileListItem* item );

    /**
     * Destructor
     */
    virtual ~ConvertItem();

    /** a reference to the file list item */
    FileListItem* fileListItem;

    /** for adding replay gain */
    ReplayGain* replayGain;

    /** if we need to encode, decode, etc. here we have our processes */
    TDEProcess* convertProcess;
    /** for moving the file to the temporary or output directory */
    TDEIO::Job* moveJob;

    KTempFile* tempInFile;
    KTempFile* tempWavFile;
    KTempFile* tempOutFile;

    TQString correctionInFile;
    TQString correctionOutFile;
    TQString correctionInputExtension;
    TQString correctionOutputExtension;

    //TQTime readOutputTimer;
    TQTime lastOutputTimer;

    /** what shall we do with the file? */
    Mode mode;
    /** and what are we doing with the file? */
    Mode state;

    /** the id with that the item is registered at the logger */
    int logID;
    /** the binary for special treatment */
//     TQString binary;

    /** the path and the name of the output file (needed for executing a command after conversion) */
    TQString outputFilePathName;

    /** if it is an audio cd and it should be ripped to one file: the number of tracks on the cd */
    int tracks;
    /** the current track */
    int track;
    int lastPercent;

    /** the time from the file list item splitted up */
    float getTime;
    float getCorrectionTime;
    float ripTime;
    float decodeTime;
    float encodeTime;
    float replaygainTime;
    /** the current conversion progress */
    int percent;
};


/**
 * @short The conversion engine
 * @author Daniel Faust <hessijames@gmail.com>
 * @version 0.3
 */
class Convert : public TQObject
{
    Q_OBJECT
  
public:
    /**
     * Constructor
     */
    Convert( Config*, TagEngine*, CDManager*, FileList*, Logger* );

    /**
     * Destructor
     */
    virtual ~Convert();

    void cleanUp();

private:
    /**
     * Copy the file with the file list item @p item to a temporary directory and download or rip, when necessary
     */
    void get( ConvertItem* item );

     /**
     * Copy the correction file with the file list item @p item to a temporary directory and download or rip, when necessary
     */
    void getCorrection( ConvertItem* item );

   /**
     * Rip the file with the convert item @p item from the CD
     */
    void rip( ConvertItem* item );

    /**
     * Decode the file with the convert item @p item
     */
    void decode( ConvertItem* item );

    /**
     * Encode the file with the convert item @p item
     */
    void encode( ConvertItem* item );

    /**
     * Calculate replaygain tags of the file with the convert item @p item
     */
    void replaygain( ConvertItem* item );

    /**
     * Write the tags of the file with the convert item @p item
     */
    void writeTags( ConvertItem* item );

    /**
     * Copy the file with the convert item @p item to the output directory
     */
    void put( ConvertItem* item );

    /**
     * Copy the correction file with the convert item @p item to the output directory
     */
    void putCorrection( ConvertItem* item );

    /**
     * Run the userscript for the convert item @p item
     */
    void executeUserScript( ConvertItem* item );

    /**
     * Decide, what to do next with out item @p item
     */
    void executeNextStep( ConvertItem* item );

    /**
     * Remove item @p item and emit the state @p state
     */
    void remove( ConvertItem* item, int state = 0 );

    /** holds all active files */
    TQValueList<ConvertItem*> items;

    Config* config;
    TagEngine* tagEngine;
    CDManager* cdManager;
    FileList* fileList;
    Logger* logger;
    TQTimer* tUpdateProgressIndicator;
    TDEProcess notify;

private slots:
    /**
     * The file is being moved
     * @p job The pinter to the job
     */
    void moveProgress( TDEIO::Job* job, unsigned long percent );

    /**
     * The file has been moved
     * @p job The pinter to the job
     */
    void moveFinished( TDEIO::Job* job );

    /**
     * Get the process' output
     * @p proc The pinter to the progess
     * @p data The output data
     * @p length The length of the data
     */
    void processOutput( TDEProcess *proc, char *data, int length );

    /**
     * The process has exited
     * @p proc The pinter to the progess
     */
    void processExit( TDEProcess *proc );

    /**
     * Updates the progress indicator
     */
    void updateProgressIndicator();

public slots:
    /**
     * Add a new @p item to the item list and start
     */
    void add( FileListItem* item );

    /**
     * Stop the item with the file list item @p item in the item list and remove it
     */
    void stop( FileListItem* item );

    /**
     * Change the process priorities
     */
//     void priorityChanged( int );

signals:
    /**
     * A job was completed
     * The job with the file list item @p item was completed
     * And report the finish @p state ( 0 = ok, -1 = error, 1 = aborted )
     */
    void finished( FileListItem* item, int state );

    /**
     * Send the logger a signal
     */
    void finishedProcess( int id, int state );

    /**
     * The next track from @p device can be ripped while the track is being encoded
     */
    void rippingFinished( const TQString& device );

    void countTime( float );
    void uncountTime( float );
    void update( float );
};

#endif // CONVERT_H