mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
load/save aliases from the settings file
This commit is contained in:
parent
af8df8e707
commit
58eebcb2a6
20
src/core.cpp
20
src/core.cpp
|
@ -722,18 +722,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag
|
|||
emit failedToAddFriend(userId);
|
||||
} else {
|
||||
// Update our friendAddresses
|
||||
bool found=false;
|
||||
QList<QString>& friendAddresses = Settings::getInstance().friendAddresses;
|
||||
for (QString& addr : friendAddresses)
|
||||
{
|
||||
if (addr.toUpper().contains(friendAddress))
|
||||
{
|
||||
addr = friendAddress;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
friendAddresses.append(friendAddress);
|
||||
Settings::getInstance().updateFriendAdress(friendAddress);
|
||||
emit friendAdded(friendId, userId);
|
||||
}
|
||||
saveConfiguration();
|
||||
|
@ -1612,10 +1601,9 @@ QString Core::getFriendAddress(int friendNumber) const
|
|||
QByteArray data((char*)rawid,TOX_CLIENT_ID_SIZE);
|
||||
QString id = data.toHex().toUpper();
|
||||
|
||||
QList<QString>& friendAddresses = Settings::getInstance().friendAddresses;
|
||||
for (QString addr : friendAddresses)
|
||||
if (addr.toUpper().contains(id))
|
||||
return addr;
|
||||
QString addr = Settings::getInstance().getFriendAdress(id);
|
||||
if (addr.size() > id.size())
|
||||
return addr;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
|
|
@ -92,12 +92,16 @@ void Settings::load()
|
|||
useCustomDhtList=false;
|
||||
s.endGroup();
|
||||
|
||||
friendAddresses.clear();
|
||||
friendLst.clear();
|
||||
s.beginGroup("Friends");
|
||||
int size = s.beginReadArray("fullAddresses");
|
||||
for (int i = 0; i < size; i ++) {
|
||||
for (int i = 0; i < size; i ++)
|
||||
{
|
||||
s.setArrayIndex(i);
|
||||
friendAddresses.append(s.value("addr").toString());
|
||||
friendProp fp;
|
||||
fp.addr = s.value("addr").toString();
|
||||
fp.alias = s.value("alias").toString();
|
||||
friendLst[ToxID::fromString(fp.addr).publicKey] = fp;
|
||||
}
|
||||
s.endArray();
|
||||
s.endGroup();
|
||||
|
@ -219,10 +223,14 @@ void Settings::save(QString path)
|
|||
s.endGroup();
|
||||
|
||||
s.beginGroup("Friends");
|
||||
s.beginWriteArray("fullAddresses", friendAddresses.size());
|
||||
for (int i = 0; i < friendAddresses.size(); i ++) {
|
||||
s.setArrayIndex(i);
|
||||
s.setValue("addr", friendAddresses[i]);
|
||||
s.beginWriteArray("fullAddresses", friendLst.size());
|
||||
int index = 0;
|
||||
for (auto &frnd : friendLst)
|
||||
{
|
||||
s.setArrayIndex(index);
|
||||
s.setValue("addr", frnd.addr);
|
||||
s.setValue("alias", frnd.alias);
|
||||
index++;
|
||||
}
|
||||
s.endArray();
|
||||
s.endGroup();
|
||||
|
@ -760,3 +768,52 @@ void Settings::setOutDev(const QString& deviceSpecifier)
|
|||
{
|
||||
outDev = deviceSpecifier;
|
||||
}
|
||||
|
||||
QString Settings::getFriendAdress(const QString &publicKey) const
|
||||
{
|
||||
QString key = ToxID::fromString(publicKey).publicKey;
|
||||
auto it = friendLst.find(key);
|
||||
if (it != friendLst.end())
|
||||
{
|
||||
return it->addr;
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
void Settings::updateFriendAdress(const QString &newAddr)
|
||||
{
|
||||
QString key = ToxID::fromString(newAddr).publicKey;
|
||||
auto it = friendLst.find(key);
|
||||
if (it != friendLst.end())
|
||||
{
|
||||
it->addr = newAddr;
|
||||
} else {
|
||||
friendProp fp;
|
||||
fp.addr = newAddr;
|
||||
fp.alias = "";
|
||||
friendLst[newAddr] = fp;
|
||||
}
|
||||
}
|
||||
|
||||
QString Settings::getFriendAlias(const ToxID &id) const
|
||||
{
|
||||
QString key = id.publicKey;
|
||||
auto it = friendLst.find(key);
|
||||
if (it != friendLst.end())
|
||||
{
|
||||
return it->alias;
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
void Settings::setFriendAlias(const ToxID &id, const QString &alias)
|
||||
{
|
||||
QString key = id.publicKey;
|
||||
auto it = friendLst.find(key);
|
||||
if (it != friendLst.end())
|
||||
{
|
||||
it->alias = alias;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
#include <QObject>
|
||||
#include <QPixmap>
|
||||
|
||||
class ToxID;
|
||||
|
||||
class Settings : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
@ -185,8 +187,13 @@ public:
|
|||
QByteArray getSplitterState() const;
|
||||
void setSplitterState(const QByteArray &value);
|
||||
|
||||
QString getFriendAdress(const QString &publicKey) const;
|
||||
void updateFriendAdress(const QString &newAddr);
|
||||
|
||||
QString getFriendAlias(const ToxID &id) const;
|
||||
void setFriendAlias(const ToxID &id, const QString &alias);
|
||||
|
||||
public:
|
||||
QList<QString> friendAddresses;
|
||||
void save();
|
||||
void save(QString path);
|
||||
void load();
|
||||
|
@ -258,6 +265,14 @@ private:
|
|||
QString inDev;
|
||||
QString outDev;
|
||||
|
||||
struct friendProp
|
||||
{
|
||||
QString alias;
|
||||
QString addr;
|
||||
};
|
||||
|
||||
QHash<QString, friendProp> friendLst;
|
||||
|
||||
signals:
|
||||
//void dataChanged();
|
||||
void dhtServerListChanged();
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <QBitmap>
|
||||
#include <QFileDialog>
|
||||
#include <QDebug>
|
||||
#include <QInputDialog>
|
||||
|
||||
FriendWidget::FriendWidget(int FriendId, QString id)
|
||||
: friendId(FriendId)
|
||||
|
@ -42,10 +43,6 @@ FriendWidget::FriendWidget(int FriendId, QString id)
|
|||
avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent);
|
||||
statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
|
||||
nameLabel->setText(id);
|
||||
nameLabel->setAttribute(Qt::WA_NoMousePropagation);
|
||||
nameLabel->setEditable(true);
|
||||
|
||||
connect(nameLabel, SIGNAL(textChanged(QString,QString)), this, SLOT(onFriendAliasChange(QString,QString)));
|
||||
}
|
||||
|
||||
void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
|
||||
|
@ -67,6 +64,8 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
|
|||
if (groupActions.isEmpty())
|
||||
inviteMenu->setEnabled(false);
|
||||
|
||||
QAction* setAlias = menu.addAction(tr("Set alias..."));
|
||||
|
||||
menu.addSeparator();
|
||||
QAction* autoAccept = menu.addAction(tr("Auto accept files from this friend", "context menu entry"));
|
||||
autoAccept->setCheckable(true);
|
||||
|
@ -82,6 +81,9 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
|
|||
{
|
||||
emit copyFriendIdToClipboard(friendId);
|
||||
return;
|
||||
} else if (selectedItem == setAlias)
|
||||
{
|
||||
setFriendAlias();
|
||||
}
|
||||
else if (selectedItem == removeFriendAction)
|
||||
{
|
||||
|
@ -215,8 +217,19 @@ void FriendWidget::mouseMoveEvent(QMouseEvent *ev)
|
|||
}
|
||||
}
|
||||
|
||||
void FriendWidget::onFriendAliasChange(QString newText, QString)
|
||||
void FriendWidget::setFriendAlias()
|
||||
{
|
||||
bool ok;
|
||||
Friend* f = FriendList::findFriend(friendId);
|
||||
f->setAlias(newText);
|
||||
|
||||
QString alias = QInputDialog::getText(nullptr, tr("User alias"), tr("Alias:"), QLineEdit::Normal,
|
||||
f->getDisplayedName(), &ok);
|
||||
|
||||
if (ok)
|
||||
{
|
||||
f->setAlias(alias);
|
||||
Settings::getInstance().setFriendAlias(f->getToxID(), alias);
|
||||
hide();
|
||||
show();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,12 +45,10 @@ public slots:
|
|||
void onAvatarChange(int FriendId, const QPixmap& pic);
|
||||
void onAvatarRemoved(int FriendId);
|
||||
|
||||
private slots:
|
||||
void onFriendAliasChange(QString newText, QString oldText);
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent* ev);
|
||||
void mouseMoveEvent(QMouseEvent* ev);
|
||||
void setFriendAlias();
|
||||
|
||||
public:
|
||||
int friendId;
|
||||
|
|
|
@ -603,7 +603,8 @@ void Widget::addFriend(int friendId, const QString &userId)
|
|||
QLayout* layout = contactListWidget->getFriendLayout(Status::Offline);
|
||||
layout->addWidget(newfriend->getFriendWidget());
|
||||
|
||||
newfriend->setAlias("");
|
||||
QString alias = Settings::getInstance().getFriendAlias(ToxID::fromString(userId));
|
||||
newfriend->setAlias(alias);
|
||||
|
||||
connect(newfriend->getFriendWidget(), SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*)));
|
||||
connect(newfriend->getFriendWidget(), SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));
|
||||
|
|
Loading…
Reference in New Issue
Block a user