diff --git a/YACReader/main_window_viewer.cpp b/YACReader/main_window_viewer.cpp index f9aecd0a..7a4f3c81 100644 --- a/YACReader/main_window_viewer.cpp +++ b/YACReader/main_window_viewer.cpp @@ -1681,12 +1681,26 @@ void MainWindowViewer::decreasePageZoomLevel() void MainWindowViewer::sendComic() { - YACReaderLocalClient * client = new YACReaderLocalClient; + YACReaderLocalClient * client = new YACReaderLocalClient; currentComicDB.info.lastTimeOpened = QDateTime::currentSecsSinceEpoch(); - viewer->updateComic(currentComicDB); - int retries = 1; - while(!client->sendComicInfo(libraryId,currentComicDB) && retries!=0) - retries--; - connect(client,SIGNAL(finished()),client,SLOT(deleteLater())); - //delete client; + viewer->updateComic(currentComicDB); + + if (currentComicDB.info.currentPage == currentComicDB.info.numPages) { + int currentIndex = siblingComics.indexOf(currentComicDB); + if(currentIndex+1 > 0 && currentIndex+1 < siblingComics.count()) + { + ComicDB & nextComic = siblingComics[currentIndex+1]; + nextComic.info.hasBeenOpened = true; + int retries = 1; + while(!client->sendComicInfo(libraryId,currentComicDB,nextComic.id) && retries!=0) + retries--; + connect(client,SIGNAL(finished()),client,SLOT(deleteLater())); + } + } else { + int retries = 1; + while(!client->sendComicInfo(libraryId,currentComicDB) && retries!=0) + retries--; + connect(client,SIGNAL(finished()),client,SLOT(deleteLater())); + } + //delete client; } diff --git a/YACReader/yacreader_local_client.cpp b/YACReader/yacreader_local_client.cpp index 5f246426..85b235fb 100644 --- a/YACReader/yacreader_local_client.cpp +++ b/YACReader/yacreader_local_client.cpp @@ -125,47 +125,92 @@ bool YACReaderLocalClient::requestComicInfo(quint64 libraryId, ComicDB & comic, } } + bool YACReaderLocalClient::sendComicInfo(quint64 libraryId, ComicDB & comic) { - localSocket->connectToServer(YACREADERLIBRARY_GUID); - if(localSocket->isOpen()) - { + localSocket->connectToServer(YACREADERLIBRARY_GUID); + if(localSocket->isOpen()) + { //QLOG_INFO() << "Connection opened for sending ComicInfo"; - QByteArray block; - QDataStream out(&block, QIODevice::WriteOnly); - out.setVersion(QDataStream::Qt_4_8); - out << (quint32)0; - out << (quint8)YACReader::SendComicInfo; - out << libraryId; - out << comic; - out.device()->seek(0); - out << (quint32)(block.size() - sizeof(quint32)); + QByteArray block; + QDataStream out(&block, QIODevice::WriteOnly); + out.setVersion(QDataStream::Qt_4_8); + out << (quint32)0; + out << (quint8)YACReader::SendComicInfo; + out << libraryId; + out << comic; + out.device()->seek(0); + out << (quint32)(block.size() - sizeof(quint32)); - int written, previousWritten; - written = previousWritten = 0; - int tries = 0; - while(written != block.size() && tries < 100) - { - written += localSocket->write(block); + int written, previousWritten; + written = previousWritten = 0; + int tries = 0; + while(written != block.size() && tries < 100) + { + written += localSocket->write(block); if(written == previousWritten) tries++; previousWritten = written; - } + } localSocket->waitForBytesWritten(2000); - localSocket->close(); + localSocket->close(); //QLOG_INFO() << QString("Sending Comic Info : writen data (%1,%2)").arg(written).arg(block.size()); - if(tries == 100 && written != block.size()) - { - emit finished(); - QLOG_ERROR() << QString("Sending Comic Info : unable to write data (%1,%2)").arg(written).arg(block.size()); - return false; - } - emit finished(); - return true; - } - - emit finished(); - QLOG_ERROR() << "Sending Comic Info : unable to connect to the server"; - return false; + if(tries == 100 && written != block.size()) + { + emit finished(); + QLOG_ERROR() << QString("Sending Comic Info : unable to write data (%1,%2)").arg(written).arg(block.size()); + return false; + } + emit finished(); + return true; + } + emit finished(); + QLOG_ERROR() << "Sending Comic Info : unable to connect to the server"; + return false; +} + +bool YACReaderLocalClient::sendComicInfo(quint64 libraryId, ComicDB & comic, qulonglong nextComicId) +{ + localSocket->connectToServer(YACREADERLIBRARY_GUID); + if(localSocket->isOpen()) + { + //QLOG_INFO() << "Connection opened for sending ComicInfo"; + QByteArray block; + QDataStream out(&block, QIODevice::WriteOnly); + out.setVersion(QDataStream::Qt_4_8); + out << (quint32)0; + out << (quint8)YACReader::SendComicInfo; + out << libraryId; + out << comic; + out << nextComicId; + out.device()->seek(0); + out << (quint32)(block.size() - sizeof(quint32)); + + int written, previousWritten; + written = previousWritten = 0; + int tries = 0; + while(written != block.size() && tries < 100) + { + written += localSocket->write(block); + if(written == previousWritten) + tries++; + previousWritten = written; + } + localSocket->waitForBytesWritten(2000); + localSocket->close(); + //QLOG_INFO() << QString("Sending Comic Info : writen data (%1,%2)").arg(written).arg(block.size()); + if(tries == 100 && written != block.size()) + { + emit finished(); + QLOG_ERROR() << QString("Sending Comic Info : unable to write data (%1,%2)").arg(written).arg(block.size()); + return false; + } + emit finished(); + return true; + } + + emit finished(); + QLOG_ERROR() << "Sending Comic Info : unable to connect to the server"; + return false; } diff --git a/YACReader/yacreader_local_client.h b/YACReader/yacreader_local_client.h index 001cb1e7..2f08825b 100644 --- a/YACReader/yacreader_local_client.h +++ b/YACReader/yacreader_local_client.h @@ -18,6 +18,8 @@ public slots: void readMessage(); bool requestComicInfo(quint64 libraryId, ComicDB & comic,QList & siblings); bool sendComicInfo(quint64 libraryId, ComicDB & comic); + bool sendComicInfo(quint64 libraryId, ComicDB & comic, qulonglong nextComicId); + private: QLocalSocket * localSocket; diff --git a/YACReaderLibrary/yacreader_local_server.cpp b/YACReaderLibrary/yacreader_local_server.cpp index fb6da837..b8ee39d0 100644 --- a/YACReaderLibrary/yacreader_local_server.cpp +++ b/YACReaderLibrary/yacreader_local_server.cpp @@ -99,6 +99,7 @@ void YACReaderClientConnectionWorker::run() quint64 libraryId; ComicDB comic; + qulonglong nextComicId; int tries = 0; int dataAvailable = 0; QByteArray packageSize; @@ -147,6 +148,15 @@ void YACReaderClientConnectionWorker::run() dataStream >> libraryId; dataStream >> comic; + bool nextComicInfoAvailable; + + if(dataStream.atEnd()) { + nextComicInfoAvailable = false; + } else { + nextComicInfoAvailable = true; + dataStream >> nextComicId; + } + switch (msgType) { case YACReader::RequestComicInfo: @@ -183,11 +193,13 @@ void YACReaderClientConnectionWorker::run() } case YACReader::SendComicInfo: { + if (nextComicInfoAvailable) { + updateComic(libraryId,comic,nextComicId); + } else { updateComic(libraryId,comic); - //clientConnection->disconnectFromServer(); + } break; } - } clientConnection->waitForDisconnected(); @@ -216,3 +228,14 @@ void YACReaderClientConnectionWorker::updateComic(quint64 libraryId, ComicDB & c DBHelper::update(libraryId, comic.info); emit comicUpdated(libraryId, comic); } + +void YACReaderClientConnectionWorker::updateComic(quint64 libraryId, ComicDB & comic, qulonglong nextComicId) +{ + QMutexLocker locker(&dbMutex); + DBHelper::update(libraryId, comic.info); + ComicInfo nextcomicinfo; + nextcomicinfo.id = nextComicId; + DBHelper::setComicAsReading(libraryId, nextcomicinfo); + + emit comicUpdated(libraryId, comic); +} diff --git a/YACReaderLibrary/yacreader_local_server.h b/YACReaderLibrary/yacreader_local_server.h index d5432e60..c104e421 100644 --- a/YACReaderLibrary/yacreader_local_server.h +++ b/YACReaderLibrary/yacreader_local_server.h @@ -43,6 +43,8 @@ private: void getComicInfo(quint64 libraryId, ComicDB & comic, QList & sibling); void updateComic(quint64 libraryId, ComicDB & comic); + void updateComic(quint64 libraryId, ComicDB & comic, qulonglong nextComicId); + QLocalSocket *clientConnection; };