• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • knewstuff
 

knewstuff

  • knewstuff
engine.cpp
1 /*
2  This file is part of KOrganizer.
3  Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
4 
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 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include <tqcstring.h>
22 #include <tqdom.h>
23 #include <tqfileinfo.h>
24 
25 #include <kapplication.h>
26 #include <kdebug.h>
27 #include <kio/job.h>
28 #include <klocale.h>
29 #include <kmessagebox.h>
30 #include <kstandarddirs.h>
31 
32 #include "knewstuff.h"
33 #include "downloaddialog.h"
34 #include "uploaddialog.h"
35 #include "providerdialog.h"
36 
37 #include "engine.h"
38 #include "engine.moc"
39 
40 using namespace KNS;
41 
42 struct Engine::Private
43 {
44  bool mIgnoreInstallResult;
45  KNewStuff *mNewStuff;
46 };
47 
48 Engine::Engine( KNewStuff *newStuff, const TQString &type,
49  TQWidget *parentWidget ) :
50  mParentWidget( parentWidget ), mDownloadDialog( 0 ),
51  mUploadDialog( 0 ), mProviderDialog( 0 ), mUploadProvider( 0 ),
52  d(new Private), mType( type )
53 {
54  d->mNewStuff = newStuff;
55  d->mIgnoreInstallResult = false;
56  mProviderLoader = new ProviderLoader( mParentWidget );
57 }
58 
59 Engine::Engine( KNewStuff *newStuff, const TQString &type,
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),
65  mType( type )
66 {
67  d->mNewStuff = newStuff;
68  d->mIgnoreInstallResult = false;
69  mProviderLoader = new ProviderLoader( mParentWidget );
70 }
71 
72 Engine::~Engine()
73 {
74  delete d;
75  delete mProviderLoader;
76 
77  delete mUploadDialog;
78  delete mDownloadDialog;
79 }
80 
81 void Engine::download()
82 {
83  kdDebug() << "Engine::download()" << endl;
84 
85  connect( mProviderLoader,
86  TQT_SIGNAL( providersLoaded( Provider::List * ) ),
87  TQT_SLOT( getMetaInformation( Provider::List * ) ) );
88  mProviderLoader->load( mType, mProviderList );
89 }
90 
91 void Engine::getMetaInformation( Provider::List *providers )
92 {
93  mProviderLoader->disconnect();
94 
95  mNewStuffJobData.clear();
96 
97  if ( !mDownloadDialog ) {
98  mDownloadDialog = new DownloadDialog( this, mParentWidget );
99  mDownloadDialog->show();
100  }
101  mDownloadDialog->clear();
102 
103  Provider *p;
104  for ( p = providers->first(); p; p = providers->next() ) {
105  if ( p->downloadUrl().isEmpty() ) continue;
106 
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 & ) ) );
112 
113  mNewStuffJobData.insert( job, "" );
114  mProviderJobs[ job ] = p;
115  }
116 }
117 
118 void Engine::slotNewStuffJobData( KIO::Job *job, const TQByteArray &data )
119 {
120  if ( data.isEmpty() ) return;
121 
122  kdDebug() << "Engine:slotNewStuffJobData()" << endl;
123 
124  TQCString str( data, data.size() + 1 );
125 
126  mNewStuffJobData[ job ].append( TQString::fromUtf8( str ) );
127 }
128 
129 void Engine::slotNewStuffJobResult( KIO::Job *job )
130 {
131  if ( job->error() ) {
132  kdDebug() << "Error downloading new stuff descriptions." << endl;
133  job->showErrorDialog( mParentWidget );
134  } else {
135  TQString knewstuffDoc = mNewStuffJobData[ job ];
136 
137  kdDebug() << "---START---" << endl << knewstuffDoc << "---END---" << endl;
138 
139  mDownloadDialog->addProvider( mProviderJobs[ job ] );
140 
141  TQDomDocument doc;
142  if ( !doc.setContent( knewstuffDoc ) ) {
143  kdDebug() << "Error parsing knewstuff.xml." << endl;
144  return;
145  } else {
146  TQDomElement knewstuff = doc.documentElement();
147 
148  if ( knewstuff.isNull() ) {
149  kdDebug() << "No document in knewstuffproviders.xml." << endl;
150  } else {
151  TQDomNode p;
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;
156 
157  Entry *entry = new Entry( stuff );
158 
159  mDownloadDialog->show();
160 
161  mDownloadDialog->addEntry( entry );
162 
163  kdDebug() << "KNEWSTUFF: " << entry->name() << endl;
164 
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;
169 
170  kdDebug() << " LANGS: " << entry->langs().join(", ") << endl;
171  }
172  }
173  }
174  }
175 
176  mNewStuffJobData.remove( job );
177  mProviderJobs.remove( job );
178 
179  if ( mNewStuffJobData.count() == 0 ) {
180  mDownloadDialog->show();
181  mDownloadDialog->raise();
182  }
183 }
184 
185 void Engine::download( Entry *entry )
186 {
187  kdDebug() << "Engine::download(entry)" << endl;
188 
189  KURL source = entry->payload();
190  mDownloadDestination = d->mNewStuff->downloadDestination( entry );
191 
192  if ( mDownloadDestination.isEmpty() ) {
193  kdDebug() << "Empty downloadDestination. Cancelling download." << endl;
194  return;
195  }
196 
197  KURL destination = KURL( mDownloadDestination );
198 
199  kdDebug() << " SOURCE: " << source.url() << endl;
200  kdDebug() << " DESTINATION: " << destination.url() << endl;
201 
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 * ) ) );
205 }
206 
207 void Engine::slotDownloadJobResult( KIO::Job *job )
208 {
209  if ( job->error() ) {
210  kdDebug() << "Error downloading new stuff payload." << endl;
211  job->showErrorDialog( mParentWidget );
212  return;
213  }
214 
215  if ( d->mNewStuff->install( mDownloadDestination ) ) {
216  if ( !d->mIgnoreInstallResult ) {
217  KMessageBox::information( mParentWidget,
218  i18n("Successfully installed hot new stuff.") );
219  }
220  } else
221  if ( !d->mIgnoreInstallResult ){
222  KMessageBox::error( mParentWidget,
223  i18n("Failed to install hot new stuff.") );
224  }
225 }
226 
227 void Engine::upload(const TQString &fileName, const TQString &previewName )
228 {
229  mUploadFile = fileName;
230  mPreviewFile = previewName;
231 
232  connect( mProviderLoader,
233  TQT_SIGNAL( providersLoaded( Provider::List * ) ),
234  TQT_SLOT( selectUploadProvider( Provider::List * ) ) );
235  mProviderLoader->load( mType );
236 }
237 
238 void Engine::selectUploadProvider( Provider::List *providers )
239 {
240  kdDebug() << "Engine:selectUploadProvider()" << endl;
241 
242  mProviderLoader->disconnect();
243 
244  if ( !mProviderDialog ) {
245  mProviderDialog = new ProviderDialog( this, mParentWidget );
246  }
247 
248  mProviderDialog->clear();
249 
250  mProviderDialog->show();
251  mProviderDialog->raise();
252 
253  for( Provider *p = providers->first(); p; p = providers->next() ) {
254  mProviderDialog->addProvider( p );
255  }
256 }
257 
258 void Engine::requestMetaInformation( Provider *provider )
259 {
260  mUploadProvider = provider;
261 
262  if ( !mUploadDialog ) {
263  mUploadDialog = new UploadDialog( this, mParentWidget );
264  }
265  mUploadDialog->setPreviewFile( mPreviewFile );
266  mUploadDialog->setPayloadFile( mUploadFile );
267  mUploadDialog->show();
268  mUploadDialog->raise();
269 }
270 
271 void Engine::upload( Entry *entry )
272 {
273  if ( mUploadFile.isNull()) {
274  mUploadFile = entry->fullName();
275  mUploadFile = locateLocal( "data", TQString(kapp->instanceName()) + "/upload/" + mUploadFile );
276 
277  if ( !d->mNewStuff->createUploadFile( mUploadFile ) ) {
278  KMessageBox::error( mParentWidget, i18n("Unable to create file to upload.") );
279  emit uploadFinished( false );
280  return;
281  }
282  }
283 
284  TQString lang = entry->langs().first();
285  TQFileInfo fi( mUploadFile );
286  entry->setPayload( KURL::fromPathOrURL( fi.fileName() ), lang );
287 
288  if ( !createMetaFile( entry ) ) {
289  emit uploadFinished( false );
290  return;
291  }
292 
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) );
297  }
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.") );
301 
302  TQString caption = i18n("Upload Files");
303 
304  if ( mUploadProvider->noUpload() ) {
305  KURL noUploadUrl = mUploadProvider->noUploadUrl();
306  if ( noUploadUrl.isEmpty() ) {
307  text.append( i18n("Please upload the files manually.") );
308  KMessageBox::information( mParentWidget, text, caption );
309  } else {
310  int result = KMessageBox::questionYesNo( mParentWidget, text, caption,
311  i18n("Upload Info"),
312  KStdGuiItem::close() );
313  if ( result == KMessageBox::Yes ) {
314  kapp->invokeBrowser( noUploadUrl.url() );
315  }
316  }
317  } else {
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() );
323 
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 * ) ) );
327  } else {
328  emit uploadFinished( false );
329  }
330  }
331 }
332 
333 bool Engine::createMetaFile( Entry *entry )
334 {
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 );
340 
341  entry->setType(type());
342  de.appendChild( entry->createDomElement( doc, de ) );
343 
344  kdDebug() << "--DOM START--" << endl << doc.toString()
345  << "--DOM_END--" << endl;
346 
347  if ( mUploadMetaFile.isNull() ) {
348  mUploadMetaFile = entry->fullName() + ".meta";
349  mUploadMetaFile = locateLocal( "data", TQString(kapp->instanceName()) + "/upload/" + mUploadMetaFile );
350  }
351 
352  TQFile f( mUploadMetaFile );
353  if ( !f.open( IO_WriteOnly ) ) {
354  mUploadMetaFile = TQString::null;
355  return false;
356  }
357 
358  TQTextStream ts( &f );
359  ts.setEncoding( TQTextStream::UnicodeUTF8 );
360  ts << doc.toString();
361 
362  f.close();
363 
364  return true;
365 }
366 
367 void Engine::slotUploadPayloadJobResult( KIO::Job *job )
368 {
369  if ( job->error() ) {
370  kdDebug() << "Error uploading new stuff payload." << endl;
371  job->showErrorDialog( mParentWidget );
372  emit uploadFinished( false );
373  return;
374  }
375 
376  if (mPreviewFile.isEmpty()) {
377  slotUploadPreviewJobResult(job);
378  return;
379  }
380 
381  TQFileInfo fi( mPreviewFile );
382 
383  KURL previewDestination = mUploadProvider->uploadUrl();
384  previewDestination.setFileName( fi.fileName() );
385 
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 * ) ) );
389 }
390 
391 void Engine::slotUploadPreviewJobResult( KIO::Job *job )
392 {
393  if ( job->error() ) {
394  kdDebug() << "Error uploading new stuff preview." << endl;
395  job->showErrorDialog( mParentWidget );
396  emit uploadFinished( true );
397  return;
398  }
399 
400  TQFileInfo fi( mUploadMetaFile );
401 
402  KURL metaDestination = mUploadProvider->uploadUrl();
403  metaDestination.setFileName( fi.fileName() );
404 
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 * ) ) );
408 }
409 
410 void Engine::slotUploadMetaJobResult( KIO::Job *job )
411 {
412  mUploadMetaFile = TQString::null;
413  if ( job->error() ) {
414  kdDebug() << "Error uploading new stuff metadata." << endl;
415  job->showErrorDialog( mParentWidget );
416  emit uploadFinished( false );
417  return;
418  }
419 
420  KMessageBox::information( mParentWidget,
421  i18n("Successfully uploaded new stuff.") );
422  emit uploadFinished( true );
423 }
424 
425 void Engine::ignoreInstallResult(bool ignore)
426 {
427  d->mIgnoreInstallResult = ignore;
428 }
KNS::Entry::payload
KURL payload(const TQString &lang=TQString::null) const
Retrieve the file name of the object.
Definition: entry.cpp:227
KNS::Engine::download
void download()
Initiates the download process, retrieving provider lists and invoking the download dialog...
Definition: engine.cpp:81
KNS::Entry::rating
int rating()
Retrieve the rating for the object, which has been determined by its users and thus might change over...
Definition: entry.cpp:271
KNS::Engine::type
TQString type() const
Returns the previously set data type.
Definition: engine.h:84
KNS::Entry
KNewStuff data entry container.
Definition: entry.h:44
KNS::DownloadDialog::addProvider
void addProvider(Provider *p)
Adds another provider to the download dialog.
Definition: downloaddialog.cpp:215
KNS::ProviderDialog::addProvider
void addProvider(Provider *)
Adds a Hotstuff provider to the list.
Definition: providerdialog.cpp:74
KNS::Engine::parentWidget
TQWidget * parentWidget() const
Returns the previously set parent widget.
Definition: engine.h:91
KNS::ProviderLoader::load
void load(const TQString &type, const TQString &providerList=TQString::null)
Starts asynchronously loading the list of providers of the specified type.
Definition: provider.cpp:215
KNS::UploadDialog
KNewStuff file upload dialog.
Definition: uploaddialog.h:47
KNS::Provider::noUploadUrl
KURL noUploadUrl() const
Retrieves the URL where a user is led if the provider does not support uploads.
Definition: provider.cpp:154
KNS::Provider::noUpload
bool noUpload() const
Query whether provider supports uploads.
Definition: provider.cpp:165
KNS::DownloadDialog::clear
void clear()
Clears the entry list of the current provider.
Definition: downloaddialog.cpp:175
KNS::Entry::langs
TQStringList langs()
Return the list of languages this object supports.
Definition: entry.cpp:295
KNS::DownloadDialog::addEntry
void addEntry(Entry *entry)
Adds an additional entry to the current provider.
Definition: downloaddialog.cpp:448
KNS::Entry::fullName
TQString fullName()
Return the full name for the meta information.
Definition: entry.cpp:287
KNS::Provider::downloadUrl
KURL downloadUrl() const
Retrieves the download URL.
Definition: provider.cpp:132
KNS::Entry::setType
void setType(const TQString &)
Sets the application type, e.g.
Definition: entry.cpp:132
KNS::Entry::summary
TQString summary(const TQString &lang=TQString::null) const
Retrieve a short description about the object.
Definition: entry.cpp:172
KNS::Provider::uploadUrl
KURL uploadUrl() const
Retrieves the upload URL.
Definition: provider.cpp:143
KNS::UploadDialog::setPreviewFile
void setPreviewFile(const TQString &previewFile)
Sets the preview filename.
Definition: uploaddialog.cpp:158
KNS::Engine::requestMetaInformation
void requestMetaInformation(Provider *provider)
Asynchronous lookup of provider information such as upload and download locations, icon etc.
Definition: engine.cpp:258
KNS::UploadDialog::setPayloadFile
void setPayloadFile(const TQString &payloadFile)
Sets the payload filename.
Definition: uploaddialog.cpp:163
KNS::Engine::ignoreInstallResult
void ignoreInstallResult(bool ignore)
Ignores the return value of the install method.
Definition: engine.cpp:425
KNS::Engine::~Engine
virtual ~Engine()
Destructor.
Definition: engine.cpp:72
KNewStuff
This class provides the functionality to download and upload "new stuff".
Definition: knewstuff.h:69
KNS::ProviderDialog::clear
void clear()
Clears the list of providers.
Definition: providerdialog.cpp:69
KNS::Engine::upload
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...
Definition: engine.cpp:227
KNS::Engine::Engine
Engine(KNewStuff *newStuff, const TQString &type, TQWidget *parentWidget=0)
Constructor.
Definition: engine.cpp:48
KNS::ProviderDialog
Dialog displaying a list of Hotstuff providers.
Definition: providerdialog.h:43
KNS::Engine::uploadFinished
void uploadFinished(bool result)
Emitted when the upload has finished.
KNS::ProviderLoader
KNewStuff provider loader.
Definition: provider.h:172
KNS
Handles security releated issues, like signing, verifying.
Definition: downloaddialog.h:36
KNS::Entry::releaseDate
TQDate releaseDate() const
Retrieve the date of the object&#39;s publication.
Definition: entry.cpp:214
KNS::Entry::version
TQString version() const
Retrieve the version string of the object.
Definition: entry.cpp:192
KNS::Provider
KNewStuff provider container.
Definition: provider.h:46
KNS::Entry::name
TQString name() const
Retrieve the name of the data object.
Definition: entry.cpp:126
KNS::DownloadDialog
Common download dialog for data browsing and installation.
Definition: downloaddialog.h:57
KNS::Entry::setPayload
void setPayload(const KURL &, const TQString &lang=TQString::null)
Sets the object&#39;s file.
Definition: entry.cpp:220

knewstuff

Skip menu "knewstuff"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

knewstuff

Skip menu "knewstuff"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for knewstuff by doxygen 1.8.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |