Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtmail / dtmail / EncryptedTextFieldUiItem.C
1 /* $TOG: EncryptedTextFieldUiItem.C /main/2 1997/11/13 13:25:10 mgreess $ */
2 /*
3  *+SNOTICE
4  *
5  *      RESTRICTED CONFIDENTIAL INFORMATION:
6  *      
7  *      The information in this document is subject to special
8  *      restrictions in a confidential disclosure agreement bertween
9  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
10  *      document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
11  *      Sun's specific written approval.  This documment and all copies
12  *      and derivative works thereof must be returned or destroyed at
13  *      Sun's request.
14  *
15  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
16  *
17  *+ENOTICE
18  */
19 #include <assert.h>
20 #include <sys/param.h>
21 #include <Xm/TextF.h>
22
23 #include <DtMail/options_util.h>
24 #include <DtMail/EncryptedTextFieldUiItem.hh>
25 #include <DtMail/PropUi.hh>
26 #include <DtMail/DtMailServer.hh>
27 #include <DtMail/DtMailTypes.h>
28
29 ////////////////////////////////////////////////////////////////////
30 EncryptedTextFieldUiItem::EncryptedTextFieldUiItem(
31                                         Widget          w,
32                                         int             source,
33                                         char            *search_key,
34                                         PropUiCallback  validator,
35                                         void            *validator_data)
36 : PropUiItem(w, source, search_key, validator, validator_data)
37 {
38     _loading = DTM_FALSE;
39     _maxtextlen = 256;
40     _text = (char*) malloc(_maxtextlen);
41     assert(NULL!=_text);
42     _writeAllowed = DTM_FALSE;
43
44     XtVaSetValues(
45                 w,
46                 XmNeditable, False,
47                 XmNvalue, "",
48                 NULL);
49     XtAddCallback(
50                 w,
51                 XmNmodifyVerifyCallback, EncryptedTextFieldUiItem::verifyCB,
52                 (XtPointer) this);
53
54     options_field_init(w, &(this->dirty_bit));
55 }
56
57 ///////////////////////////////////////////////////////////////////
58 void EncryptedTextFieldUiItem::writeAllowed(DtMailBoolean allowed)
59 {
60     Widget      w = getWidget();
61
62     if (_writeAllowed == allowed) return;
63
64     _writeAllowed = allowed;
65     if (DTM_TRUE == _writeAllowed)
66     {
67         char    *s;
68         char    *value = strdup(_text);
69
70         for (s=value; *s; s++) *s = '*';
71
72         _loading = DTM_TRUE;
73         XtVaSetValues(w, XmNeditable, True, XmNsensitive, True, NULL);
74         options_field_set_value(w, value, this->dirty_bit);
75         _loading = DTM_FALSE;
76         setDirtyBit(True);
77         free(value);
78     }
79     else
80     {
81         _loading = DTM_TRUE;
82         XtVaSetValues(w, XmNeditable, False, XmNsensitive, False, NULL);
83         options_field_set_value(w, "", this->dirty_bit);
84         _loading = DTM_FALSE;
85         setDirtyBit(True);
86     }
87 }
88
89
90 // Takes values in the UI and puts them into the source DB
91 ///////////////////////////////////////////////////////////////////
92 void EncryptedTextFieldUiItem::writeFromUiToSource()
93 {
94     if (DTM_TRUE == _writeAllowed)
95       prop_source->setValue(_text, DTM_TRUE);
96     else
97       prop_source->setValue(DTMAS_PROPDFLT_PASSWORD, DTM_TRUE);
98 }
99
100 // Takes values in the UI and puts them into the source DB
101 ///////////////////////////////////////////////////////////////////
102 void EncryptedTextFieldUiItem::writeFromSourceToUi()
103 {
104     int i;
105     char *s, *value;
106     Widget w = this->getWidget();
107
108     value = (char *)prop_source->getValue(DTM_TRUE);
109     if (! strcmp(value, PropSourceDEFAULTVALUE)) return;
110
111     validateLength(strlen(value));
112     strcpy(_text, value);
113
114     for (i=0, s=value; i<strlen(value); i++, s++)
115       *s = '*';
116
117     _loading = DTM_TRUE;
118     options_field_set_value(w, value, this->dirty_bit);
119     if (NULL != value) free((void*) value);
120     _loading = DTM_FALSE;
121 }
122
123 // Verifies that the _text array is sufficiently long for a string
124 // of the specified size.
125 ///////////////////////////////////////////////////////////////////
126 void EncryptedTextFieldUiItem::validateLength(int length)
127 {
128     length++;   // Account for the '\0'
129     if (length >= _maxtextlen)
130     {
131         _maxtextlen *= 2;
132         _text = (char*) realloc((void*) _text, (size_t) _maxtextlen);
133         assert(NULL!=_text);
134     }
135 }
136
137 ///////////////////////////////////////////////////////////////////
138 void EncryptedTextFieldUiItem::verify(XmTextVerifyPtr cbs)
139 {
140     int                 i;
141     static char         buffer[MAXPATHLEN];
142     register char       *s, *t;
143
144 #if defined(ENCRYPTED_TEXTFIELD_DEBUG)
145     printf(
146             "currInsert=%d newInsert=%d startPos=%d endPos=%d\n",
147             cbs->currInsert,cbs->newInsert,cbs->startPos, cbs->endPos);
148     if (cbs->text->ptr) printf("text->ptr=%s\n", cbs->text->ptr);
149     printf("_text->ptr=%s\n", _text);
150 #endif
151
152     for (i=0, s=buffer, t=_text; (*t && i<cbs->startPos); i++, s++, t++)
153       *s = *t;
154
155     if (cbs->text->ptr)
156     {
157         strcpy(s, cbs->text->ptr);
158         s += cbs->text->length;
159     }
160     else
161       *s = '\0';
162
163     if (strlen(_text) >= cbs->endPos)
164     {
165         t = _text+cbs->endPos;
166         if (strlen(t))
167           strcpy(s, t);
168     }
169
170     validateLength(strlen(buffer));
171     strcpy(_text, buffer);
172
173     if (cbs->text->ptr)
174       for (i=0, s=cbs->text->ptr; i<cbs->text->length; i++, s++)
175         *s = '*';
176
177 #if defined(ENCRYPTED_TEXTFIELD_DEBUG)
178     printf("text=%s\n", _text);
179 #endif
180 }
181
182 // ValueChangedCallback for the text field.  Saves the user input
183 // and echos *'s.
184 ///////////////////////////////////////////////////////////////////
185 void EncryptedTextFieldUiItem::verifyCB(
186                                         Widget          w,
187                                         XtPointer       client_data,
188                                         XtPointer       call_data)
189 {
190     EncryptedTextFieldUiItem    *etf = (EncryptedTextFieldUiItem*) client_data;
191     XmTextVerifyCallbackStruct  *cbs = (XmTextVerifyCallbackStruct*) call_data;
192
193     if (DTM_FALSE == etf->_loading) etf->verify(cbs);
194 }