entry.cpp
00001 /* 00002 This file is part of KOrganizer. 00003 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "entry.h" 00022 00023 #include <qptrdict.h> 00024 #include <qwindowdefs.h> 00025 00026 #include <kglobal.h> 00027 #include <klocale.h> 00028 00029 using namespace KNS; 00030 00031 // BCI for KDE 3.5 only 00032 00033 class EntryPrivate 00034 { 00035 public: 00036 EntryPrivate(){} 00037 QString mEmail; 00038 QMap<QString,QString> mNameMap; 00039 }; 00040 00041 static QPtrDict<EntryPrivate> *d_ptr = 0; 00042 00043 static EntryPrivate *d(const Entry *e) 00044 { 00045 if(!d_ptr) 00046 { 00047 d_ptr = new QPtrDict<EntryPrivate>(); 00048 d_ptr->setAutoDelete(true); 00049 } 00050 EntryPrivate *ret = d_ptr->find((void*)e); 00051 if(!ret) 00052 { 00053 ret = new EntryPrivate(); 00054 d_ptr->replace((void*)e, ret); 00055 } 00056 return ret; 00057 } 00058 00059 QString Entry::authorEmail() const 00060 { 00061 return d(this)->mEmail; 00062 } 00063 00064 void Entry::setAuthorEmail( const QString& email ) 00065 { 00066 d(this)->mEmail = email; 00067 } 00068 00069 QString Entry::name( const QString &lang ) const 00070 { 00071 if ( d(this)->mNameMap.isEmpty() ) return QString::null; 00072 00073 if ( !d(this)->mNameMap[ lang ].isEmpty() ) return d(this)->mNameMap[ lang ]; 00074 else { 00075 QStringList langs = KGlobal::locale()->languageList(); 00076 for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it) 00077 if( !d(this)->mNameMap[ *it ].isEmpty() ) return d(this)->mNameMap[ *it ]; 00078 } 00079 if ( !d(this)->mNameMap[ QString::null ].isEmpty() ) return d(this)->mNameMap[ QString::null ]; 00080 else return *(mSummaryMap.begin()); 00081 } 00082 00083 void Entry::setName( const QString &name, const QString &lang ) 00084 { 00085 d(this)->mNameMap.insert( lang, name ); 00086 00087 if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang ); 00088 } 00089 00090 // BCI part ends here 00091 00092 Entry::Entry() : 00093 mRelease( 0 ), mReleaseDate( QDate::currentDate() ), mRating( 0 ), 00094 mDownloads( 0 ) 00095 { 00096 } 00097 00098 Entry::Entry( const QDomElement &e ) : 00099 mRelease( 0 ), mRating( 0 ), mDownloads( 0 ) 00100 { 00101 parseDomElement( e ); 00102 } 00103 00104 Entry::~Entry() 00105 { 00106 if (d_ptr) 00107 { 00108 EntryPrivate *p = d_ptr->find(this); 00109 if (p) 00110 d_ptr->remove(p); 00111 00112 if (d_ptr->isEmpty()) 00113 { 00114 delete d_ptr; 00115 d_ptr = 0L; 00116 } 00117 } 00118 } 00119 00120 00121 void Entry::setName( const QString &name ) 00122 { 00123 mName = name; 00124 } 00125 00126 QString Entry::name() const 00127 { 00128 return mName; 00129 } 00130 00131 00132 void Entry::setType( const QString &type ) 00133 { 00134 mType = type; 00135 } 00136 00137 QString Entry::type() const 00138 { 00139 return mType; 00140 } 00141 00142 00143 void Entry::setAuthor( const QString &author ) 00144 { 00145 mAuthor = author; 00146 } 00147 00148 QString Entry::author() const 00149 { 00150 return mAuthor; 00151 } 00152 00153 00154 void Entry::setLicence( const QString &license ) 00155 { 00156 mLicence = license; 00157 } 00158 00159 QString Entry::license() const 00160 { 00161 return mLicence; 00162 } 00163 00164 00165 void Entry::setSummary( const QString &text, const QString &lang ) 00166 { 00167 mSummaryMap.insert( lang, text ); 00168 00169 if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang ); 00170 } 00171 00172 QString Entry::summary( const QString &lang ) const 00173 { 00174 if ( mSummaryMap.isEmpty() ) return QString::null; 00175 00176 if ( !mSummaryMap[ lang ].isEmpty() ) return mSummaryMap[ lang ]; 00177 else { 00178 QStringList langs = KGlobal::locale()->languageList(); 00179 for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it) 00180 if( !mSummaryMap[ *it ].isEmpty() ) return mSummaryMap[ *it ]; 00181 } 00182 if ( !mSummaryMap[ QString::null ].isEmpty() ) return mSummaryMap[ QString::null ]; 00183 else return *(mSummaryMap.begin()); 00184 } 00185 00186 00187 void Entry::setVersion( const QString &version ) 00188 { 00189 mVersion = version; 00190 } 00191 00192 QString Entry::version() const 00193 { 00194 return mVersion; 00195 } 00196 00197 00198 void Entry::setRelease( int release ) 00199 { 00200 mRelease = release; 00201 } 00202 00203 int Entry::release() const 00204 { 00205 return mRelease; 00206 } 00207 00208 00209 void Entry::setReleaseDate( const QDate &d ) 00210 { 00211 mReleaseDate = d; 00212 } 00213 00214 QDate Entry::releaseDate() const 00215 { 00216 return mReleaseDate; 00217 } 00218 00219 00220 void Entry::setPayload( const KURL &url, const QString &lang ) 00221 { 00222 mPayloadMap.insert( lang, url ); 00223 00224 if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang ); 00225 } 00226 00227 KURL Entry::payload( const QString &lang ) const 00228 { 00229 KURL payload = mPayloadMap[ lang ]; 00230 if ( payload.isEmpty() ) { 00231 QStringList langs = KGlobal::locale()->languageList(); 00232 for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it) 00233 if( !mPayloadMap[ *it ].isEmpty() ) return mPayloadMap[ *it ]; 00234 } 00235 if ( payload.isEmpty() ) payload = mPayloadMap [ QString::null ]; 00236 if ( payload.isEmpty() && !mPayloadMap.isEmpty() ) { 00237 payload = *(mPayloadMap.begin()); 00238 } 00239 return payload; 00240 } 00241 00242 00243 void Entry::setPreview( const KURL &url, const QString &lang ) 00244 { 00245 mPreviewMap.insert( lang, url ); 00246 00247 if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang ); 00248 } 00249 00250 KURL Entry::preview( const QString &lang ) const 00251 { 00252 KURL preview = mPreviewMap[ lang ]; 00253 if ( preview.isEmpty() ) { 00254 QStringList langs = KGlobal::locale()->languageList(); 00255 for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it) 00256 if( !mPreviewMap[ *it ].isEmpty() ) return mPreviewMap[ *it ]; 00257 } 00258 if ( preview.isEmpty() ) preview = mPreviewMap [ QString::null ]; 00259 if ( preview.isEmpty() && !mPreviewMap.isEmpty() ) { 00260 preview = *(mPreviewMap.begin()); 00261 } 00262 return preview; 00263 } 00264 00265 00266 void Entry::setRating( int rating ) 00267 { 00268 mRating = rating; 00269 } 00270 00271 int Entry::rating() 00272 { 00273 return mRating; 00274 } 00275 00276 00277 void Entry::setDownloads( int downloads ) 00278 { 00279 mDownloads = downloads; 00280 } 00281 00282 int Entry::downloads() 00283 { 00284 return mDownloads; 00285 } 00286 00287 QString Entry::fullName() 00288 { 00289 if ( version().isEmpty() ) 00290 return name(); 00291 else 00292 return name() + "-" + version() + "-" + QString::number( release() ); 00293 } 00294 00295 QStringList Entry::langs() 00296 { 00297 return mLangs; 00298 } 00299 00300 void Entry::parseDomElement( const QDomElement &element ) 00301 { 00302 if ( element.tagName() != "stuff" ) return; 00303 mType = element.attribute("type"); 00304 00305 QDomNode n; 00306 for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) { 00307 QDomElement e = n.toElement(); 00308 if ( e.tagName() == "name" ) 00309 { 00310 QString lang = e.attribute( "lang" ); 00311 setName( e.text().stripWhiteSpace(), lang ); 00312 if(lang.isNull()) setName( e.text().stripWhiteSpace() ); /* primary key - no i18n */ 00313 } 00314 if ( e.tagName() == "author" ) { 00315 setAuthor( e.text().stripWhiteSpace() ); 00316 QString email = e.attribute( "email" ); 00317 setAuthorEmail( email ); 00318 } 00319 if ( e.tagName() == "email" ) setAuthorEmail( e.text().stripWhiteSpace() ); /* kde-look; change on server! */ 00320 if ( e.tagName() == "licence" ) setLicence( e.text().stripWhiteSpace() ); 00321 if ( e.tagName() == "summary" ) { 00322 QString lang = e.attribute( "lang" ); 00323 setSummary( e.text().stripWhiteSpace(), lang ); 00324 } 00325 if ( e.tagName() == "version" ) setVersion( e.text().stripWhiteSpace() ); 00326 if ( e.tagName() == "release" ) setRelease( e.text().toInt() ); 00327 if ( e.tagName() == "releasedate" ) { 00328 QDate date = QDate::fromString( e.text().stripWhiteSpace(), Qt::ISODate ); 00329 setReleaseDate( date ); 00330 } 00331 if ( e.tagName() == "preview" ) { 00332 QString lang = e.attribute( "lang" ); 00333 setPreview( KURL( e.text().stripWhiteSpace() ), lang ); 00334 } 00335 if ( e.tagName() == "payload" ) { 00336 QString lang = e.attribute( "lang" ); 00337 setPayload( KURL( e.text().stripWhiteSpace() ), lang ); 00338 } 00339 if ( e.tagName() == "rating" ) setRating( e.text().toInt() ); 00340 if ( e.tagName() == "downloads" ) setDownloads( e.text().toInt() ); 00341 } 00342 } 00343 00344 QDomElement Entry::createDomElement( QDomDocument &doc, 00345 QDomElement &parent ) 00346 { 00347 QDomElement entry = doc.createElement( "stuff" ); 00348 entry.setAttribute("type", mType); 00349 parent.appendChild( entry ); 00350 00351 addElement( doc, entry, "name", name() ); 00352 addElement( doc, entry, "author", author() ); 00353 addElement( doc, entry, "email", authorEmail() ); 00354 addElement( doc, entry, "licence", license() ); 00355 addElement( doc, entry, "version", version() ); 00356 addElement( doc, entry, "release", QString::number( release() ) ); 00357 addElement( doc, entry, "rating", QString::number( rating() ) ); 00358 addElement( doc, entry, "downloads", QString::number( downloads() ) ); 00359 00360 addElement( doc, entry, "releasedate", 00361 releaseDate().toString( Qt::ISODate ) ); 00362 00363 QStringList ls = langs(); 00364 QStringList::ConstIterator it; 00365 for( it = ls.begin(); it != ls.end(); ++it ) { 00366 QDomElement e = addElement( doc, entry, "summary", summary( *it ) ); 00367 e.setAttribute( "lang", *it ); 00368 e = addElement( doc, entry, "preview", preview( *it ).url() ); 00369 e.setAttribute( "lang", *it ); 00370 e = addElement( doc, entry, "payload", payload( *it ).url() ); 00371 e.setAttribute( "lang", *it ); 00372 } 00373 00374 return entry; 00375 } 00376 00377 QDomElement Entry::addElement( QDomDocument &doc, QDomElement &parent, 00378 const QString &tag, const QString &value ) 00379 { 00380 QDomElement n = doc.createElement( tag ); 00381 n.appendChild( doc.createTextNode( value ) ); 00382 parent.appendChild( n ); 00383 00384 return n; 00385 }