summaryrefslogtreecommitdiff
path: root/sowatchd/main.cpp
blob: 22e63a2ff204522113656fe7ccc1c9aad0ce6071 (plain)
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
#include <QtGui/QApplication>
#include <QtServiceFramework/QServiceManager>
#include <QtServiceFramework/QRemoteServiceRegister>

#include <sowatch.h>
#include "global.h"
#include "service.h"

namespace sowatch
{
	Daemon* daemon;
}

using namespace sowatch;
QTM_USE_NAMESPACE


static QString adjustPath(const QString &path)
{
#ifdef Q_OS_UNIX
#ifdef Q_OS_MAC
	if (!QDir::isAbsolutePath(path))
		return QCoreApplication::applicationDirPath()
				+ QLatin1String("/../Resources/") + path;
#else
	QString pathInInstallDir;
	const QString applicationDirPath = QCoreApplication::applicationDirPath();
	pathInInstallDir = QString::fromAscii("%1/../%2").arg(applicationDirPath, path);

	if (QFileInfo(pathInInstallDir).exists())
		return pathInInstallDir;
#endif
#endif
	return path;
}

static void unregisterService()
{
	QServiceManager m;
	m.removeService("sowatch-service");
}

static void registerService()
{
	unregisterService();
	QServiceManager m;
	QString path = adjustPath("xml/service.xml");
	if (!m.addService(path)) {
		qWarning() << "Cannot register sowatch-service" << m.error() << "from" << path;
	}
}

int main(int argc, char *argv[])
{
	QApplication a(argc, argv); // Some plugins might require a UI.
	QApplication::setOrganizationDomain("com.javispedro.sowatch");
	QApplication::setOrganizationName("sowatch");
	QApplication::setApplicationName("sowatchd");

	sowatch::daemon = new Daemon();

	registerService();

	QRemoteServiceRegister *serviceRegister = new QRemoteServiceRegister();
	QRemoteServiceRegister::Entry entry =
		serviceRegister->createEntry<Service>("sowatch-service",
			"com.javispedro.sowatch.service", "1.0");
	entry.setInstantiationType(QRemoteServiceRegister::PrivateInstance);

	serviceRegister->setQuitOnLastInstanceClosed(false);
	serviceRegister->publishEntries("sowatchd");

	int res = a.exec();

	delete serviceRegister;
	delete sowatch::daemon;

	return res;
}