File indexing completed on 2024-12-15 04:14:22

0001 /*
0002  * Copyright 2018 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * This program is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU General Public License as
0006  * published by the Free Software Foundation; either version 2 of
0007  * the License or (at your option) version 3 or any later version
0008  * accepted by the membership of KDE e.V. (or its successor approved
0009  * by the membership of KDE e.V.), which shall act as a proxy
0010  * defined in Section 14 of version 3 of the license.
0011  *
0012  * This program 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
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public License
0018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include "ContentQuery.h"
0022 
0023 #include <QMimeDatabase>
0024 #include <QtGui/QImageReader>
0025 
0026 class ContentQuery::Private
0027 {
0028 public:
0029     QStringList mimeTypesForType(Type type);
0030 
0031     Type type = Any;
0032     QString searchString;
0033     QStringList locations;
0034     QStringList mimeTypes;
0035 };
0036 
0037 ContentQuery::ContentQuery(QObject* parent)
0038     : QObject(parent), d(new Private)
0039 {
0040 }
0041 
0042 ContentQuery::~ContentQuery()
0043 {
0044 }
0045 
0046 ContentQuery::Type ContentQuery::type() const
0047 {
0048     return d->type;
0049 }
0050 
0051 QString ContentQuery::searchString() const
0052 {
0053     return d->searchString;
0054 }
0055 
0056 QStringList ContentQuery::locations() const
0057 {
0058     return d->locations;
0059 }
0060 
0061 QStringList ContentQuery::mimeTypes() const
0062 {
0063     if(!d->mimeTypes.isEmpty())
0064         return d->mimeTypes;
0065 
0066     return d->mimeTypesForType(d->type);
0067 }
0068 
0069 void ContentQuery::setType(ContentQuery::Type type)
0070 {
0071     if(type == d->type)
0072         return;
0073 
0074     d->type = type;
0075     emit typeChanged();
0076 }
0077 
0078 void ContentQuery::setSearchString(const QString& searchString)
0079 {
0080     if(searchString == d->searchString)
0081         return;
0082 
0083     d->searchString = searchString;
0084     emit searchStringChanged();
0085 }
0086 
0087 void ContentQuery::setLocations(const QStringList& locations)
0088 {
0089     if(locations == d->locations)
0090         return;
0091 
0092     d->locations = locations;
0093     emit locationsChanged();
0094 }
0095 
0096 void ContentQuery::setMimeTypes(const QStringList& mimeTypes)
0097 {
0098     if(mimeTypes == d->mimeTypes)
0099         return;
0100 
0101     d->mimeTypes = mimeTypes;
0102     emit mimeTypesChanged();
0103 }
0104 
0105 namespace {
0106     QStringList contentQueryVideo () {
0107         return {
0108             QStringLiteral("video/x-matroska"),
0109             QStringLiteral("video/mp4"),
0110             QStringLiteral("video/mpeg"),
0111             QStringLiteral("video/ogg"),
0112             QStringLiteral("video/quicktime"),
0113             QStringLiteral("video/webm"),
0114             QStringLiteral("video/x-ms-wmv"),
0115             QStringLiteral("video/x-msvideo"),
0116             QStringLiteral("video/x-ogm+ogg"),
0117             QStringLiteral("video/x-theora+ogg"),
0118         };
0119     }
0120 
0121     QStringList contentQueryAudio() {
0122         return {
0123             QStringLiteral("audio/aac"),
0124             QStringLiteral("audio/flac"),
0125             QStringLiteral("audio/mp2"),
0126             QStringLiteral("audio/mp4"),
0127             QStringLiteral("audio/mpeg"),
0128             QStringLiteral("audio/ogg"),
0129             QStringLiteral("audio/webm"),
0130             QStringLiteral("audio/x-opus+ogg"),
0131             QStringLiteral("audio/x-ms-wma"),
0132             QStringLiteral("audio/x-vorbis+ogg"),
0133             QStringLiteral("audio/x-wav")
0134         };
0135     }
0136 
0137     QStringList contentQueryDocuments () {
0138         return {
0139             QStringLiteral("application/vnd.oasis.opendocument.text"),
0140             QStringLiteral("application/vnd.oasis.opendocument.spreadsheet"),
0141             QStringLiteral("application/vnd.oasis.opendocument.presentation"),
0142             QStringLiteral("application/vnd.ms-word"),
0143             QStringLiteral("application/vnd.ms-excel"),
0144             QStringLiteral("application/vnd.ms-powerpoint"),
0145             QStringLiteral("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.xml"),
0146             QStringLiteral("application/vnd.openxmlformats-officedocument.wordprocessingml.document.xml"),
0147             QStringLiteral("application/vnd.openxmlformats-officedocument.presentationml.presentation.xml"),
0148             QStringLiteral("text/plain"),
0149             QStringLiteral("application/pdf")
0150         };
0151     }
0152 
0153     QStringList contentQueryImages() {
0154         // only popylate once.
0155         static QStringList result;
0156         if (result.isEmpty()) {
0157             for(const auto& item : QImageReader::supportedMimeTypes())
0158             {
0159                 result << QString::fromUtf8(item);
0160             }
0161         }
0162         return result;
0163     }
0164 
0165     QStringList contentQueryComics() {
0166         return {
0167             QStringLiteral("application/x-cbz"),
0168             QStringLiteral("application/x-cbr"),
0169             QStringLiteral("application/x-cb7"),
0170             QStringLiteral("application/x-cbt"),
0171             QStringLiteral("application/x-cba"),
0172             QStringLiteral("application/vnd.comicbook+zip"),
0173             QStringLiteral("application/vnd.comicbook+rar"),
0174             QStringLiteral("application/vnd.ms-htmlhelp"),
0175             QStringLiteral("image/vnd.djvu"),
0176             QStringLiteral("image/x-djvu"),
0177             QStringLiteral("application/epub+zip"),
0178             QStringLiteral("application/pdf")
0179         };
0180     }
0181 }
0182 QStringList ContentQuery::Private::mimeTypesForType(ContentQuery::Type type)
0183 {
0184     QStringList ret{};
0185     switch(type) {
0186         case ContentQuery::Type::Video:     ret = contentQueryVideo(); break;
0187         case ContentQuery::Type::Audio:     ret = contentQueryAudio(); break;
0188         case ContentQuery::Type::Documents: ret = contentQueryDocuments(); break;
0189         case ContentQuery::Type::Images:    ret = contentQueryImages(); break;
0190         case ContentQuery::Type::Comics:    ret = contentQueryComics(); break;
0191         case ContentQuery::Type::Any:       /* do nothing */ break;
0192     }
0193     return ret;
0194 }