~ubuntu-branches/ubuntu/wily/kid3/wily-proposed

« back to all changes in this revision

Viewing changes to src/core/model/dirrenamer.h

  • Committer: Package Import Robot
  • Author(s): Ana Beatriz Guerrero Lopez, Patrick Matthäi, Ana Beatriz Guerrero Lopez
  • Date: 2011-11-13 16:34:13 UTC
  • mfrom: (1.1.13) (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20111113163413-5y0anlc4dqf511uh
Tags: 2.0.1-1
* New upstream release.

[ Patrick Matthäi ]
* Adjust build system.
* Add build dependency xsltproc.

[ Ana Beatriz Guerrero Lopez ]
* Some more adjustments to the build system taken from upstream's deb/
* directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file dirrenamer.h
 
3
 * Directory renamer.
 
4
 *
 
5
 * \b Project: Kid3
 
6
 * \author Urs Fleisch
 
7
 * \date 23 Jul 2011
 
8
 *
 
9
 * Copyright (C) 2011  Urs Fleisch
 
10
 *
 
11
 * This file is part of Kid3.
 
12
 *
 
13
 * Kid3 is free software; you can redistribute it and/or modify
 
14
 * it under the terms of the GNU General Public License as published by
 
15
 * the Free Software Foundation; either version 2 of the License, or
 
16
 * (at your option) any later version.
 
17
 *
 
18
 * Kid3 is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
 * GNU General Public License for more details.
 
22
 *
 
23
 * You should have received a copy of the GNU General Public License
 
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
25
 */
 
26
 
 
27
#ifndef DIRRENAMER_H
 
28
#define DIRRENAMER_H
 
29
 
 
30
#include <QObject>
 
31
#include <QString>
 
32
#include "trackdata.h"
 
33
 
 
34
class TaggedFile;
 
35
 
 
36
/**
 
37
 * Directory renamer.
 
38
 */
 
39
class DirRenamer : public QObject {
 
40
public:
 
41
  /**
 
42
   * Constructor.
 
43
   * @param parent parent object
 
44
   */
 
45
  explicit DirRenamer(QObject* parent = 0);
 
46
 
 
47
  /**
 
48
   * Destructor.
 
49
   */
 
50
  virtual ~DirRenamer();
 
51
 
 
52
  /**
 
53
   * Set version of tags used to get rename information.
 
54
   * @param tagVersion tag version
 
55
   */
 
56
  void setTagVersion(TrackData::TagVersion tagVersion) {
 
57
    m_tagVersion = tagVersion;
 
58
  }
 
59
 
 
60
  /**
 
61
   * Set action to be performed.
 
62
   * @param create true for create action
 
63
   */
 
64
  void setAction(bool create) { m_actionCreate = create; }
 
65
 
 
66
  /**
 
67
   * Set format to generate directory names.
 
68
   * @param format format
 
69
   */
 
70
  void setFormat(const QString& format) { m_format = format; }
 
71
 
 
72
  /**
 
73
   * Generate new directory name according to current settings.
 
74
   *
 
75
   * @param taggedFile file to get information from
 
76
   * @param olddir pointer to QString to place old directory name into,
 
77
   *               NULL if not used
 
78
   *
 
79
   * @return new directory name.
 
80
   */
 
81
  QString generateNewDirname(TaggedFile* taggedFile, QString* olddir);
 
82
 
 
83
  /**
 
84
   * Clear the rename actions.
 
85
   * This method has to be called before scheduling new actions.
 
86
   */
 
87
  void clearActions();
 
88
 
 
89
  /**
 
90
   * Schedule the actions necessary to rename the directory containing a file.
 
91
   *
 
92
   * @param taggedFile file in directory
 
93
   */
 
94
  void scheduleAction(TaggedFile* taggedFile);
 
95
 
 
96
  /**
 
97
   * Perform the scheduled rename actions.
 
98
   *
 
99
   * @param errorMsg if not 0 and an error occurred, a message is appended here,
 
100
   *                 otherwise it is not touched
 
101
   */
 
102
  void performActions(QString* errorMsg);
 
103
 
 
104
  /**
 
105
   * Get description of actions to be performed.
 
106
   * @return list of (action, [src,] dst) lists describing the actions to be
 
107
   * performed.
 
108
   */
 
109
  QList<QStringList> describeActions() const;
 
110
 
 
111
  /**
 
112
   * Check if dialog was aborted.
 
113
   * @return true if aborted.
 
114
   */
 
115
  bool getAbortFlag() const { return m_aborted; }
 
116
 
 
117
  /**
 
118
   * Set abort flag.
 
119
   */
 
120
  void setAbortFlag() { m_aborted = true; }
 
121
 
 
122
  /**
 
123
   * Set directory name.
 
124
   * This should be done before calling performActions(), so that the directory
 
125
   * name is changed when the application directory is renamed.
 
126
   *
 
127
   * @param dirName directory name
 
128
   */
 
129
  void setDirName(const QString& dirName) { m_dirName = dirName; }
 
130
 
 
131
  /**
 
132
   * Get directory name.
 
133
   * The directory name should be initialized with the value of
 
134
   * Kid3Application::getDirName() before performActions() is started and will
 
135
   * be updated if it is renamed while performing the actions. If it is
 
136
   * different from the application directory name after performActions(), the
 
137
   * new directory should be opened.
 
138
   *
 
139
   * @return directory.
 
140
   */
 
141
  QString getDirName() const { return m_dirName; }
 
142
 
 
143
private:
 
144
  /**
 
145
   * An action performed while renaming a directory.
 
146
   */
 
147
  class RenameAction {
 
148
  public:
 
149
    /** Action type. */
 
150
    enum Type {
 
151
      CreateDirectory,
 
152
      RenameDirectory,
 
153
      RenameFile,
 
154
      ReportError,
 
155
      NumTypes
 
156
    };
 
157
 
 
158
    /**
 
159
     * Constructor.
 
160
     * @param type type of action
 
161
     * @param src  source file or directory name
 
162
     * @param dest destination file or directory name
 
163
     */
 
164
    RenameAction(Type type, const QString& src, const QString& dest) :
 
165
      m_type(type), m_src(src), m_dest(dest) {}
 
166
 
 
167
    /**
 
168
     * Constructor.
 
169
     */
 
170
    RenameAction() : m_type(ReportError) {}
 
171
 
 
172
    /**
 
173
     * Destructor.
 
174
     */
 
175
    ~RenameAction() {}
 
176
 
 
177
    /**
 
178
     * Test for equality.
 
179
     * @param rhs right hand side
 
180
     * @return true if equal.
 
181
     */
 
182
    bool operator==(const RenameAction& rhs) const {
 
183
      return m_type == rhs.m_type && m_src == rhs.m_src && m_dest == rhs.m_dest;
 
184
    }
 
185
 
 
186
    Type m_type;    /**< type of action */
 
187
    QString m_src;  /**< source file or directory name */
 
188
    QString m_dest; /**< destination file or directory name */
 
189
  };
 
190
 
 
191
  /** List of rename actions. */
 
192
  typedef QList<RenameAction> RenameActionList;
 
193
 
 
194
  /**
 
195
   * Create a directory if it does not exist.
 
196
   *
 
197
   * @param dir      directory path
 
198
   * @param errorMsg if not NULL and an error occurred, a message is appended here,
 
199
   *                 otherwise it is not touched
 
200
   *
 
201
   * @return true if directory exists or was created successfully.
 
202
   */
 
203
  bool createDirectory(const QString& dir, QString* errorMsg) const;
 
204
 
 
205
  /**
 
206
   * Rename a directory.
 
207
   *
 
208
   * @param olddir   old directory name
 
209
   * @param newdir   new directory name
 
210
   * @param errorMsg if not NULL and an error occurred, a message is
 
211
   *                 appended here, otherwise it is not touched
 
212
   *
 
213
   * @return true if rename successful.
 
214
   */
 
215
  bool renameDirectory(
 
216
    const QString& olddir, const QString& newdir, QString* errorMsg) const;
 
217
 
 
218
  /**
 
219
   * Rename a file.
 
220
   *
 
221
   * @param oldfn    old file name
 
222
   * @param newfn    new file name
 
223
   * @param errorMsg if not NULL and an error occurred, a message is
 
224
   *                 appended here, otherwise it is not touched
 
225
   *
 
226
   * @return true if rename successful or newfn already exists.
 
227
   */
 
228
  bool renameFile(const QString& oldfn, const QString& newfn,
 
229
                  QString* errorMsg) const;
 
230
 
 
231
  /**
 
232
   * Add a rename action.
 
233
   *
 
234
   * @param type type of action
 
235
   * @param src  source file or directory name
 
236
   * @param dest destination file or directory name
 
237
   */
 
238
  void addAction(RenameAction::Type type, const QString& src,
 
239
                 const QString& dest);
 
240
 
 
241
  /**
 
242
   * Add a rename action.
 
243
   *
 
244
   * @param type type of action
 
245
   * @param dest destination file or directory name
 
246
   */
 
247
  void addAction(RenameAction::Type type, const QString& dest);
 
248
 
 
249
  /**
 
250
   * Check if there is already an action scheduled for this source.
 
251
   *
 
252
   * @return true if a rename action for the source exists.
 
253
   */
 
254
  bool actionHasSource(const QString& src) const;
 
255
 
 
256
  /**
 
257
   * Check if there is already an action scheduled for this destination.
 
258
   *
 
259
   * @return true if a rename or create action for the destination exists.
 
260
   */
 
261
  bool actionHasDestination(const QString& dest) const;
 
262
 
 
263
  /**
 
264
   * Replace directory name if there is already a rename action.
 
265
   *
 
266
   * @param src directory name, will be replaced if there is a rename action
 
267
   */
 
268
  void replaceIfAlreadyRenamed(QString& src) const;
 
269
 
 
270
  RenameActionList m_actions;
 
271
  bool m_aborted;
 
272
  TrackData::TagVersion m_tagVersion;
 
273
  bool m_actionCreate;
 
274
  QString m_format;
 
275
  QString m_dirName;
 
276
};
 
277
 
 
278
#endif // DIRRENAMER_H