Port the KGet plugin system to the KDE standard one.
[kdenetwork.git] / kget / core / kget.cpp
blobf4bbbecd23eda70a95e6e9db1c06434cddf4bada
1 /* This file is part of the KDE project
3 Copyright (C) 2005 Dario Massarin <nekkar@libero.it>
4 Copyright (C) 2007 Lukas Appelhans <l.appelhans@gmx.de>
5 Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
13 #include "core/kget.h"
15 #include "mainwindow.h"
16 #include "core/transfer.h"
17 #include "core/transfergroup.h"
18 #include "core/transfergrouphandler.h"
19 #include "core/transfertreemodel.h"
20 #include "core/transfertreeselectionmodel.h"
21 #include "core/plugin/plugin.h"
22 #include "core/plugin/transferfactory.h"
23 #include "core/observer.h"
24 #include "core/kuiserverjobs.h"
25 #include "settings.h"
27 #include <kio/netaccess.h>
28 #include <kinputdialog.h>
29 #include <kfiledialog.h>
30 #include <kmessagebox.h>
31 #include <klocale.h>
32 #include <kstandarddirs.h>
33 #include <kservicetypetrader.h>
34 #include <kiconloader.h>
35 #include <kactioncollection.h>
36 #include <kio/renamedialog.h>
37 #include <KPassivePopup>
38 #include <KSystemTrayIcon>
40 #include <QTextStream>
41 #include <QDomElement>
42 #include <QApplication>
43 #include <QClipboard>
44 #include <QAbstractItemView>
46 /**
47 * This is our KGet class. This is where the user's transfers and searches are
48 * stored and organized.
49 * Use this class from the views to add or remove transfers or searches
50 * In order to organize the transfers inside categories we have a TransferGroup
51 * class. By definition, a transfer must always belong to a TransferGroup. If we
52 * don't want it to be displayed by the gui inside a specific group, we will put
53 * it in the group named "Not grouped" (better name?).
54 **/
56 KGet& KGet::self( MainWindow * mainWindow )
58 if(mainWindow)
60 m_mainWindow = mainWindow;
63 static KGet m;
64 return m;
67 void KGet::addObserver(ModelObserver * observer)
69 kDebug(5001);
71 m_observers.append(observer);
73 //Update the new observer with the TransferGroups objects of the model
74 QList<TransferGroup *>::const_iterator it = m_transferTreeModel->transferGroups().begin();
75 QList<TransferGroup *>::const_iterator itEnd = m_transferTreeModel->transferGroups().end();
77 for( ; it!=itEnd ; ++it )
79 postAddedTransferGroupEvent(*it, observer);
82 kDebug(5001) << " >>> EXITING";
85 void KGet::delObserver(ModelObserver * observer)
87 m_observers.removeAll(observer);
90 bool KGet::addGroup(const QString& groupName)
92 kDebug(5001);
94 // Check if a group with that name already exists
95 if(m_transferTreeModel->findGroup(groupName))
96 return false;
98 TransferGroup * group = new TransferGroup(m_transferTreeModel, m_scheduler, groupName);
99 m_transferTreeModel->addGroup(group);
101 //post notifications
102 postAddedTransferGroupEvent(group);
104 return true;
107 void KGet::delGroup(const QString& groupName)
109 TransferGroup * group = m_transferTreeModel->findGroup(groupName);
111 if(group)
113 m_transferTreeModel->delGroup(group);
114 postRemovedTransferGroupEvent(group);
115 delete(group);
119 void KGet::renameGroup(const QString& oldName, const QString& newName)
121 TransferGroup *group = m_transferTreeModel->findGroup(oldName);
123 if(group)
125 group->handler()->setName(newName);
129 QStringList KGet::transferGroupNames()
131 QStringList names;
133 foreach(TransferGroup *group, m_transferTreeModel->transferGroups()) {
134 names << group->name();
137 return names;
140 void KGet::addTransfer(KUrl srcUrl, QString destDir, // krazy:exclude=passbyvalue
141 const QString& groupName, bool start)
143 kDebug(5001) << "Source:" << srcUrl.url();
145 KUrl destUrl;
147 if ( srcUrl.isEmpty() )
149 //No src location: we let the user insert it manually
150 srcUrl = urlInputDialog();
151 if( srcUrl.isEmpty() )
152 return;
155 if ( !isValidSource( srcUrl ) )
156 return;
158 if (destDir.isEmpty())
160 if (Settings::useDefaultDirectory())
161 #ifdef Q_OS_WIN //krazy:exclude=cpp
162 destDir = Settings::defaultDirectory().remove("file:///");
163 #else
164 destDir = Settings::defaultDirectory().remove("file://");
165 #endif
167 QString checkExceptions = getSaveDirectoryFromExceptions(srcUrl);
168 if (Settings::enableExceptions() && !checkExceptions.isEmpty())
169 destDir = checkExceptions;
172 if (!isValidDestDirectory(destDir))
173 destDir = destInputDialog();
175 if( (destUrl = getValidDestUrl( destDir, srcUrl )).isEmpty() )
176 return;
178 if(m_transferTreeModel->findTransferByDestination(destUrl) != 0 || (destUrl.isLocalFile() && QFile::exists(destUrl.path()))) {
179 KIO::RenameDialog dlg( m_mainWindow, i18n("Rename transfer"), srcUrl,
180 destUrl, KIO::M_MULTI);
181 if (dlg.exec() == KIO::R_RENAME)
182 destUrl = dlg.newDestUrl();
183 else
184 return;
186 createTransfer(srcUrl, destUrl, groupName, start);
189 void KGet::addTransfer(const QDomElement& e, const QString& groupName)
191 //We need to read these attributes now in order to know which transfer
192 //plugin to use.
193 KUrl srcUrl = KUrl( e.attribute("Source") );
194 KUrl destUrl = KUrl( e.attribute("Dest") );
196 kDebug(5001) << " src= " << srcUrl.url()
197 << " dest= " << destUrl.url()
198 << " group= "<< groupName << endl;
200 if ( srcUrl.isEmpty() || !isValidSource(srcUrl)
201 || !isValidDestDirectory(destUrl.directory()) )
202 return;
204 createTransfer(srcUrl, destUrl, groupName, false, &e);
207 void KGet::addTransfer(KUrl::List srcUrls, QString destDir, // krazy:exclude=passbyvalue
208 const QString& groupName, bool start)
210 KUrl::List urlsToDownload;
212 KUrl::List::ConstIterator it = srcUrls.begin();
213 KUrl::List::ConstIterator itEnd = srcUrls.end();
215 for(; it!=itEnd ; ++it)
217 if ( isValidSource( *it ) )
218 urlsToDownload.append( *it );
221 if ( urlsToDownload.count() == 0 )
222 return;
224 if ( urlsToDownload.count() == 1 )
226 // just one file -> ask for filename
227 addTransfer(srcUrls.first(), destDir, groupName, start);
228 return;
231 KUrl destUrl;
233 // multiple files -> ask for directory, not for every single filename
234 if (!isValidDestDirectory(destDir) && !Settings::useDefaultDirectory())
235 destDir = destInputDialog();
237 it = urlsToDownload.begin();
238 itEnd = urlsToDownload.end();
240 for ( ; it != itEnd; ++it )
242 if (destDir.isEmpty())
244 if (Settings::useDefaultDirectory())
245 #ifdef Q_OS_WIN //krazy:exclude=cpp
246 destDir = Settings::defaultDirectory().remove("file:///");
247 #else
248 destDir = Settings::defaultDirectory().remove("file://");
249 #endif
251 QString checkExceptions = getSaveDirectoryFromExceptions(*it);
252 if (Settings::enableExceptions() && !checkExceptions.isEmpty())
253 destDir = checkExceptions;
255 destUrl = getValidDestUrl(destDir, *it);
257 if(!isValidDestUrl(destUrl))
258 continue;
260 createTransfer(*it, destUrl, groupName, start);
265 void KGet::delTransfer(TransferHandler * transfer)
267 Transfer * t = transfer->m_transfer;
269 m_transferTreeModel->delTransfer(t);
271 if (t->status() != Job::Finished)//if the transfer is not finished, we delete the *.part-file
273 QString dest = t->dest().url() + ".part";
274 kDebug(5001) << dest;
275 QFile destFile(dest.remove("file://"));
276 destFile.remove();
277 }//TODO: Ask the user if he/she wants to delete the *.part-file? To discuss (boom1992)
279 //Here I delete the Transfer. The other possibility is to move it to a list
280 //and to delete all these transfers when kget gets closed. Obviously, after
281 //the notification to the views that the transfer has been removed, all the
282 //pointers to it are invalid.
283 transfer->postDeleteEvent();
284 // TODO: why does it crash if a download is going to be deleted which is not the last in the list?
285 // there are always no problems with the last download.
286 // delete( t );
289 void KGet::moveTransfer(TransferHandler * transfer, const QString& groupName)
291 Q_UNUSED(transfer);
292 Q_UNUSED(groupName);
295 QList<TransferHandler *> KGet::selectedTransfers()
297 // kDebug(5001) << "KGet::selectedTransfers";
299 QList<TransferHandler *> selectedTransfers;
301 QModelIndexList selectedIndexes = m_selectionModel->selectedRows();
303 foreach(QModelIndex currentIndex, selectedIndexes)
305 if(!m_transferTreeModel->isTransferGroup(currentIndex))
306 selectedTransfers.append(static_cast<TransferHandler *> (currentIndex.internalPointer()));
309 return selectedTransfers;
312 // This is the code that was used in the old selectedTransfers function
313 /* QList<TransferGroup *>::const_iterator it = m_transferTreeModel->transferGroups().begin();
314 QList<TransferGroup *>::const_iterator itEnd = m_transferTreeModel->transferGroups().end();
316 for( ; it!=itEnd ; ++it )
318 TransferGroup::iterator it2 = (*it)->begin();
319 TransferGroup::iterator it2End = (*it)->end();
321 for( ; it2!=it2End ; ++it2 )
323 Transfer * transfer = (Transfer*) *it2;
325 if( transfer->isSelected() )
326 selectedTransfers.append( transfer->handler() );
329 return selectedTransfers;*/
332 QList<TransferGroupHandler *> KGet::selectedTransferGroups()
334 QList<TransferGroupHandler *> selectedTransferGroups;
336 QModelIndexList selectedIndexes = m_selectionModel->selectedRows();
338 foreach(QModelIndex currentIndex, selectedIndexes)
340 if(m_transferTreeModel->isTransferGroup(currentIndex))
341 selectedTransferGroups.append(static_cast<TransferGroupHandler *> (currentIndex.internalPointer()));
344 return selectedTransferGroups;
347 TransferTreeSelectionModel * KGet::selectionModel()
349 return m_selectionModel;
352 void KGet::addTransferView(QAbstractItemView * view)
354 view->setModel(m_transferTreeModel);
357 void KGet::load( QString filename ) // krazy:exclude=passbyvalue
359 kDebug(5001) << "(" << filename << ")";
361 if(filename.isEmpty())
362 filename = KStandardDirs::locateLocal("appdata", "transfers.kgt");
364 QString tmpFile;
366 //Try to save the transferlist to a temporary location
367 if(!KIO::NetAccess::download(KUrl(filename), tmpFile, 0))
368 return;
370 QFile file(tmpFile);
371 QDomDocument doc;
373 kDebug(5001) << "file:" << filename;
375 if(doc.setContent(&file))
377 QDomElement root = doc.documentElement();
379 QDomNodeList nodeList = root.elementsByTagName("TransferGroup");
380 int nItems = nodeList.length();
382 for( int i = 0 ; i < nItems ; i++ )
384 TransferGroup * foundGroup = m_transferTreeModel->findGroup( nodeList.item(i).toElement().attribute("Name") );
386 kDebug(5001) << "KGet::load -> group = " << nodeList.item(i).toElement().attribute("Name");
388 if( !foundGroup )
390 kDebug(5001) << "KGet::load -> group not found";
392 TransferGroup * newGroup = new TransferGroup(m_transferTreeModel, m_scheduler);
394 m_transferTreeModel->addGroup(newGroup);
396 newGroup->load(nodeList.item(i).toElement());
398 //Post notifications
399 postAddedTransferGroupEvent(newGroup);
401 else
403 kDebug(5001) << "KGet::load -> group found";
405 //A group with this name already exists.
406 //Integrate the group's transfers with the ones read from file
407 foundGroup->load(nodeList.item(i).toElement());
411 else
413 kWarning(5001) << "Error reading the transfers file";
417 void KGet::save( QString filename ) // krazy:exclude=passbyvalue
419 if ( !filename.isEmpty()
420 && QFile::exists( filename )
421 && (KMessageBox::questionYesNoCancel(0,
422 i18n("The file %1 already exists.\nOverwrite?", filename),
423 i18n("Overwrite existing file?"), KStandardGuiItem::yes(),
424 KStandardGuiItem::no(), KStandardGuiItem::cancel(), "QuestionFilenameExists" )
425 != KMessageBox::Yes) )
426 return;
428 if(filename.isEmpty())
429 filename = KStandardDirs::locateLocal("appdata", "transfers.kgt");
431 QDomDocument doc(QString("KGetTransfers"));
432 QDomElement root = doc.createElement("Transfers");
433 doc.appendChild(root);
435 QList<TransferGroup *>::const_iterator it = m_transferTreeModel->transferGroups().begin();
436 QList<TransferGroup *>::const_iterator itEnd = m_transferTreeModel->transferGroups().end();
438 for ( ; it!=itEnd ; ++it )
440 QDomElement e = doc.createElement("TransferGroup");
441 root.appendChild(e);
442 (*it)->save(e);
444 QFile file(filename);
445 if ( !file.open( QIODevice::WriteOnly ) )
447 //kWarning(5001)<<"Unable to open output file when saving";
448 KMessageBox::error(0,
449 i18n("Unable to save to: %1", filename),
450 i18n("Error"));
451 return;
454 QTextStream stream( &file );
455 doc.save( stream, 0 );
456 file.close();
459 TransferFactory * KGet::factory(TransferHandler * transfer)
461 return transfer->m_transfer->factory();
464 KActionCollection * KGet::actionCollection()
466 return m_mainWindow->actionCollection();
469 void KGet::setSchedulerRunning(bool running)
471 if(running)
473 m_scheduler->stop(); //stopall first, to have a clean startingpoint
474 m_scheduler->start();
476 else
477 m_scheduler->stop();
480 bool KGet::schedulerRunning()
482 return (m_scheduler->countRunningJobs() > 0);
485 void KGet::setPluginsSettingsWidget(KTabWidget * widget)
487 kDebug(5001);
488 QList<TransferFactory *>::iterator it = m_transferFactories.begin();
489 QList<TransferFactory *>::iterator itEnd = m_transferFactories.end();
491 QWidget * settingsWidget;
492 for( ; it!=itEnd ; ++it)
494 KDialog *dialog = static_cast<KDialog*>(widget->parent()->parent());
495 if (!dialog)
496 return;
498 settingsWidget = (*it)->createSettingsWidget(dialog);
499 if(settingsWidget)
500 widget->addTab( settingsWidget, (*it)->displayName() );
504 QList<TransferHandler*> KGet::allTransfers()
506 QList<TransferHandler*> transfers;
508 foreach (TransferGroup *group, KGet::m_transferTreeModel->transferGroups())
510 transfers << group->handler()->transfers();
513 return transfers;
516 void KGet::checkSystemTray()
518 kDebug(5001);
519 bool running = false;
521 foreach (TransferHandler *handler, KGet::allTransfers())
523 if (handler->status() == Job::Running)
524 running = true;
526 if (running)
527 continue;
530 m_mainWindow->setSystemTrayDownloading(running);
533 void KGet::settingsChanged()
535 kDebug(5001);
537 QList<TransferFactory*>::const_iterator it = m_transferFactories.begin();
538 QList<TransferFactory*>::const_iterator itEnd = m_transferFactories.end();
540 for( ; it!=itEnd ; ++it )
542 (*it)->settingsChanged();
546 void KGet::registerKJob(KJob *job)
548 m_jobManager->registerJob(job);
551 void KGet::unregisterKJob(KJob *job)
553 m_jobManager->unregisterJob(job);
556 void KGet::reloadKJobs()
558 m_jobManager->reload();
561 // ------ STATIC MEMBERS INITIALIZATION ------
562 QList<ModelObserver *> KGet::m_observers;
563 TransferTreeModel * KGet::m_transferTreeModel;
564 TransferTreeSelectionModel * KGet::m_selectionModel;
565 QList<TransferFactory *> KGet::m_transferFactories;
566 Scheduler * KGet::m_scheduler = new Scheduler();
567 MainWindow * KGet::m_mainWindow = 0;
568 KUiServerJobs * KGet::m_jobManager = new KUiServerJobs();
570 // ------ PRIVATE FUNCTIONS ------
571 KGet::KGet()
573 m_transferTreeModel = new TransferTreeModel(m_scheduler);
574 m_selectionModel = new TransferTreeSelectionModel(m_transferTreeModel);
576 //Load all the available plugins
577 loadPlugins();
579 //Create the default group
580 addGroup(i18n("My Downloads"));
583 KGet::~KGet()
585 delete m_scheduler;
588 void KGet::createTransfer(const KUrl &src, const KUrl &dest, const QString& groupName,
589 bool start, const QDomElement * e)
591 kDebug(5001) << "srcUrl= " << src.url() << " "
592 << "destUrl= " << dest.url()
593 << "group= _" << groupName << "_" << endl;
595 TransferGroup * group = m_transferTreeModel->findGroup(groupName);
596 if (group==0)
598 kDebug(5001) << "KGet::createTransfer -> group not found";
599 group = m_transferTreeModel->transferGroups().first();
601 Transfer * newTransfer;
603 QList<TransferFactory *>::iterator it = m_transferFactories.begin();
604 QList<TransferFactory *>::iterator itEnd = m_transferFactories.end();
606 for( ; it!=itEnd ; ++it)
608 kDebug(5001) << "Trying plugin n.plugins=" << m_transferFactories.size();
609 if((newTransfer = (*it)->createTransfer(src, dest, group, m_scheduler, e)))
611 // kDebug(5001) << "KGet::createTransfer -> CREATING NEW TRANSFER ON GROUP: _" << group->name() << "_";
612 m_transferTreeModel->addTransfer(newTransfer, group);
614 if(start)
615 newTransfer->handler()->start();
617 newTransfer->handler()->addObserver(new TransferFinishedObserver());
619 return;
622 kDebug(5001) << "Warning! No plugin found to handle the given url";
625 void KGet::postAddedTransferGroupEvent(TransferGroup * group, ModelObserver * observer)
627 kDebug(5001);
628 if(observer)
630 observer->addedTransferGroupEvent(group->handler());
631 return;
634 QList<ModelObserver *>::iterator it = m_observers.begin();
635 QList<ModelObserver *>::iterator itEnd = m_observers.end();
637 for(; it!=itEnd; ++it)
639 kDebug(5001) << "message posted";
641 (*it)->addedTransferGroupEvent(group->handler());
645 void KGet::postRemovedTransferGroupEvent(TransferGroup * group, ModelObserver * observer)
647 if(observer)
649 observer->removedTransferGroupEvent(group->handler());
650 return;
653 QList<ModelObserver *>::iterator it = m_observers.begin();
654 QList<ModelObserver *>::iterator itEnd = m_observers.end();
656 for(; it!=itEnd; ++it)
658 (*it)->removedTransferGroupEvent(group->handler());
662 KUrl KGet::urlInputDialog()
664 QString newtransfer;
665 bool ok = false;
667 KUrl clipboardUrl = KUrl(QApplication::clipboard()->text(QClipboard::Clipboard).trimmed());
668 if (clipboardUrl.isValid())
669 newtransfer = clipboardUrl.url();
671 while (!ok)
673 newtransfer = KInputDialog::getText(i18n("New Download"), i18n("Enter URL:"), newtransfer, &ok, 0);
675 if (!ok)
677 //user pressed cancel
678 return KUrl();
681 KUrl src = KUrl(newtransfer);
682 if(src.isValid())
683 return src;
684 else
685 ok = false;
687 return KUrl();
690 QString KGet::destInputDialog()
692 QString destDir = KFileDialog::getExistingDirectory(Settings::lastDirectory());
694 Settings::setLastDirectory( destDir );
695 return destDir;
698 QString KGet::getSaveDirectoryFromExceptions(const KUrl &filename)
700 QString destDir;
702 QStringList list = Settings::extensionsFolderList();
703 QStringList::Iterator it = list.begin();
704 QStringList::Iterator end = list.end();
705 while (it != end) {
706 // odd list items are regular expressions for extensions
707 QString ext = *it;
708 ++it;
709 QString path = *it;
710 ++it;
712 if (!ext.startsWith('*'))
713 ext = '*' + ext;
715 QRegExp rexp(ext);
716 rexp.setPatternSyntax(QRegExp::Wildcard);
718 if (rexp.exactMatch(filename.url())) {
719 destDir = path;
720 break;
724 #ifdef Q_OS_WIN //krazy:exclude=cpp
725 destDir = destDir.remove("file:///");
726 #endif
727 return destDir.remove("file://");
730 bool KGet::isValidSource(const KUrl &source)
732 if (!source.isValid())
734 KMessageBox::error(0,
735 i18n("Malformed URL:\n%1", source.prettyUrl()),
736 i18n("Error"));
737 return false;
739 // Check if a transfer with the same url already exists
740 Transfer * transfer = m_transferTreeModel->findTransfer( source );
741 if ( transfer )
743 if ( transfer->status() == Job::Finished )
745 // transfer is finished, ask if we want to download again
746 if (KMessageBox::questionYesNoCancel(0,
747 i18n("URL already saved:\n%1\nDownload again?", source.prettyUrl()),
748 i18n("Download URL again?"), KStandardGuiItem::yes(),
749 KStandardGuiItem::no(), KStandardGuiItem::cancel(), "QuestionUrlAlreadySaved" )
750 == KMessageBox::Yes)
752 //TODO reimplement this
753 //transfer->slotRemove();
754 //checkQueue();
755 return true;
758 else
760 //Transfer is already in list and not finished, ...
761 return false;
763 return false;
765 return true;
768 bool KGet::isValidDestDirectory(const QString & destDir)
770 if (QFileInfo( destDir ).isWritable())
771 return (!destDir.isEmpty() && QFileInfo( destDir ).isDir() && QFileInfo( destDir ).isWritable());
772 if (!QFileInfo( destDir ).isWritable() && !destDir.isEmpty())
773 KMessageBox::error(0, i18n("Directory is not writable"));
774 return false;
777 bool KGet::isValidDestUrl(const KUrl &destUrl)
779 if(KIO::NetAccess::exists(destUrl, KIO::NetAccess::DestinationSide, 0))
781 if (KMessageBox::warningYesNoCancel(0,
782 i18n("Destination file \n%1\nalready exists.\n"
783 "Do you want to overwrite it?", destUrl.prettyUrl()))
784 == KMessageBox::Yes)
786 safeDeleteFile( destUrl );
787 return true;
789 else
790 return false;
792 return true;
794 KIO::open_RenameDlg(i18n("File already exists"),
795 (*it).url(), destUrl.url(),
796 KIO::M_MULTI);
800 KUrl KGet::getValidDestUrl(const QString& destDir, const KUrl &srcUrl)
802 if ( !isValidDestDirectory(destDir) )
803 return KUrl();
805 // create a proper destination file from destDir
806 KUrl destUrl = KUrl( destDir );
807 QString filename = srcUrl.fileName();
809 if ( filename.isEmpty() )
811 // simply use the full url as filename
812 filename = KUrl::toPercentEncoding( srcUrl.prettyUrl(), "/" );
813 kDebug(5001) << " Filename is empty. Setting to " << filename;
814 kDebug(5001) << " srcUrl = " << srcUrl.url();
815 kDebug(5001) << " prettyUrl = " << srcUrl.prettyUrl();
817 else
819 kDebug(5001) << " Filename is not empty";
820 destUrl.adjustPath( KUrl::AddTrailingSlash );
821 destUrl.setFileName( filename );
822 if (!isValidDestUrl(destUrl))
824 kDebug(5001) << " destUrl " << destUrl.path() << " is not valid";
825 return KUrl();
828 return destUrl;
831 void KGet::loadPlugins()
833 // Add versioning constraint
834 QString
835 str = "[X-KDE-KGet-framework-version] == ";
836 str += QString::number( FrameworkVersion );
837 str += " and ";
838 str += "[X-KDE-KGet-rank] > 0";
839 str += " and ";
840 str += "[X-KDE-KGet-plugintype] == ";
842 KService::List offers;
844 //TransferFactory plugins
845 offers = KServiceTypeTrader::self()->query( "KGet/Plugin", str + "'TransferFactory'" );
847 //Here we use a QMap only to easily sort the plugins by rank
848 QMap<int, KService::Ptr> services;
849 QMap<int, KService::Ptr>::iterator it;
851 for ( int i = 0; i < offers.count(); ++i )
853 services[ offers[i]->property( "X-KDE-KGet-rank" ).toInt() ] = offers[i];
854 kDebug(5001) << " TransferFactory plugin found:" << endl <<
855 " rank = " << offers[i]->property( "X-KDE-KGet-rank" ).toInt() << endl <<
856 " plugintype = " << offers[i]->property( "X-KDE-KGet-plugintype" ) << endl;
859 //I must fill this pluginList before and my m_transferFactories list after.
860 //This because calling the KLibLoader::globalLibrary() erases the static
861 //members of this class (why?), such as the m_transferFactories list.
862 QList<KGetPlugin *> pluginList;
864 for( it = services.begin(); it != services.end(); ++it )
866 KGetPlugin * plugin;
867 if( (plugin = createPluginFromService(*it)) != 0 )
869 pluginList.prepend(plugin);
870 kDebug(5001) << "TransferFactory plugin (" << (*it)->library()
871 << ") found and added to the list of available plugins" << endl;
873 else
874 kDebug(5001) << "Error loading TransferFactory plugin ("
875 << (*it)->library() << ")" << endl;
878 QList<KGetPlugin *>::iterator it2 = pluginList.begin();
879 QList<KGetPlugin *>::iterator it2End = pluginList.end();
881 for( ; it2!=it2End ; ++it2 )
882 m_transferFactories.append( static_cast<TransferFactory *>(*it2) );
884 kDebug(5001) << "Number of factories = " << m_transferFactories.size();
887 KGetPlugin * KGet::createPluginFromService( const KService::Ptr &service )
889 //try to load the specified library
890 KPluginFactory *factory = KPluginLoader(service->library()).factory();
892 if (!factory)
894 KMessageBox::error(0, i18n("<html><p>KPluginFactory could not load the plugin:<br/><i>%1</i></p></html>",
895 service->library()));
896 kError(5001) << "KPluginFactory could not load the plugin:" << service->library();
897 return 0;
899 KGetPlugin * plugin = factory->create< TransferFactory >(0);
901 return plugin;
904 bool KGet::safeDeleteFile( const KUrl& url )
906 if ( url.isLocalFile() )
908 QFileInfo info( url.path() );
909 if ( info.isDir() )
911 KMessageBox::information(0L,i18n("Not deleting\n%1\nas it is a "
912 "directory.", url.prettyUrl()),
913 i18n("Not Deleted"));
914 return false;
916 KIO::NetAccess::del( url, 0L );
917 return true;
920 else
921 KMessageBox::information( 0L,
922 i18n("Not deleting\n%1\nas it is not a local"
923 " file.", url.prettyUrl()),
924 i18n("Not Deleted") );
925 return false;
928 TransferFinishedObserver::TransferFinishedObserver()
929 : TransferObserver()
933 void TransferFinishedObserver::transferChangedEvent(TransferHandler * transfer)
935 kDebug(5001);
937 if (transfer->status() == Job::Finished && Settings::quitAfterCompletedTransfer())
939 checkAndFinish();
942 if (prevStatus != transfer->statusText())//FIXME: HACK: better: check statusFlags if it contains Tc_Status (flags & Transfer::Tc_Status <-doesn't work)
944 prevStatus = transfer->statusText();
945 KGet::checkSystemTray();
949 void TransferFinishedObserver::checkAndFinish()
951 bool quitFlag = true;
952 foreach(TransferGroup *transferGroup, KGet::m_transferTreeModel->transferGroups()) {
953 foreach(TransferHandler *transfer, transferGroup->handler()->transfers()) {
954 if(transfer->status() != Job::Finished) {
955 quitFlag = false;
960 // check if there is some unfinished transfer in scheduler queues
961 if(quitFlag) {
962 KPassivePopup *message;
963 // we have to call diferent message from kpassivePopup
964 // one with parent as QWidget for the mainWindow
965 // and another with parent as QSystemTrayIcon if the parent is a systemTray
966 // so passing the QSystemTrayIcon as QWidget don't work
967 if(Settings::enableSystemTray())
969 message = KPassivePopup::message(5000, KGET_QUIT_MESSAGE_TITLE,
970 KGET_QUIT_MESSAGE,
971 KGet::m_mainWindow->systemTray());
973 else
975 message = KPassivePopup::message(5000, KGET_QUIT_MESSAGE_TITLE,
976 KGET_QUIT_MESSAGE,
977 KGet::m_mainWindow);
980 QObject::connect(message, SIGNAL(destroyed()), KGet::m_mainWindow, SLOT(slotQuit()));