1 /*
2  * Copyright (C) 2018 Rafael Ostertag
3  *
4  * This file is part of YAPET.
5  *
6  * YAPET is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * YAPET is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * YAPET.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Additional permission under GNU GPL version 3 section 7
20  *
21  * If you modify this program, or any covered work, by linking or combining it
22  * with the OpenSSL project's OpenSSL library (or a modified version of that
23  * library), containing parts covered by the terms of the OpenSSL or SSLeay
24  * licenses, Rafael Ostertag grants you additional permission to convey the
25  * resulting work.  Corresponding Source for a non-source form of such a
26  * combination shall include the source code for the parts of OpenSSL used as
27  * well as that of the covered work.
28  */
29 
30 #ifndef _PASSWORDRECORD_H
31 #define _PASSWORDRECORD_H 1
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <yacurs.h>
38 #include <string>
39 
40 #include "file.h"
41 #include "pwgendialog.h"
42 
43 /**
44  * @brief A window that displays all the information associated with a
45  * decrypted password record.
46  *
47  * A window that displays all the information associated with a
48  * decrypted password record. The window allows edition of the
49  * informations. If the information are edited, a call to \c
50  * entryChanged() yields \c true if the record has been edited.
51  *
52  * To display an existing record, provide a valid pointer to \c
53  * PartDec object when constructing the object. If the record has been
54  * changed, \c getEncEntry() will return the pointer to the \c PartDec
55  * object holding the altered record.
56  *
57  * To display a window for creating a new password record, pass \c
58  * 0 to the \c PartDec pointer argument when constructing. The new
59  * record can be obtained by calling \c getEncEntry().
60  *
61  * In any case, the memory occupied by the pointer returned by \c
62  * getEncEntry() has to be freed by the caller. The class does not
63  * take care of this.
64  */
65 class PasswordRecord : public YACURS::Dialog {
66    private:
67     YACURS::VPack* vpack;
68     YACURS::Label* lname;
69     YACURS::Label* lhost;
70     YACURS::Label* lusername;
71     YACURS::Label* lpassword;
72     YACURS::Label* lcomment;
73     YACURS::Input<std::string>* name;
74     YACURS::Input<std::string>* host;
75     YACURS::Input<std::string>* username;
76     YACURS::Input<std::string>* password;
77     YACURS::Input<std::string>* comment;
78     YACURS::Button* pwgenbutton;
79     YACURS::Spacer* pwgenbutton_spacer;
80 
81     YACURS::MessageBox* errordialog;
82     YACURS::MessageBox* confirmdialog;
83     PwGenDialog* pwgendialog;
84 
85     std::shared_ptr<yapet::AbstractCryptoFactory> _cryptoFactory;
86     std::shared_ptr<yapet::PasswordListItem> _passwordListItem;
87     bool _newrecord;
88     bool _readonly;
89     bool _password_hidden;
90 
91     /**
92      * When @c true, dialog will be closed without asked for
93      * confirmation if cancel has been pressed.
94      */
95     bool _force_close;
96     bool _modified_by_pwgen;
97 
98     virtual void on_ok_button();
99 
100     void button_press_handler(YACURS::Event& e);
101 
102     void window_close_handler(YACURS::Event& e);
103 
104     virtual bool on_close();
105 
106    public:
107     /**
108      * @brief Constructor.
109      *
110      * Depending on the value passed in \c pe, either an empty
111      * record is showed or the decrypted password record including
112      * the password stored in the record in plain text is showed
113      * except the password record is displaying in read-only mode.
114      */
115     PasswordRecord(std::shared_ptr<yapet::AbstractCryptoFactory>& cryptoFactory,
116                    const yapet::PasswordListItem& passwordListItem);
117     PasswordRecord(
118         std::shared_ptr<yapet::AbstractCryptoFactory>& cryptoFactory);
119     ~PasswordRecord();
120 
121     PasswordRecord(const PasswordRecord&) = delete;
122     PasswordRecord(PasswordRecord&&) = delete;
123     PasswordRecord& operator=(const PasswordRecord&) = delete;
124     PasswordRecord& operator=(PasswordRecord&&) = delete;
125 
126     /**
127      * @brief Returns the password record.
128      *
129      * Returns the new or altered password record as \c PasswordListItem
130      * object. The caller is responsible for freeing the memory
131      * associated with the pointer returned.
132      *
133      * It returns \c 0 if the dialog has been canceled.
134      *
135      * @return pointer to the new or altered password record, or
136      * \c 0 if the dialog has been canceled. The caller is
137      * responsible for freeing the memory associated with the
138      * pointer returned.
139      */
getEncEntry()140     std::shared_ptr<yapet::PasswordListItem> getEncEntry() const {
141         return _passwordListItem;
142     }
143 
144     /**
145      * @brief Indicates whether or not the record has been
146      * changed.
147      *
148      * Indicates whether or not the record has been changed.
149      *
150      * @return \c true if the record has been changed, \c false
151      * otherwise.
152      */
153     bool changed() const;
154 
155     void readonly(bool f);
156 
readonly()157     bool readonly() const { return _readonly; }
158 
159     void password_hidden(bool f);
160 
password_hidden()161     bool password_hidden() const { return _password_hidden; }
162 
newrecord()163     bool newrecord() const { return _newrecord; }
164 };
165 
166 #endif  // _PASSWORDRECORD_H
167