Example #1
0
void ContactListEdit::GetCollectedContacts(IMailProcessor::TRecipientPublicKeys* storage) const
  {
  /** FIXME - this code generally looks bad. It will don't work when in AB will be conflict between
      kID and fName lName alias.
      Whole completer <-> contact edit communication is wrong, since it should operate on Contact
      objects not just strings (where noone knows it is a kID or alias).
      Another problem is synchronization against AB changes. It is also did wrong. To do it correclty
      instantiated completer should use dedicated model operating DIRECTLY on AB not copied string
      list.
  */
  auto addressbook = bts::get_profile()->get_addressbook();
  QStringList recipient_image_names = getListOfImageNames();
  storage->reserve(recipient_image_names.size());

  for(const auto& recipient : recipient_image_names)
    {
    std::string to_string = recipient.toStdString();
    //check first to see if we have a dac_id
    auto to_contact = addressbook->get_contact_by_dac_id(to_string);
    if (!to_contact.valid()) // if not dac_id, check if we have a full name
      to_contact = addressbook->get_contact_by_display_name(to_string);
    assert(to_contact.valid());
    storage->push_back(to_contact->public_key);
    }
  }
void ContactListEdit::onTextChanged()
{
    /** Remember last changed text range in _activeCursor member. Will be used to replace entered text
        whith contact entry.
    */
    QString text = textUnderCursor(&_activeCursor);

    if(_skipCompletion == false && _completer != nullptr && isReadOnly() == false &&
            text.isEmpty() == false)
    {
        TAutoSkipCompletion asc(this);

        std::string textKey = text.toStdString();

        bts::addressbook::wallet_contact matchedContact;

        bool contactFound = false;
        try
        {
            auto aBook = bts::get_profile()->get_addressbook();

            auto contact = aBook->get_contact_by_display_name(textKey);
            contactFound = contact;
            if(contactFound == false)
                contact = aBook->get_contact_by_dac_id(textKey);

            if(contact)
            {
                contactFound = true;
                matchedContact = *contact;
            }
        }
        catch(const fc::exception& e)
        {
            wlog( "${e}", ("e",e.to_detail_string()) );
        }

        bool semanticallyValidKey = false;
        if(contactFound)
        {
            /// Delete previously entered text - will be replaced with image control holding contact info
            deleteEnteredText();
            /// Update PK-like text with displayed form
            text = QString::fromStdString(matchedContact.get_display_name());
            /// If text already points to some found contact, just add it
            addContactEntry(text, matchedContact, false);
        }
        else if(public_key_address::is_valid(textKey, &semanticallyValidKey) && semanticallyValidKey)
        {
            /// Delete previously entered text - will be replaced with image control holding contact info
            deleteEnteredText();

            public_key_address converter(textKey);
            fc::ecc::public_key parsedKey = converter.key;

            if(Utils::matchContact(parsedKey, &matchedContact))
            {
                QString displayText = _completerModel->getDisplayText(Contact(matchedContact));
                addContactEntry(displayText, matchedContact, false);

                QString txt = tr("Known public key has been replaced with matching contact...");
                /// Don't pass here parent widget to avoid switching active window when tooltip appears
                QToolTip::showText(QCursor::pos(), txt, nullptr, QRect(), 3000);
            }
            else
            {
                QString tooltip = tr("Valid, but <b>unknown</b> public key, cannot be matched to any contact "
                                     "present in address book...");
                addContactEntry(text, matchedContact, true, &tooltip);
            }
        }
        else
        {
            QTextCharFormat fmt;
            fmt.setFontWeight(QFont::Bold);
            fmt.setForeground(QColor(Qt::red));
            fmt.setToolTip(tr("Unknown contact - can be <b>ignored</b> while sending message"));
            _activeCursor.mergeCharFormat(fmt);

            showCompleter(text);
        }
    }

    fitHeightToDocument();
}