| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "lc_shortcutsstorage.h" |
| |
|
| | #include <QDateTime> |
| | #include <QFile> |
| | #include <QMap> |
| | #include <qxmlstream.h> |
| |
|
| | #include "lc_shortcutinfo.h" |
| |
|
| | struct ShortcutsParsingContext { |
| | ShortcutsParsingContext(); |
| |
|
| | const QString el_ActionMappings; |
| | const QString el_ActionShortcut; |
| | const QString attr_name; |
| | const QString el_Key; |
| | const QString attr_Value; |
| | const QString dtdName; |
| | }; |
| |
|
| | ShortcutsParsingContext::ShortcutsParsingContext() : |
| | el_ActionMappings(QLatin1String("actions-mapping")), |
| | el_ActionShortcut(QLatin1String("action-shortcut")), |
| | attr_name(QLatin1String("name")), |
| | el_Key(QLatin1String("key")), |
| | attr_Value(QLatin1String("value")), |
| | dtdName("LibreCADActionsMapping"){ |
| | } |
| |
|
| | int LC_ShortcutsStorage::loadShortcuts(const QString &filename, QMap<QString, QKeySequence> *result) { |
| | QFile file(filename); |
| | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { |
| | return ERROR_FILE; |
| | } |
| |
|
| | ShortcutsParsingContext ctx; |
| | QXmlStreamReader r(&file); |
| |
|
| | QString currentId; |
| |
|
| | while (!r.atEnd()) { |
| | switch (r.readNext()) { |
| | case QXmlStreamReader::DTD: { |
| | const QStringView dtdName = r.dtdName(); |
| | if (ctx.dtdName != dtdName){ |
| | file.close(); |
| | return ERROR_WRONG_DTD; |
| | } |
| | break; |
| | } |
| | case QXmlStreamReader::StartElement: { |
| | const QStringView name = r.name(); |
| |
|
| | if (name == ctx.el_ActionShortcut) { |
| | if (!currentId.isEmpty()) { |
| | result->insert(currentId, QKeySequence()); |
| | } |
| | currentId = r.attributes().value(ctx.attr_name).toString(); |
| | } else if (name == ctx.el_Key) { |
| | const QXmlStreamAttributes attributes = r.attributes(); |
| | if (attributes.hasAttribute(ctx.attr_Value)) { |
| | const QString keyString = attributes.value(ctx.attr_Value).toString(); |
| | result->insert(currentId, QKeySequence(keyString)); |
| | } else { |
| | result->insert(currentId, QKeySequence()); |
| | } |
| | currentId.clear(); |
| | } |
| | } |
| | default: |
| | break; |
| | } |
| | } |
| |
|
| | file.close(); |
| | if (r.hasError()){ |
| | return ERROR_PARSE; |
| | } |
| |
|
| | return OK; |
| | } |
| |
|
| | int LC_ShortcutsStorage::saveShortcuts(const QString &filename, const QList<LC_ShortcutInfo *> &items, bool resetToDefault) |
| | { |
| | QFile file(filename); |
| | if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) { |
| | return ERROR_FILE; |
| | } |
| |
|
| | const ShortcutsParsingContext ctx; |
| | QXmlStreamWriter w(&file); |
| | w.setAutoFormatting(true); |
| | w.setAutoFormattingIndent(1); |
| | w.writeStartDocument(); |
| | w.writeDTD(QLatin1String("<!DOCTYPE %1>").arg(ctx.dtdName)); |
| | w.writeComment(QString::fromLatin1(" Written at %1."). |
| | arg(QDateTime::currentDateTime().toString(Qt::ISODate))); |
| | w.writeStartElement(ctx.el_ActionMappings); |
| | for (const LC_ShortcutInfo *item : items) { |
| | const QString id = item->getName(); |
| | if (item->hasNoKey()) { |
| | w.writeEmptyElement(ctx.el_ActionShortcut); |
| | w.writeAttribute(ctx.attr_name, id); |
| | } else { |
| | w.writeStartElement(ctx.el_ActionShortcut); |
| | w.writeAttribute(ctx.attr_name, id); |
| | w.writeEmptyElement(ctx.el_Key); |
| | w.writeAttribute(ctx.attr_Value, item->retrieveKey(resetToDefault)); |
| | w.writeEndElement(); |
| | } |
| | } |
| | w.writeEndElement(); |
| | w.writeEndDocument(); |
| |
|
| | file.close(); |
| |
|
| | if (w.hasError()){ |
| | return ERROR_PARSE; |
| | } |
| |
|
| | return OK; |
| | } |
| |
|