From bf8c0109eb7001546f9da4f6a7e6e449059bbf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Fri, 19 Jul 2013 21:05:23 +0200 Subject: [PATCH] server/client local ipc working fine --- YACReader/comic.cpp | 2 +- YACReader/main_window_viewer.cpp | 40 +++++---- YACReader/yacreader_local_client.cpp | 39 ++++++--- YACReader/yacreader_local_client.h | 2 +- YACReaderLibrary/YACReaderLibrary.pro | 2 +- YACReaderLibrary/db_helper.cpp | 61 +++++++++++++ YACReaderLibrary/db_helper.h | 2 + YACReaderLibrary/library_window.cpp | 27 +++--- YACReaderLibrary/yacreader_local_server.cpp | 94 +++++++++------------ YACReaderLibrary/yacreader_local_server.h | 2 +- common/yacreader_global.h | 6 ++ 11 files changed, 169 insertions(+), 108 deletions(-) diff --git a/YACReader/comic.cpp b/YACReader/comic.cpp index 30f62742..b9e9d83f 100644 --- a/YACReader/comic.cpp +++ b/YACReader/comic.cpp @@ -188,7 +188,7 @@ FileComic::FileComic(const QString & path, int atPage ) FileComic::~FileComic() { - _pages.clear(); + } bool FileComic::load(const QString & path, int atPage) diff --git a/YACReader/main_window_viewer.cpp b/YACReader/main_window_viewer.cpp index 74b633d7..ec8e240d 100644 --- a/YACReader/main_window_viewer.cpp +++ b/YACReader/main_window_viewer.cpp @@ -156,42 +156,40 @@ void MainWindowViewer::setupUI() { //TODO: new method open(QString) QString pathFile = QCoreApplication::arguments().at(1); - currentDirectory = pathFile; QFileInfo fi(pathFile); + currentDirectory = fi.absoluteDir().path(); getSiblingComics(fi.absolutePath(),fi.fileName()); setWindowTitle("YACReader - " + fi.fileName()); enableActions(); viewer->open(pathFile); } - /*else if(QCoreApplication::argc() == 5) + else if(QCoreApplication::argc() == 5) { QString pathFile = QCoreApplication::arguments().at(1); currentDirectory = pathFile; quint64 comicId = QCoreApplication::arguments().at(2).toULongLong(); quint64 libraryId = QCoreApplication::arguments().at(3).toULongLong(); int page = QCoreApplication::arguments().at(4).toULongLong(); - - QFileInfo fi(pathFile); enableActions(); //TODO request data to the server - ComicDB comic; - comic.id = comicId; + + currentComicDB.id = comicId; YACReaderLocalClient client; - //if(client.requestComicInfo(libraryId,comic)) + if(client.requestComicInfo(libraryId,currentComicDB,siblingComics)) { - if(comic.info.title == 0 || comic.info.title->isEmpty() ) - setWindowTitle("YACReader - " + fi.fileName()); + if(currentComicDB.info.title == 0 || currentComicDB.info.title->isEmpty() ) + setWindowTitle("YACReader - " + currentComicDB.path); else - setWindowTitle("YACReader - " + *comic.info.title); + setWindowTitle("YACReader - " + *currentComicDB.info.title); } - //else - setWindowTitle("YACReader : " + fi.fileName()); - viewer->open(pathFile,page); - } */ + else + setWindowTitle("YACReader : " + currentComicDB.path); + open(pathFile+currentComicDB.path,currentComicDB,siblingComics); + } versionChecker = new HttpVersionChecker(); @@ -594,11 +592,10 @@ void MainWindowViewer::open() void MainWindowViewer::open(QString path, ComicDB & comic, QList & siblings) { - currentComicDB = comic; - siblingComics = siblings; + //currentComicDB = comic; + //siblingComics = siblings; QFileInfo fi(path); - currentDirectory = fi.absoluteDir().path(); if(comic.info.title != 0 && !comic.info.title->isEmpty()) setWindowTitle("YACReader - " + *comic.info.title); @@ -866,8 +863,8 @@ void MainWindowViewer::openPreviousComic() if(!siblingComics.isEmpty()) { int currentIndex = siblingComics.indexOf(currentComicDB); - ComicDB previoiusComic = siblingComics.at(currentIndex-1); - open(currentDirectory+previoiusComic.path,previoiusComic,siblingComics); + currentComicDB = siblingComics.at(currentIndex-1); + open(currentDirectory+currentComicDB.path,currentComicDB,siblingComics); return; } if(!previousComicPath.isEmpty()) @@ -885,8 +882,9 @@ void MainWindowViewer::openNextComic() if(!siblingComics.isEmpty()) { int currentIndex = siblingComics.indexOf(currentComicDB); - ComicDB nextComic = siblingComics.at(currentIndex+1); - open(currentDirectory+nextComic.path,nextComic,siblingComics); + if(currentIndex+1 > 0 && currentIndex+1 < siblingComics.count()) + currentComicDB = siblingComics.at(currentIndex+1); + open(currentDirectory+currentComicDB.path,currentComicDB,siblingComics); return; } if(!nextComicPath.isEmpty()) diff --git a/YACReader/yacreader_local_client.cpp b/YACReader/yacreader_local_client.cpp index ad03f2f6..4ced2baf 100644 --- a/YACReader/yacreader_local_client.cpp +++ b/YACReader/yacreader_local_client.cpp @@ -9,7 +9,7 @@ YACReaderLocalClient::YACReaderLocalClient(QObject *parent) : { localSocket = new QLocalSocket(this); - connect(localSocket, SIGNAL(readyRead()), this, SLOT(readMessage())); + //connect(localSocket, SIGNAL(readyRead()), this, SLOT(readMessage())); /*connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(displayError(QLocalSocket::LocalSocketError)));*/ @@ -20,16 +20,22 @@ void YACReaderLocalClient::readMessage() { } - -bool YACReaderLocalClient::requestComicInfo(quint64 libraryId, ComicDB & comic) +#include +bool YACReaderLocalClient::requestComicInfo(quint64 libraryId, ComicDB & comic, QList & siblings) { localSocket->connectToServer(YACREADERLIBRARY_GUID); if(localSocket->isOpen()) { + QFile f("c:/temp/socket.txt"); + f.open(QIODevice::WriteOnly); + QTextStream outt(&f); + outt << QString(" antes : %1").arg(comic.id) << endl; + QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_4_0); + out.setVersion(QDataStream::Qt_4_8); out << (quint16)0; + out << (quint8)YACReaderIPCMessages::RequestComicInfo; out << libraryId; out << comic; out.device()->seek(0); @@ -41,18 +47,23 @@ bool YACReaderLocalClient::requestComicInfo(quint64 libraryId, ComicDB & comic) written += localSocket->write(block); localSocket->flush(); } - localSocket->waitForReadyRead(); - QByteArray data; - QDataStream in(data); - in.setVersion(QDataStream::Qt_4_0); - int totalSize = 0; - while((data.size()-sizeof(quint16)) != totalSize ) + + //QByteArray data; + while(localSocket->bytesAvailable() < sizeof(quint16)) + localSocket->waitForReadyRead(); + QDataStream sizeStream(localSocket->read(sizeof(quint16))); + sizeStream.setVersion(QDataStream::Qt_4_8); + quint16 totalSize = 0; + sizeStream >> totalSize; + + while(localSocket->bytesAvailable() < totalSize ) { - data.append(localSocket->read(1000000000)); - if(data.size()>=sizeof(quint16) && totalSize == 0) - in >> totalSize; + localSocket->waitForReadyRead(); } - in >> comic; + QDataStream dataStream(localSocket->read(totalSize)); + dataStream >> comic; + dataStream >> siblings; + outt << QString(" despues : %1").arg(comic.id) << endl; return true; } else diff --git a/YACReader/yacreader_local_client.h b/YACReader/yacreader_local_client.h index 96de6693..6de87d5f 100644 --- a/YACReader/yacreader_local_client.h +++ b/YACReader/yacreader_local_client.h @@ -16,7 +16,7 @@ signals: public slots: void readMessage(); - bool requestComicInfo(quint64 libraryId, ComicDB & comic); + bool requestComicInfo(quint64 libraryId, ComicDB & comic,QList & siblings); bool sendComicInfo(quint64 libraryId, ComicDB & comic); private: diff --git a/YACReaderLibrary/YACReaderLibrary.pro b/YACReaderLibrary/YACReaderLibrary.pro index 83498fe2..747a5eef 100644 --- a/YACReaderLibrary/YACReaderLibrary.pro +++ b/YACReaderLibrary/YACReaderLibrary.pro @@ -127,7 +127,7 @@ win32:RESOURCES += images_win.qrc unix:!macx:RESOURCES += images_win.qrc macx:RESOURCES += images_osx.qrc -include(../YACReader/YACReader.pri) +#include(../YACReader/YACReader.pri) RC_FILE = icon.rc diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index 91f5f88d..4d744833 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -105,6 +105,18 @@ ComicDB DBHelper::getComicInfo(const QString & libraryName, qulonglong id) QSqlDatabase::removeDatabase(libraryPath); return comic; } + +QList DBHelper::getSiblings(const QString & libraryName, qulonglong parentId) +{ + QString libraryPath = DBHelper::getLibraries().value(libraryName); + QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath+"/.yacreaderlibrary"); + + QList comics = DBHelper::getSortedComicsFromParent(parentId,db); + db.close(); + QSqlDatabase::removeDatabase(libraryPath); + return comics; +} + QString DBHelper::getFolderName(const QString & libraryName, qulonglong id) { QString libraryPath = DBHelper::getLibraries().value(libraryName); @@ -351,6 +363,55 @@ QList DBHelper::getFoldersFromParent(qulonglong parentId, QSqlDat return list; } +QList DBHelper::getSortedComicsFromParent(qulonglong parentId, QSqlDatabase & db) +{ + + QList list; + + QSqlQuery selectQuery(db); + selectQuery.prepare("select c.id,c.parentId,c.fileName,c.path,ci.hash from comic c inner join comic_info ci on (c.comicInfoId = ci.id) where c.parentId = :parentId"); + selectQuery.bindValue(":parentId", parentId); + selectQuery.exec(); + + ComicDB currentItem; + while (selectQuery.next()) + { + QList data; + QSqlRecord record = selectQuery.record(); + for(int i=0;i(list.back()); + QString nameLast = last.name; + QString nameCurrent = currentItem.name; + QList::iterator i; + i = list.end(); + i--; + while ((0 > (lessThan = nameCurrent.localeAwareCompare(nameLast))) && i != list.begin()) //se usa la misma ordenación que en QDir + { + i--; + nameLast = (*i).name; + } + if(lessThan>0) //si se ha encontrado un elemento menor que current, se inserta justo después + list.insert(++i,currentItem); + else + list.insert(i,currentItem); + + } + } + //selectQuery.finish(); + return list; +} QList DBHelper::getComicsFromParent(qulonglong parentId, QSqlDatabase & db, bool sort) { QList list; diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index 4c2d47de..68c93601 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -22,6 +22,7 @@ public: 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 QList getSiblings(const QString & libraryName, qulonglong parentId); static QString getFolderName(const QString & libraryName, qulonglong id); static QList getLibrariesNames(); static QString getLibraryName(int id); @@ -40,6 +41,7 @@ public: static void updateRead(ComicInfo * comicInfo, QSqlDatabase & db); //queries static QList getFoldersFromParent(qulonglong parentId, QSqlDatabase & db, bool sort = true); + static QList getSortedComicsFromParent(qulonglong parentId, QSqlDatabase & db); static QList getComicsFromParent(qulonglong parentId, QSqlDatabase & db, bool sort = true); //load static Folder loadFolder(qulonglong id, QSqlDatabase & db); diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 5811cbf4..8883988a 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -55,7 +55,6 @@ #include "comics_remover.h" #include "yacreader_library_list_widget.h" #include "yacreader_treeview.h" -#include "main_window_viewer.h" //#include "yacreader_social_dialog.h" // @@ -1005,26 +1004,22 @@ void LibraryWindow::openComic() if(!importedCovers) { ComicDB comic = dmCV->getComic(comicView->currentIndex()); - QString path = currentPath() + comic.path; - MainWindowViewer * viewer = new MainWindowViewer(); + QString path = currentPath(); QList siblings = dmCV->getAllComics(); - viewer->open(path,comic,siblings); - viewer->show(); - connect(viewer,SIGNAL(closed()),viewer,SLOT(deleteLater())); - /*quint64 comicId = comic.id; + quint64 comicId = comic.id; //TODO generate IDS for libraries... quint64 libraryId = selectedLibrary->currentIndex(); - int page = *(comic.info.numPages) / 2;*/ + int page = *(comic.info.numPages) / 2; -//#ifdef Q_OS_MAC -// -// QProcess::startDetached("open", QStringList() << "-n" << QDir::cleanPath(QCoreApplication::applicationDirPath()+"/../../../YACReader.app") << "--args" << path /*<< comicId << libraryId << page*/);//,QStringList() << path); -// //Comic is readed -//#else -// -// QProcess::startDetached(QDir::cleanPath(QCoreApplication::applicationDirPath())+QString("/YACReader \"%1\"").arg(path)/*.arg(comicId).arg(libraryId).arg(page)*/,QStringList()); -//#endif +#ifdef Q_OS_MAC + + QProcess::startDetached("open", QStringList() << "-n" << QDir::cleanPath(QCoreApplication::applicationDirPath()+"/../../../YACReader.app") << "--args" << path << comicId << libraryId << page);//,QStringList() << path); + //Comic is readed +#else + + QProcess::startDetached(QDir::cleanPath(QCoreApplication::applicationDirPath())+QString("/YACReader \"%1\" \"%2\" \"%3\" \"%4\"").arg(path).arg(comicId).arg(libraryId).arg(page),QStringList()); +#endif //Comic is readed setCurrentComicReaded(); } diff --git a/YACReaderLibrary/yacreader_local_server.cpp b/YACReaderLibrary/yacreader_local_server.cpp index d4d8c1db..41106536 100644 --- a/YACReaderLibrary/yacreader_local_server.cpp +++ b/YACReaderLibrary/yacreader_local_server.cpp @@ -23,79 +23,67 @@ bool YACReaderLocalServer::isListening() { return localServer->isListening(); } - +#include void YACReaderLocalServer::sendResponse() { - /*QLocalSocket *clientConnection = localServer->nextPendingConnection(); - - connect(clientConnection, SIGNAL(disconnected()), - clientConnection, SLOT(deleteLater())); - - QDataStream in(clientConnection); - in.setVersion(QDataStream::Qt_4_0); - - if (clientConnection->bytesAvailable() == 0) - return; - - if (in.atEnd()) - return; - - QString message; - in >> message; - - QByteArray block; - QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_4_0); - out << QString("OK"); - - clientConnection->write(block); - clientConnection->flush(); - clientConnection->disconnectFromServer();*/ - - QLocalSocket *clientConnection = localServer->nextPendingConnection(); + QLocalSocket *clientConnection = localServer->nextPendingConnection(); connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater())); quint64 libraryId; ComicDB comic; - clientConnection->waitForReadyRead(); - QByteArray data; - QDataStream inputStream(data); - inputStream.setVersion(QDataStream::Qt_4_0); - int totalSize = 0; - while((data.size()-sizeof(quint16)) != totalSize ) + + //QByteArray data; + while(clientConnection->bytesAvailable() < sizeof(quint16)) + clientConnection->waitForReadyRead(); + QDataStream sizeStream(clientConnection->read(sizeof(quint16))); + sizeStream.setVersion(QDataStream::Qt_4_8); + quint16 totalSize = 0; + sizeStream >> totalSize; + + while(clientConnection->bytesAvailable() < totalSize ) { - data.append(clientConnection->read(1000000000)); - if(data.size()>=sizeof(quint16) && totalSize == 0) - inputStream >> totalSize; + clientConnection->waitForReadyRead(); } - inputStream >> libraryId; - inputStream >> comic; + QDataStream dataStream(clientConnection->read(totalSize)); + quint8 msgType; + dataStream >> msgType; + dataStream >> libraryId; + dataStream >> comic; + QFile f("c:/temp/socket_server.txt"); + f.open(QIODevice::WriteOnly); + QTextStream outt(&f); + outt << QString(" antes : %1 - size : %2").arg(comic.id).arg(totalSize) << endl; - getComicInfo(libraryId,comic); + QList siblings; + getComicInfo(libraryId,comic,siblings); + + outt << QString(" despues : %1 - num sib : %2").arg(comic.id).arg(siblings.count()) << endl; + QByteArray block; - QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_4_0); + QDataStream out(&block, QIODevice::WriteOnly); + out.setVersion(QDataStream::Qt_4_8); out << (quint16)0; - out << comic; + out << comic; + out << siblings; out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); - int written = 0; - while(written != block.size()) - { - written += clientConnection->write(block); - clientConnection->flush(); - } - //clientConnection->waitForBytesWritten(); - clientConnection->disconnectFromServer(); - + int written = 0; + while(written != block.size()) + { + written += clientConnection->write(block); + clientConnection->flush(); + } + //clientConnection->waitForBytesWritten();*/ + //clientConnection->disconnectFromServer(); } -void YACReaderLocalServer::getComicInfo(quint64 libraryId, ComicDB & comic) +void YACReaderLocalServer::getComicInfo(quint64 libraryId, ComicDB & comic, QList & siblings) { comic = DBHelper::getComicInfo(DBHelper::getLibrariesNames().at(libraryId), comic.id); + siblings = DBHelper::getSiblings(DBHelper::getLibrariesNames().at(libraryId), comic.parentId); } bool YACReaderLocalServer::isRunning() diff --git a/YACReaderLibrary/yacreader_local_server.h b/YACReaderLibrary/yacreader_local_server.h index a48be198..c3d1badf 100644 --- a/YACReaderLibrary/yacreader_local_server.h +++ b/YACReaderLibrary/yacreader_local_server.h @@ -18,7 +18,7 @@ public slots: bool isListening(); void sendResponse(); static bool isRunning(); - void getComicInfo(quint64 libraryId, ComicDB & comic); + void getComicInfo(quint64 libraryId, ComicDB & comic, QList & sibling); private: QLocalServer * localServer; diff --git a/common/yacreader_global.h b/common/yacreader_global.h index 2beacf5d..9186a716 100644 --- a/common/yacreader_global.h +++ b/common/yacreader_global.h @@ -61,5 +61,11 @@ Custom }; + enum YACReaderIPCMessages + { + RequestComicInfo = 0, + SendComicInfo, + }; + #endif