PTLib  Version 2.12.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
httpform.h
Go to the documentation of this file.
1 /*
2  * httpform.h
3  *
4  * Forms management using HTTP User Interface.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
9  *
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/
14  *
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.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  *
26  * $Revision: 27824 $
27  * $Author: rjongbloed $
28  * $Date: 2012-06-14 08:58:20 +1000 (Thu, 14 Jun 2012) $
29  */
30 
31 #ifndef PTLIB_HTTPFORM_H
32 #define PTLIB_HTTPFORM_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #if P_HTTPFORMS
39 
40 #include <ptclib/http.h>
41 #include <ptclib/html.h>
42 
43 
45 // PHTTPField
46 
50 class PHTTPField : public PObject
51 {
53  public:
54  PHTTPField(
55  const char * bname, // base name (identifier) for the field.
56  const char * title, // Title text for field (defaults to name).
57  const char * help // Help text for the field.
58  );
59  // Create a new field in a HTTP form.
60 
66  virtual Comparison Compare(
67  const PObject & obj
68  ) const;
69 
75  const PCaselessString & GetName() const { return fullName; }
76 
82  const PCaselessString & GetBaseName() const { return baseName; }
83 
86  virtual void SetName(
87  const PString & newName // New name for field
88  );
89 
95  virtual const PHTTPField * LocateName(
96  const PString & name // Full field name to locate
97  ) const;
98 
104  const PString & GetTitle() const { return title; }
105 
111  const PString & GetHelp() const { return help; }
112 
113  void SetHelp(
114  const PString & text // Help text.
115  ) { help = text; }
116  void SetHelp(
117  const PString & hotLinkURL, // URL for link to help page.
118  const PString & linkText // Help text in the link.
119  );
120  void SetHelp(
121  const PString & hotLinkURL, // URL for link to help page.
122  const PString & imageURL, // URL for image to be displayed in link.
123  const PString & imageText // Text in the link when image unavailable.
124  );
125  // Set the help text for the field.
126 
132  virtual PHTTPField * NewField() const = 0;
133 
134  virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
135  // Splice expanded macro substitutions into text string
136 
139  virtual void GetHTMLTag(
140  PHTML & html // HTML to receive the fields HTML tag.
141  ) const = 0;
142 
145  virtual PString GetHTMLInput(
146  const PString & input // Source HTML text for input tag.
147  ) const;
148 
151  virtual PString GetHTMLSelect(
152  const PString & selection // Source HTML text for input tag.
153  ) const;
154 
157  virtual void GetHTMLHeading(
158  PHTML & html // HTML to receive the field info.
159  ) const;
160 
166  virtual PString GetValue(PBoolean dflt = false) const = 0;
167 
170  virtual void SetValue(
171  const PString & newValue // New value for the field.
172  ) = 0;
173 
177  virtual void LoadFromConfig(
178  PConfig & cfg // Configuration for value transfer.
179  );
180 
184  virtual void SaveToConfig(
185  PConfig & cfg // Configuration for value transfer.
186  ) const;
187 
193  virtual PBoolean Validated(
194  const PString & newVal, // Proposed new value for the field.
195  PStringStream & msg // Stream to take error HTML if value not valid.
196  ) const;
197 
198 
204  virtual void GetAllNames(PStringArray & names) const;
205 
208  virtual void SetAllValues(
209  const PStringToString & data // New value for the field.
210  );
211 
217  virtual PBoolean ValidateAll(
218  const PStringToString & data, // Proposed new value for the field.
219  PStringStream & msg // Stream to take error HTML if value not valid.
220  ) const;
221 
222 
223  PBoolean NotYetInHTML() const { return notInHTML; }
224  void SetInHTML() { notInHTML = false; }
225 
226  protected:
232 };
233 
234 
235 PARRAY(PHTTPFields, PHTTPField);
236 
238 {
240  public:
242  const char * name, // Name (identifier) for the field.
243  const char * title = NULL, // Title text for field (defaults to name).
244  const char * help = NULL, // Help text for the field.
245  bool includeHeaders = false // Make a sub-table and put headers on HTML fields.
246  );
247 
248  virtual void SetName(
249  const PString & name // New name for field
250  );
251 
252  virtual const PHTTPField * LocateName(
253  const PString & name // Full field name to locate
254  ) const;
255 
256  virtual PHTTPField * NewField() const;
257 
258  virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
259 
260  virtual void GetHTMLTag(
261  PHTML & html // HTML to receive the field info.
262  ) const;
263 
264  virtual PString GetHTMLInput(
265  const PString & input // Source HTML text for input tag.
266  ) const;
267 
268  virtual void GetHTMLHeading(
269  PHTML & html // HTML to receive the field info.
270  ) const;
271 
272  virtual PString GetValue(PBoolean dflt = false) const;
273 
274  virtual void SetValue(
275  const PString & newValue // New value for the field.
276  );
277 
278  virtual void LoadFromConfig(
279  PConfig & cfg // Configuration for value transfer.
280  );
281  virtual void SaveToConfig(
282  PConfig & cfg // Configuration for value transfer.
283  ) const;
284 
285  virtual void GetAllNames(PStringArray & names) const;
286  virtual void SetAllValues(
287  const PStringToString & data // New value for the field.
288  );
289 
290  virtual PBoolean ValidateAll(
291  const PStringToString & data, // Proposed new value for the field.
292  PStringStream & msg // Stream to take error HTML if value not valid.
293  ) const;
294 
295 
303  virtual PINDEX GetSize() const;
304 
305  void Append(PHTTPField * fld);
306  PHTTPField & operator[](PINDEX idx) const { return fields[idx]; }
307  void RemoveAt(PINDEX idx) { fields.RemoveAt(idx); }
308  void RemoveAll() { fields.RemoveAll(); }
309 
310  protected:
311  PHTTPFields fields;
313 };
314 
315 
317 {
319  public:
320  PHTTPSubForm(
321  const PString & subFormName, // URL for the sub-form
322  const char * name, // Name (identifier) for the field.
323  const char * title = NULL, // Title text for field (defaults to name).
324  PINDEX primaryField = 0, // Pimary field whove value is in hot link
325  PINDEX secondaryField = P_MAX_INDEX // Seconary field next to hotlink
326  );
327 
328  PHTTPField * NewField() const;
329  void GetHTMLTag(PHTML & html) const;
330  void GetHTMLHeading(PHTML & html) const;
331 
332  protected:
334  PINDEX primary;
335  PINDEX secondary;
336 };
337 
338 
340 {
342  public:
345  PBoolean ordered,
346  PINDEX fixedSize = 0
347  );
348 
350 
351 
352  virtual PHTTPField * NewField() const;
353 
354  virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
355 
356  virtual void GetHTMLTag(
357  PHTML & html // HTML to receive the field info.
358  ) const;
359 
360  virtual void LoadFromConfig(
361  PConfig & cfg // Configuration for value transfer.
362  );
363  virtual void SaveToConfig(
364  PConfig & cfg // Configuration for value transfer.
365  ) const;
366 
367 
368  virtual void SetAllValues(
369  const PStringToString & data // New value for the field.
370  );
371 
372  virtual PINDEX GetSize() const;
373  void SetSize(PINDEX newSize);
374 
376  PConfig & cfg
377  );
378 
379  void SetStrings(
380  PConfig & cfg,
381  const PStringArray & values
382  );
383 
384  protected:
385  void AddBlankField();
386  void AddArrayControlBox(PHTML & html, PINDEX fld) const;
387  void SetArrayFieldName(PINDEX idx) const;
388 
392 };
393 
394 
396 {
398  public:
400  const char * name,
401  PINDEX maxLength,
402  const char * initVal = NULL,
403  const char * help = NULL,
404  int rows = 0, // 0=auto, 1=InputText, >1=TextArea
405  int columns = 0
406  );
408  const char * name,
409  const char * title,
410  PINDEX maxLength,
411  const char * initVal = NULL,
412  const char * help = NULL,
413  int rows = 0, // 0=auto, 1=InputText, >1=TextArea
414  int columns = 0
415  );
416 
417  virtual PHTTPField * NewField() const;
418 
419  virtual void GetHTMLTag(
420  PHTML & html
421  ) const;
422 
423  virtual PString GetValue(PBoolean dflt = false) const;
424 
425  virtual void SetValue(
426  const PString & newVal
427  );
428 
429 
430  protected:
433  PINDEX maxLength;
434  int rows;
435  int columns;
436 };
437 
438 
440 {
442  public:
444  const char * name,
445  PINDEX size,
446  const char * initVal = NULL,
447  const char * help = NULL
448  );
450  const char * name,
451  const char * title,
452  PINDEX size,
453  const char * initVal = NULL,
454  const char * help = NULL
455  );
456 
457  virtual PHTTPField * NewField() const;
458 
459  virtual void GetHTMLTag(
460  PHTML & html
461  ) const;
462 
463  virtual PString GetValue(PBoolean dflt = false) const;
464 
465  virtual void SetValue(
466  const PString & newVal
467  );
468 
469  static PString Decrypt(const PString & pword);
470 };
471 
472 
474 {
476  public:
478  const char * name,
479  const PTime & initVal = PTime(0),
481  );
482 
483  virtual PHTTPField * NewField() const;
484 
485  virtual void SetValue(
486  const PString & newValue
487  );
488 
489  virtual PBoolean Validated(
490  const PString & newValue,
491  PStringStream & msg
492  ) const;
493 
494  protected:
496 };
497 
498 
500 {
502  public:
504  const char * name,
505  int low, int high,
506  int initVal = 0,
507  const char * units = NULL,
508  const char * help = NULL
509  );
511  const char * name,
512  const char * title,
513  int low, int high,
514  int initVal = 0,
515  const char * units = NULL,
516  const char * help = NULL
517  );
518 
519  virtual PHTTPField * NewField() const;
520 
521  virtual void GetHTMLTag(
522  PHTML & html
523  ) const;
524 
525  virtual PString GetValue(PBoolean dflt = false) const;
526 
527  virtual void SetValue(
528  const PString & newVal
529  );
530 
531  virtual void LoadFromConfig(
532  PConfig & cfg
533  );
534  virtual void SaveToConfig(
535  PConfig & cfg
536  ) const;
537 
538  virtual PBoolean Validated(
539  const PString & newVal,
540  PStringStream & msg
541  ) const;
542 
543 
544  protected:
545  int low, high, value;
548 };
549 
550 
552 {
554  public:
556  const char * name,
557  PBoolean initVal = false,
558  const char * help = NULL
559  );
561  const char * name,
562  const char * title,
563  PBoolean initVal = false,
564  const char * help = NULL
565  );
566 
567  virtual PHTTPField * NewField() const;
568 
569  virtual void GetHTMLTag(
570  PHTML & html
571  ) const;
572 
573  virtual PString GetHTMLInput(
574  const PString & input
575  ) const;
576 
577  virtual PString GetValue(PBoolean dflt = false) const;
578 
579  virtual void SetValue(
580  const PString & newVal
581  );
582 
583  virtual void LoadFromConfig(
584  PConfig & cfg
585  );
586  virtual void SaveToConfig(
587  PConfig & cfg
588  ) const;
589 
590 
591  protected:
593 };
594 
595 
597 {
599  public:
601  const char * name,
602  const PStringArray & valueArray,
603  PINDEX initVal = 0,
604  const char * help = NULL
605  );
607  const char * name,
608  const PStringArray & valueArray,
609  const PStringArray & titleArray,
610  PINDEX initVal = 0,
611  const char * help = NULL
612  );
614  const char * name,
615  PINDEX count,
616  const char * const * valueStrings,
617  PINDEX initVal = 0,
618  const char * help = NULL
619  );
621  const char * name,
622  PINDEX count,
623  const char * const * valueStrings,
624  const char * const * titleStrings,
625  PINDEX initVal = 0,
626  const char * help = NULL
627  );
629  const char * name,
630  const char * groupTitle,
631  const PStringArray & valueArray,
632  PINDEX initVal = 0,
633  const char * help = NULL
634  );
636  const char * name,
637  const char * groupTitle,
638  const PStringArray & valueArray,
639  const PStringArray & titleArray,
640  PINDEX initVal = 0,
641  const char * help = NULL
642  );
644  const char * name,
645  const char * groupTitle,
646  PINDEX count,
647  const char * const * valueStrings,
648  PINDEX initVal = 0,
649  const char * help = NULL
650  );
652  const char * name,
653  const char * groupTitle,
654  PINDEX count,
655  const char * const * valueStrings,
656  const char * const * titleStrings,
657  PINDEX initVal = 0,
658  const char * help = NULL
659  );
660 
661  virtual PHTTPField * NewField() const;
662 
663  virtual void GetHTMLTag(
664  PHTML & html
665  ) const;
666 
667  virtual PString GetHTMLInput(
668  const PString & input
669  ) const;
670 
671  virtual PString GetValue(PBoolean dflt = false) const;
672 
673  virtual void SetValue(
674  const PString & newVal
675  );
676 
677 
678  protected:
683 };
684 
685 
687 {
689  public:
691  const char * name,
692  const PStringArray & valueArray,
693  PINDEX initVal = 0,
694  const char * help = NULL,
695  bool enumeration = false
696  );
698  const char * name,
699  PINDEX count,
700  const char * const * valueStrings,
701  PINDEX initVal = 0,
702  const char * help = NULL,
703  bool enumeration = false
704  );
706  const char * name,
707  const char * title,
708  const PStringArray & valueArray,
709  PINDEX initVal = 0,
710  const char * help = NULL,
711  bool enumeration = false
712  );
714  const char * name,
715  const char * title,
716  PINDEX count,
717  const char * const * valueStrings,
718  PINDEX initVal = 0,
719  const char * help = NULL,
720  bool enumeration = false
721  );
722 
723  virtual PHTTPField * NewField() const;
724 
725  virtual void GetHTMLTag(
726  PHTML & html
727  ) const;
728 
729  virtual PString GetValue(PBoolean dflt = false) const;
730 
731  virtual void SetValue(
732  const PString & newVal
733  );
734 
735  protected:
740 };
741 
742 
743 template <typename Enumeration, Enumeration InitVal = (Enumeration)0>
745 {
747  public:
749  const char * name,
750  const PStringArray & valueArray,
751  Enumeration initVal = InitVal,
752  const char * help = NULL
753  ) : PHTTPSelectField(name, valueArray, initVal, help, true) { }
755  const char * name,
756  PINDEX count,
757  const char * const * valueStrings,
758  Enumeration initVal = InitVal,
759  const char * help = NULL
760  ) : PHTTPSelectField(name, count, valueStrings, initVal, help, true) { }
762  const char * name,
763  const char * title,
764  const PStringArray & valueArray,
765  Enumeration initVal = InitVal,
766  const char * help = NULL
767  ) : PHTTPSelectField(name, title, valueArray, initVal, help, true) { }
769  const char * name,
770  const char * title,
771  PINDEX count,
772  const char * const * valueStrings,
773  Enumeration initVal = InitVal,
774  const char * help = NULL
775  ) : PHTTPSelectField(name, title, count, valueStrings, initVal, help, true) { }
776 
777  virtual PHTTPField * NewField() const { return new PHTTPEnumField(baseName, title, m_values, (Enumeration)m_initialValue, help); }
778 };
779 
780 
782 // PHTTPForm
783 
784 class PHTTPForm : public PHTTPString
785 {
787  public:
788  PHTTPForm(
789  const PURL & url
790  );
791  PHTTPForm(
792  const PURL & url,
793  const PHTTPAuthority & auth
794  );
795  PHTTPForm(
796  const PURL & url,
797  const PString & html
798  );
799  PHTTPForm(
800  const PURL & url,
801  const PString & html,
802  const PHTTPAuthority & auth
803  );
804 
805 
806  virtual void OnLoadedText(
807  PHTTPRequest & request,
808  PString & text
809  );
810  virtual PBoolean Post(
811  PHTTPRequest & request,
812  const PStringToString & data,
813  PHTML & replyMessage
814  );
815 
816 
817  PHTTPField * Add(
818  PHTTPField * fld
819  );
822 
827  };
828 
829  void BuildHTML(
830  const char * heading
831  );
832  void BuildHTML(
833  const PString & heading
834  );
835  void BuildHTML(
836  PHTML & html,
837  BuildOptions option = CompleteHTML
838  );
839 
840 
841  protected:
844 };
845 
846 
848 // PHTTPConfig
849 
850 class PHTTPConfig : public PHTTPForm
851 {
853  public:
854  PHTTPConfig(
855  const PURL & url,
856  const PString & section
857  );
858  PHTTPConfig(
859  const PURL & url,
860  const PString & section,
861  const PHTTPAuthority & auth
862  );
863  PHTTPConfig(
864  const PURL & url,
865  const PString & section,
866  const PString & html
867  );
868  PHTTPConfig(
869  const PURL & url,
870  const PString & section,
871  const PString & html,
872  const PHTTPAuthority & auth
873  );
874 
875  virtual void OnLoadedText(
876  PHTTPRequest & request,
877  PString & text
878  );
879  virtual PBoolean Post(
880  PHTTPRequest & request,
881  const PStringToString & data,
882  PHTML & replyMessage
883  );
884 
885 
888  void LoadFromConfig();
889 
895  const PString & GetConfigSection() const { return section; }
896 
898  const PString & sect
899  ) { section = sect; }
900  // Set the configuration file section.
901 
907  PHTTPField * sectionFld,
908  const char * prefix = NULL,
909  const char * suffix = NULL
910  );
911 
915  void AddNewKeyFields(
916  PHTTPField * keyFld,
917  PHTTPField * valFld
918  );
919 
920 
921  protected:
928 
929  private:
930  void Construct();
931 };
932 
933 
935 // PHTTPConfigSectionList
936 
938 {
940  public:
942  const PURL & url,
943  const PHTTPAuthority & auth,
944  const PString & sectionPrefix,
946  const PURL & editSection,
947  const PURL & newSection,
948  const PString & newSectionTitle,
949  PHTML & heading
950  );
951 
952  virtual void OnLoadedText(
953  PHTTPRequest & request,
954  PString & text
955  );
956  virtual PBoolean Post(
957  PHTTPRequest & request,
958  const PStringToString & data,
959  PHTML & replyMessage
960  );
961 
962  protected:
968 };
969 
970 
971 #endif // P_HTTPFORMS
972 
973 #endif // PTLIB_HTTPFORM_H
974 
975 
976 // End Of File ///////////////////////////////////////////////////////////////