moved notification sounds to notification namespace

This commit is contained in:
Niklas 2019-05-14 17:12:09 +02:00
parent 85f79d1bd1
commit f3fd7453f9
11 changed files with 45 additions and 89 deletions

View File

@ -11,13 +11,11 @@
#include <QTemporaryFile>
#include <QIODevice>
#include <QTextStream>
#include <QMediaPlayer>
#include <QUrl>
#include <logger.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
#include <uploaders/uploadersingleton.hpp>
#include "mainwindow.hpp"
void clipboardcopy::copyClipboard() {
const QClipboard *clipboard = QApplication::clipboard();
@ -40,7 +38,7 @@ void clipboardcopy::copyClipboard() {
UploaderSingleton::inst().upload(file);
} else if (fileInfo.exists() && fileInfo.isReadable() && fileInfo.isDir()) {
notifications::notify("KShare - Directory is not uploadable", fileInfo.absolutePath(), QSystemTrayIcon::Warning);
playErrorSound();
notifications::playSound(notifications::Sound::ERROR);
} else {
QTemporaryFile tmpFile;
tmpFile.setAutoRemove(true);
@ -53,21 +51,11 @@ void clipboardcopy::copyClipboard() {
UploaderSingleton::inst().upload(tmpFile);
} else {
logger::warn("Can not open tmp file");
playErrorSound();
notifications::playSound(notifications::Sound::ERROR);
}
}
} else {
notifications::notify("Unsupported File Format", "Can not upload clipboard", QSystemTrayIcon::Warning);
playErrorSound();
notifications::playSound(notifications::Sound::ERROR);
}
}
void clipboardcopy::playErrorSound() {
QMediaPlayer*mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/errorsound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}

View File

@ -4,7 +4,6 @@
namespace clipboardcopy {
void copyClipboard();
void playErrorSound();
}

View File

@ -120,7 +120,7 @@
<item row="0" column="4">
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="tab_1">
<attribute name="title">

View File

@ -4,6 +4,7 @@
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QMediaPlayer>
void notifications::notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon) {
if (!MainWindow::inst() || !MainWindow::inst()->valid()) return;
@ -21,3 +22,31 @@ void notifications::notifyNolog(QString title, QString body, QSystemTrayIcon::Me
MainWindow::inst()->statusBar()->showMessage(title + ": " + body);
}
void notifications::playSound(notifications::Sound soundType) {
QMediaPlayer*mediaPlayer = new QMediaPlayer(MainWindow::inst());
switch (soundType) {
case notifications::Sound::CAPTURE:
mediaPlayer->setMedia(QUrl("qrc:/capturesound.wav"));
break;
case notifications::Sound::SUCCESS:
mediaPlayer->setMedia(QUrl("qrc:/successsound.wav"));
break;
case notifications::Sound::ERROR:
mediaPlayer->setMedia(QUrl("qrc:/errorsound.wav"));
break;
default:
break;
}
mediaPlayer->setVolume(25);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}

View File

@ -5,8 +5,10 @@
#include <QSystemTrayIcon>
namespace notifications {
enum class Sound { ERROR = 0, SUCCESS = 1, CAPTURE = 2 };
void notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information);
void notifyNolog(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information);
void playSound(notifications::Sound soundType);
} // namespace notifications
#endif // NOTIFICATIONS_HPP

View File

@ -12,8 +12,6 @@
#include <formatter.hpp>
#include <io/ioutils.hpp>
#include <notifications.hpp>
#include <QMediaPlayer>
#include "mainwindow.hpp"
using formats::normalFormatFromName;
using formats::normalFormatMIME;
@ -214,7 +212,7 @@ void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteAr
if (result.isObject()) {
QString url
= formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, "");
playSuccessSound();
notifications::playSound(notifications::Sound::SUCCESS);
if (!url.isEmpty()) {
QApplication::clipboard()->setText(url);
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!"));
@ -225,7 +223,7 @@ void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteAr
ioutils::addLogEntry(r, data, "", filename);
}
} else {
playErrorSound();
notifications::playSound(notifications::Sound::ERROR);
notifications::notify(tr("KShare Custom Uploader ") + name,
tr("Upload done, but result is not JSON Object! Result in clipboard."));
QApplication::clipboard()->setText(data);
@ -349,7 +347,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
QApplication::clipboard()->setText(QString::fromUtf8(result));
for (auto buffer : buffersToDelete) buffer->deleteLater();
for (auto arr : arraysToDelete) delete arr;
playSuccessSound();
notifications::playSound(notifications::Sound::SUCCESS);
notifications::notify(tr("KShare Custom Uploader ") + name(),
tr("Copied upload result to clipboard!"));
});
@ -367,7 +365,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
}
}
if (limit > 0 && data.size() > limit) {
playErrorSound();
notifications::playSound(notifications::Sound::ERROR);
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("File limit exceeded!"));
return;
}
@ -377,7 +375,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
ioutils::postData(target, h, data, [&, filename](QByteArray result, QNetworkReply *r) {
ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename);
QApplication::clipboard()->setText(QString::fromUtf8(result));
playSuccessSound();
notifications::playSound(notifications::Sound::SUCCESS);
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("Copied upload result to clipboard!"));
});
} else {
@ -388,23 +386,3 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
break;
}
}
void CustomUploader::playSuccessSound() {
QMediaPlayer* mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/successsound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}
void CustomUploader::playErrorSound() {
QMediaPlayer* mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/errorsound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}

View File

@ -34,8 +34,6 @@ private:
QString returnPathspec;
QString urlPrepend, urlAppend;
void parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename);
void playSuccessSound();
void playErrorSound();
};
#endif // CUSTOMUPLOADER_HPP

View File

@ -11,8 +11,6 @@
#include <notifications.hpp>
#include <settings.hpp>
#include <utils.hpp>
#include <QMediaPlayer>
#include "mainwindow.hpp"
struct SegfaultWorkaround { // I'm a scrub for doing this
SegfaultWorkaround(QByteArray a, ImgurUploader *u, QString m) : byteArray(), dis(u), mime(m) {
@ -94,14 +92,14 @@ void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray,
ioutils::addLogEntry(r, data, result, filename);
utils::toClipboard(result);
notifications::notify(tr("KShare imgur Uploader"), tr("Uploaded to imgur!"));
playSuccessSound();
notifications::playSound(notifications::Sound::SUCCESS);
} else {
ioutils::addLogEntry(r, data, result, filename);
notifications::notify(tr("KShare imgur Uploader "),
QString(tr("Failed upload! imgur said: HTTP %1: %2"))
.arg(r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt())
.arg(r->errorString()));
playErrorSound();
notifications::playSound(notifications::Sound::ERROR);
}
});
}
@ -109,23 +107,3 @@ void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray,
void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
handleSend(auth, mime, byteArray);
}
void ImgurUploader::playSuccessSound() {
QMediaPlayer* mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/successsound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}
void ImgurUploader::playErrorSound() {
QMediaPlayer*mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/errorsound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}

View File

@ -21,8 +21,6 @@ public:
private:
void handleSend(QString auth, QString mime, QByteArray byteArray, QString filename);
void handleSend(QString auth, QString mime, QByteArray byteArray);
void playSuccessSound();
void playErrorSound();
};
#endif // IMGURUPLOADER_HPP

View File

@ -70,7 +70,7 @@ void UploaderSingleton::upload(QPixmap pixmap) {
file = new QTemporaryFile();
}
if (file->open(QFile::ReadWrite)) {
playSound();
notifications::playSound(notifications::Sound::CAPTURE);
pixmap.save(file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt());
file->seek(0);
QFileInfo fileInfo(file->fileName());
@ -92,7 +92,7 @@ void UploaderSingleton::upload(QByteArray img, QString format) {
file = new QTemporaryFile();
}
if (file->open(QFile::WriteOnly)) {
playSound();
notifications::playSound(notifications::Sound::CAPTURE);
file->write(img);
file->close();
}
@ -107,7 +107,7 @@ void UploaderSingleton::upload(QFile &img, QString format) {
if (!saveImages || img.rename(saveDir.absoluteFilePath(
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower())))) {
playSound();
notifications::playSound(notifications::Sound::CAPTURE);
QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite))
uploaders.value(uploader)->doUpload(img.readAll(), format, fileInfo.fileName());
@ -186,14 +186,3 @@ void UploaderSingleton::updateSaveSettings() {
}
}
}
void UploaderSingleton::playSound() {
mediaPlayer = new QMediaPlayer(MainWindow::inst());
mediaPlayer->setMedia(QUrl("qrc:/capturesound.wav"));
mediaPlayer->setVolume(50);
mediaPlayer->play();
if(mediaPlayer->error() != QMediaPlayer::NoError && mediaPlayer->error() != QMediaPlayer::ServiceMissingError)
notifications::notify(QString::number(mediaPlayer->error()), mediaPlayer->errorString(), QSystemTrayIcon::Warning);
}

View File

@ -4,7 +4,6 @@
#include "uploader.hpp"
#include <QDir>
#include <QMap>
#include <QMediaPlayer>
class UploaderSingleton : public QObject {
Q_OBJECT
@ -32,12 +31,10 @@ signals:
private:
void updateSaveSettings();
void playSound();
QDir saveDir;
bool saveImages = true;
QMap<QString, Uploader *> uploaders;
QString uploader = "imgur";
QMediaPlayer *mediaPlayer;
QList<std::runtime_error> errs;
UploaderSingleton();
};