1 
2 #include "db_token.h"
3 #include "exception.h"
4 #include "pki_scard.h"
5 #include "sql.h"
6 #include "widgets/XcaWarning.h"
7 
db_token()8 db_token::db_token() : db_base("manageTokens")
9 {
10 	updateHeaders();
11 }
12 
saveHeaderState()13 void db_token::saveHeaderState()
14 {
15 }
16 
rename_token_in_database(pki_scard * token)17 void db_token::rename_token_in_database(pki_scard *token)
18 {
19 	if (!token)
20 		return;
21 	Transaction;
22 	if (!TransBegin())
23 		return;
24 	QList<pki_scard*> list = Store.sqlSELECTpki<pki_scard>(
25                 QString("SELECT item FROM tokens "
26 			"WHERE card_serial=? AND card_model=? and object_id=?"),
27                 QList<QVariant>() << QVariant(token->getSerial())
28 				<< QVariant(token->getModel())
29 				<< QVariant(token->getId()));
30 
31 	foreach(pki_scard *item, list) {
32 		if (token->compare(item))
33 			item->updateLabel(token->getIntName());
34 	}
35 	TransCommit();
36 }
37 
setData(const QModelIndex & index,const QVariant & value,int role)38 bool db_token::setData(const QModelIndex &index, const QVariant &value, int role)
39 {
40 	QString on, nn;
41 	pki_base *item;
42 	if (index.isValid() && role == Qt::EditRole) {
43 		nn = value.toString();
44 		item = fromIndex(index);
45 		on = item->getIntName();
46 		if (on == nn)
47 			return true;
48 		try {
49 			if (item->renameOnToken(slot, nn)) {
50 				item->setIntName(nn);
51 				rename_token_in_database(
52 					dynamic_cast<pki_scard*>(item));
53 				emit dataChanged(index, index);
54 				return true;
55 			}
56 		} catch (errorEx &err) {
57 			XCA_ERROR(err);
58 		}
59 	}
60 	return false;
61 }
62