yacreader/YACReaderLibrary/server/controllers/foldercontentcontroller.cpp
Luis Ángel San Martín 3df1e99929 replaced separator char
2016-01-25 22:28:15 +01:00

71 lines
2.3 KiB
C++

#include "foldercontentcontroller.h"
#include <QUrl>
#include "db_helper.h"
#include "comic_db.h"
#include "folder.h"
#include "qnaturalsorting.h"
#include <ctime>
using namespace std;
struct LibraryItemSorter
{
bool operator()(const LibraryItem * a,const LibraryItem * b) const
{
return naturalSortLessThanCI(a->name,b->name);
}
};
FolderContentController::FolderContentController() {}
void FolderContentController::service(HttpRequest& request, HttpResponse& response)
{
response.setHeader("Content-Type", "text/plain; charset=utf-8");
QString path = QUrl::fromPercentEncoding(request.getPath()).toUtf8();
QStringList pathElements = path.split('/');
int libraryId = pathElements.at(2).toInt();
qulonglong parentId = pathElements.at(4).toULongLong();
serviceContent(libraryId, parentId, response);
response.writeText("",true);
}
void FolderContentController::serviceContent(const int &library, const qulonglong &folderId, HttpResponse &response)
{
clock_t begin = clock();
QList<LibraryItem *> folderContent = DBHelper::getFolderSubfoldersFromLibrary(library,folderId);
QList<LibraryItem *> folderComics = DBHelper::getFolderComicsFromLibrary(library,folderId);
folderContent.append(folderComics);
qSort(folderContent.begin(),folderContent.end(),LibraryItemSorter());
folderComics.clear();
ComicDB * currentComic;
Folder * currentFolder;
for(QList<LibraryItem *>::const_iterator itr = folderContent.constBegin();itr!=folderContent.constEnd();itr++)
{
if((*itr)->isDir())
{
currentFolder = (Folder *)(*itr);
response.writeText(QString("f\t%1\t%2\t%3\t%4\t%5\r\n").arg(library).arg(currentFolder->id).arg(currentFolder->name).arg(currentFolder->getNumChildren()).arg(currentFolder->getFirstChildHash()));
}
else
{
currentComic = (ComicDB *)(*itr);
response.writeText(QString("c\t%1\t%2\t%3\t%4\t%5\r\n").arg(library).arg(currentComic->id).arg(currentComic->getFileName()).arg(currentComic->getFileSize()).arg(currentComic->info.hash));
}
}
clock_t end = clock();
double msecs = double(end - begin);
response.writeText(QString("%1ms").arg(msecs));
}