forked from ieatlint/MagRead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magread.cpp
431 lines (334 loc) · 10.7 KB
/
magread.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
/*
This file is part of MagRead.
MagRead 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 3 of the License, or
(at your option) any later version.
MagRead 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 MagRead. If not, see <http://www.gnu.org/licenses/>.
Written by Jeffrey Malone <[email protected]>
http://blog.tehinterweb.com
*/
#include "magread.h"
#include <QDebug>
MagRead::MagRead(QWidget *parent) : QMainWindow(parent) {
captureAudio = false;
partialRead = false;
audioFormat.setFrequency( 48000 );
audioFormat.setChannels( 1 );
audioFormat.setSampleSize( 16 );
audioFormat.setCodec( "audio/pcm" );
audioFormat.setByteOrder( QAudioFormat::LittleEndian );
audioFormat.setSampleType( QAudioFormat::SignedInt );
magDec = NULL;
qRegisterMetaType<MagCard>( "MagCard" );
settings = new QSettings;
//Set the auto-rotation for Maemo5
#ifdef Q_WS_MAEMO_5
if( settings->value( "autoReorient" ) == true ) {
setAttribute( Qt::WA_Maemo5AutoOrientation, true );
}
#endif
#ifdef Q_WS_MAEMO_5
font.setPointSize( 32 );
#else
font.setPointSize( 16 );
#endif
// Start/stop/back selections
#ifdef Q_OS_SYMBIAN
backSoftKey = new QAction( "Start", this );
backSoftKey->setSoftKeyRole( QAction::PositiveSoftKey );
connect( backSoftKey, SIGNAL( triggered() ), this, SLOT( toggleRead() ) );
addAction( backSoftKey );
#else
mainWidget = new QWidget;
mainLayout = new QVBoxLayout( mainWidget );
mainWidget->setLayout( mainLayout );
mainBackBtn = new QPushButton( "Start" );
mainLayout->addWidget( mainBackBtn, 0 );
connect( mainBackBtn, SIGNAL( clicked() ), this, SLOT( toggleRead() ) );
setCentralWidget( mainWidget );
#endif
//Option and Exit selections
#ifndef Q_OS_SYMBIAN
settingsAction = new QAction( "&Settings", this );
connect( settingsAction, SIGNAL( triggered() ), this, SLOT( settingsPage() ) );
aboutAction = new QAction( "&About", this );
connect( aboutAction, SIGNAL( triggered() ), this, SLOT( aboutDialogue() ) );
exitAction = new QAction( "E&xit", this );
connect( exitAction, SIGNAL( triggered() ), this, SLOT( close() ) );
showDataAction = new QAction( "Show &Data", this );
connect( showDataAction, SIGNAL( triggered() ), this, SLOT( showData() ) );
#endif
#ifdef Q_WS_MAEMO_5
menuBar()->addAction( settingsAction );
menuBar()->addAction( aboutAction );
menuBar()->addAction( exitAction );
#elif !defined( Q_OS_SYMBIAN )
fileMenu = menuBar()->addMenu( "&File" );
fileMenu->addAction( settingsAction );
fileMenu->addAction( aboutAction );
fileMenu->addAction( exitAction );
#endif
mainPage();
}
void MagRead::notice( QString msg, int timeout, mboxStatus status ) {
qDebug() << status << timeout << msg;
if( status == CRITICAL ) {
QMessageBox *mbox = new QMessageBox;
mbox->setText( msg );
mbox->setIcon( QMessageBox::Critical );
mbox->setStandardButtons( QMessageBox::Abort | QMessageBox::Ignore );
int retval = mbox->exec();
if( retval == QMessageBox::Abort )
destroy();
} else {
#ifdef Q_WS_MAEMO_5
QMaemo5InformationBox infoBox;
infoBox.information( 0, msg, timeout );
infoBox.show();
#else
QMessageBox *mbox = new QMessageBox;
mbox->setText( msg );
if( status == INFORMATION )
mbox->setIcon( QMessageBox::Information );
else
mbox->setIcon( QMessageBox::Warning );
mbox->setStandardButtons( QMessageBox::NoButton );
mbox->show();
QTimer::singleShot( timeout, mbox, SLOT( hide() ) );
#endif
}
}
void MagRead::showData() {
if( !card.charStream.isEmpty() ) {
miscPage();
}
}
void MagRead::cardRead( const MagCard _card ) {
card = _card;
cardDetect.setCard( &card );
if( ( card.type & MagCard::CARD_CC || card.type == MagCard::CARD_AAA ) && card.accountValid ) {
if( settings->value( "formatCredit" ) == true )
creditPage();
else
miscPage();
} else if( card.type == MagCard::CARD_AAMVA ) {
if( settings->value( "formatAAMVA" ) == true )
aamvaPage();
else
miscPage();
} else if( card.swipeValid ) {
miscPage();
} else if( partialRead && !card.charStream.isEmpty() ) {
notice( "Showing data from a partial read; may be incomplete/invalid", 750, WARNING );
miscPage( true );
} else {
notice( "Swipe Failed! Please Retry", 750, WARNING );
}
}
/* Main Page */
void MagRead::mainPage() {
QWidget *widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout( widget );
widget->setLayout( layout );
QLabel *label;
label = new QLabel( "MagRead" );
label->setFont( font );
layout->addWidget( label, 1, Qt::AlignHCenter );
QCheckBox *cbox = new QCheckBox( "Show Partial Data" );
layout->addWidget( cbox );
connect( cbox, SIGNAL( toggled( bool ) ), this, SLOT( togglePartialRead( bool ) ) );
onMainPage = true;
#ifdef Q_OS_SYMBIAN
QHBoxLayout *optionsBox = new QHBoxLayout;
QPushButton *settingsBtn = new QPushButton( "Settings" );
optionsBox->addWidget( settingsBtn );
connect( settingsBtn, SIGNAL( clicked() ), this, SLOT( settingsPage() ) );
QPushButton *aboutBtn = new QPushButton( "About" );
optionsBox->addWidget( aboutBtn );
connect( aboutBtn, SIGNAL( clicked() ), this, SLOT( aboutDialogue() ) );
layout->addLayout( optionsBox );
setCentralWidget( widget );
#else
if( mainLayout->count() > 1 ) {
mainLayout->itemAt( 0 )->widget()->hide();
mainLayout->removeItem( mainLayout->itemAt( 0 ) );
}
mainLayout->insertWidget( 0, widget, 1 );
#endif
removeShowData();
}
void MagRead::aboutDialogue() {
QMessageBox *mbox = new QMessageBox;
mbox->setText( "MagRead v" QUOTE( APP_VERSION ) "<br>\nwritten by Jeffrey Malone <<a href=\"mailto:[email protected]\">[email protected]</a>><br>\nMore information can be found at <a href=\"http://blog.tehinterweb.com/\">http://blog.tehinterweb.com</a>" );
mbox->exec();
}
void MagRead::toggleRead() {
QString backStr;
if( captureAudio ) {
captureStop();
if( onMainPage )
backStr = "Start";
else
backStr = "Back";
captureAudio = false;
} else if( onMainPage ) {
captureStart();
backStr = "Stop";
captureAudio = true;
} else {
card.clear();
backStr = "Start";
mainPage();
}
#ifdef Q_OS_SYMBIAN
backSoftKey->setText( backStr );
#else
mainBackBtn->setText( backStr );
#endif
}
void MagRead::captureStart() {
magDec = new MagDecode( this );
connect( magDec, SIGNAL( cardRead( MagCard ) ), this, SLOT( cardRead( MagCard ) ) );
connect( magDec, SIGNAL( errorMsg( QString ) ), this, SLOT( notice( QString ) ) );
magDec->setThreshold( settings->value( "silenceThreshold" ).toInt() );
magDec->setTimeOut( settings->value( "timeOut" ).toInt() );
if( settings->value( "normAuto" ) == false )
magDec->setNorm( settings->value( "norm" ).toInt() );
magDec->setAlgorithm( settings->value( "algorithm" ).toString() );
audioInput = 0;
QList<QAudioDeviceInfo> inputDevices = QAudioDeviceInfo::availableDevices( QAudio::AudioInput );
for( int i = 0; i < inputDevices.size(); i++ ) {
if( inputDevices.at( i ).deviceName() == settings->value( "audioDevice" ) ) {
audioInput = new QAudioInput( inputDevices.at( i ), audioFormat, this );
}
}
if( audioInput == 0 )
audioInput = new QAudioInput( audioFormat, this );
magDec->start();
audioInput->start( magDec );
}
void MagRead::captureStop() {
if( !magDec )
return;
audioInput->stop();
magDec->stop();
delete audioInput;
delete magDec;
magDec = NULL;
}
void MagRead::togglePartialRead( bool _partialRead ) {
partialRead = _partialRead;
}
void MagRead::addShowData() {
#ifdef Q_WS_MAEMO_5
if( !menuBar()->actions().contains( showDataAction ) )
menuBar()->addAction( showDataAction );
#elif !defined( Q_OS_SYMBIAN )
if( !fileMenu->actions().contains( showDataAction ) )
fileMenu->addAction( showDataAction );
#endif
}
void MagRead::removeShowData() {
#ifdef Q_WS_MAEMO_5
if( menuBar()->actions().contains( showDataAction ) )
menuBar()->removeAction( showDataAction );
#elif !defined( Q_OS_SYMBIAN )
if( fileMenu->actions().contains( showDataAction ) )
fileMenu->removeAction( showDataAction );
#endif
}
void MagRead::settingsPage() {
onMainPage = false;
SettingsPage *settingsWidget = new SettingsPage;
connect( settingsWidget, SIGNAL( autoReorientSig( bool ) ), this, SLOT( autoReorient( bool ) ) );
#ifdef Q_OS_SYMBIAN
setCentralWidget( settingsWidget );
#else
if( mainLayout->count() > 1 ) {
mainLayout->itemAt( 0 )->widget()->hide();
mainLayout->removeItem( mainLayout->itemAt( 0 ) );
}
mainLayout->insertWidget( 0, settingsWidget, 1 );
#endif
#ifdef Q_OS_SYMBIAN
backSoftKey->setText( "Back" );
#else
mainBackBtn->setText( "Back" );
#endif
removeShowData();
}
void MagRead::autoReorient( bool enabled ) {
#ifdef Q_WS_MAEMO_5
setAttribute( Qt::WA_Maemo5AutoOrientation, enabled );
#else
Q_UNUSED( enabled );
#endif
}
/* Credit Page */
void MagRead::creditPage() {
notice( "Successfully Read Credit Card", 750 );
onMainPage = false;
AccountCard *accountCard = new AccountCard( &card );
#ifdef Q_OS_SYMBIAN
setCentralWidget( accountCard );
#else
if( mainLayout->count() > 1 ) {
mainLayout->itemAt( 0 )->widget()->hide();
mainLayout->removeItem( mainLayout->itemAt( 0 ) );
}
mainLayout->insertWidget( 0, accountCard, 1 );
#endif
addShowData();
}
void MagRead::aamvaPage() {
notice( "Successfully Read AAMVA Card", 750 );
onMainPage = false;
AAMVACard *aamvaCard = new AAMVACard( &card );
#ifdef Q_OS_SYMBIAN
setCentralWidget( aamvaCard );
#else
if( mainLayout->count() > 1 ) {
mainLayout->itemAt( 0 )->widget()->hide();
mainLayout->removeItem( mainLayout->itemAt( 0 ) );
}
mainLayout->insertWidget( 0, aamvaCard, 1 );
#endif
addShowData();
}
/* Misc Page */
void MagRead::miscPage( bool partial ) {
if( !partial )
notice( "Misc Card Page", 750 );
onMainPage = false;
QScrollArea *scroll = new QScrollArea;
QString tmpStr = card.charStream;
if( tmpStr.contains( '%' ) ) {
tmpStr.replace( '^', "<br>\n[Field Separator]<br>\n" );
} else {
tmpStr.replace( '=', "<br>\n[Field Separator]<br>\n" );
}
tmpStr.replace( '|', "<font color=\"red\">|</font>" );
tmpStr.prepend( "<div align=\"center\">" );
tmpStr.append( "</div>" );
QLabel *label = new QLabel( tmpStr );
label->setFont( font );
scroll->setWidget( label );
scroll->setWidgetResizable( true );
label->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
#ifdef Q_OS_SYMBIAN
setCentralWidget( scroll );
#else
if( mainLayout->count() > 1 ) {
mainLayout->itemAt( 0 )->widget()->hide();
mainLayout->removeItem( mainLayout->itemAt( 0 ) );
}
mainLayout->insertWidget( 0, scroll, 1 );
#endif
removeShowData();
}