Browse Source

create and add cid to Addressbook

pull/130/head
DenioD 4 years ago
parent
commit
dd48ab87eb
  1. 45
      src/addressbook.cpp
  2. 6
      src/addressbook.h
  3. 2
      src/contactmodel.cpp
  4. 16
      src/contactmodel.h
  5. 4
      src/mainwindow.cpp
  6. 28
      src/mainwindow.ui

45
src/addressbook.cpp

@ -8,7 +8,7 @@
AddressBookModel::AddressBookModel(QTableView *parent) : QAbstractTableModel(parent) AddressBookModel::AddressBookModel(QTableView *parent) : QAbstractTableModel(parent)
{ {
headers << tr("Label") << tr("Address") << tr("HushChatAddress"); headers << tr("Label") << tr("Address") << tr("HushChatAddress") << tr("cid");
this->parent = parent; this->parent = parent;
loadData(); loadData();
} }
@ -38,10 +38,10 @@ void AddressBookModel::loadData()
); );
} }
void AddressBookModel::addNewLabel(QString label, QString addr, QString myAddr) void AddressBookModel::addNewLabel(QString label, QString addr, QString myAddr, QString cid)
{ {
//labels.push_back(QPair<QString, QString>(label, addr)); //labels.push_back(QPair<QString, QString>(label, addr));
AddressBook::getInstance()->addAddressLabel(label, addr, myAddr); AddressBook::getInstance()->addAddressLabel(label, addr, myAddr, cid);
updateUi(); updateUi();
} }
@ -59,7 +59,7 @@ void AddressBookModel::removeItemAt(int row)
if (row >= labels.size()) if (row >= labels.size())
return; return;
AddressBook::getInstance()->removeAddressLabel(labels[row].getName(), labels[row].getPartnerAddress(), labels[row].getMyAddress()); AddressBook::getInstance()->removeAddressLabel(labels[row].getName(), labels[row].getPartnerAddress(), labels[row].getMyAddress(),labels[row].getcid());
labels.clear(); labels.clear();
labels = AddressBook::getInstance()->getAllAddressLabels(); labels = AddressBook::getInstance()->getAllAddressLabels();
dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1)); dataChanged(index(0, 0), index(labels.size()-1, columnCount(index(0,0))-1));
@ -70,7 +70,7 @@ ContactItem AddressBookModel::itemAt(int row)
{ {
if (row >= labels.size()) if (row >= labels.size())
{ {
ContactItem item = ContactItem("", "", ""); ContactItem item = ContactItem("", "", "", "");
return item; return item;
} }
@ -100,6 +100,7 @@ QVariant AddressBookModel::data(const QModelIndex &index, int role) const
case 0: return labels.at(index.row()).getName(); case 0: return labels.at(index.row()).getName();
case 1: return labels.at(index.row()).getPartnerAddress(); case 1: return labels.at(index.row()).getPartnerAddress();
case 2: return labels.at(index.row()).getMyAddress(); case 2: return labels.at(index.row()).getMyAddress();
case 3: return labels.at(index.row()).getcid();
} }
} }
@ -190,17 +191,24 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
QMessageBox::critical( QMessageBox::critical(
parent, parent,
QObject::tr("Success"), QObject::tr("Success"),
message, //todo traslate this shit message, //todo translate this
QMessageBox::Ok QMessageBox::Ok
); );
// ab.addr_chat->setText(myAddr); // ab.addr_chat->setText(myAddr);
qDebug() << "new generated myAddr" << myAddr; qDebug() << "new generated myAddr" << myAddr;
AddressBook::getInstance()->addAddressLabel(newLabel, ab.addr->text(), myAddr); QString cid = QUuid::createUuid().toString(QUuid::WithoutBraces); //
AddressBook::getInstance()->addAddressLabel(newLabel, ab.addr->text(), myAddr, cid);
}); });
model.updateUi(); //todo fix updating gui after adding model.updateUi(); //todo fix updating gui after adding
}); });
/// Generate CID for Contact
// AddressBook::getInstance()->addAddressLabel(newLabel, ab.addr->text(), cid);
// Import Button // Import Button
QObject::connect(ab.btnImport, &QPushButton::clicked, [&] () { QObject::connect(ab.btnImport, &QPushButton::clicked, [&] () {
// Get the import file name. // Get the import file name.
@ -236,7 +244,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
continue; continue;
// Add label, address. // Add label, address.
model.addNewLabel(items.at(1), items.at(0), ""); model.addNewLabel(items.at(1), items.at(0), "", "");
numImported++; numImported++;
} }
@ -247,7 +255,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
); );
}); });
auto fnSetTargetLabelAddr = [=] (QLineEdit* target, QString label, QString addr, QString myAddr) { auto fnSetTargetLabelAddr = [=] (QLineEdit* target, QString label, QString addr, QString myAddr, QString cid) {
target->setText(label % "/" % addr % myAddr); target->setText(label % "/" % addr % myAddr);
}; };
@ -263,8 +271,9 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
QString lbl = model.itemAt(index.row()).getName(); QString lbl = model.itemAt(index.row()).getName();
QString addr = model.itemAt(index.row()).getPartnerAddress(); QString addr = model.itemAt(index.row()).getPartnerAddress();
QString myAddr = model.itemAt(index.row()).getMyAddress(); QString myAddr = model.itemAt(index.row()).getMyAddress();
QString cid = model.itemAt(index.row()).getcid();
d.accept(); d.accept();
fnSetTargetLabelAddr(target, lbl, addr, myAddr); fnSetTargetLabelAddr(target, lbl, addr, myAddr, cid);
}); });
// Right-Click // Right-Click
@ -277,13 +286,14 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
QString lbl = model.itemAt(index.row()).getName(); QString lbl = model.itemAt(index.row()).getName();
QString addr = model.itemAt(index.row()).getPartnerAddress(); QString addr = model.itemAt(index.row()).getPartnerAddress();
QString myAddr = model.itemAt(index.row()).getMyAddress(); QString myAddr = model.itemAt(index.row()).getMyAddress();
QString cid = model.itemAt(index.row()).getcid();
QMenu menu(parent); QMenu menu(parent);
if (target != nullptr) if (target != nullptr)
menu.addAction("Pick", [&] () { menu.addAction("Pick", [&] () {
d.accept(); d.accept();
fnSetTargetLabelAddr(target, lbl, addr, myAddr); fnSetTargetLabelAddr(target, lbl, addr, myAddr, cid);
}); });
menu.addAction(QObject::tr("Copy address"), [&] () { menu.addAction(QObject::tr("Copy address"), [&] () {
@ -302,7 +312,7 @@ void AddressBook::open(MainWindow* parent, QLineEdit* target)
auto selection = ab.addresses->selectionModel(); auto selection = ab.addresses->selectionModel();
if (selection && selection->hasSelection() && selection->selectedRows().size() > 0) { if (selection && selection->hasSelection() && selection->selectedRows().size() > 0) {
auto item = model.itemAt(selection->selectedRows().at(0).row()); auto item = model.itemAt(selection->selectedRows().at(0).row());
fnSetTargetLabelAddr(target, item.getName(), item.getMyAddress(), item.getPartnerAddress()); fnSetTargetLabelAddr(target, item.getName(), item.getMyAddress(), item.getPartnerAddress(), item.getcid());
} }
}; };
@ -348,7 +358,7 @@ void AddressBook::readFromStorage()
//qDebug() << "0:" << stuff[i][0]; //qDebug() << "0:" << stuff[i][0];
//qDebug() << "1:" << stuff[i][1]; //qDebug() << "1:" << stuff[i][1];
//qDebug() << "2:" << stuff[i][2]; //qDebug() << "2:" << stuff[i][2];
ContactItem contact = ContactItem(stuff[i][2], stuff[i][1], stuff[i][0]); ContactItem contact = ContactItem(stuff[i][3],stuff[i][2], stuff[i][1], stuff[i][0]);
//qDebug() << "contact=" << contact.toQTString(); //qDebug() << "contact=" << contact.toQTString();
allLabels.push_back(contact); allLabels.push_back(contact);
} }
@ -383,6 +393,7 @@ void AddressBook::writeToStorage()
c.push_back(item.getName()); c.push_back(item.getName());
c.push_back(item.getPartnerAddress()); c.push_back(item.getPartnerAddress());
c.push_back(item.getMyAddress()); c.push_back(item.getMyAddress());
c.push_back(item.getcid());
contacts.push_back(c); contacts.push_back(c);
} }
out << QString("v1") << contacts; out << QString("v1") << contacts;
@ -405,22 +416,22 @@ QString AddressBook::writeableFile()
// Add a new address/label to the database // Add a new address/label to the database
void AddressBook::addAddressLabel(QString label, QString address, QString myAddr) void AddressBook::addAddressLabel(QString label, QString address, QString myAddr, QString cid)
{ {
Q_ASSERT(Settings::isValidAddress(address)); Q_ASSERT(Settings::isValidAddress(address));
// getName(), remove any existing label // getName(), remove any existing label
// Iterate over the list and remove the label/address // Iterate over the list and remove the label/address
for (int i=0; i < allLabels.size(); i++) for (int i=0; i < allLabels.size(); i++)
if (allLabels[i].getName() == label) if (allLabels[i].getName() == label)
removeAddressLabel(allLabels[i].getName(), allLabels[i].getPartnerAddress(),allLabels[i].getMyAddress()); removeAddressLabel(allLabels[i].getName(), allLabels[i].getPartnerAddress(),allLabels[i].getMyAddress(), allLabels[i].getcid());
ContactItem item = ContactItem(myAddr, address, label); ContactItem item = ContactItem(myAddr, address, label, cid);
allLabels.push_back(item); allLabels.push_back(item);
writeToStorage(); writeToStorage();
} }
// Remove a new address/label from the database // Remove a new address/label from the database
void AddressBook::removeAddressLabel(QString label, QString address, QString myAddr) void AddressBook::removeAddressLabel(QString label, QString address, QString myAddr, QString cid)
{ {
// Iterate over the list and remove the label/address // Iterate over the list and remove the label/address
for (int i=0; i < allLabels.size(); i++) for (int i=0; i < allLabels.size(); i++)

6
src/addressbook.h

@ -12,7 +12,7 @@ public:
AddressBookModel(QTableView* parent); AddressBookModel(QTableView* parent);
~AddressBookModel(); ~AddressBookModel();
void addNewLabel(QString label, QString address, QString myAddr); void addNewLabel(QString label, QString address, QString myAddr, QString cid);
void updateUi(); void updateUi();
void removeItemAt(int row); void removeItemAt(int row);
//QPair<QString, QString> itemAt(int row); //QPair<QString, QString> itemAt(int row);
@ -43,10 +43,10 @@ public:
static QString addressFromAddressLabel(const QString& lblAddr); static QString addressFromAddressLabel(const QString& lblAddr);
// Add a new address/label to the database // Add a new address/label to the database
void addAddressLabel(QString label, QString address, QString myAddr); void addAddressLabel(QString label, QString address, QString myAddr, QString cid);
// Remove a new address/label from the database // Remove a new address/label from the database
void removeAddressLabel(QString label, QString address, QString myAddr); void removeAddressLabel(QString label, QString address, QString myAddr, QString cid);
// Update a label/address // Update a label/address
void updateLabel(QString oldlabel, QString address, QString newlabel); void updateLabel(QString oldlabel, QString address, QString newlabel);

2
src/contactmodel.cpp

@ -10,7 +10,7 @@ void ContactModel::renderContactList(QListWidget* view)
} }
for(auto &c : AddressBook::getInstance()->getAllAddressLabels()) for(auto &c : AddressBook::getInstance()->getAllAddressLabels())
{ {
view->addItem(c.getPartnerAddress()); view->addItem(c.getPartnerAddress()); //hide
} }

16
src/contactmodel.h

@ -11,14 +11,16 @@ class ContactItem
QString _myAddress; QString _myAddress;
QString _partnerAddress; QString _partnerAddress;
QString _name; QString _name;
QString _cid;
public: public:
ContactItem(); ContactItem();
ContactItem(QString myAddress, QString partnerAddress, QString name) ContactItem(QString myAddress, QString partnerAddress, QString name, QString cid)
{ {
_myAddress = myAddress; _myAddress = myAddress;
_partnerAddress = partnerAddress; _partnerAddress = partnerAddress;
_name = name; _name = name;
_cid = cid;
} }
QString getName() const QString getName() const
@ -36,6 +38,11 @@ class ContactItem
return _partnerAddress; return _partnerAddress;
} }
QString getcid() const
{
return _cid;
}
void setName(QString name) void setName(QString name)
{ {
_name = name; _name = name;
@ -51,9 +58,14 @@ class ContactItem
_partnerAddress = partnerAddress; _partnerAddress = partnerAddress;
} }
void setcid(QString cid)
{
_cid = cid;
}
QString toQTString() QString toQTString()
{ {
return _name + "|" + _partnerAddress + "|" + _myAddress; return _name + "|" + _partnerAddress + "|" + _myAddress + "|" + _cid;
} }
}; };

4
src/mainwindow.cpp

@ -1296,7 +1296,7 @@ void MainWindow::setupReceiveTab() {
if (!curLabel.isEmpty() && label.isEmpty()) { if (!curLabel.isEmpty() && label.isEmpty()) {
info = "Removed Label '" % curLabel % "'"; info = "Removed Label '" % curLabel % "'";
AddressBook::getInstance()->removeAddressLabel(curLabel, addr, ""); AddressBook::getInstance()->removeAddressLabel(curLabel, addr, "", "");
} }
else if (!curLabel.isEmpty() && !label.isEmpty()) { else if (!curLabel.isEmpty() && !label.isEmpty()) {
info = "Updated Label '" % curLabel % "' to '" % label % "'"; info = "Updated Label '" % curLabel % "' to '" % label % "'";
@ -1304,7 +1304,7 @@ void MainWindow::setupReceiveTab() {
} }
else if (curLabel.isEmpty() && !label.isEmpty()) { else if (curLabel.isEmpty() && !label.isEmpty()) {
info = "Added Label '" % label % "'"; info = "Added Label '" % label % "'";
AddressBook::getInstance()->addAddressLabel(label, addr, ""); AddressBook::getInstance()->addAddressLabel(label, addr, "", "");
} }
// Update labels everywhere on the UI // Update labels everywhere on the UI

28
src/mainwindow.ui

@ -14,7 +14,7 @@
<string>SilentDragonLite</string> <string>SilentDragonLite</string>
</property> </property>
<property name="windowIcon"> <property name="windowIcon">
<iconset resource="../application.qrc"> <iconset>
<normaloff>:/icons/res/icon.ico</normaloff>:/icons/res/icon.ico</iconset> <normaloff>:/icons/res/icon.ico</normaloff>:/icons/res/icon.ico</iconset>
</property> </property>
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
@ -1502,6 +1502,28 @@
<string/> <string/>
</property> </property>
</widget> </widget>
<widget class="QPushButton" name="sendChatButton_2">
<property name="geometry">
<rect>
<x>943</x>
<y>20</y>
<width>211</width>
<height>25</height>
</rect>
</property>
<property name="baseSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Sicheren Kontakt hinzufügen</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</widget> </widget>
</widget> </widget>
</item> </item>
@ -1714,8 +1736,6 @@
<tabstop>minerFeeAmt</tabstop> <tabstop>minerFeeAmt</tabstop>
<tabstop>sendToScrollArea</tabstop> <tabstop>sendToScrollArea</tabstop>
</tabstops> </tabstops>
<resources> <resources/>
<include location="../application.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>

Loading…
Cancel
Save