added sync back reading progress from remote clients - full info sync is out of scope by now

This commit is contained in:
Luis Ángel San Martín 2015-04-24 22:48:17 +02:00
parent ebeddbe73b
commit f38251dbff
4 changed files with 80 additions and 8 deletions

View File

@ -348,7 +348,7 @@ void DBHelper::update(ComicInfo * comicInfo, QSqlDatabase & db)
updateComicInfo.bindValue(":comicVineID", comicInfo->comicVineID);
updateComicInfo.exec();
updateComicInfo.exec();
}
void DBHelper::updateRead(ComicInfo * comicInfo, QSqlDatabase & db)
@ -391,6 +391,44 @@ void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo)
QSqlDatabase::removeDatabase(libraryPath);
}
void DBHelper::updateReadingRemoteProgress(const ComicInfo &comicInfo, QSqlDatabase &db)
{
QSqlQuery updateComicInfo(db);
updateComicInfo.prepare("UPDATE comic_info SET "
"read = :read, "
"currentPage = :currentPage, "
"hasBeenOpened = :hasBeenOpened"
" WHERE id = :id ");
updateComicInfo.bindValue(":read", comicInfo.read?1:0);
updateComicInfo.bindValue(":currentPage", comicInfo.currentPage);
updateComicInfo.bindValue(":hasBeenOpened", comicInfo.hasBeenOpened?1:0);
updateComicInfo.bindValue(":id", comicInfo.id);
updateComicInfo.exec();
}
void DBHelper::updateFromRemoteClient(qulonglong libraryId,const ComicInfo & comicInfo)
{
QString libraryPath = DBHelper::getLibraries().getPath(libraryId);
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary");
ComicDB comic = DBHelper::loadComic(comicInfo.id,db);
if(comic.info.hash == comicInfo.hash)
{
if(comic.info.currentPage == comic.info.numPages)
comic.info.read = true;
comic.info.currentPage = comicInfo.currentPage;
comic.info.hasBeenOpened = true;
DBHelper::updateReadingRemoteProgress(comic.info,db);
}
db.close();
QSqlDatabase::removeDatabase(libraryPath);
}
void DBHelper::renameLabel(qulonglong id, const QString &name, QSqlDatabase &db)
{
QSqlQuery renameLabelQuery(db);

View File

@ -57,6 +57,8 @@ public:
static void updateRead(ComicInfo * comicInfo, QSqlDatabase & db);
static void update(const Folder & folder, QSqlDatabase & db);
static void updateProgress(qulonglong libraryId,const ComicInfo & comicInfo);
static void updateReadingRemoteProgress(const ComicInfo & comicInfo, QSqlDatabase & db);
static void updateFromRemoteClient(qulonglong libraryId,const ComicInfo & comicInfo);
static void renameLabel(qulonglong id, const QString & name, QSqlDatabase & db);
static void renameList(qulonglong id, const QString & name, QSqlDatabase & db);
static void reasignOrderToSublists(QList<qulonglong> ids, QSqlDatabase & db);

View File

@ -3,6 +3,9 @@
#include "QsLog.h"
#include <QUrl>
#include "comic_db.h"
#include "db_helper.h"
SyncController::SyncController()
{
@ -10,15 +13,42 @@ SyncController::SyncController()
void SyncController::service(HttpRequest &request, HttpResponse &response)
{
QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8();
QStringList pathElements = path.split('/');
qulonglong libraryId = pathElements.at(2).toULongLong();
QString postData = QString::fromUtf8(request.getBody());
QLOG_INFO() << "POST DATA: " << postData;
//TODO Process postData and update the comics
if(postData.length()>0) {
QList<QString> data = postData.split("\n");
qulonglong libraryId;
qulonglong comicId;
int currentPage;
QString hash;
foreach(QString comicInfo, data)
{
QList<QString> comicInfoProgress = comicInfo.split("\t");
if(comicInfoProgress.length() == 4)
{
libraryId = comicInfoProgress.at(0).toULongLong();
comicId = comicInfoProgress.at(1).toULongLong();
hash = comicInfoProgress.at(2);
currentPage = comicInfoProgress.at(3).toInt();
ComicInfo info;
info.currentPage = currentPage;
info.hash = hash; //TODO remove the hash check and add UUIDs for libraries
info.id = comicId;
DBHelper::updateFromRemoteClient(libraryId,info);
}
}
}
else
{
response.setStatus(412,"No comic info received");
response.writeText("",true);
return;
}
response.write("OK",true);
}

View File

@ -106,13 +106,14 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) {
QRegExp comicPage("/library/.+/comic/[0-9]+/page/[0-9]+/?"); //get comic page
QRegExp comicPageRemote("/library/.+/comic/[0-9]+/page/[0-9]+/remote?"); //get comic page (remote reading)
QRegExp sync("/library/.+/sync");
QRegExp sync("/sync");
QRegExp library("/library/([0-9]+)/.+"); //permite verificar que la biblioteca solicitada existe
path = QUrl::fromPercentEncoding(path).toUtf8();
loadSession(request, response);
if(!sync.exactMatch(path)) //no session is needed for syncback info, until security will be added
loadSession(request, response);
//primera petición, se ha hecho un post, se sirven las bibliotecas si la seguridad mediante login no está habilitada
if(path == "/") //Don't send data to the server using '/' !!!!
@ -123,6 +124,7 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) {
{
if(sync.exactMatch(path))
SyncController().service(request, response);
else
{
//se comprueba que la sesión sea la correcta con el fin de evitar accesos no autorizados
HttpSession session=Static::sessionStore->getSession(request,response,false);