Пример #1
0
bool DatabaseManager::removeDetection(DetectionJob* job, const bool& cascade) {

	SDPASSERT(Logger::instancePtr());
	Logger* log = Logger::instancePtr();

	if ( !detectionExists(job->id()) ) {
		log->addMessage(Logger::WARNING, __func__,
		    QString("Failed to remove detection job %1 from database. Item not found.").arg(job->id()));
		return false;
	}

	if ( cascade ) {
		QSqlQuery selTrig(QString("SELECT COUNT(*) FROM main.Trigger WHERE detection_id LIKE '%1'").arg(job->id()));
		if ( selTrig.next() )
		    log->addMessage(Logger::INFO, __func__, QString("%1 triggers of detection object %2 will be removed from database.")
		        .arg(selTrig.value(0).toString()).arg(job->id()));

		QSqlQuery rmTrig(QString("DELETE FROM main.Trigger WHERE detection_id LIKE '%1'").arg(job->id()));
		if ( rmTrig.exec() )
		    log->addMessage(Logger::INFO, __func__, QString("Removed detection object's %1 triggers from database.").arg(job->id()));
	}

	Logger::instancePtr()->addMessage(Logger::INFO, __func__,
	    QString("Removing detection object %1 from database.").arg(job->id()), true);

	return ::removeRecord(__db, "main.Detection", job->id());
}
Пример #2
0
void Attribute::setLink(Attribute *attribute) {
	if (!attribute)
		throw ProjectException("Cannot set a null link to an attribute.");

	switch (attribute->getType()) {
	case Attribute::LIFE:
	case Attribute::ENERGY:
	case Attribute::EVASION:
	case Attribute::AGILITY:
		throw ProjectException("A standard attribute cannot be linked.");
		break;
	}

	switch (getType()) {
	case Attribute::LIFE:
	case Attribute::ENERGY:
	case Attribute::EVASION:
	case Attribute::AGILITY:
		throw ProjectException("This attribute cannot be linked to one of the standard attributes.");
		break;
	}

	if (_type == attribute->getType())
		throw ProjectException("A link can only be established between an offensive and a defensive attribute.");

	if (!!_link)
		disconnect(_link, 0, this, 0);

	_link = attribute;
	connect(_link, SIGNAL(deleteSignal(QString)), this, SLOT(attributeDeleted(QString)));

	Logger *logger = Logger::instance();
	logger->addMessage("Linked an attribute to attribute " + _name);
	logger->setStatus(true);
}
Пример #3
0
bool ParameterManager::registerObjectParameter(const QString& objectName,
                                               const QString& name,
                                               QVariant data) {

	Logger* log = Logger::instancePtr();

	if ( __objectParameters.contains(objectName) ) {

		ParameterList& l = __objectParameters[objectName];
		if ( l.contains(name) ) {
			log->addMessage(Logger::WARNING, __func__, QString("Object %1 parameter already stored in list.")
			    .arg(objectName).arg(name));
			return false;
		}
		else {
			l.insert(name, data);
		}
	}

	else {
		ParameterList l;
		l.insert(name, data);
		__objectParameters.insert(objectName, l);
	}

	return true;
}
Пример #4
0
Attribute::~Attribute() {
	emit deleteSignal(_name);

	Logger *logger = Logger::instance();
	logger->setStatus(true);
	logger->addMessage("Destroyed attribute " + _name);
}
Пример #5
0
CharacterSystem::CharacterSystem()
	: _characters(0) {

		Logger *logger = Logger::instance();
		logger->addMessage("Created a character system");
		logger->setStatus(true);
}
Пример #6
0
/**
*	Standard destructor.
*/
Tileset::~Tileset() {
	emit deleteSignal(_name);

	Logger *logger = Logger::instance();
	logger->setStatus(true);
	logger->addMessage("Destroyed tileset " + _name);
}
Пример #7
0
ClassSystem::ClassSystem()
	: _classes(0) {

		Logger *logger = Logger::instance();
		logger->addMessage("Created an cat system");
		logger->setStatus(true);
}
Пример #8
0
/**
*	Delete all messages of this text event.
*/
void TextEvent::removeAllMessages() {
	_messages.clear();

	Logger *logger = Logger::instance();
	logger->addMessage("Removed all messages from text event " + _name);
	logger->setStatus(true);
}
Пример #9
0
BattleEvent::BattleEvent(QString ID, Event::Trigger trigger)
	: Event(ID, Event::BATTLE, trigger), _activation(1), _groups(0) {

		Logger *logger = Logger::instance();
		logger->addMessage("Created battle event with ID " + _name);
		logger->setStatus(true);
}
Пример #10
0
/**
*	Constructs a text event with name <i>name</i> and trigger <i>trigger</i>.
*/
TextEvent::TextEvent(QString name, Event::Trigger trigger)
	: Event(name, Event::TEXT, trigger), _messages(QVector<QString>(0)) {

		Logger *logger = Logger::instance();
		logger->addMessage("Created text event with ID " + _name);
		logger->setStatus(true);
}
Пример #11
0
Attribute::Attribute(QString name, Attribute::Type type)
	: _name(name), _type(type), _link(0) {

		Logger *logger = Logger::instance();
		logger->setStatus(true);
		logger->addMessage("Created attribute " + _name);
}
Пример #12
0
/**
*	Add a message to the end of this text event.
*/
void TextEvent::addMessage(QString message) {
	_messages.append(message);

	Logger *logger = Logger::instance();
	logger->addMessage("Added message to text event " + _name);
	logger->setStatus(true);
}
Пример #13
0
Element::Element(QString name)
	: _name(name),
	_resistant(QVector<Element*>(0)),
	_vulnerable(QVector<Element*>(0)) {

		Logger *logger = Logger::instance();
		logger->setStatus(true);
		logger->addMessage("Created element " + _name);
}
Пример #14
0
void Element::setName(QString name) {
	if (name == QString())
		throw ProjectException("An element should not have an empty name.");

	_name = name;
	Logger *logger = Logger::instance();
	logger->setStatus(true);
	logger->addMessage("Changed name of element to " + _name);
}
Пример #15
0
/**
*	Standard constructor.
*/
EventSystem::EventSystem()
	: _textEvents(QVector<TextEvent*>(0)),
	_portalEvents(QVector<PortalEvent*>(0)),
	_battleEvents(QVector<BattleEvent*>(0)) {

		Logger *logger = Logger::instance();
		logger->addMessage("Created the event system");
		logger->setStatus(true);
}
Пример #16
0
/**
*	Construct a tileset with name <i>name</i>, type <i>type</i> and load the tileset image <i>imagename</i>.
*/
Tileset::Tileset(QString name, Type type, QString imagename)
	: _type(type),
	_name(name),
	_imagename(imagename) {

		Logger *logger = Logger::instance();
		logger->setStatus(true);
		logger->addMessage("Created tileset " + _name);
}
Пример #17
0
void Attribute::unsetLink() {
	if (!!_link)
		disconnect(_link, 0, this, 0);

	_link = 0;
	Logger *logger = Logger::instance();
	logger->addMessage("Removed attribute link from attribute " + _name);
	logger->setStatus(true);
}
Пример #18
0
/**
*	Remove the message specified at position <i>position</i>.
*	The position must be valid, a <i>ProjectException</i> is thrown otherwise.
*/
void TextEvent::removeMessage(int position) {
	if ((position < 0) || (position >= _messages.size()))
		throw ProjectException("The index " + QString::number(position) + " does not exist in the text event.");

	_messages.remove(position);

	Logger *logger = Logger::instance();
	logger->addMessage("Removed message from text event " + _name);
	logger->setStatus(true);
}
Пример #19
0
void BattleEvent::setActivation(int activation) {
	if ((activation < 1) || (activation > 100))
		throw ProjectException("The activation chance of a battle event should lie between 1 and 100");

	_activation = activation;

	Logger *logger = Logger::instance();
	logger->addMessage("Changed activation of battle event " + _name);
	logger->setStatus(true);
}
Пример #20
0
/**
*	Set the name of the tileset.
*/
void Tileset::setName(QString name) {
	if (name == QString())
		throw ProjectException("A tileset cannot have an empty name.");

	_name = name;

	Logger *logger = Logger::instance();
	logger->setStatus(true);
	logger->addMessage("Changed name of tileset to " + _name);
}
Пример #21
0
Element::~Element() {
	emit deleteSignal(_name);

	this->_resistant.clear();
	this->_vulnerable.clear();

	Logger *logger = Logger::instance();
	logger->setStatus(true);
	logger->addMessage("Destroyed element " + _name);
}
Пример #22
0
void Application::runExternalProgram(BitTorrent::TorrentHandle *const torrent) const
{
    QString program = Preferences::instance()->getAutoRunProgram();
    program.replace("%N", torrent->name());
    program.replace("%L", torrent->category());
    program.replace("%F", Utils::Fs::toNativePath(torrent->contentPath()));
    program.replace("%R", Utils::Fs::toNativePath(torrent->rootPath()));
    program.replace("%D", Utils::Fs::toNativePath(torrent->savePath()));
    program.replace("%C", QString::number(torrent->filesCount()));
    program.replace("%Z", QString::number(torrent->totalSize()));
    program.replace("%T", torrent->currentTracker());
    program.replace("%I", torrent->hash());

    Logger *logger = Logger::instance();
    logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name()).arg(program));

#if defined(Q_OS_UNIX)
    QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program});
#elif defined(Q_OS_WIN)  // test cmd: `echo "%F" > "c:\ab ba.txt"`
    program.prepend(QLatin1String("\"")).append(QLatin1String("\""));
    program.prepend(Utils::Misc::windowsSystemPath() + QLatin1String("\\cmd.exe /C "));
    const int cmdMaxLength = 32768;  // max length (incl. terminate char) for `lpCommandLine` in `CreateProcessW()`
    if ((program.size() + 1) > cmdMaxLength) {
        logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(cmdMaxLength), Log::CRITICAL);
        return;
    }

    STARTUPINFOW si = {0};
    si.cb = sizeof(si);
    PROCESS_INFORMATION pi = {0};

    WCHAR *arg = new WCHAR[program.size() + 1];
    program.toWCharArray(arg);
    arg[program.size()] = L'\0';
    if (CreateProcessW(NULL, arg, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }
    delete[] arg;
#else
    QProcess::startDetached(program);
#endif
}
Пример #23
0
void EnemyDrops::setExperience(int experience) {
	if (experience < 0)
		throw ProjectException("The experience should be greater than 0.");

	_experience = experience;

	Logger *logger = Logger::instance();
	logger->addMessage("Set experience of enemy drops");
	logger->setStatus(true);
}
Пример #24
0
void CharacterSystem::addCharacter(Character *character) {
	if (!character)
		throw ProjectException("Cannot add a null character.");

	_characters.append(character);

	Logger *logger = Logger::instance();
	logger->addMessage("Added character " + character->getName() + " to the project");
	logger->setStatus(true);
}
Пример #25
0
void Attribute::setName(QString name) {
	if (name == QString())
		throw ProjectException("An attribute should not have an empty name");

	this->_name = name;

	Logger *logger = Logger::instance();
	logger->setStatus(true);
	logger->addMessage("Changed name of attribute to " + _name);
}
Пример #26
0
/**
*	Edit the message with text <i>message</i> specified at position <i>position</i>.
*	The position must be valid, a <i>ProjectException</i> is thrown otherwise.
*/
void TextEvent::editMessage(QString message, int position) {
	if ((position < 0) || (position >= _messages.size()))
		throw ProjectException("The index " + QString::number(position) + " does not exist in the text event.");

	_messages.replace(position, message);

	Logger *logger = Logger::instance();
	logger->addMessage("Edited message of text event " + _name);
	logger->setStatus(true);
}
Пример #27
0
void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) const
{
    QString program = Preferences::instance()->getAutoRunProgram().trimmed();
    program.replace("%N", torrent->name());
    program.replace("%L", torrent->category());

    QStringList tags = torrent->tags().toList();
    std::sort(tags.begin(), tags.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
    program.replace("%G", tags.join(','));

#if defined(Q_OS_WIN)
    const auto chopPathSep = [](const QString &str) -> QString
    {
        if (str.endsWith('\\'))
            return str.mid(0, (str.length() -1));
        return str;
    };
    program.replace("%F", chopPathSep(Utils::Fs::toNativePath(torrent->contentPath())));
    program.replace("%R", chopPathSep(Utils::Fs::toNativePath(torrent->rootPath())));
    program.replace("%D", chopPathSep(Utils::Fs::toNativePath(torrent->savePath())));
#else
    program.replace("%F", Utils::Fs::toNativePath(torrent->contentPath()));
    program.replace("%R", Utils::Fs::toNativePath(torrent->rootPath()));
    program.replace("%D", Utils::Fs::toNativePath(torrent->savePath()));
#endif
    program.replace("%C", QString::number(torrent->filesCount()));
    program.replace("%Z", QString::number(torrent->totalSize()));
    program.replace("%T", torrent->currentTracker());
    program.replace("%I", torrent->hash());

    Logger *logger = Logger::instance();
    logger->addMessage(tr("Torrent: %1, running external program, command: %2").arg(torrent->name(), program));

#if defined(Q_OS_WIN)
    std::unique_ptr<wchar_t[]> programWchar(new wchar_t[program.length() + 1] {});
    program.toWCharArray(programWchar.get());

    // Need to split arguments manually because QProcess::startDetached(QString)
    // will strip off empty parameters.
    // E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
    int argCount = 0;
    LPWSTR *args = ::CommandLineToArgvW(programWchar.get(), &argCount);

    QStringList argList;
    for (int i = 1; i < argCount; ++i)
        argList += QString::fromWCharArray(args[i]);

    QProcess::startDetached(QString::fromWCharArray(args[0]), argList);

    ::LocalFree(args);
#else
    QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program});
#endif
}
Пример #28
0
void ClassSystem::addClass(Class *cat) {
	if (!cat)
		throw ProjectException("Cannot add a null cat.");

	_classes.append(cat);
	emit addedClass(cat);

	Logger *logger = Logger::instance();
	logger->addMessage("Added cat " + cat->getName() + " to the project");
	logger->setStatus(true);
}
Пример #29
0
QVariant ParameterManager::objectParameter(const QString& objectName,
                                           const QString& name) {

	Logger* log = Logger::instancePtr();

	if ( !__objectParameters.contains(objectName) ) {
		log->addMessage(Logger::WARNING, __func__, QString("Couldn't find Object %1 in list")
		    .arg(objectName));
		return false;
	}

	ParameterList& l = __objectParameters[objectName];
	if ( !l.contains(name) ) {
		log->addMessage(Logger::WARNING, __func__, QString("Couldn't find Object's %1 parameter %2 in list")
		    .arg(objectName).arg(name));
		return false;
	}

	return l[name];
}
Пример #30
0
DatabaseManager::DatabaseManager(const QString& filename) :
		__dbFile(filename) {

	SDPASSERT(Logger::instancePtr());
	Logger* log = Logger::instancePtr();

	log->addMessage(Logger::DEBUG, __func__, QString("Initiating application database from file %1...").arg(filename));

	if ( openDatabase(filename) ) {
		log->addMessage(Logger::INFO, __func__, "Database found and opened.");
		if ( __db.tables().isEmpty() ) {
			log->addMessage(Logger::INFO, __func__, "Database appears to be empty, setting up entities.");
			initDatabase();
		}
	}
	else {
		log->addMessage(Logger::CRITICAL, __func__, QString("Failed to open database (%1).").arg(filename));
		log->addMessage(Logger::WARNING, __func__, "Initiating new database for application.");
		initDatabase();
	}
}