move code to Utils

This commit is contained in:
Patrick von Reth 2015-04-13 22:46:26 +02:00
parent 24a8bcd287
commit c717150236
9 changed files with 67 additions and 38 deletions

View File

@ -18,6 +18,7 @@
#include "icon_p.h"
#include "../snore_p.h"
#include "../utils.h"
#include <QApplication>
#include <QFile>
@ -30,7 +31,7 @@ QSet<QString> IconData::s_localImageCache;
IconData::IconData(const QString &url):
m_url(url),
m_hash(SnoreCorePrivate::computeHash(m_url.toLatin1())),
m_hash(Utils::computeMD5Hash(m_url.toLatin1())),
m_localUrl(createLocalFileName(m_hash)),
m_isLocalFile(false),
m_isResource(m_url.startsWith(":/") || m_url.startsWith("qrc:/"))
@ -80,7 +81,7 @@ IconData::IconData(const QString &url):
IconData::IconData(const QImage &img):
m_img(img),
m_hash(SnoreCorePrivate::computeHash(Icon::dataFromImage(img))),
m_hash(Utils::computeMD5Hash(Icon::dataFromImage(img))),
m_localUrl(createLocalFileName(m_hash)),
m_isLocalFile(false),
m_isResource(false),

View File

@ -29,8 +29,6 @@
#include <QSettings>
#include <QStringList>
#include <QTextDocument>
#include <QTextDocumentFragment>
/**
* Snore is a platform independent Qt notification framework.
@ -182,21 +180,6 @@ private:
};
/**
*
* @param string A string to decode of if needed.
* @return if the string was rhichtext or html encoded a decoded string, else the original string.
*/
static inline QString toPlainText(const QString &string)
{
if (Qt::mightBeRichText(string)) {
return QTextDocumentFragment::fromHtml(string).toPlainText();
} else {
return string;
}
}
}
#endif // SNORESERVER_H

View File

@ -25,7 +25,6 @@
#include <QPointer>
#include <QApplication>
#include <QCryptographicHash>
namespace Snore
{
@ -35,11 +34,6 @@ class SNORE_EXPORT SnoreCorePrivate : public QObject
Q_OBJECT
public:
static inline QString computeHash(const QByteArray &data)
{
return QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex();
}
static void registerMetaTypes();
/**

View File

@ -21,6 +21,9 @@
#include "snore_exports.h"
#include <QApplication>
#include <QCryptographicHash>
#include <QTextDocument>
#include <QTextDocumentFragment>
namespace Snore {
class SNORE_EXPORT Utils : public QObject {
@ -29,9 +32,50 @@ public:
Utils(QObject *parent = nullptr);
~Utils();
/**
* Raise a window to the front.
* @param wid the Id of the window to raise.
* @param focus whether the window should request focus.
*/
//TODO: make Wid usable with the meta system and change signature.
Q_INVOKABLE static void bringWindowToFront(qlonglong wid, bool focus);
/**
*
* @param string A string to decode if needed.
* @return if the string was rhichtext or html encoded a decoded string, else the original string.
*/
static inline QString toPlainText(const QString &string)
{
if (Qt::mightBeRichText(string)) {
return QTextDocumentFragment::fromHtml(string).toPlainText();
} else {
return string;
}
}
/**
*
* @param string A string to encode if needed.
* @return if the string was rhichtext html encode string is returnd otherwise the original string.
*/
static inline QString toHtml(const QString &string)
{
if (Qt::mightBeRichText(string)) {
return QTextDocumentFragment::fromHtml(string).toHtml("UTF-8");
} else {
return string;
}
}
/**
* Computes a md5 hash of the provided data.
*/
static inline QString computeMD5Hash(const QByteArray &data)
{
return QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex();
}
};
}

View File

@ -21,6 +21,7 @@
#include "libsnore/snore.h"
#include "libsnore/snore_p.h"
#include "libsnore/utils.h"
using namespace Snore;
@ -87,11 +88,11 @@ void GrowlBackend::slotNotify(Notification notification)
{
Growl *growl = m_applications.value(notification.application().name());
QString alert = notification.alert().name();
snoreDebug(SNORE_DEBUG) << "Notify Growl:" << notification.application() << alert << Snore::toPlainText(notification.title());
snoreDebug(SNORE_DEBUG) << "Notify Growl:" << notification.application() << alert << Utils::toPlainText(notification.title());
GrowlNotificationData data(alert.toUtf8().constData(), notification.id(),
Snore::toPlainText(notification.title()).toUtf8().constData(),
Snore::toPlainText(notification.text()).toUtf8().constData());
Utils::toPlainText(notification.title()).toUtf8().constData(),
Utils::toPlainText(notification.text()).toUtf8().constData());
if (notification.icon().isValid()) {
data.setIcon(notification.icon().localUrl().toUtf8().constData());

View File

@ -21,6 +21,7 @@
#include "libsnore/snore.h"
#include "libsnore/snore_p.h"
#include "libsnore/utils.h"
#include "libsnore/plugins/plugins.h"
#include "libsnore/plugins/snorebackend.h"
#include "libsnore/notification/notification_p.h"
@ -216,8 +217,8 @@ void SnarlBackend::slotNotify(Notification notification)
if (!notification.isUpdate()) {
id = snarlInterface->Notify(notification.alert().name().toUtf8().constData(),
Snore::toPlainText(notification.title()).toUtf8().constData(),
Snore::toPlainText(notification.text()).toUtf8().constData(),
Utils::toPlainText(notification.title()).toUtf8().constData(),
Utils::toPlainText(notification.text()).toUtf8().constData(),
notification.timeout(),
notification.icon().isLocalFile() ? notification.icon().localUrl().toUtf8().constData() : 0,
!notification.icon().isLocalFile() ? Icon::dataFromImage(notification.icon().image()).toBase64().constData() : 0,
@ -233,8 +234,8 @@ void SnarlBackend::slotNotify(Notification notification)
id = notification.old().hints().privateValue(this, "id").toUInt();
snarlInterface->Update(id,
notification.alert().name().toUtf8().constData(),
Snore::toPlainText(notification.title()).toUtf8().constData(),
Snore::toPlainText(notification.text()).toUtf8().constData(),
Utils::toPlainText(notification.title()).toUtf8().constData(),
Utils::toPlainText(notification.text()).toUtf8().constData(),
notification.timeout(),
notification.icon().isLocalFile() ? notification.icon().localUrl().toUtf8().constData() : 0,
!notification.icon().isLocalFile() ? Icon::dataFromImage(notification.icon().image()).toBase64().constData() : 0,

View File

@ -1,6 +1,8 @@
#include "snoretoast.h"
#include "libsnore/snore.h"
#include "libsnore/snore_p.h"
#include "libsnore/utils.h"
#include "libsnore/plugins/plugins.h"
#include "libsnore/plugins/snorebackend.h"
@ -41,9 +43,9 @@ void SnoreToast::slotNotify(Notification notification)
QStringList arguements;
arguements << "-t"
<< Snore::toPlainText(notification.title())
<< Utils::toPlainText(notification.title())
<< "-m"
<< Snore::toPlainText(notification.text());
<< Utils::toPlainText(notification.text());
if (notification.icon().isValid()) {
arguements << "-p"
<< QDir::toNativeSeparators(notification.icon().localUrl());

View File

@ -1,6 +1,7 @@
#include "trayiconnotifer.h"
#include "libsnore/snore.h"
#include "libsnore/snore_p.h"
#include "libsnore/utils.h"
#include <QSystemTrayIcon>
using namespace Snore;
@ -83,7 +84,7 @@ void TrayIconNotifer::displayNotification(QSystemTrayIcon *icon)
m_currentlyDisplaying = true;
Notification notification = m_notificationQue.takeFirst();
m_displayed = notification.id();
icon->showMessage(Snore::toPlainText(notification.title()), Snore::toPlainText(notification.text()), QSystemTrayIcon::NoIcon, notification.timeout() * 1000);
icon->showMessage(Utils::toPlainText(notification.title()), Utils::toPlainText(notification.text()), QSystemTrayIcon::NoIcon, notification.timeout() * 1000);
startTimeout(notification);
}

View File

@ -17,6 +17,7 @@
*/
#include "toasty.h"
#include "toastysettings.h"
#include "libsnore/utils.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
@ -45,14 +46,15 @@ void Toasty::slotNotify(Notification notification)
}
QNetworkRequest request(QString("http://api.supertoasty.com/notify/%1").arg(key));
QHttpMultiPart *mp = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart title;
title.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"title\""));
title.setBody(Snore::toPlainText(notification.title()).toUtf8().constData());
title.setBody(Utils::toPlainText(notification.title()).toUtf8().constData());
mp->append(title);
QHttpPart text;
text.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\""));
text.setBody(Snore::toPlainText(notification.text()).toUtf8().constData());
text.setBody(Utils::toPlainText(notification.text()).toUtf8().constData());
mp->append(text);
QHttpPart app;
@ -89,5 +91,5 @@ void Toasty::slotNotify(Notification notification)
PluginSettingsWidget *Toasty::settingsWidget()
{
return new ToastySettings(this);
return new NotifyMyAndroidSettings(this);
}