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
|
/*************************************************************************************
* Copyright (C) 2008-2011 by Aleix Pol <aleixpol@kde.org> *
* Copyright (C) 2008-2011 by Alex Fiestas <alex@eyeos.org> *
* Copyright (C) 2013 by Michael G. Hansen <mike@mghansen.de> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#include "kamoso.h"
#include "kamosoSettings.h"
#include "video/webcamcontrol.h"
#include "devicemanager.h"
#include <KIO/Global>
#include <KIO/CopyJob>
#include <KIO/MkpathJob>
#include <KIO/FileUndoManager>
#include <KIO/JobUiDelegate>
#include <KFormat>
#include <KFileUtils>
#include <KJobWidgets/KJobWidgets>
#include <KLocalizedString>
#include <QFile>
#include <QJsonArray>
#include <QDir>
#include <QTemporaryFile>
#include <QIcon>
Kamoso::Kamoso(WebcamControl *webcamControl)
: m_webcamControl(webcamControl)
{
m_recordingTime.restart();
m_recordingTimer.setInterval(30);
connect(m_webcamControl, &WebcamControl::mirroredChanged, this, &Kamoso::mirroredChanged);
connect(m_webcamControl, &WebcamControl::photoTaken, this, &Kamoso::photoTaken);
connect(&m_recordingTimer, &QTimer::timeout, this, &Kamoso::recordingTimeChanged);
}
Kamoso::~Kamoso() = default;
QUrl Kamoso::fileNameSuggestion(const QUrl &saveUrl, const QString &name, const QString& extension) const
{
const QString date = QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd_hh-mm-ss"));
const QString initialName = QStringLiteral("%1_%2.%3").arg(name, date, extension);
QUrl url(saveUrl.toString() + '/' + initialName);
if (url.isLocalFile() && QFile::exists(url.toLocalFile())) {
url.setPath(saveUrl.path() + '/' + KFileUtils::suggestName(saveUrl, initialName));
}
return url;
}
const QString Kamoso::takePhoto()
{
const auto saveUrl = Settings::saveUrl();
if (saveUrl.isLocalFile()) {
qDebug() << "Creating" << saveUrl;
QDir().mkpath(saveUrl.toLocalFile());
}
const QUrl path = fileNameSuggestion(saveUrl, "picture", "jpg");
m_webcamControl->takePhoto(path, true);
if (path.isLocalFile()) {
m_sampleImagePath = path.toLocalFile();
Q_EMIT sampleImageChanged(m_sampleImagePath);
}
return path.toDisplayString();
}
void Kamoso::resetDeviceSettings()
{
Device *device = DeviceManager::self()->playingDevice();
device->reset();
}
void Kamoso::setRecording(bool recording)
{
if (recording == m_recordingTimer.isActive())
return;
if (recording) {
m_webcamControl->startRecording();
m_recordingTime.restart();
m_recordingTimer.start();
auto job = KIO::mkpath(Settings::saveVideos());
job->start();
connect(job, &KJob::finished, this, [this, job] {
if (job->error() == 0) {
return;
}
qWarning() << "Could not create" << Settings::saveVideos();
Q_EMIT error(job->errorString());
});
} else {
const QUrl path = fileNameSuggestion(Settings::saveVideos(), "video", "mkv");
const auto temp = m_webcamControl->stopRecording();
KJob *job = KIO::move(QUrl::fromLocalFile(temp), path);
job->start();
connect(job, &KJob::finished, this, [this, temp, path, job] {
if (job->error() == 0) {
qDebug() << "video saved successfully";
return;
}
qWarning() << "Could not move" << temp << "to" << path;
Q_EMIT error(job->errorString());
});
m_webcamControl->playDevice(DeviceManager::self()->playingDevice());
m_recordingTimer.stop();
}
Q_EMIT isRecordingChanged(m_recordingTimer.isActive());
}
QString Kamoso::recordingTime() const
{
return KFormat().formatDuration(m_recordingTime.elapsed());
}
bool Kamoso::isRecording() const
{
return m_recordingTimer.isActive();
}
void Kamoso::trashFiles(const QJsonArray& urls)
{
QList<QUrl> list;
Q_FOREACH(const QJsonValue& val, urls) {
list += QUrl(val.toString());
}
KIO::Job* job = KIO::trash(list);
KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, list, QUrl("trash:/"), job);
// KJobWidgets::setWindow(job, window);
}
QString Kamoso::sampleImage()
{
if (m_sampleImagePath.isEmpty()) {
QScopedPointer<QTemporaryFile> temporary(new QTemporaryFile(QDir::temp().absoluteFilePath("XXXXXX-sampleimage.jpg"), this));
temporary->open();
m_sampleImagePath = temporary->fileName();
temporary->close();
m_webcamControl->takePhoto(QUrl::fromLocalFile(m_sampleImagePath), false);
}
return m_sampleImagePath;
}
bool Kamoso::mirrored() const
{
return m_webcamControl->mirrored();
}
void Kamoso::setMirrored(bool m)
{
m_webcamControl->setMirrored(m);
}
|