Esempio n. 1
0
void HelpBrowser::closeEvent(QCloseEvent * event)
{
	delete menuModel;

	// no need to delete child widgets, Qt does it all for us
	// bookmarks
	QFile bookFile(bookmarkFile());
	if (bookFile.open(QIODevice::WriteOnly))
	{
		QTextStream stream(&bookFile);
		stream.setCodec("UTF-8");
		stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		stream << "<bookmarks>\n";
		QTreeWidgetItemIterator it(helpNav->bookmarksView);
		while (*it) 
		{
			if (bookmarkIndex.contains((*it)->text(0)))
			{
				QString pagetitle(bookmarkIndex.value((*it)->text(0)).first);
				QString filename(bookmarkIndex.value((*it)->text(0)).second);
				stream << "\t<item title=\"" << (*it)->text(0) << "\" pagetitle=\"" << pagetitle << "\" url=\"" << filename << "\" />\n";
			}
			++it;
		}
		stream << "</bookmarks>\n";
		bookFile.close();
	}
	// history
  	QFile histFile(historyFile());
	if (histFile.open(QIODevice::WriteOnly))
	{
		QTextStream stream(&histFile);
		stream.setCodec("UTF-8");
		stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		stream << "<history>\n";
		for (QMap<QAction*,histd2>::Iterator it = mHistory.begin() ; it != mHistory.end(); ++it)
			stream << "\t<item title=\"" << it.value().title << "\" url=\"" << it.value().url << "\" />\n";
		stream << "</history>\n";
		histFile.close();
	}
	// size
	prefs->set("xsize", width());
	prefs->set("ysize", height());

	emit closed();
}
bool BooksDBUtil::canRemoveFile(const std::string &filePath) {
	ZLFile bookFile(filePath);
	std::string physicalPath = bookFile.physicalFilePath();
	if (filePath != physicalPath) {
		ZLFile zipFile(physicalPath);
		shared_ptr<ZLDir> zipDir = zipFile.directory();
		if (zipDir.isNull()) {
			return false;
		}
		std::vector<std::string> entries;
		zipDir->collectFiles(entries, false); // TODO: replace with BooksDB call???
		if (entries.size() != 1) {
			return false;
		}
		if (zipDir->itemPath(entries[0]) != filePath) {
			return false;
		}
	}
	return ZLFile(physicalPath).canRemove();
}
BookInfo* ModelBackendOneFile::loadDefaultInfo( const QString& path )
{
    qDebug() << Q_FUNC_INFO << "Path: " << path;

    BookInfo* bookInfo = new BookInfo();
    QString collection;

    if (MetaDataExtractor::getMetaData(path, bookInfo->title, bookInfo->author, bookInfo->publisher, bookInfo->publishTime, bookInfo->synopsis, bookInfo->format, bookInfo->isDRMFile , collection, bookInfo->language)==true) {
        if(bookInfo->title.isEmpty())
            bookInfo->title = QFileInfo(path).baseName();

        //New empty string for author field and use to translate the unknow author string.
        if(bookInfo->author.isEmpty())
            bookInfo->author = QString("---");

        if (!collection.isEmpty())
        {
            addCollection(collection);
            double index = MetaDataExtractor::getCollectionIndex(path);
            bookInfo->addCollection(collection, index);
        }
        bookInfo->setCSSFileList(MetaDataExtractor::extractCSS(path));
    } else {
        qWarning() << Q_FUNC_INFO << "Corrupted book: " << path;
        bookInfo->title = QFileInfo(path).baseName();
        bookInfo->corrupted = true;
        // FIXME: Get proper icon for this
        bookInfo->thumbnail = QString(":/res/corrupted_book_ico.png");
        // TODO: No están todos los atributos.
    }

    bookInfo->readingStatus = BookInfo::NO_READ_BOOK;
    bookInfo->path = path;
    QFileInfo bookFile(path);
    bookFile.refresh();
    bookInfo->size = bookFile.size();
    if (!path.toLower().endsWith(".pdf"))
         bookInfo->fontSize = 2;

    return bookInfo;
}
BookInfo* ModelBackendOneFile::loadBook (QXmlStreamReader& xml)
{
    BookInfo * info = new BookInfo();

    while (!xml.atEnd() && !xml.hasError())
    {
        xml.readNext();
        if(xml.isEndElement())
        {
            if(xml.name() == "book")
                break;
        } else {
            QStringRef name = xml.name();
            if (name == "isbn") {
                info->isbn = xml.readElementText();
            } else if (name == "path") {
                info->path = xml.readElementText();
            } else if (name == "format") {
                info->format = xml.readElementText();
            } else if (name == "publisher") {
                info->publisher = xml.readElementText();
            } else if (name == "synopsis") {
                info->synopsis = xml.readElementText();
            } else if (name == "syncDate") {
                info->syncDate = QDateTime::fromString(xml.readElementText(), Qt::ISODate);
            } else if (name == "title") {
                info->title = xml.readElementText();
            } else if (name == "author") {
                info->author = xml.readElementText();
            }else if (name == "thumbnail") {
                info->thumbnail = xml.readElementText();
            }else if (name == "publish-time") {
                info->publishTime = QDateTime::fromString(xml.readElementText(), Qt::ISODate);
            } else if (name == "download-time") {
                info->downloadTime = QDateTime::fromString(xml.readElementText(), Qt::ISODate);
            } else if (name == "lastTimeRead") {
                QString lastTime = xml.readElementText();
                if(!lastTime.isEmpty())
                    info->lastTimeRead = QDateTime::fromString(lastTime, Qt::ISODate);
            } else if (name == "readingPercentage") {
                info->readingPercentage = xml.readElementText().toDouble();
            } else if (name == "readingProgress") {
                info->readingProgress = xml.readElementText().toDouble();
            } else if (name == "readingPeriod") {
                info->setReadingPeriod(xml.readElementText().toLong());
            } else if (name == "mark-count") {
                info->markCount = xml.readElementText().toInt();
            } else if (name == "note-count") {
                info->noteCount = xml.readElementText().toInt();
            } else if (name == "hili-count") {
                info->hiliCount = xml.readElementText().toInt();
            } else if (name == "page-count") {
                info->pageCount = xml.readElementText().toInt();
            } else if (name == "last-read-link") {
                info->lastReadLink = xml.readElementText();
            } else if (name == "last-read-page") {
                info->lastReadPage = xml.readElementText().toInt();
            } else if (name == "font-size") {
                info->fontSize = xml.readElementText().toDouble();
            } else if (name == "page-mode") {
                info->pageMode = xml.readElementText().toInt();
            } else if (name == "page-orientation") {
                info->orientation = (BookInfo::orientationEnum)xml.readElementText().toInt();
            } else if (name == "corrupted") {
                info->corrupted = xml.readElementText().toInt();
            } else if (name == "type") {
                info->m_type = (BookInfo::bookTypeEnum)xml.readElementText().toInt();
            } else if (name == "archived") {
                info->m_archived = xml.readElementText().toInt();
            } else if (name == "isDRMFile") {
                info->isDRMFile = xml.readElementText().toInt();
            } else if (name == "storePrice") {
                info->storePrice = xml.readElementText();
            } else if (name == "expirationDate") {
                info->m_expirationDate = QDateTime::fromString(xml.readElementText(), Qt::ISODate);
            } else if (name == "totalReadingTime") {
                info->totalReadingTime = xml.readElementText().toInt();
            } else if (name == "timestamp") {
                info->timestamp = xml.readElementText().toLongLong();
            } else if (name == "cssFiles") {
                info->setCSSFileList(xml.readElementText().split(";"));
            } else if (name == "percentageList") {
                info->percentageList = xml.readElementText();
            } else if (name == "readingStatus") {
                info->readingStatus = (BookInfo::readStateEnum)xml.readElementText().toInt();
            } else if (name == "size") {
                info->size = xml.readElementText().toInt();
            }else if (name == "language"){
                info->language = xml.readElementText();
            }else if (name == "marks") {
                while (!xml.atEnd() && !xml.hasError())
                {
                    xml.readNext();
                    if (xml.isEndElement()) {
                        if (xml.name() == "marks") break;
                    } else if (xml.isStartElement()) {
                        if (xml.name() == "mark") {
                            BookLocation* loc = loadMark(xml);
                            info->addLocation(loc->bookmark, loc);
                        }

                    }
                }
            }
            else if (name == "collections") {
                QString collection;
                while (!xml.atEnd() && !xml.hasError())
                {
                    xml.readNext();
                    double index = 0;
                    if (xml.isEndElement()) {
                        if (xml.name() == "collections") break;
                    } else
                    {
                        QStringRef name = xml.name();
                        if (name == "collection") {
                            collection = xml.readElementText();
                            info->addCollection(collection, index);
                            addCollection(collection);
                        }

                        else if(name == "index")
                        {
                            if(!collection.isEmpty())
                            {
                                index = xml.readElementText().toDouble();
                                info->addCollection(collection, index);
                                addCollection(collection);
                            }
                        }
                    }
                }
            }
        }
    }
    if(info->readingStatus == BookInfo::NONE)
    {
        if (info->lastTimeRead.isValid()) // reading ongoing
        {
            info->readingStatus = BookInfo::READING_BOOK;
        }
        else
        {
            if(info->lastReadLink.isEmpty()) // New ebooks ever opened
                info->readingStatus = BookInfo::NO_READ_BOOK;
            else // Closed ebooks
                info->readingStatus = BookInfo::READ_BOOK;
        }
    }

    if(info->size == 0)
    {
        QFileInfo bookFile(info->path);
        bookFile.refresh();
        info->size = bookFile.size();
    }

    //For added mobi books try to extract the metadata if the author has the previous empty string.
    if(info->format == "mobi" && info->author =="--")
    {
        info->author = "";
        info->title = "";
        info->thumbnail = "";
        QString collection = "";
        if(MetaDataExtractor::getMetaData(info->path, info->title, info->author, info->publisher, info->publishTime, info->synopsis, info->format, info->isDRMFile, collection, info->language) == true)
        {
            if(info->title.isEmpty())
                info->title = QFileInfo(info->path).baseName();

            if(info->author.isEmpty())
                info->author = QString("---");
            if(!collection.isEmpty())
            {
                double collectionIndex = MetaDataExtractor::getCollectionIndex(info->path);
                info->addCollection(collection, collectionIndex);
            }
            info->setCSSFileList(MetaDataExtractor::extractCSS(info->path));
        }
    }

    //Change the empty string for author to prevent extract the metadata again.
    else if(info->author == "--")
        info->author = QString("---");

    return info;
}