2010-09-28 16:33:34 -04:00
|
|
|
/****************************************************************
|
|
|
|
* This file is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010, RetroShare Team
|
|
|
|
*
|
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
****************************************************************/
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <retroshare/rspeers.h>
|
2013-08-30 16:40:28 -04:00
|
|
|
#include <retroshare/rsmsgs.h>
|
2014-03-29 10:18:05 -04:00
|
|
|
#include <retroshare/rsidentity.h>
|
2010-09-28 16:33:34 -04:00
|
|
|
|
|
|
|
#include "PeerDefs.h"
|
|
|
|
|
|
|
|
const QString PeerDefs::nameWithLocation(const RsPeerDetails &details)
|
|
|
|
{
|
2011-05-07 19:14:09 -04:00
|
|
|
QString name = QString::fromUtf8(details.name.c_str());
|
2010-09-28 16:33:34 -04:00
|
|
|
if (details.location.empty() == false) {
|
2011-05-07 19:14:09 -04:00
|
|
|
name += " (" + QString::fromUtf8(details.location.c_str()) + ")";
|
2010-09-28 16:33:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2014-05-08 03:52:28 -04:00
|
|
|
const QString PeerDefs::nameWithLocation(const RsIdentityDetails &details)
|
|
|
|
{
|
|
|
|
return QString::fromUtf8(details.mNickname.c_str()) + " (" + QString::fromStdString(details.mId.toStdString()) + ")";
|
|
|
|
}
|
2015-01-23 09:42:28 -05:00
|
|
|
|
|
|
|
const QString PeerDefs::nameWithId(const RsIdentityDetails &details)
|
|
|
|
{
|
|
|
|
return QString::fromUtf8(details.mNickname.c_str()) + " <" + QString::fromUtf8(details.mNickname.c_str()) + "@" + QString::fromStdString(details.mId.toStdString()) + ">";
|
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
const QString PeerDefs::rsid(const std::string &name, const RsPgpId &id)
|
|
|
|
{
|
|
|
|
if (name.empty()) {
|
|
|
|
return qApp->translate("PeerDefs", "Unknown") + "@" + QString::fromStdString(id.toStdString());
|
|
|
|
}
|
2010-09-28 16:33:34 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
return QString::fromUtf8(name.c_str()) + "@" + QString::fromStdString(id.toStdString());
|
|
|
|
}
|
2014-03-29 10:18:05 -04:00
|
|
|
const QString PeerDefs::rsid(const std::string &name, const RsGxsId &id)
|
|
|
|
{
|
|
|
|
if (name.empty()) {
|
|
|
|
return qApp->translate("PeerDefs", "Unknown") + "@" + QString::fromStdString(id.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return QString::fromUtf8(name.c_str()) + "@" + QString::fromStdString(id.toStdString());
|
|
|
|
}
|
2014-03-17 16:56:06 -04:00
|
|
|
const QString PeerDefs::rsid(const std::string &name, const RsPeerId &id)
|
2010-09-28 16:33:34 -04:00
|
|
|
{
|
|
|
|
if (name.empty()) {
|
2014-03-17 16:56:06 -04:00
|
|
|
return qApp->translate("PeerDefs", "Unknown") + "@" + QString::fromStdString(id.toStdString());
|
2010-09-28 16:33:34 -04:00
|
|
|
}
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
return QString::fromUtf8(name.c_str()) + "@" + QString::fromStdString(id.toStdString());
|
2010-09-28 16:33:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const QString PeerDefs::rsid(const RsPeerDetails &details)
|
|
|
|
{
|
|
|
|
return rsid(details.name, details.id);
|
|
|
|
}
|
2014-05-08 03:52:28 -04:00
|
|
|
const QString PeerDefs::rsidFromId(const RsGxsId &id, QString *name /* = NULL*/)
|
|
|
|
{
|
|
|
|
QString rsid;
|
|
|
|
|
2014-05-08 04:39:04 -04:00
|
|
|
// Check own GXS ids first, since they can be obtained faster.
|
2014-05-08 03:52:28 -04:00
|
|
|
//
|
|
|
|
std::list<RsGxsId> gxs_ids ;
|
|
|
|
rsIdentity->getOwnIds(gxs_ids) ;
|
|
|
|
|
|
|
|
for(std::list<RsGxsId>::const_iterator it(gxs_ids.begin());it!=gxs_ids.end();++it)
|
|
|
|
if(*it == id)
|
|
|
|
{
|
|
|
|
// not a real peer. Try from hash for distant messages
|
|
|
|
|
|
|
|
RsIdentityDetails details ;
|
|
|
|
if(!rsIdentity->getIdDetails(*it,details))
|
|
|
|
continue ;
|
|
|
|
|
|
|
|
std::string peerName = details.mNickname ;
|
|
|
|
|
|
|
|
rsid = PeerDefs::rsid(peerName, *it);
|
|
|
|
if(name)
|
|
|
|
*name = QString::fromUtf8(peerName.c_str());
|
|
|
|
|
|
|
|
return rsid ;
|
|
|
|
}
|
|
|
|
|
2014-05-08 04:39:04 -04:00
|
|
|
RsIdentityDetails details ;
|
|
|
|
|
|
|
|
if(rsIdentity->getIdDetails(id,details))
|
|
|
|
{
|
|
|
|
std::string peerName = details.mNickname ;
|
|
|
|
rsid = PeerDefs::rsid(peerName, id);
|
|
|
|
|
|
|
|
if(name)
|
|
|
|
*name = QString::fromUtf8(peerName.c_str());
|
|
|
|
|
|
|
|
return rsid ;
|
|
|
|
}
|
2014-05-08 03:52:28 -04:00
|
|
|
rsid = PeerDefs::rsid("", id);
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
*name = qApp->translate("PeerDefs", "Unknown");
|
|
|
|
|
|
|
|
return rsid;
|
|
|
|
}
|
2014-03-17 16:56:06 -04:00
|
|
|
const QString PeerDefs::rsidFromId(const RsPeerId &id, QString *name /* = NULL*/)
|
2010-09-28 16:33:34 -04:00
|
|
|
{
|
|
|
|
QString rsid;
|
|
|
|
|
|
|
|
std::string peerName = rsPeers->getPeerName(id);
|
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
if(!peerName.empty())
|
|
|
|
{
|
2010-09-28 16:33:34 -04:00
|
|
|
rsid = PeerDefs::rsid(peerName, id);
|
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
if (name)
|
2011-05-07 19:14:09 -04:00
|
|
|
*name = QString::fromUtf8(peerName.c_str());
|
2014-03-29 10:18:05 -04:00
|
|
|
|
|
|
|
return rsid ;
|
|
|
|
|
2010-09-28 16:33:34 -04:00
|
|
|
}
|
2013-08-30 16:40:28 -04:00
|
|
|
|
2014-03-29 10:18:05 -04:00
|
|
|
std::list<RsGxsId> gxs_ids ;
|
|
|
|
rsIdentity->getOwnIds(gxs_ids) ;
|
|
|
|
|
|
|
|
for(std::list<RsGxsId>::const_iterator it(gxs_ids.begin());it!=gxs_ids.end();++it)
|
|
|
|
if(RsPeerId(*it) == id)
|
|
|
|
{
|
|
|
|
// not a real peer. Try from hash for distant messages
|
|
|
|
|
|
|
|
RsIdentityDetails details ;
|
|
|
|
if(!rsIdentity->getIdDetails(*it,details))
|
|
|
|
continue ;
|
|
|
|
|
|
|
|
peerName = details.mNickname ;
|
|
|
|
|
|
|
|
rsid = PeerDefs::rsid(peerName, *it);
|
|
|
|
if(name)
|
|
|
|
*name = QString::fromUtf8(peerName.c_str());
|
2014-05-07 16:53:16 -04:00
|
|
|
|
|
|
|
return rsid ;
|
2014-03-29 10:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rsid = PeerDefs::rsid("", id);
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
*name = qApp->translate("PeerDefs", "Unknown");
|
2010-09-28 16:33:34 -04:00
|
|
|
|
|
|
|
return rsid;
|
|
|
|
}
|
2014-03-17 16:56:06 -04:00
|
|
|
const QString PeerDefs::rsidFromId(const RsPgpId &id, QString *name /* = NULL*/)
|
|
|
|
{
|
|
|
|
QString rsid;
|
|
|
|
|
|
|
|
std::string peerName = rsPeers->getGPGName(id);
|
|
|
|
|
|
|
|
if(!peerName.empty())
|
|
|
|
{
|
|
|
|
rsid = PeerDefs::rsid(peerName, id);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
*name = QString::fromUtf8(peerName.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rsid = PeerDefs::rsid("", id);
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
*name = qApp->translate("PeerDefs", "Unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsid;
|
|
|
|
}
|
2010-09-28 16:33:34 -04:00
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
RsPeerId PeerDefs::idFromRsid(const QString &rsid, bool check)
|
2010-09-28 16:33:34 -04:00
|
|
|
{
|
|
|
|
// search for cert id in string
|
2014-03-17 16:56:06 -04:00
|
|
|
RsPeerId id;
|
2010-09-28 16:33:34 -04:00
|
|
|
|
|
|
|
int index = rsid.indexOf("@");
|
|
|
|
if (index >= 0) {
|
|
|
|
// found "@", extract cert id from string
|
2014-03-17 16:56:06 -04:00
|
|
|
id = RsPeerId(rsid.mid(index + 1).toStdString());
|
2010-09-28 16:33:34 -04:00
|
|
|
} else {
|
|
|
|
// maybe its only the cert id
|
2014-03-17 16:56:06 -04:00
|
|
|
id = RsPeerId(rsid.toStdString());
|
2010-09-28 16:33:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (check) {
|
|
|
|
RsPeerDetails detail;
|
|
|
|
if (rsPeers->getPeerDetails(id, detail) == false) {
|
|
|
|
id.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|