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

181 lines
4.8 KiB
C++
Raw Normal View History

2014-07-25 20:52:14 +08:00
/*
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
This file is part of Tox Qt GUI.
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 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"
#include "settings.h"
#include <QFileInfo>
#include <QFile>
#include <QtXml>
#include <QDebug>
SmileyPack::SmileyPack()
{
load(Settings::getInstance().getSmileyPack());
connect(&Settings::getInstance(), &Settings::smileyPackChanged, this, &SmileyPack::onSmileyPackChanged);
}
SmileyPack& SmileyPack::getInstance()
{
static SmileyPack smileyPack;
return smileyPack;
}
2014-07-30 15:18:41 +08:00
QStringList SmileyPack::listSmileyPacks(const QString &path)
{
QStringList smileyPacks;
QDir dir(path);
foreach (const QString& subdirectory, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
{
dir.cd(subdirectory);
QFileInfoList entries = dir.entryInfoList(QStringList() << "emoticons.xml", QDir::Files);
if (entries.size() > 0) // does it contain a file called emoticons.xml?
smileyPacks << QDir(QCoreApplication::applicationDirPath()).relativeFilePath(entries[0].absoluteFilePath());
dir.cdUp();
}
return smileyPacks;
}
2014-07-25 20:52:14 +08:00
bool SmileyPack::load(const QString& filename)
{
// discard old data
2014-07-29 16:50:39 +08:00
assignmentTable.clear();
cache.clear();
2014-07-31 04:07:26 +08:00
emoticons.clear();
2014-07-25 20:52:14 +08:00
// open emoticons.xml
QFile xmlFile(filename);
if(!xmlFile.open(QIODevice::ReadOnly))
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>
*/
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())
{
2014-07-31 04:07:26 +08:00
QString emoticon = stringElement.text();
assignmentTable.insert(emoticon, file);
emoticonSet.push_back(emoticon);
2014-07-25 20:52:14 +08:00
stringElement = stringElement.nextSibling().toElement();
}
2014-07-31 04:07:26 +08:00
emoticons.push_back(emoticonSet);
2014-07-25 20:52:14 +08:00
}
2014-07-29 16:50:39 +08:00
path = QFileInfo(filename).absolutePath();
2014-07-25 20:52:14 +08:00
// success!
return true;
}
2014-07-29 17:22:47 +08:00
QString SmileyPack::replaceEmoticons(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-29 16:50:39 +08:00
if (assignmentTable.contains(key))
2014-07-25 20:52:14 +08:00
{
2014-07-29 16:50:39 +08:00
QString file = assignmentTable[key];
if (!cache.contains(file)) {
loadSmiley(file);
}
2014-07-25 20:52:14 +08:00
2014-07-29 16:59:58 +08:00
QString imgRichText = "<img src=\"data:image/png;base64," % cache[file] % "\">";
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;
}
QString SmileyPack::getRichText(const QString &key)
{
QString file = assignmentTable[key];
if (!cache.contains(file)) {
loadSmiley(file);
}
return "<img src=\"data:image/png;base64," % cache[file] % "\">";
}
QIcon SmileyPack::getIcon(const QString &key)
{
return QIcon(path + '/' + assignmentTable[key]);
}
2014-07-29 16:50:39 +08:00
void SmileyPack::loadSmiley(const QString &name)
{
QSize size(16, 16); // TODO: adapt to text size
2014-07-29 17:22:47 +08:00
QString filename = path % '/' % name;
2014-07-29 16:50:39 +08:00
QImage img(filename);
if (!img.isNull())
{
QImage scaledImg = img.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QByteArray scaledImgData;
QBuffer buffer(&scaledImgData);
scaledImg.save(&buffer, "PNG");
cache.insert(name, scaledImgData.toBase64());
}
}
2014-07-25 20:52:14 +08:00
void SmileyPack::onSmileyPackChanged()
{
load(Settings::getInstance().getSmileyPack());
}