File indexing completed on 2024-05-19 15:10:51

0001 /*
0002     SPDX-FileCopyrightText: 2008 Carlo Segato <brandon.ml@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "kemoticonsprovider.h"
0008 #include "kemoticons.h"
0009 
0010 #include <QFileInfo>
0011 #include <QDir>
0012 #include <QPixmap>
0013 #include "kemoticons_core_debug.h"
0014 
0015 class KEmoticonsProviderPrivate
0016 {
0017 public:
0018     KEmoticonsProviderPrivate();
0019     QString m_themeName;
0020     QString m_fileName;
0021     QString m_themePath;
0022     QHash<QString, QStringList> m_emoticonsMap;
0023     QHash<QChar, QList<KEmoticonsProvider::Emoticon> > m_emoticonsIndex;
0024     QSize m_preferredSize;
0025 };
0026 
0027 KEmoticonsProviderPrivate::KEmoticonsProviderPrivate()
0028 {
0029 }
0030 
0031 KEmoticonsProvider::KEmoticonsProvider(QObject *parent)
0032     : QObject(parent), d(new KEmoticonsProviderPrivate)
0033 {
0034 }
0035 
0036 KEmoticonsProvider::~KEmoticonsProvider()
0037 {
0038 }
0039 
0040 #if KEMOTICONS_BUILD_DEPRECATED_SINCE(5, 0)
0041 void KEmoticonsProvider::save()
0042 {
0043     saveTheme();
0044 }
0045 #endif
0046 
0047 QString KEmoticonsProvider::themeName() const
0048 {
0049     return d->m_themeName;
0050 }
0051 
0052 void KEmoticonsProvider::setThemeName(const QString &name)
0053 {
0054     d->m_themeName = name;
0055 }
0056 
0057 QString KEmoticonsProvider::themePath() const
0058 {
0059     return d->m_themePath;
0060 }
0061 
0062 QString KEmoticonsProvider::fileName() const
0063 {
0064     return d->m_fileName;
0065 }
0066 
0067 void KEmoticonsProvider::clearEmoticonsMap()
0068 {
0069     d->m_emoticonsMap.clear();
0070 }
0071 
0072 #if KEMOTICONS_BUILD_DEPRECATED_SINCE(5, 0)
0073 void KEmoticonsProvider::addEmoticonsMap(QString key, QStringList value)
0074 {
0075     addMapItem(key, value);
0076 }
0077 #endif
0078 
0079 void KEmoticonsProvider::addMapItem(QString key, QStringList value)
0080 {
0081     if (!value.isEmpty()) {
0082         d->m_emoticonsMap.insert(key, value);
0083     }
0084 }
0085 
0086 #if KEMOTICONS_BUILD_DEPRECATED_SINCE(5, 0)
0087 void KEmoticonsProvider::removeEmoticonsMap(QString key)
0088 {
0089     removeMapItem(key);
0090 }
0091 #endif
0092 
0093 void KEmoticonsProvider::removeMapItem(QString key)
0094 {
0095     d->m_emoticonsMap.remove(key);
0096 }
0097 
0098 QHash<QString, QStringList> KEmoticonsProvider::emoticonsMap() const
0099 {
0100     return d->m_emoticonsMap;
0101 }
0102 
0103 QHash<QChar, QList<KEmoticonsProvider::Emoticon> > KEmoticonsProvider::emoticonsIndex() const
0104 {
0105     return d->m_emoticonsIndex;
0106 }
0107 
0108 #if KEMOTICONS_BUILD_DEPRECATED_SINCE(5, 0)
0109 void KEmoticonsProvider::createNew()
0110 {
0111     newTheme();
0112 }
0113 #endif
0114 
0115 void KEmoticonsProvider::setThemePath(const QString &path)
0116 {
0117     QFileInfo info(path);
0118     d->m_fileName = info.fileName();
0119     d->m_themeName = info.dir().dirName();
0120     d->m_themePath = info.absolutePath();
0121 }
0122 
0123 bool KEmoticonsProvider::copyEmoticon(const QString &emo)
0124 {
0125     QFile file(emo);
0126     QFileInfo info(file);
0127     QString newPath(d->m_themePath + QLatin1Char('/') + info.fileName());
0128     return file.copy(newPath);
0129 }
0130 
0131 #if KEMOTICONS_BUILD_DEPRECATED_SINCE(5, 0)
0132 void KEmoticonsProvider::addEmoticonIndex(const QString &path, const QStringList &emoList)
0133 {
0134     addIndexItem(path, emoList);
0135 }
0136 #endif
0137 
0138 void KEmoticonsProvider::addIndexItem(const QString &path, const QStringList &emoList)
0139 {
0140     for (const QString &s : emoList) {
0141         KEmoticonsProvider::Emoticon e;
0142         QPixmap p;
0143 
0144         QString escaped = s.toHtmlEscaped();
0145         e.picPath = path;
0146         p.load(path);
0147 
0148         const bool hasPreferredSize = d->m_preferredSize.isValid();
0149         const int preferredHeight = hasPreferredSize ? d->m_preferredSize.height() : p.height();
0150         const int preferredWidth = hasPreferredSize ? d->m_preferredSize.width() : p.width();
0151 
0152         e.picHTMLCode = QStringLiteral("<img align=\"center\" title=\"%1\" alt=\"%1\" src=\"file://%2\" width=\"%3\" height=\"%4\" />").arg(escaped, path, QString::number(preferredWidth), QString::number(preferredHeight));
0153 
0154         e.matchTextEscaped = escaped;
0155         e.matchText = s;
0156 
0157         if (!s.isEmpty() && !escaped.isEmpty()) {
0158             d->m_emoticonsIndex[escaped[0]].append(e);
0159             d->m_emoticonsIndex[s[0]].append(e);
0160         }
0161     }
0162 }
0163 
0164 #if KEMOTICONS_BUILD_DEPRECATED_SINCE(5, 0)
0165 void KEmoticonsProvider::removeEmoticonIndex(const QString &path, const QStringList &emoList)
0166 {
0167     removeIndexItem(path, emoList);
0168 }
0169 #endif
0170 
0171 void KEmoticonsProvider::removeIndexItem(const QString &path, const QStringList &emoList)
0172 {
0173     for (const QString &s : emoList) {
0174         QString escaped = s.toHtmlEscaped();
0175 
0176         if (s.isEmpty() || escaped.isEmpty()) {
0177             continue;
0178         }
0179 
0180         QList<Emoticon> ls = d->m_emoticonsIndex.value(escaped[0]);
0181 
0182         for (int i = 0; i < ls.size(); ++i) {
0183             if (ls.at(i).picPath == path) {
0184                 ls.removeAt(i);
0185             }
0186         }
0187 
0188         ls = d->m_emoticonsIndex.value(s[0]);
0189 
0190         for (int i = 0; i < ls.size(); ++i) {
0191             if (ls.at(i).picPath == path) {
0192                 ls.removeAt(i);
0193             }
0194         }
0195     }
0196 }
0197 
0198 void KEmoticonsProvider::setPreferredEmoticonSize(const QSize &size)
0199 {
0200     d->m_preferredSize = size;
0201 }
0202 
0203 QSize KEmoticonsProvider::preferredEmoticonSize() const
0204 {
0205     return d->m_preferredSize;
0206 }
0207 
0208 #include "moc_kemoticonsprovider.cpp"