simplify down to one constructor and try harder to have all information at all times with clearer code

This commit is contained in:
Aaron Seigo 2011-07-13 20:03:00 +02:00
parent a386f40f73
commit e4be1cecf5
2 changed files with 52 additions and 50 deletions

View File

@ -31,7 +31,9 @@
#include <kdebug.h> #include <kdebug.h>
#include <ktemporaryfile.h> #include <ktemporaryfile.h>
#include <kzip.h> #include <kzip.h>
#include <kservicetypetrader.h>
#include <QDir>
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QHostInfo> #include <QHostInfo>
@ -53,6 +55,10 @@ void PlasmoidServiceJob::start()
{ {
if (operationName() == "GetPackage") { if (operationName() == "GetPackage") {
kDebug() << "sending " << m_service->m_packagePath; kDebug() << "sending " << m_service->m_packagePath;
if (m_service->m_packagePath.isEmpty()) {
// just return the plugin name in this case
setResult(m_service->m_pluginName);
} else {
QFileInfo fileInfo(m_service->m_packagePath); QFileInfo fileInfo(m_service->m_packagePath);
if (fileInfo.exists() && fileInfo.isAbsolute()) { if (fileInfo.exists() && fileInfo.isAbsolute()) {
@ -61,8 +67,8 @@ void PlasmoidServiceJob::start()
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
setResult(file.readAll()); setResult(file.readAll());
} else { } else {
kDebug() << "file doesn't exists, we're sending the plugin name"; setResult(QString());
setResult(m_service->m_packagePath); }
} }
} else if (operationName() == "GetMetaData") { } else if (operationName() == "GetMetaData") {
QFile file(m_service->m_metadata); QFile file(m_service->m_metadata);
@ -78,27 +84,19 @@ void PlasmoidServiceJob::start()
} }
setResult(serviceName); setResult(serviceName);
} }
setResult(false);
} }
PlasmoidService::PlasmoidService(Applet *applet)
PlasmoidService::PlasmoidService(const QString &packageLocation) : Plasma::Service(applet),
: Plasma::Service(0) m_pluginName(applet->pluginName())
{ {
setName("plasmoidservice"); setName("plasmoidservice");
QString location; if (applet->package() && !applet->package()->isValid()) {
location = packageLocation; const QString root = applet->package()->path();
if (!location.endsWith('/')) { m_metadata = root + "metadata.desktop";
location.append('/');
}
m_metadata = location + "metadata.desktop";
/*FIXME: either do something useful on error or don't waste time with them
if (!QFile::exists(m_metadata)) {
kDebug() << "not a valid package";
}
*/
m_tempFile.open(); m_tempFile.open();
m_packagePath = m_tempFile.fileName(); m_packagePath = m_tempFile.fileName();
@ -107,23 +105,27 @@ PlasmoidService::PlasmoidService(const QString &packageLocation)
// put everything into a zip archive // put everything into a zip archive
KZip creation(m_packagePath); KZip creation(m_packagePath);
creation.setCompression(KZip::NoCompression); creation.setCompression(KZip::NoCompression);
if (!creation.open(QIODevice::WriteOnly)) { if (creation.open(QIODevice::WriteOnly)) {
/*FIXME: either do something useful on error or don't waste time with it */ QDir dir(root);
foreach (const QString &entry, dir.entryList(QDir::Files)) {
creation.addLocalFile(root + entry, entry);
}
foreach (const QString &entry, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
creation.addLocalDirectory(root + entry, entry);
}
creation.close();
} else {
kDebug() << "could not open archive"; kDebug() << "could not open archive";
} }
} else {
creation.addLocalFile(location + "metadata.desktop", "metadata.desktop"); kDebug() << "applet lacks a valid package";
location.append("contents/"); const QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(applet->pluginName());
creation.addLocalDirectory(location, "contents"); KService::List offers = KServiceTypeTrader::self()->query("Plasma/Applet", constraint);
creation.close(); if (!offers.isEmpty()) {
m_metadata = offers.first()->entryPath();
} }
PlasmoidService::PlasmoidService(Applet *applet)
{
setName("plasmoidservice");
if (!applet->package() || !applet->package()->isValid()) {
kDebug() << "not a valid package";
m_packagePath = applet->pluginName();
} }
} }

View File

@ -57,7 +57,6 @@ class PlasmoidService : public Service, DataEngineConsumer
Q_OBJECT Q_OBJECT
public: public:
PlasmoidService(const QString &plasmoidLocation);
PlasmoidService(Applet *applet); PlasmoidService(Applet *applet);
protected: protected:
@ -66,6 +65,7 @@ class PlasmoidService : public Service, DataEngineConsumer
private: private:
QString m_packagePath; QString m_packagePath;
QString m_metadata; QString m_metadata;
QString m_pluginName;
KTemporaryFile m_tempFile; KTemporaryFile m_tempFile;
friend class PlasmoidServiceJob; friend class PlasmoidServiceJob;