1 | /* |
2 | scdgetinfoassuantransaction.cpp - Assuan Transaction to get information from scdaemon |
3 | Copyright (C) 2009 Klarälvdalens Datakonsult AB |
4 | |
5 | This file is part of GPGME++. |
6 | |
7 | GPGME++ is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. |
11 | |
12 | GPGME++ is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU Library General Public License for more details. |
16 | |
17 | You should have received a copy of the GNU Library General Public License |
18 | along with GPGME++; see the file COPYING.LIB. If not, write to the |
19 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include <config-gpgme++.h> |
24 | |
25 | #include "scdgetinfoassuantransaction.h" |
26 | #include "error.h" |
27 | #include "data.h" |
28 | #include "util.h" |
29 | |
30 | #include <boost/algorithm/string/split.hpp> |
31 | #include <boost/algorithm/string/classification.hpp> |
32 | #include <boost/static_assert.hpp> |
33 | |
34 | #include <sstream> |
35 | |
36 | using namespace GpgME; |
37 | using namespace boost; |
38 | |
39 | ScdGetInfoAssuanTransaction::ScdGetInfoAssuanTransaction( InfoItem item ) |
40 | : AssuanTransaction(), |
41 | m_item( item ), |
42 | m_command(), |
43 | m_data() |
44 | { |
45 | |
46 | } |
47 | |
48 | ScdGetInfoAssuanTransaction::~ScdGetInfoAssuanTransaction() {} |
49 | |
50 | static std::vector<std::string> to_reader_list( const std::string & s ) { |
51 | std::vector<std::string> result; |
52 | return split( result, s, is_any_of( "\n" ), token_compress_on ); |
53 | } |
54 | |
55 | static std::vector<std::string> to_app_list( const std::string & s ) { |
56 | return to_reader_list( s ); |
57 | } |
58 | |
59 | std::string ScdGetInfoAssuanTransaction::version() const { |
60 | if ( m_item == Version ) { |
61 | return m_data; |
62 | } else { |
63 | return std::string(); |
64 | } |
65 | } |
66 | |
67 | unsigned int ScdGetInfoAssuanTransaction::pid() const { |
68 | if ( m_item == Pid ) { |
69 | return to_pid( m_data ); |
70 | } else { |
71 | return 0U; |
72 | } |
73 | } |
74 | |
75 | std::string ScdGetInfoAssuanTransaction::socketName() const { |
76 | if ( m_item == SocketName ) { |
77 | return m_data; |
78 | } else { |
79 | return std::string(); |
80 | } |
81 | } |
82 | |
83 | char ScdGetInfoAssuanTransaction::status() const { |
84 | if ( m_item == Status && !m_data.empty() ) { |
85 | return m_data[0]; |
86 | } else { |
87 | return '\0'; |
88 | } |
89 | } |
90 | |
91 | std::vector<std::string> ScdGetInfoAssuanTransaction::readerList() const { |
92 | if ( m_item == ReaderList ) { |
93 | return to_reader_list( m_data ); |
94 | } else { |
95 | return std::vector<std::string>(); |
96 | } |
97 | } |
98 | |
99 | std::vector<std::string> ScdGetInfoAssuanTransaction::applicationList() const { |
100 | if ( m_item == ApplicationList ) { |
101 | return to_app_list( m_data ); |
102 | } else { |
103 | return std::vector<std::string>(); |
104 | } |
105 | } |
106 | |
107 | static const char * scd_getinfo_tokens[] = { |
108 | "version" , |
109 | "pid" , |
110 | "socket_name" , |
111 | "status" , |
112 | "reader_list" , |
113 | "deny_admin" , |
114 | "app_list" , |
115 | }; |
116 | BOOST_STATIC_ASSERT( ( sizeof scd_getinfo_tokens / sizeof *scd_getinfo_tokens == ScdGetInfoAssuanTransaction::LastInfoItem ) ); |
117 | |
118 | void ScdGetInfoAssuanTransaction::makeCommand() const { |
119 | assert( m_item >= 0 ); |
120 | assert( m_item < LastInfoItem ); |
121 | m_command = "SCD GETINFO " ; |
122 | m_command += scd_getinfo_tokens[m_item]; |
123 | } |
124 | |
125 | const char * ScdGetInfoAssuanTransaction::command() const { |
126 | makeCommand(); |
127 | return m_command.c_str(); |
128 | } |
129 | |
130 | Error ScdGetInfoAssuanTransaction::data( const char * data, size_t len ) { |
131 | m_data.append( data, len ); |
132 | return Error(); |
133 | } |
134 | |
135 | Data ScdGetInfoAssuanTransaction::inquire( const char * name, const char * args, Error & err ) { |
136 | (void)name; (void)args; (void)err; |
137 | return Data::null; |
138 | } |
139 | |
140 | Error ScdGetInfoAssuanTransaction::status( const char * status, const char * args ) { |
141 | (void)status; (void)args; |
142 | return Error(); |
143 | } |
144 | |