aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/cerence/hwr/plugin/t9writedictionary.cpp
blob: 14c3ae2fb579a9313092930f98f544c174c8f2c5 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "t9writedictionary_p.h"
#include <QLoggingCategory>

QT_BEGIN_NAMESPACE
namespace QtVirtualKeyboard {

Q_DECLARE_LOGGING_CATEGORY(lcT9Write)

// T9WriteAbstractSource

T9WriteAbstractSource::T9WriteAbstractSource(const DECUMA_SRC_DICTIONARY_INFO &info) :
    _info(info)
{
}

T9WriteAbstractSource::~T9WriteAbstractSource()
{
}

const DECUMA_SRC_DICTIONARY_INFO *T9WriteAbstractSource::info() const
{
    return &_info;
}

// T9WriteFileSource

T9WriteFileSource::T9WriteFileSource(const DECUMA_SRC_DICTIONARY_INFO &info, const QString &fileName) :
    T9WriteAbstractSource(info),
    file(fileName),
    _data(nullptr),
    _size(0)
{
}

bool T9WriteFileSource::load()
{
    if (!_data) {
        if (file.open(QIODevice::ReadOnly)) {
            _size = file.size();
            _data = file.map(0, _size, QFile::NoOptions);
            if (!_data) {
                _size = 0;
                qCWarning(lcT9Write) << "Could not read dictionary file" << file.fileName();
            }
            file.close();
        }
    }

    return _data != nullptr;
}

bool T9WriteFileSource::create(qint64 createSize)
{
    close();

    if (file.open(QIODevice::ReadWrite)) {
        if (file.resize(createSize)) {
            _size = file.size();
            _data = file.map(0, _size, QFile::NoOptions);
            if (!_data) {
                _size = 0;
                qCWarning(lcT9Write) << "Could not read dictionary file" << file.fileName();
            }
        } else {
            qCWarning(lcT9Write) << "Could not resize dictionary file" << file.fileName();
        }
        file.close();
    }

    return _data != nullptr;
}

void T9WriteFileSource::close()
{
    if (_data) {
        file.unmap(static_cast<uchar *>(_data));
        _data = nullptr;
        _size = 0;
    }
}

QString T9WriteFileSource::name() const
{
    return file.fileName();
}

const void *T9WriteFileSource::data() const
{
    return _data;
}

qint64 T9WriteFileSource::size() const
{
    return _size;
}

// T9WriteStringSource

T9WriteStringSource::T9WriteStringSource(const DECUMA_SRC_DICTIONARY_INFO &info, const QStringList &source, const QString &name) :
    T9WriteAbstractSource(info),
    source(source),
    _name(name)
{
}

bool T9WriteStringSource::load()
{
    _data = source.join(QLatin1Char('\r'));
    return true;
}

QString T9WriteStringSource::name() const
{
    return _name;
}

const void *T9WriteStringSource::data() const
{
    return _data.utf16();
}

qint64 T9WriteStringSource::size() const
{
    return _data.size();
}

// T9WriteDictionary

T9WriteDictionary::T9WriteDictionary(QSharedPointer<T9WriteAbstractSource> source,
                                     DECUMA_SESSION *decumaSession,
                                     const DECUMA_MEM_FUNCTIONS &memFuncs,
                                     bool cjk) :
    source(source),
    decumaSession(decumaSession),
    memFuncs(memFuncs),
    cjk(cjk),
    convertedData(nullptr),
    convertedSize(0)
{
}

T9WriteDictionary::~T9WriteDictionary()
{
    if (convertedData) {
        DECUMA_STATUS status = DECUMA_API(DestroyConvertedDictionary)(&convertedData, &memFuncs);
        Q_ASSERT(status == decumaNoError);
        Q_ASSERT(convertedData == nullptr);
    }
}

bool T9WriteDictionary::load()
{
    return source->load();
}

bool T9WriteDictionary::convert()
{
    if (!source->data() || convertedData)
        return false;

    DECUMA_STATUS status;
    status = DECUMA_API(ConvertDictionary)(&convertedData, source->data(), static_cast<DECUMA_UINT32>(source->size()),
                                           source->info(), &convertedSize, &memFuncs);

    if (status != decumaNoError) {
        qCWarning(lcT9Write) << "Could not convert dictionary";
    }

    return status == decumaNoError;
}

QString T9WriteDictionary::name() const
{
    return source->name();
}

const void *T9WriteDictionary::data() const
{
    return convertedData ? convertedData : source->data();
}

qint64 T9WriteDictionary::size() const
{
    return convertedData ? convertedSize : source->size();
}

T9WriteDynamicDictionary::T9WriteDynamicDictionary(QSharedPointer<T9WriteAbstractSource> source, int maxWords, const DECUMA_MEM_FUNCTIONS &memFuncs, bool cjk) :
    source(source),
    memFuncs(memFuncs),
    cjk(cjk),
    _data(nullptr)
{
    DECUMA_API(DynamicDictionaryCreate)(&_data, static_cast<DECUMA_UINT32>(maxWords), &memFuncs);
}

T9WriteDynamicDictionary::~T9WriteDynamicDictionary()
{
    if (_data) {
        DECUMA_API(DynamicDictionaryDestroy)(&_data);
    }
}

bool T9WriteDynamicDictionary::load()
{
    if (!_data)
        return false;

    DECUMA_STATUS status;
    DECUMA_UINT32 nUnprocessedSize = 0;
    if (source->load()) {
        status = DECUMA_API(DynamicDictionaryAddWords)(_data, source->data(), static_cast<DECUMA_UINT32>(source->size()), &nUnprocessedSize);
        if (status) {
            qCWarning(lcT9Write) << "Could not load words to dynamic dictionary, error" << status;
        }
        source->close();
    }

    return true;
}

QString T9WriteDynamicDictionary::name() const
{
    return source->name();
}

const void *T9WriteDynamicDictionary::data() const
{
    return _data;
}

qint64 T9WriteDynamicDictionary::size() const
{
    return 0;
}

qint64 T9WriteDynamicDictionary::bufferSize() const
{
    DECUMA_UINT32 result = 0;
    if (_data)
        DECUMA_API(DynamicDictionaryGetWordsBufferSize)(_data, &result);
    return result;
}

void T9WriteDynamicDictionary::save()
{
    if (_data) {
        DECUMA_UINT32 nWordsWritten = 0;
        DECUMA_UINT32 nBytesWritten = 0;
        qint64 fileSize = bufferSize();
        if (source->create(fileSize)) {
            DECUMA_STATUS status = DECUMA_API(DynamicDictionaryGetWords)(_data, const_cast<void *>(source->data()), static_cast<DECUMA_UINT32>(fileSize), &nWordsWritten, &nBytesWritten);
            source->close();
        }
    }
}

bool T9WriteDynamicDictionary::hasWord(const QString &word)
{
    if (!_data)
        return false;

    int found = 0;
    DECUMA_API(DynamicDictionaryHasWord)(_data, word.utf16(), &found);

    return found != 0;
}

bool T9WriteDynamicDictionary::removeWord(const QString &word)
{
    DECUMA_STATUS status;

    status = DECUMA_API(DynamicDictionaryDeleteWord)(_data, word.utf16());

    return !status;
}

} // namespace QtVirtualKeyboard
QT_END_NAMESPACE