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 <QTemporaryFile>
#include <QIODevice> #include <QIODevice>
#include <QTextStream> #include <QTextStream>
#include <QMediaPlayer>
#include <QUrl> #include <QUrl>
#include <logger.hpp> #include <logger.hpp>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
#include <notifications.hpp> #include <notifications.hpp>
#include <uploaders/uploadersingleton.hpp> #include <uploaders/uploadersingleton.hpp>
#include "mainwindow.hpp"
void clipboardcopy::copyClipboard() { void clipboardcopy::copyClipboard() {
const QClipboard *clipboard = QApplication::clipboard(); const QClipboard *clipboard = QApplication::clipboard();
@ -40,7 +38,7 @@ void clipboardcopy::copyClipboard() {
UploaderSingleton::inst().upload(file); UploaderSingleton::inst().upload(file);
} else if (fileInfo.exists() && fileInfo.isReadable() && fileInfo.isDir()) { } else if (fileInfo.exists() && fileInfo.isReadable() && fileInfo.isDir()) {
notifications::notify("KShare - Directory is not uploadable", fileInfo.absolutePath(), QSystemTrayIcon::Warning); notifications::notify("KShare - Directory is not uploadable", fileInfo.absolutePath(), QSystemTrayIcon::Warning);
playErrorSound(); notifications::playSound(notifications::Sound::ERROR);
} else { } else {
QTemporaryFile tmpFile; QTemporaryFile tmpFile;
tmpFile.setAutoRemove(true); tmpFile.setAutoRemove(true);
@ -53,21 +51,11 @@ void clipboardcopy::copyClipboard() {
UploaderSingleton::inst().upload(tmpFile); UploaderSingleton::inst().upload(tmpFile);
} else { } else {
logger::warn("Can not open tmp file"); logger::warn("Can not open tmp file");
playErrorSound(); notifications::playSound(notifications::Sound::ERROR);
} }
} }
} else { } else {
notifications::notify("Unsupported File Format", "Can not upload clipboard", QSystemTrayIcon::Warning); 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 { namespace clipboardcopy {
void copyClipboard(); void copyClipboard();
void playErrorSound();
} }

View File

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

View File

@ -4,6 +4,7 @@
#include "mainwindow.hpp" #include "mainwindow.hpp"
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include <QApplication> #include <QApplication>
#include <QMediaPlayer>
void notifications::notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon) { void notifications::notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon) {
if (!MainWindow::inst() || !MainWindow::inst()->valid()) return; 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); 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> #include <QSystemTrayIcon>
namespace notifications { namespace notifications {
enum class Sound { ERROR = 0, SUCCESS = 1, CAPTURE = 2 };
void notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information); void notify(QString title, QString body, QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::Information);
void notifyNolog(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 } // namespace notifications
#endif // NOTIFICATIONS_HPP #endif // NOTIFICATIONS_HPP

View File

@ -12,8 +12,6 @@
#include <formatter.hpp> #include <formatter.hpp>
#include <io/ioutils.hpp> #include <io/ioutils.hpp>
#include <notifications.hpp> #include <notifications.hpp>
#include <QMediaPlayer>
#include "mainwindow.hpp"
using formats::normalFormatFromName; using formats::normalFormatFromName;
using formats::normalFormatMIME; using formats::normalFormatMIME;
@ -214,7 +212,7 @@ void CustomUploader::parseResult(QNetworkReply *r, QJsonDocument result, QByteAr
if (result.isObject()) { if (result.isObject()) {
QString url QString url
= formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, ""); = formatter::format(urlPrepend, "") + parsePathspec(result, returnPathspec) + formatter::format(urlAppend, "");
playSuccessSound(); notifications::playSound(notifications::Sound::SUCCESS);
if (!url.isEmpty()) { if (!url.isEmpty()) {
QApplication::clipboard()->setText(url); QApplication::clipboard()->setText(url);
notifications::notify(tr("KShare Custom Uploader ") + name, tr("Copied upload link to clipboard!")); 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); ioutils::addLogEntry(r, data, "", filename);
} }
} else { } else {
playErrorSound(); notifications::playSound(notifications::Sound::ERROR);
notifications::notify(tr("KShare Custom Uploader ") + name, notifications::notify(tr("KShare Custom Uploader ") + name,
tr("Upload done, but result is not JSON Object! Result in clipboard.")); tr("Upload done, but result is not JSON Object! Result in clipboard."));
QApplication::clipboard()->setText(data); QApplication::clipboard()->setText(data);
@ -349,7 +347,7 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
QApplication::clipboard()->setText(QString::fromUtf8(result)); QApplication::clipboard()->setText(QString::fromUtf8(result));
for (auto buffer : buffersToDelete) buffer->deleteLater(); for (auto buffer : buffersToDelete) buffer->deleteLater();
for (auto arr : arraysToDelete) delete arr; for (auto arr : arraysToDelete) delete arr;
playSuccessSound(); notifications::playSound(notifications::Sound::SUCCESS);
notifications::notify(tr("KShare Custom Uploader ") + name(), notifications::notify(tr("KShare Custom Uploader ") + name(),
tr("Copied upload result to clipboard!")); 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) { if (limit > 0 && data.size() > limit) {
playErrorSound(); notifications::playSound(notifications::Sound::ERROR);
notifications::notify(tr("KShare Custom Uploader ") + name(), tr("File limit exceeded!")); notifications::notify(tr("KShare Custom Uploader ") + name(), tr("File limit exceeded!"));
return; 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::postData(target, h, data, [&, filename](QByteArray result, QNetworkReply *r) {
ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename); ioutils::addLogEntry(r, result, QString::fromUtf8(result), filename);
QApplication::clipboard()->setText(QString::fromUtf8(result)); 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!")); notifications::notify(tr("KShare Custom Uploader ") + name(), tr("Copied upload result to clipboard!"));
}); });
} else { } else {
@ -388,23 +386,3 @@ void CustomUploader::doUpload(QByteArray imgData, QString format, QString filena
break; 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 returnPathspec;
QString urlPrepend, urlAppend; QString urlPrepend, urlAppend;
void parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename); void parseResult(QNetworkReply *r, QJsonDocument result, QByteArray data, QString returnPathspec, QString name, QString filename);
void playSuccessSound();
void playErrorSound();
}; };
#endif // CUSTOMUPLOADER_HPP #endif // CUSTOMUPLOADER_HPP

View File

@ -11,8 +11,6 @@
#include <notifications.hpp> #include <notifications.hpp>
#include <settings.hpp> #include <settings.hpp>
#include <utils.hpp> #include <utils.hpp>
#include <QMediaPlayer>
#include "mainwindow.hpp"
struct SegfaultWorkaround { // I'm a scrub for doing this struct SegfaultWorkaround { // I'm a scrub for doing this
SegfaultWorkaround(QByteArray a, ImgurUploader *u, QString m) : byteArray(), dis(u), mime(m) { 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); ioutils::addLogEntry(r, data, result, filename);
utils::toClipboard(result); utils::toClipboard(result);
notifications::notify(tr("KShare imgur Uploader"), tr("Uploaded to imgur!")); notifications::notify(tr("KShare imgur Uploader"), tr("Uploaded to imgur!"));
playSuccessSound(); notifications::playSound(notifications::Sound::SUCCESS);
} else { } else {
ioutils::addLogEntry(r, data, result, filename); ioutils::addLogEntry(r, data, result, filename);
notifications::notify(tr("KShare imgur Uploader "), notifications::notify(tr("KShare imgur Uploader "),
QString(tr("Failed upload! imgur said: HTTP %1: %2")) QString(tr("Failed upload! imgur said: HTTP %1: %2"))
.arg(r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()) .arg(r->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt())
.arg(r->errorString())); .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) { void ImgurUploader::handleSend(QString auth, QString mime, QByteArray byteArray) {
handleSend(auth, mime, 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: private:
void handleSend(QString auth, QString mime, QByteArray byteArray, QString filename); void handleSend(QString auth, QString mime, QByteArray byteArray, QString filename);
void handleSend(QString auth, QString mime, QByteArray byteArray); void handleSend(QString auth, QString mime, QByteArray byteArray);
void playSuccessSound();
void playErrorSound();
}; };
#endif // IMGURUPLOADER_HPP #endif // IMGURUPLOADER_HPP

View File

@ -70,7 +70,7 @@ void UploaderSingleton::upload(QPixmap pixmap) {
file = new QTemporaryFile(); file = new QTemporaryFile();
} }
if (file->open(QFile::ReadWrite)) { if (file->open(QFile::ReadWrite)) {
playSound(); notifications::playSound(notifications::Sound::CAPTURE);
pixmap.save(file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt()); pixmap.save(file, format.toLocal8Bit().constData(), settings::settings().value("imageQuality", -1).toInt());
file->seek(0); file->seek(0);
QFileInfo fileInfo(file->fileName()); QFileInfo fileInfo(file->fileName());
@ -92,7 +92,7 @@ void UploaderSingleton::upload(QByteArray img, QString format) {
file = new QTemporaryFile(); file = new QTemporaryFile();
} }
if (file->open(QFile::WriteOnly)) { if (file->open(QFile::WriteOnly)) {
playSound(); notifications::playSound(notifications::Sound::CAPTURE);
file->write(img); file->write(img);
file->close(); file->close();
} }
@ -107,7 +107,7 @@ void UploaderSingleton::upload(QFile &img, QString format) {
if (!saveImages || img.rename(saveDir.absoluteFilePath( if (!saveImages || img.rename(saveDir.absoluteFilePath(
formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(), formatter::format(settings::settings().value("fileFormat", "Screenshot %(yyyy-MM-dd HH-mm-ss)date.%ext").toString(),
format.toLower())))) { format.toLower())))) {
playSound(); notifications::playSound(notifications::Sound::CAPTURE);
QFileInfo fileInfo(img.fileName()); QFileInfo fileInfo(img.fileName());
if (img.open(QFile::ReadWrite)) if (img.open(QFile::ReadWrite))
uploaders.value(uploader)->doUpload(img.readAll(), format, fileInfo.fileName()); 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 "uploader.hpp"
#include <QDir> #include <QDir>
#include <QMap> #include <QMap>
#include <QMediaPlayer>
class UploaderSingleton : public QObject { class UploaderSingleton : public QObject {
Q_OBJECT Q_OBJECT
@ -32,12 +31,10 @@ signals:
private: private:
void updateSaveSettings(); void updateSaveSettings();
void playSound();
QDir saveDir; QDir saveDir;
bool saveImages = true; bool saveImages = true;
QMap<QString, Uploader *> uploaders; QMap<QString, Uploader *> uploaders;
QString uploader = "imgur"; QString uploader = "imgur";
QMediaPlayer *mediaPlayer;
QList<std::runtime_error> errs; QList<std::runtime_error> errs;
UploaderSingleton(); UploaderSingleton();
}; };