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

KNewStuff

engine.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of KNewStuff2.
00003     Copyright (c) 2008 Jeremy Whiting <jeremy@scitools.com>
00004     Copyright (c) 2007 Josef Spillner <spillner@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2.1 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 #include "knewstuff2/engine.h"
00020 
00021 #include "knewstuff2/ui/downloaddialog.h"
00022 #include "knewstuff2/ui/uploaddialog.h"
00023 #include "knewstuff2/ui/providerdialog.h"
00024 
00025 #include "knewstuff2/core/entryhandler.h" // tmp
00026 
00027 #include <kcomponentdata.h>
00028 #include <kglobal.h>
00029 #include <kdebug.h>
00030 
00031 #include <qeventloop.h>
00032 
00033 using namespace KNS;
00034 
00035 class KNS::EnginePrivate : public DxsEngine
00036 {
00037     Q_OBJECT
00038 
00039 public:
00040     EnginePrivate(QWidget* parent)
00041             : DxsEngine(parent) {
00042         m_command = EnginePrivate::command_none;
00043         m_uploaddialog = NULL;
00044         m_downloaddialog = NULL;
00045         m_uploadedEntry = NULL;
00046         m_modal = false;
00047         m_parent = parent;
00048         m_loop = 0;
00049     }
00050 
00051     enum Command {
00052         command_none,
00053         command_upload,
00054         command_download
00055     };
00056 
00057     void workflow();
00058     KNS::Entry* upload(const QString& file);
00059 
00060     Command m_command;
00061     UploadDialog *m_uploaddialog;
00062     DownloadDialog *m_downloaddialog;
00063     QString m_uploadfile;
00064     KNS::Entry *m_uploadedEntry;
00065     KNS::Provider::List m_providers;
00066     bool m_modal;
00067     QWidget * m_parent;
00068     QSet<KNS::Entry*> m_changedEntries;
00069     QEventLoop* m_loop;
00070 
00071 private Q_SLOTS:
00073     void stopLoop();
00074 
00075     void slotProviderLoaded(KNS::Provider *provider);
00076 
00079     void slotEntryChanged(KNS::Entry *entry);
00080 
00081     void slotProvidersFinished();
00082     void slotEntriesFinished();
00083 
00084     void slotDownloadDialogClosed();
00085 };
00086 
00087 
00088 Engine::Engine(QWidget* parent)
00089         : d(new EnginePrivate(parent))
00090 {
00091 }
00092 
00093 Engine::~Engine()
00094 {
00095     delete d;
00096 }
00097 
00098 void EnginePrivate::workflow()
00099 {
00100     if ((m_command == command_upload) || (m_command == command_download)) {
00101         connect(this,
00102                 SIGNAL(signalProviderLoaded(KNS::Provider*)),
00103                 SLOT(slotProviderLoaded(KNS::Provider*)));
00104         connect(this,
00105                 SIGNAL(signalProvidersFailed()),
00106                 SLOT(stopLoop()));
00107     }
00108 
00109     if (m_command == command_upload) {
00110         connect(this,
00111                 SIGNAL(signalProvidersFinished()),
00112                 SLOT(slotProvidersFinished()));
00113 
00114         m_uploadedEntry = NULL;
00115     }
00116 
00117     if (m_command == command_download) {
00118         m_downloaddialog = new DownloadDialog(this, m_parent);
00119 
00120         connect(this, SIGNAL(signalEntriesFinished()),
00121                 SLOT(slotEntriesFinished()));
00122         connect(this,
00123                 SIGNAL(signalEntryChanged(KNS::Entry *)),
00124                 SLOT(slotEntryChanged(KNS::Entry *)));
00125 
00126         m_downloaddialog->show();
00127 
00128         connect(m_downloaddialog, SIGNAL(finished()), SLOT(slotDownloadDialogClosed()));
00129     }
00130 
00131     start();
00132 
00133     if (m_modal) {
00134         QEventLoop loop;
00135         m_loop = &loop;
00136         loop.exec();
00137     }
00138 }
00139 
00140 void EnginePrivate::stopLoop()
00141 {
00142     m_command = command_none;
00143 
00144     if (m_loop) {
00145         m_loop->exit();
00146         m_loop = 0;
00147     }
00148 }
00149 
00150 KNS::Entry::List Engine::download()
00151 {
00152     KNS::Entry::List entries;
00153 
00154     Engine *engine = new Engine(0);
00155 
00156     KComponentData component = KGlobal::activeComponent();
00157     QString name = component.componentName();
00158 
00159     bool ret = engine->init(name + ".knsrc");
00160     if (!ret) {
00161         delete engine;
00162         return entries;
00163     }
00164 
00165     KNS::Entry::List tempList = engine->downloadDialogModal(0);
00166 
00167     // copy the list since the entries will be deleted when we delete the engine
00168     foreach(Entry * entry, tempList) {
00169         entries << new Entry(*entry);
00170     }
00171     delete engine;
00172 
00173     return entries;
00174 }
00175 
00176 KNS::Entry::List Engine::downloadDialogModal(QWidget*)
00177 {
00178     //kDebug() << "Engine: downloadDialogModal";
00179 
00180     d->m_command = EnginePrivate::command_download;
00181     d->m_modal = true;
00182 
00183     d->workflow();
00184 
00185     return QList<KNS::Entry*>::fromSet(d->m_changedEntries);
00186 }
00187 
00188 void Engine::downloadDialog()
00189 {
00190     //kDebug() << "Engine: downloadDialog";
00191 
00192     if (d->m_command != EnginePrivate::command_none) {
00193         kError() << "Engine: asynchronous workflow already going on" << endl;
00194     }
00195 
00196     d->m_command = EnginePrivate::command_download;
00197     d->m_modal = false;
00198 
00199     d->workflow();
00200 }
00201 
00202 KNS::Entry *EnginePrivate::upload(const QString& file)
00203 {
00204     KNS::Entry *entry = NULL;
00205 
00206     Engine engine(0);
00207 
00208     KComponentData component = KGlobal::activeComponent();
00209     QString name = component.componentName();
00210 
00211     bool ret = engine.init(name + ".knsrc");
00212     if (!ret) return entry;
00213 
00214     entry = engine.uploadDialogModal(file);
00215 
00216     // FIXME: refcounting?
00217     return entry;
00218 }
00219 
00220 bool Engine::init(const QString& config)
00221 {
00222     return d->init(config);
00223 }
00224 
00225 
00226 KNS::Entry *Engine::upload(const QString& file)
00227 {
00228 #ifdef __GNUC__
00229 #warning KNS::Engine::upload() not implemented!
00230 #endif
00231 #if 0
00232     return d->upload(file);
00233 #else
00234     Q_UNUSED(file);
00235 #endif
00236     Q_ASSERT(false);
00237     return 0;
00238 }
00239 
00240 KNS::Entry *Engine::uploadDialogModal(const QString& file)
00241 {
00242     //kDebug() << "Engine: uploadDialogModal";
00243 
00244     d->m_command = EnginePrivate::command_upload;
00245     d->m_modal = true;
00246     d->m_uploadfile = file;
00247 
00248     d->workflow();
00249 
00250     return d->m_uploadedEntry;
00251 }
00252 
00253 void Engine::uploadDialog(const QString& file)
00254 {
00255     //kDebug() << "Engine: uploadDialog";
00256 
00257     if (d->m_command != EnginePrivate::command_none) {
00258         kError() << "Engine: asynchronous workflow already going on" << endl;
00259     }
00260 
00261     d->m_command = EnginePrivate::command_upload;
00262     d->m_modal = false;
00263     d->m_uploadfile = file;
00264 
00265     d->workflow();
00266 }
00267 
00268 void EnginePrivate::slotProviderLoaded(KNS::Provider *provider)
00269 {
00270     if (m_command == command_download) {
00271         loadEntries(provider);
00272     } else if (m_command == command_upload) {
00273         // FIXME: inject into upload dialog
00274         // FIXME: dialog could do this by itself!
00275 
00276         // FIXME: for the modal dialog, do nothing here
00277         // ... and wait for slotProvidersFinished()
00278         m_providers.append(provider);
00279     } else {
00280         kError() << "Engine: invalid command" << endl;
00281     }
00282 }
00283 
00284 void EnginePrivate::slotProvidersFinished()
00285 {
00286     // NOTE: this is only connected when we are doing an upload
00287     //kDebug() << "Engine: slotProvidersFinished";
00288 
00289     int ret;
00290 
00291     //Provider *fakeprovider = new Provider();
00292     //fakeprovider->setName(QString("Fake Provider"));
00293     //fakeprovider->setUploadUrl(KUrl("http://localhost/dav/"));
00294     //fakeprovider->setUploadUrl(KUrl("webdav://localhost/uploads/"));
00295 
00296     ProviderDialog provdialog(0);
00297     for (Provider::List::Iterator it = m_providers.begin(); it != m_providers.end(); ++it) {
00298         Provider *provider = (*it);
00299         provdialog.addProvider(provider);
00300     }
00301     //provdialog.addProvider(fakeprovider);
00302     ret = provdialog.exec();
00303     if (ret == QDialog::Rejected) {
00304         stopLoop();
00305         return;
00306     }
00307 
00308     KNS::Provider *provider = provdialog.provider();
00309 
00310     UploadDialog uploaddialog(0);
00311     uploaddialog.setPayloadFile(KUrl(m_uploadfile));
00312     ret = uploaddialog.exec();
00313     if (ret == QDialog::Rejected) {
00314         stopLoop();
00315         return;
00316     }
00317 
00318     Entry *entry = uploaddialog.entry();
00319     entry->setPayload(m_uploadfile);
00320     if (!entry) {
00321         stopLoop();
00322         return;
00323     }
00324 
00325     EntryHandler eh(*entry);
00326     QDomElement xml = eh.entryXML();
00327     QByteArray ar;
00328     QTextStream txt(&ar);
00329     txt << xml;
00330     //kDebug() << "Upload: " << QString(ar);
00331 
00332     connect(this,
00333             SIGNAL(signalEntryUploaded()),
00334             SLOT(stopLoop()));
00335     connect(this,
00336             SIGNAL(signalEntryFailed()),
00337             SLOT(stopLoop()));
00338 
00339     uploadEntry(provider, entry);
00340 }
00341 
00342 void EnginePrivate::slotEntryChanged(KNS::Entry * entry)
00343 {
00344     //kDebug() << "adding entries to list of changed entries";
00345     m_changedEntries << entry;
00346 }
00347 
00348 // BIGFIXME: make this method go away when we are using goya
00349 void EnginePrivate::slotEntriesFinished()
00350 {
00351     //m_downloaddialog->refresh();
00352 }
00353 
00354 void EnginePrivate::slotDownloadDialogClosed()
00355 {
00356     m_downloaddialog->deleteLater();
00357     m_downloaddialog = NULL;
00358 
00359     stopLoop();
00360 }
00361 
00362 #include "engine.moc"

KNewStuff

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal