22 #include <tqcstring.h>
24 #include <tqfileinfo.h>
26 #include <tdeapplication.h>
28 #include <tdeio/job.h>
29 #include <tdelocale.h>
30 #include <tdemessagebox.h>
31 #include <kstandarddirs.h>
33 #include "knewstuff.h"
34 #include "downloaddialog.h"
35 #include "uploaddialog.h"
36 #include "providerdialog.h"
43 struct Engine::Private
45 bool mIgnoreInstallResult;
50 TQWidget *parentWidget ) :
51 mParentWidget( parentWidget ), mDownloadDialog( 0 ),
52 mUploadDialog( 0 ), mProviderDialog( 0 ), mUploadProvider( 0 ),
53 d(new Private), mType( type )
55 d->mNewStuff = newStuff;
56 d->mIgnoreInstallResult =
false;
61 const TQString &providerList, TQWidget *parentWidget ) :
62 mParentWidget( parentWidget ),
63 mDownloadDialog( 0 ), mUploadDialog( 0 ),
64 mProviderDialog( 0 ), mUploadProvider( 0 ),
65 mProviderList( providerList ), d(new Private),
68 d->mNewStuff = newStuff;
69 d->mIgnoreInstallResult =
false;
76 delete mProviderLoader;
79 delete mDownloadDialog;
84 kdDebug() <<
"Engine::download()" << endl;
86 connect( mProviderLoader,
87 TQT_SIGNAL( providersLoaded( Provider::List * ) ),
88 TQT_SLOT( getMetaInformation( Provider::List * ) ) );
89 mProviderLoader->
load( mType, mProviderList );
92 void Engine::getMetaInformation( Provider::List *providers )
94 mProviderLoader->disconnect();
96 mNewStuffJobData.clear();
98 if ( !mDownloadDialog ) {
100 mDownloadDialog->show();
102 mDownloadDialog->
clear();
105 for ( p = providers->first(); p; p = providers->next() ) {
108 TDEIO::TransferJob *job = TDEIO::get( p->
downloadUrl(),
false,
false );
109 connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
110 TQT_SLOT( slotNewStuffJobResult( TDEIO::Job * ) ) );
111 connect( job, TQT_SIGNAL( data( TDEIO::Job *,
const TQByteArray & ) ),
112 TQT_SLOT( slotNewStuffJobData( TDEIO::Job *,
const TQByteArray & ) ) );
114 mNewStuffJobData.insert( job,
"" );
115 mProviderJobs[ job ] = p;
119 void Engine::slotNewStuffJobData( TDEIO::Job *job,
const TQByteArray &data )
121 if ( data.isEmpty() )
return;
123 kdDebug() <<
"Engine:slotNewStuffJobData()" << endl;
125 TQCString str( data, data.size() + 1 );
127 mNewStuffJobData[ job ].append( TQString::fromUtf8( str ) );
130 void Engine::slotNewStuffJobResult( TDEIO::Job *job )
132 if ( job->error() ) {
133 kdDebug() <<
"Error downloading new stuff descriptions." << endl;
134 job->showErrorDialog( mParentWidget );
136 TQString knewstuffDoc = mNewStuffJobData[ job ];
138 kdDebug() <<
"---START---" << endl << knewstuffDoc <<
"---END---" << endl;
140 mDownloadDialog->
addProvider( mProviderJobs[ job ] );
143 if ( !doc.setContent( knewstuffDoc ) ) {
144 kdDebug() <<
"Error parsing OCS response." << endl;
148 TQDomElement knewstuff = doc.documentElement();
150 if ( knewstuff.isNull() ) {
151 kdDebug() <<
"No document in OCS response." << endl;
154 TQDomElement content;
155 for(TQDomNode pn = knewstuff.firstChild(); !pn.isNull(); pn = pn.nextSibling())
157 TQDomElement stuff = pn.toElement();
159 if(stuff.tagName() ==
"data")
161 content = pn.toElement();
165 if ( content.isNull() ) {
166 kdDebug() <<
"No content in OCS response." << endl;
170 for ( p = content.firstChild(); !p.isNull(); p = p.nextSibling() ) {
171 TQDomElement stuff = p.toElement();
172 if ( stuff.tagName() !=
"content" )
continue;
176 mDownloadDialog->show();
180 kdDebug() <<
"KNEWSTUFF: " << entry->
name() << endl;
182 kdDebug() <<
" SUMMARY: " << entry->
summary() << endl;
183 kdDebug() <<
" VERSION: " << entry->
version() << endl;
184 kdDebug() <<
" RELEASEDATE: " << TQString(entry->
releaseDate().toString()) << endl;
185 kdDebug() <<
" RATING: " << entry->
rating() << endl;
187 kdDebug() <<
" LANGS: " << entry->
langs().join(
", ") << endl;
194 mNewStuffJobData.remove( job );
195 mProviderJobs.remove( job );
197 if ( mNewStuffJobData.count() == 0 ) {
198 mDownloadDialog->show();
199 mDownloadDialog->raise();
205 kdDebug() <<
"Engine::download(entry)" << endl;
207 KURL source = entry->
payload();
208 mDownloadDestination = d->mNewStuff->downloadDestination( entry );
210 if ( mDownloadDestination.isEmpty() ) {
211 kdDebug() <<
"Empty downloadDestination. Cancelling download." << endl;
215 KURL destination = KURL( mDownloadDestination );
217 kdDebug() <<
" SOURCE: " << source.url() << endl;
218 kdDebug() <<
" DESTINATION: " << destination.url() << endl;
220 TDEIO::FileCopyJob *job = TDEIO::file_copy( source, destination, -1,
true );
221 connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
222 TQT_SLOT( slotDownloadJobResult( TDEIO::Job * ) ) );
225 void Engine::slotDownloadJobResult( TDEIO::Job *job )
227 if ( job->error() ) {
228 kdDebug() <<
"Error downloading new stuff payload." << endl;
229 job->showErrorDialog( mParentWidget );
233 if ( d->mNewStuff->install( mDownloadDestination ) ) {
234 if ( !d->mIgnoreInstallResult ) {
235 KMessageBox::information( mParentWidget,
236 i18n(
"Successfully installed hot new stuff.") );
239 if ( !d->mIgnoreInstallResult ){
240 KMessageBox::error( mParentWidget,
241 i18n(
"Failed to install hot new stuff.") );
247 mUploadFile = fileName;
248 mPreviewFile = previewName;
250 connect( mProviderLoader,
251 TQT_SIGNAL( providersLoaded( Provider::List * ) ),
252 TQT_SLOT( selectUploadProvider( Provider::List * ) ) );
253 mProviderLoader->
load( mType );
256 void Engine::selectUploadProvider( Provider::List *providers )
258 kdDebug() <<
"Engine:selectUploadProvider()" << endl;
260 mProviderLoader->disconnect();
262 if ( !mProviderDialog ) {
266 mProviderDialog->
clear();
268 mProviderDialog->show();
269 mProviderDialog->raise();
271 for(
Provider *p = providers->first(); p; p = providers->next() ) {
278 mUploadProvider = provider;
280 if ( !mUploadDialog ) {
281 mUploadDialog =
new UploadDialog(
this, mParentWidget );
285 mUploadDialog->show();
286 mUploadDialog->raise();
291 if ( mUploadFile.isNull()) {
293 mUploadFile = locateLocal(
"data", TQString(kapp->instanceName()) +
"/upload/" + mUploadFile );
295 if ( !d->mNewStuff->createUploadFile( mUploadFile ) ) {
296 KMessageBox::error( mParentWidget, i18n(
"Unable to create file to upload.") );
302 TQString lang = entry->
langs().first();
303 TQFileInfo fi( mUploadFile );
304 entry->
setPayload( KURL::fromPathOrURL( fi.fileName() ), lang );
306 if ( !createMetaFile( entry ) ) {
311 TQString text = i18n(
"The files to be uploaded have been created at:\n");
312 text.append( i18n(
"Data file: %1\n").arg( mUploadFile) );
313 if (!mPreviewFile.isEmpty()) {
314 text.append( i18n(
"Preview image: %1\n").arg( mPreviewFile) );
316 text.append( i18n(
"Content information: %1\n").arg( mUploadMetaFile) );
317 text.append( i18n(
"Those files can now be uploaded.\n") );
318 text.append( i18n(
"Beware that any people might have access to them at any time.") );
320 TQString caption = i18n(
"Upload Files");
322 if ( mUploadProvider->
noUpload() ) {
324 if ( noUploadUrl.isEmpty() ) {
325 text.append( i18n(
"Please upload the files manually.") );
326 KMessageBox::information( mParentWidget, text, caption );
328 int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
330 KStdGuiItem::close() );
331 if ( result == KMessageBox::Yes ) {
332 kapp->invokeBrowser( noUploadUrl.url() );
336 int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
337 i18n(
"&Upload"), KStdGuiItem::cancel() );
338 if ( result == KMessageBox::Yes ) {
339 KURL destination = mUploadProvider->
uploadUrl();
340 destination.setFileName( fi.fileName() );
342 TDEIO::FileCopyJob *job = TDEIO::file_copy( KURL::fromPathOrURL( mUploadFile ), destination );
343 connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
344 TQT_SLOT( slotUploadPayloadJobResult( TDEIO::Job * ) ) );
351 bool Engine::createMetaFile(
Entry *entry )
353 TQDomDocument doc(
"knewstuff");
354 doc.appendChild( doc.createProcessingInstruction(
355 "xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
356 TQDomElement de = doc.createElement(
"knewstuff");
357 doc.appendChild( de );
360 de.appendChild( entry->createDomElement( doc, de ) );
362 kdDebug() <<
"--DOM START--" << endl << doc.toString()
363 <<
"--DOM_END--" << endl;
365 if ( mUploadMetaFile.isNull() ) {
366 mUploadMetaFile = entry->
fullName() +
".meta";
367 mUploadMetaFile = locateLocal(
"data", TQString(kapp->instanceName()) +
"/upload/" + mUploadMetaFile );
370 TQFile f( mUploadMetaFile );
371 if ( !f.open( IO_WriteOnly ) ) {
372 mUploadMetaFile = TQString::null;
376 TQTextStream ts( &f );
377 ts.setEncoding( TQTextStream::UnicodeUTF8 );
378 ts << doc.toString();
385 void Engine::slotUploadPayloadJobResult( TDEIO::Job *job )
387 if ( job->error() ) {
388 kdDebug() <<
"Error uploading new stuff payload." << endl;
389 job->showErrorDialog( mParentWidget );
394 if (mPreviewFile.isEmpty()) {
395 slotUploadPreviewJobResult(job);
399 TQFileInfo fi( mPreviewFile );
401 KURL previewDestination = mUploadProvider->
uploadUrl();
402 previewDestination.setFileName( fi.fileName() );
404 TDEIO::FileCopyJob *newJob = TDEIO::file_copy( KURL::fromPathOrURL( mPreviewFile ), previewDestination );
405 connect( newJob, TQT_SIGNAL( result( TDEIO::Job * ) ),
406 TQT_SLOT( slotUploadPreviewJobResult( TDEIO::Job * ) ) );
409 void Engine::slotUploadPreviewJobResult( TDEIO::Job *job )
411 if ( job->error() ) {
412 kdDebug() <<
"Error uploading new stuff preview." << endl;
413 job->showErrorDialog( mParentWidget );
418 TQFileInfo fi( mUploadMetaFile );
420 KURL metaDestination = mUploadProvider->
uploadUrl();
421 metaDestination.setFileName( fi.fileName() );
423 TDEIO::FileCopyJob *newJob = TDEIO::file_copy( KURL::fromPathOrURL( mUploadMetaFile ), metaDestination );
424 connect( newJob, TQT_SIGNAL( result( TDEIO::Job * ) ),
425 TQT_SLOT( slotUploadMetaJobResult( TDEIO::Job * ) ) );
428 void Engine::slotUploadMetaJobResult( TDEIO::Job *job )
430 mUploadMetaFile = TQString::null;
431 if ( job->error() ) {
432 kdDebug() <<
"Error uploading new stuff metadata." << endl;
433 job->showErrorDialog( mParentWidget );
438 KMessageBox::information( mParentWidget,
439 i18n(
"Successfully uploaded new stuff.") );
445 d->mIgnoreInstallResult = ignore;