File indexing completed on 2024-05-12 17:21:22

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
0003  * SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 #include "recording.h"
0009 
0010 #include <QFile>
0011 #include <QStandardPaths>
0012 #include <QJsonObject>
0013 #include <QDebug>
0014 #include <QJsonDocument>
0015 #include <QJsonArray>
0016 #include <QRegularExpression>
0017 #include "utils.h"
0018 
0019 Recording::Recording(QObject *parent, const QString &filePath, const QString &fileName, QDateTime recordDate, int recordingLength)
0020     : QObject{ parent }
0021     , m_filePath{ filePath }
0022     , m_fileName{ fileName }
0023     , m_recordDate{ recordDate }
0024     , m_recordingLength{ recordingLength }
0025 {}
0026 
0027 Recording::Recording(QObject *parent, const QJsonObject &obj)
0028     : QObject{ parent }
0029     , m_filePath{ obj[QStringLiteral("filePath")].toString() }
0030     , m_fileName{ obj[QStringLiteral("fileName")].toString() }
0031     , m_recordDate{ QDateTime::fromString(obj[QStringLiteral("recordDate")].toString(), Qt::DateFormat::ISODate) }
0032     , m_recordingLength{ obj[QStringLiteral("recordingLength")].toInt() }
0033 {}
0034 
0035 QJsonObject Recording::toJson() const
0036 {
0037     QJsonObject obj;
0038     obj[QStringLiteral("filePath")] = m_filePath;
0039     obj[QStringLiteral("fileName")] = m_fileName;
0040     obj[QStringLiteral("recordDate")] = m_recordDate.toString(Qt::DateFormat::ISODate);
0041     obj[QStringLiteral("recordingLength")] = m_recordingLength;
0042     return obj;
0043 }
0044 
0045 QString Recording::filePath() const
0046 {
0047     return m_filePath;
0048 }
0049 
0050 QString Recording::fileName() const
0051 {
0052     return m_fileName;
0053 }
0054 
0055 QString Recording::fileExtension() const
0056 {
0057     auto split = m_filePath.split(QStringLiteral("."));
0058     if (split.length() > 0) {
0059         return split[split.length() - 1];
0060     }
0061     return QString{};
0062 }
0063 
0064 QDateTime Recording::recordDate() const
0065 {
0066     return m_recordDate;
0067 }
0068 
0069 QString Recording::recordDatePretty() const
0070 {
0071     return m_recordDate.toString(QStringLiteral("yyyy-MM-dd"));
0072 }
0073 
0074 int Recording::recordingLength() const
0075 {
0076     return m_recordingLength;
0077 }
0078 
0079 QString Recording::recordingLengthPretty() const
0080 {
0081     const int min = m_recordingLength / 60;
0082     const int sec = m_recordingLength - min * 60;
0083     return QStringLiteral("%1:%2").arg(min).arg(sec, 2, 10, QLatin1Char('0'));
0084 }
0085 
0086 void Recording::setFilePath(const QString &filePath)
0087 {
0088     QFile(m_filePath).rename(filePath);
0089     m_filePath = filePath;
0090 
0091     QStringList spl = filePath.split(QStringLiteral("/"));
0092     m_fileName = spl[spl.size()-1].split(QStringLiteral("."))[0];
0093 
0094     Q_EMIT propertyChanged();
0095 }
0096 
0097 void Recording::setFileName(const QString &fileName)
0098 {
0099     QString oldPath = m_filePath;
0100 
0101     m_filePath.replace(QRegularExpression(m_fileName + QStringLiteral("(?!.*") + m_fileName + QStringLiteral(")")), fileName);
0102 
0103     QFile(oldPath).rename(m_filePath);
0104 
0105     m_fileName = fileName;
0106     Q_EMIT propertyChanged();
0107 }
0108 
0109 void Recording::setRecordDate(const QDateTime &date)
0110 {
0111     m_recordDate = date;
0112     Q_EMIT propertyChanged();
0113 }
0114 
0115 void Recording::setRecordingLength(int recordingLength)
0116 {
0117     m_recordingLength = recordingLength;
0118     Q_EMIT propertyChanged();
0119 }
0120 
0121 void Recording::createCopyOfFile(const QString &path)
0122 {
0123     QFile::copy(m_filePath, path);
0124 }