ContentLine.cpp

00001 /*
00002     libvcard - vCard parsing library for vCard version 3.0
00003 
00004     Copyright (C) 1999 Rik Hemsley rik@kde.org
00005     
00006   Permission is hereby granted, free of charge, to any person obtaining a copy
00007   of this software and associated documentation files (the "Software"), to
00008   deal in the Software without restriction, including without limitation the
00009   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00010   sell copies of the Software, and to permit persons to whom the Software is
00011   furnished to do so, subject to the following conditions:
00012 
00013   The above copyright notice and this permission notice shall be included in
00014   all copies or substantial portions of the Software.
00015 
00016   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019   AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00020   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00021   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 */
00023 
00024 #include <qcstring.h>
00025 #include <qstrlist.h>
00026 #include <qregexp.h>
00027 
00028 #include <kdebug.h>
00029 
00030 #include <VCardAdrParam.h>
00031 #include <VCardAgentParam.h>
00032 #include <VCardDateParam.h>
00033 #include <VCardEmailParam.h>
00034 #include <VCardImageParam.h>
00035 #include <VCardSourceParam.h>
00036 #include <VCardTelParam.h>
00037 #include <VCardTextBinParam.h>
00038 #include <VCardTextParam.h>
00039 
00040 #include <VCardAdrValue.h>
00041 #include <VCardAgentValue.h>
00042 #include <VCardDateValue.h>
00043 #include <VCardImageValue.h>
00044 #include <VCardTextValue.h>
00045 #include <VCardTextBinValue.h>
00046 #include <VCardLangValue.h>
00047 #include <VCardNValue.h>
00048 #include <VCardURIValue.h>
00049 #include <VCardSoundValue.h>
00050 #include <VCardClassValue.h>
00051 #include <VCardFloatValue.h>
00052 #include <VCardOrgValue.h>
00053 #include <VCardTelValue.h>
00054 #include <VCardTextListValue.h>
00055 #include <VCardUTCValue.h>
00056 #include <VCardGeoValue.h>
00057 
00058 #include <VCardRToken.h>
00059 #include <VCardContentLine.h>
00060 
00061 #include <VCardEntity.h>
00062 #include <VCardEnum.h>
00063 #include <VCardDefines.h>
00064 
00065 using namespace VCARD;
00066 
00067 ContentLine::ContentLine()
00068     :   Entity(),
00069         value_(0),
00070         paramType_( ParamUnknown ),
00071         valueType_( ValueUnknown ),
00072         entityType_( EntityUnknown )
00073 {
00074 }
00075 
00076 ContentLine::ContentLine(const ContentLine & x)
00077     :   Entity(x),
00078                 group_ (x.group_),
00079                 name_ (x.name_),
00080         paramList_(x.paramList_),
00081         value_(x.value_->clone()),
00082         paramType_( x.paramType_ ),
00083         valueType_( x.valueType_ ),
00084         entityType_( x.entityType_ )
00085 {
00086 }
00087 
00088 ContentLine::ContentLine(const QCString & s)
00089     :   Entity(s),
00090         value_(0),
00091         paramType_( ParamUnknown ),
00092         valueType_( ValueUnknown ),
00093         entityType_( EntityUnknown )
00094 {
00095 }
00096 
00097     ContentLine &
00098 ContentLine::operator = (ContentLine & x)
00099 {
00100     if (*this == x) return *this;
00101     
00102     paramList_ = x.paramList();
00103     value_ = x.value_->clone();
00104 
00105     Entity::operator = (x);
00106     return *this;
00107 }
00108 
00109     ContentLine &
00110 ContentLine::operator = (const QCString & s)
00111 {
00112     Entity::operator = (s);
00113     delete value_;
00114     value_ = 0;
00115     return *this;
00116 }
00117 
00118     bool
00119 ContentLine::operator == (ContentLine & x)
00120 {
00121     x.parse();
00122     
00123     QPtrListIterator<Param> it(x.paramList());
00124     
00125     if (!paramList_.find(it.current()))
00126         return false;
00127 
00128     return true;
00129 }
00130 
00131 ContentLine::~ContentLine()
00132 {
00133     delete value_;
00134     value_ = 0;
00135 }
00136 
00137     void
00138 ContentLine::_parse()
00139 {
00140     vDebug("parse");
00141     
00142     // Unqote newlines
00143     strRep_ = strRep_.replace( QRegExp( "\\\\n" ), "\n" );
00144     
00145     int split = strRep_.find(':');
00146     
00147     if (split == -1) { // invalid content line
00148         vDebug("No ':'");
00149         return;
00150     }
00151     
00152     QCString firstPart(strRep_.left(split));
00153     QCString valuePart(strRep_.mid(split + 1));
00154     
00155     split = firstPart.find('.');
00156     
00157     if (split != -1) {
00158         group_      = firstPart.left(split);
00159         firstPart   = firstPart.mid(split + 1);
00160     }
00161     
00162     vDebug("Group == " + group_);
00163     vDebug("firstPart == " + firstPart);
00164     vDebug("valuePart == " + valuePart);
00165     
00166     // Now we have the group, the name and param list together and the value.
00167     
00168     QStrList l;
00169     
00170     RTokenise(firstPart, ";", l);
00171     
00172     if (l.count() == 0) {// invalid - no name !
00173         vDebug("No name for this content line !");
00174         return;
00175     }
00176     
00177     name_ = l.at(0);
00178 
00179     // Now we have the name, so the rest of 'l' is the params.
00180     // Remove the name part.
00181     l.remove(0u);
00182     
00183     entityType_ = EntityNameToEntityType(name_);
00184     paramType_  = EntityTypeToParamType(entityType_);
00185     
00186     unsigned int i = 0;
00187     
00188     // For each parameter, create a new parameter of the correct type.
00189 
00190     QStrListIterator it(l);
00191     
00192     for (; it.current(); ++it, i++) {
00193 
00194         QCString str = *it;
00195 
00196         split = str.find("=");
00197         if (split < 0 ) {
00198             vDebug("No '=' in parameter.");
00199             continue;
00200         }
00201         
00202         QCString paraName = str.left(split);
00203         QCString paraValue = str.mid(split + 1);
00204         
00205         QStrList paraValues;
00206         RTokenise(paraValue, ",", paraValues);
00207         
00208         QStrListIterator it2( paraValues );
00209         
00210         for(; it2.current(); ++it2) {       
00211         
00212             Param *p = new Param;
00213             p->setName( paraName );
00214             p->setValue( *it2 );
00215     
00216             paramList_.append(p);
00217         }
00218     }
00219 
00220     // Create a new value of the correct type.
00221 
00222     valueType_ = EntityTypeToValueType(entityType_);
00223     
00224 //  kdDebug(5710) << "valueType: " << valueType_ << endl;
00225     
00226     switch (valueType_) {
00227         
00228         case ValueSound:    value_ = new SoundValue;    break;
00229         case ValueAgent:    value_ = new AgentValue;    break;
00230         case ValueAddress:  value_ = new AdrValue;      break;
00231         case ValueTel:      value_ = new TelValue;      break;
00232         case ValueTextBin:  value_ = new TextBinValue;  break;
00233         case ValueOrg:      value_ = new OrgValue;      break;
00234         case ValueN:        value_ = new NValue;        break;
00235         case ValueUTC:      value_ = new UTCValue;      break;
00236         case ValueURI:      value_ = new URIValue;      break;
00237         case ValueClass:    value_ = new ClassValue;    break;
00238         case ValueFloat:    value_ = new FloatValue;    break;
00239         case ValueImage:    value_ = new ImageValue;    break;
00240         case ValueDate:     value_ = new DateValue;     break;
00241         case ValueTextList: value_ = new TextListValue; break;
00242         case ValueGeo:      value_ = new GeoValue;      break;
00243         case ValueText:
00244         case ValueUnknown:
00245         default:        value_ = new TextValue;     break;
00246     }
00247     
00248     *value_ = valuePart;
00249 }
00250 
00251     void
00252 ContentLine::_assemble()
00253 {
00254     vDebug("Assemble (argl) - my name is \"" + name_ + "\"");
00255     strRep_.truncate(0);
00256 
00257     QCString line;
00258     
00259     if (!group_.isEmpty())
00260         line += group_ + '.';
00261     
00262     line += name_;
00263 
00264     vDebug("Adding parameters");
00265     ParamListIterator it(paramList_);
00266     
00267     for (; it.current(); ++it)
00268         line += ";" + it.current()->asString();
00269     
00270     vDebug("Adding value");
00271     if (value_ != 0)
00272         line += ":" + value_->asString();
00273     else {
00274         vDebug("No value");
00275         }
00276 
00277     // Quote newlines
00278     line = line.replace( QRegExp( "\n" ), "\\n" );
00279         
00280     // Fold lines longer than 72 chars
00281     const int maxLen = 72;
00282     uint cursor = 0;
00283     while( line.length() > ( cursor + 1 ) * maxLen ) {
00284         strRep_ += line.mid( cursor * maxLen, maxLen );
00285         strRep_ += "\r\n ";
00286         ++cursor;
00287     }
00288     strRep_ += line.mid( cursor * maxLen );
00289 }
00290 
00291     void
00292 ContentLine::clear()
00293 {
00294     group_.truncate(0);
00295     name_.truncate(0);
00296     paramList_.clear();
00297         delete value_;
00298     value_ = 0;
00299     paramType_ = ParamUnknown;
00300     valueType_ = ValueUnknown;
00301     entityType_ = EntityUnknown;
00302 }
KDE Home | KDE Accessibility Home | Description of Access Keys