1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// This file is part of the aMule Project.
//
// Copyright (c) 2010-2011 Werner Mahr (Vollstrecker) <amule@vollstreckernet.de>
//
// Any parts of this program contributed by third-party developers are copyrighted
// by their respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

#include "plasma-engine-plasmamule.h"
#include "qt-emc.h"

#include <kdebug.h>
#include <knotification.h>

#include <kio/scheduler.h>

#include <plasma/datacontainer.h>

#include <QDir>
#include <QTimer>

#include <QtDBus/QDBusInterface>

PlasmaMuleEngine::PlasmaMuleEngine (QObject* parent, const QVariantList& args)
	: Plasma::DataEngine (parent, args)
{
	Q_UNUSED (args)
	setMinimumPollingInterval (0);
}

bool PlasmaMuleEngine::sourceRequestEvent (const QString &name)
{
	return updateSourceEvent (name);
}

QStringList PlasmaMuleEngine::sources() const
{
	return QStringList() << "cat_dirs"
		<< "cat_names"
		<< "clients_in_up_queue"
		<< "config_found"
		<< "down_speed"
		<< "ed2k_state"
		<< "ed2k_server_name"
		<< "ed2k_server_ip"
		<< "ed2k_server_port"
		<< "ed2k_id_high_low"
		<< "kad_status"
		<< "nickname"
		<< "os_active"
		<< "session_bytes_downloaded"
		<< "session_bytes_uploaded"
		<< "shared_files_count"
		<< "total_bytes_downloaded"
		<< "total_bytes_uploaded"
		<< "up_speed"
		<< "uptime"
		<< "version";
}

void PlasmaMuleEngine::init ()
{
	Home = QDir::homePath();

	QTimer *timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
	timer->start(60000);
	m_timer = TRUE;
	setData(I18N_NOOP("uptime"), 0);

	if (!Home.endsWith("/"))
	{
		Home += "/";
	}

	m_debugChannel = KDebug::registerArea ("plasmamule-engine",
#ifdef __DEBUG__
	true
#else
	false
#endif
	);

	regDbus();
	initVals();
}

void PlasmaMuleEngine::regDbus ()
{
	new EngineAdaptor(this);
	QDBusConnection dbus = QDBusConnection::sessionBus();
	dbus.registerObject("/Link", this);
	kDebug(m_debugChannel) << "Registered dbus: " << dbus.registerService("org.amule.engine");
}

void PlasmaMuleEngine::downloadFinished (KIO::Job* job,const QByteArray& data)
{

	if (data.length() == 0)
	{
		KNotification::event(KNotification::Notification, QString("Download of %1 failed.").arg(job->queryMetaData("Name")));
		return;
	}

	kDebug(m_debugChannel) << QString("Finished download of %1").arg(job->queryMetaData("Name"));

	QString downloadFileName(QString("/tmp/plasmamule-download-%1.emulecollection").arg(qrand()));

	QFile downloadFile(downloadFileName);

	if (!downloadFile.open (QIODevice::WriteOnly | QIODevice::Append))
	{
		KNotification::event(KNotification::Notification, QString("%1 can't be written to temp-file.").arg(job->queryMetaData("Name")));
		return;
	}

	QDataStream out(&downloadFile);
	out.writeRawData(data, data.length());
	downloadFile.close();

	engine_add_link (downloadFileName, job->queryMetaData("Category").toInt(), job->queryMetaData("Name"));

	downloadFile.remove();
}

void PlasmaMuleEngine::engine_add_link (const QString &link, const int &category, const QString &printname)
{
	kDebug(m_debugChannel) << "Received Link " << link << " with cat " << category;

	QString link_to_write;


	if (link.startsWith("ed2k:") || link.startsWith("magnet:"))
	{
		link_to_write = link;

		if (category > 0)
		{
			link_to_write.append(QString(":%1").arg(category));
		}

		link_to_write.append("\n");
	} else if (link.contains(".emulecollection") && KUrl(link).isLocalFile())
	{
		qtEmc* collection = new qtEmc(link);
		if (collection->isValid())
		{
			QStringList links = collection->getLinks();
			for (QStringList::const_iterator constIterator = links.constBegin(); constIterator != links.constEnd(); ++constIterator)
			{
				link_to_write.append(*constIterator);

				if (category > 0)
				{
					link_to_write.append(QString(":%1").arg(category));
				}

				link_to_write.append("\n");
			}
		} else {
			KNotification::event(KNotification::Notification, collection->getErrorMessage());
		}

		delete collection;
	} else {
		KIO::TransferJob *job = KIO::get(KUrl(link));
		job->addMetaData("Name", link);
		job->addMetaData("Category", QString(category));
		connect (job, SIGNAL(data(KIO::Job *, const QByteArray&)), this,
			SLOT(downloadFinished(KIO::Job *,const QByteArray&)));
		kDebug(m_debugChannel) << QString("Starting download of %1").arg(printname);
		return;
	}

	QFile link_file (Home + ".aMule/ED2KLinks");

	if (!link_file.open (QIODevice::WriteOnly | QIODevice::Append))
	{
		KNotification::event(KNotification::Notification, QString("Problem opening %1 for writing").arg(link_file.fileName()));
		return;
	}

	QTextStream out (&link_file);
	out << link_to_write;
	out.flush();
	link_file.close();

	KNotification::event(KNotification::Notification, QString("Downloading %1").arg(printname));

}

void PlasmaMuleEngine::initVals ()
{
	QStringList catDir;
	QStringList catName;
	QStringList tempIncomingDirs;
	QStringList cleanedIncomingDirs;
	QStringList::const_iterator constIterator;

	QFile config_file (Home + ".aMule/amule.conf");

	catName.append("Default");
	if (!config_file.open (QIODevice::ReadOnly | QIODevice::Text))
	{
		setData(I18N_NOOP ("config_found"), FALSE);
		return;
	}

	QTextStream in (&config_file);
	while (!in.atEnd())
	{
		QString line = in.readLine();
		if (line.startsWith ("OnlineSignature="))
		{
			if (line.remove (0,line.indexOf ("=")+1) == "1")
			{
				m_OSActive = TRUE;
			} else {
				m_OSActive = FALSE;
			}

			setData(I18N_NOOP ("os_active"), m_OSActive);
		} else if (line.contains ("OSDirectory"))
		{
			m_OSFile.setFileName(line.remove (0,line.indexOf ("=")+1) + "amulesig.dat");

		} else if (line.contains ("Incoming"))
		{
			if (!tempIncomingDirs.contains(line.remove (0,line.indexOf ("=")+1)))
			{
				tempIncomingDirs.append(line.remove (0,line.indexOf ("=")+1));
			}

			catDir.append(line.remove (0,line.indexOf ("=")+1));
		} else if (line.startsWith ("Title"))
		{
			catName.append(line.remove (0,line.indexOf ("=")+1));
		}
        }

	setData(I18N_NOOP ("cat_names"), catName);
	setData(I18N_NOOP ("cat_dirs"), catDir);

	if (m_OSActive && !m_dirwatcher.contains(m_OSFile.fileName()))
	{
		kDebug(m_debugChannel) << "Registering: " << m_OSFile.fileName() << " for monitoring";
		m_dirwatcher.addFile (m_OSFile.fileName());
		connect (&m_dirwatcher, SIGNAL (dirty (const QString &)), SLOT (file_changed (const QString&)));
		connect (&m_dirwatcher, SIGNAL (created (const QString &)), SLOT (new_file (const QString&)));
	}

	for (constIterator = tempIncomingDirs.constBegin(); constIterator != tempIncomingDirs.constEnd(); ++constIterator)
	{
		if (!m_dirwatcher.contains(*constIterator))
		{
			kDebug(m_debugChannel) << "Registering: " << *constIterator << " for monitoring";
			cleanedIncomingDirs.append (*constIterator);
			m_dirwatcher.addDir (*constIterator, KDirWatch::WatchFiles);
		} else {
			cleanedIncomingDirs.append (*constIterator);
		}
	}

	for (constIterator = m_incoming_dirs.constBegin(); constIterator != m_incoming_dirs.constEnd(); ++constIterator)
	{
		if (!cleanedIncomingDirs.contains (*constIterator))
		{
			kDebug(m_debugChannel) << "Removing " << *constIterator << " from monitored dirs";
			m_dirwatcher.removeDir (*constIterator);
		}
	}

	m_incoming_dirs = cleanedIncomingDirs;
	config_file.close ();
	setName("plasmamule");
	setData(I18N_NOOP ("config_found"), TRUE);
	scheduleSourcesUpdated();
}

void PlasmaMuleEngine::file_changed (const QString &path)
{
	if (path == m_OSFile.fileName())
	{
		kDebug(m_debugChannel) << "Rereading " << path;
		updateSourceEvent ("dummy");
	}
}

void PlasmaMuleEngine::new_file (const QString &path)
{
	if (path != m_OSFile.fileName())
	{
		kDebug(m_debugChannel) << "File " << path << "was created";
		KNotification::event(KNotification::Notification, QString("Finished Download of %1").arg(path));
	} else {
		kDebug(m_debugChannel) << "Rereading " << path;
		updateSourceEvent ("dummy");
	}
}

void PlasmaMuleEngine::timeout()
{
	initVals();
}

bool PlasmaMuleEngine::updateSourceEvent(const QString &name)
{
	Q_UNUSED (name)

	if (m_OSFile.open (QIODevice::ReadOnly | QIODevice::Text) && m_OSActive)
	{
		QTextStream in (&m_OSFile);
		setData(I18N_NOOP("ed2k_state"), in.readLine().toInt());
		setData(I18N_NOOP("ed2k_server_name"), in.readLine());
		setData(I18N_NOOP("ed2k_server_ip"), in.readLine());
		setData(I18N_NOOP("ed2k_server_port"), in.readLine().toInt());
		setData(I18N_NOOP("ed2k_id_high_low"), in.readLine());
		setData(I18N_NOOP("kad_status"), in.readLine().toInt());
		setData(I18N_NOOP("down_speed"), in.readLine().toDouble());
		setData(I18N_NOOP("up_speed"), in.readLine().toDouble());
		setData(I18N_NOOP("clients_in_up_queue"), in.readLine().toInt());
		setData(I18N_NOOP("shared_files_count"), in.readLine().toInt());
		setData(I18N_NOOP("nickname"), in.readLine());
		setData(I18N_NOOP("total_bytes_downloaded"), in.readLine().toLongLong());
		setData(I18N_NOOP("total_bytes_uploaded"), in.readLine().toLongLong());
		setData(I18N_NOOP("version"), in.readLine());
		setData(I18N_NOOP("session_bytes_downloaded"), in.readLine().toLongLong());
		setData(I18N_NOOP("session_bytes_uploaded"), in.readLine().toLongLong());
		setData(I18N_NOOP("uptime"), in.readLine().toInt());
		m_OSFile.close();
		scheduleSourcesUpdated();
		return true;
	} else {
		return false;
	}
}

K_EXPORT_PLASMA_DATAENGINE(plasmamule, PlasmaMuleEngine)<--- There is an unknown macro here somewhere. Configuration is required. If K_EXPORT_PLASMA_DATAENGINE is a macro then please configure it.

EngineAdaptor::EngineAdaptor(QObject *parent): QDBusAbstractAdaptor(parent)
{
	setAutoRelaySignals(true);
}

EngineAdaptor::~EngineAdaptor()
{
}

void EngineAdaptor::engine_add_link(const QString &link, const int &category)
{
	QMetaObject::invokeMethod(parent(), "engine_add_link", Q_ARG(QString, link), Q_ARG(int, category), Q_ARG(QString, link));
}

#include "plasma-engine-plasmamule.moc"