Commit 958dc85c authored by Andrea Scarpino's avatar Andrea Scarpino

Save the username

parent a11bd14b
......@@ -160,6 +160,7 @@ Page {
}
Component.onCompleted: {
username.text = manager.getUsername();
var isAuth = manager.isAuthenticated();
isAuthenticated(isAuth);
......@@ -177,14 +178,12 @@ Page {
} else {
login.text = qsTr("Login");
details.visible = false;
username.text = "";
password.text = "";
}
}
function updateDetails() {
var user = manager.loggedUser();
username.text = user.id;
details.visible = true;
created.value = Qt.formatDateTime(user.created);
karma.value = user.karma;
......
* Thu Mar 04 2016 Andrea Scarpino <me@andreascarpino.it> 0.8.1-1
* Fri Jun 03 2016 Andrea Scarpino <me@andreascarpino.it> 0.8.1-1
- Add the capability to post comments
- Fix new comments non visible when refreshing
- Show logged user details in the settings
- Save the username
* Thu Mar 03 2016 Andrea Scarpino <me@andreascarpino.it> 0.8-1
- Implement authentication
......
......@@ -13,7 +13,7 @@ Name: harbour-sailhn
%{!?qtc_make:%define qtc_make make}
%{?qtc_builddir:%define _builddir %qtc_builddir}
Summary: Unofficial Hacker News client
Version: 0.8
Version: 0.8.1
Release: 1
Group: Qt/Qt
License: MIT
......
Name: harbour-sailhn
Summary: Unofficial Hacker News client
Version: 0.8
Version: 0.8.1
Release: 1
# The contents of the Group field should be one of the groups listed here:
# http://gitorious.org/meego-developer-tools/spectacle/blobs/master/data/GROUPS
......
......@@ -24,6 +24,7 @@
#include "hnmanager.h"
#include <QCoreApplication>
#include <QDebug>
#include <QEventLoop>
#include <QNetworkAccessManager>
......@@ -32,6 +33,7 @@
#include <QNetworkReply>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QSettings>
#include <QUrlQuery>
#include "hackernewsapi.h"
......@@ -43,8 +45,10 @@ HNManager::HNManager(QObject *parent) :
, api(new HackerNewsAPI(this))
, network(new QNetworkAccessManager(this))
, m_loggedUser(0)
, m_loggedUsername(QString())
{
m_settings = new QSettings(QCoreApplication::applicationName(), QCoreApplication::applicationName(), this);
setUsername(m_settings->value("Username").toString());
network->setCookieJar(new QNetworkCookieJar(this));
}
......@@ -53,12 +57,13 @@ HNManager::~HNManager()
delete api;
delete network;
delete m_loggedUser;
delete m_settings;
}
void HNManager::authenticate(const QString &username, const QString &password)
{
qDebug() << "Log in with username" << username;
m_loggedUsername = username;
setUsername(username);
QNetworkRequest req(QUrl(BASE_URL + QLatin1String("/login")));
req.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("application/x-www-form-urlencoded"));
......@@ -104,9 +109,20 @@ void HNManager::onLoggedUserFetched(User *user)
bool HNManager::isAuthenticated() const
{
//qDebug() << "Is authenticated as:" << m_loggedUsername;
//qDebug() << "Is authenticated as:" << m_loggedUser->id();
return m_loggedUser != 0;
}
void HNManager::setUsername(const QString &username)
{
m_loggedUsername = username;
m_settings->setValue("Username", username);
}
return !m_loggedUsername.isEmpty();
QString HNManager::getUsername() const
{
return m_loggedUsername;
}
User* HNManager::loggedUser()
......@@ -116,7 +132,8 @@ User* HNManager::loggedUser()
void HNManager::logout()
{
m_loggedUsername.clear();
setUsername(QString());
delete m_loggedUser;
// FIXME: is there a better way?
network->cookieJar()->deleteLater();
......
......@@ -34,6 +34,7 @@ class HackerNewsAPI;
class QNetworkAccessManager;
class QNetworkReply;
class QRegularExpression;
class QSettings;
class HNManager : public QObject
{
......@@ -46,6 +47,7 @@ public:
Q_INVOKABLE void authenticate(const QString &username, const QString &password);
Q_INVOKABLE bool isAuthenticated() const;
Q_INVOKABLE User* loggedUser();
Q_INVOKABLE QString getUsername() const;
Q_INVOKABLE void logout();
Q_INVOKABLE void submit(const QString &title, const QString &url, const QString &text);
Q_INVOKABLE void comment(const int parentId, const QString &text);
......@@ -65,11 +67,13 @@ private:
QString getSubmitCSRF() const;
QString getCommentCSRF(const int itemId) const;
QString getCSRF(QNetworkReply *reply, const QRegularExpression &regexp) const;
void setUsername(const QString &username);
HackerNewsAPI *api;
QNetworkAccessManager *network;
User *m_loggedUser;
QString m_loggedUsername;
QSettings *m_settings;
};
#endif // HNMANAGER_H
......@@ -119,7 +119,7 @@
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="176"/>
<location filename="../qml/pages/Settings.qml" line="177"/>
<source>Logged</source>
<translation>Connesso</translation>
</message>
......@@ -150,7 +150,7 @@
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="95"/>
<location filename="../qml/pages/Settings.qml" line="178"/>
<location filename="../qml/pages/Settings.qml" line="179"/>
<source>Login</source>
<translation>Entra</translation>
</message>
......
......@@ -119,7 +119,7 @@
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="176"/>
<location filename="../qml/pages/Settings.qml" line="177"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
......@@ -150,7 +150,7 @@
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="95"/>
<location filename="../qml/pages/Settings.qml" line="178"/>
<location filename="../qml/pages/Settings.qml" line="179"/>
<source>Login</source>
<translation type="unfinished"></translation>
</message>
......
......@@ -119,7 +119,7 @@
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="176"/>
<location filename="../qml/pages/Settings.qml" line="177"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
......@@ -150,7 +150,7 @@
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="95"/>
<location filename="../qml/pages/Settings.qml" line="178"/>
<location filename="../qml/pages/Settings.qml" line="179"/>
<source>Login</source>
<translation type="unfinished"></translation>
</message>
......
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