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.

engine.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 <tqcstring.h>
  18. #include <tqdom.h>
  19. #include <tqfileinfo.h>
  20. #include <tdeapplication.h>
  21. #include <kdebug.h>
  22. #include <tdeio/job.h>
  23. #include <tdelocale.h>
  24. #include <tdemessagebox.h>
  25. #include <kstandarddirs.h>
  26. #include "knewstuff.h"
  27. #include "downloaddialog.h"
  28. #include "uploaddialog.h"
  29. #include "providerdialog.h"
  30. #include "engine.h"
  31. #include "engine.moc"
  32. using namespace KNS;
  33. struct Engine::Private
  34. {
  35. bool mIgnoreInstallResult;
  36. KNewStuff *mNewStuff;
  37. };
  38. Engine::Engine( KNewStuff *newStuff, const TQString &type,
  39. TQWidget *parentWidget ) :
  40. mParentWidget( parentWidget ), mDownloadDialog( 0 ),
  41. mUploadDialog( 0 ), mProviderDialog( 0 ), mUploadProvider( 0 ),
  42. d(new Private), mType( type )
  43. {
  44. d->mNewStuff = newStuff;
  45. d->mIgnoreInstallResult = false;
  46. mProviderLoader = new ProviderLoader( mParentWidget );
  47. }
  48. Engine::Engine( KNewStuff *newStuff, const TQString &type,
  49. const TQString &providerList, TQWidget *parentWidget ) :
  50. mParentWidget( parentWidget ),
  51. mDownloadDialog( 0 ), mUploadDialog( 0 ),
  52. mProviderDialog( 0 ), mUploadProvider( 0 ),
  53. mProviderList( providerList ), d(new Private),
  54. mType( type )
  55. {
  56. d->mNewStuff = newStuff;
  57. d->mIgnoreInstallResult = false;
  58. mProviderLoader = new ProviderLoader( mParentWidget );
  59. }
  60. Engine::~Engine()
  61. {
  62. delete d;
  63. delete mProviderLoader;
  64. delete mUploadDialog;
  65. delete mDownloadDialog;
  66. }
  67. void Engine::download()
  68. {
  69. kdDebug() << "Engine::download()" << endl;
  70. connect( mProviderLoader,
  71. TQT_SIGNAL( providersLoaded( Provider::List * ) ),
  72. TQT_SLOT( getMetaInformation( Provider::List * ) ) );
  73. mProviderLoader->load( mType, mProviderList );
  74. }
  75. void Engine::getMetaInformation( Provider::List *providers )
  76. {
  77. mProviderLoader->disconnect();
  78. mNewStuffJobData.clear();
  79. if ( !mDownloadDialog ) {
  80. mDownloadDialog = new DownloadDialog( this, mParentWidget );
  81. mDownloadDialog->show();
  82. }
  83. mDownloadDialog->clear();
  84. Provider *p;
  85. for ( p = providers->first(); p; p = providers->next() ) {
  86. if ( p->downloadUrl().isEmpty() ) continue;
  87. TDEIO::TransferJob *job = TDEIO::get( p->downloadUrl(), false, false );
  88. connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
  89. TQT_SLOT( slotNewStuffJobResult( TDEIO::Job * ) ) );
  90. connect( job, TQT_SIGNAL( data( TDEIO::Job *, const TQByteArray & ) ),
  91. TQT_SLOT( slotNewStuffJobData( TDEIO::Job *, const TQByteArray & ) ) );
  92. mNewStuffJobData.insert( job, "" );
  93. mProviderJobs[ job ] = p;
  94. }
  95. }
  96. void Engine::slotNewStuffJobData( TDEIO::Job *job, const TQByteArray &data )
  97. {
  98. if ( data.isEmpty() ) return;
  99. kdDebug() << "Engine:slotNewStuffJobData()" << endl;
  100. TQCString str( data, data.size() + 1 );
  101. mNewStuffJobData[ job ].append( TQString::fromUtf8( str ) );
  102. }
  103. void Engine::slotNewStuffJobResult( TDEIO::Job *job )
  104. {
  105. if ( job->error() ) {
  106. kdDebug() << "Error downloading new stuff descriptions." << endl;
  107. job->showErrorDialog( mParentWidget );
  108. } else {
  109. TQString knewstuffDoc = mNewStuffJobData[ job ];
  110. kdDebug() << "---START---" << endl << knewstuffDoc << "---END---" << endl;
  111. mDownloadDialog->addProvider( mProviderJobs[ job ] );
  112. TQDomDocument doc;
  113. if ( !doc.setContent( knewstuffDoc ) ) {
  114. kdDebug() << "Error parsing knewstuff.xml." << endl;
  115. return;
  116. } else {
  117. TQDomElement knewstuff = doc.documentElement();
  118. if ( knewstuff.isNull() ) {
  119. kdDebug() << "No document in knewstuffproviders.xml." << endl;
  120. } else {
  121. TQDomNode p;
  122. for ( p = knewstuff.firstChild(); !p.isNull(); p = p.nextSibling() ) {
  123. TQDomElement stuff = p.toElement();
  124. if ( stuff.tagName() != "stuff" ) continue;
  125. if ( stuff.attribute("type", mType) != mType ) continue;
  126. Entry *entry = new Entry( stuff );
  127. mDownloadDialog->show();
  128. mDownloadDialog->addEntry( entry );
  129. kdDebug() << "KNEWSTUFF: " << entry->name() << endl;
  130. kdDebug() << " SUMMARY: " << entry->summary() << endl;
  131. kdDebug() << " VERSION: " << entry->version() << endl;
  132. kdDebug() << " RELEASEDATE: " << TQString(entry->releaseDate().toString()) << endl;
  133. kdDebug() << " RATING: " << entry->rating() << endl;
  134. kdDebug() << " LANGS: " << entry->langs().join(", ") << endl;
  135. }
  136. }
  137. }
  138. }
  139. mNewStuffJobData.remove( job );
  140. mProviderJobs.remove( job );
  141. if ( mNewStuffJobData.count() == 0 ) {
  142. mDownloadDialog->show();
  143. mDownloadDialog->raise();
  144. }
  145. }
  146. void Engine::download( Entry *entry )
  147. {
  148. kdDebug() << "Engine::download(entry)" << endl;
  149. KURL source = entry->payload();
  150. mDownloadDestination = d->mNewStuff->downloadDestination( entry );
  151. if ( mDownloadDestination.isEmpty() ) {
  152. kdDebug() << "Empty downloadDestination. Cancelling download." << endl;
  153. return;
  154. }
  155. KURL destination = KURL( mDownloadDestination );
  156. kdDebug() << " SOURCE: " << source.url() << endl;
  157. kdDebug() << " DESTINATION: " << destination.url() << endl;
  158. TDEIO::FileCopyJob *job = TDEIO::file_copy( source, destination, -1, true );
  159. connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
  160. TQT_SLOT( slotDownloadJobResult( TDEIO::Job * ) ) );
  161. }
  162. void Engine::slotDownloadJobResult( TDEIO::Job *job )
  163. {
  164. if ( job->error() ) {
  165. kdDebug() << "Error downloading new stuff payload." << endl;
  166. job->showErrorDialog( mParentWidget );
  167. return;
  168. }
  169. if ( d->mNewStuff->install( mDownloadDestination ) ) {
  170. if ( !d->mIgnoreInstallResult ) {
  171. KMessageBox::information( mParentWidget,
  172. i18n("Successfully installed hot new stuff.") );
  173. }
  174. } else
  175. if ( !d->mIgnoreInstallResult ){
  176. KMessageBox::error( mParentWidget,
  177. i18n("Failed to install hot new stuff.") );
  178. }
  179. }
  180. void Engine::upload(const TQString &fileName, const TQString &previewName )
  181. {
  182. mUploadFile = fileName;
  183. mPreviewFile = previewName;
  184. connect( mProviderLoader,
  185. TQT_SIGNAL( providersLoaded( Provider::List * ) ),
  186. TQT_SLOT( selectUploadProvider( Provider::List * ) ) );
  187. mProviderLoader->load( mType );
  188. }
  189. void Engine::selectUploadProvider( Provider::List *providers )
  190. {
  191. kdDebug() << "Engine:selectUploadProvider()" << endl;
  192. mProviderLoader->disconnect();
  193. if ( !mProviderDialog ) {
  194. mProviderDialog = new ProviderDialog( this, mParentWidget );
  195. }
  196. mProviderDialog->clear();
  197. mProviderDialog->show();
  198. mProviderDialog->raise();
  199. for( Provider *p = providers->first(); p; p = providers->next() ) {
  200. mProviderDialog->addProvider( p );
  201. }
  202. }
  203. void Engine::requestMetaInformation( Provider *provider )
  204. {
  205. mUploadProvider = provider;
  206. if ( !mUploadDialog ) {
  207. mUploadDialog = new UploadDialog( this, mParentWidget );
  208. }
  209. mUploadDialog->setPreviewFile( mPreviewFile );
  210. mUploadDialog->setPayloadFile( mUploadFile );
  211. mUploadDialog->show();
  212. mUploadDialog->raise();
  213. }
  214. void Engine::upload( Entry *entry )
  215. {
  216. if ( mUploadFile.isNull()) {
  217. mUploadFile = entry->fullName();
  218. mUploadFile = locateLocal( "data", TQString(kapp->instanceName()) + "/upload/" + mUploadFile );
  219. if ( !d->mNewStuff->createUploadFile( mUploadFile ) ) {
  220. KMessageBox::error( mParentWidget, i18n("Unable to create file to upload.") );
  221. emit uploadFinished( false );
  222. return;
  223. }
  224. }
  225. TQString lang = entry->langs().first();
  226. TQFileInfo fi( mUploadFile );
  227. entry->setPayload( KURL::fromPathOrURL( fi.fileName() ), lang );
  228. if ( !createMetaFile( entry ) ) {
  229. emit uploadFinished( false );
  230. return;
  231. }
  232. TQString text = i18n("The files to be uploaded have been created at:\n");
  233. text.append( i18n("Data file: %1\n").arg( mUploadFile) );
  234. if (!mPreviewFile.isEmpty()) {
  235. text.append( i18n("Preview image: %1\n").arg( mPreviewFile) );
  236. }
  237. text.append( i18n("Content information: %1\n").arg( mUploadMetaFile) );
  238. text.append( i18n("Those files can now be uploaded.\n") );
  239. text.append( i18n("Beware that any people might have access to them at any time.") );
  240. TQString caption = i18n("Upload Files");
  241. if ( mUploadProvider->noUpload() ) {
  242. KURL noUploadUrl = mUploadProvider->noUploadUrl();
  243. if ( noUploadUrl.isEmpty() ) {
  244. text.append( i18n("Please upload the files manually.") );
  245. KMessageBox::information( mParentWidget, text, caption );
  246. } else {
  247. int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
  248. i18n("Upload Info"),
  249. KStdGuiItem::close() );
  250. if ( result == KMessageBox::Yes ) {
  251. kapp->invokeBrowser( noUploadUrl.url() );
  252. }
  253. }
  254. } else {
  255. int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
  256. i18n("&Upload"), KStdGuiItem::cancel() );
  257. if ( result == KMessageBox::Yes ) {
  258. KURL destination = mUploadProvider->uploadUrl();
  259. destination.setFileName( fi.fileName() );
  260. TDEIO::FileCopyJob *job = TDEIO::file_copy( KURL::fromPathOrURL( mUploadFile ), destination );
  261. connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
  262. TQT_SLOT( slotUploadPayloadJobResult( TDEIO::Job * ) ) );
  263. } else {
  264. emit uploadFinished( false );
  265. }
  266. }
  267. }
  268. bool Engine::createMetaFile( Entry *entry )
  269. {
  270. TQDomDocument doc("knewstuff");
  271. doc.appendChild( doc.createProcessingInstruction(
  272. "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
  273. TQDomElement de = doc.createElement("knewstuff");
  274. doc.appendChild( de );
  275. entry->setType(type());
  276. de.appendChild( entry->createDomElement( doc, de ) );
  277. kdDebug() << "--DOM START--" << endl << doc.toString()
  278. << "--DOM_END--" << endl;
  279. if ( mUploadMetaFile.isNull() ) {
  280. mUploadMetaFile = entry->fullName() + ".meta";
  281. mUploadMetaFile = locateLocal( "data", TQString(kapp->instanceName()) + "/upload/" + mUploadMetaFile );
  282. }
  283. TQFile f( mUploadMetaFile );
  284. if ( !f.open( IO_WriteOnly ) ) {
  285. mUploadMetaFile = TQString::null;
  286. return false;
  287. }
  288. TQTextStream ts( &f );
  289. ts.setEncoding( TQTextStream::UnicodeUTF8 );
  290. ts << doc.toString();
  291. f.close();
  292. return true;
  293. }
  294. void Engine::slotUploadPayloadJobResult( TDEIO::Job *job )
  295. {
  296. if ( job->error() ) {
  297. kdDebug() << "Error uploading new stuff payload." << endl;
  298. job->showErrorDialog( mParentWidget );
  299. emit uploadFinished( false );
  300. return;
  301. }
  302. if (mPreviewFile.isEmpty()) {
  303. slotUploadPreviewJobResult(job);
  304. return;
  305. }
  306. TQFileInfo fi( mPreviewFile );
  307. KURL previewDestination = mUploadProvider->uploadUrl();
  308. previewDestination.setFileName( fi.fileName() );
  309. TDEIO::FileCopyJob *newJob = TDEIO::file_copy( KURL::fromPathOrURL( mPreviewFile ), previewDestination );
  310. connect( newJob, TQT_SIGNAL( result( TDEIO::Job * ) ),
  311. TQT_SLOT( slotUploadPreviewJobResult( TDEIO::Job * ) ) );
  312. }
  313. void Engine::slotUploadPreviewJobResult( TDEIO::Job *job )
  314. {
  315. if ( job->error() ) {
  316. kdDebug() << "Error uploading new stuff preview." << endl;
  317. job->showErrorDialog( mParentWidget );
  318. emit uploadFinished( true );
  319. return;
  320. }
  321. TQFileInfo fi( mUploadMetaFile );
  322. KURL metaDestination = mUploadProvider->uploadUrl();
  323. metaDestination.setFileName( fi.fileName() );
  324. TDEIO::FileCopyJob *newJob = TDEIO::file_copy( KURL::fromPathOrURL( mUploadMetaFile ), metaDestination );
  325. connect( newJob, TQT_SIGNAL( result( TDEIO::Job * ) ),
  326. TQT_SLOT( slotUploadMetaJobResult( TDEIO::Job * ) ) );
  327. }
  328. void Engine::slotUploadMetaJobResult( TDEIO::Job *job )
  329. {
  330. mUploadMetaFile = TQString::null;
  331. if ( job->error() ) {
  332. kdDebug() << "Error uploading new stuff metadata." << endl;
  333. job->showErrorDialog( mParentWidget );
  334. emit uploadFinished( false );
  335. return;
  336. }
  337. KMessageBox::information( mParentWidget,
  338. i18n("Successfully uploaded new stuff.") );
  339. emit uploadFinished( true );
  340. }
  341. void Engine::ignoreInstallResult(bool ignore)
  342. {
  343. d->mIgnoreInstallResult = ignore;
  344. }