fixed levels browsing in web library (up and drill down)

This commit is contained in:
Luis Ángel San Martín 2014-08-19 16:10:22 +02:00
parent acf04b5cd8
commit 679cd96d5b
5 changed files with 102 additions and 121 deletions

View File

@ -11,6 +11,8 @@
#include "qnaturalsorting.h"
#include "QsLog.h"
struct LibraryItemSorter
{
bool operator()(const LibraryItem * a,const LibraryItem * b) const
@ -37,23 +39,23 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
QStringList pathElements = path.split('/');
int libraryId = pathElements.at(2).toInt();
QString libraryName = DBHelper::getLibraryName(libraryId);
qulonglong parentId = pathElements.at(4).toULongLong();
qulonglong folderId = pathElements.at(4).toULongLong();
parentId = qMax<qulonglong>(1,parentId);
folderId = qMax<qulonglong>(1,folderId);
QString folderName = DBHelper::getFolderName(libraryName,parentId);
QString folderName = DBHelper::getFolderName(libraryName,folderId);
if(folderName.isEmpty())
{
ErrorController(300).service(request,response);
return;
}
if(parentId!=1)
if(folderId!=1)
t.setVariable("folder.name",folderName);
else
t.setVariable("folder.name",libraryName);
QList<LibraryItem *> folderContent = DBHelper::getFolderContentFromLibrary(libraryName,parentId);
QList<LibraryItem *> folderComics = DBHelper::getFolderComicsFromLibrary(libraryName,parentId);
QList<LibraryItem *> folderContent = DBHelper::getFolderContentFromLibrary(libraryName,folderId);
QList<LibraryItem *> folderComics = DBHelper::getFolderComicsFromLibrary(libraryName,folderId);
//response.writeText(libraryName);
@ -62,7 +64,7 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
qSort(folderContent.begin(),folderContent.end(),LibraryItemSorter());
folderComics.clear();
qulonglong backId = DBHelper::getParentFromComicFolderId(libraryName,parentId);
//qulonglong backId = DBHelper::getParentFromComicFolderId(libraryName,folderId);
int page = 0;
QByteArray p = request.getParameter("page");
@ -79,67 +81,50 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
if(map.contains("up"))
fromUp = true;
int upPage = 0;
//int upPage = 0;
if(parentId == 1)
session.clearFoldersPath();
else
if(folderId == 1)
{
if(fromUp)
session.popFolder();
else
if(session.getFoldersPath().contains(parentId))
{
while(session.topFolder()!=parentId)
session.popFolder();
}
else
session.pushFolder(parentId);
}
if(backId == 1 && parentId == 1)
{
session.popPage();
session.pushPage(page);
t.setVariable(QString("upurl"),"/?page=0");
session.clearNavigationPath();
session.pushNavigationItem(QPair<qulonglong,quint32>(folderId,page));
t.setVariable(QString("upurl"),"/");
}
else
{
if(fromUp)
session.popNavigationItem();
else //drill down or direct access
{
session.popPage();
upPage = session.topPage();
page = upPage;
}
else //este nivel puede haberse cargado por primera vez ó puede que estemos navegando horizontalmente
if(p.length() == 0) // acabamos de entrar
QStack<QPair<qulonglong, quint32> > path = session.getNavigationPath();
bool found=false;
for(QStack<QPair<qulonglong, quint32> >::const_iterator itr = path.begin(); itr!=path.end(); itr++)
if(itr->first == folderId)
{
upPage = session.topPage();
session.pushPage(page);
}
else //navegación horizontal
{
session.popPage();
upPage = session.topPage();
session.pushPage(page);
}
t.setVariable(QString("upurl"),"/library/" + QString::number(libraryId) + "/folder/" +QString("%1?page=%2&up=true").arg(backId).arg(upPage));
found = true;
break;
}
/*if(currentPath.length()>0)
{
if(currentPath.contains(QString("%1").arg(parentId))
if(found)
{
while(session.topNavigationItem().first != folderId)
session.popNavigationItem();
session.updateTopItem(QPair<qulonglong,quint32>(folderId,page));
}
else
{
session.set("currentPath",currentPath+QString("/%1/%2").arg(parentId).arg(page);
session.pushNavigationItem(QPair<qulonglong,quint32>(folderId,page));
}
}*/
//t.loop("element",folderContent.length());
QStack<QPair<qulonglong, quint32> > path = session.getNavigationPath();
if(path.length()>1)
{
QPair<qulonglong, quint32> parentItem = path.at(path.length()-2);
qulonglong upParent = parentItem.first;
quint32 upPage = parentItem.second;
t.setVariable(QString("upurl"),"/library/" + QString::number(libraryId) + "/folder/" +QString("%1?page=%2&up=true").arg(upParent).arg(upPage));
} else
t.setVariable(QString("upurl"),"/");
}
int elementsPerPage = 24;
@ -161,14 +146,13 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
int numFoldersAtCurrentPage = qMax(0,qMin(numFolders - indexCurrentPage, elementsPerPage));
//PATH
QStack<int> foldersPath = session.getFoldersPath();
QStack<QPair<qulonglong,quint32> > foldersPath = session.getNavigationPath();
t.setVariable(QString("library.name"),libraryName);
t.setVariable(QString("library.url"),QString("/library/%1/folder/1").arg(libraryId));
t.loop("path",foldersPath.count());
for(int i = 0; i < foldersPath.count(); i++){
t.setVariable(QString("path%1.url").arg(i),QString("/library/%1/folder/%2").arg(libraryId).arg(foldersPath[i]));
t.setVariable(QString("path%1.name").arg(i),DBHelper::getFolderName(libraryName,foldersPath[i]));
t.loop("path",foldersPath.count()-1);
for(int i = 1; i < foldersPath.count(); i++){
t.setVariable(QString("path%1.url").arg(i-1),QString("/library/%1/folder/%2").arg(libraryId).arg(foldersPath[i].first));
t.setVariable(QString("path%1.name").arg(i-1),DBHelper::getFolderName(libraryName,foldersPath[i].first));
}
t.loop("element",numFoldersAtCurrentPage);
@ -280,7 +264,7 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
{
//response.writeText(QString("%1 - %2 <br />").arg(*itr).arg(count));
t.setVariable(QString("index%1.indexname").arg(i), *itr);
t.setVariable(QString("index%1.url").arg(i),QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(parentId).arg(indexPage));
t.setVariable(QString("index%1.url").arg(i),QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(folderId).arg(indexPage));
i++;
count += indexCount.value(*itr);
indexPage = count/elementsPerPage;
@ -298,7 +282,7 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
while(z < numPages)
{
t.setVariable(QString("page%1.url").arg(z),QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(parentId).arg(z));
t.setVariable(QString("page%1.url").arg(z),QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(folderId).arg(z));
t.setVariable(QString("page%1.number").arg(z),QString("%1").arg(z+1));
if(page == z)
t.setVariable(QString("page%1.current").arg(z),"current");
@ -307,10 +291,10 @@ void FolderController::service(HttpRequest& request, HttpResponse& response)
z++;
}
t.setVariable("page.first",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(parentId).arg(0));
t.setVariable("page.previous",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(parentId).arg((page==0)?page:page-1));
t.setVariable("page.next",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(parentId).arg((page==numPages-1)?page:page+1));
t.setVariable("page.last",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(parentId).arg(numPages-1));
t.setVariable("page.first",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(folderId).arg(0));
t.setVariable("page.previous",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(folderId).arg((page==0)?page:page-1));
t.setVariable("page.next",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(folderId).arg((page==numPages-1)?page:page+1));
t.setVariable("page.last",QString("/library/%1/folder/%2?page=%3").arg(libraryId).arg(folderId).arg(numPages-1));
t.setCondition("index", true);
}
else

View File

@ -11,13 +11,12 @@ LibrariesController::LibrariesController() {}
void LibrariesController::service(HttpRequest& request, HttpResponse& response)
{
HttpSession session=Static::sessionStore->getSession(request,response);
HttpSession session=Static::sessionStore->getSession(request,response,false);
response.setHeader("Content-Type", "text/html; charset=ISO-8859-1");
response.setHeader("Connection","close");
session.clearNavigationPath();
session.clearFoldersPath();
Template t=Static::templateLoader->getTemplate("libraries_"+session.getDeviceType(),request.getHeader("Accept-Language"));
t.enableWarnings();

View File

@ -339,55 +339,43 @@ void HttpSession::clearNavigationPath()
dataPtr->yacreaderSessionData.navigationPath.clear();
}
int HttpSession::popPage()
QPair<qulonglong, quint32> HttpSession::popNavigationItem()
{
if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty()))
return dataPtr->yacreaderSessionData.navigationPath.pop();
return 0;
return QPair<qulonglong, quint32>();
}
void HttpSession::pushPage(int page)
QPair<qulonglong, quint32> HttpSession::topNavigationItem()
{
if(dataPtr)
dataPtr->yacreaderSessionData.navigationPath.push(page);
}
int HttpSession::topPage()
{
if(dataPtr)
if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty()))
return dataPtr->yacreaderSessionData.navigationPath.top();
return 0;
return QPair<qulonglong, quint32>();
}
void HttpSession::clearFoldersPath()
void HttpSession::pushNavigationItem(const QPair<qulonglong, quint32> &item)
{
if(dataPtr)
dataPtr->yacreaderSessionData.foldersPath.clear();
dataPtr->yacreaderSessionData.navigationPath.push(item);
}
int HttpSession::popFolder()
void HttpSession::updateTopItem(const QPair<qulonglong, quint32> &item)
{
if(dataPtr && !(dataPtr->yacreaderSessionData.foldersPath.isEmpty()))
return dataPtr->yacreaderSessionData.foldersPath.pop();
return 0;
if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty()))
{
dataPtr->yacreaderSessionData.navigationPath.pop();
dataPtr->yacreaderSessionData.navigationPath.push(item);
} else if(dataPtr)
{
dataPtr->yacreaderSessionData.navigationPath.push(item);
}
}
void HttpSession::pushFolder(int page)
QStack<QPair<qulonglong, quint32> > HttpSession::getNavigationPath()
{
if(dataPtr)
dataPtr->yacreaderSessionData.foldersPath.push(page);
return dataPtr->yacreaderSessionData.navigationPath;
else
return QStack<QPair<qulonglong, quint32> >();
}
int HttpSession::topFolder()
{
if(dataPtr)
return dataPtr->yacreaderSessionData.foldersPath.top();
return 0;
}
QStack<int> HttpSession::getFoldersPath()
{
if(dataPtr)
return dataPtr->yacreaderSessionData.foldersPath;
return QStack<int>();
}

View File

@ -121,8 +121,8 @@ public:
void setDeviceType(const QString & device);
void setDisplayType(const QString & display);
void clearNavigationPath();
int popPage();
/*int popPage();
void pushPage(int page);
int topPage();
@ -130,7 +130,19 @@ public:
int popFolder();
void pushFolder(int page);
int topFolder();
QStack<int> getFoldersPath();
QStack<int> getFoldersPath();*/
void clearNavigationPath();
QPair<qulonglong, quint32> popNavigationItem();
QPair<qulonglong, quint32> topNavigationItem();
void pushNavigationItem(const QPair<qulonglong, quint32> & item);
void updateTopItem(const QPair<qulonglong, quint32> & item);
//TODO replace QPair by a custom class for storing folderId, page and folderName(save some DB accesses)
QStack<QPair<qulonglong, quint32> > getNavigationPath();
private:
@ -145,8 +157,8 @@ private:
qulonglong comicId;
qulonglong remoteComicId;
QStack<int> navigationPath;
QStack<int> foldersPath;
//folder_id, page_number
QStack<QPair<qulonglong, quint32> > navigationPath;
Comic * comic;
Comic * remoteComic;

View File

@ -70,8 +70,6 @@ void RequestMapper::loadSession(HttpRequest & request, HttpResponse& response)
QList<QString> data = postData.split("\n");
QLOG_INFO() << "Data lenght : " << data.length();
if(data.length() > 2)
{
session.setDeviceType(data.at(0).split(":").at(1));
@ -112,7 +110,7 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) {
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 == "/")
if(path == "/") //Don't send data to the server using '/' !!!!
{
LibrariesController().service(request, response);
}