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
|
/***************************************************************************
copyright : (C) 2005-2006 by Robby Stephenson
email : robby@periapsis.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "upcvalidator.h"
#include "isbnvalidator.h"
#include <kmdcodec.h>
using Tellico::UPCValidator;
UPCValidator::UPCValidator(TQObject* parent_, const char* name_/*=0*/)
: TQValidator(parent_, name_), m_checkISBN(false) {
}
TQValidator::State UPCValidator::validate(TQString& input_, int& pos_) const {
// check if it's a cuecat first
State catState = decodeCat(input_);
if(catState == Acceptable) {
pos_ = input_.length();
return catState;
}
// no spaces allowed
if(input_.contains(' ')) {
return TQValidator::Invalid;
}
// no real checking, just if starts with 978, use isbnvalidator
const uint len = input_.length();
if(len < 10) {
m_isbn = false;
}
if(!m_checkISBN || (!m_isbn && len < 13)) {
return TQValidator::Intermediate;
}
// once it gets converted to an ISBN, remember that, and use it for later
if(input_.startsWith(TQString::tqfromLatin1("978")) || input_.startsWith(TQString::tqfromLatin1("979"))) {
ISBNValidator val(0);
TQValidator::State s = val.validate(input_, pos_);
if(s == TQValidator::Acceptable) {
m_isbn = true;
// bad hack
UPCValidator* that = const_cast<UPCValidator*>(this);
that->signalISBN();
}
return s;
}
return TQValidator::Intermediate;
}
void UPCValidator::fixup(TQString& input_) const {
if(input_.isEmpty()) {
return;
}
input_ = input_.stripWhiteSpace();
int pos = input_.find(' ');
if(pos > -1) {
input_ = input_.left(pos);
}
if(!m_checkISBN) {
return;
}
const uint len = input_.length();
if(len > 12 && (input_.startsWith(TQString::tqfromLatin1("978")) || input_.startsWith(TQString::tqfromLatin1("979")))) {
TQString s = input_;
ISBNValidator val(0);
int p = 0;
int state = val.validate(s, p);
if(state == TQValidator::Acceptable) {
// bad hack
UPCValidator* that = const_cast<UPCValidator*>(this);
that->signalISBN();
input_ = s;
}
}
}
TQValidator::State UPCValidator::decodeCat(TQString& input_) const {
if(input_.length() < 3) {
return Intermediate;
}
if(!input_.startsWith(TQString::tqfromLatin1(".C3"))) { // all cuecat codes start with .C3
return Invalid;
}
const int periods = input_.contains('.');
if(periods < 4) {
return Intermediate; // not enough yet
} else if(periods > 4) {
return Invalid;
}
// ok, let's have a go, take the third token
TQString code = TQStringList::split('.', input_)[2];
while(code.length() % 4 > 0) {
code += '=';
}
for(uint i = 0; i < code.length(); ++i) {
if(code[i] >= 'A' && code[i] <= 'Z') {
code.replace(i, 1, code[i].lower());
} else if(code[i] >= 'a' && code[i] <= 'z') {
code.replace(i, 1, code[i].upper());
}
}
code = TQString::tqfromLatin1(KCodecs::base64Decode(TQCString(code.latin1())));
for(uint i = 0; i < code.length(); ++i) {
char c = code[i].latin1() ^ 'C';
code.replace(i, 1, c);
}
input_ = code;
return Acceptable;
}
#include "upcvalidator.moc"
|