From f3fd7453f94d7c5fc03384e59456ea950c3770fb Mon Sep 17 00:00:00 2001 From: Gurkengewuerz Date: Tue, 14 May 2019 17:12:09 +0200 Subject: [PATCH] moved notification sounds to notification namespace --- src/clipboard/clipboardcopy.cpp | 18 +++----------- src/clipboard/clipboardcopy.hpp | 1 - src/mainwindow.ui | 2 +- src/notifications.cpp | 29 ++++++++++++++++++++++ src/notifications.hpp | 2 ++ src/uploaders/customuploader.cpp | 32 ++++--------------------- src/uploaders/customuploader.hpp | 2 -- src/uploaders/default/imguruploader.cpp | 26 ++------------------ src/uploaders/default/imguruploader.hpp | 2 -- src/uploaders/uploadersingleton.cpp | 17 +++---------- src/uploaders/uploadersingleton.hpp | 3 --- 11 files changed, 45 insertions(+), 89 deletions(-) diff --git a/src/clipboard/clipboardcopy.cpp b/src/clipboard/clipboardcopy.cpp index 441d93b..d079f3c 100644 --- a/src/clipboard/clipboardcopy.cpp +++ b/src/clipboard/clipboardcopy.cpp @@ -11,13 +11,11 @@ #include #include #include -#include #include #include #include #include #include -#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); -} \ No newline at end of file diff --git a/src/clipboard/clipboardcopy.hpp b/src/clipboard/clipboardcopy.hpp index 88d85d0..9f12ff9 100644 --- a/src/clipboard/clipboardcopy.hpp +++ b/src/clipboard/clipboardcopy.hpp @@ -4,7 +4,6 @@ namespace clipboardcopy { void copyClipboard(); - void playErrorSound(); } diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 8258547..cfd1eff 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -120,7 +120,7 @@ - 1 + 0 diff --git a/src/notifications.cpp b/src/notifications.cpp index f37a7ed..3106553 100644 --- a/src/notifications.cpp +++ b/src/notifications.cpp @@ -4,6 +4,7 @@ #include "mainwindow.hpp" #include "ui_mainwindow.h" #include +#include 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); + +} diff --git a/src/notifications.hpp b/src/notifications.hpp index 75306be..9ff6930 100644 --- a/src/notifications.hpp +++ b/src/notifications.hpp @@ -5,8 +5,10 @@ #include 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 diff --git a/src/uploaders/customuploader.cpp b/src/uploaders/customuploader.cpp index 1872db9..13f5e33 100644 --- a/src/uploaders/customuploader.cpp +++ b/src/uploaders/customuploader.cpp @@ -12,8 +12,6 @@ #include #include #include -#include -#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); -} \ No newline at end of file diff --git a/src/uploaders/customuploader.hpp b/src/uploaders/customuploader.hpp index 512916a..80a88b0 100644 --- a/src/uploaders/customuploader.hpp +++ b/src/uploaders/customuploader.hpp @@ -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 diff --git a/src/uploaders/default/imguruploader.cpp b/src/uploaders/default/imguruploader.cpp index a069a72..33e54fc 100644 --- a/src/uploaders/default/imguruploader.cpp +++ b/src/uploaders/default/imguruploader.cpp @@ -11,8 +11,6 @@ #include #include #include -#include -#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); -} diff --git a/src/uploaders/default/imguruploader.hpp b/src/uploaders/default/imguruploader.hpp index e9459dc..c3031a8 100644 --- a/src/uploaders/default/imguruploader.hpp +++ b/src/uploaders/default/imguruploader.hpp @@ -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 diff --git a/src/uploaders/uploadersingleton.cpp b/src/uploaders/uploadersingleton.cpp index e33e325..a554c8e 100644 --- a/src/uploaders/uploadersingleton.cpp +++ b/src/uploaders/uploadersingleton.cpp @@ -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); - -} diff --git a/src/uploaders/uploadersingleton.hpp b/src/uploaders/uploadersingleton.hpp index 72263f9..332c838 100644 --- a/src/uploaders/uploadersingleton.hpp +++ b/src/uploaders/uploadersingleton.hpp @@ -4,7 +4,6 @@ #include "uploader.hpp" #include #include -#include class UploaderSingleton : public QObject { Q_OBJECT @@ -32,12 +31,10 @@ signals: private: void updateSaveSettings(); - void playSound(); QDir saveDir; bool saveImages = true; QMap uploaders; QString uploader = "imgur"; - QMediaPlayer *mediaPlayer; QList errs; UploaderSingleton(); };