1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qmediastoragelocation_p.h"
5
6#include <QStandardPaths>
7
8QT_BEGIN_NAMESPACE
9
10QDir QMediaStorageLocation::defaultDirectory(QStandardPaths::StandardLocation type)
11{
12 QStringList dirCandidates;
13
14#if QT_CONFIG(mmrenderer)
15 dirCandidates << QLatin1String("shared/camera");
16#endif
17
18 dirCandidates << QStandardPaths::writableLocation(type);
19 dirCandidates << QStandardPaths::writableLocation(type: QStandardPaths::DocumentsLocation);
20 dirCandidates << QDir::homePath();
21 dirCandidates << QDir::currentPath();
22 dirCandidates << QDir::tempPath();
23
24 for (const QString &path : std::as_const(t&: dirCandidates)) {
25 QDir dir(path);
26 if (dir.exists() && QFileInfo(path).isWritable())
27 return dir;
28 }
29
30 return QDir();
31}
32
33static QString generateFileName(const QDir &dir, const QString &prefix, const QString &extension)
34{
35 auto lastMediaIndex = 0;
36 const auto list = dir.entryList(nameFilters: { QString::fromLatin1(ba: "%1*.%2").arg(args: prefix, args: extension) });
37 for (const QString &fileName : list) {
38 auto mediaIndex = QStringView{fileName}.mid(pos: prefix.size(), n: fileName.size() - prefix.size() - extension.size() - 1).toInt();
39 lastMediaIndex = qMax(a: lastMediaIndex, b: mediaIndex);
40 }
41
42 const QString name = QString::fromLatin1(ba: "%1%2.%3")
43 .arg(a: prefix)
44 .arg(a: lastMediaIndex + 1, fieldWidth: 4, base: 10, fillChar: QLatin1Char('0'))
45 .arg(a: extension);
46
47 return dir.absoluteFilePath(fileName: name);
48}
49
50
51QString QMediaStorageLocation::generateFileName(const QString &requestedName,
52 QStandardPaths::StandardLocation type,
53 const QString &extension)
54{
55 auto prefix = QLatin1String("clip_");
56 switch (type) {
57 case QStandardPaths::PicturesLocation: prefix = QLatin1String("image_"); break;
58 case QStandardPaths::MoviesLocation: prefix = QLatin1String("video_"); break;
59 case QStandardPaths::MusicLocation: prefix = QLatin1String("record_"); break;
60 default: break;
61 }
62
63 if (requestedName.isEmpty())
64 return generateFileName(dir: defaultDirectory(type), prefix, extension);
65
66 QString path = requestedName;
67
68 if (QFileInfo(path).isRelative())
69 path = defaultDirectory(type).absoluteFilePath(fileName: path);
70
71 if (QFileInfo(path).isDir())
72 return generateFileName(dir: QDir(path), prefix, extension);
73
74 if (!path.endsWith(s: extension))
75 path.append(s: QString(QLatin1String(".%1")).arg(a: extension));
76
77 return path;
78}
79
80QT_END_NAMESPACE
81

source code of qtmultimedia/src/multimedia/qmediastoragelocation.cpp