21 #include <tqcstring.h>
23 #include <tqfileinfo.h>
25 #include <kapplication.h>
29 #include <kmessagebox.h>
30 #include <kstandarddirs.h>
32 #include "knewstuff.h"
33 #include "downloaddialog.h"
34 #include "uploaddialog.h"
35 #include "providerdialog.h"
42 struct Engine::Private
44 bool mIgnoreInstallResult;
49 TQWidget *parentWidget ) :
50 mParentWidget( parentWidget ), mDownloadDialog( 0 ),
51 mUploadDialog( 0 ), mProviderDialog( 0 ), mUploadProvider( 0 ),
52 d(new Private), mType( type )
54 d->mNewStuff = newStuff;
55 d->mIgnoreInstallResult =
false;
60 const TQString &providerList, TQWidget *parentWidget ) :
61 mParentWidget( parentWidget ),
62 mDownloadDialog( 0 ), mUploadDialog( 0 ),
63 mProviderDialog( 0 ), mUploadProvider( 0 ),
64 mProviderList( providerList ), d(new Private),
67 d->mNewStuff = newStuff;
68 d->mIgnoreInstallResult =
false;
75 delete mProviderLoader;
78 delete mDownloadDialog;
83 kdDebug() <<
"Engine::download()" << endl;
85 connect( mProviderLoader,
86 TQT_SIGNAL( providersLoaded( Provider::List * ) ),
87 TQT_SLOT( getMetaInformation( Provider::List * ) ) );
88 mProviderLoader->
load( mType, mProviderList );
91 void Engine::getMetaInformation( Provider::List *providers )
93 mProviderLoader->disconnect();
95 mNewStuffJobData.clear();
97 if ( !mDownloadDialog ) {
99 mDownloadDialog->show();
101 mDownloadDialog->
clear();
104 for ( p = providers->first(); p; p = providers->next() ) {
107 KIO::TransferJob *job = KIO::get( p->
downloadUrl(),
false, false );
108 connect( job, TQT_SIGNAL( result( KIO::Job * ) ),
109 TQT_SLOT( slotNewStuffJobResult( KIO::Job * ) ) );
110 connect( job, TQT_SIGNAL( data( KIO::Job *,
const TQByteArray & ) ),
111 TQT_SLOT( slotNewStuffJobData( KIO::Job *,
const TQByteArray & ) ) );
113 mNewStuffJobData.insert( job,
"" );
114 mProviderJobs[ job ] = p;
118 void Engine::slotNewStuffJobData( KIO::Job *job,
const TQByteArray &data )
120 if ( data.isEmpty() )
return;
122 kdDebug() <<
"Engine:slotNewStuffJobData()" << endl;
124 TQCString str( data, data.size() + 1 );
126 mNewStuffJobData[ job ].append( TQString::fromUtf8( str ) );
129 void Engine::slotNewStuffJobResult( KIO::Job *job )
131 if ( job->error() ) {
132 kdDebug() <<
"Error downloading new stuff descriptions." << endl;
133 job->showErrorDialog( mParentWidget );
135 TQString knewstuffDoc = mNewStuffJobData[ job ];
137 kdDebug() <<
"---START---" << endl << knewstuffDoc <<
"---END---" << endl;
139 mDownloadDialog->
addProvider( mProviderJobs[ job ] );
142 if ( !doc.setContent( knewstuffDoc ) ) {
143 kdDebug() <<
"Error parsing knewstuff.xml." << endl;
146 TQDomElement knewstuff = doc.documentElement();
148 if ( knewstuff.isNull() ) {
149 kdDebug() <<
"No document in knewstuffproviders.xml." << endl;
152 for ( p = knewstuff.firstChild(); !p.isNull(); p = p.nextSibling() ) {
153 TQDomElement stuff = p.toElement();
154 if ( stuff.tagName() !=
"stuff" )
continue;
155 if ( stuff.attribute(
"type", mType) != mType )
continue;
159 mDownloadDialog->show();
163 kdDebug() <<
"KNEWSTUFF: " << entry->
name() << endl;
165 kdDebug() <<
" SUMMARY: " << entry->
summary() << endl;
166 kdDebug() <<
" VERSION: " << entry->
version() << endl;
167 kdDebug() <<
" RELEASEDATE: " << TQString(entry->
releaseDate().toString()) << endl;
168 kdDebug() <<
" RATING: " << entry->
rating() << endl;
170 kdDebug() <<
" LANGS: " << entry->
langs().join(
", ") << endl;
176 mNewStuffJobData.remove( job );
177 mProviderJobs.remove( job );
179 if ( mNewStuffJobData.count() == 0 ) {
180 mDownloadDialog->show();
181 mDownloadDialog->raise();
187 kdDebug() <<
"Engine::download(entry)" << endl;
189 KURL source = entry->
payload();
190 mDownloadDestination = d->mNewStuff->downloadDestination( entry );
192 if ( mDownloadDestination.isEmpty() ) {
193 kdDebug() <<
"Empty downloadDestination. Cancelling download." << endl;
197 KURL destination = KURL( mDownloadDestination );
199 kdDebug() <<
" SOURCE: " << source.url() << endl;
200 kdDebug() <<
" DESTINATION: " << destination.url() << endl;
202 KIO::FileCopyJob *job = KIO::file_copy( source, destination, -1,
true );
203 connect( job, TQT_SIGNAL( result( KIO::Job * ) ),
204 TQT_SLOT( slotDownloadJobResult( KIO::Job * ) ) );
207 void Engine::slotDownloadJobResult( KIO::Job *job )
209 if ( job->error() ) {
210 kdDebug() <<
"Error downloading new stuff payload." << endl;
211 job->showErrorDialog( mParentWidget );
215 if ( d->mNewStuff->install( mDownloadDestination ) ) {
216 if ( !d->mIgnoreInstallResult ) {
217 KMessageBox::information( mParentWidget,
218 i18n(
"Successfully installed hot new stuff.") );
221 if ( !d->mIgnoreInstallResult ){
222 KMessageBox::error( mParentWidget,
223 i18n(
"Failed to install hot new stuff.") );
229 mUploadFile = fileName;
230 mPreviewFile = previewName;
232 connect( mProviderLoader,
233 TQT_SIGNAL( providersLoaded( Provider::List * ) ),
234 TQT_SLOT( selectUploadProvider( Provider::List * ) ) );
235 mProviderLoader->
load( mType );
238 void Engine::selectUploadProvider( Provider::List *providers )
240 kdDebug() <<
"Engine:selectUploadProvider()" << endl;
242 mProviderLoader->disconnect();
244 if ( !mProviderDialog ) {
248 mProviderDialog->
clear();
250 mProviderDialog->show();
251 mProviderDialog->raise();
253 for(
Provider *p = providers->first(); p; p = providers->next() ) {
260 mUploadProvider = provider;
262 if ( !mUploadDialog ) {
263 mUploadDialog =
new UploadDialog(
this, mParentWidget );
267 mUploadDialog->show();
268 mUploadDialog->raise();
273 if ( mUploadFile.isNull()) {
275 mUploadFile = locateLocal(
"data", TQString(kapp->instanceName()) +
"/upload/" + mUploadFile );
277 if ( !d->mNewStuff->createUploadFile( mUploadFile ) ) {
278 KMessageBox::error( mParentWidget, i18n(
"Unable to create file to upload.") );
284 TQString lang = entry->
langs().first();
285 TQFileInfo fi( mUploadFile );
286 entry->
setPayload( KURL::fromPathOrURL( fi.fileName() ), lang );
288 if ( !createMetaFile( entry ) ) {
293 TQString text = i18n(
"The files to be uploaded have been created at:\n");
294 text.append( i18n(
"Data file: %1\n").arg( mUploadFile) );
295 if (!mPreviewFile.isEmpty()) {
296 text.append( i18n(
"Preview image: %1\n").arg( mPreviewFile) );
298 text.append( i18n(
"Content information: %1\n").arg( mUploadMetaFile) );
299 text.append( i18n(
"Those files can now be uploaded.\n") );
300 text.append( i18n(
"Beware that any people might have access to them at any time.") );
302 TQString caption = i18n(
"Upload Files");
304 if ( mUploadProvider->
noUpload() ) {
306 if ( noUploadUrl.isEmpty() ) {
307 text.append( i18n(
"Please upload the files manually.") );
308 KMessageBox::information( mParentWidget, text, caption );
310 int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
312 KStdGuiItem::close() );
313 if ( result == KMessageBox::Yes ) {
314 kapp->invokeBrowser( noUploadUrl.url() );
318 int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
319 i18n(
"&Upload"), KStdGuiItem::cancel() );
320 if ( result == KMessageBox::Yes ) {
321 KURL destination = mUploadProvider->
uploadUrl();
322 destination.setFileName( fi.fileName() );
324 KIO::FileCopyJob *job = KIO::file_copy( KURL::fromPathOrURL( mUploadFile ), destination );
325 connect( job, TQT_SIGNAL( result( KIO::Job * ) ),
326 TQT_SLOT( slotUploadPayloadJobResult( KIO::Job * ) ) );
333 bool Engine::createMetaFile(
Entry *entry )
335 TQDomDocument doc(
"knewstuff");
336 doc.appendChild( doc.createProcessingInstruction(
337 "xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
338 TQDomElement de = doc.createElement(
"knewstuff");
339 doc.appendChild( de );
342 de.appendChild( entry->createDomElement( doc, de ) );
344 kdDebug() <<
"--DOM START--" << endl << doc.toString()
345 <<
"--DOM_END--" << endl;
347 if ( mUploadMetaFile.isNull() ) {
348 mUploadMetaFile = entry->
fullName() +
".meta";
349 mUploadMetaFile = locateLocal(
"data", TQString(kapp->instanceName()) +
"/upload/" + mUploadMetaFile );
352 TQFile f( mUploadMetaFile );
353 if ( !f.open( IO_WriteOnly ) ) {
354 mUploadMetaFile = TQString::null;
358 TQTextStream ts( &f );
359 ts.setEncoding( TQTextStream::UnicodeUTF8 );
360 ts << doc.toString();
367 void Engine::slotUploadPayloadJobResult( KIO::Job *job )
369 if ( job->error() ) {
370 kdDebug() <<
"Error uploading new stuff payload." << endl;
371 job->showErrorDialog( mParentWidget );
376 if (mPreviewFile.isEmpty()) {
377 slotUploadPreviewJobResult(job);
381 TQFileInfo fi( mPreviewFile );
383 KURL previewDestination = mUploadProvider->
uploadUrl();
384 previewDestination.setFileName( fi.fileName() );
386 KIO::FileCopyJob *newJob = KIO::file_copy( KURL::fromPathOrURL( mPreviewFile ), previewDestination );
387 connect( newJob, TQT_SIGNAL( result( KIO::Job * ) ),
388 TQT_SLOT( slotUploadPreviewJobResult( KIO::Job * ) ) );
391 void Engine::slotUploadPreviewJobResult( KIO::Job *job )
393 if ( job->error() ) {
394 kdDebug() <<
"Error uploading new stuff preview." << endl;
395 job->showErrorDialog( mParentWidget );
400 TQFileInfo fi( mUploadMetaFile );
402 KURL metaDestination = mUploadProvider->
uploadUrl();
403 metaDestination.setFileName( fi.fileName() );
405 KIO::FileCopyJob *newJob = KIO::file_copy( KURL::fromPathOrURL( mUploadMetaFile ), metaDestination );
406 connect( newJob, TQT_SIGNAL( result( KIO::Job * ) ),
407 TQT_SLOT( slotUploadMetaJobResult( KIO::Job * ) ) );
410 void Engine::slotUploadMetaJobResult( KIO::Job *job )
412 mUploadMetaFile = TQString::null;
413 if ( job->error() ) {
414 kdDebug() <<
"Error uploading new stuff metadata." << endl;
415 job->showErrorDialog( mParentWidget );
420 KMessageBox::information( mParentWidget,
421 i18n(
"Successfully uploaded new stuff.") );
427 d->mIgnoreInstallResult = ignore;
void download()
Initiates the download process, retrieving provider lists and invoking the download dialog...
int rating()
Retrieve the rating for the object, which has been determined by its users and thus might change over...
KURL noUploadUrl() const
Retrieves the URL where a user is led if the provider does not support uploads.
KNewStuff data entry container.
KURL downloadUrl() const
Retrieves the download URL.
void addProvider(Provider *p)
Adds another provider to the download dialog.
void addProvider(Provider *)
Adds a Hotstuff provider to the list.
bool noUpload() const
Query whether provider supports uploads.
void load(const TQString &type, const TQString &providerList=TQString::null)
Starts asynchronously loading the list of providers of the specified type.
KNewStuff file upload dialog.
void clear()
Clears the entry list of the current provider.
TQStringList langs()
Return the list of languages this object supports.
void addEntry(Entry *entry)
Adds an additional entry to the current provider.
TQString fullName()
Return the full name for the meta information.
void setType(const TQString &)
Sets the application type, e.g.
TQString name() const
Retrieve the name of the data object.
void setPreviewFile(const TQString &previewFile)
Sets the preview filename.
void requestMetaInformation(Provider *provider)
Asynchronous lookup of provider information such as upload and download locations, icon etc.
void setPayloadFile(const TQString &payloadFile)
Sets the payload filename.
KURL uploadUrl() const
Retrieves the upload URL.
void ignoreInstallResult(bool ignore)
Ignores the return value of the install method.
virtual ~Engine()
Destructor.
TQString summary(const TQString &lang=TQString::null) const
Retrieve a short description about the object.
This class provides the functionality to download and upload "new stuff".
void clear()
Clears the list of providers.
TQString type() const
Returns the previously set data type.
void upload(const TQString &fileName=TQString::null, const TQString &previewName=TQString::null)
Initiates the upload process, invoking the provider selection dialog and the file upload dialog...
Engine(KNewStuff *newStuff, const TQString &type, TQWidget *parentWidget=0)
Constructor.
KURL payload(const TQString &lang=TQString::null) const
Retrieve the file name of the object.
TQDate releaseDate() const
Retrieve the date of the object's publication.
Dialog displaying a list of Hotstuff providers.
void uploadFinished(bool result)
Emitted when the upload has finished.
KNewStuff provider loader.
Handles security releated issues, like signing, verifying.
TQString version() const
Retrieve the version string of the object.
KNewStuff provider container.
Common download dialog for data browsing and installation.
void setPayload(const KURL &, const TQString &lang=TQString::null)
Sets the object's file.