forked from qt-creator/qt-creator
Read localized description, name and category from external tools
This commit is contained in:
@@ -99,37 +99,67 @@ ExternalTool::OutputHandling ExternalTool::outputHandling() const
|
|||||||
return m_outputHandling;
|
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;
|
ExternalTool *tool = new ExternalTool;
|
||||||
QXmlStreamReader reader(xml);
|
QXmlStreamReader reader(xml);
|
||||||
|
|
||||||
if (!reader.readNextStartElement() || reader.name() != QLatin1String(kExternalTool))
|
if (!reader.readNextStartElement() || reader.name() != QLatin1String(kExternalTool))
|
||||||
reader.raiseError(QLatin1String("Missing start element <externaltool>"));
|
reader.raiseError(QLatin1String("Missing start element <externaltool>"));
|
||||||
while (reader.readNextStartElement()) {
|
while (reader.readNextStartElement()) {
|
||||||
if (reader.name() == QLatin1String(kDescription)) {
|
if (reader.name() == QLatin1String(kDescription)) {
|
||||||
// TODO locale check
|
localizedText(locales, &reader, &descriptionLocale, &tool->m_description);
|
||||||
if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang))
|
|
||||||
&& tool->m_description.isEmpty()) {
|
|
||||||
tool->m_description = reader.readElementText();
|
|
||||||
} else {
|
|
||||||
reader.skipCurrentElement();
|
|
||||||
}
|
|
||||||
} else if (reader.name() == QLatin1String(kDisplayName)) {
|
} else if (reader.name() == QLatin1String(kDisplayName)) {
|
||||||
// TODO locale check
|
localizedText(locales, &reader, &nameLocale, &tool->m_displayName);
|
||||||
if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang))
|
|
||||||
&& tool->m_displayName.isEmpty()) {
|
|
||||||
tool->m_displayName = reader.readElementText();
|
|
||||||
} else {
|
|
||||||
reader.skipCurrentElement();
|
|
||||||
}
|
|
||||||
} else if (reader.name() == QLatin1String(kCategory)) {
|
} else if (reader.name() == QLatin1String(kCategory)) {
|
||||||
// TODO locale check
|
localizedText(locales, &reader, &categoryLocale, &tool->m_displayCategory);
|
||||||
if (!reader.attributes().hasAttribute(QLatin1String(kXmlLang))
|
|
||||||
&& tool->m_displayCategory.isEmpty()) {
|
|
||||||
tool->m_displayCategory = reader.readElementText();
|
|
||||||
} else {
|
|
||||||
reader.skipCurrentElement();
|
|
||||||
}
|
|
||||||
} else if (reader.name() == QLatin1String(kOrder)) {
|
} else if (reader.name() == QLatin1String(kOrder)) {
|
||||||
if (tool->m_order >= 0) {
|
if (tool->m_order >= 0) {
|
||||||
reader.raiseError(QLatin1String("only one <order> element allowed"));
|
reader.raiseError(QLatin1String("only one <order> element allowed"));
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ public:
|
|||||||
QString arguments() const;
|
QString arguments() const;
|
||||||
QString workingDirectory() 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:
|
private:
|
||||||
QString m_description;
|
QString m_description;
|
||||||
|
|||||||
@@ -53,6 +53,22 @@ static const char * const TEST_XML3 =
|
|||||||
" </executable>"
|
" </executable>"
|
||||||
"</externaltool>";
|
"</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
|
class ExternaltoolTest : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -61,6 +77,7 @@ private Q_SLOTS:
|
|||||||
void testRead1();
|
void testRead1();
|
||||||
void testRead2();
|
void testRead2();
|
||||||
void testRead3();
|
void testRead3();
|
||||||
|
void testReadLocale();
|
||||||
};
|
};
|
||||||
|
|
||||||
void ExternaltoolTest::testRead1()
|
void ExternaltoolTest::testRead1()
|
||||||
@@ -79,6 +96,7 @@ void ExternaltoolTest::testRead1()
|
|||||||
QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentProjectFilePath}"));
|
QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentProjectFilePath}"));
|
||||||
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentProjectPath}"));
|
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentProjectPath}"));
|
||||||
QCOMPARE(tool->outputHandling(), ExternalTool::ShowInPane);
|
QCOMPARE(tool->outputHandling(), ExternalTool::ShowInPane);
|
||||||
|
delete tool;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExternaltoolTest::testRead2()
|
void ExternaltoolTest::testRead2()
|
||||||
@@ -96,6 +114,7 @@ void ExternaltoolTest::testRead2()
|
|||||||
QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentSelectionFilePath}"));
|
QCOMPARE(tool->arguments(), QString::fromLatin1("%{CurrentSelectionFilePath}"));
|
||||||
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}"));
|
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}"));
|
||||||
QCOMPARE(tool->outputHandling(), ExternalTool::ReplaceSelection);
|
QCOMPARE(tool->outputHandling(), ExternalTool::ReplaceSelection);
|
||||||
|
delete tool;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ExternaltoolTest::testRead3()
|
void ExternaltoolTest::testRead3()
|
||||||
@@ -113,8 +132,46 @@ void ExternaltoolTest::testRead3()
|
|||||||
QVERIFY(tool->arguments().startsWith(QLatin1String("-geom %{")));
|
QVERIFY(tool->arguments().startsWith(QLatin1String("-geom %{")));
|
||||||
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}"));
|
QCOMPARE(tool->workingDirectory(), QString::fromLatin1("%{CurrentPath}"));
|
||||||
QCOMPARE(tool->outputHandling(), ExternalTool::ReloadDocument);
|
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);
|
QTEST_APPLESS_MAIN(ExternaltoolTest);
|
||||||
|
|
||||||
#include "tst_externaltooltest.moc"
|
#include "tst_externaltooltest.moc"
|
||||||
|
|||||||
Reference in New Issue
Block a user