| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qibustypes.h" |
| 5 | |
| 6 | #include <QHash> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | Q_LOGGING_CATEGORY(qtQpaInputMethods, "qt.qpa.input.methods" ) |
| 11 | Q_LOGGING_CATEGORY(qtQpaInputMethodsSerialize, "qt.qpa.input.methods.serialize" ) |
| 12 | |
| 13 | QIBusSerializable::QIBusSerializable() |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | void QIBusSerializable::deserializeFrom(const QDBusArgument &argument) |
| 18 | { |
| 19 | argument >> name; |
| 20 | |
| 21 | argument.beginMap(); |
| 22 | while (!argument.atEnd()) { |
| 23 | argument.beginMapEntry(); |
| 24 | QString key; |
| 25 | QDBusVariant value; |
| 26 | argument >> key; |
| 27 | argument >> value; |
| 28 | argument.endMapEntry(); |
| 29 | attachments[key] = qvariant_cast<QDBusArgument>(v: value.variant()); |
| 30 | } |
| 31 | argument.endMap(); |
| 32 | } |
| 33 | |
| 34 | void QIBusSerializable::serializeTo(QDBusArgument &argument) const |
| 35 | { |
| 36 | argument << name; |
| 37 | |
| 38 | argument.beginMap(keyMetaTypeId: qMetaTypeId<QString>(), valueMetaTypeId: qMetaTypeId<QDBusVariant>()); |
| 39 | |
| 40 | for (auto i = attachments.begin(), end = attachments.end(); i != end; ++i) { |
| 41 | argument.beginMapEntry(); |
| 42 | argument << i.key(); |
| 43 | |
| 44 | QDBusVariant variant(i.value().asVariant()); |
| 45 | |
| 46 | argument << variant; |
| 47 | argument.endMapEntry(); |
| 48 | } |
| 49 | argument.endMap(); |
| 50 | } |
| 51 | |
| 52 | QIBusAttribute::QIBusAttribute() |
| 53 | : type(Invalid), |
| 54 | value(0), |
| 55 | start(0), |
| 56 | end(0) |
| 57 | { |
| 58 | name = "IBusAttribute" ; |
| 59 | } |
| 60 | |
| 61 | void QIBusAttribute::serializeTo(QDBusArgument &argument) const |
| 62 | { |
| 63 | argument.beginStructure(); |
| 64 | |
| 65 | QIBusSerializable::serializeTo(argument); |
| 66 | |
| 67 | quint32 t = (quint32) type; |
| 68 | argument << t; |
| 69 | argument << value; |
| 70 | argument << start; |
| 71 | argument << end; |
| 72 | |
| 73 | argument.endStructure(); |
| 74 | } |
| 75 | |
| 76 | void QIBusAttribute::deserializeFrom(const QDBusArgument &argument) |
| 77 | { |
| 78 | argument.beginStructure(); |
| 79 | |
| 80 | QIBusSerializable::deserializeFrom(argument); |
| 81 | |
| 82 | quint32 t; |
| 83 | argument >> t; |
| 84 | type = (QIBusAttribute::Type) t; |
| 85 | argument >> value; |
| 86 | argument >> start; |
| 87 | argument >> end; |
| 88 | |
| 89 | argument.endStructure(); |
| 90 | } |
| 91 | |
| 92 | QTextCharFormat QIBusAttribute::format() const |
| 93 | { |
| 94 | QTextCharFormat fmt; |
| 95 | switch (type) { |
| 96 | case Invalid: |
| 97 | break; |
| 98 | case Underline: { |
| 99 | QTextCharFormat::UnderlineStyle style = QTextCharFormat::NoUnderline; |
| 100 | |
| 101 | switch (value) { |
| 102 | case UnderlineNone: |
| 103 | break; |
| 104 | case UnderlineSingle: |
| 105 | style = QTextCharFormat::SingleUnderline; |
| 106 | break; |
| 107 | case UnderlineDouble: |
| 108 | style = QTextCharFormat::DashUnderline; |
| 109 | break; |
| 110 | case UnderlineLow: |
| 111 | style = QTextCharFormat::DashDotLine; |
| 112 | break; |
| 113 | case UnderlineError: |
| 114 | style = QTextCharFormat::WaveUnderline; |
| 115 | fmt.setUnderlineColor(Qt::red); |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | fmt.setUnderlineStyle(style); |
| 120 | break; |
| 121 | } |
| 122 | case Foreground: |
| 123 | fmt.setForeground(QColor(value)); |
| 124 | break; |
| 125 | case Background: |
| 126 | fmt.setBackground(QColor(value)); |
| 127 | break; |
| 128 | } |
| 129 | return fmt; |
| 130 | } |
| 131 | |
| 132 | QIBusAttributeList::QIBusAttributeList() |
| 133 | { |
| 134 | name = "IBusAttrList" ; |
| 135 | } |
| 136 | |
| 137 | void QIBusAttributeList::serializeTo(QDBusArgument &argument) const |
| 138 | { |
| 139 | argument.beginStructure(); |
| 140 | |
| 141 | QIBusSerializable::serializeTo(argument); |
| 142 | |
| 143 | argument.beginArray(elementMetaTypeId: qMetaTypeId<QDBusVariant>()); |
| 144 | for (int i = 0; i < attributes.size(); ++i) { |
| 145 | QVariant variant; |
| 146 | variant.setValue(attributes.at(i)); |
| 147 | argument << QDBusVariant (variant); |
| 148 | } |
| 149 | argument.endArray(); |
| 150 | |
| 151 | argument.endStructure(); |
| 152 | } |
| 153 | |
| 154 | void QIBusAttributeList::deserializeFrom(const QDBusArgument &arg) |
| 155 | { |
| 156 | qCDebug(qtQpaInputMethodsSerialize) << "QIBusAttributeList::fromDBusArgument()" << arg.currentSignature(); |
| 157 | |
| 158 | arg.beginStructure(); |
| 159 | |
| 160 | QIBusSerializable::deserializeFrom(argument: arg); |
| 161 | |
| 162 | arg.beginArray(); |
| 163 | while (!arg.atEnd()) { |
| 164 | QDBusVariant var; |
| 165 | arg >> var; |
| 166 | |
| 167 | QIBusAttribute attr; |
| 168 | qvariant_cast<QDBusArgument>(v: var.variant()) >> attr; |
| 169 | attributes.append(t: std::move(attr)); |
| 170 | } |
| 171 | arg.endArray(); |
| 172 | |
| 173 | arg.endStructure(); |
| 174 | } |
| 175 | |
| 176 | QList<QInputMethodEvent::Attribute> QIBusAttributeList::imAttributes() const |
| 177 | { |
| 178 | QHash<std::pair<int, int>, QTextCharFormat> rangeAttrs; |
| 179 | const int numAttributes = attributes.size(); |
| 180 | |
| 181 | // Merge text formats for identical ranges into a single QTextFormat. |
| 182 | for (int i = 0; i < numAttributes; ++i) { |
| 183 | const QIBusAttribute &attr = attributes.at(i); |
| 184 | const QTextCharFormat &format = attr.format(); |
| 185 | |
| 186 | if (format.isValid()) { |
| 187 | const std::pair<int, int> range(attr.start, attr.end); |
| 188 | rangeAttrs[range].merge(other: format); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Assemble list in original attribute order. |
| 193 | QList<QInputMethodEvent::Attribute> imAttrs; |
| 194 | imAttrs.reserve(asize: numAttributes); |
| 195 | |
| 196 | for (int i = 0; i < numAttributes; ++i) { |
| 197 | const QIBusAttribute &attr = attributes.at(i); |
| 198 | const QTextFormat &format = attr.format(); |
| 199 | |
| 200 | imAttrs += QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, |
| 201 | attr.start, |
| 202 | attr.end - attr.start, |
| 203 | format.isValid() ? rangeAttrs[std::pair<int, int>(attr.start, attr.end)] : format); |
| 204 | } |
| 205 | |
| 206 | return imAttrs; |
| 207 | } |
| 208 | |
| 209 | QIBusText::QIBusText() |
| 210 | { |
| 211 | name = "IBusText" ; |
| 212 | } |
| 213 | |
| 214 | void QIBusText::serializeTo(QDBusArgument &argument) const |
| 215 | { |
| 216 | argument.beginStructure(); |
| 217 | |
| 218 | QIBusSerializable::serializeTo(argument); |
| 219 | |
| 220 | argument << text << attributes; |
| 221 | argument.endStructure(); |
| 222 | } |
| 223 | |
| 224 | void QIBusText::deserializeFrom(const QDBusArgument &argument) |
| 225 | { |
| 226 | qCDebug(qtQpaInputMethodsSerialize) << "QIBusText::fromDBusArgument()" << argument.currentSignature(); |
| 227 | |
| 228 | argument.beginStructure(); |
| 229 | |
| 230 | QIBusSerializable::deserializeFrom(argument); |
| 231 | |
| 232 | argument >> text; |
| 233 | QDBusVariant variant; |
| 234 | argument >> variant; |
| 235 | qvariant_cast<QDBusArgument>(v: variant.variant()) >> attributes; |
| 236 | |
| 237 | argument.endStructure(); |
| 238 | } |
| 239 | |
| 240 | QIBusEngineDesc::QIBusEngineDesc() |
| 241 | : rank(0) |
| 242 | { |
| 243 | name = "IBusEngineDesc" ; |
| 244 | } |
| 245 | |
| 246 | void QIBusEngineDesc::serializeTo(QDBusArgument &argument) const |
| 247 | { |
| 248 | argument.beginStructure(); |
| 249 | |
| 250 | QIBusSerializable::serializeTo(argument); |
| 251 | |
| 252 | argument << engine_name; |
| 253 | argument << longname; |
| 254 | argument << description; |
| 255 | argument << language; |
| 256 | argument << license; |
| 257 | argument << author; |
| 258 | argument << icon; |
| 259 | argument << layout; |
| 260 | argument << rank; |
| 261 | argument << hotkeys; |
| 262 | argument << symbol; |
| 263 | argument << setup; |
| 264 | argument << layout_variant; |
| 265 | argument << layout_option; |
| 266 | argument << version; |
| 267 | argument << textdomain; |
| 268 | argument << iconpropkey; |
| 269 | |
| 270 | argument.endStructure(); |
| 271 | } |
| 272 | |
| 273 | void QIBusEngineDesc::deserializeFrom(const QDBusArgument &argument) |
| 274 | { |
| 275 | qCDebug(qtQpaInputMethodsSerialize) << "QIBusEngineDesc::fromDBusArgument()" << argument.currentSignature(); |
| 276 | argument.beginStructure(); |
| 277 | |
| 278 | QIBusSerializable::deserializeFrom(argument); |
| 279 | |
| 280 | argument >> engine_name; |
| 281 | argument >> longname; |
| 282 | argument >> description; |
| 283 | argument >> language; |
| 284 | argument >> license; |
| 285 | argument >> author; |
| 286 | argument >> icon; |
| 287 | argument >> layout; |
| 288 | argument >> rank; |
| 289 | argument >> hotkeys; |
| 290 | argument >> symbol; |
| 291 | argument >> setup; |
| 292 | // Previous IBusEngineDesc supports the arguments between engine_name |
| 293 | // and setup. |
| 294 | if (argument.currentSignature() == "" ) |
| 295 | goto olderThanV2; |
| 296 | argument >> layout_variant; |
| 297 | argument >> layout_option; |
| 298 | // Previous IBusEngineDesc supports the arguments between engine_name |
| 299 | // and layout_option. |
| 300 | if (argument.currentSignature() == "" ) |
| 301 | goto olderThanV3; |
| 302 | argument >> version; |
| 303 | if (argument.currentSignature() == "" ) |
| 304 | goto olderThanV4; |
| 305 | argument >> textdomain; |
| 306 | if (argument.currentSignature() == "" ) |
| 307 | goto olderThanV5; |
| 308 | argument >> iconpropkey; |
| 309 | // <-- insert new member streaming here (1/2) |
| 310 | goto newest; |
| 311 | olderThanV2: |
| 312 | layout_variant.clear(); |
| 313 | layout_option.clear(); |
| 314 | olderThanV3: |
| 315 | version.clear(); |
| 316 | olderThanV4: |
| 317 | textdomain.clear(); |
| 318 | olderThanV5: |
| 319 | iconpropkey.clear(); |
| 320 | // <-- insert new members here (2/2) |
| 321 | newest: |
| 322 | argument.endStructure(); |
| 323 | } |
| 324 | |
| 325 | QIBusPropTypeClientCommitPreedit::QIBusPropTypeClientCommitPreedit(bool inClientCommitPreedit) |
| 326 | : clientCommitPreedit(inClientCommitPreedit) |
| 327 | { |
| 328 | } |
| 329 | |
| 330 | void QIBusPropTypeClientCommitPreedit::serializeTo(QDBusArgument &argument) const |
| 331 | { |
| 332 | argument.beginStructure(); |
| 333 | argument << clientCommitPreedit; |
| 334 | argument.endStructure(); |
| 335 | } |
| 336 | |
| 337 | void QIBusPropTypeClientCommitPreedit::deserializeFrom(const QDBusArgument &argument) |
| 338 | { |
| 339 | argument.beginStructure(); |
| 340 | argument >> clientCommitPreedit; |
| 341 | argument.endStructure(); |
| 342 | } |
| 343 | |
| 344 | QIBusPropTypeContentType::QIBusPropTypeContentType(unsigned int inPurpose, unsigned int inHints) |
| 345 | : purpose(inPurpose) |
| 346 | , hints(inHints) |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | void QIBusPropTypeContentType::serializeTo(QDBusArgument &argument) const |
| 351 | { |
| 352 | argument.beginStructure(); |
| 353 | argument << purpose << hints; |
| 354 | argument.endStructure(); |
| 355 | } |
| 356 | |
| 357 | void QIBusPropTypeContentType::deserializeFrom(const QDBusArgument &argument) |
| 358 | { |
| 359 | argument.beginStructure(); |
| 360 | argument >> purpose; |
| 361 | argument >> hints; |
| 362 | argument.endStructure(); |
| 363 | } |
| 364 | |
| 365 | QT_END_NAMESPACE |
| 366 | |