Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit f234893e authored by Laurent Montel's avatar Laurent Montel 😁

Improve gravatar support

parent 3923d109
......@@ -287,6 +287,9 @@ add_subdirectory(temporaryfile/autotests)
add_subdirectory(util/autotests)
add_subdirectory(gravatar/autotests)
add_subdirectory(gravatar/tests)
if( LibKGAPI2_FOUND )
add_subdirectory(storageservice/gdrive/tests/)
add_subdirectory(storageservice/gdrive/icons/)
......
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
include_directories(
${CMAKE_SOURCE_DIR}/pimcommon
)
set( pimcommon_gravatar_SRCS gravatarresolvurljobtest.cpp )
kde4_add_unit_test( pimcommon_gravatartest ${pimcommon_gravatar_SRCS})
target_link_libraries( pimcommon_gravatartest ${QT_QTTEST_LIBRARY} ${KDE4_KDEUI_LIBS} pimcommon)
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 "gravatarresolvurljobtest.h"
#include "gravatar/gravatarresolvurljob.h"
#include <qtest_kde.h>
GravatarResolvUrlJobTest::GravatarResolvUrlJobTest(QObject *parent)
: QObject(parent)
{
}
GravatarResolvUrlJobTest::~GravatarResolvUrlJobTest()
{
}
void GravatarResolvUrlJobTest::shouldHaveDefaultValue()
{
PimCommon::GravatarResolvUrlJob job;
QVERIFY(job.email().isEmpty());
}
void GravatarResolvUrlJobTest::shouldNotStart()
{
PimCommon::GravatarResolvUrlJob job;
QVERIFY(!job.canStart());
job.setEmail(QLatin1String("foo"));
QVERIFY(!job.canStart());
job.setEmail(QLatin1String(" "));
QVERIFY(!job.canStart());
job.setEmail(QLatin1String("foo@kde.org"));
QVERIFY(job.canStart());
}
void GravatarResolvUrlJobTest::shouldGenerateGravatarUrl_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<KUrl>("output");
QTest::newRow("empty") << QString() << KUrl();
QTest::newRow("no domain") << QString(QLatin1String("foo")) << KUrl();
QTest::newRow("validemail") << QString(QLatin1String("foo@kde.org")) << KUrl("http://www.gravatar.com:80/avatar/89b4e14cf2fc6d426275c019c6dc9de6?d=404");
}
void GravatarResolvUrlJobTest::shouldGenerateGravatarUrl()
{
QFETCH( QString, input );
QFETCH( KUrl, output );
PimCommon::GravatarResolvUrlJob job;
job.setEmail(input);
KUrl url = job.generateGravatarUrl();
QCOMPARE(url, output);
}
QTEST_KDEMAIN(GravatarResolvUrlJobTest, NoGUI)
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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
*/
#ifndef GRAVATARRESOLVURLJOBTEST_H
#define GRAVATARRESOLVURLJOBTEST_H
#include <QObject>
class GravatarResolvUrlJobTest : public QObject
{
Q_OBJECT
public:
explicit GravatarResolvUrlJobTest(QObject *parent = 0);
~GravatarResolvUrlJobTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldNotStart();
void shouldGenerateGravatarUrl_data();
void shouldGenerateGravatarUrl();
};
#endif // GRAVATARRESOLVURLJOBTEST_H
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
based on code from Sune Vuorela <sune@vuorela.dk> (Rawatar source code)
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
......@@ -17,6 +19,9 @@
#include "gravatarresolvurljob.h"
#include <QCryptographicHash>
#include <QStringList>
#include <QDebug>
using namespace PimCommon;
......@@ -33,13 +38,24 @@ GravatarResolvUrlJob::~GravatarResolvUrlJob()
bool GravatarResolvUrlJob::canStart() const
{
return !mEmail.trimmed().isEmpty();
return !mEmail.trimmed().isEmpty() && (mEmail.contains(QLatin1Char('@')));
}
KUrl GravatarResolvUrlJob::generateGravatarUrl()
{
return createUrl();
}
bool GravatarResolvUrlJob::hasGravatar() const
{
//TODO
return true;
}
void GravatarResolvUrlJob::start()
{
if (canStart()) {
const KUrl url = createUrl();
} else {
//TODO return message Error.
deleteLater();
......@@ -56,3 +72,25 @@ void GravatarResolvUrlJob::setEmail(const QString &email)
mEmail = email;
}
QString GravatarResolvUrlJob::calculateHash()
{
//KF5 add support for libravatar
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(mEmail.toLower().toUtf8());
return QString::fromUtf8(hash.result().toHex());
}
KUrl GravatarResolvUrlJob::createUrl()
{
if (!canStart()) {
return KUrl();
}
KUrl url;
url.setScheme(QLatin1String("http"));
url.setHost(QLatin1String("www.gravatar.com"));
url.setPort(80);
url.setPath(QLatin1String("/avatar/") + calculateHash());
//Add ?d=404
url.addQueryItem(QLatin1String("d"), QLatin1String("404"));
return url;
}
......@@ -20,6 +20,7 @@
#include "pimcommon_export.h"
#include <QObject>
#include <KUrl>
namespace PimCommon {
class PIMCOMMON_EXPORT GravatarResolvUrlJob : public QObject
......@@ -35,7 +36,17 @@ public:
QString email() const;
void setEmail(const QString &email);
KUrl generateGravatarUrl();
bool hasGravatar() const;
Q_SIGNALS:
void urlResolved(const KUrl &url);
private:
KUrl createUrl();
QString calculateHash();
QString mEmail;
};
}
......
include_directories(${CMAKE_SOURCE_DIR}/pimcommon/
)
set(gravatarwidget_gui_SRCS gravatarwidget_gui.cpp)
kde4_add_executable(gravatarwidget_gui TEST ${gravatarwidget_gui_SRCS})
target_link_libraries(gravatarwidget_gui
${QT_QTTEST_LIBRARY}
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY}
${KDE4_KDECORE_LIBS}
${KDE4_KDEUI_LIBS}
pimcommon
)
/*
Copyright (c) 2015 Montel Laurent <montel@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 <kdebug.h>
#include <kapplication.h>
#include <KCmdLineArgs>
#include <KLocalizedString>
int main (int argc, char **argv)
{
KCmdLineArgs::init(argc, argv, "gravatar_gui", 0, ki18n("GravatarTest_Gui"),
"1.0", ki18n("Test for gravatar widget"));
KApplication app;
//w->show();
app.exec();
//delete w;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment