2 // This file is part of the aMule Project.
4 // Copyright (c) 2010-2011 Werner Mahr (Vollstrecker) <amule@vollstreckernet.de>
6 // Any parts of this program contributed by third-party developers are copyrighted
7 // by their respective authors.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "plasma-engine-plasmamule.h"
28 #include <knotification.h>
30 #include <kio/scheduler.h>
32 #include <plasma/datacontainer.h>
37 #include <QtDBus/QDBusInterface>
39 PlasmaMuleEngine::PlasmaMuleEngine (QObject
* parent
, const QVariantList
& args
)
40 : Plasma::DataEngine (parent
, args
)
43 setMinimumPollingInterval (0);
46 bool PlasmaMuleEngine::sourceRequestEvent (const QString
&name
)
48 return updateSourceEvent (name
);
51 QStringList
PlasmaMuleEngine::sources() const
53 return QStringList() << "cat_dirs"
55 << "clients_in_up_queue"
66 << "session_bytes_downloaded"
67 << "session_bytes_uploaded"
68 << "shared_files_count"
69 << "total_bytes_downloaded"
70 << "total_bytes_uploaded"
76 void PlasmaMuleEngine::init ()
78 Home
= QDir::homePath();
80 QTimer
*timer
= new QTimer(this);
81 connect(timer
, SIGNAL(timeout()), this, SLOT(timeout()));
84 setData(I18N_NOOP("uptime"), 0);
86 if (!Home
.endsWith("/"))
91 m_debugChannel
= KDebug::registerArea ("plasmamule-engine",
103 void PlasmaMuleEngine::regDbus ()
105 new EngineAdaptor(this);
106 QDBusConnection dbus
= QDBusConnection::sessionBus();
107 dbus
.registerObject("/Link", this);
108 kDebug(m_debugChannel
) << "Registerred dbus: " << dbus
.registerService("org.amule.engine");
111 void PlasmaMuleEngine::downloadFinished (KIO::Job
* job
,const QByteArray
& data
)
114 if (data
.length() == 0)
116 KNotification::event(KNotification::Notification
, QString("Download of %1 failed.").arg(job
->queryMetaData("Name")));
120 kDebug(m_debugChannel
) << QString("Finished download of %1").arg(job
->queryMetaData("Name"));
122 QString
downloadFileName(QString("/tmp/plasmamule-download-%1.emulecollection").arg(qrand()));
124 QFile
downloadFile(downloadFileName
);
126 if (!downloadFile
.open (QIODevice::WriteOnly
| QIODevice::Append
))
128 KNotification::event(KNotification::Notification
, QString("%1 can't be written to temp-file.").arg(job
->queryMetaData("Name")));
132 QDataStream
out(&downloadFile
);
133 out
.writeRawData(data
, data
.length());
134 downloadFile
.close();
136 engine_add_link (downloadFileName
, job
->queryMetaData("Category").toInt(), job
->queryMetaData("Name"));
138 downloadFile
.remove();
141 void PlasmaMuleEngine::engine_add_link (const QString
&link
, const int &category
, const QString
&printname
)
143 kDebug(m_debugChannel
) << "Received Link " << link
<< " with cat " << category
;
145 QString link_to_write
;
148 if (link
.startsWith("ed2k:") || link
.startsWith("magnet:"))
150 link_to_write
= link
;
154 link_to_write
.append(QString(":%1").arg(category
));
157 link_to_write
.append("\n");
158 } else if (link
.contains(".emulecollection") && KUrl(link
).isLocalFile())
160 qtEmc
* collection
= new qtEmc(link
);
161 if (collection
->isValid())
163 QStringList links
= collection
->getLinks();
164 for (QStringList::const_iterator constIterator
= links
.constBegin(); constIterator
!= links
.constEnd(); ++constIterator
)
166 link_to_write
.append(*constIterator
);
170 link_to_write
.append(QString(":%1").arg(category
));
173 link_to_write
.append("\n");
176 KNotification::event(KNotification::Notification
, collection
->getErrorMessage());
181 KIO::TransferJob
*job
= KIO::get(KUrl(link
));
182 job
->addMetaData("Name", link
);
183 job
->addMetaData("Category", QString(category
));
184 connect (job
, SIGNAL(data(KIO::Job
*, const QByteArray
&)), this,
185 SLOT(downloadFinished(KIO::Job
*,const QByteArray
&)));
186 kDebug(m_debugChannel
) << QString("Starting download of %1").arg(printname
);
190 QFile
link_file (Home
+ ".aMule/ED2KLinks");
192 if (!link_file
.open (QIODevice::WriteOnly
| QIODevice::Append
))
194 KNotification::event(KNotification::Notification
, QString("Problem opening %1 for writing").arg(link_file
.fileName()));
198 QTextStream
out (&link_file
);
199 out
<< link_to_write
;
203 KNotification::event(KNotification::Notification
, QString("Downloading %1").arg(printname
));
207 void PlasmaMuleEngine::initVals ()
211 QStringList tempIncomingDirs
;
212 QStringList cleanedIncomingDirs
;
213 QStringList::const_iterator constIterator
;
215 QFile
config_file (Home
+ ".aMule/amule.conf");
217 catName
.append("Default");
218 if (!config_file
.open (QIODevice::ReadOnly
| QIODevice::Text
))
220 setData(I18N_NOOP ("config_found"), FALSE
);
224 QTextStream
in (&config_file
);
227 QString line
= in
.readLine();
228 if (line
.startsWith ("OnlineSignature="))
230 if (line
.remove (0,line
.indexOf ("=")+1) == "1")
237 setData(I18N_NOOP ("os_active"), m_OSActive
);
238 } else if (line
.contains ("OSDirectory"))
240 m_OSFile
.setFileName(line
.remove (0,line
.indexOf ("=")+1) + "amulesig.dat");
242 } else if (line
.contains ("Incoming"))
244 if (!tempIncomingDirs
.contains(line
.remove (0,line
.indexOf ("=")+1)))
246 tempIncomingDirs
.append(line
.remove (0,line
.indexOf ("=")+1));
249 catDir
.append(line
.remove (0,line
.indexOf ("=")+1));
250 } else if (line
.startsWith ("Title"))
252 catName
.append(line
.remove (0,line
.indexOf ("=")+1));
256 setData(I18N_NOOP ("cat_names"), catName
);
257 setData(I18N_NOOP ("cat_dirs"), catDir
);
259 if (m_OSActive
&& !m_dirwatcher
.contains(m_OSFile
.fileName()))
261 kDebug(m_debugChannel
) << "Registering: " << m_OSFile
.fileName() << " for monitoring";
262 m_dirwatcher
.addFile (m_OSFile
.fileName());
263 connect (&m_dirwatcher
, SIGNAL (dirty (const QString
&)), SLOT (file_changed (const QString
&)));
264 connect (&m_dirwatcher
, SIGNAL (created (const QString
&)), SLOT (new_file (const QString
&)));
267 for (constIterator
= tempIncomingDirs
.constBegin(); constIterator
!= tempIncomingDirs
.constEnd(); ++constIterator
)
269 if (!m_dirwatcher
.contains(*constIterator
))
271 kDebug(m_debugChannel
) << "Registering: " << *constIterator
<< " for monitoring";
272 cleanedIncomingDirs
.append (*constIterator
);
273 m_dirwatcher
.addDir (*constIterator
, KDirWatch::WatchFiles
);
275 cleanedIncomingDirs
.append (*constIterator
);
279 for (constIterator
= m_incoming_dirs
.constBegin(); constIterator
!= m_incoming_dirs
.constEnd(); ++constIterator
)
281 if (!cleanedIncomingDirs
.contains (*constIterator
))
283 kDebug(m_debugChannel
) << "Removing " << *constIterator
<< " from monitored dirs";
284 m_dirwatcher
.removeDir (*constIterator
);
288 m_incoming_dirs
= cleanedIncomingDirs
;
289 config_file
.close ();
290 setName("plasmamule");
291 setData(I18N_NOOP ("config_found"), TRUE
);
292 scheduleSourcesUpdated();
295 void PlasmaMuleEngine::file_changed (const QString
&path
)
297 if (path
== m_OSFile
.fileName())
299 kDebug(m_debugChannel
) << "Rereading " << path
;
300 updateSourceEvent ("dummy");
304 void PlasmaMuleEngine::new_file (const QString
&path
)
306 if (path
!= m_OSFile
.fileName())
308 kDebug(m_debugChannel
) << "File " << path
<< "was created";
309 KNotification::event(KNotification::Notification
, QString("Finished Download of %1").arg(path
));
311 kDebug(m_debugChannel
) << "Rereading " << path
;
312 updateSourceEvent ("dummy");
316 void PlasmaMuleEngine::timeout()
321 bool PlasmaMuleEngine::updateSourceEvent(const QString
&name
)
325 if (m_OSFile
.open (QIODevice::ReadOnly
| QIODevice::Text
) && m_OSActive
)
327 QTextStream
in (&m_OSFile
);
328 setData(I18N_NOOP("ed2k_state"), in
.readLine().toInt());
329 setData(I18N_NOOP("ed2k_server_name"), in
.readLine());
330 setData(I18N_NOOP("ed2k_server_ip"), in
.readLine());
331 setData(I18N_NOOP("ed2k_server_port"), in
.readLine().toInt());
332 setData(I18N_NOOP("ed2k_id_high_low"), in
.readLine());
333 setData(I18N_NOOP("kad_status"), in
.readLine().toInt());
334 setData(I18N_NOOP("down_speed"), in
.readLine().toDouble());
335 setData(I18N_NOOP("up_speed"), in
.readLine().toDouble());
336 setData(I18N_NOOP("clients_in_up_queue"), in
.readLine().toInt());
337 setData(I18N_NOOP("shared_files_count"), in
.readLine().toInt());
338 setData(I18N_NOOP("nickname"), in
.readLine());
339 setData(I18N_NOOP("total_bytes_downloaded"), in
.readLine().toLongLong());
340 setData(I18N_NOOP("total_bytes_uploaded"), in
.readLine().toLongLong());
341 setData(I18N_NOOP("version"), in
.readLine());
342 setData(I18N_NOOP("session_bytes_downloaded"), in
.readLine().toLongLong());
343 setData(I18N_NOOP("session_bytes_uploaded"), in
.readLine().toLongLong());
344 setData(I18N_NOOP("uptime"), in
.readLine().toInt());
346 scheduleSourcesUpdated();
353 K_EXPORT_PLASMA_DATAENGINE(plasmamule
, PlasmaMuleEngine
)
355 EngineAdaptor::EngineAdaptor(QObject
*parent
): QDBusAbstractAdaptor(parent
)
357 setAutoRelaySignals(true);
360 EngineAdaptor::~EngineAdaptor()
364 void EngineAdaptor::engine_add_link(const QString
&link
, const int &category
)
366 QMetaObject::invokeMethod(parent(), "engine_add_link", Q_ARG(QString
, link
), Q_ARG(int, category
), Q_ARG(QString
, link
));
369 #include "plasma-engine-plasmamule.moc"