Exemplo n.º 1
0
void Client::updateClientSettingsInfo(QString const& currentFileName) {
	ui.lblClientIni->setText(currentFileName);
	if (clientConfiguration != nullptr) {
		if (!contactRegistry->hasIdentity(clientConfiguration->getClientIdentity())) {
			IdentityContact* iContact = new IdentityContact(clientConfiguration->getClientIdentity(), clientConfiguration->getClientLongTermKeyPair());
			iContact->setNickname("You");
			contactRegistry->addIdentity(iContact);
		}
		contactRegistry->setSelfContact(contactRegistry->getIdentity(clientConfiguration->getClientIdentity()));
	}
}
Exemplo n.º 2
0
void ContactRegistry::fromFile(QString const& filename, bool dryRun) {
	if (!QFile::exists(filename)) {
		throw IllegalArgumentException() << QString("Could not open the specified contacts file as it does not exist: %1").arg(filename).toStdString();
	}
	QFile inputFile(filename);
	if (!inputFile.open(QFile::ReadOnly | QFile::Text)) {
		throw IllegalArgumentException() << QString("Could not open the specified contacts file for reading: %1").arg(filename).toStdString();
	}

	QRegExp commentRegExp("^\\s*#.*$", Qt::CaseInsensitive, QRegExp::RegExp2);
	QRegExp identityRegExp("^\\s*([A-Z0-9]{8})\\s*:\\s*([a-fA-F0-9]{64})\\s*(?::\\s*(.*)\\s*)?$", Qt::CaseInsensitive, QRegExp::RegExp2);
	QRegExp groupRegExp("^\\s*([a-fA-F0-9]{16})\\s*:\\s*([A-Z0-9]{8})\\s*:\\s*([A-Z0-9]{8}(?:\\s*,\\s*[A-Z0-9]{8})*)\\s*:\\s*(.*)\\s*$", Qt::CaseInsensitive, QRegExp::RegExp2);

	QTextStream in(&inputFile);
	in.setCodec("UTF-8"); // change the file codec to UTF-8.
	while (!in.atEnd()) {
		QString line = in.readLine().trimmed();
		if (line.isEmpty() || commentRegExp.exactMatch(line)) {
			continue;
		} else if (identityRegExp.exactMatch(line)) {
			if (!dryRun) {
				accessMutex.lock();

				ContactId const contactId(identityRegExp.cap(1));
				IdentityContact* iContact = new IdentityContact(contactId, PublicKey::fromHexString(identityRegExp.cap(2)));
				if (!identityRegExp.cap(3).trimmed().isEmpty()) {
					// Nickname given.
					iContact->setNickname(identityRegExp.cap(3).trimmed());
				}
				connectContact(iContact);
				identityToIdentityContactHashMap.insert(contactId, iContact);

				accessMutex.unlock();
			}
		} else if (groupRegExp.exactMatch(line)) {
			if (!dryRun) {
				accessMutex.lock();

				quint64 groupIdentity = IdentityHelper::groupIdByteArrayToUint64(QByteArray::fromHex(groupRegExp.cap(1).toLatin1()));
				quint64 groupOwner = IdentityHelper::identityStringToUint64(groupRegExp.cap(2));
				GroupContact* gContact = new GroupContact(GroupId(groupOwner, groupIdentity));
				if (!groupRegExp.cap(4).trimmed().isEmpty()) {
					gContact->setGroupName(groupRegExp.cap(4).trimmed());
				}

				QStringList ids = groupRegExp.cap(3).split(',', QString::SkipEmptyParts);
				if (ids.size() == 0) {
					throw IllegalArgumentException() << QString("Invalid or ill-formated line in contacts file \"%1\".\nProblematic line: %2").arg(filename).arg(line).toStdString();
				}

				QStringList::const_iterator it = ids.constBegin();
				QStringList::const_iterator end = ids.constEnd();
				for (; it != end; ++it) {
					QString trimmedId(it->trimmed());
					ContactId const memberId(trimmedId);
					gContact->addMember(memberId);
				}

				connectContact(gContact);
				identityToGroupContactHashMap.insert(GroupId(groupOwner, groupIdentity), gContact);

				accessMutex.unlock();
			}
		} else {
			throw IllegalArgumentException() << QString("Invalid or ill-formated line in contacts file \"%1\".\nProblematic line: %2").arg(filename).arg(line).toStdString();
		}
	}

	inputFile.close();
	if (!dryRun) {
		emit identitiesChanged();
	}
}