bookunrar.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "bookunrar.h"
  2. #include "bookitemlist.h"
  3. #include <QByteArray>
  4. #include <QImageReader>
  5. #include "book.h"
  6. const QString UnrarExe("/opt/meecomicui/bin/unrar");
  7. BookUnrar::BookUnrar(QObject *parent):
  8. QObject(parent),
  9. m_state(Nothing),
  10. m_pageCount(0),
  11. m_files(0)
  12. {
  13. m_process = new QProcess(this);
  14. connect(m_process,SIGNAL(error(QProcess::ProcessError)),this,SLOT(onError(QProcess::ProcessError)));
  15. connect(m_process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(onFinished(int,QProcess::ExitStatus)));
  16. connect(m_process,SIGNAL(readyReadStandardError()),this,SLOT(onReadyReadStandardError()));
  17. connect(m_process,SIGNAL(readyReadStandardOutput()),this,SLOT(onReadyReadStandardOutput()));
  18. m_process->setWorkingDirectory("/opt/meecomicui/bin");
  19. m_queueExtractFile = new BookUnrarQueueThread(BookUnrarQueueThread::PageImage);
  20. m_queueFirstPage = new BookUnrarQueueThread(BookUnrarQueueThread::PageName);
  21. connect(m_queueExtractFile,SIGNAL(queueNext(Book*)),this,SLOT(startExtractFile(Book*)));
  22. connect(m_queueFirstPage,SIGNAL(queueNext(Book*)),this,SLOT(startFirstImage(Book*)));
  23. }
  24. void BookUnrar::setArchiveName(QString text)
  25. {
  26. m_archiveName = text;
  27. }
  28. void BookUnrar::extractAll()
  29. {
  30. if(!m_archiveName.isEmpty()){
  31. m_state = Extract;
  32. m_files = 0;
  33. m_params = QStringList() << "e" << "-o+" << "-p-" << m_archiveName << PRIVATE_BOOKDIR_EXTRACTED+"/";
  34. m_process->start(UnrarExe,m_params);
  35. }
  36. }
  37. void BookUnrar::extractFile(Book* book)
  38. {
  39. if(book != NULL){
  40. m_queueExtractFile->append(book);
  41. if(!m_queueFirstPage->isRunning() && m_process->state() == QProcess::NotRunning)
  42. m_queueExtractFile->next();
  43. }
  44. }
  45. void BookUnrar::startExtractFile(Book* book)
  46. {
  47. if(book != NULL){
  48. m_state = ExtractFile;
  49. m_params = QStringList() << "x" << "-o+" << "-p-" << book->getBookArchiveName() << book->getFirstPageFilename() << PRIVATE_BOOKDIR_EXTRACTED+"/";
  50. m_currentBook = book;
  51. m_process->start(UnrarExe,m_params);
  52. }
  53. }
  54. void BookUnrar::startFirstImage(Book* book)
  55. {
  56. if(!book->getBookArchiveName().isEmpty()){
  57. m_state = FirstPage;
  58. m_params = QStringList() << "vb" << book->getBookArchiveName();
  59. m_currentBook = book;
  60. m_process->start(UnrarExe,m_params);
  61. }
  62. }
  63. void BookUnrar::getFirstImage(Book* book)
  64. {
  65. if(!book->getBookArchiveName().isEmpty()){
  66. m_queueFirstPage->append(book);
  67. if(!m_queueExtractFile->isRunning() && m_process->state() == QProcess::NotRunning)
  68. m_queueFirstPage->next();
  69. }
  70. }
  71. void BookUnrar::getPageCount()
  72. {
  73. if(!m_archiveName.isEmpty()){
  74. m_state = PageCount;
  75. m_pageCount = 0;
  76. m_params = QStringList() << "l" << m_archiveName;
  77. m_process->start(UnrarExe,m_params);
  78. }
  79. }
  80. void BookUnrar::onError(QProcess::ProcessError error )
  81. {
  82. Q_UNUSED(error);
  83. m_process->terminate();
  84. }
  85. void BookUnrar::onFinished( int exitCode, QProcess::ExitStatus exitStatus )
  86. {
  87. qDebug() << "exitCode: " << exitCode << " exitStatus: " << exitStatus;
  88. BookItemList::ErrorCodes code(BookItemList::NoError);
  89. if(exitStatus == QProcess::CrashExit)
  90. code = BookItemList::ZipBroken;
  91. if(exitCode)
  92. code = BookItemList::ZipBroken;
  93. Book* sendBack = NULL;
  94. if(m_currentBook!=NULL)
  95. sendBack = m_currentBook;
  96. m_currentBook = NULL;
  97. QString pageName;
  98. pageName = m_firstPage;
  99. m_firstPage.clear();
  100. switch(m_state){
  101. case PageCount:{
  102. emit this->pageCount(m_pageCount);
  103. }break;
  104. case FirstPage:{
  105. extractFile(sendBack);
  106. if(m_queueFirstPage->haveItems())
  107. m_queueFirstPage->next();
  108. else if(m_queueExtractFile->haveItems())
  109. m_queueExtractFile->next();
  110. }break;
  111. case Extract:{
  112. emit this->extractFinished(code);
  113. }break;
  114. case ExtractFile:{
  115. if(m_queueExtractFile->haveItems())
  116. m_queueExtractFile->next();
  117. emit this->firstImageExtracted(code,sendBack);
  118. }break;
  119. default:break;
  120. }
  121. }
  122. void BookUnrar::onReadyReadStandardError()
  123. {
  124. if(m_process->state() == QProcess::Running)
  125. m_process->terminate();
  126. }
  127. void BookUnrar::onReadyReadStandardOutput()
  128. {
  129. while(m_process->canReadLine()){
  130. QString stdLine;
  131. QByteArray tmpline = m_process->readLine();
  132. stdLine = QString::fromUtf8(tmpline);
  133. stdLine = stdLine.trimmed();
  134. switch(m_state){
  135. case Extract:{
  136. if(stdLine.startsWith("Extracting") && stdLine.endsWith("OK")){
  137. m_files++;
  138. emit this->openedZipPage(m_files,m_pageCount,QString());
  139. }
  140. }break;
  141. case FirstPage:{
  142. if(m_firstPage.isEmpty()){
  143. QList<QByteArray> sported = QImageReader::supportedImageFormats();
  144. foreach(QByteArray e,sported){
  145. QString ext("."+e);
  146. QString fname;
  147. int ind = stdLine.indexOf(ext,0,Qt::CaseInsensitive);
  148. if(ind>-1){
  149. fname = stdLine.mid(0,ind);
  150. fname.append(stdLine.mid(ind,ext.length()));
  151. m_firstPage = fname;
  152. m_currentBook->setFirstPageFilename(m_firstPage);
  153. }
  154. }
  155. }
  156. }break;
  157. case ExtractFile:{
  158. if(stdLine.startsWith("Extracting") && stdLine.endsWith("OK")){
  159. m_firstPage = PRIVATE_BOOKDIR_EXTRACTED+"/"+m_currentBook->getFirstPageFilename();
  160. QFile file(m_firstPage);
  161. if(file.open(QIODevice::ReadOnly) && m_currentBook != NULL){
  162. QImage img;
  163. QString imgname = m_firstPage;
  164. imgname = imgname.replace(QRegExp("\\s"),QString("_"));
  165. imgname = imgname.replace(QRegExp("\\W"),QString("_"));
  166. imgname = PRIVATE_BOOKDIR_THUMBS+"/"+imgname+".png";
  167. img.loadFromData(file.readAll());
  168. img = img.scaled(SCALED_ICON_SIZE);
  169. img.save(imgname);
  170. m_currentBook->setListImage(QUrl(imgname));
  171. file.close();
  172. }
  173. }
  174. }break;
  175. default:break;
  176. }
  177. }
  178. }