1
0
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:
apprb 2014-11-08 01:02:10 +09:00
parent af8df8e707
commit 58eebcb2a6
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
6 changed files with 106 additions and 34 deletions

View File

@ -722,18 +722,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag
emit failedToAddFriend(userId); emit failedToAddFriend(userId);
} else { } else {
// Update our friendAddresses // Update our friendAddresses
bool found=false; Settings::getInstance().updateFriendAdress(friendAddress);
QList<QString>& friendAddresses = Settings::getInstance().friendAddresses;
for (QString& addr : friendAddresses)
{
if (addr.toUpper().contains(friendAddress))
{
addr = friendAddress;
found = true;
}
}
if (!found)
friendAddresses.append(friendAddress);
emit friendAdded(friendId, userId); emit friendAdded(friendId, userId);
} }
saveConfiguration(); saveConfiguration();
@ -1612,10 +1601,9 @@ QString Core::getFriendAddress(int friendNumber) const
QByteArray data((char*)rawid,TOX_CLIENT_ID_SIZE); QByteArray data((char*)rawid,TOX_CLIENT_ID_SIZE);
QString id = data.toHex().toUpper(); QString id = data.toHex().toUpper();
QList<QString>& friendAddresses = Settings::getInstance().friendAddresses; QString addr = Settings::getInstance().getFriendAdress(id);
for (QString addr : friendAddresses) if (addr.size() > id.size())
if (addr.toUpper().contains(id)) return addr;
return addr;
return id; return id;
} }

View File

@ -92,12 +92,16 @@ void Settings::load()
useCustomDhtList=false; useCustomDhtList=false;
s.endGroup(); s.endGroup();
friendAddresses.clear(); friendLst.clear();
s.beginGroup("Friends"); s.beginGroup("Friends");
int size = s.beginReadArray("fullAddresses"); int size = s.beginReadArray("fullAddresses");
for (int i = 0; i < size; i ++) { for (int i = 0; i < size; i ++)
{
s.setArrayIndex(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.endArray();
s.endGroup(); s.endGroup();
@ -219,10 +223,14 @@ void Settings::save(QString path)
s.endGroup(); s.endGroup();
s.beginGroup("Friends"); s.beginGroup("Friends");
s.beginWriteArray("fullAddresses", friendAddresses.size()); s.beginWriteArray("fullAddresses", friendLst.size());
for (int i = 0; i < friendAddresses.size(); i ++) { int index = 0;
s.setArrayIndex(i); for (auto &frnd : friendLst)
s.setValue("addr", friendAddresses[i]); {
s.setArrayIndex(index);
s.setValue("addr", frnd.addr);
s.setValue("alias", frnd.alias);
index++;
} }
s.endArray(); s.endArray();
s.endGroup(); s.endGroup();
@ -760,3 +768,52 @@ void Settings::setOutDev(const QString& deviceSpecifier)
{ {
outDev = 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;
}
}

View File

@ -21,6 +21,8 @@
#include <QObject> #include <QObject>
#include <QPixmap> #include <QPixmap>
class ToxID;
class Settings : public QObject class Settings : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -185,8 +187,13 @@ public:
QByteArray getSplitterState() const; QByteArray getSplitterState() const;
void setSplitterState(const QByteArray &value); 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: public:
QList<QString> friendAddresses;
void save(); void save();
void save(QString path); void save(QString path);
void load(); void load();
@ -258,6 +265,14 @@ private:
QString inDev; QString inDev;
QString outDev; QString outDev;
struct friendProp
{
QString alias;
QString addr;
};
QHash<QString, friendProp> friendLst;
signals: signals:
//void dataChanged(); //void dataChanged();
void dhtServerListChanged(); void dhtServerListChanged();

View File

@ -34,6 +34,7 @@
#include <QBitmap> #include <QBitmap>
#include <QFileDialog> #include <QFileDialog>
#include <QDebug> #include <QDebug>
#include <QInputDialog>
FriendWidget::FriendWidget(int FriendId, QString id) FriendWidget::FriendWidget(int FriendId, QString id)
: friendId(FriendId) : friendId(FriendId)
@ -42,10 +43,6 @@ FriendWidget::FriendWidget(int FriendId, QString id)
avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent); avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent);
statusPic.setPixmap(QPixmap(":img/status/dot_away.png")); statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
nameLabel->setText(id); 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) void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
@ -67,6 +64,8 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
if (groupActions.isEmpty()) if (groupActions.isEmpty())
inviteMenu->setEnabled(false); inviteMenu->setEnabled(false);
QAction* setAlias = menu.addAction(tr("Set alias..."));
menu.addSeparator(); menu.addSeparator();
QAction* autoAccept = menu.addAction(tr("Auto accept files from this friend", "context menu entry")); QAction* autoAccept = menu.addAction(tr("Auto accept files from this friend", "context menu entry"));
autoAccept->setCheckable(true); autoAccept->setCheckable(true);
@ -82,6 +81,9 @@ void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
{ {
emit copyFriendIdToClipboard(friendId); emit copyFriendIdToClipboard(friendId);
return; return;
} else if (selectedItem == setAlias)
{
setFriendAlias();
} }
else if (selectedItem == removeFriendAction) 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); 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();
}
} }

View File

@ -45,12 +45,10 @@ public slots:
void onAvatarChange(int FriendId, const QPixmap& pic); void onAvatarChange(int FriendId, const QPixmap& pic);
void onAvatarRemoved(int FriendId); void onAvatarRemoved(int FriendId);
private slots:
void onFriendAliasChange(QString newText, QString oldText);
protected: protected:
void mousePressEvent(QMouseEvent* ev); void mousePressEvent(QMouseEvent* ev);
void mouseMoveEvent(QMouseEvent* ev); void mouseMoveEvent(QMouseEvent* ev);
void setFriendAlias();
public: public:
int friendId; int friendId;

View File

@ -603,7 +603,8 @@ void Widget::addFriend(int friendId, const QString &userId)
QLayout* layout = contactListWidget->getFriendLayout(Status::Offline); QLayout* layout = contactListWidget->getFriendLayout(Status::Offline);
layout->addWidget(newfriend->getFriendWidget()); 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(chatroomWidgetClicked(GenericChatroomWidget*)), this, SLOT(onChatroomWidgetClicked(GenericChatroomWidget*)));
connect(newfriend->getFriendWidget(), SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int))); connect(newfriend->getFriendWidget(), SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int)));