httpform.h

Go to the documentation of this file.
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: 20385 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00029  */
00030 
00031 #ifndef _PHTTPFORM_H
00032 #define _PHTTPFORM_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 = PFalse) 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 = PFalse; }
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     );
00246 
00247     virtual void SetName(
00248       const PString & name   // New name for field
00249     );
00250 
00251     virtual const PHTTPField * LocateName(
00252       const PString & name    // Full field name to locate
00253     ) const;
00254 
00255     virtual PHTTPField * NewField() const;
00256 
00257     virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
00258 
00259     virtual void GetHTMLTag(
00260       PHTML & html    // HTML to receive the field info.
00261     ) const;
00262 
00263     virtual PString GetHTMLInput(
00264       const PString & input // Source HTML text for input tag.
00265     ) const;
00266 
00267     virtual void GetHTMLHeading(
00268       PHTML & html    // HTML to receive the field info.
00269     ) const;
00270 
00271     virtual PString GetValue(PBoolean dflt = PFalse) const;
00272 
00273     virtual void SetValue(
00274       const PString & newValue   // New value for the field.
00275     );
00276 
00277     virtual void LoadFromConfig(
00278       PConfig & cfg   // Configuration for value transfer.
00279     );
00280     virtual void SaveToConfig(
00281       PConfig & cfg   // Configuration for value transfer.
00282     ) const;
00283 
00284     virtual void GetAllNames(PStringArray & names) const;
00285     virtual void SetAllValues(
00286       const PStringToString & data   // New value for the field.
00287     );
00288 
00289     virtual PBoolean ValidateAll(
00290       const PStringToString & data, // Proposed new value for the field.
00291       PStringStream & msg     // Stream to take error HTML if value not valid.
00292     ) const;
00293 
00294 
00302     virtual PINDEX GetSize() const;
00303 
00304     void Append(PHTTPField * fld);
00305     PHTTPField & operator[](PINDEX idx) const { return fields[idx]; }
00306     void RemoveAt(PINDEX idx) { fields.RemoveAt(idx); }
00307     void RemoveAll() { fields.RemoveAll(); }
00308 
00309   protected:
00310     PHTTPFields fields;
00311 };
00312 
00313 
00314 class PHTTPSubForm : public PHTTPCompositeField
00315 {
00316   PCLASSINFO(PHTTPSubForm, PHTTPCompositeField)
00317   public:
00318     PHTTPSubForm(
00319       const PString & subFormName, // URL for the sub-form
00320       const char * name,           // Name (identifier) for the field.
00321       const char * title = NULL,   // Title text for field (defaults to name).
00322       PINDEX primaryField = 0,     // Pimary field whove value is in hot link
00323       PINDEX secondaryField = P_MAX_INDEX   // Seconary field next to hotlink
00324     );
00325 
00326   PHTTPField * NewField() const;
00327   void GetHTMLTag(PHTML & html) const;
00328   void GetHTMLHeading(PHTML & html) const;
00329 
00330   protected:
00331     PString subFormName;
00332     PINDEX primary;
00333     PINDEX secondary;
00334 };
00335 
00336 
00337 class PHTTPFieldArray : public PHTTPCompositeField
00338 {
00339   PCLASSINFO(PHTTPFieldArray, PHTTPCompositeField)
00340   public:
00341     PHTTPFieldArray(
00342       PHTTPField * baseField,
00343       PBoolean ordered,
00344       PINDEX fixedSize = 0
00345     );
00346 
00347     ~PHTTPFieldArray();
00348 
00349 
00350     virtual PHTTPField * NewField() const;
00351 
00352     virtual void ExpandFieldNames(PString & text, PINDEX start, PINDEX & finish) const;
00353 
00354     virtual void GetHTMLTag(
00355       PHTML & html    // HTML to receive the field info.
00356     ) const;
00357 
00358     virtual void LoadFromConfig(
00359       PConfig & cfg   // Configuration for value transfer.
00360     );
00361     virtual void SaveToConfig(
00362       PConfig & cfg   // Configuration for value transfer.
00363     ) const;
00364 
00365 
00366     virtual void SetAllValues(
00367       const PStringToString & data   // New value for the field.
00368     );
00369 
00370     virtual PINDEX GetSize() const;
00371     void SetSize(PINDEX newSize);
00372 
00373     PStringArray GetStrings(
00374       PConfig & cfg   
00375     );
00376 
00377     void SetStrings(
00378       PConfig & cfg,   
00379       const PStringArray & values 
00380     );
00381 
00382   protected:
00383     void AddBlankField();
00384     void AddArrayControlBox(PHTML & html, PINDEX fld) const;
00385     void SetArrayFieldName(PINDEX idx) const;
00386 
00387     PHTTPField * baseField;
00388     PBoolean orderedArray;
00389     PBoolean canAddElements;
00390 };
00391 
00392 
00393 class PHTTPStringField : public PHTTPField
00394 {
00395   PCLASSINFO(PHTTPStringField, PHTTPField)
00396   public:
00397     PHTTPStringField(
00398       const char * name,
00399       PINDEX size,
00400       const char * initVal = NULL,
00401       const char * help = NULL
00402     );
00403     PHTTPStringField(
00404       const char * name,
00405       const char * title,
00406       PINDEX size,
00407       const char * initVal = NULL,
00408       const char * help = NULL
00409     );
00410 
00411     virtual PHTTPField * NewField() const;
00412 
00413     virtual void GetHTMLTag(
00414       PHTML & html    
00415     ) const;
00416 
00417     virtual PString GetValue(PBoolean dflt = PFalse) const;
00418 
00419     virtual void SetValue(
00420       const PString & newVal
00421     );
00422 
00423 
00424   protected:
00425     PString value;
00426     PString initialValue;
00427     PINDEX size;
00428 };
00429 
00430 
00431 class PHTTPPasswordField : public PHTTPStringField
00432 {
00433   PCLASSINFO(PHTTPPasswordField, PHTTPStringField)
00434   public:
00435     PHTTPPasswordField(
00436       const char * name,
00437       PINDEX size,
00438       const char * initVal = NULL,
00439       const char * help = NULL
00440     );
00441     PHTTPPasswordField(
00442       const char * name,
00443       const char * title,
00444       PINDEX size,
00445       const char * initVal = NULL,
00446       const char * help = NULL
00447     );
00448 
00449     virtual PHTTPField * NewField() const;
00450 
00451     virtual void GetHTMLTag(
00452       PHTML & html    
00453     ) const;
00454 
00455     virtual PString GetValue(PBoolean dflt = PFalse) const;
00456 
00457     virtual void SetValue(
00458       const PString & newVal
00459     );
00460 
00461     static PString Decrypt(const PString & pword);
00462 };
00463 
00464 
00465 class PHTTPIntegerField : public PHTTPField
00466 {
00467   PCLASSINFO(PHTTPIntegerField, PHTTPField)
00468   public:
00469     PHTTPIntegerField(
00470       const char * name,
00471       int low, int high,
00472       int initVal = 0,
00473       const char * units = NULL,
00474       const char * help = NULL
00475     );
00476     PHTTPIntegerField(
00477       const char * name,
00478       const char * title,
00479       int low, int high,
00480       int initVal = 0,
00481       const char * units = NULL,
00482       const char * help = NULL
00483     );
00484 
00485     virtual PHTTPField * NewField() const;
00486 
00487     virtual void GetHTMLTag(
00488       PHTML & html    
00489     ) const;
00490 
00491     virtual PString GetValue(PBoolean dflt = PFalse) const;
00492 
00493     virtual void SetValue(
00494       const PString & newVal
00495     );
00496 
00497     virtual void LoadFromConfig(
00498       PConfig & cfg   
00499     );
00500     virtual void SaveToConfig(
00501       PConfig & cfg   
00502     ) const;
00503 
00504     virtual PBoolean Validated(
00505       const PString & newVal,
00506       PStringStream & msg
00507     ) const;
00508 
00509 
00510   protected:
00511     int low, high, value;
00512     int initialValue;
00513     PString units;
00514 };
00515 
00516 
00517 class PHTTPBooleanField : public PHTTPField
00518 {
00519   PCLASSINFO(PHTTPBooleanField, PHTTPField)
00520   public:
00521     PHTTPBooleanField(
00522       const char * name,
00523       PBoolean initVal = PFalse,
00524       const char * help = NULL
00525     );
00526     PHTTPBooleanField(
00527       const char * name,
00528       const char * title,
00529       PBoolean initVal = PFalse,
00530       const char * help = NULL
00531     );
00532 
00533     virtual PHTTPField * NewField() const;
00534 
00535     virtual void GetHTMLTag(
00536       PHTML & html    
00537     ) const;
00538 
00539     virtual PString GetHTMLInput(
00540       const PString & input
00541     ) const;
00542 
00543     virtual PString GetValue(PBoolean dflt = PFalse) const;
00544 
00545     virtual void SetValue(
00546       const PString & newVal
00547     );
00548 
00549     virtual void LoadFromConfig(
00550       PConfig & cfg   
00551     );
00552     virtual void SaveToConfig(
00553       PConfig & cfg   
00554     ) const;
00555 
00556 
00557   protected:
00558     PBoolean value, initialValue;
00559 };
00560 
00561 
00562 class PHTTPRadioField : public PHTTPField
00563 {
00564   PCLASSINFO(PHTTPRadioField, PHTTPField)
00565   public:
00566     PHTTPRadioField(
00567       const char * name,
00568       const PStringArray & valueArray,
00569       PINDEX initVal = 0,
00570       const char * help = NULL
00571     );
00572     PHTTPRadioField(
00573       const char * name,
00574       const PStringArray & valueArray,
00575       const PStringArray & titleArray,
00576       PINDEX initVal = 0,
00577       const char * help = NULL
00578     );
00579     PHTTPRadioField(
00580       const char * name,
00581       PINDEX count,
00582       const char * const * valueStrings,
00583       PINDEX initVal = 0,
00584       const char * help = NULL
00585     );
00586     PHTTPRadioField(
00587       const char * name,
00588       PINDEX count,
00589       const char * const * valueStrings,
00590       const char * const * titleStrings,
00591       PINDEX initVal = 0,
00592       const char * help = NULL
00593     );
00594     PHTTPRadioField(
00595       const char * name,
00596       const char * groupTitle,
00597       const PStringArray & valueArray,
00598       PINDEX initVal = 0,
00599       const char * help = NULL
00600     );
00601     PHTTPRadioField(
00602       const char * name,
00603       const char * groupTitle,
00604       const PStringArray & valueArray,
00605       const PStringArray & titleArray,
00606       PINDEX initVal = 0,
00607       const char * help = NULL
00608     );
00609     PHTTPRadioField(
00610       const char * name,
00611       const char * groupTitle,
00612       PINDEX count,
00613       const char * const * valueStrings,
00614       PINDEX initVal = 0,
00615       const char * help = NULL
00616     );
00617     PHTTPRadioField(
00618       const char * name,
00619       const char * groupTitle,
00620       PINDEX count,
00621       const char * const * valueStrings,
00622       const char * const * titleStrings,
00623       PINDEX initVal = 0,
00624       const char * help = NULL
00625     );
00626 
00627     virtual PHTTPField * NewField() const;
00628 
00629     virtual void GetHTMLTag(
00630       PHTML & html    
00631     ) const;
00632 
00633     virtual PString GetHTMLInput(
00634       const PString & input
00635     ) const;
00636 
00637     virtual PString GetValue(PBoolean dflt = PFalse) const;
00638 
00639     virtual void SetValue(
00640       const PString & newVal
00641     );
00642 
00643 
00644   protected:
00645     PStringArray values;
00646     PStringArray titles;
00647     PString value;
00648     PString initialValue;
00649 };
00650 
00651 
00652 class PHTTPSelectField : public PHTTPField
00653 {
00654   PCLASSINFO(PHTTPSelectField, PHTTPField)
00655   public:
00656     PHTTPSelectField(
00657       const char * name,
00658       const PStringArray & valueArray,
00659       PINDEX initVal = 0,
00660       const char * help = NULL
00661     );
00662     PHTTPSelectField(
00663       const char * name,
00664       PINDEX count,
00665       const char * const * valueStrings,
00666       PINDEX initVal = 0,
00667       const char * help = NULL
00668     );
00669     PHTTPSelectField(
00670       const char * name,
00671       const char * title,
00672       const PStringArray & valueArray,
00673       PINDEX initVal = 0,
00674       const char * help = NULL
00675     );
00676     PHTTPSelectField(
00677       const char * name,
00678       const char * title,
00679       PINDEX count,
00680       const char * const * valueStrings,
00681       PINDEX initVal = 0,
00682       const char * help = NULL
00683     );
00684 
00685     virtual PHTTPField * NewField() const;
00686 
00687     virtual void GetHTMLTag(
00688       PHTML & html    
00689     ) const;
00690 
00691     virtual PString GetValue(PBoolean dflt = PFalse) const;
00692 
00693     virtual void SetValue(
00694       const PString & newVal
00695     );
00696 
00697 
00698     PStringArray values;
00699 
00700 
00701   protected:
00702     PString value;
00703     PINDEX initialValue;
00704 };
00705 
00706 
00708 // PHTTPForm
00709 
00710 class PHTTPForm : public PHTTPString
00711 {
00712   PCLASSINFO(PHTTPForm, PHTTPString)
00713   public:
00714     PHTTPForm(
00715       const PURL & url
00716     );
00717     PHTTPForm(
00718       const PURL & url,
00719       const PHTTPAuthority & auth
00720     );
00721     PHTTPForm(
00722       const PURL & url,
00723       const PString & html
00724     );
00725     PHTTPForm(
00726       const PURL & url,
00727       const PString & html,
00728       const PHTTPAuthority & auth
00729     );
00730 
00731 
00732     virtual void OnLoadedText(
00733       PHTTPRequest & request,    
00734       PString & text             
00735     );
00736     virtual PBoolean Post(
00737       PHTTPRequest & request,       
00738       const PStringToString & data, 
00739       PHTML & replyMessage          
00740     );
00741 
00742 
00743     PHTTPField * Add(
00744       PHTTPField * fld
00745     );
00746     void RemoveAllFields()
00747       { fields.RemoveAll(); fieldNames.RemoveAll(); }
00748 
00749     enum BuildOptions {
00750       CompleteHTML,
00751       InsertIntoForm,
00752       InsertIntoHTML
00753     };
00754 
00755     void BuildHTML(
00756       const char * heading
00757     );
00758     void BuildHTML(
00759       const PString & heading
00760     );
00761     void BuildHTML(
00762       PHTML & html,
00763       BuildOptions option = CompleteHTML
00764     );
00765 
00766 
00767   protected:
00768     PHTTPCompositeField fields;
00769     PStringSet fieldNames;
00770 };
00771 
00772 
00774 // PHTTPConfig
00775 
00776 class PHTTPConfig : public PHTTPForm
00777 {
00778   PCLASSINFO(PHTTPConfig, PHTTPForm)
00779   public:
00780     PHTTPConfig(
00781       const PURL & url,
00782       const PString & section
00783     );
00784     PHTTPConfig(
00785       const PURL & url,
00786       const PString & section,
00787       const PHTTPAuthority & auth
00788     );
00789     PHTTPConfig(
00790       const PURL & url,
00791       const PString & section,
00792       const PString & html
00793     );
00794     PHTTPConfig(
00795       const PURL & url,
00796       const PString & section,
00797       const PString & html,
00798       const PHTTPAuthority & auth
00799     );
00800 
00801     virtual void OnLoadedText(
00802       PHTTPRequest & request,    
00803       PString & text             
00804     );
00805     virtual PBoolean Post(
00806       PHTTPRequest & request,       
00807       const PStringToString & data, 
00808       PHTML & replyMessage          
00809     );
00810 
00811 
00814     void LoadFromConfig();
00815 
00821     const PString & GetConfigSection() const { return section; }
00822 
00823     void SetConfigSection(
00824       const PString & sect   
00825     ) { section = sect; }
00826     // Set the configuration file section.
00827 
00832     PHTTPField * AddSectionField(
00833       PHTTPField * sectionFld,     
00834       const char * prefix = NULL,  
00835       const char * suffix = NULL   
00836     );
00837 
00841     void AddNewKeyFields(
00842       PHTTPField * keyFld,  
00843       PHTTPField * valFld   
00844     );
00845 
00846 
00847   protected:
00848     PString section;
00849     PString sectionPrefix;
00850     PString sectionSuffix;
00851     PHTTPField * sectionField;
00852     PHTTPField * keyField;
00853     PHTTPField * valField;
00854 
00855   private:
00856     void Construct();
00857 };
00858 
00859 
00861 // PHTTPConfigSectionList
00862 
00863 class PHTTPConfigSectionList : public PHTTPString
00864 {
00865   PCLASSINFO(PHTTPConfigSectionList, PHTTPString)
00866   public:
00867     PHTTPConfigSectionList(
00868       const PURL & url,
00869       const PHTTPAuthority & auth,
00870       const PString & sectionPrefix,
00871       const PString & additionalValueName,
00872       const PURL & editSection,
00873       const PURL & newSection,
00874       const PString & newSectionTitle,
00875       PHTML & heading
00876     );
00877 
00878     virtual void OnLoadedText(
00879       PHTTPRequest & request,    
00880       PString & text             
00881     );
00882     virtual PBoolean Post(
00883       PHTTPRequest & request,       
00884       const PStringToString & data, 
00885       PHTML & replyMessage          
00886     );
00887 
00888   protected:
00889     PString sectionPrefix;
00890     PString additionalValueName;
00891     PString newSectionLink;
00892     PString newSectionTitle;
00893     PString editSectionLink;
00894 };
00895 
00896 
00897 #endif // P_HTTPFORMS
00898 
00899 #endif // _HTTPFORM_H
00900 
00901 
00902 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1