void ContactRegistry::toFile(QString const& filename) const { QFile outputFile(filename); if (!outputFile.open(QFile::WriteOnly | QFile::Text)) { throw IllegalArgumentException() << QString("Could not open the specified contacts file for writing: %1").arg(filename).toStdString(); } QTextStream outStream(&outputFile); outStream.setCodec("UTF-8"); // change the file codec to UTF-8. outStream << "# This is a comment line.\n"; outStream << "# Format of this file: \n"; outStream << "# IDENTITY : PUBKEY : Nickname\n"; outStream << "# where \n"; outStream << "# - IDENTITY is an eight character ID of the form [A-Z0-9]{8} and stands for a users public id,\n"; outStream << "# - PUBKEY is an 64 character key of the form [a-fA-F0-9]{64} and stands for a users 32-Byte long-term public key,\n"; outStream << "# - Nickname is an optional screen-name for the given identity.\n"; outStream << "# GROUPID : GROUPOWNER : IDENTITY, IDENTITY, IDENTITY : Group Name\n"; outStream << "# where \n"; outStream << "# - IDENTITY is an eight character ID of the form [A-Z0-9]{8} and stands for a users public id,\n"; outStream << "# - GROUPID is an 16 character key of the form [a-fA-F0-9]{16} and stands for a groups unique identifier,\n"; outStream << "# - GROUPOWNER is an IDENTITY and stands for a groups creator and owner,\n"; outStream << "# - Group Name is the displayed title of the group.\n"; accessMutex.lock(); // Convert identityToIdentityContactHashMap into a QMap for sorted output. QMap<ContactId, IdentityContact*> sortedContactMap; QHash<ContactId, IdentityContact*>::const_iterator itContacts = identityToIdentityContactHashMap.constBegin(); QHash<ContactId, IdentityContact*>::const_iterator endContacts = identityToIdentityContactHashMap.constEnd(); for (; itContacts != endContacts; ++itContacts) { sortedContactMap.insert(itContacts.key(), itContacts.value()); } QMap<ContactId, IdentityContact*>::const_iterator itSortedContacts = sortedContactMap.constBegin(); QMap<ContactId, IdentityContact*>::const_iterator endSortedContacts = sortedContactMap.constEnd(); for (; itSortedContacts != endSortedContacts; ++itSortedContacts) { IdentityContact* ic = itSortedContacts.value(); QString nickname = ic->getNickname(); if (nickname.isNull() || nickname.isEmpty()) { nickname = ""; } outStream << ic->getContactId().toQString() << " : " << QString(ic->getPublicKey().getPublicKey().toHex()) << " : " << nickname << "\n"; } // Convert identityToGroupContactHashMap into a QMap for sorted output. QMap<GroupId, GroupContact*> sortedGroupMap; QHash<GroupId, GroupContact*>::const_iterator itGroups = identityToGroupContactHashMap.constBegin(); QHash<GroupId, GroupContact*>::const_iterator endGroups = identityToGroupContactHashMap.constEnd(); for (; itGroups != endGroups; ++itGroups) { sortedGroupMap.insert(itGroups.key(), itGroups.value()); } QMap<GroupId, GroupContact*>::const_iterator itSortedGroups = sortedGroupMap.constBegin(); QMap<GroupId, GroupContact*>::const_iterator endSortedGroups = sortedGroupMap.constEnd(); for (; itSortedGroups != endSortedGroups; ++itSortedGroups) { GroupContact* gc = itSortedGroups.value(); QSet<ContactId> const& members = gc->getGroupMembers(); QSet<ContactId>::const_iterator membersIt = members.constBegin(); QSet<ContactId>::const_iterator membersEnd = members.constEnd(); QStringList memberIds; for (; membersIt != membersEnd; ++membersIt) { memberIds.append(membersIt->toQString()); } memberIds.sort(Qt::CaseInsensitive); outStream << gc->getGroupId().toContactFileFormat() << " : " << memberIds.join(',') << " : " << gc->getContactName() << "\n"; } accessMutex.unlock(); outputFile.close(); }