24#include <tqfileinfo.h> 
   26#include <tdeapplication.h> 
   30#include <tdemessagebox.h> 
   31#include <tdestandarddirs.h> 
   34#include "downloaddialog.h" 
   35#include "uploaddialog.h" 
   36#include "providerdialog.h" 
   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           TQ_SIGNAL( providersLoaded( Provider::List * ) ),
 
   88           TQ_SLOT( getMetaInformation( Provider::List * ) ) );
 
   89  mProviderLoader->
load( mType, mProviderList );
 
   92void 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, TQ_SIGNAL( result( TDEIO::Job * ) ),
 
  110             TQ_SLOT( slotNewStuffJobResult( TDEIO::Job * ) ) );
 
  111    connect( job, TQ_SIGNAL( data( TDEIO::Job *, 
const TQByteArray & ) ),
 
  112             TQ_SLOT( slotNewStuffJobData( TDEIO::Job *, 
const TQByteArray & ) ) );
 
  114    mNewStuffJobData.insert( job, 
"" );
 
  115    mProviderJobs[ job ] = p;
 
  119void 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 ) );
 
  130void 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, TQ_SIGNAL( result( TDEIO::Job * ) ),
 
  222           TQ_SLOT( slotDownloadJobResult( TDEIO::Job * ) ) );
 
  225void 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           TQ_SIGNAL( providersLoaded( Provider::List * ) ),
 
  252           TQ_SLOT( selectUploadProvider( Provider::List * ) ) );
 
  253  mProviderLoader->
load( mType );
 
  256void 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(tdeApp->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        tdeApp->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, TQ_SIGNAL( result( TDEIO::Job * ) ),
 
  344               TQ_SLOT( slotUploadPayloadJobResult( TDEIO::Job * ) ) );
 
  351bool 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(tdeApp->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();
 
  385void 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, TQ_SIGNAL( result( TDEIO::Job * ) ),
 
  406           TQ_SLOT( slotUploadPreviewJobResult( TDEIO::Job * ) ) );
 
  409void 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, TQ_SIGNAL( result( TDEIO::Job * ) ),
 
  425           TQ_SLOT( slotUploadMetaJobResult( TDEIO::Job * ) ) );
 
  428void 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;
 
Common download dialog for data browsing and installation.
 
void addProvider(Provider *p)
Adds another provider to the download dialog.
 
void addEntry(Entry *entry)
Adds an additional entry to the current provider.
 
void clear()
Clears the entry list of the current provider.
 
Engine(TDENewStuff *newStuff, const TQString &type, TQWidget *parentWidget=0)
Constructor.
 
void download()
Initiates the download process, retrieving provider lists and invoking the download dialog.
 
TQString type() const
Returns the previously set data type.
 
virtual ~Engine()
Destructor.
 
void uploadFinished(bool result)
Emitted when the upload has finished.
 
void ignoreInstallResult(bool ignore)
Ignores the return value of the install method.
 
void requestMetaInformation(Provider *provider)
Asynchronous lookup of provider information such as upload and download locations,...
 
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.
 
TDENewStuff data entry container.
 
TQString fullName()
Return the full name for the meta information.
 
int rating()
Retrieve the rating for the object, which has been determined by its users and thus might change over...
 
TQString name() const
Retrieve the name of the data object.
 
TQDate releaseDate() const
Retrieve the date of the object's publication.
 
KURL payload(const TQString &lang=TQString::null) const
Retrieve the file name of the object.
 
TQStringList langs()
Return the list of languages this object supports.
 
TQString summary(const TQString &lang=TQString::null) const
Retrieve a short description about the object.
 
void setType(const TQString &)
Sets the application type, e.g.
 
TQString version() const
Retrieve the version string of the object.
 
void setPayload(const KURL &, const TQString &lang=TQString::null)
Sets the object's file.
 
Dialog displaying a list of Hotstuff providers.
 
void addProvider(Provider *)
Adds a Hotstuff provider to the list.
 
void clear()
Clears the list of providers.
 
TDENewStuff provider loader.
 
void load(const TQString &type, const TQString &providerList=TQString::null)
Starts asynchronously loading the list of providers of the specified type.
 
TDENewStuff provider container.
 
KURL downloadUrl() const
Retrieves the download URL.
 
KURL uploadUrl() const
Retrieves the upload URL.
 
bool noUpload() const
Query whether provider supports uploads.
 
KURL noUploadUrl() const
Retrieves the URL where a user is led if the provider does not support uploads.
 
TDENewStuff file upload dialog.
 
void setPayloadFile(const TQString &payloadFile)
Sets the payload filename.
 
void setPreviewFile(const TQString &previewFile)
Sets the preview filename.
 
This class provides the functionality to download and upload "new stuff".
 
Handles security releated issues, like signing, verifying.