1 /* QWinFF - a qt4 gui frontend for ffmpeg
2 * Copyright (C) 2011-2013 Timothy Lin <lzh9102@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "filepathoperations.h"
19 #include <QCoreApplication>
20
FilePathOperations()21 FilePathOperations::FilePathOperations()
22 {
23 }
24
GenerateUniqueFileName(const QDir & output_dir,const QString & input_file_basename,const QString & ext,const QHash<QString,int> & extra)25 QString FilePathOperations::GenerateUniqueFileName(const QDir& output_dir, const QString& input_file_basename
26 , const QString& ext, const QHash<QString, int>& extra)
27 {
28 int filename_index = 1;
29 QString result;
30 do {
31 // The index part of the file
32 QString str_index("");
33 if (filename_index > 1) {
34 // If the index is larger than 1, append -index to the filename.
35 str_index = QString("-%1").arg(filename_index);
36 }
37
38 // Fill in output filename.
39 result =
40 output_dir.absoluteFilePath(input_file_basename) // filename
41 + str_index // index
42 + '.' // point
43 + ext; // extension
44
45 ++filename_index;
46 } while (QFileInfo(result).exists()
47 || extra.contains(result)); // If file(n) exists, try file(n+1).
48 return result;
49 }
50
GenerateUniqueFileName(const QString & filename,const QHash<QString,int> & extra)51 QString FilePathOperations::GenerateUniqueFileName(const QString &filename, const QHash<QString, int>& extra)
52 {
53 QDir dir = QFileInfo(filename).dir();
54 QString basename = QFileInfo(filename).completeBaseName();
55 QString ext = QFileInfo(filename).suffix();
56 return GenerateUniqueFileName(dir, basename, ext, extra);
57 }
58
GenerateTempFileName(const QString & filename)59 QString FilePathOperations::GenerateTempFileName(const QString& filename)
60 {
61 QString result;
62 do {
63 // Generate temporary file name.
64 result = QString("%1-%2-temp-%3.%4").arg(filename)
65 .arg(qrand()).arg(QCoreApplication::applicationPid())
66 .arg(QFileInfo(filename).suffix());
67 } while (QFileInfo(result).exists()); // Regenerate if exists.
68
69 return result;
70 }
71