knewstuff Library API Documentation

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., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "entry.h"
00022 
00023 #include <kglobal.h>
00024 #include <klocale.h>
00025 
00026 using namespace KNS;
00027 
00028 Entry::Entry() :
00029   mRelease( 0 ), mReleaseDate( QDate::currentDate() ), mRating( 0 ),
00030   mDownloads( 0 )
00031 {
00032 }
00033 
00034 Entry::Entry( const QDomElement &e )
00035 {
00036   parseDomElement( e );
00037 }
00038 
00039 Entry::~Entry()
00040 {
00041 }
00042 
00043 
00044 void Entry::setName( const QString &name )
00045 {
00046   mName = name;
00047 }
00048 
00049 QString Entry::name() const
00050 {
00051   return mName;
00052 }
00053 
00054 
00055 void Entry::setType( const QString &type )
00056 {
00057   mType = type;
00058 }
00059 
00060 QString Entry::type() const
00061 {
00062   return mType;
00063 }
00064 
00065 
00066 void Entry::setAuthor( const QString &author )
00067 {
00068   mAuthor = author;
00069 }
00070 
00071 QString Entry::author() const
00072 {
00073   return mAuthor;
00074 }
00075 
00076 
00077 void Entry::setLicence( const QString &license )
00078 {
00079   mLicence = license;
00080 }
00081 
00082 QString Entry::license() const
00083 {
00084   return mLicence;
00085 }
00086 
00087 
00088 void Entry::setSummary( const QString &text, const QString &lang )
00089 {
00090   mSummaryMap.insert( lang, text );
00091 
00092   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00093 }
00094 
00095 QString Entry::summary( const QString &lang ) const
00096 {
00097   if ( mSummaryMap.isEmpty() ) return QString::null;
00098 
00099   if ( !mSummaryMap[ lang ].isEmpty() ) return mSummaryMap[ lang ];
00100   else {
00101     QStringList langs = KGlobal::locale()->languageList();
00102     for(QStringList::Iterator it = langs.begin(); it != langs.end(); it++)
00103       if( !mSummaryMap[ *it ].isEmpty() ) return mSummaryMap[ *it ];
00104   }
00105   if ( !mSummaryMap[ QString::null ].isEmpty() ) return mSummaryMap[ QString::null ];
00106   else return *(mSummaryMap.begin());
00107 }
00108 
00109 
00110 void Entry::setVersion( const QString &version )
00111 {
00112   mVersion = version;
00113 }
00114 
00115 QString Entry::version() const
00116 {
00117   return mVersion;
00118 }
00119 
00120 
00121 void Entry::setRelease( int release )
00122 {
00123   mRelease = release;
00124 }
00125 
00126 int Entry::release() const
00127 {
00128   return mRelease;
00129 }
00130 
00131 
00132 void Entry::setReleaseDate( const QDate &d )
00133 {
00134   mReleaseDate = d;
00135 }
00136 
00137 QDate Entry::releaseDate() const
00138 {
00139   return mReleaseDate;
00140 }
00141 
00142 
00143 void Entry::setPayload( const KURL &url, const QString &lang )
00144 {
00145   mPayloadMap.insert( lang, url );
00146 
00147   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00148 }
00149 
00150 KURL Entry::payload( const QString &lang ) const
00151 {
00152   KURL payload = mPayloadMap[ lang ];
00153   if ( payload.isEmpty() ) {
00154     QStringList langs = KGlobal::locale()->languageList();
00155     for(QStringList::Iterator it = langs.begin(); it != langs.end(); it++)
00156       if( !mPayloadMap[ *it ].isEmpty() ) return mPayloadMap[ *it ];
00157   }
00158   if ( payload.isEmpty() ) payload = mPayloadMap [ QString::null ];
00159   if ( payload.isEmpty() && !mPayloadMap.isEmpty() ) {
00160     payload = *(mPayloadMap.begin());
00161   }
00162   return payload;
00163 }
00164 
00165 
00166 void Entry::setPreview( const KURL &url, const QString &lang )
00167 {
00168   mPreviewMap.insert( lang, url );
00169   
00170   if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
00171 }
00172 
00173 KURL Entry::preview( const QString &lang ) const
00174 {
00175   KURL preview = mPreviewMap[ lang ];
00176   if ( preview.isEmpty() ) {
00177     QStringList langs = KGlobal::locale()->languageList();
00178     for(QStringList::Iterator it = langs.begin(); it != langs.end(); it++)
00179       if( !mPreviewMap[ *it ].isEmpty() ) return mPreviewMap[ *it ];
00180   }
00181   if ( preview.isEmpty() ) preview = mPreviewMap [ QString::null ];
00182   if ( preview.isEmpty() && !mPreviewMap.isEmpty() ) {
00183     preview = *(mPreviewMap.begin());
00184   }
00185   return preview;
00186 }
00187 
00188 
00189 void Entry::setRating( int rating )
00190 {
00191   mRating = rating;
00192 }
00193 
00194 int Entry::rating()
00195 {
00196   return mRating;
00197 }
00198 
00199 
00200 void Entry::setDownloads( int downloads )
00201 {
00202   mDownloads = downloads;
00203 }
00204 
00205 int Entry::downloads()
00206 {
00207   return mDownloads;
00208 }
00209 
00210 QString Entry::fullName()
00211 {
00212   return name() + "-" + version() + "-" + QString::number( release() );
00213 }
00214 
00215 QStringList Entry::langs()
00216 {
00217   return mLangs;
00218 }
00219 
00220 void Entry::parseDomElement( const QDomElement &element )
00221 {
00222   if ( element.tagName() != "stuff" ) return;
00223   mType = element.attribute("type");
00224 
00225   QDomNode n;
00226   for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
00227     QDomElement e = n.toElement();
00228     if ( e.tagName() == "name" ) setName( e.text().stripWhiteSpace() );
00229     if ( e.tagName() == "author" ) setAuthor( e.text().stripWhiteSpace() );
00230     if ( e.tagName() == "licence" ) setLicence( e.text().stripWhiteSpace() );
00231     if ( e.tagName() == "summary" ) {
00232       QString lang = e.attribute( "lang" );
00233       setSummary( e.text().stripWhiteSpace(), lang );
00234     }
00235     if ( e.tagName() == "version" ) setVersion( e.text().stripWhiteSpace() );
00236     if ( e.tagName() == "release" ) setRelease( e.text().toInt() );
00237     if ( e.tagName() == "releasedate" ) {
00238       QDate date = QDate::fromString( e.text().stripWhiteSpace(), Qt::ISODate );
00239       setReleaseDate( date );
00240     }
00241     if ( e.tagName() == "preview" ) {
00242       QString lang = e.attribute( "lang" );
00243       setPreview( KURL( e.text().stripWhiteSpace() ), lang );
00244     }
00245     if ( e.tagName() == "payload" ) {
00246       QString lang = e.attribute( "lang" );
00247       setPayload( KURL( e.text().stripWhiteSpace() ), lang );
00248     }
00249     if ( e.tagName() == "rating" ) setRating( e.text().toInt() );
00250     if ( e.tagName() == "downloads" ) setDownloads( e.text().toInt() );
00251   }
00252 }
00253 
00254 QDomElement Entry::createDomElement( QDomDocument &doc,
00255                                               QDomElement &parent )
00256 {
00257   QDomElement entry = doc.createElement( "stuff" );
00258   entry.setAttribute("type", mType);
00259   parent.appendChild( entry );
00260 
00261   addElement( doc, entry, "name", name() );
00262   addElement( doc, entry, "author", author() );
00263   addElement( doc, entry, "licence", license() );
00264   addElement( doc, entry, "version", version() );
00265   addElement( doc, entry, "release", QString::number( release() ) );
00266   addElement( doc, entry, "rating", QString::number( rating() ) );
00267   addElement( doc, entry, "downloads", QString::number( downloads() ) );
00268 
00269   addElement( doc, entry, "releasedate",
00270               releaseDate().toString( Qt::ISODate ) );
00271 
00272   QStringList ls = langs();
00273   QStringList::ConstIterator it;
00274   for( it = ls.begin(); it != ls.end(); ++it ) {
00275     QDomElement e = addElement( doc, entry, "summary", summary( *it ) );
00276     e.setAttribute( "lang", *it );
00277     e = addElement( doc, entry, "preview", preview( *it ).url() );
00278     e.setAttribute( "lang", *it );
00279     e = addElement( doc, entry, "payload", payload( *it ).url() );
00280     e.setAttribute( "lang", *it );
00281   }
00282 
00283   return entry;
00284 }
00285 
00286 QDomElement Entry::addElement( QDomDocument &doc, QDomElement &parent,
00287                                const QString &tag, const QString &value )
00288 {
00289   QDomElement n = doc.createElement( tag );
00290   n.appendChild( doc.createTextNode( value ) );
00291   parent.appendChild( n );
00292 
00293   return n;
00294 }
KDE Logo
This file is part of the documentation for knewstuff Library Version 3.3.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jan 22 16:48:06 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003