~nico-izo-ya/+junk/aaron2

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
/****************************************************************************
 * options.cpp
 *  Copyright © 2010, Vsevolod Velichko <torkvema@gmail.com>.
 *  Licence: GPLv3 or later
 *
 ****************************************************************************
 *                                                                          *
 *   This library 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.                                    *
 *                                                                          *
 ****************************************************************************/

#include "options.h"
#include "3rdparty/qxt/qxtcommandoptions.h"
#include <boost/serialization/singleton.hpp>
#include <stdlib.h>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QCoreApplication>

Options::Options () :
	QObject(QCoreApplication::instance())
{
	// Default options
	options["botjid"] = "bot@xmpp.org/bot";
	options["botpassword"] = "password";
	options["conferenceNick"] = "nickname";
	options["conferenceName"] = "talks@conference.qutim.org";
}

Options::~Options ()
{
}

void Options::readCommandLine ()
{
	QxtCommandOptions parser;
	parser.add("jid", "Full JID of the bot", QxtCommandOptions::ValueRequired);
	parser.alias("jid", "j");
	parser.add("password", "Jabber password of the bot", QxtCommandOptions::ValueRequired);
	parser.alias("password", "p");
	parser.add("nick", "Nickname of the bot", QxtCommandOptions::ValueRequired);
	parser.alias("nick", "n");
	parser.add("conference", "Conference address", QxtCommandOptions::ValueRequired);
	parser.alias("conference", "c");
	parser.add("database", "SQLite Database filename", QxtCommandOptions::ValueRequired);
	parser.alias("database", "db");

	parser.add("help", "This help message");
	parser.alias("help", "?");
	parser.alias("help", "h");

	parser.parse(QCoreApplication::arguments());

	if (parser.count("help") || parser.showUnrecognizedWarning())
	{
		parser.showUsage();
		options["shouldExit"] = true;
	}
	if (parser.count("jid"))
		options["botjid"] = parser.value("jid");
	if (parser.count("password"))
		options["botpassword"] = parser.value("password");
	if (parser.count("nick"))
		options["conferenceNick"] = parser.value("nick");
	if (parser.count("conference"))
		options["conferenceName"] = parser.value("conference");
	if (parser.count("database"))
		options["database"] = parser.value("database");
}

void Options::parseConfig(const QString &path)
{
	static QRegExp commentStripper("###.*");
	static QRegExp whitespaceStripper1("^\\s+|\\s+$");
	static QRegExp whitespaceStripper2("\\s{2,}");
	static QRegExp parameterTuple("^([a-zA-Z_]+)\\s*=\\s*\"(.+)\"$");
	static QRegExp listElement("^([a-zA-Z_]+)\\[\\]\\s*=\\s*\"(.+)\"$");
	static QRegExp mapElement("^([a-zA-Z_]+)\\[([a-zA-Z_]+)\\]\\s*=\\s*\"(.+)\"$");

	if (!QFile::exists(path))
		return;
	QFile configFile(path);
	if (!configFile.open(QIODevice::ReadOnly|QIODevice::Text))
		return;
	QTextStream in(&configFile);
	while (!in.atEnd())
	{
		QString line = in.readLine();
		if (line
			.replace(commentStripper, "")
			.replace(whitespaceStripper1, "")
			.replace(whitespaceStripper2, " ")
			.isEmpty()
		   )
			continue;
		if (mapElement.indexIn(line) != -1)
		{
			QString name = mapElement.cap(1);
			qDebug() << "Loading map element" << mapElement.cap(2) << "into" << name << "with value:" << mapElement.cap(3);
			QMap<QString, QVariant> map = (isset(name) ? options[name].toMap() : QMap<QString, QVariant>());
			map[mapElement.cap(2)] = mapElement.cap(3);
			options[name] = map;
		}
		else if (listElement.indexIn(line) != -1)
		{
			QString name = listElement.cap(1);
			qDebug() << "Loading list element" << listElement.cap(2) << "into" << name;
			options[name] = (isset(name) ? options[name].toStringList() : QStringList()) << listElement.cap(2);
		}
		else if (parameterTuple.indexIn(line) != -1)
			options[parameterTuple.cap(1)] = parameterTuple.cap(2);
	}
}

void Options::readConfig()
{
	parseConfig(QDir::currentPath() + "/bot.conf");
	readCommandLine();
}

QVariant Options::operator[](const QString &name) const
{
	return options.value(name);
}

bool Options::isset(const QString &name) const
{
	return options.contains(name);
}

const Options& Options::instance()
{
	return boost::serialization::singleton<Options>::get_const_instance();
}

Options& Options::mutableInstance()
{
	return boost::serialization::singleton<Options>::get_mutable_instance();
}