Web: support for saving cookies across sessions - fixes #2447

This commit is contained in:
Yorik van Havre 2016-05-24 12:38:18 -03:00
parent 960191677b
commit b4a5582309
No known key found for this signature in database
GPG Key ID: 4C7794EF1595A813
4 changed files with 198 additions and 0 deletions

View File

@ -53,6 +53,7 @@
#endif
#include "BrowserView.h"
#include "CookieJar.h"
#include <Gui/Application.h>
#include <Gui/MainWindow.h>
#include <Gui/ProgressBar.h>
@ -168,6 +169,10 @@ BrowserView::BrowserView(QWidget* parent)
view->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
view->page()->setForwardUnsupportedContent(true);
// set our custom cookie manager
FcCookieJar* cookiejar = new FcCookieJar(this);
view->page()->networkAccessManager()->setCookieJar(cookiejar);
// setting background to white
QPalette palette = view->palette();

View File

@ -24,10 +24,13 @@ SET(WebGui_SRCS
Workbench.h
BrowserView.h
BrowserView.cpp
CookieJar.cpp
CookieJar.h
)
set(WebGui_MOC_HDRS
BrowserView.h
CookieJar.h
)
fc_wrap_cpp(WebGui_MOC_SRCS ${WebGui_MOC_HDRS})
SOURCE_GROUP("Moc" FILES ${SketcherGui_MOC_SRCS})

View File

@ -0,0 +1,131 @@
/***************************************************************************
* Copyright (c) 2016 Yorik van Havre <yorik@uncreated.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QTextStream>
#include <QFile>
#include <QTimer>
#endif
#include "CookieJar.h"
#include <Base/FileInfo.h>
#include <App/Application.h>
using namespace WebGui;
/**
* The default cookiejar of qt does not implement saving and restoring of
* cookies to disk. So we extend it here, and make it save and restore cookies
* to a simple text file saved in the FreeCAD user folder.
* adapted from https://github.com/adobe/webkit/blob/master/Tools/QtTestBrowser
*/
FcCookieJar::FcCookieJar(QObject* parent)
: QNetworkCookieJar(parent)
{
// We use a timer for the real disk write to avoid multiple IO
// syscalls in sequence (when loading pages which set multiple cookies).
m_timer.setInterval(10000);
m_timer.setSingleShot(true);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(saveToDisk()));
Base::FileInfo cookiefile(App::Application::getUserAppDataDir() + "cookies");
m_file.setFileName(QString::fromUtf8(cookiefile.filePath().c_str()));
if (allCookies().isEmpty())
loadFromDisk();
}
FcCookieJar::~FcCookieJar()
{
extractRawCookies();
saveToDisk();
}
bool FcCookieJar::setCookiesFromUrl(const QList<QNetworkCookie>& cookieList, const QUrl& url)
{
bool status = QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
if (status)
scheduleSaveToDisk();
return status;
}
void FcCookieJar::scheduleSaveToDisk()
{
// We extract the raw cookies here because the user may
// enable/disable/clear cookies while the timer is running.
extractRawCookies();
m_timer.start();
}
void FcCookieJar::extractRawCookies()
{
QList<QNetworkCookie> cookies = allCookies();
m_rawCookies.clear();
for (QList<QNetworkCookie>::iterator i = cookies.begin(); i != cookies.end(); i++) {
if (!(*i).isSessionCookie())
m_rawCookies.append((*i).toRawForm());
}
}
void FcCookieJar::saveToDisk()
{
m_timer.stop();
if (m_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&m_file);
for (QList<QByteArray>::iterator i = m_rawCookies.begin(); i != m_rawCookies.end(); i++) {
out << (*i) + "\n";
}
m_file.close();
} else
qWarning("IO error handling cookiejar file");
}
void FcCookieJar::loadFromDisk()
{
if (!m_file.exists())
return;
QList<QNetworkCookie> cookies;
if (m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&m_file);
while (!in.atEnd())
cookies.append(QNetworkCookie::parseCookies(in.readLine().toUtf8()));
m_file.close();
} else
qWarning("IO error handling cookiejar file");
setAllCookies(cookies);
}
void FcCookieJar::reset()
{
setAllCookies(QList<QNetworkCookie>());
scheduleSaveToDisk();
}
#include "moc_CookieJar.cpp"

View File

@ -0,0 +1,59 @@
/***************************************************************************
* Copyright (c) 2016 Yorik van Havre <yorik@uncreated.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#ifndef WEBGUI_COOKIEJAR_H
#define WEBGUI_COOKIEJAR_H
#include <QNetworkCookieJar>
class QNetworkCookieJar;
namespace WebGui {
class WebGuiExport FcCookieJar : public QNetworkCookieJar {
Q_OBJECT
public:
FcCookieJar(QObject* parent = 0);
virtual ~FcCookieJar();
virtual bool setCookiesFromUrl(const QList<QNetworkCookie>&, const QUrl&);
public Q_SLOTS:
void scheduleSaveToDisk();
void loadFromDisk();
void reset();
private Q_SLOTS:
void saveToDisk();
private:
void extractRawCookies();
QList<QByteArray> m_rawCookies;
QFile m_file;
QTimer m_timer;
};
}
#endif // WEBGUI_COOKIEJAR_H