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

View File

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

View File

@ -335,59 +335,47 @@ void HttpSession::setDisplayType(const QString & display)
void HttpSession::clearNavigationPath() void HttpSession::clearNavigationPath()
{ {
if(dataPtr) if(dataPtr)
dataPtr->yacreaderSessionData.navigationPath.clear(); dataPtr->yacreaderSessionData.navigationPath.clear();
} }
int HttpSession::popPage() QPair<qulonglong, quint32> HttpSession::popNavigationItem()
{ {
if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty())) if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty()))
return dataPtr->yacreaderSessionData.navigationPath.pop(); return dataPtr->yacreaderSessionData.navigationPath.pop();
return 0; return QPair<qulonglong, quint32>();
} }
void HttpSession::pushPage(int page) QPair<qulonglong, quint32> HttpSession::topNavigationItem()
{ {
if(dataPtr) if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty()))
dataPtr->yacreaderSessionData.navigationPath.push(page); return dataPtr->yacreaderSessionData.navigationPath.top();
return QPair<qulonglong, quint32>();
} }
int HttpSession::topPage() void HttpSession::pushNavigationItem(const QPair<qulonglong, quint32> &item)
{
if(dataPtr)
return dataPtr->yacreaderSessionData.navigationPath.top();
return 0;
}
void HttpSession::clearFoldersPath()
{ {
if(dataPtr) 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())) if(dataPtr && !(dataPtr->yacreaderSessionData.navigationPath.isEmpty()))
return dataPtr->yacreaderSessionData.foldersPath.pop(); {
return 0; 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) 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 setDeviceType(const QString & device);
void setDisplayType(const QString & display); void setDisplayType(const QString & display);
void clearNavigationPath();
int popPage(); /*int popPage();
void pushPage(int page); void pushPage(int page);
int topPage(); int topPage();
@ -130,7 +130,19 @@ public:
int popFolder(); int popFolder();
void pushFolder(int page); void pushFolder(int page);
int topFolder(); 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: private:
@ -145,8 +157,8 @@ private:
qulonglong comicId; qulonglong comicId;
qulonglong remoteComicId; qulonglong remoteComicId;
QStack<int> navigationPath; //folder_id, page_number
QStack<int> foldersPath; QStack<QPair<qulonglong, quint32> > navigationPath;
Comic * comic; Comic * comic;
Comic * remoteComic; Comic * remoteComic;

View File

@ -70,8 +70,6 @@ void RequestMapper::loadSession(HttpRequest & request, HttpResponse& response)
QList<QString> data = postData.split("\n"); QList<QString> data = postData.split("\n");
QLOG_INFO() << "Data lenght : " << data.length();
if(data.length() > 2) if(data.length() > 2)
{ {
session.setDeviceType(data.at(0).split(":").at(1)); session.setDeviceType(data.at(0).split(":").at(1));
@ -112,8 +110,8 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) {
loadSession(request, response); loadSession(request, response);
//primera petición, se ha hecho un post, se sirven las bibliotecas si la seguridad mediante login no está habilitada //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); LibrariesController().service(request, response);
} }