-
Notifications
You must be signed in to change notification settings - Fork 6
/
nfcmodeltondef.cpp
859 lines (805 loc) · 32.2 KB
/
nfcmodeltondef.cpp
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/****************************************************************************
**
** Copyright (C) 2012-2013 Andreas Jakl.
** All rights reserved.
** Contact: Andreas Jakl ([email protected])
**
** This file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
****************************************************************************/
#include "nfcmodeltondef.h"
/*!
\brief Construct a new converter, based on the list of NfcRecordItems, as managed
by the NfcRecordModel.
*/
NfcModelToNdef::NfcModelToNdef(QList<NfcRecordItem*> &nfcRecordItems, QObject *parent) :
QObject(parent),
m_recordItems(nfcRecordItems)
{
}
void NfcModelToNdef::setNfcStats(NfcStats *nfcStats)
{
m_nfcStats = nfcStats;
}
/*!
\brief Convert the current data in the record model to an NDEF message.
*/
QNdefMessage* NfcModelToNdef::convertToNdefMessage()
{
QNdefMessage* ndefMessage = new QNdefMessage();
NfcTypes::MessageType curMessageType;
NfcTypes::RecordContent curRecordContent;
int curRecordIndex = 0;
if (m_nfcStats) {
m_nfcStats->resetComposedMsgCount();
}
//qDebug() << "NfcModelToNdef::convertToNdefMessage() Record item array size: " << recordItems.size();
// Go through array and convert the data to Ndef Records
while(curRecordIndex < m_recordItems.size())
{
NfcRecordItem* curItem = m_recordItems[curRecordIndex];
curRecordContent = curItem->recordContent();
curMessageType = curItem->messageType();
// If we find a new record header, check the message type
// and convert this to a QNdefRecord, which is appended to the
// ndefMessage.
if (curRecordContent == NfcTypes::RecordHeader) {
// Starting a new Ndef Record
int parseEndIndex = -1;
curMessageType = curItem->messageType();
if (m_nfcStats) {
m_nfcStats->incComposedMsgCount(curMessageType);
}
switch (curMessageType) {
case NfcTypes::MsgSmartPoster:
ndefMessage->append(*convertSpFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgUri:
ndefMessage->append(*convertUriFromModel(curRecordIndex, parseEndIndex, true));
break;
case NfcTypes::MsgText:
ndefMessage->append(*convertTextFromModel(curRecordIndex, parseEndIndex, true));
break;
case NfcTypes::MsgSms:
ndefMessage->append(*convertSmsFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgBusinessCard:
ndefMessage->append(*convertBusinessCardFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgSocialNetwork:
ndefMessage->append(*convertSocialNetworkFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgGeo:
ndefMessage->append(*convertGeoFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgStore:
ndefMessage->append(*convertStoreFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgImage:
ndefMessage->append(*convertImageFromModel(curRecordIndex, parseEndIndex, true));
break;
case NfcTypes::MsgCustom:
ndefMessage->append(*convertCustomFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgLaunchApp:
ndefMessage->append(*convertLaunchAppFromModel(curRecordIndex, parseEndIndex));
break;
case NfcTypes::MsgAndroidAppRecord:
ndefMessage->append(*convertAndroidAppRecordFromModel(curRecordIndex, parseEndIndex));
break;
default:
// MsgAnnotatedUrl, MsgCombination and a few others
// are just templates to add multiple records
// at once and don't exist as a type in the final model.
qDebug() << "Warning: don't know how to handle this message type in NfcModelToNdef::convertToNdefMessage().";
break;
}
if (parseEndIndex == curRecordIndex || parseEndIndex == -1) {
// Record wasn't parsed
// Skip it
qDebug() << "Warning: unable to parse record " << curRecordIndex;
curRecordIndex++;
} else {
// Parse end index is set - jump over the already parsed items.
// parseEndIndex is the index of the next item to be looked at,
// so no need to increment here.
curRecordIndex = parseEndIndex;
}
} else {
// It shouldn't be possible to have a different record type without
// having a header item before, so this would be an error in the UI
qDebug() << "Error in model: new message type without header item at index " << curRecordIndex << " (can be the case while deleting a record)";
curRecordIndex++;
}
}
return ndefMessage;
}
NdefNfcSpRecord* NfcModelToNdef::convertSpFromModel(const int startIndex, int& endIndex)
{
NdefNfcSpRecord* newRecord = new NdefNfcSpRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgSmartPoster ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordUri: {
newRecord->setUri(*convertUriFromModel(curIndex, curIndex, false));
break; }
case NfcTypes::RecordText:
case NfcTypes::RecordTextLanguage: {
newRecord->addTitle(*convertTextFromModel(curIndex, curIndex, false));
break; }
case NfcTypes::RecordSpAction: {
NdefNfcSpRecord::NfcAction selectedAction = NdefNfcSpRecord::RFU;
switch (curItem->selectedOption()) {
case 0:
selectedAction = NdefNfcSpRecord::DoAction;
break;
case 1:
selectedAction = NdefNfcSpRecord::SaveForLater;
break;
case 2:
selectedAction = NdefNfcSpRecord::OpenForEditing;
break;
}
newRecord->setAction(selectedAction);
curIndex ++;
break; }
case NfcTypes::RecordSpSize:
newRecord->setSize(curItem->currentText().toUInt());
curIndex ++;
break;
case NfcTypes::RecordSpType:
newRecord->setMimeType(curItem->currentText());
curIndex ++;
break;
case NfcTypes::RecordImageFilename: {
newRecord->setImage(*convertImageFromModel(curIndex, curIndex, false));
break; }
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
//qDebug() << "Sp payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
return newRecord;
}
QNdefNfcUriRecord* NfcModelToNdef::convertUriFromModel(const int startIndex, int& endIndex, bool expectHeader)
{
QNdefNfcUriRecord* newRecord = new QNdefNfcUriRecord();
int curIndex = startIndex;
if (expectHeader) {
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgUri ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
curIndex = startIndex + 1;
}
if (curIndex >= m_recordItems.size())
return newRecord;
NfcRecordItem* curItem = m_recordItems[curIndex];
newRecord->setUri(QUrl(curItem->currentText()));
endIndex = curIndex + 1;
return newRecord;
}
QNdefNfcTextRecord* NfcModelToNdef::convertTextFromModel(const int startIndex, int& endIndex, bool expectHeader)
{
QNdefNfcTextRecord* newRecord = new QNdefNfcTextRecord();
int curIndex = startIndex;
if (expectHeader) {
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgText ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
curIndex = startIndex + 1;
}
bool reachedRecordEnd = false;
bool foundText = false;
bool foundLocale = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordText:
if (foundText) {
reachedRecordEnd = true;
} else {
newRecord->setText(curItem->currentText());
foundText = true;
}
break;
case NfcTypes::RecordTextLanguage:
if (foundLocale) {
reachedRecordEnd = true;
} else {
newRecord->setLocale(curItem->currentText());
foundLocale = true;
}
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
curIndex ++;
}
endIndex = curIndex;
return newRecord;
}
NdefNfcMimeImageRecord* NfcModelToNdef::convertImageFromModel(const int startIndex, int& endIndex, bool expectHeader)
{
NdefNfcMimeImageRecord* newRecord = new NdefNfcMimeImageRecord();
int curIndex = startIndex;
if (expectHeader) {
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgImage ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
curIndex = startIndex + 1;
}
if (curIndex >= m_recordItems.size())
return newRecord;
NfcRecordItem* curItem = m_recordItems[curIndex];
if (newRecord->setImage(curItem->currentText())) {
// Loading the image was successful
} else {
// Not successful in loading the image
qDebug() << "Error loading the image for creating the payload.";
}
endIndex = curIndex + 1;
return newRecord;
}
NdefNfcSmsRecord* NfcModelToNdef::convertSmsFromModel(const int startIndex, int& endIndex)
{
NdefNfcSmsRecord* newRecord = new NdefNfcSmsRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgSms ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
// Need to find out all the info beforehand, in order to decide on
// whether to write an URI record or a Smart Poster
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordPhoneNumber:
newRecord->setSmsNumber(curItem->currentText());
curIndex ++;
break;
case NfcTypes::RecordSmsBody:
newRecord->setSmsBody(curItem->currentText());
curIndex ++;
break;
case NfcTypes::RecordText:
case NfcTypes::RecordTextLanguage:
newRecord->addTitle(*convertTextFromModel(curIndex, curIndex, false));
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
//qDebug() << "Sms payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
//qDebug() << "Is Sp: " << newRecord->isSp();
return newRecord;
}
NdefNfcMimeVcardRecord* NfcModelToNdef::convertBusinessCardFromModel(const int startIndex, int& endIndex)
{
NdefNfcMimeVcardRecord* newRecord = new NdefNfcMimeVcardRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgBusinessCard ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
QContact contact;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
QString value = curItem->currentText();
const NfcTypes::RecordContent contentType = curItem->recordContent();
switch (contentType) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordNamePrefix:
case NfcTypes::RecordFirstName:
case NfcTypes::RecordMiddleName:
case NfcTypes::RecordLastName:
case NfcTypes::RecordNameSuffix:
contactSetDetail<QContactName>(contact, contentType, value);
break;
case NfcTypes::RecordNickname:
contactSetDetail<QContactNickname>(contact, contentType, value);
break;
case NfcTypes::RecordPhoneNumber:
contactSetDetail<QContactPhoneNumber>(contact, contentType, value);
break;
case NfcTypes::RecordEmailAddress:
contactSetDetail<QContactEmailAddress>(contact, contentType, value);
break;
case NfcTypes::RecordContactUrl:
contactSetDetail<QContactUrl>(contact, contentType, value);
break;
case NfcTypes::RecordOrganizationName:
case NfcTypes::RecordOrganizationDepartment:
case NfcTypes::RecordOrganizationRole:
case NfcTypes::RecordOrganizationTitle:
contactSetDetail<QContactOrganization>(contact, contentType, value);
break;
case NfcTypes::RecordBirthday:
contactSetDetail<QContactBirthday>(contact, contentType, value);
break;
case NfcTypes::RecordNote:
contactSetDetail<QContactNote>(contact, contentType, value);
break;
case NfcTypes::RecordImageFilename:
contactSetDetail<QContactThumbnail>(contact, contentType, value);
break;
case NfcTypes::RecordCountry:
case NfcTypes::RecordLocality:
case NfcTypes::RecordPostOfficeBox:
case NfcTypes::RecordPostcode:
case NfcTypes::RecordRegion:
case NfcTypes::RecordStreet:
contactSetDetail<QContactAddress>(contact, contentType, value);
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
curIndex ++;
}
endIndex = curIndex;
newRecord->setContact(contact);
//qDebug() << "Contact payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
return newRecord;
}
template<class T> bool NfcModelToNdef::contactSetDetail(QContact& contact, const NfcTypes::RecordContent contentType, const QString& value)
{
// Well, should work with the following way:
//QContactName curDetail = contact.detail<QContactName>();
// But then, the definitionName() of the contact detail isn't set
// properly, causing the detail not to be written to the vCard.
// Therefore the workaround with the template for the specific
// detail class, and then a conversion to the generic QContactDetail
// through its constructor.
T curDetailSpecific = contact.detail<T>();
QContactDetail curDetail(curDetailSpecific);
const char* contactField;
switch (contentType) {
case NfcTypes::RecordNamePrefix:
contactField = QContactName::FieldPrefix.latin1();
break;
case NfcTypes::RecordFirstName:
contactField = QContactName::FieldFirstName.latin1();
break;
case NfcTypes::RecordMiddleName:
contactField = QContactName::FieldMiddleName.latin1();
break;
case NfcTypes::RecordLastName:
contactField = QContactName::FieldLastName.latin1();
break;
case NfcTypes::RecordNameSuffix:
contactField = QContactName::FieldSuffix.latin1();
break;
case NfcTypes::RecordNickname:
contactField = QContactNickname::FieldNickname.latin1();
break;
case NfcTypes::RecordPhoneNumber:
contactField = QContactPhoneNumber::FieldNumber.latin1();
break;
case NfcTypes::RecordEmailAddress:
contactField = QContactEmailAddress::FieldEmailAddress.latin1();
break;
case NfcTypes::RecordContactUrl:
contactField = QContactUrl::FieldUrl.latin1();
break;
case NfcTypes::RecordOrganizationName:
contactField = QContactOrganization::FieldName.latin1();
break;
case NfcTypes::RecordOrganizationDepartment:
contactField = QContactOrganization::FieldDepartment.latin1();
break;
case NfcTypes::RecordOrganizationRole:
contactField = QContactOrganization::FieldRole.latin1();
break;
case NfcTypes::RecordOrganizationTitle:
contactField = QContactOrganization::FieldTitle.latin1();
break;
case NfcTypes::RecordBirthday:
contactField = QContactBirthday::FieldBirthday.latin1();
break;
case NfcTypes::RecordNote:
contactField = QContactNote::FieldNote.latin1();
break;
case NfcTypes::RecordImageFilename:
contactField = QContactThumbnail::FieldThumbnail.latin1();
break;
case NfcTypes::RecordCountry:
contactField = QContactAddress::FieldCountry.latin1();
break;
case NfcTypes::RecordLocality:
contactField = QContactAddress::FieldLocality.latin1();
break;
case NfcTypes::RecordPostOfficeBox:
contactField = QContactAddress::FieldPostOfficeBox.latin1();
break;
case NfcTypes::RecordPostcode:
contactField = QContactAddress::FieldPostcode.latin1();
break;
case NfcTypes::RecordRegion:
contactField = QContactAddress::FieldRegion.latin1();
break;
case NfcTypes::RecordStreet:
contactField = QContactAddress::FieldStreet.latin1();
break;
default:
qDebug() << "Warning: Unknown contact detail in NfcModelToNdef::contactSetDetail()";
contactField = "Unknown";
break;
}
if (contentType == NfcTypes::RecordImageFilename) {
// Image-based detail
QImage contactImg(value);
if (!contactImg.isNull()) {
// Succeeded in loading the image
curDetail.setValue(contactField, contactImg);
}
} else {
// Text-based detail
curDetail.setValue(contactField, value);
}
return contact.saveDetail(&curDetail);
}
NdefNfcSocialRecord *NfcModelToNdef::convertSocialNetworkFromModel(const int startIndex, int &endIndex)
{
NdefNfcSocialRecord* newRecord = new NdefNfcSocialRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgSocialNetwork ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordSocialNetworkType: {
NdefNfcSocialRecord::NfcSocialType socialType = (NdefNfcSocialRecord::NfcSocialType)curItem->selectedOption();
newRecord->setSocialType(socialType);
curIndex ++;
break; }
case NfcTypes::RecordSocialNetworkName:
newRecord->setSocialUserName(curItem->currentText());
curIndex ++;
break;
case NfcTypes::RecordText:
case NfcTypes::RecordTextLanguage:
newRecord->addTitle(*convertTextFromModel(curIndex, curIndex, false));
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
//qDebug() << "Social payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
//qDebug() << "Is Social == Sp: " << newRecord->isSp();
return newRecord;
}
NdefNfcGeoRecord *NfcModelToNdef::convertGeoFromModel(const int startIndex, int &endIndex)
{
NdefNfcGeoRecord* newRecord = new NdefNfcGeoRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgGeo ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordGeoLatitude:
newRecord->setLatitude(curItem->currentText().toDouble());
curIndex ++;
break;
case NfcTypes::RecordGeoLongitude:
newRecord->setLongitude(curItem->currentText().toDouble());
curIndex ++;
break;
case NfcTypes::RecordText:
case NfcTypes::RecordTextLanguage:
newRecord->addTitle(*convertTextFromModel(curIndex, curIndex, false));
break;
case NfcTypes::RecordGeoType: {
NdefNfcGeoRecord::NfcGeoType geoType = (NdefNfcGeoRecord::NfcGeoType)curItem->selectedOption();
newRecord->setGeoType(geoType);
curIndex ++;
break; }
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
//qDebug() << "Geo payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
//qDebug() << "Is Geo == Sp: " << newRecord->isSp();
return newRecord;
}
NdefNfcStoreLinkRecord *NfcModelToNdef::convertStoreFromModel(const int startIndex, int &endIndex)
{
NdefNfcStoreLinkRecord* newRecord = new NdefNfcStoreLinkRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgStore ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordStoreNokia:
case NfcTypes::RecordStoreSymbian:
case NfcTypes::RecordStoreMeeGoHarmattan:
case NfcTypes::RecordStoreSeries40:
case NfcTypes::RecordStoreWindowsPhone:
case NfcTypes::RecordStoreAndroid:
case NfcTypes::RecordStoreiOS:
case NfcTypes::RecordStoreBlackberry:
case NfcTypes::RecordStoreCustomName:
newRecord->addAppId(appStoreFromRecordContentType(curItem->recordContent()), curItem->currentText());
curIndex ++;
break;
case NfcTypes::RecordText:
case NfcTypes::RecordTextLanguage:
newRecord->addTitle(*convertTextFromModel(curIndex, curIndex, false));
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
//qDebug() << "Store payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
//qDebug() << "Is Store == Sp: " << newRecord->isSp();
return newRecord;
}
NdefNfcStoreLinkRecord::AppStore NfcModelToNdef::appStoreFromRecordContentType(const NfcTypes::RecordContent contentType)
{
switch (contentType) {
case NfcTypes::RecordStoreNokia:
return NdefNfcStoreLinkRecord::StoreNokia;
break;
case NfcTypes::RecordStoreSymbian:
return NdefNfcStoreLinkRecord::StoreSymbian;
break;
case NfcTypes::RecordStoreMeeGoHarmattan:
return NdefNfcStoreLinkRecord::StoreMeeGoHarmattan;
break;
case NfcTypes::RecordStoreSeries40:
return NdefNfcStoreLinkRecord::StoreSeries40;
break;
case NfcTypes::RecordStoreWindowsPhone:
return NdefNfcStoreLinkRecord::StoreWindowsPhone;
break;
case NfcTypes::RecordStoreAndroid:
return NdefNfcStoreLinkRecord::StoreAndroid;
break;
case NfcTypes::RecordStoreiOS:
return NdefNfcStoreLinkRecord::StoreiOS;
break;
case NfcTypes::RecordStoreBlackberry:
return NdefNfcStoreLinkRecord::StoreBlackberry;
break;
case NfcTypes::RecordStoreCustomName:
return NdefNfcStoreLinkRecord::StoreCustomName;
break;
default:
qDebug() << "Warning: Unknown record content type passed to NfcModelToNdef::appStoreFromRecordContentType.";
return NdefNfcStoreLinkRecord::StoreNokia;
break;
}
}
QNdefRecord *NfcModelToNdef::convertCustomFromModel(const int startIndex, int &endIndex)
{
QNdefRecord* newRecord = new NdefNfcGeoRecord();
newRecord->setTypeNameFormat(QNdefRecord::ExternalRtd);
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgCustom ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
bool isEmptyRecord = true;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordTypeNameFormat: {
QNdefRecord::TypeNameFormat recordTnf = (QNdefRecord::TypeNameFormat)curItem->selectedOption();
newRecord->setTypeNameFormat(recordTnf);
curIndex ++;
break; }
case NfcTypes::RecordTypeName: {
newRecord->setType(curItem->currentText().toLatin1());
curIndex ++;
break; }
case NfcTypes::RecordRawPayload:
newRecord->setPayload(curItem->currentText().toLatin1());
curIndex ++;
isEmptyRecord = false;
break;
case NfcTypes::RecordId:
newRecord->setId(curItem->currentText().toLatin1());
curIndex ++;
isEmptyRecord = false;
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
//qDebug() << "Custom payload: (" << newRecord->payload().count() << "): " << newRecord->payload();
return newRecord;
}
NdefNfcLaunchAppRecord *NfcModelToNdef::convertLaunchAppFromModel(const int startIndex, int &endIndex)
{
NdefNfcLaunchAppRecord* newRecord = new NdefNfcLaunchAppRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgLaunchApp ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordLaunchAppArguments:
newRecord->setArguments(curItem->currentText());
break;
case NfcTypes::RecordLaunchAppWindows:
newRecord->addPlatformAppId("Windows", curItem->currentText());
break;
case NfcTypes::RecordLaunchAppWindowsPhone:
newRecord->addPlatformAppId("WindowsPhone", curItem->currentText());
break;
case NfcTypes::RecordLaunchAppPlatform:
{
QString platform = curItem->currentText();
curIndex++;
if (curIndex < m_recordItems.size()) {
QString appId = m_recordItems[curIndex]->currentText();
newRecord->addPlatformAppId(platform, appId);
} else {
qDebug() << "Error in record model: LaunchApp platform without ID.";
reachedRecordEnd = true;
}
break;
}
case NfcTypes::RecordLaunchAppId:
qDebug() << "Error in record model: LaunchApp App ID needs to be preceded by the platform name.";
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
return newRecord;
}
NdefNfcAndroidAppRecord *NfcModelToNdef::convertAndroidAppRecordFromModel(const int startIndex, int &endIndex)
{
NdefNfcAndroidAppRecord* newRecord = new NdefNfcAndroidAppRecord();
if (m_recordItems[startIndex]->messageType() != NfcTypes::MsgAndroidAppRecord ||
m_recordItems[startIndex]->recordContent() != NfcTypes::RecordHeader) {
return newRecord;
}
// Start at the next item after the header
int curIndex = startIndex + 1;
bool reachedRecordEnd = false;
while (curIndex < m_recordItems.size()) {
NfcRecordItem* curItem = m_recordItems[curIndex];
switch (curItem->recordContent()) {
case NfcTypes::RecordHeader:
// Next record starts - quit!
reachedRecordEnd = true;
break;
case NfcTypes::RecordAndroidPackageName:
newRecord->setPackageName(curItem->currentText());
curIndex ++;
break;
default:
// Unknown record content that doesn't belong to this record
reachedRecordEnd = true;
break;
}
if (reachedRecordEnd)
break;
//curIndex ++; // Already incremented by convert...() methods.
}
endIndex = curIndex;
return newRecord;
}