dthelp: Change to ANSI function definitions
[oweals/cde.git] / cde / programs / dtmail / dtmail / EncryptedTextFieldUiItem.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $TOG: EncryptedTextFieldUiItem.C /main/2 1997/11/13 13:25:10 mgreess $ */
24 /*
25  *+SNOTICE
26  *
27  *      RESTRICTED CONFIDENTIAL INFORMATION:
28  *      
29  *      The information in this document is subject to special
30  *      restrictions in a confidential disclosure agreement bertween
31  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
32  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
33  *      Sun's specific written approval.  This documment and all copies
34  *      and derivative works thereof must be returned or destroyed at
35  *      Sun's request.
36  *
37  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
38  *
39  *+ENOTICE
40  */
41 #include <assert.h>
42 #include <sys/param.h>
43 #include <Xm/TextF.h>
44
45 #include <DtMail/options_util.h>
46 #include <DtMail/EncryptedTextFieldUiItem.hh>
47 #include <DtMail/PropUi.hh>
48 #include <DtMail/DtMailServer.hh>
49 #include <DtMail/DtMailTypes.h>
50
51 ////////////////////////////////////////////////////////////////////
52 EncryptedTextFieldUiItem::EncryptedTextFieldUiItem(
53                                         Widget          w,
54                                         int             source,
55                                         char            *search_key,
56                                         PropUiCallback  validator,
57                                         void            *validator_data)
58 : PropUiItem(w, source, search_key, validator, validator_data)
59 {
60     _loading = DTM_FALSE;
61     _maxtextlen = 256;
62     _text = (char*) malloc(_maxtextlen);
63     assert(NULL!=_text);
64     _writeAllowed = DTM_FALSE;
65
66     XtVaSetValues(
67                 w,
68                 XmNeditable, False,
69                 XmNvalue, "",
70                 NULL);
71     XtAddCallback(
72                 w,
73                 XmNmodifyVerifyCallback, EncryptedTextFieldUiItem::verifyCB,
74                 (XtPointer) this);
75
76     options_field_init(w, &(this->dirty_bit));
77 }
78
79 ///////////////////////////////////////////////////////////////////
80 void EncryptedTextFieldUiItem::writeAllowed(DtMailBoolean allowed)
81 {
82     Widget      w = getWidget();
83
84     if (_writeAllowed == allowed) return;
85
86     _writeAllowed = allowed;
87     if (DTM_TRUE == _writeAllowed)
88     {
89         char    *s;
90         char    *value = strdup(_text);
91
92         for (s=value; *s; s++) *s = '*';
93
94         _loading = DTM_TRUE;
95         XtVaSetValues(w, XmNeditable, True, XmNsensitive, True, NULL);
96         options_field_set_value(w, value, this->dirty_bit);
97         _loading = DTM_FALSE;
98         setDirtyBit(True);
99         free(value);
100     }
101     else
102     {
103         _loading = DTM_TRUE;
104         XtVaSetValues(w, XmNeditable, False, XmNsensitive, False, NULL);
105         options_field_set_value(w, "", this->dirty_bit);
106         _loading = DTM_FALSE;
107         setDirtyBit(True);
108     }
109 }
110
111
112 // Takes values in the UI and puts them into the source DB
113 ///////////////////////////////////////////////////////////////////
114 void EncryptedTextFieldUiItem::writeFromUiToSource()
115 {
116     if (DTM_TRUE == _writeAllowed)
117       prop_source->setValue(_text, DTM_TRUE);
118     else
119       prop_source->setValue(DTMAS_PROPDFLT_PASSWORD, DTM_TRUE);
120 }
121
122 // Takes values in the UI and puts them into the source DB
123 ///////////////////////////////////////////////////////////////////
124 void EncryptedTextFieldUiItem::writeFromSourceToUi()
125 {
126     int i;
127     char *s, *value;
128     Widget w = this->getWidget();
129
130     value = (char *)prop_source->getValue(DTM_TRUE);
131     if (! strcmp(value, PropSourceDEFAULTVALUE)) return;
132
133     validateLength(strlen(value));
134     strcpy(_text, value);
135
136     for (i=0, s=value; i<strlen(value); i++, s++)
137       *s = '*';
138
139     _loading = DTM_TRUE;
140     options_field_set_value(w, value, this->dirty_bit);
141     free((void*) value);
142     _loading = DTM_FALSE;
143 }
144
145 // Verifies that the _text array is sufficiently long for a string
146 // of the specified size.
147 ///////////////////////////////////////////////////////////////////
148 void EncryptedTextFieldUiItem::validateLength(int length)
149 {
150     length++;   // Account for the '\0'
151     if (length >= _maxtextlen)
152     {
153         _maxtextlen *= 2;
154         _text = (char*) realloc((void*) _text, (size_t) _maxtextlen);
155         assert(NULL!=_text);
156     }
157 }
158
159 ///////////////////////////////////////////////////////////////////
160 void EncryptedTextFieldUiItem::verify(XmTextVerifyPtr cbs)
161 {
162     int                 i;
163     static char         buffer[MAXPATHLEN];
164     register char       *s, *t;
165
166 #if defined(ENCRYPTED_TEXTFIELD_DEBUG)
167     printf(
168             "currInsert=%d newInsert=%d startPos=%d endPos=%d\n",
169             cbs->currInsert,cbs->newInsert,cbs->startPos, cbs->endPos);
170     if (cbs->text->ptr) printf("text->ptr=%s\n", cbs->text->ptr);
171     printf("_text->ptr=%s\n", _text);
172 #endif
173
174     for (i=0, s=buffer, t=_text; (*t && i<cbs->startPos); i++, s++, t++)
175       *s = *t;
176
177     if (cbs->text->ptr)
178     {
179         strcpy(s, cbs->text->ptr);
180         s += cbs->text->length;
181     }
182     else
183       *s = '\0';
184
185     if (strlen(_text) >= cbs->endPos)
186     {
187         t = _text+cbs->endPos;
188         if (strlen(t))
189           strcpy(s, t);
190     }
191
192     validateLength(strlen(buffer));
193     strcpy(_text, buffer);
194
195     if (cbs->text->ptr)
196       for (i=0, s=cbs->text->ptr; i<cbs->text->length; i++, s++)
197         *s = '*';
198
199 #if defined(ENCRYPTED_TEXTFIELD_DEBUG)
200     printf("text=%s\n", _text);
201 #endif
202 }
203
204 // ValueChangedCallback for the text field.  Saves the user input
205 // and echos *'s.
206 ///////////////////////////////////////////////////////////////////
207 void EncryptedTextFieldUiItem::verifyCB(
208                                         Widget          w,
209                                         XtPointer       client_data,
210                                         XtPointer       call_data)
211 {
212     EncryptedTextFieldUiItem    *etf = (EncryptedTextFieldUiItem*) client_data;
213     XmTextVerifyCallbackStruct  *cbs = (XmTextVerifyCallbackStruct*) call_data;
214
215     if (DTM_FALSE == etf->_loading) etf->verify(cbs);
216 }