Commit 965ce7ab authored by con's avatar con
Browse files

Read localized description, name and category from external tools

parent 740743dc
......@@ -99,37 +99,67 @@ ExternalTool::OutputHandling ExternalTool::outputHandling() const
return m_outputHandling;
}
ExternalTool * ExternalTool::createFromXml(const QString &xml, QString *errorMessage)
static QStringList splitLocale(const QString &locale)
{
QString value = locale;
QStringList values;
if (!value.isEmpty())
values << value;
int index = value.indexOf(QLatin1Char('.'));
if (index >= 0) {
value = value.left(index);
if (!value.isEmpty())
values << value;
}
index = value.indexOf(QLatin1Char('_'));
if (index >= 0) {
value = value.left(index);
if (!value.isEmpty())
values << value;
}
return values;
}
static void localizedText(const QStringList &locales, QXmlStreamReader *reader, int *currentLocale, QString *currentText)
{
Q_ASSERT(reader);
Q_ASSERT(currentLocale);
Q_ASSERT(currentText);
if (reader->attributes().hasAttribute(QLatin1String(kXmlLang))) {
int index = locales.indexOf(reader->attributes().value(QLatin1String(kXmlLang)).toString());
if (index >= 0 && (index < *currentLocale || *currentLocale < 0)) {
*currentText = reader->readElementText();
*currentLocale = index;
} else {
reader->skipCurrentElement();
}
} else {
if (*currentLocale < 0 && currentText->isEmpty()) {
*currentText = reader->readElementText();
} else {
reader->skipCurrentElement();
}
}
}
ExternalTool * ExternalTool::createFromXml(const QString &xml, QString *errorMessage, const QString &locale)
{
int descriptionLocale = -1;
int nameLocale = -1;
int categoryLocale = -1;
const QStringList &locales = splitLocale(locale);
ExternalTool *tool = new ExternalTool;
QXmlStreamReader reader(xml);
if (!reader.readNextStartElement() || reader.name() != QLatin1String(kExternalTool))
reader.raiseError(QLatin1String("Missing start element <externaltool>"));
while (reader.readNextStartElement()) {
if (reader.name() == QLatin1String(kDescription)) {
// TODO locale check
if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang))
&& tool->m_description.isEmpty()) {
tool->m_description = reader.readElementText();
} else {
reader.skipCurrentElement();
}
localizedText(locales, &reader, &descriptionLocale, &tool->m_description);
} else if (reader.name() == QLatin1String(kDisplayName)) {
// TODO locale check
if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang))
&& tool->m_displayName.isEmpty()) {
tool->m_displayName = reader.readElementText();
} else {
reader.skipCurrentElement();
}
localizedText(locales, &reader, &nameLocale, &tool->m_displayName);
} else if (reader.name() == QLatin1String(kCategory)) {
// TODO locale check
if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang))
&& tool->m_displayCategory.isEmpty()) {
tool->m_displayCategory = reader.readElementText();
} else {
reader.skipCurrentElement();
}
localizedText(locales, &reader, &categoryLocale, &tool->m_displayCategory);
} else if (reader.name() == QLatin1String(kOrder)) {
if (tool->m_order >= 0) {
reader.raiseError(QLatin1String("only one <order> element allowed"));
......
......@@ -57,7 +57,7 @@ public:
QString arguments() const;
QString workingDirectory() const;
static ExternalTool *createFromXml(const QString &xml, QString *errorMessage = 0);
static ExternalTool *createFromXml(const QString &xml, QString *errorMessage = 0, const QString &locale = QString());
private:
QString m_description;
......
......@@ -53,6 +53,22 @@ static const char * const TEST_XML3 =
" </executable>"
"</externaltool>";
static const char * const TEST_XML_LANG =
"<externaltool>"
" <description>Hi</description>"
" <description xml:lang=\"de\">Hallo</description>"
" <description xml:lang=\"de_CH\">Grüezi</description>"
" <displayname xml:lang=\"de\">Hallo</displayname>"
" <displayname>Hi</displayname>"
" <displayname xml:lang=\"de_CH\">Grüezi</displayname>"
" <category xml:lang=\"de_CH\">Grüezi</category>"
" <category>Hi</category>"
" <category xml:lang=\"de\">Hallo</category>"
" <executable>"
" <path>foo</path>"
" </executable>"
"</externaltool>";
class ExternaltoolTest : public QObject
{
Q_OBJECT
......@@ -61,6 +77,7 @@ private Q_SLOTS:
void testRead1();
void testRead2();
void testRead3();
void testReadLocale();
};
void ExternaltoolTest::testRead1()
......@@ -79,6 +96,7 @@ void ExternaltoolTest::testRead1()
QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentProjectFilePath}"));
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentProjectPath}"));
QCOMPARE(tool->outputHandling(), ExternalTool::ShowInPane);
delete tool;
}
void ExternaltoolTest::testRead2()
......@@ -96,6 +114,7 @@ void ExternaltoolTest::testRead2()
QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentSelectionFilePath}"));
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}"));
QCOMPARE(tool->outputHandling(), ExternalTool::ReplaceSelection);
delete tool;
}
void ExternaltoolTest::testRead3()
......@@ -113,8 +132,46 @@ void ExternaltoolTest::testRead3()
QVERIFY(tool->arguments().startsWith(QLatin1String("-geom %{")));
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}"));
QCOMPARE(tool->outputHandling(), ExternalTool::ReloadDocument);
delete tool;
}
void ExternaltoolTest::testReadLocale()
{
QString error;
ExternalTool *tool;
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error);
QVERIFY(tool != 0);
QVERIFY(error.isEmpty());
QCOMPARE(tool->description(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi"));
delete tool;
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("uk"));
QVERIFY(tool != 0);
QVERIFY(error.isEmpty());
QCOMPARE(tool->description(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Hi"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hi"));
delete tool;
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("de_DE.UTF-8"));
QVERIFY(tool != 0);
QVERIFY(error.isEmpty());
QCOMPARE(tool->description(), QString::fromLatin1("Hallo"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Hallo"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Hallo"));
delete tool;
tool = ExternalTool::createFromXml(QLatin1String(TEST_XML_LANG), &error, QLatin1String("de_CH"));
QVERIFY(tool != 0);
QVERIFY(error.isEmpty());
QCOMPARE(tool->description(), QString::fromLatin1("Grüezi"));
QCOMPARE(tool->displayName(), QString::fromLatin1("Grüezi"));
QCOMPARE(tool->displayCategory(), QString::fromLatin1("Grüezi"));
delete tool;}
QTEST_APPLESS_MAIN(ExternaltoolTest);
#include "tst_externaltooltest.moc"
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