diff --git a/messageviewer/src/utils/messageviewerutil.h b/messageviewer/src/utils/messageviewerutil.h --- a/messageviewer/src/utils/messageviewerutil.h +++ b/messageviewer/src/utils/messageviewerutil.h @@ -39,15 +39,15 @@ #define MAILVIEWERUTIL_H #include "messageviewer_export.h" +#include "mimetype.h" #include "kmime/kmime_content.h" #include #include #include class QUrl; class QWidget; class QActionGroup; class QAction; -class QMimeType; namespace KMime { @@ -91,35 +91,11 @@ bool saveContents(QWidget *parent, const KMime::Content::List &contents, QUrl ¤tFolder); bool saveContent(QWidget *parent, KMime::Content *content, const QUrl &url); -/** - * Finds the filename of an icon based on the given mimetype or filenames. - * - * Always use this functions when looking up icon names for mime types, don't use - * KMimeType directly. - * - * Uses the IconNameCache internally to speed things up. - * - * @param mimeType The primary mime type used to find the icon, e.g. "application/zip". Alias - * mimetypes are resolved. - * @param size Size of the requested icon, e.g. KIconLoader::Desktop - * @param fallbackFileName(1|2) When the icon is not found by the given mime type, use the file - * name extensions of these file names to look the icon up. - * Example: "test.zip" - * @return the full file name of the icon file - */ -QString MESSAGEVIEWER_EXPORT fileNameForMimetype(const QString &mimeType, int iconSize, - const QString &fallbackFileName1 = QString(), - const QString &fallbackFileName2 = QString()); bool MESSAGEVIEWER_EXPORT saveMessageInMbox(const Akonadi::Item::List &retrievedMsgs, QWidget *parent, bool appendMessages = false); bool MESSAGEVIEWER_EXPORT saveAttachments(const KMime::Content::List &contents, QWidget *parent, QUrl ¤tFolder); MESSAGEVIEWER_EXPORT QAction *createAppAction(const KService::Ptr &service, bool singleOffer, QActionGroup *actionGroup, QObject *parent); - -/** - * Search mimetype from filename when mimetype is empty or application/octet-stream - **/ -QMimeType mimetype(const QString &name); } } diff --git a/messageviewer/src/utils/messageviewerutil.cpp b/messageviewer/src/utils/messageviewerutil.cpp --- a/messageviewer/src/utils/messageviewerutil.cpp +++ b/messageviewer/src/utils/messageviewerutil.cpp @@ -38,7 +38,6 @@ #include "messageviewer/messageviewerutil.h" #include "kpimtextedit/texttospeech.h" -#include "utils/iconnamecache.h" #include "messageviewer/nodehelper.h" #include "messageviewer_debug.h" #include "MessageCore/MessageCoreSettings" @@ -72,7 +71,6 @@ #include #include #include -#include using namespace MessageViewer; @@ -99,52 +97,6 @@ return true; } -QString Util::fileNameForMimetype(const QString &mimeType, int iconSize, - const QString &fallbackFileName1, - const QString &fallbackFileName2) -{ - QString fileName; - QString tMimeType = mimeType; - - // convert non-registered types to registered types - if (mimeType == QLatin1String("application/x-vnd.kolab.contact")) { - tMimeType = QStringLiteral("text/x-vcard"); - } else if (mimeType == QLatin1String("application/x-vnd.kolab.event")) { - tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.event"); - } else if (mimeType == QLatin1String("application/x-vnd.kolab.task")) { - tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo"); - } else if (mimeType == QLatin1String("application/x-vnd.kolab.journal")) { - tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.journal"); - } else if (mimeType == QLatin1String("application/x-vnd.kolab.note")) { - tMimeType = QStringLiteral("application/x-vnd.akonadi.note"); - } - QMimeDatabase mimeDb; - auto mime = mimeDb.mimeTypeForName(tMimeType); - if (mime.isValid()) { - fileName = mime.iconName(); - } else { - fileName = QStringLiteral("unknown"); - if (!tMimeType.isEmpty()) { - qCWarning(MESSAGEVIEWER_LOG) << "unknown mimetype" << tMimeType; - } - } - //WorkAround for #199083 - if (fileName == QLatin1String("text-vcard")) { - fileName = QStringLiteral("text-x-vcard"); - } - if (fileName.isEmpty()) { - fileName = fallbackFileName1; - if (fileName.isEmpty()) { - fileName = fallbackFileName2; - } - if (!fileName.isEmpty()) { - fileName = mimeDb.mimeTypeForFile(QLatin1String("/tmp/") + fileName).iconName(); - } - } - - return IconNameCache::instance()->iconPath(fileName, iconSize); -} - #if defined Q_OS_WIN || defined Q_OS_MACX #include #endif @@ -527,20 +479,4 @@ actionGroup->addAction(act); act->setData(QVariant::fromValue(service)); return act; -} - -QMimeType Util::mimetype(const QString &name) -{ - QMimeDatabase db; - // consider the filename if mimetype cannot be found by content-type - auto mimeTypes = db.mimeTypesForFileName(name); - foreach (const auto &mt, mimeTypes) { - if (mt.name() != QLatin1String("application/octet-stream")) { - return mt; - } - } - - // consider the attachment's contents if neither the Content-Type header - // nor the filename give us a clue - return db.mimeTypeForFile(name); -} +} \ No newline at end of file diff --git a/messageviewer/src/utils/mimetype.h b/messageviewer/src/utils/mimetype.h new file mode 100644 --- /dev/null +++ b/messageviewer/src/utils/mimetype.h @@ -0,0 +1,64 @@ +/* + Copyright (C) 2005 Till Adam + Copyright (c) 2016 Sandro Knauß + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef MESSAGEVIEWER_UTIL_MIMETYPE_H +#define MESSAGEVIEWER_UTIL_MIMETYPE_H + +#include "messageviewer_export.h" + +#include +class QMimeType; + +namespace MessageViewer +{ + +/** + * The Util namespace contains a collection of helper functions use in + * various places. + */ +namespace Util +{ + +/** + * Search mimetype from filename when mimetype is empty or application/octet-stream + **/ +QMimeType mimetype(const QString &name); + +/** + * Finds the filename of an icon based on the given mimetype or filenames. + * + * Always use this functions when looking up icon names for mime types, don't use + * KMimeType directly. + * + * Uses the IconNameCache internally to speed things up. + * + * @param mimeType The primary mime type used to find the icon, e.g. "application/zip". Alias + * mimetypes are resolved. + * @param size Size of the requested icon, e.g. KIconLoader::Desktop + * @param fallbackFileName(1|2) When the icon is not found by the given mime type, use the file + * name extensions of these file names to look the icon up. + * Example: "test.zip" + * @return the full file name of the icon file + */ +QString MESSAGEVIEWER_EXPORT fileNameForMimetype(const QString &mimeType, int iconSize, + const QString &fallbackFileName1 = QString(), + const QString &fallbackFileName2 = QString()); +} +} +#endif \ No newline at end of file diff --git a/messageviewer/src/utils/mimetype.cpp b/messageviewer/src/utils/mimetype.cpp new file mode 100644 --- /dev/null +++ b/messageviewer/src/utils/mimetype.cpp @@ -0,0 +1,86 @@ +/* + Copyright (C) 2005 Till Adam + Copyright (c) 2016 Sandro Knauß + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "utils/mimetype.h" +#include "utils/iconnamecache.h" +#include "messageviewer_debug.h" + +#include + +QMimeType MessageViewer::Util::mimetype(const QString &name) +{ + QMimeDatabase db; + // consider the filename if mimetype cannot be found by content-type + auto mimeTypes = db.mimeTypesForFileName(name); + foreach (const auto &mt, mimeTypes) { + if (mt.name() != QLatin1String("application/octet-stream")) { + return mt; + } + } + + // consider the attachment's contents if neither the Content-Type header + // nor the filename give us a clue + return db.mimeTypeForFile(name); +} + +QString MessageViewer::Util::fileNameForMimetype(const QString &mimeType, int iconSize, + const QString &fallbackFileName1, + const QString &fallbackFileName2) +{ + QString fileName; + QString tMimeType = mimeType; + + // convert non-registered types to registered types + if (mimeType == QLatin1String("application/x-vnd.kolab.contact")) { + tMimeType = QStringLiteral("text/x-vcard"); + } else if (mimeType == QLatin1String("application/x-vnd.kolab.event")) { + tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.event"); + } else if (mimeType == QLatin1String("application/x-vnd.kolab.task")) { + tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.todo"); + } else if (mimeType == QLatin1String("application/x-vnd.kolab.journal")) { + tMimeType = QStringLiteral("application/x-vnd.akonadi.calendar.journal"); + } else if (mimeType == QLatin1String("application/x-vnd.kolab.note")) { + tMimeType = QStringLiteral("application/x-vnd.akonadi.note"); + } + QMimeDatabase mimeDb; + auto mime = mimeDb.mimeTypeForName(tMimeType); + if (mime.isValid()) { + fileName = mime.iconName(); + } else { + fileName = QStringLiteral("unknown"); + if (!tMimeType.isEmpty()) { + qCWarning(MESSAGEVIEWER_LOG) << "unknown mimetype" << tMimeType; + } + } + //WorkAround for #199083 + if (fileName == QLatin1String("text-vcard")) { + fileName = QStringLiteral("text-x-vcard"); + } + if (fileName.isEmpty()) { + fileName = fallbackFileName1; + if (fileName.isEmpty()) { + fileName = fallbackFileName2; + } + if (!fileName.isEmpty()) { + fileName = mimeDb.mimeTypeForFile(QLatin1String("/tmp/") + fileName).iconName(); + } + } + + return IconNameCache::instance()->iconPath(fileName, iconSize); +} \ No newline at end of file