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