Commit c2f3b96d authored by Andrea Scarpino's avatar Andrea Scarpino

Store cookies

parent 6a801d55
......@@ -4,6 +4,7 @@ CONFIG += sailfishapp
SOURCES += \
src/main.cpp \
src/cookiejar.cpp \
src/hackernewsapi.cpp \
src/hnmanager.cpp \
src/item.cpp \
......@@ -11,6 +12,7 @@ SOURCES += \
src/user.cpp
HEADERS += \
src/cookiejar.h \
src/hackernewsapi.h \
src/hnmanager.h \
src/item.h \
......
......@@ -45,8 +45,8 @@ Page {
if (!storiesLoadedOnce) {
loadStories();
}
listView.submitEnabled = manager.isAuthenticated();
} else if (status === PageStatus.Active) {
listView.submitEnabled = manager.isAuthenticated();
pageStack.pushAttached(Qt.resolvedUrl("JobStoriesPage.qml"));
} else if (status === PageStatus.Deactivating) {
storiesLoadedOnce = true;
......
......@@ -45,6 +45,7 @@ Page {
if (!storiesLoadedOnce) {
loadStories();
}
} else if (status === PageStatus.Active) {
listView.submitEnabled = manager.isAuthenticated();
} else if (status === PageStatus.Deactivating) {
storiesLoadedOnce = true;
......
......@@ -45,8 +45,8 @@ Page {
if (!storiesLoadedOnce) {
loadStories();
}
listView.submitEnabled = manager.isAuthenticated();
} else if (status === PageStatus.Active) {
listView.submitEnabled = manager.isAuthenticated();
pageStack.pushAttached(Qt.resolvedUrl("BestStoriesPage.qml"));
} else if (status === PageStatus.Deactivating) {
storiesLoadedOnce = true;
......
......@@ -45,8 +45,8 @@ Page {
if (!storiesLoadedOnce) {
loadStories();
}
listView.submitEnabled = manager.isAuthenticated();
} else if (status === PageStatus.Active) {
listView.submitEnabled = manager.isAuthenticated();
pageStack.pushAttached(Qt.resolvedUrl("ShowStoriesPage.qml"));
} else if (status === PageStatus.Deactivating) {
storiesLoadedOnce = true;
......
......@@ -80,6 +80,8 @@ Page {
id: username
width: parent.width
placeholderText: qsTr("Username")
onTextChanged: login.enabled = (text.length > 0 && password.text.length > 0)
}
TextField {
......@@ -87,20 +89,21 @@ Page {
width: parent.width
placeholderText: qsTr("Password")
echoMode: TextInput.Password
onTextChanged: login.enabled = (text.length > 0 && username.text.length > 0)
}
Button {
id: login
text: qsTr("Login");
anchors.horizontalCenter: parent.horizontalCenter
enabled: false
onClicked: {
if (username.text.length > 0 && password.text.length > 0) {
manager.authenticate(username.text, password.text);
login.enabled = false;
busy.visible = busy.running = true;
msg.visible = false;
}
manager.authenticate(username.text, password.text);
login.enabled = false;
busy.visible = busy.running = true;
msg.visible = false;
}
}
......@@ -174,7 +177,7 @@ Page {
}
function isAuthenticated(isAuth) {
username.enabled = password.enabled = login.enabled = !isAuth;
username.enabled = password.enabled = !isAuth;
logout.enabled = isAuth;
if (isAuth) {
......
......@@ -45,8 +45,8 @@ Page {
if (!storiesLoadedOnce) {
loadStories();
}
listView.submitEnabled = manager.isAuthenticated();
} else if (status === PageStatus.Active) {
listView.submitEnabled = manager.isAuthenticated();
pageStack.pushAttached(Qt.resolvedUrl("AskStoriesPage.qml"));
} else if (status === PageStatus.Deactivating) {
storiesLoadedOnce = true;
......
......@@ -30,11 +30,19 @@ SilicaListView {
id: listView
property string pageTitle
property bool submitEnabled
property alias submitEnabled: submit.enabled
property alias stories: model
signal refreshClicked
Connections {
target: manager
onLoggedUserFetched: {
submit.enabled = true;
}
}
PullDownMenu {
MenuItem {
......@@ -44,8 +52,9 @@ SilicaListView {
}
MenuItem {
id: submit
text: qsTr("Submit")
enabled: submitEnabled
enabled: false
onClicked: pageStack.push(Qt.resolvedUrl("Submit.qml"))
}
......
......@@ -45,8 +45,8 @@ Page {
if (!storiesLoadedOnce) {
loadStories();
}
listView.submitEnabled = manager.isAuthenticated();
} else if (status === PageStatus.Active) {
listView.submitEnabled = manager.isAuthenticated();
pageStack.pushAttached(Qt.resolvedUrl("NewStoriesPage.qml"));
} else if (status === PageStatus.Deactivating) {
storiesLoadedOnce = true;
......
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Nathan Osman
* 2016 Andrea Scarpino <me@andreascarpino.it>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
**/
#include "cookiejar.h"
#include <QCoreApplication>
#include <QNetworkCookie>
#include <QSettings>
#include <QVariantList>
CookieJar::CookieJar(QObject *parent) :
QNetworkCookieJar(parent)
{
// Load the list of cookies
m_settings = new QSettings(QCoreApplication::applicationName(), QCoreApplication::applicationName(), this);
// Read and parse each of the cookies
QList<QNetworkCookie> cookies;
Q_FOREACH (const QVariant cookie, m_settings->value("Cookies").toList()) {
cookies.append(QNetworkCookie::parseCookies(cookie.toByteArray()));
}
// Set the list
setAllCookies(cookies);
}
CookieJar::~CookieJar()
{
// Build a list of all cookies
QVariantList cookies;
Q_FOREACH (const QNetworkCookie cookie, allCookies()) {
cookies.append(cookie.toRawForm());
}
// Store the list
m_settings->setValue("Cookies", cookies);
m_settings->deleteLater();
}
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Nathan Osman
* 2016 Andrea Scarpino <me@andreascarpino.it>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
**/
#ifndef COOKIEJAR_H
#define COOKIEJAR_H
#include <QNetworkCookieJar>
class QSettings;
class CookieJar : public QNetworkCookieJar
{
Q_OBJECT
public:
explicit CookieJar(QObject *parent);
virtual ~CookieJar();
private:
QSettings *m_settings;
};
#endif // COOKIEJAR_H
......@@ -211,7 +211,7 @@ void HackerNewsAPI::onStoriesResult()
} else {
QJsonDocument json = QJsonDocument::fromJson(reply->readAll());
if (!json.isNull()) {
qDebug() << "There are" << json.array().size() << "items";
//qDebug() << "There are" << json.array().size() << "items";
QList<int> ids;
Q_FOREACH (const QJsonValue id, json.array()) {
......
......@@ -28,7 +28,7 @@
#include <QDebug>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkCookieJar>
#include <QNetworkCookie>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QRegularExpression>
......@@ -36,6 +36,7 @@
#include <QSettings>
#include <QUrlQuery>
#include "cookiejar.h"
#include "hackernewsapi.h"
const static QString BASE_URL = QStringLiteral("https://news.ycombinator.com");
......@@ -49,7 +50,13 @@ HNManager::HNManager(QObject *parent) :
m_settings = new QSettings(QCoreApplication::applicationName(), QCoreApplication::applicationName(), this);
setUsername(m_settings->value("Username").toString());
network->setCookieJar(new QNetworkCookieJar(this));
network->setCookieJar(new CookieJar(this));
if (!network->cookieJar()->cookiesForUrl(QUrl(BASE_URL + "/")).isEmpty()) {
api->getUser(getUsername());
connect(api, &HackerNewsAPI::userFetched, this, &HNManager::onLoggedUserFetched);
}
}
HNManager::~HNManager()
......@@ -109,7 +116,7 @@ void HNManager::onLoggedUserFetched(User *user)
bool HNManager::isAuthenticated() const
{
//qDebug() << "Is authenticated as:" << m_loggedUser->id();
//qDebug() << "Is authenticated as:" << m_loggedUser;
return m_loggedUser != 0;
}
......@@ -133,11 +140,13 @@ User* HNManager::loggedUser()
void HNManager::logout()
{
setUsername(QString());
delete m_loggedUser;
m_loggedUser = 0;
// FIXME: is there a better way?
network->cookieJar()->deleteLater();
network->setCookieJar(new QNetworkCookieJar(this));
QNetworkCookieJar* cookieJar = network->cookieJar();
Q_FOREACH (const QNetworkCookie cookie, cookieJar->cookiesForUrl(QUrl(BASE_URL + "/"))) {
cookieJar->deleteCookie(cookie);
}
m_settings->setValue("Cookies", QVariantList());
}
void HNManager::submit(const QString &title, const QString &url, const QString &text)
......
......@@ -118,18 +118,13 @@
</context>
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="181"/>
<source>Logged</source>
<translation>Connesso</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<source>Logout</source>
<translation>Esci</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="110"/>
<location filename="../qml/pages/Settings.qml" line="113"/>
<source>Login failed</source>
<translation>Login fallito</translation>
</message>
......@@ -144,56 +139,61 @@
<translation>Username</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="88"/>
<location filename="../qml/pages/Settings.qml" line="90"/>
<source>Password</source>
<translation>Password</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="94"/>
<location filename="../qml/pages/Settings.qml" line="183"/>
<location filename="../qml/pages/Settings.qml" line="98"/>
<location filename="../qml/pages/Settings.qml" line="186"/>
<source>Login</source>
<translation>Entra</translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="127"/>
<location filename="../qml/pages/Settings.qml" line="130"/>
<source>User details</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="133"/>
<location filename="../qml/pages/Settings.qml" line="136"/>
<source>created</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="139"/>
<location filename="../qml/pages/Settings.qml" line="142"/>
<source>karma</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="145"/>
<location filename="../qml/pages/Settings.qml" line="148"/>
<source>About</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="184"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>StoriesListView</name>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="41"/>
<location filename="../qml/pages/StoriesListView.qml" line="49"/>
<source>Settings</source>
<translation type="unfinished">Impostazioni</translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="47"/>
<location filename="../qml/pages/StoriesListView.qml" line="56"/>
<source>Submit</source>
<translation type="unfinished">Invia</translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="54"/>
<location filename="../qml/pages/StoriesListView.qml" line="63"/>
<source>Refresh</source>
<translation type="unfinished">Aggiorna</translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="63"/>
<location filename="../qml/pages/StoriesListView.qml" line="72"/>
<source>Load more</source>
<translation type="unfinished">Carica altro</translation>
</message>
......
......@@ -118,18 +118,13 @@
</context>
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="181"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<source>Logout</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="110"/>
<location filename="../qml/pages/Settings.qml" line="113"/>
<source>Login failed</source>
<translation type="unfinished"></translation>
</message>
......@@ -144,56 +139,61 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="88"/>
<location filename="../qml/pages/Settings.qml" line="90"/>
<source>Password</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="94"/>
<location filename="../qml/pages/Settings.qml" line="183"/>
<location filename="../qml/pages/Settings.qml" line="98"/>
<location filename="../qml/pages/Settings.qml" line="186"/>
<source>Login</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="127"/>
<location filename="../qml/pages/Settings.qml" line="130"/>
<source>User details</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="133"/>
<location filename="../qml/pages/Settings.qml" line="136"/>
<source>created</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="139"/>
<location filename="../qml/pages/Settings.qml" line="142"/>
<source>karma</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="145"/>
<location filename="../qml/pages/Settings.qml" line="148"/>
<source>About</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="184"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>StoriesListView</name>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="41"/>
<location filename="../qml/pages/StoriesListView.qml" line="49"/>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="47"/>
<location filename="../qml/pages/StoriesListView.qml" line="56"/>
<source>Submit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="54"/>
<location filename="../qml/pages/StoriesListView.qml" line="63"/>
<source>Refresh</source>
<translation type="unfinished">Yenile</translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="63"/>
<location filename="../qml/pages/StoriesListView.qml" line="72"/>
<source>Load more</source>
<translation type="unfinished">Daha fazla yükle</translation>
</message>
......
......@@ -118,18 +118,13 @@
</context>
<context>
<name>Settings</name>
<message>
<location filename="../qml/pages/Settings.qml" line="181"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="61"/>
<source>Logout</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="110"/>
<location filename="../qml/pages/Settings.qml" line="113"/>
<source>Login failed</source>
<translation type="unfinished"></translation>
</message>
......@@ -144,56 +139,61 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="88"/>
<location filename="../qml/pages/Settings.qml" line="90"/>
<source>Password</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="94"/>
<location filename="../qml/pages/Settings.qml" line="183"/>
<location filename="../qml/pages/Settings.qml" line="98"/>
<location filename="../qml/pages/Settings.qml" line="186"/>
<source>Login</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="127"/>
<location filename="../qml/pages/Settings.qml" line="130"/>
<source>User details</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="133"/>
<location filename="../qml/pages/Settings.qml" line="136"/>
<source>created</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="139"/>
<location filename="../qml/pages/Settings.qml" line="142"/>
<source>karma</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="145"/>
<location filename="../qml/pages/Settings.qml" line="148"/>
<source>About</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/Settings.qml" line="184"/>
<source>Logged</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>StoriesListView</name>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="41"/>
<location filename="../qml/pages/StoriesListView.qml" line="49"/>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="47"/>
<location filename="../qml/pages/StoriesListView.qml" line="56"/>
<source>Submit</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="54"/>
<location filename="../qml/pages/StoriesListView.qml" line="63"/>
<source>Refresh</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/StoriesListView.qml" line="63"/>
<location filename="../qml/pages/StoriesListView.qml" line="72"/>
<source>Load more</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