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 void cleanup_d_ptr()
00044 {
00045   delete d_ptr;
00046   d_ptr = 0; // not in BIC guide - add there
00047 }
00048 
00049 static EntryPrivate *d(const Entry *e)
00050 {
00051   if(!d_ptr)
00052   {
00053     d_ptr = new QPtrDict<EntryPrivate>();
00054     qAddPostRoutine(cleanup_d_ptr);
00055   }
00056   EntryPrivate *ret = d_ptr->find((void*)e);
00057   if(!ret)
00058   {
00059     ret = new EntryPrivate();
00060     d_ptr->replace((void*)e, ret);
00061   }
00062   return ret;
00063 }
00064 
00065 QString Entry::authorEmail() const
00066 {
00067   return d(this)->mEmail;
00068 }
00069 
00070 void Entry::setAuthorEmail( const QString& email )
00071 {
00072   d(this)->mEmail = email;
00073 }
00074 
00075 QString Entry::name( const QString &lang ) const
00076 {
00077   if ( d(this)->mNameMap.isEmpty() ) return QString::null;
00078 
00079   if ( !d(this)->mNameMap[ lang ].isEmpty() ) return d(this)->mNameMap[ lang ];
00080   else {
00081     QStringList langs = KGlobal::locale()->languageList();
00082     for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
00083       if( !d(this)->mNameMap[ *it ].isEmpty() ) return d(this)->mNameMap[ *it ];
00084   }
00085   if ( !d(this)->mNameMap[ QString::null ].isEmpty() ) return d(this)->mNameMap[ QString::null ];
00086   else return *(mSummaryMap.begin());
00087 }
00088 
00089 void Entry::setName( const QString &name, const QString &lang )
00090 {
00091   d(this)->mNameMap.insert( lang, name );
00092 
00093   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00094 }
00095 
00096 // BCI part ends here
00097 
00098 Entry::Entry() :
00099   mRelease( 0 ), mReleaseDate( QDate::currentDate() ), mRating( 0 ),
00100   mDownloads( 0 )
00101 {
00102 }
00103 
00104 Entry::Entry( const QDomElement &e ) :
00105   mRelease( 0 ), mRating( 0 ), mDownloads( 0 )
00106 {
00107   parseDomElement( e );
00108 }
00109 
00110 Entry::~Entry()
00111 {
00112 }
00113 
00114 
00115 void Entry::setName( const QString &name )
00116 {
00117   mName = name;
00118 }
00119 
00120 QString Entry::name() const
00121 {
00122   return mName;
00123 }
00124 
00125 
00126 void Entry::setType( const QString &type )
00127 {
00128   mType = type;
00129 }
00130 
00131 QString Entry::type() const
00132 {
00133   return mType;
00134 }
00135 
00136 
00137 void Entry::setAuthor( const QString &author )
00138 {
00139   mAuthor = author;
00140 }
00141 
00142 QString Entry::author() const
00143 {
00144   return mAuthor;
00145 }
00146 
00147 
00148 void Entry::setLicence( const QString &license )
00149 {
00150   mLicence = license;
00151 }
00152 
00153 QString Entry::license() const
00154 {
00155   return mLicence;
00156 }
00157 
00158 
00159 void Entry::setSummary( const QString &text, const QString &lang )
00160 {
00161   mSummaryMap.insert( lang, text );
00162 
00163   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00164 }
00165 
00166 QString Entry::summary( const QString &lang ) const
00167 {
00168   if ( mSummaryMap.isEmpty() ) return QString::null;
00169 
00170   if ( !mSummaryMap[ lang ].isEmpty() ) return mSummaryMap[ lang ];
00171   else {
00172     QStringList langs = KGlobal::locale()->languageList();
00173     for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
00174       if( !mSummaryMap[ *it ].isEmpty() ) return mSummaryMap[ *it ];
00175   }
00176   if ( !mSummaryMap[ QString::null ].isEmpty() ) return mSummaryMap[ QString::null ];
00177   else return *(mSummaryMap.begin());
00178 }
00179 
00180 
00181 void Entry::setVersion( const QString &version )
00182 {
00183   mVersion = version;
00184 }
00185 
00186 QString Entry::version() const
00187 {
00188   return mVersion;
00189 }
00190 
00191 
00192 void Entry::setRelease( int release )
00193 {
00194   mRelease = release;
00195 }
00196 
00197 int Entry::release() const
00198 {
00199   return mRelease;
00200 }
00201 
00202 
00203 void Entry::setReleaseDate( const QDate &d )
00204 {
00205   mReleaseDate = d;
00206 }
00207 
00208 QDate Entry::releaseDate() const
00209 {
00210   return mReleaseDate;
00211 }
00212 
00213 
00214 void Entry::setPayload( const KURL &url, const QString &lang )
00215 {
00216   mPayloadMap.insert( lang, url );
00217 
00218   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00219 }
00220 
00221 KURL Entry::payload( const QString &lang ) const
00222 {
00223   KURL payload = mPayloadMap[ lang ];
00224   if ( payload.isEmpty() ) {
00225     QStringList langs = KGlobal::locale()->languageList();
00226     for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
00227       if( !mPayloadMap[ *it ].isEmpty() ) return mPayloadMap[ *it ];
00228   }
00229   if ( payload.isEmpty() ) payload = mPayloadMap [ QString::null ];
00230   if ( payload.isEmpty() && !mPayloadMap.isEmpty() ) {
00231     payload = *(mPayloadMap.begin());
00232   }
00233   return payload;
00234 }
00235 
00236 
00237 void Entry::setPreview( const KURL &url, const QString &lang )
00238 {
00239   mPreviewMap.insert( lang, url );
00240   
00241   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00242 }
00243 
00244 KURL Entry::preview( const QString &lang ) const
00245 {
00246   KURL preview = mPreviewMap[ lang ];
00247   if ( preview.isEmpty() ) {
00248     QStringList langs = KGlobal::locale()->languageList();
00249     for(QStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
00250       if( !mPreviewMap[ *it ].isEmpty() ) return mPreviewMap[ *it ];
00251   }
00252   if ( preview.isEmpty() ) preview = mPreviewMap [ QString::null ];
00253   if ( preview.isEmpty() && !mPreviewMap.isEmpty() ) {
00254     preview = *(mPreviewMap.begin());
00255   }
00256   return preview;
00257 }
00258 
00259 
00260 void Entry::setRating( int rating )
00261 {
00262   mRating = rating;
00263 }
00264 
00265 int Entry::rating()
00266 {
00267   return mRating;
00268 }
00269 
00270 
00271 void Entry::setDownloads( int downloads )
00272 {
00273   mDownloads = downloads;
00274 }
00275 
00276 int Entry::downloads()
00277 {
00278   return mDownloads;
00279 }
00280 
00281 QString Entry::fullName()
00282 {
00283   return name() + "-" + version() + "-" + QString::number( release() );
00284 }
00285 
00286 QStringList Entry::langs()
00287 {
00288   return mLangs;
00289 }
00290 
00291 void Entry::parseDomElement( const QDomElement &element )
00292 {
00293   if ( element.tagName() != "stuff" ) return;
00294   mType = element.attribute("type");
00295 
00296   QDomNode n;
00297   for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
00298     QDomElement e = n.toElement();
00299     if ( e.tagName() == "name" )
00300     {
00301       QString lang = e.attribute( "lang" );
00302       setName( e.text().stripWhiteSpace(), lang );
00303       if(lang.isNull()) setName( e.text().stripWhiteSpace() ); /* primary key - no i18n */
00304     }
00305     if ( e.tagName() == "author" ) {
00306       setAuthor( e.text().stripWhiteSpace() );
00307       QString email = e.attribute( "email" );
00308       setAuthorEmail( email );
00309     }
00310     if ( e.tagName() == "email" ) setAuthorEmail( e.text().stripWhiteSpace() ); /* kde-look; change on server! */
00311     if ( e.tagName() == "licence" ) setLicence( e.text().stripWhiteSpace() );
00312     if ( e.tagName() == "summary" ) {
00313       QString lang = e.attribute( "lang" );
00314       setSummary( e.text().stripWhiteSpace(), lang );
00315     }
00316     if ( e.tagName() == "version" ) setVersion( e.text().stripWhiteSpace() );
00317     if ( e.tagName() == "release" ) setRelease( e.text().toInt() );
00318     if ( e.tagName() == "releasedate" ) {
00319       QDate date = QDate::fromString( e.text().stripWhiteSpace(), Qt::ISODate );
00320       setReleaseDate( date );
00321     }
00322     if ( e.tagName() == "preview" ) {
00323       QString lang = e.attribute( "lang" );
00324       setPreview( KURL( e.text().stripWhiteSpace() ), lang );
00325     }
00326     if ( e.tagName() == "payload" ) {
00327       QString lang = e.attribute( "lang" );
00328       setPayload( KURL( e.text().stripWhiteSpace() ), lang );
00329     }
00330     if ( e.tagName() == "rating" ) setRating( e.text().toInt() );
00331     if ( e.tagName() == "downloads" ) setDownloads( e.text().toInt() );
00332   }
00333 }
00334 
00335 QDomElement Entry::createDomElement( QDomDocument &doc,
00336                                               QDomElement &parent )
00337 {
00338   QDomElement entry = doc.createElement( "stuff" );
00339   entry.setAttribute("type", mType);
00340   parent.appendChild( entry );
00341 
00342   addElement( doc, entry, "name", name() );
00343   addElement( doc, entry, "author", author() );
00344   addElement( doc, entry, "licence", license() );
00345   addElement( doc, entry, "version", version() );
00346   addElement( doc, entry, "release", QString::number( release() ) );
00347   addElement( doc, entry, "rating", QString::number( rating() ) );
00348   addElement( doc, entry, "downloads", QString::number( downloads() ) );
00349 
00350   addElement( doc, entry, "releasedate",
00351               releaseDate().toString( Qt::ISODate ) );
00352 
00353   QStringList ls = langs();
00354   QStringList::ConstIterator it;
00355   for( it = ls.begin(); it != ls.end(); ++it ) {
00356     QDomElement e = addElement( doc, entry, "summary", summary( *it ) );
00357     e.setAttribute( "lang", *it );
00358     e = addElement( doc, entry, "preview", preview( *it ).url() );
00359     e.setAttribute( "lang", *it );
00360     e = addElement( doc, entry, "payload", payload( *it ).url() );
00361     e.setAttribute( "lang", *it );
00362   }
00363 
00364   return entry;
00365 }
00366 
00367 QDomElement Entry::addElement( QDomDocument &doc, QDomElement &parent,
00368                                const QString &tag, const QString &value )
00369 {
00370   QDomElement n = doc.createElement( tag );
00371   n.appendChild( doc.createTextNode( value ) );
00372   parent.appendChild( n );
00373 
00374   return n;
00375 }
KDE Home | KDE Accessibility Home | Description of Access Keys