PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/orig_compat_experimental/importbook.cpp

https://gitlab.com/orayta_yoch/orayta
C++ | 222 lines | 155 code | 40 blank | 27 comment | 15 complexity | d373097336f47d6170af816b35ed6d19 MD5 | raw file
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program; if not, write to the Free Software
  12. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. *
  14. * Author: Moshe Wagner. <moshe.wagner@gmail.com>
  15. */
  16. #include "importbook.h"
  17. #include "functions.h"
  18. #include "ui_importbook.h"
  19. #include "mainwindow.h"
  20. #include <QFileDialog>
  21. #include <QDirIterator>
  22. #include <quazip/quazip.h>
  23. #include <quazip/quazipfile.h>
  24. importBook::importBook(QWidget *parent) : QDialog(parent), ui(new Ui::importBook)
  25. {
  26. ui->setupUi(this);
  27. ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
  28. ui->cancelBTN->hide();
  29. ui->importBTN->hide();
  30. ui->deleteButton->hide();
  31. ui->progressBar->hide();
  32. ui->fileCopied->hide();
  33. }
  34. importBook::~importBook()
  35. {
  36. delete ui;
  37. }
  38. void importBook::on_addFolder_clicked()
  39. {
  40. QString dirName = QFileDialog::getExistingDirectory(this, "", QDir::homePath(), QFileDialog::ShowDirsOnly);
  41. if (!dirName.isEmpty())
  42. {
  43. ui->listWidget->addItem(dirName);
  44. ui->cancelBTN->show();
  45. ui->importBTN->show();
  46. ui->deleteButton->show();
  47. }
  48. }
  49. void importBook::on_addBooks_clicked()
  50. {
  51. QString filters = tr("All supported files "
  52. "(*.html *.htm *.txt *.pdf);;"
  53. "Html files(*.htm *.html);;"
  54. "Text files(*.txt);;"
  55. "Pdf files(*.pdf)");
  56. QStringList fileNames = QFileDialog::getOpenFileNames(this, "", QDir::homePath(), filters);
  57. if (!fileNames.empty())
  58. {
  59. for (int i=0; i<fileNames.size(); i++)
  60. {
  61. ui->listWidget->addItem(fileNames[i]);
  62. }
  63. ui->cancelBTN->show();
  64. ui->importBTN->show();
  65. ui->deleteButton->show();
  66. }
  67. }
  68. void importBook::on_deleteButton_clicked()
  69. {
  70. QList<QListWidgetItem *> selected_items = ui->listWidget->selectedItems();
  71. qDeleteAll(selected_items.begin(), selected_items.end());
  72. if ( ui->listWidget->count() == 0 )
  73. ui->deleteButton->hide();
  74. }
  75. void importBook::on_label_linkActivated(QString link)
  76. {
  77. //Open the project's site in a browser
  78. QDesktopServices::openUrl(QUrl(link));
  79. }
  80. void importBook::on_cancelBTN_clicked()
  81. {
  82. close();
  83. }
  84. static QList <QPair <QString,QString> > listAllFiles(const QString& srcPath, const QString& destPath, const QStringList& nameFilters)
  85. {
  86. QList <QPair <QString,QString> > ret;
  87. QDirIterator it(srcPath, nameFilters, QDir::Files | QDir::NoDotAndDotDot | QDir::AllDirs, QDirIterator::Subdirectories);
  88. while (it.hasNext())
  89. {
  90. QString filename = it.next();
  91. ret << QPair<QString,QString>(filename, QString(filename).replace(srcPath, destPath));
  92. }
  93. return ret;
  94. }
  95. static bool copyFile(const QString& src, const QString& dest)
  96. {
  97. if (src.endsWith(".txt"))
  98. {
  99. QuaZip zip(QString(dest).replace(".txt", ".obk")); // not totally safe
  100. if (zip.open(QuaZip::mdCreate))
  101. {
  102. QuaZipFile zfile(&zip);
  103. zfile.open(QIODevice::WriteOnly, QuaZipNewInfo("BookText"));
  104. // allowed encoding is only utf-8...
  105. QTextStream out(&zfile);
  106. out.setCodec( QTextCodec::codecForName("UTF-8") );
  107. out << readfile(src, "UTF-8");
  108. return true;
  109. }
  110. return false;
  111. }
  112. else
  113. {
  114. return QFile::copy(src, dest);
  115. }
  116. }
  117. void importBook::importRoutine()
  118. {
  119. QString booksUserPath = USERPATH + "Books/";
  120. //Make sure the dir exists
  121. QDir d(booksUserPath) ;
  122. if ( !d.exists() )
  123. d.mkpath(booksUserPath);
  124. QStringList filters;
  125. filters << "*.html" << "*.htm" << "*.pdf" << "*.txt" << "*.conf";
  126. QList <QPair <QString,QString> > fileslist;
  127. //Copy to user's book folder
  128. for (int i=0; i < ui->listWidget->count(); i++)
  129. {
  130. QFileInfo f( ui->listWidget->item(i)->text() );
  131. if ( f.exists() )
  132. {
  133. fileslist << QPair<QString, QString>(f.absoluteFilePath(), booksUserPath + f.fileName());
  134. //Copy whole directory
  135. if( f.isDir() )
  136. {
  137. fileslist << listAllFiles(f.absoluteFilePath(), booksUserPath + f.fileName(), filters);
  138. }
  139. }
  140. }
  141. int nFiles = fileslist.size();
  142. int cmt = 0;
  143. if (nFiles > 0)
  144. {
  145. QPair <QString, QString> it;
  146. foreach(it, fileslist)
  147. {
  148. if (QFileInfo(it.first).isDir())
  149. {
  150. //qDebug() << "make dir " << it.second;
  151. d.mkdir(it.second);
  152. }
  153. else
  154. {
  155. QString label = it.first.mid(it.first.lastIndexOf("/"));
  156. emit fileCopied(label);
  157. if ( !copyFile(it.first, it.second) )
  158. qDebug() << "Can't copy file" << it.first << " to " << it.second;
  159. emit importProgress( int(++cmt * 100 / nFiles) );
  160. }
  161. QApplication::processEvents();
  162. }
  163. }
  164. }
  165. void importBook::on_importBTN_clicked()
  166. {
  167. // A la place, on pourrait mettre une barre de progression, et fermer à la fin
  168. //Set a busy cursor before actually resizing the text, and restores it by the end
  169. QApplication::setOverrideCursor(Qt::WaitCursor);
  170. ui->progressBar->show();
  171. ui->fileCopied->show();
  172. QObject::connect(this, SIGNAL(importProgress(int)), ui->progressBar, SLOT(setValue(int)));
  173. QObject::connect(this, SIGNAL(fileCopied(QString)), ui->fileCopied, SLOT(setText(QString)));
  174. importRoutine();
  175. /*
  176. QFuture<void> future = QtConcurrent::run( this, &importBook::importRoutine);
  177. future.waitForFinished();
  178. */
  179. close();
  180. emit updateTreeSignal();
  181. QApplication::restoreOverrideCursor();
  182. }