2008-03-04 16:31:11 -05:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Robert Fernie
|
|
|
|
*
|
|
|
|
* 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 "NetworkView.h"
|
2010-08-06 05:40:23 -04:00
|
|
|
#include <retroshare/rspeers.h>
|
2010-11-05 18:46:40 -04:00
|
|
|
#include <retroshare/rsdisc.h>
|
2009-05-25 07:54:43 -04:00
|
|
|
|
2010-02-14 09:18:53 -05:00
|
|
|
#include <gpgme.h>
|
2009-05-23 11:13:01 -04:00
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
#include <deque>
|
|
|
|
#include <set>
|
2008-03-04 16:31:11 -05:00
|
|
|
#include <iostream>
|
2008-11-02 10:20:42 -05:00
|
|
|
#include <algorithm>
|
2008-03-04 16:31:11 -05:00
|
|
|
|
2009-05-12 19:44:51 -04:00
|
|
|
#include "gui/elastic/node.h"
|
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
/********
|
|
|
|
* #define DEBUG_NETWORKVIEW
|
|
|
|
********/
|
|
|
|
|
2008-03-04 16:31:11 -05:00
|
|
|
/** Constructor */
|
|
|
|
NetworkView::NetworkView(QWidget *parent)
|
2010-11-05 18:46:40 -04:00
|
|
|
: RsAutoUpdatePage(60000,parent)
|
2008-03-04 16:31:11 -05:00
|
|
|
{
|
|
|
|
/* Invoke the Qt Designer generated object setup routine */
|
|
|
|
ui.setupUi(this);
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
mScene = new QGraphicsScene();
|
2010-11-10 15:18:53 -05:00
|
|
|
mScene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
|
|
|
mScene->setSceneRect(-200, -200, 1200, 1200);
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
ui.graphicsView->setScene(mScene);
|
2010-11-01 10:17:18 -04:00
|
|
|
ui.graphicsView->setEdgeLength(ui.edgeLengthSB->value()) ;
|
|
|
|
|
|
|
|
setMaxFriendLevel(ui.maxFriendLevelSB->value()) ;
|
2008-03-04 16:31:11 -05:00
|
|
|
|
|
|
|
/* add button */
|
2010-11-14 16:03:36 -05:00
|
|
|
connect( ui.refreshButton, SIGNAL( clicked( void ) ), this, SLOT( redraw( void ) ) );
|
2010-10-22 16:47:08 -04:00
|
|
|
connect( mScene, SIGNAL( changed ( const QList<QRectF> & ) ), this, SLOT ( changedScene( void ) ) );
|
2008-03-21 19:31:00 -04:00
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
/* Hide Settings frame */
|
2010-11-01 10:17:18 -04:00
|
|
|
connect( ui.maxFriendLevelSB, SIGNAL(valueChanged(int)), this, SLOT(setMaxFriendLevel(int)));
|
|
|
|
connect( ui.edgeLengthSB, SIGNAL(valueChanged(int)), this, SLOT(setEdgeLength(int)));
|
2010-10-22 16:47:08 -04:00
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
_should_update = true ;
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
void NetworkView::setEdgeLength(int l)
|
|
|
|
{
|
|
|
|
ui.graphicsView->setEdgeLength(l);
|
|
|
|
}
|
|
|
|
void NetworkView::setMaxFriendLevel(int m)
|
|
|
|
{
|
2010-11-14 16:03:36 -05:00
|
|
|
ui.graphicsView->snapshotNodesPositions() ;
|
|
|
|
clear() ;
|
2010-11-01 10:17:18 -04:00
|
|
|
_max_friend_level = m ;
|
2010-11-05 18:46:40 -04:00
|
|
|
updateDisplay() ;
|
2010-11-01 10:17:18 -04:00
|
|
|
}
|
2008-03-04 16:31:11 -05:00
|
|
|
void NetworkView::changedFoFCheckBox( )
|
|
|
|
{
|
2010-11-05 18:46:40 -04:00
|
|
|
updateDisplay();
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
2010-11-14 16:03:36 -05:00
|
|
|
void NetworkView::redraw()
|
2008-03-04 16:31:11 -05:00
|
|
|
{
|
2010-11-14 16:03:36 -05:00
|
|
|
ui.graphicsView->clearNodesPositions() ;
|
|
|
|
clear() ;
|
|
|
|
updateDisplay() ;
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
void NetworkView::clear()
|
2008-03-04 16:31:11 -05:00
|
|
|
{
|
2010-11-14 16:03:36 -05:00
|
|
|
ui.graphicsView->clearGraph() ;
|
|
|
|
_node_ids.clear() ;
|
|
|
|
update() ;
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
class NodeInfo
|
|
|
|
{
|
|
|
|
public:
|
2010-11-14 16:03:36 -05:00
|
|
|
NodeInfo(const std::string& _gpg_id,uint32_t lev) : gpg_id(_gpg_id),friend_level(lev) {}
|
2010-11-01 10:17:18 -04:00
|
|
|
|
|
|
|
std::string gpg_id ;
|
|
|
|
uint32_t friend_level ;
|
|
|
|
} ;
|
2008-03-04 16:31:11 -05:00
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
void NetworkView::update()
|
2008-03-04 16:31:11 -05:00
|
|
|
{
|
2010-11-05 18:46:40 -04:00
|
|
|
_should_update = true ;
|
|
|
|
}
|
|
|
|
void NetworkView::updateDisplay()
|
|
|
|
{
|
|
|
|
if(!isVisible())
|
|
|
|
return ;
|
|
|
|
if(!_should_update)
|
|
|
|
return ;
|
2008-03-04 16:31:11 -05:00
|
|
|
|
|
|
|
/* add all friends */
|
2010-11-05 18:46:40 -04:00
|
|
|
std::string ownGPGId = rsPeers->getGPGOwnId();
|
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
|
|
|
std::cerr << "NetworkView::updateDisplay()" << std::endl;
|
|
|
|
#endif
|
2008-03-04 16:31:11 -05:00
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
int i = 0;
|
2010-10-22 16:47:08 -04:00
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
std::deque<NodeInfo> nodes_to_treat ; // list of nodes to be treated. Used as a queue. The int is the level of friendness
|
|
|
|
std::set<std::string> nodes_considered ; // list of nodes already considered. Eases lookup.
|
2010-10-22 16:47:08 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
nodes_to_treat.push_front(NodeInfo(ownGPGId,0)) ; // initialize queue with own id.
|
2010-11-05 18:46:40 -04:00
|
|
|
nodes_considered.insert(rsPeers->getOwnId()) ;
|
2009-05-23 11:13:01 -04:00
|
|
|
|
2010-11-01 10:17:18 -04:00
|
|
|
// Put own id in queue, and empty the queue, treating all nodes.
|
|
|
|
//
|
|
|
|
while(!nodes_to_treat.empty())
|
|
|
|
{
|
|
|
|
NodeInfo info(nodes_to_treat.back()) ;
|
|
|
|
nodes_to_treat.pop_back() ;
|
2010-11-05 18:46:40 -04:00
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
2010-11-14 16:03:36 -05:00
|
|
|
std::cerr << " Poped out of queue: " << info.gpg_id << ", with level " << info.friend_level << std::endl ;
|
2010-11-05 18:46:40 -04:00
|
|
|
#endif
|
2010-11-14 16:03:36 -05:00
|
|
|
GraphWidget::NodeType type ;
|
|
|
|
GraphWidget::AuthType auth ;
|
2010-11-01 10:17:18 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
switch(info.friend_level)
|
2008-03-04 16:31:11 -05:00
|
|
|
{
|
2010-11-14 16:03:36 -05:00
|
|
|
case 0: type = GraphWidget::ELASTIC_NODE_TYPE_OWN ;
|
|
|
|
break ;
|
|
|
|
case 1: type = GraphWidget::ELASTIC_NODE_TYPE_FRIEND ;
|
|
|
|
break ;
|
|
|
|
case 2: type = GraphWidget::ELASTIC_NODE_TYPE_F_OF_F ;
|
|
|
|
break ;
|
|
|
|
default:
|
|
|
|
type = GraphWidget::ELASTIC_NODE_TYPE_UNKNOWN ;
|
|
|
|
}
|
2010-11-01 10:17:18 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
RsPeerDetails detail ;
|
|
|
|
if(!rsPeers->getPeerDetails(info.gpg_id, detail))
|
|
|
|
continue ;
|
2010-11-01 10:17:18 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
switch(detail.validLvl)
|
|
|
|
{
|
|
|
|
case GPGME_VALIDITY_MARGINAL: auth = GraphWidget::ELASTIC_NODE_AUTH_MARGINAL ; break;
|
|
|
|
case GPGME_VALIDITY_FULL:
|
|
|
|
case GPGME_VALIDITY_ULTIMATE: auth = GraphWidget::ELASTIC_NODE_AUTH_FULL ; break;
|
|
|
|
case GPGME_VALIDITY_UNKNOWN:
|
|
|
|
case GPGME_VALIDITY_UNDEFINED:
|
|
|
|
case GPGME_VALIDITY_NEVER:
|
|
|
|
default: auth = GraphWidget::ELASTIC_NODE_AUTH_UNKNOWN ; break ;
|
|
|
|
}
|
2010-11-05 18:46:40 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
if(info.friend_level <= _max_friend_level && _node_ids.find(info.gpg_id) == _node_ids.end())
|
|
|
|
{
|
|
|
|
_node_ids[info.gpg_id] = ui.graphicsView->addNode(" "+detail.name, detail.name+"@"+detail.gpg_id,type,auth,"",info.gpg_id);
|
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
|
|
|
std::cerr << " inserted node " << info.gpg_id << ", type=" << type << ", auth=" << auth << std::endl ;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<std::string> friendList;
|
|
|
|
rsDisc->getDiscGPGFriends(info.gpg_id, friendList);
|
2010-11-01 10:17:18 -04:00
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
2010-11-14 16:03:36 -05:00
|
|
|
std::cerr << " Got a list of " << friendList.size() << " friends for this peer." << std::endl ;
|
2010-11-05 18:46:40 -04:00
|
|
|
#endif
|
2009-05-25 07:54:43 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
if(info.friend_level+1 <= _max_friend_level)
|
2010-11-05 18:46:40 -04:00
|
|
|
for(std::list<std::string>::const_iterator sit(friendList.begin()); sit != friendList.end(); ++sit)
|
2010-11-01 11:15:56 -04:00
|
|
|
if(nodes_considered.find(*sit) == nodes_considered.end())
|
|
|
|
{
|
2010-11-05 18:46:40 -04:00
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
2010-11-01 11:15:56 -04:00
|
|
|
std::cerr << " adding to queue: " << *sit << ", with level " << info.friend_level+1 << std::endl ;
|
2010-11-05 18:46:40 -04:00
|
|
|
#endif
|
2010-11-14 16:03:36 -05:00
|
|
|
nodes_to_treat.push_front( NodeInfo(*sit,info.friend_level + 1) ) ;
|
2010-11-10 15:18:53 -05:00
|
|
|
nodes_considered.insert(*sit) ;
|
|
|
|
}
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
|
|
|
|
2010-10-22 16:47:08 -04:00
|
|
|
/* iterate through all friends */
|
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
2010-10-22 16:47:08 -04:00
|
|
|
std::cerr << "NetworkView::insertSignatures()" << std::endl;
|
2010-11-05 18:46:40 -04:00
|
|
|
#endif
|
2010-10-22 16:47:08 -04:00
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
for(std::map<std::string,GraphWidget::NodeId>::const_iterator it(_node_ids.begin()); it != _node_ids.end(); it++)
|
2008-03-04 16:31:11 -05:00
|
|
|
{
|
2010-11-05 18:46:40 -04:00
|
|
|
std::list<std::string> friendList ;
|
2008-03-20 19:49:12 -04:00
|
|
|
|
2010-11-14 16:03:36 -05:00
|
|
|
if(rsDisc->getDiscGPGFriends(it->first,friendList))
|
2010-11-05 18:46:40 -04:00
|
|
|
for(std::list<std::string>::const_iterator sit(friendList.begin()); sit != friendList.end(); sit++)
|
2008-03-20 19:49:12 -04:00
|
|
|
{
|
2010-11-05 18:46:40 -04:00
|
|
|
#ifdef DEBUG_NETWORKVIEW
|
2010-11-14 16:03:36 -05:00
|
|
|
std::cerr << "NetworkView: Adding Edge: ";
|
2010-11-05 18:46:40 -04:00
|
|
|
std::cerr << *sit << " <-> " << it->first;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2008-03-04 16:31:11 -05:00
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
if(_node_ids.find(*sit) != _node_ids.end())
|
|
|
|
ui.graphicsView->addEdge(_node_ids[*sit], it->second);
|
2008-03-20 19:49:12 -04:00
|
|
|
}
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
|
|
|
|
2010-11-05 18:46:40 -04:00
|
|
|
_should_update = false ;
|
2008-03-04 16:31:11 -05:00
|
|
|
}
|
|
|
|
|