conversion from QSettings to XML

This commit is contained in:
sehraf 2015-08-19 19:02:28 +02:00
parent 80d765e1d6
commit 941959cc86

View File

@ -27,6 +27,7 @@
#include <QWidgetAction> #include <QWidgetAction>
#include <QDateTime> #include <QDateTime>
#include <QPainter> #include <QPainter>
#include <QtXml>
#include "rsserver/rsaccounts.h" #include "rsserver/rsaccounts.h"
#include "retroshare/rspeers.h" #include "retroshare/rspeers.h"
@ -1665,8 +1666,8 @@ void FriendList::exportFriendlistClicked()
QMessageBox mbox; QMessageBox mbox;
mbox.setIcon(QMessageBox::Information); mbox.setIcon(QMessageBox::Information);
mbox.setText(tr("Done!")); mbox.setText(tr("Done!"));
mbox.setInformativeText("Your friendlist is stored at:\n" + fileName + mbox.setInformativeText(tr("Your friendlist is stored at:\n") + fileName +
"\n(keep in mind that the file is unencrypted!)"); tr("\n(keep in mind that the file is unencrypted!)"));
mbox.setStandardButtons(QMessageBox::Ok); mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec(); mbox.exec();
} }
@ -1683,16 +1684,16 @@ void FriendList::importFriendlistClicked()
QMessageBox mbox; QMessageBox mbox;
mbox.setIcon(QMessageBox::Information); mbox.setIcon(QMessageBox::Information);
mbox.setText(tr("Done!")); mbox.setText(tr("Done!"));
mbox.setInformativeText("Your friendlist was imported from:\n" + fileName); mbox.setInformativeText(tr("Your friendlist was imported from:\n") + fileName);
mbox.setStandardButtons(QMessageBox::Ok); mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec(); mbox.exec();
} else { } else {
QMessageBox mbox; QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning); mbox.setIcon(QMessageBox::Warning);
mbox.setText(tr("Done - but errors happened!")); mbox.setText(tr("Done - but errors happened!"));
mbox.setInformativeText("Your friendlist was imported from:\n" + fileName + mbox.setInformativeText(tr("Your friendlist was imported from:\n") + fileName +
(errorPeers ? "\nat least one peer was not added" : "") + (errorPeers ? tr("\nat least one peer was not added") : "") +
(errorGroups ? "\nat least one peer was not added to a group" : "") (errorGroups ? tr("\nat least one peer was not added to a group") : "")
); );
mbox.setStandardButtons(QMessageBox::Ok); mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec(); mbox.exec();
@ -1713,7 +1714,7 @@ bool FriendList::importExportFriendlistFileDialog(QString &fileName, bool import
RshareSettings::LASTDIR_CERT, RshareSettings::LASTDIR_CERT,
(import ? tr("Select file for importing yoour friendlist from") : (import ? tr("Select file for importing yoour friendlist from") :
tr("Select a file for exporting your friendlist to")), tr("Select a file for exporting your friendlist to")),
tr("Text file (*.txt);; Ini File (*.ini);;All Files (*)"), tr("XML File (*.xml);;All Files (*)"),
fileName, fileName,
NULL, NULL,
(import ? QFileDialog::DontConfirmOverwrite : (QFileDialog::Options)0) (import ? QFileDialog::DontConfirmOverwrite : (QFileDialog::Options)0)
@ -1722,7 +1723,7 @@ bool FriendList::importExportFriendlistFileDialog(QString &fileName, bool import
QMessageBox mbox; QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning); mbox.setIcon(QMessageBox::Warning);
mbox.setText(tr("Error")); mbox.setText(tr("Error"));
mbox.setInformativeText("Failed to get a file!"); mbox.setInformativeText(tr("Failed to get a file!"));
mbox.setStandardButtons(QMessageBox::Ok); mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec(); mbox.exec();
return false; return false;
@ -1739,22 +1740,17 @@ bool FriendList::importExportFriendlistFileDialog(QString &fileName, bool import
*/ */
bool FriendList::exportFriendlist(QString &fileName) bool FriendList::exportFriendlist(QString &fileName)
{ {
// this has to be IniFormat because it is platform idependet (qsettings documentation) QDomDocument doc("FriendListWithGroups");
QSettings s(fileName, QSettings::IniFormat); QDomElement root = doc.createElement("root");
s.clear(); doc.appendChild(root);
if(!s.isWritable()) {
QFile file(fileName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
// show error to user // show error to user
QMessageBox mbox; QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning); mbox.setIcon(QMessageBox::Warning);
mbox.setText(tr("Error")); mbox.setText(tr("Error"));
mbox.setInformativeText("File is not writeable!\n" + fileName + "\nStatus: " + mbox.setInformativeText(tr("File is not writeable!\n") + fileName);
(
(s.status() == QSettings::NoError) ? "no error" :
(
(s.status() == QSettings::AccessError) ? "access error" : "format error"
)
)
);
mbox.setStandardButtons(QMessageBox::Ok); mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec(); mbox.exec();
return false; return false;
@ -1766,18 +1762,15 @@ bool FriendList::exportFriendlist(QString &fileName)
std::list<RsGroupInfo> group_info_list; std::list<RsGroupInfo> group_info_list;
rsPeers->getGroupInfoList(group_info_list); rsPeers->getGroupInfoList(group_info_list);
s.beginGroup("pgpIDs"); QDomElement pgpIDs = doc.createElement("pgpIDs");
RsPeerDetails details; RsPeerDetails details;
for(std::list<RsPgpId>::iterator list_iter = gpg_ids.begin(); list_iter != gpg_ids.end(); list_iter++) { for(std::list<RsPgpId>::iterator list_iter = gpg_ids.begin(); list_iter != gpg_ids.end(); list_iter++) {
rsPeers->getGPGDetails(*list_iter, details); rsPeers->getGPGDetails(*list_iter, details);
s.beginGroup(details.gpg_id.toStdString().c_str()); QDomElement pgpID = doc.createElement("pgpID");
// since everything is loaded from the public key there is no need to save these // these values aren't used and just stored for better human readability
//s.setValue("pgpID", details.gpg_id.toStdString().c_str()); pgpID.setAttribute("id", QString::fromStdString(details.gpg_id.toStdString()));
s.setValue("name", details.name.c_str()); // storing name for better human readability pgpID.setAttribute("name", QString::fromUtf8(details.name.c_str()));
//s.setValue("email", details.email.c_str());
//s.setValue("trustLvl", details.trustLvl);
s.beginGroup("sslIDs");
std::list<RsPeerId> ssl_ids; std::list<RsPeerId> ssl_ids;
rsPeers->getAssociatedSSLIds(*list_iter, ssl_ids); rsPeers->getAssociatedSSLIds(*list_iter, ssl_ids);
for(std::list<RsPeerId>::iterator list_iter = ssl_ids.begin(); list_iter != ssl_ids.end(); list_iter++) { for(std::list<RsPeerId>::iterator list_iter = ssl_ids.begin(); list_iter != ssl_ids.end(); list_iter++) {
@ -1786,56 +1779,107 @@ bool FriendList::exportFriendlist(QString &fileName)
continue; continue;
std::string invite = rsPeers->GetRetroshareInvite(detail2.id, true); std::string invite = rsPeers->GetRetroshareInvite(detail2.id, true);
std::string sid = detail2.id.toStdString();
s.beginGroup(sid.c_str()); QDomElement sslID = doc.createElement("sslID");
// since everything is loaded from the public key there is no need to save these // these values aren't used and just stored for better human readability
//s.setValue("sslID", sid.c_str()); sslID.setAttribute("sslID", QString::fromStdString(detail2.id.toStdString()));
//s.setValue("location", detail2.location.c_str()); sslID.setAttribute("location", QString::fromUtf8(detail2.location.c_str()));
s.setValue("pubkey", invite.c_str());
s.setValue("service_perm_flags", detail2.service_perm_flags.toUInt32()); // required values
s.endGroup(); // sid sslID.setAttribute("pubkey", QString::fromStdString(invite));
sslID.setAttribute("service_perm_flags", detail2.service_perm_flags.toUInt32());
pgpID.appendChild(sslID);
} }
s.endGroup(); // sslIDs pgpIDs.appendChild(pgpID);
s.endGroup(); // details.gpg_id
} }
s.endGroup(); // pgpIDs root.appendChild(pgpIDs);
s.beginGroup("groups"); QDomElement groups = doc.createElement("groups");
for(std::list<RsGroupInfo>::iterator list_iter = group_info_list.begin(); list_iter != group_info_list.end(); list_iter++) { for(std::list<RsGroupInfo>::iterator list_iter = group_info_list.begin(); list_iter != group_info_list.end(); list_iter++) {
RsGroupInfo group_info = *list_iter; RsGroupInfo group_info = *list_iter;
s.beginGroup(group_info.id.c_str());
//skip groups without peers
if(group_info.peerIds.empty())
continue;
QDomElement group = doc.createElement("group");
// id is not needed since it may differ between locatiosn / pgp ids (groups are identified by name) // id is not needed since it may differ between locatiosn / pgp ids (groups are identified by name)
s.setValue("name", group_info.name.c_str()); group.setAttribute("name", QString::fromStdString(group_info.name));
s.setValue("flag", group_info.flag); group.setAttribute("flag", group_info.flag);
s.beginGroup("peerIDs");
for(std::set<RsPgpId>::iterator i = group_info.peerIds.begin(); i != group_info.peerIds.end(); i++) { for(std::set<RsPgpId>::iterator i = group_info.peerIds.begin(); i != group_info.peerIds.end(); i++) {
QDomElement pgpID = doc.createElement("pgpID");
std::string pid = i->toStdString(); std::string pid = i->toStdString();
// key = peerId, value = arbitrary pgpID.setAttribute("id", QString::fromStdString(pid));
s.setValue(pid.c_str(), 0); group.appendChild(pgpID);
} }
s.endGroup(); // peerIDs groups.appendChild(group);
s.endGroup(); // group_info.id
} }
s.endGroup(); // groups root.appendChild(groups);
QTextStream ts(&file);
ts.setCodec("UTF-8");
ts << doc.toString();
file.close();
return true; return true;
} }
/**
* @brief helper function to show a message box
*/
void showXMLParsingError()
{
// show error to user
QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning);
mbox.setText(QObject::tr("Error"));
mbox.setInformativeText(QObject::tr("unable to parse XML file!"));
mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec();
}
/** /**
* @brief Imports friends from a given file * @brief Imports friends from a given file
* @param fileName file to load friends from * @param fileName file to load friends from
* @param errorPeers an error occured while adding a peer * @param errorPeers an error occured while adding a peer
* @param errorGroups an error occured while adding a peer to a group * @param errorGroups an error occured while adding a peer to a group
* @return success or failure (an error means that adding a peer and/or adding a peer to a group failed at least once) * @return success or failure (an error can also happen when adding a peer and/or adding a peer to a group fails at least once)
*/ */
bool FriendList::importFriendlist(QString &fileName, bool &errorPeers, bool &errorGroups) bool FriendList::importFriendlist(QString &fileName, bool &errorPeers, bool &errorGroups)
{ {
// this has to be IniFormat because it is platform idependet (qsettings documentation) QDomDocument doc;
QSettings s(fileName, QSettings::IniFormat); // load from file
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
// show error to user
QMessageBox mbox;
mbox.setIcon(QMessageBox::Warning);
mbox.setText(tr("Error"));
mbox.setInformativeText(tr("File is not readable!\n") + fileName);
mbox.setStandardButtons(QMessageBox::Ok);
mbox.exec();
return false;
}
bool ok = doc.setContent(&file);
file.close();
if(!ok) {
showXMLParsingError();
return false;
}
}
QDomElement root = doc.documentElement();
if(root.tagName() != "root") {
showXMLParsingError();
return false;
}
errorPeers = false; errorPeers = false;
errorGroups = false; errorGroups = false;
@ -1848,25 +1892,33 @@ bool FriendList::importFriendlist(QString &fileName, bool &errorPeers, bool &err
// lock all events for faster processing // lock all events for faster processing
RsAutoUpdatePage::lockAllEvents(); RsAutoUpdatePage::lockAllEvents();
s.beginGroup("pgpIDs"); // pgp and ssl IDs
QStringList pgpIDs = s.childGroups(); QDomElement pgpIDs;
foreach (QString pgpID, pgpIDs) { {
s.beginGroup(pgpID); QDomNodeList nodes = root.elementsByTagName("pgpIDs");
if(nodes.isEmpty() || nodes.size() != 1){
// enter sslIDs group and iterate over all ssl IDs showXMLParsingError();
s.beginGroup("sslIDs"); return false;
QStringList sslIDs = s.childGroups(); }
foreach (QString sslID, sslIDs) {
s.beginGroup(sslID);
pgpIDs = nodes.item(0).toElement();
if(pgpIDs.isNull()){
showXMLParsingError();
return false;
}
}
QDomNode pgpIDElem = pgpIDs.firstChildElement("pgpID");
while (!pgpIDElem.isNull()) {
QDomElement sslIDElem = pgpIDElem.firstChildElement("sslID");
while (!sslIDElem.isNull()) {
rsPeerID.clear(); rsPeerID.clear();
rsPgpID.clear(); rsPgpID.clear();
// load everything needed from the pubkey string // load everything needed from the pubkey string
std::string pubkey = s.value("pubkey").toString().toStdString(); std::string pubkey = sslIDElem.attribute("pubkey").toStdString();
if(rsPeers->loadDetailsFromStringCert(pubkey, rsPeerDetails, error_code)) { if(rsPeers->loadDetailsFromStringCert(pubkey, rsPeerDetails, error_code)) {
if(rsPeers->loadCertificateFromString(pubkey, rsPeerID, rsPgpID, error_string)) { if(rsPeers->loadCertificateFromString(pubkey, rsPeerID, rsPgpID, error_string)) {
ServicePermissionFlags service_perm_flags(s.value("service_perm_flags").toInt()); ServicePermissionFlags service_perm_flags(sslIDElem.attribute("service_perm_flags").toInt());
// everything is loaded - start setting things // everything is loaded - start setting things
if (!rsPeerDetails.id.isNull() && !rsPeerDetails.gpg_id.isNull()) { if (!rsPeerDetails.id.isNull() && !rsPeerDetails.gpg_id.isNull()) {
@ -1893,54 +1945,62 @@ bool FriendList::importFriendlist(QString &fileName, bool &errorPeers, bool &err
rsPeers->addFriend(pid, rsPeerDetails.gpg_id, service_perm_flags); rsPeers->addFriend(pid, rsPeerDetails.gpg_id, service_perm_flags);
} else { } else {
errorPeers = true; errorPeers = true;
std::cerr << "FriendList::importFriendlist(): error while procvessing SSL id " << sslID.toStdString() << std::endl; std::cerr << "FriendList::importFriendlist(): error while processing SSL id: " << sslIDElem.attribute("sslID", "invalid").toStdString() << std::endl;
} }
} else { } else {
errorPeers = true; errorPeers = true;
std::cerr << "FriendList::importFriendlist(): failed to get peer detaisl from public key (SSL id: " << sslID.toStdString() << " - error: " << error_string << ")" << std::endl; std::cerr << "FriendList::importFriendlist(): failed to get peer detaisl from public key (SSL id: " << sslIDElem.attribute("sslID", "invalid").toStdString() << " - error: " << error_string << ")" << std::endl;
} }
} else { } else {
errorPeers = true; errorPeers = true;
std::cerr << "FriendList::importFriendlist(): failed to get peer detaisl from public key (SSL id: " << sslID.toStdString() << " - error: " << error_code << ")" << std::endl; std::cerr << "FriendList::importFriendlist(): failed to get peer detaisl from public key (SSL id: " << sslIDElem.attribute("sslID", "invalid").toStdString() << " - error: " << error_code << ")" << std::endl;
} }
s.endGroup(); // sslID sslIDElem = sslIDElem.nextSiblingElement("sslID");
} }
s.endGroup(); // sslIDs pgpIDElem = pgpIDElem.nextSiblingElement("pgpID");
s.endGroup(); // pgpID
} }
s.endGroup(); // pgpIDs
// enter groups group and iterate over all groups // groups
s.beginGroup("groups"); QDomElement groups;
QStringList groupList = s.childGroups(); {
foreach (QString group, groupList) { QDomNodeList nodes = root.elementsByTagName("groups");
s.beginGroup(group); if(nodes.isEmpty() || nodes.size() != 1){
showXMLParsingError();
return false;
}
groups = nodes.item(0).toElement();
if(groups.isNull()){
showXMLParsingError();
return false;
}
}
QDomElement group = groups.firstChildElement("group");
while (!group.isNull()) {
// get name and flags and try to get the group ID // get name and flags and try to get the group ID
std::string groupName = s.value("name").toString().toStdString(); std::string groupName = group.attribute("name").toStdString();
uint32_t flag = s.value("flag").toUInt(); uint32_t flag = group.attribute("flag").toInt();
std::string groupId; std::string groupId;
if(getOrCreateGroup(groupName, flag, groupId)) { if(getOrCreateGroup(groupName, flag, groupId)) {
// group id found! // group id found!
// enter peerIDs group and iterate over all pgpIDs of this group QDomElement pgpID = group.firstChildElement("pgpID");
s.beginGroup("peerIDs"); while (!pgpID.isNull()) {
QStringList pgpIDsForGroup = s.allKeys();
foreach (QString pgpIDForGroup, pgpIDsForGroup) {
// add pgp id to group // add pgp id to group
RsPgpId rsPgpId(pgpIDForGroup.toStdString()); RsPgpId rsPgpId(pgpID.attribute("id").toStdString());
if(rsPeers->assignPeerToGroup(groupId, rsPgpId, true)) { if(rsPgpID.isNull() || !rsPeers->assignPeerToGroup(groupId, rsPgpId, true)) {
errorGroups = true; errorGroups = true;
std::cerr << "FriendList::importFriendlist(): failed to add '" << rsPeers->getGPGName(rsPgpId) << "'' to group '" << groupName << "'" << std::endl; std::cerr << "FriendList::importFriendlist(): failed to add '" << rsPeers->getGPGName(rsPgpId) << "'' to group '" << groupName << "'" << std::endl;
} }
pgpID = pgpID.nextSiblingElement("pgpID");
} }
s.endGroup(); // peerIDs pgpID = pgpID.nextSiblingElement("pgpID");
} else { } else {
errorGroups = true; errorGroups = true;
std::cerr << "FriendList::importFriendlist(): failed to find/create group '" << groupName << "'" << std::endl; std::cerr << "FriendList::importFriendlist(): failed to find/create group '" << groupName << "'" << std::endl;
} }
s.endGroup(); // group group = group.nextSiblingElement("group");
} }
s.endGroup(); // groups
// unlock events // unlock events
RsAutoUpdatePage::unlockAllEvents(); RsAutoUpdatePage::unlockAllEvents();