1/*
2 gpgsetownertrusteditinteractor.cpp - Edit Interactor to change the expiry time of an OpenPGP key
3 Copyright (C) 2007 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 "gpgsetownertrusteditinteractor.h"
24#include "error.h"
25
26#include <gpgme.h>
27
28#include <cstring>
29
30using std::strcmp;
31
32// avoid conflict (msvc)
33#ifdef ERROR
34# undef ERROR
35#endif
36
37using namespace GpgME;
38
39GpgSetOwnerTrustEditInteractor::GpgSetOwnerTrustEditInteractor( Key::OwnerTrust ot )
40 : EditInteractor(),
41 m_ownertrust( ot )
42{
43
44}
45
46GpgSetOwnerTrustEditInteractor::~GpgSetOwnerTrustEditInteractor() {}
47
48// work around --enable-final
49namespace GpgSetOwnerTrustEditInteractor_Private {
50enum {
51 START = EditInteractor::StartState,
52 COMMAND,
53 VALUE,
54 REALLY_ULTIMATE,
55 QUIT,
56 SAVE,
57
58 ERROR = EditInteractor::ErrorState
59};
60}
61
62const char * GpgSetOwnerTrustEditInteractor::action( Error & err ) const {
63 static const char truststrings[][2] = { "1", "1", "2", "3", "4", "5" };
64
65 using namespace GpgSetOwnerTrustEditInteractor_Private;
66
67 switch ( state() ) {
68 case COMMAND:
69 return "trust";
70 case VALUE:
71 return truststrings[m_ownertrust];
72 case REALLY_ULTIMATE:
73 return "Y";
74 case QUIT:
75 return "quit";
76 case SAVE:
77 return "Y";
78 case START:
79 case ERROR:
80 return 0;
81 default:
82 err = Error::fromCode( GPG_ERR_GENERAL );
83 return 0;
84 }
85}
86
87unsigned int GpgSetOwnerTrustEditInteractor::nextState( unsigned int status, const char * args, Error & err ) const {
88
89 static const Error GENERAL_ERROR = Error::fromCode( GPG_ERR_GENERAL );
90 //static const Error INV_TIME_ERROR = Error::fromCode( GPG_ERR_INV_TIME );
91
92 if ( needsNoResponse( status ) ) {
93 return state();
94 }
95
96 using namespace GpgSetOwnerTrustEditInteractor_Private;
97
98 switch ( state() ) {
99 case START:
100 if ( status == GPGME_STATUS_GET_LINE &&
101 strcmp( args, "keyedit.prompt" ) == 0 ) {
102 return COMMAND;
103 }
104 err = GENERAL_ERROR;
105 return ERROR;
106 case COMMAND:
107 if ( status == GPGME_STATUS_GET_LINE &&
108 strcmp( args, "edit_ownertrust.value" ) == 0 ) {
109 return VALUE;
110 }
111 err = GENERAL_ERROR;
112 return ERROR;
113 case VALUE:
114 if ( status == GPGME_STATUS_GET_LINE &&
115 strcmp( args, "keyedit.prompt" ) == 0 ) {
116 return QUIT;
117 } else if ( status == GPGME_STATUS_GET_BOOL &&
118 strcmp( args, "edit_ownertrust.set_ultimate.okay" ) == 0 ) {
119 return REALLY_ULTIMATE;
120 }
121 err = GENERAL_ERROR;
122 return ERROR;
123 case REALLY_ULTIMATE:
124 if ( status == GPGME_STATUS_GET_LINE &&
125 strcmp( args, "keyedit.prompt" ) == 0 ) {
126 return QUIT;
127 }
128 err = GENERAL_ERROR;
129 return ERROR;
130 case QUIT:
131 if ( status == GPGME_STATUS_GET_BOOL &&
132 strcmp( args, "keyedit.save.okay" ) == 0 ) {
133 return SAVE;
134 }
135 err = GENERAL_ERROR;
136 return ERROR;
137 case ERROR:
138 if ( status == GPGME_STATUS_GET_LINE &&
139 strcmp( args, "keyedit.prompt" ) == 0 ) {
140 return QUIT;
141 }
142 err = lastError();
143 return ERROR;
144 default:
145 err = GENERAL_ERROR;
146 return ERROR;
147 };
148}
149