diff --git a/YACReader/YACReader.pro b/YACReader/YACReader.pro index 174da361..078a691c 100644 --- a/YACReader/YACReader.pro +++ b/YACReader/YACReader.pro @@ -11,6 +11,9 @@ INCLUDEPATH += ../common win32 { INCLUDEPATH += ../dependencies/poppler/include LIBS += -L../dependencies/poppler/lib -lpoppler-qt4 +QMAKE_CXXFLAGS_RELEASE += /MP /Ob2 /Oi /Ot /GT /GL +QMAKE_LFLAGS_RELEASE += /LTCG +CONFIG -= embed_manifest_exe } unix:!macx{ diff --git a/YACReaderLibrary/YACReaderLibrary.pro b/YACReaderLibrary/YACReaderLibrary.pro index db4b1b57..8f3d3f37 100644 --- a/YACReaderLibrary/YACReaderLibrary.pro +++ b/YACReaderLibrary/YACReaderLibrary.pro @@ -10,10 +10,15 @@ INCLUDEPATH += ../common \ ./server \ ./db \ ../YACReader + +DEFINES += SERVER_RELEASE win32 { INCLUDEPATH += ../dependencies/poppler/include LIBS += -L../dependencies/poppler/lib -lpoppler-qt4 +QMAKE_CXXFLAGS_RELEASE += /MP /Ob2 /Oi /Ot /GT /GL +QMAKE_LFLAGS_RELEASE += /LTCG +CONFIG -= embed_manifest_exe } unix:!macx{ @@ -51,6 +56,7 @@ HEADERS += comic_flow.h \ import_comics_info_dialog.h \ server_config_dialog.h \ comic_flow_widget.h \ + db_helper.h \ ./db/data_base_management.h \ ./db/treeitem.h \ ./db/treemodel.h \ @@ -85,6 +91,7 @@ SOURCES += comic_flow.cpp \ import_comics_info_dialog.cpp \ server_config_dialog.cpp \ comic_flow_widget.cpp \ + db_helper.cpp \ ./db/data_base_management.cpp \ ./db/treeitem.cpp \ ./db/treemodel.cpp \ diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp new file mode 100644 index 00000000..4a1a55f0 --- /dev/null +++ b/YACReaderLibrary/db_helper.cpp @@ -0,0 +1,123 @@ +#include "db_helper.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "library_item.h" +#include "comic_db.h" +#include "data_base_management.h" +#include "folder.h" + +//server + +//TODO optimizar, evitar que se tenga que leer en cada petición el archivo +//conservar un QDateTime stático que compruebe si libraries.yacr ha sido modificado +//libraries debe ser una variable estática +static QDateTime lastModified; +static QMap libraries; + +QMap DBHelper::getLibraries() +{ + QFileInfo fi(QCoreApplication::applicationDirPath()+"/libraries.yacr"); + if(fi.lastModified() == lastModified) + return libraries; + + lastModified = fi.lastModified(); + libraries.clear(); + QFile f(QCoreApplication::applicationDirPath()+"/libraries.yacr"); + f.open(QIODevice::ReadOnly); + QTextStream txtS(&f); + QString content = txtS.readAll(); + QStringList lines = content.split('\n'); + QString line,name; + int i=0; + foreach(line,lines) + { + if((i%2)==0) + { + name = line; + } + else + { + libraries.insert(name.trimmed(),line.trimmed()); + } + i++; + } + f.close(); + return libraries; +} +QList DBHelper::getFolderContentFromLibrary(const QString & libraryName, qulonglong folderId) +{ + QString libraryPath = DBHelper::getLibraries().value(libraryName); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + QList list = Folder::getFoldersFromParent(folderId,db,false); + + db.close(); + QSqlDatabase::removeDatabase(libraryPath); + return list; +} +QList DBHelper::getFolderComicsFromLibrary(const QString & libraryName, qulonglong folderId) +{ + QString libraryPath = DBHelper::getLibraries().value(libraryName); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + QList list = ComicDB::getComicsFromParent(folderId,db,false); + + db.close(); + QSqlDatabase::removeDatabase(libraryPath); + return list; +} +qulonglong DBHelper::getParentFromComicFolderId(const QString & libraryName, qulonglong id) +{ + QString libraryPath = DBHelper::getLibraries().value(libraryName); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + Folder f(id,db); + + db.close(); + QSqlDatabase::removeDatabase(libraryPath); + return f.parentId; +} +ComicDB DBHelper::getComicInfo(const QString & libraryName, qulonglong id) +{ + QString libraryPath = DBHelper::getLibraries().value(libraryName); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + ComicDB comic; + comic.load(id,db); + + db.close(); + QSqlDatabase::removeDatabase(libraryPath); + return comic; +} +QString DBHelper::getFolderName(const QString & libraryName, qulonglong id) +{ + QString libraryPath = DBHelper::getLibraries().value(libraryName); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + QString name=""; + + { + QSqlQuery selectQuery(db); //TODO check + selectQuery.prepare("SELECT name FROM folder WHERE id = :id"); + selectQuery.bindValue(":id", id); + selectQuery.exec(); + + if(selectQuery.next()) + { + QSqlRecord record = selectQuery.record(); + name = record.value(0).toString(); + } + } + + db.close(); + QSqlDatabase::removeDatabase(libraryPath); + return name; +} \ No newline at end of file diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h new file mode 100644 index 00000000..e248d1b6 --- /dev/null +++ b/YACReaderLibrary/db_helper.h @@ -0,0 +1,22 @@ +#ifndef DB_HELPER_H +#define DB_HELPER_H + +class QString; +class LibraryItem; +class ComicDB; +#include +#include + +class DBHelper +{ +public: + //server + static QMap getLibraries(); + static QList getFolderContentFromLibrary(const QString & libraryName, qulonglong folderId); + static QList getFolderComicsFromLibrary(const QString & libraryName, qulonglong folderId); + static qulonglong getParentFromComicFolderId(const QString & libraryName, qulonglong id); + static ComicDB getComicInfo(const QString & libraryName, qulonglong id); + static QString getFolderName(const QString & libraryName, qulonglong id); +}; + +#endif \ No newline at end of file diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index a71fae04..7e1cd743 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -843,17 +843,21 @@ void LibraryWindow::reloadCovers() void LibraryWindow::centerComicFlow(const QModelIndex & mi) { - int distance = comicFlow->centerIndex()-mi.row(); - if(abs(distance)>10) + //TODO corregir el comportamiento de ComicFlowWidgetSW para evitar skip + if(typeid(comicFlow) == typeid(ComicFlowWidgetSW)) { - if(distance<0) - comicFlow->setCenterIndex(comicFlow->centerIndex()+(-distance)-10); + int distance = comicFlow->centerIndex()-mi.row(); + if(abs(distance)>10) + { + if(distance<0) + comicFlow->setCenterIndex(comicFlow->centerIndex()+(-distance)-10); + else + comicFlow->setCenterIndex(comicFlow->centerIndex()-distance+10); + skip = 10; + } else - comicFlow->setCenterIndex(comicFlow->centerIndex()-distance+10); - skip = 10; + skip = abs(comicFlow->centerIndex()-mi.row()); } - else - skip = abs(comicFlow->centerIndex()-mi.row()); comicFlow->showSlide(mi.row()); comicFlow->setFocus(Qt::OtherFocusReason); } diff --git a/YACReaderLibrary/main.cpp b/YACReaderLibrary/main.cpp index 23bb7998..1b4f1328 100644 --- a/YACReaderLibrary/main.cpp +++ b/YACReaderLibrary/main.cpp @@ -5,11 +5,8 @@ #define PICTUREFLOW_QT4 1 //interfaz al servidor -LibraryWindow * mw; Startup * s; - - int main( int argc, char ** argv ) { QApplication app( argc, argv ); @@ -25,11 +22,9 @@ int main( int argc, char ** argv ) s->start(); #endif - mw = new LibraryWindow(); + LibraryWindow * mw = new LibraryWindow(); mw->resize(800,480); mw->showMaximized(); - - return app.exec(); } diff --git a/YACReaderLibrary/server/controllers/comiccontroller.cpp b/YACReaderLibrary/server/controllers/comiccontroller.cpp index 79403fc2..a7b99ba6 100644 --- a/YACReaderLibrary/server/controllers/comiccontroller.cpp +++ b/YACReaderLibrary/server/controllers/comiccontroller.cpp @@ -1,8 +1,6 @@ #include "comiccontroller.h" -#include "library_window.h" - -extern LibraryWindow * mw; +#include "db_helper.h" #include "template.h" #include "../static.h" @@ -33,12 +31,17 @@ void ComicController::service(HttpRequest& request, HttpResponse& response) // response.write("",true); // return; // } - //} + //} - QMap libraries = mw->getLibraries(); + //Aplicar a todos los controladores + //TODO usar LibraryWindow para acceder a información de las bases de datos está mal, hay + //que crear una clase que se encargue de estas cosas + //¿Se está accediendo a la UI desde un hilo? + + QMap libraries = DBHelper::getLibraries(); - ComicDB comic = mw->getComicInfo(libraryName, comicId); + ComicDB comic = DBHelper::getComicInfo(libraryName, comicId); session.setDownloadedComic(comic.info.hash); diff --git a/YACReaderLibrary/server/controllers/covercontroller.cpp b/YACReaderLibrary/server/controllers/covercontroller.cpp index 3f8523be..6afa62fc 100644 --- a/YACReaderLibrary/server/controllers/covercontroller.cpp +++ b/YACReaderLibrary/server/controllers/covercontroller.cpp @@ -1,11 +1,9 @@ #include "covercontroller.h" -#include "library_window.h" //get libraries +#include "db_helper.h" //get libraries #include "template.h" #include "../static.h" -extern LibraryWindow * mw; - CoverController::CoverController() {} void CoverController::service(HttpRequest& request, HttpResponse& response) @@ -17,7 +15,7 @@ void CoverController::service(HttpRequest& request, HttpResponse& response) response.setHeader("Connection","close"); //response.setHeader("Content-Type", "plain/text; charset=ISO-8859-1"); - QMap libraries = mw->getLibraries(); + QMap libraries = DBHelper::getLibraries(); QString path = QUrl::fromPercentEncoding(request.getPath()).toLatin1(); QStringList pathElements = path.split('/'); @@ -55,5 +53,11 @@ void CoverController::service(HttpRequest& request, HttpResponse& response) img.save(&buffer, "JPG"); response.write(ba,true); } + //DONE else, hay que devolver un 404 + else + { + response.setStatus(404,"not found"); + response.write("404 not found",true); + } } diff --git a/YACReaderLibrary/server/controllers/errorcontroller.cpp b/YACReaderLibrary/server/controllers/errorcontroller.cpp index 02ae039c..7fc21f44 100644 --- a/YACReaderLibrary/server/controllers/errorcontroller.cpp +++ b/YACReaderLibrary/server/controllers/errorcontroller.cpp @@ -1,10 +1,8 @@ #include "errorcontroller.h" -#include "library_window.h" //get libraries #include "template.h" #include "../static.h" -extern LibraryWindow * mw; ErrorController::ErrorController(int errorCode) :error(errorCode) diff --git a/YACReaderLibrary/server/controllers/foldercontroller.cpp b/YACReaderLibrary/server/controllers/foldercontroller.cpp index e4b45abf..70e0a1c0 100644 --- a/YACReaderLibrary/server/controllers/foldercontroller.cpp +++ b/YACReaderLibrary/server/controllers/foldercontroller.cpp @@ -1,5 +1,6 @@ #include "foldercontroller.h" -#include "library_window.h" //get libraries +#include "db_helper.h" //get libraries +#include "comic_db.h" #include "folder.h" @@ -16,8 +17,6 @@ struct LibraryItemSorter } }; -extern LibraryWindow * mw; - FolderController::FolderController() {} void FolderController::service(HttpRequest& request, HttpResponse& response) @@ -36,13 +35,13 @@ void FolderController::service(HttpRequest& request, HttpResponse& response) QStringList pathElements = path.split('/'); QString libraryName = pathElements.at(2); qulonglong parentId = pathElements.at(4).toULongLong(); - QString folderName = mw->getFolderName(libraryName,parentId); + QString folderName = DBHelper::getFolderName(libraryName,parentId); if(parentId!=1) t.setVariable("folder.name",folderName); else t.setVariable("folder.name",libraryName); - QList folderContent = mw->getFolderContentFromLibrary(libraryName,parentId); - QList folderComics = mw->getFolderComicsFromLibrary(libraryName,parentId); + QList folderContent = DBHelper::getFolderContentFromLibrary(libraryName,parentId); + QList folderComics = DBHelper::getFolderComicsFromLibrary(libraryName,parentId); //response.writeText(libraryName); @@ -51,7 +50,7 @@ void FolderController::service(HttpRequest& request, HttpResponse& response) qSort(folderContent.begin(),folderContent.end(),LibraryItemSorter()); folderComics.clear(); - qulonglong backId = mw->getParentFromComicFolderId(libraryName,parentId); + qulonglong backId = DBHelper::getParentFromComicFolderId(libraryName,parentId); int page = 0; QByteArray p = request.getParameter("page"); diff --git a/YACReaderLibrary/server/controllers/folderinfocontroller.cpp b/YACReaderLibrary/server/controllers/folderinfocontroller.cpp index f9404a27..ba602fc8 100644 --- a/YACReaderLibrary/server/controllers/folderinfocontroller.cpp +++ b/YACReaderLibrary/server/controllers/folderinfocontroller.cpp @@ -1,5 +1,5 @@ #include "folderinfocontroller.h" -#include "library_window.h" //get libraries +#include "db_helper.h" //get libraries #include "folder.h" #include "comic_db.h" @@ -7,7 +7,6 @@ #include "template.h" #include "../static.h" -extern LibraryWindow * mw; FolderInfoController::FolderInfoController() {} @@ -19,8 +18,8 @@ void FolderInfoController::service(HttpRequest& request, HttpResponse& response) QStringList pathElements = path.split('/'); QString libraryName = pathElements.at(2); qulonglong parentId = pathElements.at(4).toULongLong(); - QList folderContent = mw->getFolderContentFromLibrary(libraryName,parentId); - QList folderComics = mw->getFolderComicsFromLibrary(libraryName,parentId); + QList folderContent = DBHelper::getFolderContentFromLibrary(libraryName,parentId); + QList folderComics = DBHelper::getFolderComicsFromLibrary(libraryName,parentId); Folder * currentFolder; for(QList::const_iterator itr = folderContent.constBegin();itr!=folderContent.constEnd();itr++) diff --git a/YACReaderLibrary/server/controllers/librariescontroller.cpp b/YACReaderLibrary/server/controllers/librariescontroller.cpp index 8c4efe88..adf60f06 100644 --- a/YACReaderLibrary/server/controllers/librariescontroller.cpp +++ b/YACReaderLibrary/server/controllers/librariescontroller.cpp @@ -1,11 +1,9 @@ #include "librariescontroller.h" -#include "library_window.h" //get libraries +#include "db_helper.h" //get libraries #include "template.h" #include "../static.h" -extern LibraryWindow * mw; - LibrariesController::LibrariesController() {} void LibrariesController::service(HttpRequest& request, HttpResponse& response) @@ -40,7 +38,7 @@ void LibrariesController::service(HttpRequest& request, HttpResponse& response) Template t=Static::templateLoader->getTemplate("libraries_"+session.getDeviceType(),request.getHeader("Accept-Language")); t.enableWarnings(); - QMap libraries = mw->getLibraries(); + QMap libraries = DBHelper::getLibraries(); QList names = libraries.keys(); t.loop("library",names.length()); diff --git a/YACReaderLibrary/server/requestmapper.cpp b/YACReaderLibrary/server/requestmapper.cpp index d7a64aa2..584f7689 100644 --- a/YACReaderLibrary/server/requestmapper.cpp +++ b/YACReaderLibrary/server/requestmapper.cpp @@ -20,9 +20,7 @@ #include "controllers/pagecontroller.h" #include "controllers/errorcontroller.h" -#include "library_window.h" - -extern LibraryWindow * mw; +#include "db_helper.h" RequestMapper::RequestMapper(QObject* parent) :HttpRequestHandler(parent) {} @@ -55,7 +53,7 @@ void RequestMapper::service(HttpRequest& request, HttpResponse& response) { HttpSession session=Static::sessionStore->getSession(request,response,false); if(!session.isNull() && session.contains("ySession")) { - if(library.indexIn(path)!=-1 && mw->getLibraries().contains(library.cap(1)) ) + if(library.indexIn(path)!=-1 && DBHelper::getLibraries().contains(library.cap(1)) ) { //listar el contenido del folder if(folder.exactMatch(path))