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

tdenewstuff

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

tdenewstuff

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

tdenewstuff

Skip menu "tdenewstuff"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdenewstuff by doxygen 1.9.1
This website is maintained by Timothy Pearson.