PTLib
Version 2.10.4
|
00001 /* 00002 * httpform.h 00003 * 00004 * Forms management using HTTP User Interface. 00005 * 00006 * Portable Windows Library 00007 * 00008 * Copyright (c) 1993-2002 Equivalence Pty. Ltd. 00009 * 00010 * The contents of this file are subject to the Mozilla Public License 00011 * Version 1.0 (the "License"); you may not use this file except in 00012 * compliance with the License. You may obtain a copy of the License at 00013 * http://www.mozilla.org/MPL/ 00014 * 00015 * Software distributed under the License is distributed on an "AS IS" 00016 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 00017 * the License for the specific language governing rights and limitations 00018 * under the License. 00019 * 00020 * The Original Code is Portable Windows Library. 00021 * 00022 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 00023 * 00024 * Contributor(s): ______________________________________. 00025 * 00026 * $Revision: 24177 $ 00027 * $Author: rjongbloed $ 00028 * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $ 00029 */ 00030 00031 #ifndef PTLIB_HTTPFORM_H 00032 #define PTLIB_HTTPFORM_H 00033 00034 #ifdef P_USE_PRAGMA 00035 #pragma interface 00036 #endif 00037 00038 #if P_HTTPFORMS 00039 00040 #include <ptclib/http.h> 00041 #include <ptclib/html.h> 00042 00043 00045 // PHTTPField 00046 00050 class PHTTPField : public PObject 00051 { 00052 PCLASSINFO(PHTTPField, PObject) 00053 public: 00054 PHTTPField( 00055 const char * bname, // base name (identifier) for the field. 00056 const char * title, // Title text for field (defaults to name). 00057 const char * help // Help text for the field. 00058 ); 00059 // Create a new field in a HTTP form. 00060 00066 virtual Comparison Compare( 00067 const PObject & obj 00068 ) const; 00069 00075 const PCaselessString & GetName() const { return fullName; } 00076 00082 const PCaselessString & GetBaseName() const { return baseName; } 00083 00086 virtual void SetName( 00087 const PString & newName // New name for field 00088 ); 00089 00095 virtual const PHTTPField * LocateName( 00096 const PString & name // Full field name to locate 00097 ) const; 00098 00104 const PString & GetTitle() const { return title; } 00105 00111 const PString & GetHelp() const { return help; } 00112 00113 void SetHelp( 00114 const PString & text // Help text. 00115 ) { help = text; } 00116 void SetHelp( 00117 const PString & hotLinkURL, // URL for link to help page. 00118 const PString & linkText // Help text in the link. 00119 ); 00120 void SetHelp( 00121 const PString & hotLinkURL, // URL for link to help page. 00122 const PString & imageURL, // URL for image to be displayed in link. 00123 const PString & imageText // Text in the link when image unavailable. 00124 ); 00125 // Set the help text for the field. 00126 00132 virtual PHTTPField * NewField() const = 0; 00133 00134 virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const; 00135 // Splice expanded macro substitutions into text string 00136 00139 virtual void GetHTMLTag( 00140 PHTML & html // HTML to receive the fields HTML tag. 00141 ) const = 0; 00142 00145 virtual PString GetHTMLInput( 00146 const PString & input // Source HTML text for input tag. 00147 ) const; 00148 00151 virtual PString GetHTMLSelect( 00152 const PString & selection // Source HTML text for input tag. 00153 ) const; 00154 00157 virtual void GetHTMLHeading( 00158 PHTML & html // HTML to receive the field info. 00159 ) const; 00160 00166 virtual PString GetValue(PBoolean dflt = false) const = 0; 00167 00170 virtual void SetValue( 00171 const PString & newValue // New value for the field. 00172 ) = 0; 00173 00177 virtual void LoadFromConfig( 00178 PConfig & cfg // Configuration for value transfer. 00179 ); 00180 00184 virtual void SaveToConfig( 00185 PConfig & cfg // Configuration for value transfer. 00186 ) const; 00187 00193 virtual PBoolean Validated( 00194 const PString & newVal, // Proposed new value for the field. 00195 PStringStream & msg // Stream to take error HTML if value not valid. 00196 ) const; 00197 00198 00204 virtual void GetAllNames(PStringArray & names) const; 00205 00208 virtual void SetAllValues( 00209 const PStringToString & data // New value for the field. 00210 ); 00211 00217 virtual PBoolean ValidateAll( 00218 const PStringToString & data, // Proposed new value for the field. 00219 PStringStream & msg // Stream to take error HTML if value not valid. 00220 ) const; 00221 00222 00223 PBoolean NotYetInHTML() const { return notInHTML; } 00224 void SetInHTML() { notInHTML = false; } 00225 00226 protected: 00227 PCaselessString baseName; 00228 PCaselessString fullName; 00229 PString title; 00230 PString help; 00231 PBoolean notInHTML; 00232 }; 00233 00234 00235 PARRAY(PHTTPFields, PHTTPField); 00236 00237 class PHTTPCompositeField : public PHTTPField 00238 { 00239 PCLASSINFO(PHTTPCompositeField, PHTTPField) 00240 public: 00241 PHTTPCompositeField( 00242 const char * name, // Name (identifier) for the field. 00243 const char * title = NULL, // Title text for field (defaults to name). 00244 const char * help = NULL, // Help text for the field. 00245 bool includeHeaders = false // Make a sub-table and put headers on HTML fields. 00246 ); 00247 00248 virtual void SetName( 00249 const PString & name // New name for field 00250 ); 00251 00252 virtual const PHTTPField * LocateName( 00253 const PString & name // Full field name to locate 00254 ) const; 00255 00256 virtual PHTTPField * NewField() const; 00257 00258 virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const; 00259 00260 virtual void GetHTMLTag( 00261 PHTML & html // HTML to receive the field info. 00262 ) const; 00263 00264 virtual PString GetHTMLInput( 00265 const PString & input // Source HTML text for input tag. 00266 ) const; 00267 00268 virtual void GetHTMLHeading( 00269 PHTML & html // HTML to receive the field info. 00270 ) const; 00271 00272 virtual PString GetValue(PBoolean dflt = false) const; 00273 00274 virtual void SetValue( 00275 const PString & newValue // New value for the field. 00276 ); 00277 00278 virtual void LoadFromConfig( 00279 PConfig & cfg // Configuration for value transfer. 00280 ); 00281 virtual void SaveToConfig( 00282 PConfig & cfg // Configuration for value transfer. 00283 ) const; 00284 00285 virtual void GetAllNames(PStringArray & names) const; 00286 virtual void SetAllValues( 00287 const PStringToString & data // New value for the field. 00288 ); 00289 00290 virtual PBoolean ValidateAll( 00291 const PStringToString & data, // Proposed new value for the field. 00292 PStringStream & msg // Stream to take error HTML if value not valid. 00293 ) const; 00294 00295 00303 virtual PINDEX GetSize() const; 00304 00305 void Append(PHTTPField * fld); 00306 PHTTPField & operator[](PINDEX idx) const { return fields[idx]; } 00307 void RemoveAt(PINDEX idx) { fields.RemoveAt(idx); } 00308 void RemoveAll() { fields.RemoveAll(); } 00309 00310 protected: 00311 PHTTPFields fields; 00312 bool m_includeHeaders; 00313 }; 00314 00315 00316 class PHTTPSubForm : public PHTTPCompositeField 00317 { 00318 PCLASSINFO(PHTTPSubForm, PHTTPCompositeField) 00319 public: 00320 PHTTPSubForm( 00321 const PString & subFormName, // URL for the sub-form 00322 const char * name, // Name (identifier) for the field. 00323 const char * title = NULL, // Title text for field (defaults to name). 00324 PINDEX primaryField = 0, // Pimary field whove value is in hot link 00325 PINDEX secondaryField = P_MAX_INDEX // Seconary field next to hotlink 00326 ); 00327 00328 PHTTPField * NewField() const; 00329 void GetHTMLTag(PHTML & html) const; 00330 void GetHTMLHeading(PHTML & html) const; 00331 00332 protected: 00333 PString subFormName; 00334 PINDEX primary; 00335 PINDEX secondary; 00336 }; 00337 00338 00339 class PHTTPFieldArray : public PHTTPCompositeField 00340 { 00341 PCLASSINFO(PHTTPFieldArray, PHTTPCompositeField) 00342 public: 00343 PHTTPFieldArray( 00344 PHTTPField * baseField, 00345 PBoolean ordered, 00346 PINDEX fixedSize = 0 00347 ); 00348 00349 ~PHTTPFieldArray(); 00350 00351 00352 virtual PHTTPField * NewField() const; 00353 00354 virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const; 00355 00356 virtual void GetHTMLTag( 00357 PHTML & html // HTML to receive the field info. 00358 ) const; 00359 00360 virtual void LoadFromConfig( 00361 PConfig & cfg // Configuration for value transfer. 00362 ); 00363 virtual void SaveToConfig( 00364 PConfig & cfg // Configuration for value transfer. 00365 ) const; 00366 00367 00368 virtual void SetAllValues( 00369 const PStringToString & data // New value for the field. 00370 ); 00371 00372 virtual PINDEX GetSize() const; 00373 void SetSize(PINDEX newSize); 00374 00375 PStringArray GetStrings( 00376 PConfig & cfg 00377 ); 00378 00379 void SetStrings( 00380 PConfig & cfg, 00381 const PStringArray & values 00382 ); 00383 00384 protected: 00385 void AddBlankField(); 00386 void AddArrayControlBox(PHTML & html, PINDEX fld) const; 00387 void SetArrayFieldName(PINDEX idx) const; 00388 00389 PHTTPField * baseField; 00390 PBoolean orderedArray; 00391 PBoolean canAddElements; 00392 }; 00393 00394 00395 class PHTTPStringField : public PHTTPField 00396 { 00397 PCLASSINFO(PHTTPStringField, PHTTPField) 00398 public: 00399 PHTTPStringField( 00400 const char * name, 00401 PINDEX size, 00402 const char * initVal = NULL, 00403 const char * help = NULL 00404 ); 00405 PHTTPStringField( 00406 const char * name, 00407 const char * title, 00408 PINDEX size, 00409 const char * initVal = NULL, 00410 const char * help = NULL 00411 ); 00412 00413 virtual PHTTPField * NewField() const; 00414 00415 virtual void GetHTMLTag( 00416 PHTML & html 00417 ) const; 00418 00419 virtual PString GetValue(PBoolean dflt = false) const; 00420 00421 virtual void SetValue( 00422 const PString & newVal 00423 ); 00424 00425 00426 protected: 00427 PString value; 00428 PString initialValue; 00429 PINDEX size; 00430 }; 00431 00432 00433 class PHTTPPasswordField : public PHTTPStringField 00434 { 00435 PCLASSINFO(PHTTPPasswordField, PHTTPStringField) 00436 public: 00437 PHTTPPasswordField( 00438 const char * name, 00439 PINDEX size, 00440 const char * initVal = NULL, 00441 const char * help = NULL 00442 ); 00443 PHTTPPasswordField( 00444 const char * name, 00445 const char * title, 00446 PINDEX size, 00447 const char * initVal = NULL, 00448 const char * help = NULL 00449 ); 00450 00451 virtual PHTTPField * NewField() const; 00452 00453 virtual void GetHTMLTag( 00454 PHTML & html 00455 ) const; 00456 00457 virtual PString GetValue(PBoolean dflt = false) const; 00458 00459 virtual void SetValue( 00460 const PString & newVal 00461 ); 00462 00463 static PString Decrypt(const PString & pword); 00464 }; 00465 00466 00467 class PHTTPDateField : public PHTTPStringField 00468 { 00469 PCLASSINFO(PHTTPDateField, PHTTPStringField) 00470 public: 00471 PHTTPDateField( 00472 const char * name, 00473 const PTime & initVal = PTime(0), 00474 PTime::TimeFormat fmt = PTime::ShortDate 00475 ); 00476 00477 virtual PHTTPField * NewField() const; 00478 00479 virtual void SetValue( 00480 const PString & newValue 00481 ); 00482 00483 virtual PBoolean Validated( 00484 const PString & newValue, 00485 PStringStream & msg 00486 ) const; 00487 00488 protected: 00489 PTime::TimeFormat m_format; 00490 }; 00491 00492 00493 class PHTTPIntegerField : public PHTTPField 00494 { 00495 PCLASSINFO(PHTTPIntegerField, PHTTPField) 00496 public: 00497 PHTTPIntegerField( 00498 const char * name, 00499 int low, int high, 00500 int initVal = 0, 00501 const char * units = NULL, 00502 const char * help = NULL 00503 ); 00504 PHTTPIntegerField( 00505 const char * name, 00506 const char * title, 00507 int low, int high, 00508 int initVal = 0, 00509 const char * units = NULL, 00510 const char * help = NULL 00511 ); 00512 00513 virtual PHTTPField * NewField() const; 00514 00515 virtual void GetHTMLTag( 00516 PHTML & html 00517 ) const; 00518 00519 virtual PString GetValue(PBoolean dflt = false) const; 00520 00521 virtual void SetValue( 00522 const PString & newVal 00523 ); 00524 00525 virtual void LoadFromConfig( 00526 PConfig & cfg 00527 ); 00528 virtual void SaveToConfig( 00529 PConfig & cfg 00530 ) const; 00531 00532 virtual PBoolean Validated( 00533 const PString & newVal, 00534 PStringStream & msg 00535 ) const; 00536 00537 00538 protected: 00539 int low, high, value; 00540 int initialValue; 00541 PString units; 00542 }; 00543 00544 00545 class PHTTPBooleanField : public PHTTPField 00546 { 00547 PCLASSINFO(PHTTPBooleanField, PHTTPField) 00548 public: 00549 PHTTPBooleanField( 00550 const char * name, 00551 PBoolean initVal = false, 00552 const char * help = NULL 00553 ); 00554 PHTTPBooleanField( 00555 const char * name, 00556 const char * title, 00557 PBoolean initVal = false, 00558 const char * help = NULL 00559 ); 00560 00561 virtual PHTTPField * NewField() const; 00562 00563 virtual void GetHTMLTag( 00564 PHTML & html 00565 ) const; 00566 00567 virtual PString GetHTMLInput( 00568 const PString & input 00569 ) const; 00570 00571 virtual PString GetValue(PBoolean dflt = false) const; 00572 00573 virtual void SetValue( 00574 const PString & newVal 00575 ); 00576 00577 virtual void LoadFromConfig( 00578 PConfig & cfg 00579 ); 00580 virtual void SaveToConfig( 00581 PConfig & cfg 00582 ) const; 00583 00584 00585 protected: 00586 PBoolean value, initialValue; 00587 }; 00588 00589 00590 class PHTTPRadioField : public PHTTPField 00591 { 00592 PCLASSINFO(PHTTPRadioField, PHTTPField) 00593 public: 00594 PHTTPRadioField( 00595 const char * name, 00596 const PStringArray & valueArray, 00597 PINDEX initVal = 0, 00598 const char * help = NULL 00599 ); 00600 PHTTPRadioField( 00601 const char * name, 00602 const PStringArray & valueArray, 00603 const PStringArray & titleArray, 00604 PINDEX initVal = 0, 00605 const char * help = NULL 00606 ); 00607 PHTTPRadioField( 00608 const char * name, 00609 PINDEX count, 00610 const char * const * valueStrings, 00611 PINDEX initVal = 0, 00612 const char * help = NULL 00613 ); 00614 PHTTPRadioField( 00615 const char * name, 00616 PINDEX count, 00617 const char * const * valueStrings, 00618 const char * const * titleStrings, 00619 PINDEX initVal = 0, 00620 const char * help = NULL 00621 ); 00622 PHTTPRadioField( 00623 const char * name, 00624 const char * groupTitle, 00625 const PStringArray & valueArray, 00626 PINDEX initVal = 0, 00627 const char * help = NULL 00628 ); 00629 PHTTPRadioField( 00630 const char * name, 00631 const char * groupTitle, 00632 const PStringArray & valueArray, 00633 const PStringArray & titleArray, 00634 PINDEX initVal = 0, 00635 const char * help = NULL 00636 ); 00637 PHTTPRadioField( 00638 const char * name, 00639 const char * groupTitle, 00640 PINDEX count, 00641 const char * const * valueStrings, 00642 PINDEX initVal = 0, 00643 const char * help = NULL 00644 ); 00645 PHTTPRadioField( 00646 const char * name, 00647 const char * groupTitle, 00648 PINDEX count, 00649 const char * const * valueStrings, 00650 const char * const * titleStrings, 00651 PINDEX initVal = 0, 00652 const char * help = NULL 00653 ); 00654 00655 virtual PHTTPField * NewField() const; 00656 00657 virtual void GetHTMLTag( 00658 PHTML & html 00659 ) const; 00660 00661 virtual PString GetHTMLInput( 00662 const PString & input 00663 ) const; 00664 00665 virtual PString GetValue(PBoolean dflt = false) const; 00666 00667 virtual void SetValue( 00668 const PString & newVal 00669 ); 00670 00671 00672 protected: 00673 PStringArray values; 00674 PStringArray titles; 00675 PString value; 00676 PString initialValue; 00677 }; 00678 00679 00680 class PHTTPSelectField : public PHTTPField 00681 { 00682 PCLASSINFO(PHTTPSelectField, PHTTPField) 00683 public: 00684 PHTTPSelectField( 00685 const char * name, 00686 const PStringArray & valueArray, 00687 PINDEX initVal = 0, 00688 const char * help = NULL 00689 ); 00690 PHTTPSelectField( 00691 const char * name, 00692 PINDEX count, 00693 const char * const * valueStrings, 00694 PINDEX initVal = 0, 00695 const char * help = NULL 00696 ); 00697 PHTTPSelectField( 00698 const char * name, 00699 const char * title, 00700 const PStringArray & valueArray, 00701 PINDEX initVal = 0, 00702 const char * help = NULL 00703 ); 00704 PHTTPSelectField( 00705 const char * name, 00706 const char * title, 00707 PINDEX count, 00708 const char * const * valueStrings, 00709 PINDEX initVal = 0, 00710 const char * help = NULL 00711 ); 00712 00713 virtual PHTTPField * NewField() const; 00714 00715 virtual void GetHTMLTag( 00716 PHTML & html 00717 ) const; 00718 00719 virtual PString GetValue(PBoolean dflt = false) const; 00720 00721 virtual void SetValue( 00722 const PString & newVal 00723 ); 00724 00725 00726 PStringArray values; 00727 00728 00729 protected: 00730 PString value; 00731 PINDEX initialValue; 00732 }; 00733 00734 00736 // PHTTPForm 00737 00738 class PHTTPForm : public PHTTPString 00739 { 00740 PCLASSINFO(PHTTPForm, PHTTPString) 00741 public: 00742 PHTTPForm( 00743 const PURL & url 00744 ); 00745 PHTTPForm( 00746 const PURL & url, 00747 const PHTTPAuthority & auth 00748 ); 00749 PHTTPForm( 00750 const PURL & url, 00751 const PString & html 00752 ); 00753 PHTTPForm( 00754 const PURL & url, 00755 const PString & html, 00756 const PHTTPAuthority & auth 00757 ); 00758 00759 00760 virtual void OnLoadedText( 00761 PHTTPRequest & request, 00762 PString & text 00763 ); 00764 virtual PBoolean Post( 00765 PHTTPRequest & request, 00766 const PStringToString & data, 00767 PHTML & replyMessage 00768 ); 00769 00770 00771 PHTTPField * Add( 00772 PHTTPField * fld 00773 ); 00774 void RemoveAllFields() 00775 { fields.RemoveAll(); fieldNames.RemoveAll(); } 00776 00777 enum BuildOptions { 00778 CompleteHTML, 00779 InsertIntoForm, 00780 InsertIntoHTML 00781 }; 00782 00783 void BuildHTML( 00784 const char * heading 00785 ); 00786 void BuildHTML( 00787 const PString & heading 00788 ); 00789 void BuildHTML( 00790 PHTML & html, 00791 BuildOptions option = CompleteHTML 00792 ); 00793 00794 00795 protected: 00796 PHTTPCompositeField fields; 00797 PStringSet fieldNames; 00798 }; 00799 00800 00802 // PHTTPConfig 00803 00804 class PHTTPConfig : public PHTTPForm 00805 { 00806 PCLASSINFO(PHTTPConfig, PHTTPForm) 00807 public: 00808 PHTTPConfig( 00809 const PURL & url, 00810 const PString & section 00811 ); 00812 PHTTPConfig( 00813 const PURL & url, 00814 const PString & section, 00815 const PHTTPAuthority & auth 00816 ); 00817 PHTTPConfig( 00818 const PURL & url, 00819 const PString & section, 00820 const PString & html 00821 ); 00822 PHTTPConfig( 00823 const PURL & url, 00824 const PString & section, 00825 const PString & html, 00826 const PHTTPAuthority & auth 00827 ); 00828 00829 virtual void OnLoadedText( 00830 PHTTPRequest & request, 00831 PString & text 00832 ); 00833 virtual PBoolean Post( 00834 PHTTPRequest & request, 00835 const PStringToString & data, 00836 PHTML & replyMessage 00837 ); 00838 00839 00842 void LoadFromConfig(); 00843 00849 const PString & GetConfigSection() const { return section; } 00850 00851 void SetConfigSection( 00852 const PString & sect 00853 ) { section = sect; } 00854 // Set the configuration file section. 00855 00860 PHTTPField * AddSectionField( 00861 PHTTPField * sectionFld, 00862 const char * prefix = NULL, 00863 const char * suffix = NULL 00864 ); 00865 00869 void AddNewKeyFields( 00870 PHTTPField * keyFld, 00871 PHTTPField * valFld 00872 ); 00873 00874 00875 protected: 00876 PString section; 00877 PString sectionPrefix; 00878 PString sectionSuffix; 00879 PHTTPField * sectionField; 00880 PHTTPField * keyField; 00881 PHTTPField * valField; 00882 00883 private: 00884 void Construct(); 00885 }; 00886 00887 00889 // PHTTPConfigSectionList 00890 00891 class PHTTPConfigSectionList : public PHTTPString 00892 { 00893 PCLASSINFO(PHTTPConfigSectionList, PHTTPString) 00894 public: 00895 PHTTPConfigSectionList( 00896 const PURL & url, 00897 const PHTTPAuthority & auth, 00898 const PString & sectionPrefix, 00899 const PString & additionalValueName, 00900 const PURL & editSection, 00901 const PURL & newSection, 00902 const PString & newSectionTitle, 00903 PHTML & heading 00904 ); 00905 00906 virtual void OnLoadedText( 00907 PHTTPRequest & request, 00908 PString & text 00909 ); 00910 virtual PBoolean Post( 00911 PHTTPRequest & request, 00912 const PStringToString & data, 00913 PHTML & replyMessage 00914 ); 00915 00916 protected: 00917 PString sectionPrefix; 00918 PString additionalValueName; 00919 PString newSectionLink; 00920 PString newSectionTitle; 00921 PString editSectionLink; 00922 }; 00923 00924 00925 #endif // P_HTTPFORMS 00926 00927 #endif // PTLIB_HTTPFORM_H 00928 00929 00930 // End Of File ///////////////////////////////////////////////////////////////