TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

entry.cpp 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. This file is part of KOrganizer.
  3. Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include "entry.h"
  18. #include <tqptrdict.h>
  19. #include <tqwindowdefs.h>
  20. #include <tdeglobal.h>
  21. #include <tdelocale.h>
  22. using namespace KNS;
  23. // BCI for KDE 3.5 only
  24. class EntryPrivate
  25. {
  26. public:
  27. EntryPrivate(){}
  28. TQString mEmail;
  29. TQMap<TQString,TQString> mNameMap;
  30. };
  31. static TQPtrDict<EntryPrivate> *d_ptr = 0;
  32. static EntryPrivate *d(const Entry *e)
  33. {
  34. if(!d_ptr)
  35. {
  36. d_ptr = new TQPtrDict<EntryPrivate>();
  37. d_ptr->setAutoDelete(true);
  38. }
  39. EntryPrivate *ret = d_ptr->find((void*)e);
  40. if(!ret)
  41. {
  42. ret = new EntryPrivate();
  43. d_ptr->replace((void*)e, ret);
  44. }
  45. return ret;
  46. }
  47. TQString Entry::authorEmail() const
  48. {
  49. return d(this)->mEmail;
  50. }
  51. void Entry::setAuthorEmail( const TQString& email )
  52. {
  53. d(this)->mEmail = email;
  54. }
  55. TQString Entry::name( const TQString &lang ) const
  56. {
  57. if ( d(this)->mNameMap.isEmpty() ) return TQString::null;
  58. if ( !d(this)->mNameMap[ lang ].isEmpty() ) return d(this)->mNameMap[ lang ];
  59. else {
  60. TQStringList langs = TDEGlobal::locale()->languageList();
  61. for(TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
  62. if( !d(this)->mNameMap[ *it ].isEmpty() ) return d(this)->mNameMap[ *it ];
  63. }
  64. if ( !d(this)->mNameMap[ TQString::null ].isEmpty() ) return d(this)->mNameMap[ TQString::null ];
  65. else return *(mSummaryMap.begin());
  66. }
  67. void Entry::setName( const TQString &name, const TQString &lang )
  68. {
  69. d(this)->mNameMap.insert( lang, name );
  70. if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
  71. }
  72. // BCI part ends here
  73. Entry::Entry() :
  74. mRelease( 0 ), mReleaseDate( TQDate::currentDate() ), mRating( 0 ),
  75. mDownloads( 0 )
  76. {
  77. }
  78. Entry::Entry( const TQDomElement &e ) :
  79. mRelease( 0 ), mRating( 0 ), mDownloads( 0 )
  80. {
  81. parseDomElement( e );
  82. }
  83. Entry::~Entry()
  84. {
  85. if (d_ptr)
  86. {
  87. EntryPrivate *p = d_ptr->find(this);
  88. if (p)
  89. d_ptr->remove(p);
  90. if (d_ptr->isEmpty())
  91. {
  92. delete d_ptr;
  93. d_ptr = 0L;
  94. }
  95. }
  96. }
  97. void Entry::setName( const TQString &name )
  98. {
  99. mName = name;
  100. }
  101. TQString Entry::name() const
  102. {
  103. return mName;
  104. }
  105. void Entry::setType( const TQString &type )
  106. {
  107. mType = type;
  108. }
  109. TQString Entry::type() const
  110. {
  111. return mType;
  112. }
  113. void Entry::setAuthor( const TQString &author )
  114. {
  115. mAuthor = author;
  116. }
  117. TQString Entry::author() const
  118. {
  119. return mAuthor;
  120. }
  121. void Entry::setLicence( const TQString &license )
  122. {
  123. mLicence = license;
  124. }
  125. TQString Entry::license() const
  126. {
  127. return mLicence;
  128. }
  129. void Entry::setSummary( const TQString &text, const TQString &lang )
  130. {
  131. mSummaryMap.insert( lang, text );
  132. if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
  133. }
  134. TQString Entry::summary( const TQString &lang ) const
  135. {
  136. if ( mSummaryMap.isEmpty() ) return TQString::null;
  137. if ( !mSummaryMap[ lang ].isEmpty() ) return mSummaryMap[ lang ];
  138. else {
  139. TQStringList langs = TDEGlobal::locale()->languageList();
  140. for(TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
  141. if( !mSummaryMap[ *it ].isEmpty() ) return mSummaryMap[ *it ];
  142. }
  143. if ( !mSummaryMap[ TQString::null ].isEmpty() ) return mSummaryMap[ TQString::null ];
  144. else return *(mSummaryMap.begin());
  145. }
  146. void Entry::setVersion( const TQString &version )
  147. {
  148. mVersion = version;
  149. }
  150. TQString Entry::version() const
  151. {
  152. return mVersion;
  153. }
  154. void Entry::setRelease( int release )
  155. {
  156. mRelease = release;
  157. }
  158. int Entry::release() const
  159. {
  160. return mRelease;
  161. }
  162. void Entry::setReleaseDate( const TQDate &d )
  163. {
  164. mReleaseDate = d;
  165. }
  166. TQDate Entry::releaseDate() const
  167. {
  168. return mReleaseDate;
  169. }
  170. void Entry::setPayload( const KURL &url, const TQString &lang )
  171. {
  172. mPayloadMap.insert( lang, url );
  173. if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
  174. }
  175. KURL Entry::payload( const TQString &lang ) const
  176. {
  177. KURL payload = mPayloadMap[ lang ];
  178. if ( payload.isEmpty() ) {
  179. TQStringList langs = TDEGlobal::locale()->languageList();
  180. for(TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
  181. if( !mPayloadMap[ *it ].isEmpty() ) return mPayloadMap[ *it ];
  182. }
  183. if ( payload.isEmpty() ) payload = mPayloadMap [ TQString::null ];
  184. if ( payload.isEmpty() && !mPayloadMap.isEmpty() ) {
  185. payload = *(mPayloadMap.begin());
  186. }
  187. return payload;
  188. }
  189. void Entry::setPreview( const KURL &url, const TQString &lang )
  190. {
  191. mPreviewMap.insert( lang, url );
  192. if ( mLangs.find( lang ) == mLangs.end() ) mLangs.append( lang );
  193. }
  194. KURL Entry::preview( const TQString &lang ) const
  195. {
  196. KURL preview = mPreviewMap[ lang ];
  197. if ( preview.isEmpty() ) {
  198. TQStringList langs = TDEGlobal::locale()->languageList();
  199. for(TQStringList::Iterator it = langs.begin(); it != langs.end(); ++it)
  200. if( !mPreviewMap[ *it ].isEmpty() ) return mPreviewMap[ *it ];
  201. }
  202. if ( preview.isEmpty() ) preview = mPreviewMap [ TQString::null ];
  203. if ( preview.isEmpty() && !mPreviewMap.isEmpty() ) {
  204. preview = *(mPreviewMap.begin());
  205. }
  206. return preview;
  207. }
  208. void Entry::setRating( int rating )
  209. {
  210. mRating = rating;
  211. }
  212. int Entry::rating()
  213. {
  214. return mRating;
  215. }
  216. void Entry::setDownloads( int downloads )
  217. {
  218. mDownloads = downloads;
  219. }
  220. int Entry::downloads()
  221. {
  222. return mDownloads;
  223. }
  224. TQString Entry::fullName()
  225. {
  226. if ( version().isEmpty() )
  227. return name();
  228. else
  229. return name() + "-" + version() + "-" + TQString::number( release() );
  230. }
  231. TQStringList Entry::langs()
  232. {
  233. return mLangs;
  234. }
  235. void Entry::parseDomElement( const TQDomElement &element )
  236. {
  237. if ( element.tagName() != "stuff" ) return;
  238. mType = element.attribute("type");
  239. TQDomNode n;
  240. for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
  241. TQDomElement e = n.toElement();
  242. if ( e.tagName() == "name" )
  243. {
  244. TQString lang = e.attribute( "lang" );
  245. setName( e.text().stripWhiteSpace(), lang );
  246. if(lang.isNull()) setName( e.text().stripWhiteSpace() ); /* primary key - no i18n */
  247. }
  248. if ( e.tagName() == "author" ) {
  249. setAuthor( e.text().stripWhiteSpace() );
  250. TQString email = e.attribute( "email" );
  251. setAuthorEmail( email );
  252. }
  253. if ( e.tagName() == "email" ) setAuthorEmail( e.text().stripWhiteSpace() ); /* kde-look; change on server! */
  254. if ( e.tagName() == "licence" ) setLicence( e.text().stripWhiteSpace() );
  255. if ( e.tagName() == "summary" ) {
  256. TQString lang = e.attribute( "lang" );
  257. setSummary( e.text().stripWhiteSpace(), lang );
  258. }
  259. if ( e.tagName() == "version" ) setVersion( e.text().stripWhiteSpace() );
  260. if ( e.tagName() == "release" ) setRelease( e.text().toInt() );
  261. if ( e.tagName() == "releasedate" ) {
  262. TQDate date = TQT_TQDATE_OBJECT(TQDate::fromString( e.text().stripWhiteSpace(), Qt::ISODate ));
  263. setReleaseDate( date );
  264. }
  265. if ( e.tagName() == "preview" ) {
  266. TQString lang = e.attribute( "lang" );
  267. setPreview( KURL( e.text().stripWhiteSpace() ), lang );
  268. }
  269. if ( e.tagName() == "payload" ) {
  270. TQString lang = e.attribute( "lang" );
  271. setPayload( KURL( e.text().stripWhiteSpace() ), lang );
  272. }
  273. if ( e.tagName() == "rating" ) setRating( e.text().toInt() );
  274. if ( e.tagName() == "downloads" ) setDownloads( e.text().toInt() );
  275. }
  276. }
  277. TQDomElement Entry::createDomElement( TQDomDocument &doc,
  278. TQDomElement &parent )
  279. {
  280. TQDomElement entry = doc.createElement( "stuff" );
  281. entry.setAttribute("type", mType);
  282. parent.appendChild( entry );
  283. addElement( doc, entry, "name", name() );
  284. addElement( doc, entry, "author", author() );
  285. addElement( doc, entry, "email", authorEmail() );
  286. addElement( doc, entry, "licence", license() );
  287. addElement( doc, entry, "version", version() );
  288. addElement( doc, entry, "release", TQString::number( release() ) );
  289. addElement( doc, entry, "rating", TQString::number( rating() ) );
  290. addElement( doc, entry, "downloads", TQString::number( downloads() ) );
  291. addElement( doc, entry, "releasedate",
  292. releaseDate().toString( Qt::ISODate ) );
  293. TQStringList ls = langs();
  294. TQStringList::ConstIterator it;
  295. for( it = ls.begin(); it != ls.end(); ++it ) {
  296. TQDomElement e = addElement( doc, entry, "summary", summary( *it ) );
  297. e.setAttribute( "lang", *it );
  298. e = addElement( doc, entry, "preview", preview( *it ).url() );
  299. e.setAttribute( "lang", *it );
  300. e = addElement( doc, entry, "payload", payload( *it ).url() );
  301. e.setAttribute( "lang", *it );
  302. }
  303. return entry;
  304. }
  305. TQDomElement Entry::addElement( TQDomDocument &doc, TQDomElement &parent,
  306. const TQString &tag, const TQString &value )
  307. {
  308. TQDomElement n = doc.createElement( tag );
  309. n.appendChild( doc.createTextNode( value ) );
  310. parent.appendChild( n );
  311. return n;
  312. }