Commit 1b9b27d2 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Load localized tag entries

parent 1a433eeb
......@@ -21,6 +21,10 @@
#include <QIODevice>
#include <QLocale>
#include <QBuffer>
#include <QByteArray>
#include <QStandardPaths>
#include <QFile>
#include "kconfigini_p.h"
#include "kconfigbackend_p.h"
......@@ -28,11 +32,107 @@
#include <kis_debug.h>
const QByteArray KisTagLoader::s_group {"Desktop Entry"};
const QByteArray KisTagLoader::s_type {"Type"};
const QByteArray KisTagLoader::s_tag {"Tag"};
const QByteArray KisTagLoader::s_name {"Name"};
const QByteArray KisTagLoader::s_url {"URL"};
const QByteArray KisTagLoader::s_comment {"Comment"};
class KisTagLoader::Private {
public:
QString url;
QString name;
QString comment;
KEntryMap map;
QString expandString(const QString &value)
{
QString aValue = value;
// check for environment variables and make necessary translations
int nDollarPos = aValue.indexOf(QLatin1Char('$'));
while (nDollarPos != -1 && nDollarPos + 1 < aValue.length()) {
// there is at least one $
if (aValue[nDollarPos + 1] == QLatin1Char('(')) {
int nEndPos = nDollarPos + 1;
// the next character is not $
while ((nEndPos <= aValue.length()) && (aValue[nEndPos] != QLatin1Char(')'))) {
nEndPos++;
}
nEndPos++;
QString cmd = aValue.mid(nDollarPos + 2, nEndPos - nDollarPos - 3);
QString result;
// FIXME: wince does not have pipes
#ifndef _WIN32_WCE
FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
if (fs) {
QTextStream ts(fs, QIODevice::ReadOnly);
result = ts.readAll().trimmed();
pclose(fs);
}
#endif
aValue.replace(nDollarPos, nEndPos - nDollarPos, result);
nDollarPos += result.length();
} else if (aValue[nDollarPos + 1] != QLatin1Char('$')) {
int nEndPos = nDollarPos + 1;
// the next character is not $
QStringRef aVarName;
if (aValue[nEndPos] == QLatin1Char('{')) {
while ((nEndPos <= aValue.length()) && (aValue[nEndPos] != QLatin1Char('}'))) {
nEndPos++;
}
nEndPos++;
aVarName = aValue.midRef(nDollarPos + 2, nEndPos - nDollarPos - 3);
} else {
while (nEndPos <= aValue.length() &&
(aValue[nEndPos].isNumber() ||
aValue[nEndPos].isLetter() ||
aValue[nEndPos] == QLatin1Char('_'))) {
nEndPos++;
}
aVarName = aValue.midRef(nDollarPos + 1, nEndPos - nDollarPos - 1);
}
QString env;
if (!aVarName.isEmpty()) {
#ifdef Q_OS_WIN
if (aVarName == QLatin1String("HOME")) {
env = QDir::homePath();
} else
#endif
{
QByteArray pEnv = qgetenv(aVarName.toLatin1().constData());
if (!pEnv.isEmpty()) {
env = QString::fromLocal8Bit(pEnv.constData());
} else {
if (aVarName == QStringLiteral("QT_DATA_HOME")) {
env = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
} else if (aVarName == QStringLiteral("QT_CONFIG_HOME")) {
env = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
} else if (aVarName == QStringLiteral("QT_CACHE_HOME")) {
env = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
}
}
}
aValue.replace(nDollarPos, nEndPos - nDollarPos, env);
nDollarPos += env.length();
} else {
aValue.remove(nDollarPos, nEndPos - nDollarPos);
}
} else {
// remove one of the dollar signs
aValue.remove(nDollarPos, 1);
nDollarPos++;
}
nDollarPos = aValue.indexOf(QLatin1Char('$'), nDollarPos);
}
return aValue;
}
};
KisTagLoader::KisTagLoader()
......@@ -52,6 +152,7 @@ QString KisTagLoader::name() const
void KisTagLoader::setName(QString &name) const
{
d->map.setEntry(s_group, s_name, name, KEntryMap::EntryDirty);
d->name = name;
}
......@@ -62,6 +163,7 @@ QString KisTagLoader::url() const
void KisTagLoader::setUrl(const QString &url) const
{
d->map.setEntry(s_group, s_url, url, KEntryMap::EntryDirty);
d->url = url;
}
......@@ -72,6 +174,7 @@ QString KisTagLoader::comment() const
void KisTagLoader::setComment(const QString &comment) const
{
d->map.setEntry(s_group, s_comment, comment, KEntryMap::EntryDirty);
d->comment = comment;
}
......@@ -82,27 +185,30 @@ bool KisTagLoader::load(QIODevice &io)
}
KIS_ASSERT(io.isOpen());
KEntryMap map;
KConfigIniBackend ini;
KConfigBackend::ParseInfo r = ini.parseConfigIO(io, QLocale().name().toUtf8(), map, KConfigBackend::ParseOption::ParseGlobal, false);
if (!r == KConfigBackend::ParseInfo::ParseOk) {
io.reset();
qWarning() << "Could not load this tag file" << QString::fromUtf8(io.readAll());
KConfigBackend::ParseInfo r = ini.parseConfigIO(io, QLocale().name().toUtf8(), d->map, KConfigBackend::ParseOption::ParseGlobal, false);
if (r != KConfigBackend::ParseInfo::ParseOk) {
qWarning() << "Could not load this tag file" << r;
return false;
}
QString type = map.getEntry("Desktop Entry", "Type");
if (type != "Tag") return false;
QString t = d->map.getEntry(s_group, s_type);
if (t != s_tag) {
qWarning() << "Not a tag desktop file" << t;
return false;
}
d->url = map.getEntry("Desktop Entry", "URL");
d->name = map.getEntry("Desktop Entry", "Name");
d->comment = map.getEntry("Desktop Entry", "Comment");
d->url = d->map.getEntry(s_group, s_url);
d->name = d->map.getEntry(s_group, s_name, QString(), KEntryMap::SearchLocalized);
d->comment = d->map.getEntry(s_group, s_comment, QString(), KEntryMap::SearchLocalized);
return true;
}
bool KisTagLoader::save(QIODevice &io)
{
return false;
KConfigIniBackend ini;
ini.writeEntries(QLocale().name().toUtf8(), io, d->map);
return true;
}
......@@ -56,6 +56,12 @@ public:
private:
static const QByteArray s_group;
static const QByteArray s_type;
static const QByteArray s_tag;
static const QByteArray s_name;
static const QByteArray s_url;
static const QByteArray s_comment;
class Private;
QScopedPointer<Private> d;
};
......
......@@ -42,20 +42,30 @@ void TestTagLoader ::testLoadTag()
bool r = tagLoader.load(f);
f.close();
QVERIFY(r);
// QVERIFY(tagLoader.name() == "* Favorites");
// QVERIFY(tagLoader.comment() == "Your favorite brush presets");
// QVERIFY(tagLoader.url() == "* Favorites");
QVERIFY(tagLoader.name() == "* Favorites");
QVERIFY(tagLoader.comment() == "Your favorite brush presets");
QVERIFY(tagLoader.url() == "* Favorites");
QLocale nl(QLocale::Dutch, QLocale::Netherlands);
QLocale::setDefault(nl);
f.open(QFile::ReadOnly);
QVERIFY(f.isOpen());
r = tagLoader.load(f);
f.close();
// QLocale nl(QLocale::Dutch, QLocale::Netherlands);
// QLocale::setDefault(nl);
QVERIFY(r);
QVERIFY(tagLoader.name() == "* Favorieten");
QVERIFY(tagLoader.comment() == "Jouw favoriete penseel presets");
QVERIFY(tagLoader.url() == "* Favorites");
// bool r = tagLoader.load(f);
}
// QVERIFY(r);
// QVERIFY(tagLoader.name() == "* Favorieten");
// QVERIFY(tagLoader.comment() == "Jouw favoriete penseel presets");
// QVERIFY(tagLoader.url() == "* Favorites");
void TestTagLoader::testSaveTag()
{
}
......
......@@ -27,6 +27,7 @@ class TestTagLoader : public QObject
Q_OBJECT
private Q_SLOTS:
void testLoadTag();
void testSaveTag();
private:
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment