2013-03-15 17:02:43 -04:00
|
|
|
/*
|
|
|
|
* Retroshare Gxs Support
|
2012-11-19 17:14:45 -05:00
|
|
|
*
|
2013-03-15 17:02:43 -04:00
|
|
|
* Copyright 2012-2013 by Robert Fernie.
|
2012-11-19 17:14:45 -05:00
|
|
|
*
|
2013-03-15 17:02:43 -04:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2.1 as published by the Free Software Foundation.
|
2012-11-19 17:14:45 -05:00
|
|
|
*
|
2013-03-15 17:02:43 -04:00
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
2012-11-19 17:14:45 -05:00
|
|
|
*
|
2013-03-15 17:02:43 -04:00
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
2012-11-19 17:14:45 -05:00
|
|
|
|
|
|
|
#include "GxsIdChooser.h"
|
2014-02-02 06:25:11 -05:00
|
|
|
#include "GxsIdDetails.h"
|
2014-07-06 07:19:58 -04:00
|
|
|
#include "RsGxsUpdateBroadcastBase.h"
|
2014-12-24 09:43:06 -05:00
|
|
|
#include "gui/Identity/IdEditDialog.h"
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2013-06-23 14:55:30 -04:00
|
|
|
#include <QSortFilterProxyModel>
|
2012-11-19 17:14:45 -05:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <retroshare/rspeers.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2013-06-23 14:55:30 -04:00
|
|
|
#define ROLE_SORT Qt::UserRole + 1 // Qt::UserRole is reserved for data
|
2014-07-06 07:19:58 -04:00
|
|
|
#define ROLE_TYPE Qt::UserRole + 2 //
|
|
|
|
|
2014-12-23 14:07:57 -05:00
|
|
|
/* Used for sorting too */
|
2014-07-06 07:19:58 -04:00
|
|
|
#define TYPE_NO_ID 1
|
|
|
|
#define TYPE_FOUND_ID 2
|
|
|
|
#define TYPE_UNKNOWN_ID 3
|
2014-12-23 14:07:57 -05:00
|
|
|
#define TYPE_CREATE_ID 4
|
2014-07-06 07:19:58 -04:00
|
|
|
|
|
|
|
#define IDCHOOSER_REFRESH 1
|
2013-06-23 14:55:30 -04:00
|
|
|
|
2014-12-23 14:07:57 -05:00
|
|
|
//#define IDCHOOSER_DEBUG
|
|
|
|
|
2012-11-19 17:14:45 -05:00
|
|
|
/** Constructor */
|
|
|
|
GxsIdChooser::GxsIdChooser(QWidget *parent)
|
2014-12-23 14:07:57 -05:00
|
|
|
: QComboBox(parent), mFlags(IDCHOOSER_ANON_DEFAULT)
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2014-07-06 07:19:58 -04:00
|
|
|
mBase = new RsGxsUpdateBroadcastBase(rsIdentity, this);
|
|
|
|
connect(mBase, SIGNAL(fillDisplay(bool)), this, SLOT(fillDisplay(bool)));
|
|
|
|
|
2014-12-23 19:10:15 -05:00
|
|
|
/* Initialize ui */
|
|
|
|
setSizeAdjustPolicy(QComboBox::AdjustToContents);
|
|
|
|
|
2014-12-23 14:07:57 -05:00
|
|
|
mFirstLoad = true;
|
2014-07-06 07:19:58 -04:00
|
|
|
|
2014-04-26 12:32:33 -04:00
|
|
|
mDefaultId.clear() ;
|
2013-06-23 14:55:30 -04:00
|
|
|
|
|
|
|
/* Enable sort with own role */
|
|
|
|
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
|
|
|
|
proxy->setSourceModel(model());
|
2015-03-20 10:52:10 -04:00
|
|
|
proxy->setDynamicSortFilter(false);
|
2013-06-23 14:55:30 -04:00
|
|
|
model()->setParent(proxy);
|
|
|
|
setModel(proxy);
|
|
|
|
|
|
|
|
proxy->setSortRole(ROLE_SORT);
|
2014-12-23 14:07:57 -05:00
|
|
|
|
|
|
|
/* Connect signals */
|
|
|
|
connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(myCurrentIndexChanged(int)));
|
|
|
|
connect(this, SIGNAL(activated(int)), this, SLOT(indexActivated(int)));
|
2014-07-06 07:19:58 -04:00
|
|
|
}
|
|
|
|
|
2014-11-24 16:02:18 -05:00
|
|
|
void GxsIdChooser::setFlags(uint32_t flags)
|
|
|
|
{
|
2014-12-23 14:07:57 -05:00
|
|
|
mFlags = flags ;
|
|
|
|
updateDisplay(true);
|
2014-11-24 16:02:18 -05:00
|
|
|
}
|
2014-12-23 14:07:57 -05:00
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
GxsIdChooser::~GxsIdChooser()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsIdChooser::fillDisplay(bool complete)
|
|
|
|
{
|
|
|
|
updateDisplay(complete);
|
|
|
|
update(); // Qt flush
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsIdChooser::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
mBase->showEvent(event);
|
|
|
|
QComboBox::showEvent(event);
|
2012-11-19 17:14:45 -05:00
|
|
|
}
|
|
|
|
|
2015-02-10 07:55:16 -05:00
|
|
|
void GxsIdChooser::loadIds(uint32_t chooserFlags, const RsGxsId &defId)
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
|
|
|
mFlags = chooserFlags;
|
|
|
|
mDefaultId = defId;
|
2013-02-27 18:52:27 -05:00
|
|
|
clear();
|
2014-07-06 07:19:58 -04:00
|
|
|
mFirstLoad = true;
|
2012-11-19 17:14:45 -05:00
|
|
|
}
|
|
|
|
|
2015-02-10 07:55:16 -05:00
|
|
|
void GxsIdChooser::setDefaultId(const RsGxsId &defId)
|
|
|
|
{
|
|
|
|
mDefaultId = defId;
|
|
|
|
}
|
|
|
|
|
2014-11-26 19:55:48 -05:00
|
|
|
static void loadPrivateIdsCallback(GxsIdDetailsType type, const RsIdentityDetails &details, QObject *object, const QVariant &/*data*/)
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2014-11-26 19:55:48 -05:00
|
|
|
GxsIdChooser *chooser = dynamic_cast<GxsIdChooser*>(object);
|
|
|
|
if (!chooser) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-23 14:55:30 -04:00
|
|
|
|
2015-03-06 16:13:23 -05:00
|
|
|
// this prevents the objects that depend on what's in the combo-box to activate and
|
|
|
|
// perform any change.Only user-changes should cause this.
|
|
|
|
chooser->blockSignals(true) ;
|
|
|
|
|
2014-11-26 19:55:48 -05:00
|
|
|
QString text = GxsIdDetails::getNameForType(type, details);
|
|
|
|
QString id = QString::fromStdString(details.mId.toStdString());
|
2013-06-23 14:55:30 -04:00
|
|
|
|
2014-11-26 19:55:48 -05:00
|
|
|
/* Find and replace text of exisiting item */
|
|
|
|
int index = chooser->findData(id);
|
|
|
|
if (index >= 0) {
|
|
|
|
chooser->setItemText(index, text);
|
|
|
|
} else {
|
|
|
|
/* Add new item */
|
|
|
|
chooser->addItem(text, id);
|
|
|
|
index = chooser->count() - 1;
|
|
|
|
}
|
2013-06-23 14:55:30 -04:00
|
|
|
|
2014-12-30 16:22:45 -05:00
|
|
|
QList<QIcon> icons;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GXS_ID_DETAILS_TYPE_EMPTY:
|
|
|
|
case GXS_ID_DETAILS_TYPE_FAILED:
|
|
|
|
// icons = ;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXS_ID_DETAILS_TYPE_LOADING:
|
|
|
|
icons.push_back(GxsIdDetails::getLoadingIcon(details.mId));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GXS_ID_DETAILS_TYPE_DONE:
|
|
|
|
GxsIdDetails::getIcons(details, icons);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-12-23 14:07:57 -05:00
|
|
|
chooser->setItemData(index, QString("%1_%2").arg((type == GXS_ID_DETAILS_TYPE_DONE) ? TYPE_FOUND_ID : TYPE_UNKNOWN_ID).arg(text), ROLE_SORT);
|
2014-11-26 19:55:48 -05:00
|
|
|
chooser->setItemData(index, (type == GXS_ID_DETAILS_TYPE_DONE) ? TYPE_FOUND_ID : TYPE_UNKNOWN_ID, ROLE_TYPE);
|
2014-12-30 16:22:45 -05:00
|
|
|
chooser->setItemIcon(index, icons.empty() ? QIcon() : icons[0]);
|
|
|
|
|
2015-03-06 16:13:23 -05:00
|
|
|
chooser->model()->sort(0);
|
|
|
|
|
|
|
|
chooser->blockSignals(false) ;
|
2013-06-23 14:55:30 -04:00
|
|
|
}
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2015-03-18 09:33:38 -04:00
|
|
|
void GxsIdChooser::loadPrivateIds()
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2014-12-23 14:07:57 -05:00
|
|
|
if (mFirstLoad) {
|
|
|
|
clear();
|
|
|
|
}
|
2013-06-23 14:55:30 -04:00
|
|
|
|
2012-11-19 17:14:45 -05:00
|
|
|
std::list<RsGxsId> ids;
|
2015-03-18 09:33:38 -04:00
|
|
|
rsIdentity->getOwnIds(ids);
|
2012-11-19 17:14:45 -05:00
|
|
|
|
|
|
|
//rsIdentity->getDefaultId(defId);
|
|
|
|
// Prefer to use an application specific default???
|
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
if (mFirstLoad) {
|
|
|
|
if (!(mFlags & IDCHOOSER_ID_REQUIRED)) {
|
2014-12-23 14:07:57 -05:00
|
|
|
/* add No Signature option */
|
|
|
|
QString str = tr("No Signature");
|
|
|
|
QString id = "";
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2014-12-23 14:07:57 -05:00
|
|
|
addItem(str, id);
|
|
|
|
setItemData(count() - 1, QString("%1_%2").arg(TYPE_NO_ID).arg(str), ROLE_SORT);
|
2014-07-06 07:19:58 -04:00
|
|
|
setItemData(count() - 1, TYPE_NO_ID, ROLE_TYPE);
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int idx = 0; idx < count(); ++idx) {
|
|
|
|
QVariant type = itemData(idx, ROLE_TYPE);
|
2014-07-06 07:19:58 -04:00
|
|
|
switch (type.toInt()) {
|
2014-12-23 14:07:57 -05:00
|
|
|
case TYPE_NO_ID:
|
|
|
|
case TYPE_CREATE_ID:
|
2014-07-06 07:19:58 -04:00
|
|
|
break;
|
2014-12-23 14:07:57 -05:00
|
|
|
case TYPE_FOUND_ID:
|
|
|
|
case TYPE_UNKNOWN_ID:
|
|
|
|
{
|
2014-07-06 07:19:58 -04:00
|
|
|
QVariant var = itemData(idx);
|
|
|
|
RsGxsId gxsId = RsGxsId(var.toString().toStdString());
|
2014-12-23 14:07:57 -05:00
|
|
|
std::list<RsGxsId>::iterator lit = std::find(ids.begin(), ids.end(), gxsId);
|
2014-07-06 07:19:58 -04:00
|
|
|
if (lit == ids.end()) {
|
|
|
|
removeItem(idx);
|
|
|
|
idx--;
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<RsGxsId>::iterator it = ids.begin(); it != ids.end(); ++it) {
|
2014-07-06 07:19:58 -04:00
|
|
|
/* add to Chooser */
|
2014-11-26 19:55:48 -05:00
|
|
|
GxsIdDetails::process(*it, loadPrivateIdsCallback, this);
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
2014-07-06 07:19:58 -04:00
|
|
|
|
2014-12-23 19:10:15 -05:00
|
|
|
if (mFirstLoad) {
|
|
|
|
if (!(mFlags & IDCHOOSER_NO_CREATE)) {
|
|
|
|
/* add Create Identity option */
|
|
|
|
QString str = tr("Create new Identity");
|
|
|
|
QString id = "";
|
|
|
|
|
2014-12-30 16:22:45 -05:00
|
|
|
addItem(QIcon(":/images/identity/identity_create_32.png"), str, id);
|
2014-12-23 19:10:15 -05:00
|
|
|
setItemData(count() - 1, QString("%1_%2").arg(TYPE_CREATE_ID).arg(str), ROLE_SORT);
|
|
|
|
setItemData(count() - 1, TYPE_CREATE_ID, ROLE_TYPE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mFirstLoad = false;
|
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
setDefaultItem();
|
2012-11-19 17:14:45 -05:00
|
|
|
}
|
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
void GxsIdChooser::setDefaultItem()
|
2012-11-19 17:14:45 -05:00
|
|
|
{
|
2014-07-06 07:19:58 -04:00
|
|
|
int def = -1;
|
|
|
|
|
|
|
|
if ((mFlags & IDCHOOSER_ANON_DEFAULT) && !(mFlags & IDCHOOSER_ID_REQUIRED)) {
|
|
|
|
def = findData(TYPE_NO_ID, ROLE_TYPE);
|
|
|
|
} else {
|
2014-12-23 19:10:15 -05:00
|
|
|
if (!mDefaultId.isNull()) {
|
|
|
|
QString id = QString::fromStdString(mDefaultId.toStdString());
|
|
|
|
def = findData(id);
|
|
|
|
}
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
2014-07-06 07:19:58 -04:00
|
|
|
|
|
|
|
if (def >= 0) {
|
|
|
|
setCurrentIndex(def);
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
2014-07-06 07:19:58 -04:00
|
|
|
}
|
|
|
|
|
2015-02-10 07:55:16 -05:00
|
|
|
bool GxsIdChooser::setChosenId(const RsGxsId &gxsId)
|
2014-12-23 14:07:57 -05:00
|
|
|
{
|
2014-07-06 07:19:58 -04:00
|
|
|
QString id = QString::fromStdString(gxsId.toStdString());
|
|
|
|
|
|
|
|
/* Find text of exisiting item */
|
|
|
|
int index = findData(id);
|
|
|
|
if (index >= 0) {
|
|
|
|
setCurrentIndex(index);
|
|
|
|
return true;
|
2012-11-19 17:14:45 -05:00
|
|
|
}
|
2014-12-23 14:07:57 -05:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
GxsIdChooser::ChosenId_Ret GxsIdChooser::getChosenId(RsGxsId &gxsId)
|
|
|
|
{
|
|
|
|
if (count() < 1) {
|
|
|
|
return None;
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
2014-07-06 07:19:58 -04:00
|
|
|
|
2012-11-19 17:14:45 -05:00
|
|
|
int idx = currentIndex();
|
|
|
|
|
|
|
|
QVariant var = itemData(idx);
|
2014-07-06 07:19:58 -04:00
|
|
|
gxsId = RsGxsId(var.toString().toStdString());
|
2014-12-23 14:07:57 -05:00
|
|
|
QVariant type = itemData(idx, ROLE_TYPE);
|
2014-07-06 07:19:58 -04:00
|
|
|
switch (type.toInt()) {
|
2014-12-23 14:07:57 -05:00
|
|
|
case TYPE_NO_ID:
|
2014-07-06 07:19:58 -04:00
|
|
|
return NoId;
|
2014-12-23 14:07:57 -05:00
|
|
|
case TYPE_FOUND_ID:
|
2014-07-06 07:19:58 -04:00
|
|
|
return KnowId;
|
2014-12-23 14:07:57 -05:00
|
|
|
case TYPE_UNKNOWN_ID:
|
2014-07-06 07:19:58 -04:00
|
|
|
return UnKnowId;
|
2014-12-23 14:07:57 -05:00
|
|
|
case TYPE_CREATE_ID:
|
|
|
|
break;
|
|
|
|
}
|
2014-07-06 07:19:58 -04:00
|
|
|
|
|
|
|
return None;
|
|
|
|
}
|
2012-11-19 17:14:45 -05:00
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
void GxsIdChooser::myCurrentIndexChanged(int index)
|
|
|
|
{
|
2014-12-23 14:07:57 -05:00
|
|
|
Q_UNUSED(index);
|
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
QFontMetrics fm = QFontMetrics(font());
|
|
|
|
QString text = currentText();
|
2014-12-23 14:07:57 -05:00
|
|
|
if (width() < fm.boundingRect(text).width()) {
|
2014-07-06 07:19:58 -04:00
|
|
|
setToolTip(text);
|
|
|
|
} else {
|
|
|
|
setToolTip("");
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GxsIdChooser::indexActivated(int index)
|
|
|
|
{
|
|
|
|
int type = itemData(index, ROLE_TYPE).toInt();
|
|
|
|
if (type == TYPE_CREATE_ID) {
|
|
|
|
IdEditDialog dlg(this);
|
|
|
|
dlg.setupNewId(false);
|
2015-02-10 07:55:16 -05:00
|
|
|
if (dlg.exec() == QDialog::Accepted) {
|
|
|
|
setDefaultId(RsGxsId(dlg.groupId()));
|
|
|
|
}
|
2014-12-23 14:07:57 -05:00
|
|
|
}
|
2012-11-19 17:14:45 -05:00
|
|
|
}
|
2014-07-06 07:19:58 -04:00
|
|
|
|
2015-03-26 17:20:57 -04:00
|
|
|
void GxsIdChooser::updateDisplay(bool reset)
|
2014-07-06 07:19:58 -04:00
|
|
|
{
|
2015-03-26 17:20:57 -04:00
|
|
|
if(reset)
|
|
|
|
mFirstLoad = true ;
|
2014-12-23 14:07:57 -05:00
|
|
|
|
2014-07-06 07:19:58 -04:00
|
|
|
/* Update identity list */
|
2015-03-18 09:33:38 -04:00
|
|
|
loadPrivateIds();
|
2013-06-23 14:55:30 -04:00
|
|
|
}
|