Added a parameter to semaphore constructor to avoid ambiguity
[pwlib.git] / include / ptclib / httpform.h
blob476472614ffad9028c2aca17887e0447858aae93
1 /*
2 * httpform.h
4 * Forms management using HTTP User Interface.
6 * Portable Windows Library
8 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * $Log$
27 * Revision 1.17 2003/03/24 04:30:41 robertj
28 * Added function to set and get strings from PConfig in correct format for
29 * use with HTTP form array contsructs.
31 * Revision 1.16 2002/11/06 22:47:24 robertj
32 * Fixed header comment (copyright etc)
34 * Revision 1.15 2002/09/16 01:08:59 robertj
35 * Added #define so can select if #pragma interface/implementation is used on
36 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
38 * Revision 1.14 2001/10/10 08:07:48 robertj
39 * Fixed large memory leak of strings when doing POST to a form.
41 * Revision 1.13 2000/12/18 07:12:37 robertj
42 * Added ability to have fixed length array fields.
44 * Revision 1.12 1999/03/09 08:01:46 robertj
45 * Changed comments for doc++ support (more to come).
47 * Revision 1.11 1999/02/16 08:07:10 robertj
48 * MSVC 6.0 compatibility changes.
50 * Revision 1.10 1998/11/30 02:50:48 robertj
51 * New directory structure
53 * Revision 1.9 1998/09/23 06:19:31 robertj
54 * Added open source copyright license.
56 * Revision 1.8 1998/08/20 05:45:33 robertj
57 * Fixed bug where substitutions did not always occur if near end of macro block.
59 * Revision 1.7 1998/01/26 00:25:24 robertj
60 * Major rewrite of HTTP forms management.
62 * Revision 1.6 1997/08/09 07:46:51 robertj
63 * Fixed problems with value of SELECT fields in form
65 * Revision 1.5 1997/07/26 11:38:17 robertj
66 * Support for overridable pages in HTTP service applications.
68 * Revision 1.4 1997/07/08 13:16:12 robertj
69 * Major HTTP form enhancements for lists and arrays of fields.
71 * Revision 1.3 1997/06/08 04:49:40 robertj
72 * Adding new llist based form field.
74 * Revision 1.2 1997/04/01 06:01:39 robertj
75 * Allowed value list in drop down box to be modified once created.
77 * Revision 1.1 1996/06/28 12:55:56 robertj
78 * Initial revision
82 #ifndef _PHTTPFORM
83 #define _PHTTPFORM
85 #ifdef P_USE_PRAGMA
86 #pragma interface
87 #endif
89 #include <ptclib/http.h>
92 ///////////////////////////////////////////////////////////////////////////////
93 // PHTTPField
95 /** This class is the abstract base class for fields in a #PHTTPForm#
96 resource type.
98 class PHTTPField : public PObject
100 PCLASSINFO(PHTTPField, PObject)
101 public:
102 PHTTPField(
103 const char * bname, // base name (identifier) for the field.
104 const char * title, // Title text for field (defaults to name).
105 const char * help // Help text for the field.
107 // Create a new field in a HTTP form.
109 /** Compare the fields using the field names.
111 @return
112 Comparison of the name fields of the two fields.
114 virtual Comparison Compare(
115 const PObject & obj
116 ) const;
118 /** Get the identifier name of the field.
120 @return
121 String for field name.
123 const PCaselessString & GetName() const { return fullName; }
125 /** Get the identifier name of the field.
127 @return
128 String for field name.
130 const PCaselessString & GetBaseName() const { return baseName; }
132 /** Set the name for the field.
134 virtual void SetName(
135 const PString & newName // New name for field
138 /** Locate the field naem, recusing down for composite fields.
140 @return
141 Pointer to located field, or NULL if not found.
143 virtual const PHTTPField * LocateName(
144 const PString & name // Full field name to locate
145 ) const;
147 /** Get the title of the field.
149 @return
150 String for title placed next to the field.
152 const PString & GetTitle() const { return title; }
154 /** Get the title of the field.
156 @return
157 String for title placed next to the field.
159 const PString & GetHelp() const { return help; }
161 void SetHelp(
162 const PString & text // Help text.
163 ) { help = text; }
164 void SetHelp(
165 const PString & hotLinkURL, // URL for link to help page.
166 const PString & linkText // Help text in the link.
168 void SetHelp(
169 const PString & hotLinkURL, // URL for link to help page.
170 const PString & imageURL, // URL for image to be displayed in link.
171 const PString & imageText // Text in the link when image unavailable.
173 // Set the help text for the field.
175 /** Create a new field of the same class as the current field.
177 @return
178 New field object instance.
180 virtual PHTTPField * NewField() const = 0;
182 virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
183 // Splice expanded macro substitutions into text string
185 /** Convert the field to HTML form tag for inclusion into the HTTP page.
187 virtual void GetHTMLTag(
188 PHTML & html // HTML to receive the fields HTML tag.
189 ) const = 0;
191 /** Convert the field input to HTML for inclusion into the HTTP page.
193 virtual PString GetHTMLInput(
194 const PString & input // Source HTML text for input tag.
195 ) const;
197 /** Convert the field input to HTML for inclusion into the HTTP page.
199 virtual PString GetHTMLSelect(
200 const PString & selection // Source HTML text for input tag.
201 ) const;
203 /** Convert the field to HTML for inclusion into the HTTP page.
205 virtual void GetHTMLHeading(
206 PHTML & html // HTML to receive the field info.
207 ) const;
209 /** Get the string value of the field.
211 @return
212 String for field value.
214 virtual PString GetValue(BOOL dflt = FALSE) const = 0;
216 /** Set the value of the field.
218 virtual void SetValue(
219 const PString & newValue // New value for the field.
220 ) = 0;
222 /** Get the value of the PConfig to the sub-field. If the field is not
223 composite then it always sets the value as for the non-indexed version.
225 virtual void LoadFromConfig(
226 PConfig & cfg // Configuration for value transfer.
229 /** Set the value of the sub-field into the PConfig. If the field is not
230 composite then it always sets the value as for the non-indexed version.
232 virtual void SaveToConfig(
233 PConfig & cfg // Configuration for value transfer.
234 ) const;
236 /** Validate the new field value before #SetValue()# is called.
238 @return
239 BOOL if the new field value is OK.
241 virtual BOOL Validated(
242 const PString & newVal, // Proposed new value for the field.
243 PStringStream & msg // Stream to take error HTML if value not valid.
244 ) const;
247 /** Retrieve all the names in the field and subfields.
249 @return
250 List of strings for each subfield.
252 virtual void GetAllNames(PStringList & list) const;
254 /** Set the value of the field in a list of fields.
256 virtual void SetAllValues(
257 const PStringToString & data // New value for the field.
260 /** Validate the new field value in a list before #SetValue()# is called.
262 @return
263 BOOL if the all the new field values are OK.
265 virtual BOOL ValidateAll(
266 const PStringToString & data, // Proposed new value for the field.
267 PStringStream & msg // Stream to take error HTML if value not valid.
268 ) const;
271 BOOL NotYetInHTML() const { return notInHTML; }
272 void SetInHTML() { notInHTML = FALSE; }
274 protected:
275 PCaselessString baseName;
276 PCaselessString fullName;
277 PString title;
278 PString help;
279 BOOL notInHTML;
283 PLIST(PHTTPFieldList, PHTTPField);
285 class PHTTPCompositeField : public PHTTPField
287 PCLASSINFO(PHTTPCompositeField, PHTTPField)
288 public:
289 PHTTPCompositeField(
290 const char * name, // Name (identifier) for the field.
291 const char * title = NULL, // Title text for field (defaults to name).
292 const char * help = NULL // Help text for the field.
295 virtual void SetName(
296 const PString & name // New name for field
299 virtual const PHTTPField * LocateName(
300 const PString & name // Full field name to locate
301 ) const;
303 virtual PHTTPField * NewField() const;
305 virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
307 virtual void GetHTMLTag(
308 PHTML & html // HTML to receive the field info.
309 ) const;
311 virtual PString GetHTMLInput(
312 const PString & input // Source HTML text for input tag.
313 ) const;
315 virtual void GetHTMLHeading(
316 PHTML & html // HTML to receive the field info.
317 ) const;
319 virtual PString GetValue(BOOL dflt = FALSE) const;
321 virtual void SetValue(
322 const PString & newValue // New value for the field.
325 virtual void LoadFromConfig(
326 PConfig & cfg // Configuration for value transfer.
328 virtual void SaveToConfig(
329 PConfig & cfg // Configuration for value transfer.
330 ) const;
332 virtual void GetAllNames(PStringList & list) const;
333 virtual void SetAllValues(
334 const PStringToString & data // New value for the field.
337 virtual BOOL ValidateAll(
338 const PStringToString & data, // Proposed new value for the field.
339 PStringStream & msg // Stream to take error HTML if value not valid.
340 ) const;
343 /** Get the number of sub-fields in the composite field. Note that this is
344 the total including any composite sub-fields, ie, it is the size of the
345 whole tree of primitive fields.
347 @return
348 Returns field count.
350 virtual PINDEX GetSize() const;
352 void Append(PHTTPField * fld);
353 PHTTPField & operator[](PINDEX idx) const { return fields[idx]; }
354 void RemoveAt(PINDEX idx) { fields.RemoveAt(idx); }
355 void RemoveAll() { fields.RemoveAll(); }
357 protected:
358 PHTTPFieldList fields;
362 class PHTTPSubForm : public PHTTPCompositeField
364 PCLASSINFO(PHTTPSubForm, PHTTPCompositeField)
365 public:
366 PHTTPSubForm(
367 const PString & subFormName, // URL for the sub-form
368 const char * name, // Name (identifier) for the field.
369 const char * title = NULL, // Title text for field (defaults to name).
370 PINDEX primaryField = 0, // Pimary field whove value is in hot link
371 PINDEX secondaryField = P_MAX_INDEX // Seconary field next to hotlink
374 PHTTPField * NewField() const;
375 void GetHTMLTag(PHTML & html) const;
376 void GetHTMLHeading(PHTML & html) const;
378 protected:
379 PString subFormName;
380 PINDEX primary;
381 PINDEX secondary;
385 class PHTTPFieldArray : public PHTTPCompositeField
387 PCLASSINFO(PHTTPFieldArray, PHTTPCompositeField)
388 public:
389 PHTTPFieldArray(
390 PHTTPField * baseField,
391 BOOL ordered,
392 PINDEX fixedSize = 0
395 ~PHTTPFieldArray();
398 virtual PHTTPField * NewField() const;
400 virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
402 virtual void GetHTMLTag(
403 PHTML & html // HTML to receive the field info.
404 ) const;
406 virtual void LoadFromConfig(
407 PConfig & cfg // Configuration for value transfer.
409 virtual void SaveToConfig(
410 PConfig & cfg // Configuration for value transfer.
411 ) const;
414 virtual void SetAllValues(
415 const PStringToString & data // New value for the field.
418 virtual PINDEX GetSize() const;
419 void SetSize(PINDEX newSize);
421 PStringArray GetStrings(
422 PConfig & cfg /// Config file to get strings from
425 void SetStrings(
426 PConfig & cfg, /// Config file to Set strings to
427 const PStringArray & values /// Strings to set
430 protected:
431 void AddBlankField();
432 void AddArrayControlBox(PHTML & html, PINDEX fld) const;
433 void SetArrayFieldName(PINDEX idx) const;
435 PHTTPField * baseField;
436 BOOL orderedArray;
437 BOOL canAddElements;
441 class PHTTPStringField : public PHTTPField
443 PCLASSINFO(PHTTPStringField, PHTTPField)
444 public:
445 PHTTPStringField(
446 const char * name,
447 PINDEX size,
448 const char * initVal = NULL,
449 const char * help = NULL
451 PHTTPStringField(
452 const char * name,
453 const char * title,
454 PINDEX size,
455 const char * initVal = NULL,
456 const char * help = NULL
459 virtual PHTTPField * NewField() const;
461 virtual void GetHTMLTag(
462 PHTML & html // HTML to receive the field info.
463 ) const;
465 virtual PString GetValue(BOOL dflt = FALSE) const;
467 virtual void SetValue(
468 const PString & newVal
472 protected:
473 PString value;
474 PString initialValue;
475 PINDEX size;
479 class PHTTPPasswordField : public PHTTPStringField
481 PCLASSINFO(PHTTPPasswordField, PHTTPStringField)
482 public:
483 PHTTPPasswordField(
484 const char * name,
485 PINDEX size,
486 const char * initVal = NULL,
487 const char * help = NULL
489 PHTTPPasswordField(
490 const char * name,
491 const char * title,
492 PINDEX size,
493 const char * initVal = NULL,
494 const char * help = NULL
497 virtual PHTTPField * NewField() const;
499 virtual void GetHTMLTag(
500 PHTML & html // HTML to receive the field info.
501 ) const;
503 virtual PString GetValue(BOOL dflt = FALSE) const;
505 virtual void SetValue(
506 const PString & newVal
509 static PString Decrypt(const PString & pword);
513 class PHTTPIntegerField : public PHTTPField
515 PCLASSINFO(PHTTPIntegerField, PHTTPField)
516 public:
517 PHTTPIntegerField(
518 const char * name,
519 int low, int high,
520 int initVal = 0,
521 const char * units = NULL,
522 const char * help = NULL
524 PHTTPIntegerField(
525 const char * name,
526 const char * title,
527 int low, int high,
528 int initVal = 0,
529 const char * units = NULL,
530 const char * help = NULL
533 virtual PHTTPField * NewField() const;
535 virtual void GetHTMLTag(
536 PHTML & html // HTML to receive the field info.
537 ) const;
539 virtual PString GetValue(BOOL dflt = FALSE) const;
541 virtual void SetValue(
542 const PString & newVal
545 virtual void LoadFromConfig(
546 PConfig & cfg // Configuration for value transfer.
548 virtual void SaveToConfig(
549 PConfig & cfg // Configuration for value transfer.
550 ) const;
552 virtual BOOL Validated(
553 const PString & newVal,
554 PStringStream & msg
555 ) const;
558 protected:
559 int low, high, value;
560 int initialValue;
561 PString units;
565 class PHTTPBooleanField : public PHTTPField
567 PCLASSINFO(PHTTPBooleanField, PHTTPField)
568 public:
569 PHTTPBooleanField(
570 const char * name,
571 BOOL initVal = FALSE,
572 const char * help = NULL
574 PHTTPBooleanField(
575 const char * name,
576 const char * title,
577 BOOL initVal = FALSE,
578 const char * help = NULL
581 virtual PHTTPField * NewField() const;
583 virtual void GetHTMLTag(
584 PHTML & html // HTML to receive the field info.
585 ) const;
587 virtual PString GetHTMLInput(
588 const PString & input
589 ) const;
591 virtual PString GetValue(BOOL dflt = FALSE) const;
593 virtual void SetValue(
594 const PString & newVal
597 virtual void LoadFromConfig(
598 PConfig & cfg // Configuration for value transfer.
600 virtual void SaveToConfig(
601 PConfig & cfg // Configuration for value transfer.
602 ) const;
605 protected:
606 BOOL value, initialValue;
610 class PHTTPRadioField : public PHTTPField
612 PCLASSINFO(PHTTPRadioField, PHTTPField)
613 public:
614 PHTTPRadioField(
615 const char * name,
616 const PStringArray & valueArray,
617 PINDEX initVal = 0,
618 const char * help = NULL
620 PHTTPRadioField(
621 const char * name,
622 const PStringArray & valueArray,
623 const PStringArray & titleArray,
624 PINDEX initVal = 0,
625 const char * help = NULL
627 PHTTPRadioField(
628 const char * name,
629 PINDEX count,
630 const char * const * valueStrings,
631 PINDEX initVal = 0,
632 const char * help = NULL
634 PHTTPRadioField(
635 const char * name,
636 PINDEX count,
637 const char * const * valueStrings,
638 const char * const * titleStrings,
639 PINDEX initVal = 0,
640 const char * help = NULL
642 PHTTPRadioField(
643 const char * name,
644 const char * groupTitle,
645 const PStringArray & valueArray,
646 PINDEX initVal = 0,
647 const char * help = NULL
649 PHTTPRadioField(
650 const char * name,
651 const char * groupTitle,
652 const PStringArray & valueArray,
653 const PStringArray & titleArray,
654 PINDEX initVal = 0,
655 const char * help = NULL
657 PHTTPRadioField(
658 const char * name,
659 const char * groupTitle,
660 PINDEX count,
661 const char * const * valueStrings,
662 PINDEX initVal = 0,
663 const char * help = NULL
665 PHTTPRadioField(
666 const char * name,
667 const char * groupTitle,
668 PINDEX count,
669 const char * const * valueStrings,
670 const char * const * titleStrings,
671 PINDEX initVal = 0,
672 const char * help = NULL
675 virtual PHTTPField * NewField() const;
677 virtual void GetHTMLTag(
678 PHTML & html // HTML to receive the field info.
679 ) const;
681 virtual PString GetHTMLInput(
682 const PString & input
683 ) const;
685 virtual PString GetValue(BOOL dflt = FALSE) const;
687 virtual void SetValue(
688 const PString & newVal
692 protected:
693 PStringArray values;
694 PStringArray titles;
695 PString value;
696 PString initialValue;
700 class PHTTPSelectField : public PHTTPField
702 PCLASSINFO(PHTTPSelectField, PHTTPField)
703 public:
704 PHTTPSelectField(
705 const char * name,
706 const PStringArray & valueArray,
707 PINDEX initVal = 0,
708 const char * help = NULL
710 PHTTPSelectField(
711 const char * name,
712 PINDEX count,
713 const char * const * valueStrings,
714 PINDEX initVal = 0,
715 const char * help = NULL
717 PHTTPSelectField(
718 const char * name,
719 const char * title,
720 const PStringArray & valueArray,
721 PINDEX initVal = 0,
722 const char * help = NULL
724 PHTTPSelectField(
725 const char * name,
726 const char * title,
727 PINDEX count,
728 const char * const * valueStrings,
729 PINDEX initVal = 0,
730 const char * help = NULL
733 virtual PHTTPField * NewField() const;
735 virtual void GetHTMLTag(
736 PHTML & html // HTML to receive the field info.
737 ) const;
739 virtual PString GetValue(BOOL dflt = FALSE) const;
741 virtual void SetValue(
742 const PString & newVal
746 PStringArray values;
749 protected:
750 PString value;
751 PINDEX initialValue;
755 ///////////////////////////////////////////////////////////////////////////////
756 // PHTTPForm
758 class PHTTPForm : public PHTTPString
760 PCLASSINFO(PHTTPForm, PHTTPString)
761 public:
762 PHTTPForm(
763 const PURL & url
765 PHTTPForm(
766 const PURL & url,
767 const PHTTPAuthority & auth
769 PHTTPForm(
770 const PURL & url,
771 const PString & html
773 PHTTPForm(
774 const PURL & url,
775 const PString & html,
776 const PHTTPAuthority & auth
780 virtual void OnLoadedText(
781 PHTTPRequest & request, // Information on this request.
782 PString & text // Data used in reply.
784 virtual BOOL Post(
785 PHTTPRequest & request, // Information on this request.
786 const PStringToString & data, // Variables in the POST data.
787 PHTML & replyMessage // Reply message for post.
791 PHTTPField * Add(
792 PHTTPField * fld
794 void RemoveAllFields()
795 { fields.RemoveAll(); fieldNames.RemoveAll(); }
797 enum BuildOptions {
798 CompleteHTML,
799 InsertIntoForm,
800 InsertIntoHTML
803 void BuildHTML(
804 const char * heading
806 void BuildHTML(
807 const PString & heading
809 void BuildHTML(
810 PHTML & html,
811 BuildOptions option = CompleteHTML
815 protected:
816 PHTTPCompositeField fields;
817 PStringSet fieldNames;
821 //////////////////////////////////////////////////////////////////////////////
822 // PHTTPConfig
824 class PHTTPConfig : public PHTTPForm
826 PCLASSINFO(PHTTPConfig, PHTTPForm)
827 public:
828 PHTTPConfig(
829 const PURL & url,
830 const PString & section
832 PHTTPConfig(
833 const PURL & url,
834 const PString & section,
835 const PHTTPAuthority & auth
837 PHTTPConfig(
838 const PURL & url,
839 const PString & section,
840 const PString & html
842 PHTTPConfig(
843 const PURL & url,
844 const PString & section,
845 const PString & html,
846 const PHTTPAuthority & auth
849 virtual void OnLoadedText(
850 PHTTPRequest & request, // Information on this request.
851 PString & text // Data used in reply.
853 virtual BOOL Post(
854 PHTTPRequest & request, // Information on this request.
855 const PStringToString & data, // Variables in the POST data.
856 PHTML & replyMessage // Reply message for post.
860 /** Load all of the values for the resource from the configuration.
862 void LoadFromConfig();
864 /** Get the configuration file section that the page will alter.
866 @return
867 String for config file section.
869 const PString & GetConfigSection() const { return section; }
871 void SetConfigSection(
872 const PString & sect // New section for the config page.
873 ) { section = sect; }
874 // Set the configuration file section.
876 /** Add a field that will determine the name opf the secontion into which
877 the other fields are to be added as keys. The section is not created and
878 and error generated if the section already exists.
880 PHTTPField * AddSectionField(
881 PHTTPField * sectionFld, // Field to set as the section name
882 const char * prefix = NULL, // String to attach before the field value
883 const char * suffix = NULL // String to attach after the field value
886 /** Add fields to the HTTP form for adding a new key to the config file
887 section.
889 void AddNewKeyFields(
890 PHTTPField * keyFld, // Field for the key to be added.
891 PHTTPField * valFld // Field for the value of the key yto be added.
895 protected:
896 PString section;
897 PString sectionPrefix;
898 PString sectionSuffix;
899 PHTTPField * sectionField;
900 PHTTPField * keyField;
901 PHTTPField * valField;
903 private:
904 void Construct();
908 //////////////////////////////////////////////////////////////////////////////
909 // PHTTPConfigSectionList
911 class PHTTPConfigSectionList : public PHTTPString
913 PCLASSINFO(PHTTPConfigSectionList, PHTTPString)
914 public:
915 PHTTPConfigSectionList(
916 const PURL & url,
917 const PHTTPAuthority & auth,
918 const PString & sectionPrefix,
919 const PString & additionalValueName,
920 const PURL & editSection,
921 const PURL & newSection,
922 const PString & newSectionTitle,
923 PHTML & heading
926 virtual void OnLoadedText(
927 PHTTPRequest & request, // Information on this request.
928 PString & text // Data used in reply.
930 virtual BOOL Post(
931 PHTTPRequest & request, // Information on this request.
932 const PStringToString & data, // Variables in the POST data.
933 PHTML & replyMessage // Reply message for post.
936 protected:
937 PString sectionPrefix;
938 PString additionalValueName;
939 PString newSectionLink;
940 PString newSectionTitle;
941 PString editSectionLink;
945 #endif
948 // End Of File ///////////////////////////////////////////////////////////////