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.6KB

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