Пример #1
0
/* get the list of Neighbours from the RsIface.  */
void NetworkDialog::insertConnect()
{
//	static time_t last_time = 0 ;

	if (!rsPeers)
		return;

//	// Because this is called from a qt signal, there's no limitation between calls.
	time_t now = time(NULL);
//	if(last_time + 5 > now)		// never update more often then every 5 seconds.
//		return ;
//
//	last_time = now ;

    std::list<RsPgpId> neighs; //these are GPG ids
    std::list<RsPgpId>::iterator it;
	rsPeers->getGPGAllList(neighs);

	/* get a link to the table */
	QTreeWidget *connectWidget = ui.connectTreeWidget;
	/* disable sorting while editing the table */
	connectWidget->setSortingEnabled(false);

	//remove items
	int index = 0;
	while (index < connectWidget->topLevelItemCount()) 
	{
        RsPgpId gpg_widget_id( (connectWidget->topLevelItem(index))->text(COLUMN_PEERID).toStdString() );
		RsPeerDetails detail;
		if ( (!rsPeers->getGPGDetails(gpg_widget_id, detail)) || (ui.onlyTrustedKeys->isChecked() && (detail.validLvl < RS_TRUST_LVL_MARGINAL && !detail.accept_connection))) 
			delete (connectWidget->takeTopLevelItem(index));
		else 
			++index;
	}
    //QList<QTreeWidgetItem *> validItems;
    //QList<QTreeWidgetItem *> unvalidItems;

	for(it = neighs.begin(); it != neighs.end(); ++it)
	{
#ifdef NET_DEBUG
		std::cerr << "NetworkDialog::insertConnect() inserting gpg key : " << *it << std::endl;
#endif
		if (*it == rsPeers->getGPGOwnId()) {
			continue;
		}

		RsPeerDetails detail;
		if (!rsPeers->getGPGDetails(*it, detail))
		{
			continue; /* BAD */
		}

		/* make a widget per friend */
		QTreeWidgetItem *item;
        QList<QTreeWidgetItem *> list = connectWidget->findItems(QString::fromStdString( (*it).toStdString() ), Qt::MatchExactly, COLUMN_PEERID);
		if (list.size() == 1) {
			item = list.front();
		} else {
				//create new item
#ifdef NET_DEBUG
				std::cerr << "NetworkDialog::insertConnect() creating new tree widget item : " << *it << std::endl;
#endif
				item = new RSTreeWidgetItem(NULL, 0);
				item -> setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);

				int S = QFontMetricsF(font()).height() ;
                item -> setSizeHint(COLUMN_CHECK, QSize( S,S ) );

				/* (1) Person */
				item -> setText(COLUMN_PEERNAME, QString::fromUtf8(detail.name.c_str()));

				/* (4) key id */
				item -> setText(COLUMN_PEERID, QString::fromStdString(detail.gpg_id.toStdString()));
		}

        //QString TrustLevelString ;

		/* (2) Key validity */
		if (detail.ownsign) 
		{
            item -> setText(COLUMN_I_AUTH_PEER, tr("Personal signature"));
            item -> setToolTip(COLUMN_I_AUTH_PEER, tr("PGP key signed by you"));
		} 
		else 
			switch(detail.trustLvl)
			{
                case RS_TRUST_LVL_MARGINAL: item->setText(COLUMN_I_AUTH_PEER,tr("Marginally trusted peer")) ; break;
				case RS_TRUST_LVL_FULL:
                case RS_TRUST_LVL_ULTIMATE: item->setText(COLUMN_I_AUTH_PEER,tr("Fully trusted peer")) ; break ;
				case RS_TRUST_LVL_UNKNOWN:
				case RS_TRUST_LVL_UNDEFINED: 
				case RS_TRUST_LVL_NEVER:
				default: 							item->setText(2,tr("Untrusted peer")) ; break ;
			}

		/* (3) has me auth */
		QString PeerAuthenticationString ;

		if (detail.hasSignedMe)
			PeerAuthenticationString = tr("Has authenticated me");
		else
			PeerAuthenticationString = tr("Unknown");

        item->setText(COLUMN_PEER_AUTH_ME,PeerAuthenticationString) ;

		uint64_t last_time_used = now - detail.lastUsed ;
		QString lst_used_str ;
		
		if(last_time_used < 3600)
			lst_used_str = tr("Last hour") ;
		else if(last_time_used < 86400)
			lst_used_str = tr("Today") ;
		else if(last_time_used > 86400 * 15000)
			lst_used_str = tr("Never");
		else
			lst_used_str = tr("%1 days ago").arg((int)( last_time_used / 86400 )) ;

		item->setText(COLUMN_LAST_USED,lst_used_str) ;

		/**
		 * Determinated the Background Color
		 */
		QColor backgrndcolor;

		if (detail.accept_connection)
		{
            item -> setText(COLUMN_CHECK, "0");
            item -> setIcon(COLUMN_CHECK,(QIcon(IMAGE_AUTHED)));
			if (detail.ownsign) 
			{
				backgrndcolor = backgroundColorOwnSign();
			} 
			else 
			{
				backgrndcolor = backgroundColorAcceptConnection();
			}
		} 
		else 
		{
            item -> setText(COLUMN_CHECK, "1");

			if (detail.hasSignedMe)
			{
				backgrndcolor = backgroundColorHasSignedMe();
                item -> setIcon(COLUMN_CHECK,(QIcon(IMAGE_DENIED)));
                for(int k=0;k<COLUMN_COUNT;++k)
					item -> setToolTip(k, QString::fromUtf8(detail.name.c_str()) + tr(" has authenticated you. \nRight-click and select 'make friend' to be able to connect."));
			}
			else
			{
				backgrndcolor = backgroundColorDenied();
                item -> setIcon(COLUMN_CHECK,(QIcon(IMAGE_DENIED)));
			}
		}

		// Color each Background column in the Network Tab except the first one => 1-9
		// whith the determinated color
        for(int i = 0; i <COLUMN_COUNT; ++i)
			item -> setBackground(i,QBrush(backgrndcolor));

		if( (detail.accept_connection || detail.validLvl >= RS_TRUST_LVL_MARGINAL) || !ui.onlyTrustedKeys->isChecked()) 
			connectWidget->addTopLevelItem(item);
	}

	// add self to network.
	RsPeerDetails ownGPGDetails;
	rsPeers->getGPGDetails(rsPeers->getGPGOwnId(), ownGPGDetails);
	/* make a widget per friend */
	QTreeWidgetItem *self_item;
    QList<QTreeWidgetItem *> list = connectWidget->findItems(QString::fromStdString(ownGPGDetails.gpg_id.toStdString()), Qt::MatchExactly, COLUMN_PEERID);
	if (list.size() == 1) {
		self_item = list.front();
	} else {
		self_item = new RSTreeWidgetItem(NULL, 0);
		self_item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
	}
    self_item -> setText(COLUMN_CHECK, "0");
    self_item->setIcon(COLUMN_CHECK,(QIcon(IMAGE_AUTHED)));
	self_item->setText(COLUMN_PEERNAME, QString::fromUtf8(ownGPGDetails.name.c_str()) + " (" + tr("yourself") + ")");
    self_item->setText(COLUMN_I_AUTH_PEER,"N/A");
	self_item->setText(COLUMN_PEERID, QString::fromStdString(ownGPGDetails.gpg_id.toStdString()));

	// Color each Background column in the Network Tab except the first one => 1-9
    for(int i=0;i<COLUMN_COUNT;++i)
	{
		self_item->setBackground(i,backgroundColorSelf()) ;
	}
	connectWidget->addTopLevelItem(self_item);

	/* enable sorting */
	connectWidget->setSortingEnabled(true);
	/* update display */
	connectWidget->update();

	if (ui.filterLineEdit->text().isEmpty() == false) {
		filterItems(ui.filterLineEdit->text());
	}

}
Пример #2
0
/* get the list of Neighbours from the RsIface.  */
void NetworkDialog::insertConnect()
{
	static time_t last_time = 0 ;

	if (!rsPeers)
		return;

	if (ui.showUnvalidKeys->isChecked()) {
		ui.unvalidGPGkeyWidget->show();
	} else {
		ui.unvalidGPGkeyWidget->hide();
	}

	// Because this is called from a qt signal, there's no limitation between calls.
	time_t now = time(NULL);
	if(last_time + 5 > now)		// never update more often then every 5 seconds.
		return ;

	last_time = now ;

	std::list<std::string> neighs; //these are GPG ids
	std::list<std::string>::iterator it;
	rsPeers->getGPGAllList(neighs);

	/* get a link to the table */
	QTreeWidget *connectWidget = ui.connecttreeWidget;

	//remove items
	int index = 0;
	while (index < connectWidget->topLevelItemCount()) {
		std::string gpg_widget_id = (connectWidget->topLevelItem(index))->text(COLUMN_PEERID).toStdString();
		RsPeerDetails detail;
		if (!rsPeers->getGPGDetails(gpg_widget_id, detail) || (detail.validLvl < RS_TRUST_LVL_MARGINAL && !detail.accept_connection)) {
			delete (connectWidget->takeTopLevelItem(index));
		} else {
			index++;
		}
	}
	index = 0;
	while (index < ui.unvalidGPGkeyWidget->topLevelItemCount()) {
		std::string gpg_widget_id = (ui.unvalidGPGkeyWidget->topLevelItem(index))->text(COLUMN_PEERID).toStdString();
		RsPeerDetails detail;
		if (!rsPeers->getGPGDetails(gpg_widget_id, detail) || detail.validLvl >= RS_TRUST_LVL_MARGINAL || detail.accept_connection) {
			delete (ui.unvalidGPGkeyWidget->takeTopLevelItem(index));
		} else {
			index++;
		}
	}

	QList<QTreeWidgetItem *> validItems;
	QList<QTreeWidgetItem *> unvalidItems;
	for(it = neighs.begin(); it != neighs.end(); it++)
	{
#ifdef NET_DEBUG
		std::cerr << "NetworkDialog::insertConnect() inserting gpg key : " << *it << std::endl;
#endif
		if (*it == rsPeers->getGPGOwnId()) {
			continue;
		}

		RsPeerDetails detail;
		if (!rsPeers->getGPGDetails(*it, detail))
		{
			continue; /* BAD */
		}

		/* make a widget per friend */
		QTreeWidgetItem *item;
		QList<QTreeWidgetItem *> list = connectWidget->findItems(QString::fromStdString(*it), Qt::MatchExactly, 4);
		if (list.size() == 1) {
			item = list.front();
		} else {
			list = ui.unvalidGPGkeyWidget->findItems(QString::fromStdString(*it), Qt::MatchExactly, 4);
			if (list.size() == 1) {
				item = list.front();
			} else {
				//create new item
#ifdef NET_DEBUG
				std::cerr << "NetworkDialog::insertConnect() creating new tree widget item : " << *it << std::endl;
#endif
				item = new RSTreeWidgetItem(NULL, 0);
				item -> setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
				item -> setSizeHint(0, QSize( 18,18 ) );

				/* (1) Person */
				item -> setText(COLUMN_PEERNAME, QString::fromUtf8(detail.name.c_str()));

				/* (4) key id */
				item -> setText(COLUMN_PEERID, QString::fromStdString(detail.id));
			}
		}

		QString TrustLevelString ;

		/* (2) Key validity */
		if (detail.ownsign) 
		{
			item -> setText(2, tr("Personal signature"));
			item -> setToolTip(2, tr("PGP key signed by you"));
		} 
		else 
			switch(detail.trustLvl)
			{
				case RS_TRUST_LVL_MARGINAL: item->setText(2,tr("Marginally trusted peer")) ; break;
				case RS_TRUST_LVL_FULL:
				case RS_TRUST_LVL_ULTIMATE: item->setText(2,tr("Fully trusted peer")) ; break ;
				case RS_TRUST_LVL_UNKNOWN:
				case RS_TRUST_LVL_UNDEFINED: 
				case RS_TRUST_LVL_NEVER:
				default: 							item->setText(2,tr("Untrusted peer")) ; break ;
			}

		QString PeerAuthenticationString = tr("Unknown") ;
		/* (3) has me auth */
		if (detail.hasSignedMe)
			PeerAuthenticationString = tr("Has authenticated me");

		item->setText(3,PeerAuthenticationString) ;

		/**
		 * Determinated the Background Color
		 */
		QColor backgrndcolor;

		if (detail.accept_connection)
		{
			if (detail.ownsign) 
			{
				item -> setText(0, "0");
				item -> setIcon(0,(QIcon(IMAGE_AUTHED)));
				backgrndcolor = backgroundColorOwnSign();
			} 
			else 
			{
				item -> setText(0, "0");
				item -> setIcon(0,(QIcon(IMAGE_AUTHED)));
				backgrndcolor = backgroundColorAcceptConnection();
			}
		} 
		else 
		{
			item -> setText(0, "1");

			if (detail.hasSignedMe)
			{
				backgrndcolor = backgroundColorHasSignedMe();
				item -> setIcon(0,(QIcon(IMAGE_DENIED)));
				for(int k=0;k<8;++k)
					item -> setToolTip(k, QString::fromUtf8(detail.name.c_str()) + tr(" has authenticated you. \nRight-click and select 'make friend' to be able to connect."));
			}
			else
			{
				backgrndcolor = backgroundColorDenied();
				item -> setIcon(0,(QIcon(IMAGE_DENIED)));
			}
		}

		// Color each Background column in the Network Tab except the first one => 1-9
		// whith the determinated color
		for(int i = 0; i <10; i++)
			item -> setBackground(i,QBrush(backgrndcolor));

		/* add to the list */
		if (detail.accept_connection || detail.validLvl >= RS_TRUST_LVL_MARGINAL) 
		{
			/* add gpg item to the list. If item is already in the list, it won't be duplicated thanks to Qt */
			connectWidget->addTopLevelItem(item);
		} 
		else 
		{
			ui.unvalidGPGkeyWidget->addTopLevelItem(item);
		}

	}

	// add self to network.
	RsPeerDetails ownGPGDetails;
	rsPeers->getGPGDetails(rsPeers->getGPGOwnId(), ownGPGDetails);
	/* make a widget per friend */
	QTreeWidgetItem *self_item;
	QList<QTreeWidgetItem *> list = connectWidget->findItems(QString::fromStdString(ownGPGDetails.gpg_id), Qt::MatchExactly, 4);
	if (list.size() == 1) {
		self_item = list.front();
	} else {
		self_item = new RSTreeWidgetItem(NULL, 0);
		self_item->setChildIndicatorPolicy(QTreeWidgetItem::DontShowIndicatorWhenChildless);
	}
	self_item -> setText(0, "0");
	self_item->setIcon(0,(QIcon(IMAGE_AUTHED)));
	self_item->setText(COLUMN_PEERNAME, QString::fromUtf8(ownGPGDetails.name.c_str()) + " (" + tr("yourself") + ")");
	self_item->setText(2,"N/A");
	self_item->setText(COLUMN_PEERID, QString::fromStdString(ownGPGDetails.id));

	// Color each Background column in the Network Tab except the first one => 1-9
	for(int i=0;i<10;++i)
	{
		self_item->setBackground(i,backgroundColorSelf()) ;
	}
	connectWidget->addTopLevelItem(self_item);

	connectWidget->update(); /* update display */
	ui.unvalidGPGkeyWidget->update(); /* update display */

	if (ui.filterLineEdit->text().isEmpty() == false) {
		filterItems(ui.filterLineEdit->text());
	}

}