File size: 4,979 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*******************************************************************************
 *
 This file is part of the LibreCAD project, a 2D CAD program

 Copyright (C) 2024 LibreCAD.org
 Copyright (C) 2024 sand1024

 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 2
 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
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ******************************************************************************/

#include "lc_shortcutsstorage.h"

#include <QDateTime>
#include <QFile>
#include <QMap>
#include <qxmlstream.h>

#include "lc_shortcutinfo.h"

struct ShortcutsParsingContext {// XML parsing context with strings.
    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()) {// shortcut element without key element == empty shortcut
                        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();
                } // if key element
            } // case QXmlStreamReader::StartElement
            default:
                break;
        } // switch
    } // while !atEnd

    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); // Historical, used to be QDom.
    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(); // Shortcut
        }
    }
    w.writeEndElement();
    w.writeEndDocument();

    file.close();

    if (w.hasError()){
        return ERROR_PARSE;
    }

    return OK;
}