File indexing completed on 2025-03-09 04:23:06

0001 /*
0002  * Copyright (C) 2015 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  *
0020  */
0021 
0022 #include "FolderBookModel.h"
0023 #include <QMimeDatabase>
0024 #include <QDir>
0025 #ifdef KFILEMETADATA_FOUND
0026 #include <KFileMetaData/UserMetaData>
0027 #endif
0028 FolderBookModel::FolderBookModel(QObject* parent)
0029     : BookModel(parent)
0030 {
0031 }
0032 
0033 FolderBookModel::~FolderBookModel()
0034 {
0035 }
0036 
0037 void FolderBookModel::setFilename(QString newFilename)
0038 {
0039     setProcessing(true);
0040     clearPages();
0041 
0042     QMimeDatabase mimeDb;
0043     QString mimeType = mimeDb.mimeTypeForFile(newFilename).name();
0044     QString currentPageFile;
0045     if(mimeType == "image/jpeg" || mimeType == "image/png") {
0046         QFileInfo file(newFilename);
0047         newFilename = file.absolutePath();
0048         currentPageFile = file.fileName();
0049     }
0050 
0051     QDir dir(newFilename);
0052     if(dir.exists())
0053     {
0054         QFileInfoList entries = dir.entryInfoList(QDir::Files, QDir::Name);
0055         QLatin1String undesired("thumbs.db");
0056         int i = 0;
0057         for(const QFileInfo& entry : qAsConst(entries))
0058         {
0059             if(entry.fileName().toLower() == undesired) {
0060                 continue;
0061             }
0062             addPage(QString("file://").append(entry.canonicalFilePath()), entry.fileName());
0063             if(currentPageFile == entry.fileName()) {
0064                 BookModel::setCurrentPage(i, false);
0065             }
0066             ++i;
0067         }
0068     }
0069 
0070     BookModel::setFilename(newFilename);
0071 
0072     // We might have opened this in the past, so reset the current page to the actual one
0073     // rather than the filename we tried to open. Recent File is the opened page, rather
0074     // than the directory (because it makes for much simpler code), so reset rather than
0075     // doing other magic.
0076 #ifdef KFILEMETADATA_FOUND
0077     KFileMetaData::UserMetaData data(filename());
0078     if(data.hasAttribute("peruse.currentPage"))
0079         BookModel::setCurrentPage(data.attribute("peruse.currentPage").toInt(), false);
0080 #endif
0081 
0082     Q_EMIT loadingCompleted(true);
0083     setProcessing(false);
0084 }
0085