void QtZLWorker::savePositions(const QString &path, const QList<CuteReader::BookTextPosition> &positions) { auto work = [path, positions] () { ZLFile file(path.toStdString()); auto book = Book::loadFromFile(file); std::deque<ReadingState> states; for (const CuteReader::BookTextPosition &position : positions) { if (position.body != 0) continue; states.push_back(ReadingState(position.paragraph, position.word, position.character)); } BooksDB::Instance().saveBookStateStack(*book, states); }; selfMethod.invoke(this, Qt::QueuedConnection, Q_ARG(QtZLWork, work)); }
bool BooksDB::loadBookStateStack(const Book &book, std::deque<ReadingState> &stack) { if (book.bookId() == 0) { return false; } ((DBIntValue&)*myLoadBookStateStack->parameter("@book_id").value()) = book.bookId(); shared_ptr<DBDataReader> reader = myLoadBookStateStack->executeReader(); if (reader.isNull()) { return false; } while (reader->next()) { if (reader->type(0) != DBValue::DBINT /* paragraph */ || reader->type(1) != DBValue::DBINT /* word */ || reader->type(2) != DBValue::DBINT /* char */) { return false; } const int paragraph = reader->intValue(0); const int word = reader->intValue(1); const int character = reader->intValue(2); stack.push_back(ReadingState(paragraph, word, character)); } return true; }
QtZLGuard QtZLWorker::openBook(QObject *object, const QString &path, const std::function<void (const QtZLBookInfo &)> &handler, const std::function<void (const QString &)> &error) { QtZLGuard guard = QtZLGuard::create(); run(object, guard, [this, path, handler, error] () -> QtZLWork { ZLFile file(path.toStdString()); shared_ptr<FormatPlugin> plugin = PluginCollection::Instance().plugin(file, false); if (!plugin.isNull()) { const QString errorStr = QString::fromStdString(plugin->tryOpen(file)); if (!errorStr.isEmpty()) { return [errorStr, error] () { error(errorStr); }; } } else { return [error] () { error(tr("Unknown format")); }; } shared_ptr<Book> book = BooksDBUtil::getBook(file.path()); if (book.isNull()) { shared_ptr<FormatPlugin> plugin = PluginCollection::Instance().plugin(file, false); if (!plugin.isNull()) { const QString errorStr = QString::fromStdString(plugin->tryOpen(file)); if (errorStr.isEmpty()) { return [errorStr, error] () { error(errorStr); }; } } return [error] () { error(tr("Unknown format")); }; } m_data->model = new BookModel(book); m_data->bodies.clear(); m_data->bodies << m_data->model->bookTextModel(); shared_ptr<ZLTextModel> contents = m_data->model->contentsModel(); ContentsModel &contentsModel = static_cast<ContentsModel &>(*contents); for (size_t i = 0; i < contentsModel.paragraphsNumber(); ++i) { } // BooksDB::Instance().insertIntoBookList(*book); ZLTextHyphenator::Instance().load(book->language()); QtZLBookInfo result; result.book = toBookInfo(book); std::deque<ReadingState> states; if (!BooksDB::Instance().loadBookStateStack(*book, states)) states.push_back(ReadingState(0, 0, 0)); for (const ReadingState &state : states) { CuteReader::BookTextPosition position = { 0, state.Paragraph, state.Word, state.Character }; result.positions << position; } // if (book.isNull()) { // info = toBookInfo(book); // shared_ptr<BookModel> model = new BookModel(book); // } return [result, handler] () { handler(result); }; }); return guard; }