1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/persistence/smileypack.cpp

217 lines
5.9 KiB
C++
Raw Normal View History

2014-07-25 20:52:14 +08:00
/*
2014-08-02 22:53:18 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
2014-07-25 20:52:14 +08:00
2014-08-02 22:53:18 +08:00
This program is libre software: you can redistribute it and/or modify
2014-07-25 20:52:14 +08:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 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 COPYING file for more details.
*/
#include "smileypack.h"
2015-06-06 07:44:47 +08:00
#include "src/persistence/settings.h"
#include "src/widget/style.h"
2014-07-25 20:52:14 +08:00
#include <QFileInfo>
#include <QFile>
#include <QFont>
#include <QFontInfo>
2014-09-11 21:44:34 +08:00
#include <QIcon>
#include <QPixmap>
#include <QDir>
#include <QCoreApplication>
#include <QDomDocument>
#include <QDomElement>
#include <QBuffer>
#include <QStringBuilder>
2014-07-25 20:52:14 +08:00
SmileyPack::SmileyPack()
{
load(Settings::getInstance().getSmileyPack());
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this, &SmileyPack::onSmileyPackChanged);
}
SmileyPack& SmileyPack::getInstance()
{
static SmileyPack smileyPack;
return smileyPack;
}
QList<QPair<QString, QString> > SmileyPack::listSmileyPacks(const QStringList &paths)
2014-07-30 15:18:41 +08:00
{
2014-07-31 23:36:55 +08:00
QList<QPair<QString, QString> > smileyPacks;
2014-07-30 15:18:41 +08:00
for (QString path : paths)
2014-07-30 15:18:41 +08:00
{
if (path.leftRef(1) == "~")
path.replace(0, 1, QDir::homePath());
2014-07-30 15:18:41 +08:00
QDir dir(path);
if (!dir.exists())
continue;
for (const QString& subdirectory : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
2014-07-31 23:36:55 +08:00
{
dir.cd(subdirectory);
QFileInfoList entries = dir.entryInfoList(QStringList() << "emoticons.xml", QDir::Files);
if (entries.size() > 0) // does it contain a file called emoticons.xml?
{
QString packageName = dir.dirName();
QString absPath = entries[0].absoluteFilePath();
QString relPath = QDir(QCoreApplication::applicationDirPath()).relativeFilePath(absPath);
if (relPath.leftRef(2) == "..")
{
if (!smileyPacks.contains(QPair<QString, QString>(packageName, absPath)))
smileyPacks << QPair<QString, QString>(packageName, absPath);
else if (!smileyPacks.contains(QPair<QString, QString>(packageName, relPath)))
smileyPacks << QPair<QString, QString>(packageName, relPath); // use relative path for subdirectories
}
}
dir.cdUp();
2014-07-31 23:36:55 +08:00
}
2014-07-30 15:18:41 +08:00
}
return smileyPacks;
}
2014-08-11 00:50:40 +08:00
bool SmileyPack::isValid(const QString &filename)
{
return QFile(filename).exists();
}
2014-07-25 20:52:14 +08:00
bool SmileyPack::load(const QString& filename)
{
// discard old data
2014-07-31 22:08:44 +08:00
filenameTable.clear();
iconCache.clear();
2014-07-31 04:07:26 +08:00
emoticons.clear();
2014-07-31 22:08:44 +08:00
path.clear();
2014-07-25 20:52:14 +08:00
// open emoticons.xml
QFile xmlFile(filename);
if (!xmlFile.open(QIODevice::ReadOnly))
2014-07-25 20:52:14 +08:00
return false; // cannot open file
2014-07-29 16:50:39 +08:00
/* parse the cfg file
2014-07-25 20:52:14 +08:00
* sample:
* <?xml version='1.0'?>
* <messaging-emoticon-map>
* <emoticon file="smile.png" >
* <string>:)</string>
* <string>:-)</string>
* </emoticon>
* <emoticon file="sad.png" >
* <string>:(</string>
* <string>:-(</string>
* </emoticon>
* </messaging-emoticon-map>
*/
2014-07-31 22:08:44 +08:00
path = QFileInfo(filename).absolutePath();
2014-07-25 20:52:14 +08:00
QDomDocument doc;
doc.setContent(xmlFile.readAll());
QDomNodeList emoticonElements = doc.elementsByTagName("emoticon");
for (int i = 0; i < emoticonElements.size(); ++i)
{
QString file = emoticonElements.at(i).attributes().namedItem("file").nodeValue();
QDomElement stringElement = emoticonElements.at(i).firstChildElement("string");
2014-07-31 04:07:26 +08:00
QStringList emoticonSet; // { ":)", ":-)" } etc.
2014-07-25 20:52:14 +08:00
while (!stringElement.isNull())
{
2015-01-25 04:46:53 +08:00
QString emoticon = stringElement.text()
.replace("<","&lt;").replace(">","&gt;");
2014-07-31 22:08:44 +08:00
filenameTable.insert(emoticon, file);
2014-07-31 22:08:44 +08:00
cacheSmiley(file); // preload all smileys
if (!getCachedSmiley(emoticon).isNull())
emoticonSet.push_back(emoticon);
2014-07-25 20:52:14 +08:00
stringElement = stringElement.nextSibling().toElement();
2014-07-25 20:52:14 +08:00
}
if (emoticonSet.size() > 0)
emoticons.push_back(emoticonSet);
2014-07-25 20:52:14 +08:00
}
// success!
return true;
}
2014-07-31 23:36:55 +08:00
QString SmileyPack::smileyfied(QString msg)
2014-07-25 20:52:14 +08:00
{
2014-07-29 17:22:47 +08:00
QRegExp exp("\\S+"); // matches words
2014-07-25 20:52:14 +08:00
int index = msg.indexOf(exp);
2014-07-26 16:05:32 +08:00
// if a word is key of a smiley, replace it by its corresponding image in Rich Text
while (index >= 0)
2014-07-25 20:52:14 +08:00
{
QString key = exp.cap();
2014-07-31 22:08:44 +08:00
if (filenameTable.contains(key))
2014-07-25 20:52:14 +08:00
{
2014-07-31 23:36:55 +08:00
QString imgRichText = getAsRichText(key);
2014-07-25 20:52:14 +08:00
2014-07-30 15:18:41 +08:00
msg.replace(index, key.length(), imgRichText);
2014-07-29 17:22:47 +08:00
index += imgRichText.length() - key.length();
2014-07-25 20:52:14 +08:00
}
2014-07-29 17:22:47 +08:00
index = msg.indexOf(exp, index + key.length());
2014-07-25 20:52:14 +08:00
}
2014-07-29 17:22:47 +08:00
return msg;
2014-07-25 20:52:14 +08:00
}
2014-07-31 04:07:26 +08:00
QList<QStringList> SmileyPack::getEmoticons() const
{
return emoticons;
}
2014-07-31 23:36:55 +08:00
QString SmileyPack::getAsRichText(const QString &key)
2014-07-31 04:07:26 +08:00
{
2014-11-16 19:40:44 +08:00
return QString("<img title=\"%1\" src=\"key:%1\"\\>").arg(key);
2014-07-31 04:07:26 +08:00
}
QIcon SmileyPack::getAsIcon(const QString &key)
2014-11-16 19:40:44 +08:00
{
2014-11-16 19:52:53 +08:00
return getCachedSmiley(key);
2014-11-16 19:40:44 +08:00
}
2014-07-31 22:08:44 +08:00
void SmileyPack::cacheSmiley(const QString &name)
2014-07-29 16:50:39 +08:00
{
QString filename = QDir(path).filePath(name);
2014-07-29 16:50:39 +08:00
QIcon icon;
icon.addFile(filename);
iconCache.insert(name, icon);
2014-07-31 22:08:44 +08:00
}
QIcon SmileyPack::getCachedSmiley(const QString &key)
2014-07-31 22:08:44 +08:00
{
// valid key?
if (!filenameTable.contains(key))
2014-11-16 19:52:53 +08:00
return QPixmap();
2014-07-31 22:08:44 +08:00
// cache it if needed
QString file = filenameTable.value(key);
if (!iconCache.contains(file))
2014-07-31 22:08:44 +08:00
cacheSmiley(file);
return iconCache.value(file);
2014-07-29 16:50:39 +08:00
}
2014-07-25 20:52:14 +08:00
void SmileyPack::onSmileyPackChanged()
{
load(Settings::getInstance().getSmileyPack());
}