• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KNewStuff

  • sources
  • kde-4.14
  • kdelibs
  • knewstuff
  • knewstuff2
knewstuff2/engine.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KNewStuff2.
3  Copyright (c) 2008 Jeremy Whiting <jpwhiting@kde.org>
4  Copyright (c) 2007 Josef Spillner <spillner@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 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  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "knewstuff2/engine.h"
20 
21 #include "knewstuff2/ui/downloaddialog.h"
22 #include "knewstuff2/ui/uploaddialog.h"
23 #include "knewstuff2/ui/providerdialog.h"
24 
25 #include "knewstuff2/core/entryhandler.h" // tmp
26 
27 #include <kcomponentdata.h>
28 #include <kdebug.h>
29 #include <kglobal.h>
30 #include <kwindowsystem.h>
31 
32 #include <qeventloop.h>
33 
34 using namespace KNS;
35 
36 class KNS::EnginePrivate : public DxsEngine
37 {
38  Q_OBJECT
39 
40 public:
41  EnginePrivate(QWidget* parent)
42  : DxsEngine(parent) {
43  m_command = EnginePrivate::command_none;
44  m_uploaddialog = NULL;
45  m_downloaddialog = NULL;
46  m_uploadedEntry = NULL;
47  m_modal = false;
48  m_parent = parent;
49  m_loop = 0;
50  }
51 
52  enum Command {
53  command_none,
54  command_upload,
55  command_download
56  };
57 
58  void workflow();
59  KNS::Entry* upload(const QString& file);
60 
61  static QHash<QString, QPointer<KDialog> > s_dialogs;
62 
63  Command m_command;
64  UploadDialog *m_uploaddialog;
65  DownloadDialog *m_downloaddialog;
66  QString m_uploadfile;
67  KNS::Entry *m_uploadedEntry;
68  KNS::Provider::List m_providers;
69  bool m_modal;
70  QWidget * m_parent;
71  QSet<KNS::Entry*> m_changedEntries;
72  QEventLoop* m_loop;
73 
74 public Q_SLOTS:
75  void slotDownloadDialogDestroyed();
76 
77 private Q_SLOTS:
79  void stopLoop();
80 
81  void slotProviderLoaded(KNS::Provider *provider);
82 
85  void slotEntryChanged(KNS::Entry *entry);
86 
87  void slotHandleUpload();
88  void slotEntriesFinished();
89 
90  void slotDownloadDialogClosed();
91 };
92 
93 
94 QHash<QString, QPointer<KDialog> > KNS::EnginePrivate::s_dialogs;
95 
96 Engine::Engine(QWidget* parent)
97  : d(new EnginePrivate(parent))
98 {
99 }
100 
101 Engine::~Engine()
102 {
103  //kDebug() << d->m_downloaddialog;
104  if (d->m_downloaddialog) {
105  d->slotDownloadDialogDestroyed();
106  }
107 
108  delete d;
109 }
110 
111 void EnginePrivate::workflow()
112 {
113  disconnect(this, 0, this, 0);
114  if ((m_command == command_upload) || (m_command == command_download)) {
115  connect(this,
116  SIGNAL(signalProviderLoaded(KNS::Provider*)),
117  SLOT(slotProviderLoaded(KNS::Provider*)));
118  }
119 
120  if (m_command == command_upload) {
121  connect(this,
122  SIGNAL(signalProvidersFinished()),
123  SLOT(slotHandleUpload()));
124  connect(this,
125  SIGNAL(signalProvidersFailed()),
126  SLOT(stopLoop()));
127 
128  m_uploadedEntry = NULL;
129  }
130 
131  if (m_command == command_download) {
132  m_downloaddialog = new DownloadDialog(this, m_parent);
133  //kDebug() << "adding!";
134  s_dialogs.insert(componentName(), m_downloaddialog);
135 
136  connect(this, SIGNAL(signalEntriesFinished()),
137  SLOT(slotEntriesFinished()));
138  connect(this,
139  SIGNAL(signalEntryChanged(KNS::Entry*)),
140  SLOT(slotEntryChanged(KNS::Entry*)));
141  connect(this,
142  SIGNAL(signalProvidersFailed()),
143  SLOT(slotDownloadDialogClosed()));
144  connect(m_downloaddialog,
145  SIGNAL(destroyed(QObject*)),
146  SLOT(slotDownloadDialogDestroyed()));
147  connect(m_downloaddialog, SIGNAL(finished()), SLOT(slotDownloadDialogClosed()));
148  //kDebug() << "done adding!";
149 
150  m_downloaddialog->show();
151  }
152 
153  start();
154 
155  if (m_modal) {
156  QEventLoop loop;
157  m_loop = &loop;
158  loop.exec();
159  }
160 }
161 
162 void EnginePrivate::stopLoop()
163 {
164  m_command = command_none;
165 
166  if (m_loop) {
167  m_loop->exit();
168  m_loop = 0;
169 
170  if (m_downloaddialog) {
171  slotDownloadDialogDestroyed();
172  }
173  }
174 }
175 
176 KNS::Entry::List Engine::download()
177 {
178  KNS::Entry::List entries;
179 
180  Engine *engine = new Engine(0);
181 
182  KComponentData component = KGlobal::activeComponent();
183  QString name = component.componentName();
184 
185  bool ret = engine->init(name + ".knsrc");
186  if (!ret) {
187  delete engine;
188  return entries;
189  }
190 
191  KNS::Entry::List tempList = engine->downloadDialogModal(0);
192 
193  // copy the list since the entries will be deleted when we delete the engine
194  foreach(Entry * entry, tempList) {
195  entries << new Entry(*entry);
196  }
197  delete engine;
198 
199  return entries;
200 }
201 
202 KNS::Entry::List Engine::downloadDialogModal(QWidget* parent)
203 {
204  //kDebug() << "Engine: downloadDialogModal";
205  KDialog *existingDialog = EnginePrivate::s_dialogs.value(d->componentName());
206  if (existingDialog) {
207  existingDialog->show();
208  KWindowSystem::setOnDesktop(existingDialog->winId(), KWindowSystem::currentDesktop());
209  KWindowSystem::activateWindow(existingDialog->winId());
210  return QList<KNS::Entry*>();
211  }
212 
213  d->m_command = EnginePrivate::command_download;
214  d->m_modal = true;
215  if (parent) {
216  d->m_parent = parent;
217  }
218 
219  d->workflow();
220 
221  return QList<KNS::Entry*>::fromSet(d->m_changedEntries);
222 }
223 
224 void Engine::downloadDialog()
225 {
226  //kDebug() << "Engine: downloadDialog";
227  KDialog *existingDialog = EnginePrivate::s_dialogs.value(d->componentName());
228  if (existingDialog) {
229  //kDebug() << "got an existing dialog";
230  existingDialog->show();
231  KWindowSystem::setOnDesktop(existingDialog->winId(), KWindowSystem::currentDesktop());
232  KWindowSystem::activateWindow(existingDialog->winId());
233  return;
234  }
235 
236  if (d->m_command != EnginePrivate::command_none) {
237  kError() << "Engine: asynchronous workflow already going on" << endl;
238  return;
239  }
240 
241  d->m_command = EnginePrivate::command_download;
242  d->m_modal = false;
243 
244  d->workflow();
245 }
246 
247 void Engine::downloadDialog(QObject * receiver, const char * slot)
248 {
249  QObject::disconnect(d, SIGNAL(signalDownloadDialogDone(KNS::Entry::List)), receiver, slot);
250  QObject::connect(d, SIGNAL(signalDownloadDialogDone(KNS::Entry::List)), receiver, slot);
251  downloadDialog();
252 }
253 
254 KNS::Entry *EnginePrivate::upload(const QString& file)
255 {
256  KNS::Entry *entry = NULL;
257 
258  Engine engine(0);
259 
260  KComponentData component = KGlobal::activeComponent();
261  QString name = component.componentName();
262 
263  bool ret = engine.init(name + ".knsrc");
264  if (!ret) return entry;
265 
266  entry = engine.uploadDialogModal(file);
267 
268  // FIXME: refcounting?
269  return entry;
270 }
271 
272 bool Engine::init(const QString& config)
273 {
274  return d->init(config);
275 }
276 
277 
278 KNS::Entry *Engine::upload(const QString& file)
279 {
280 #ifdef __GNUC__
281 #warning KNS::Engine::upload() not implemented!
282 #endif
283 #if 0
284  return d->upload(file);
285 #else
286  Q_UNUSED(file);
287 #endif
288  Q_ASSERT(false);
289  return 0;
290 }
291 
292 KNS::Entry *Engine::uploadDialogModal(const QString& file)
293 {
294  //kDebug() << "Engine: uploadDialogModal";
295 
296  d->m_command = EnginePrivate::command_upload;
297  d->m_modal = true;
298  d->m_uploadfile = file;
299 
300  d->workflow();
301 
302  return d->m_uploadedEntry;
303 }
304 
305 void Engine::uploadDialog(const QString& file)
306 {
307  //kDebug() << "Engine: uploadDialog";
308 
309  if (d->m_command != EnginePrivate::command_none) {
310  kError() << "Engine: asynchronous workflow already going on" << endl;
311  return;
312  }
313 
314  d->m_command = EnginePrivate::command_upload;
315  d->m_modal = false;
316  d->m_uploadfile = file;
317 
318  d->workflow();
319 }
320 
321 void EnginePrivate::slotProviderLoaded(KNS::Provider *provider)
322 {
323  if (m_command == command_download) {
324  loadEntries(provider);
325  } else if (m_command == command_upload) {
326  // FIXME: inject into upload dialog
327  // FIXME: dialog could do this by itself!
328 
329  // FIXME: for the modal dialog, do nothing here
330  // ... and wait for slotProvidersFinished()
331  m_providers.append(provider);
332  } else {
333  kError() << "Engine: invalid command" << endl;
334  }
335 }
336 
337 void EnginePrivate::slotHandleUpload()
338 {
339  // NOTE: this is only connected when we are doing an upload
340  //kDebug() << "Engine: slotProvidersFinished";
341 
342  //Provider *fakeprovider = new Provider();
343  //fakeprovider->setName(QString("Fake Provider"));
344  //fakeprovider->setUploadUrl(KUrl("http://localhost/dav/"));
345  //fakeprovider->setUploadUrl(KUrl("webdav://localhost/uploads/"));
346 
347 
348  // let the user select the provider
349  QPointer<ProviderDialog> provdialog = new ProviderDialog(NULL);
350  for (Provider::List::Iterator it = m_providers.begin(); it != m_providers.end(); ++it) {
351  Provider *provider = (*it);
352  provdialog->addProvider(provider);
353  }
354  //provdialog.addProvider(fakeprovider);
355  if (provdialog->exec() == QDialog::Rejected) {
356  stopLoop();
357  return;
358  }
359 
360  KNS::Provider *provider = provdialog->provider();
361 
362  // fill in the details of the upload (name, author...)
363  QPointer<UploadDialog> uploaddialog = new UploadDialog(NULL);
364  uploaddialog->setPayloadFile(KUrl(m_uploadfile));
365  if (uploaddialog->exec() == QDialog::Rejected) {
366  stopLoop();
367  return;
368  }
369 
370  Entry *entry = uploaddialog->entry();
371  if (!entry) {
372  stopLoop();
373  return;
374  }
375 
376  KTranslatable payload;
377  // add all the translations to the payload
378  QStringList langs = entry->name().languages();
379  for (QStringList::const_iterator it = langs.constBegin(); it != langs.constEnd(); ++it) {
380  payload.addString(*it, m_uploadfile);
381  }
382  entry->setPayload(payload);
383 
384  EntryHandler eh(*entry);
385  QDomElement xml = eh.entryXML();
386  QByteArray ar;
387  QTextStream txt(&ar);
388  txt << xml;
389  //kDebug() << "Upload: " << QString(ar);
390 
391  connect(this, SIGNAL(signalEntryUploaded()), SLOT(stopLoop()));
392  connect(this, SIGNAL(signalEntryFailed()), SLOT(stopLoop()));
393 
394  uploadEntry(provider, entry);
395  m_uploadedEntry=entry;
396 }
397 
398 void EnginePrivate::slotEntryChanged(KNS::Entry * entry)
399 {
400  //kDebug() << "adding entries to list of changed entries";
401  m_changedEntries << entry;
402 }
403 
404 // BIGFIXME: make this method go away when we are using goya
405 void EnginePrivate::slotEntriesFinished()
406 {
407  //m_downloaddialog->refresh();
408 }
409 
410 void EnginePrivate::slotDownloadDialogDestroyed()
411 {
412  //kDebug() << m_downloaddialog << "is destroyed!" << s_dialogs.count() << s_dialogs.keys();
413  QHash<QString, QPointer<KDialog> >::iterator it = s_dialogs.begin();
414  while (it != s_dialogs.end()) {
415  if (it.value() == m_downloaddialog) {
416  //kDebug() << "found it!";
417  it = s_dialogs.erase(it);
418  }
419 
420  if (it != s_dialogs.end()) {
421  ++it;
422  }
423  }
424 
425  //kDebug() << s_dialogs.count() << s_dialogs.keys();
426 }
427 
428 void EnginePrivate::slotDownloadDialogClosed()
429 {
430  //kDebug() << sender() << m_downloaddialog;
431  disconnect(m_downloaddialog, SIGNAL(destroyed(QObject*)),
432  this, SLOT(slotDownloadDialogDestroyed()));
433  slotDownloadDialogDestroyed();
434  m_downloaddialog->deleteLater();
435  m_downloaddialog = NULL;
436  stopLoop();
437  emit signalDownloadDialogDone(QList<KNS::Entry*>::fromSet(m_changedEntries));
438 }
439 
440 #include "engine.moc"
uploaddialog.h
KNS::Engine::download
static KNS::Entry::List download()
Recommended download workflow entry point.
Definition: knewstuff2/engine.cpp:176
QWidget
KNS::Engine::downloadDialog
void downloadDialog()
Asynchronous way of starting the download workflow.
Definition: knewstuff2/engine.cpp:224
QEventLoop
kdebug.h
KNS::Engine::uploadDialogModal
KNS::Entry * uploadDialogModal(const QString &file)
Synchronous way of starting the upload workflow.
Definition: knewstuff2/engine.cpp:292
QByteArray
KNS::Entry
KNewStuff data entry container.
Definition: knewstuff2/core/entry.h:46
KWindowSystem::setOnDesktop
static void setOnDesktop(WId win, int desktop)
KNS::KTranslatable
String class with multiple localized representations.
Definition: ktranslatable.h:41
Command
Command
QPointer
KNS::KTranslatable::addString
void addString(const QString &lang, const QString &string)
Adds a string to the contents of this object.
Definition: ktranslatable.cpp:59
name
const char * name(StandardAction id)
kError
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KNS::Engine::init
bool init(const QString &config)
Definition: knewstuff2/engine.cpp:272
KDialog
KNS::UploadDialog
KNewStuff file upload dialog.
Definition: knewstuff2/ui/uploaddialog.h:52
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
KNS::Engine::uploadDialog
void uploadDialog(const QString &file)
Asynchronous way of starting the upload workflow.
Definition: knewstuff2/engine.cpp:305
KNS::Entry::name
KTranslatable name() const
Retrieve the name of the data object.
Definition: knewstuff2/core/entry.cpp:81
QList::const_iterator
KUrl
QTextStream
KNS::DxsEngine
KNewStuff DXS engine.
Definition: dxsengine.h:39
QList::fromSet
QList< T > fromSet(const QSet< T > &set)
KWindowSystem::activateWindow
static void activateWindow(WId win, long time=0)
KWindowSystem::currentDesktop
static int currentDesktop()
kglobal.h
KNS::Engine::Engine
Engine(QWidget *parent=0)
Engine constructor.
Definition: knewstuff2/engine.cpp:96
QHash
QEventLoop::exec
int exec(QFlags< QEventLoop::ProcessEventsFlag > flags)
QObject
QHash::begin
iterator begin()
entryhandler.h
KGlobal::activeComponent
KComponentData activeComponent()
QList< Provider * >::Iterator
typedef Iterator
QSet
QWidget::winId
WId winId() const
KComponentData::componentName
QString componentName() const
QString
QList< Provider * >
QStringList
QHash::erase
iterator erase(iterator pos)
QHash::value
const T value(const Key &key) const
KNS::Engine::~Engine
~Engine()
Engine destructor.
Definition: knewstuff2/engine.cpp:101
providerdialog.h
KNS::KTranslatable::languages
QStringList languages() const
Returns the list of all languages for which strings are stored.
Definition: ktranslatable.cpp:96
KNS::ProviderDialog
Dialog displaying a list of Hotstuff providers.
Definition: providerdialog.h:46
KNS::Engine::downloadDialogModal
KNS::Entry::List downloadDialogModal(QWidget *parent=0)
Synchronous way of starting the download workflow.
Definition: knewstuff2/engine.cpp:202
kwindowsystem.h
KNS::Entry::setPayload
void setPayload(const KTranslatable &url)
Sets the object's file.
Definition: knewstuff2/core/entry.cpp:156
engine.h
QWidget::show
void show()
KNS::Engine::upload
static KNS::Entry * upload(const QString &file)
Recommended upload workflow entry point.
Definition: knewstuff2/engine.cpp:278
QHash::end
iterator end()
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
kcomponentdata.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
KNS::Engine
The KNewStuff2 engine is the top-level class to handle GHNS and DXS workflows.
Definition: knewstuff2/engine.h:43
QDomElement
KNS::EntryHandler
Parser and dumper for KNewStuff data entries.
Definition: entryhandler.h:42
KNS::Provider
KNewStuff provider container.
Definition: knewstuff2/core/provider.h:51
downloaddialog.h
KComponentData
KNS::DownloadDialog
KNewStuff download dialog.
Definition: knewstuff2/ui/downloaddialog.h:68
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:25:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal