void TFileAttachmentWidget::onAttachementTableSelectionChanged()
{
  TSelection selection;
  RetrieveSelection(&selection);

  unsigned int selectedCount = selection.size();
  bool anySelection = selectedCount != 0;
  bool singleSelection = selectedCount == 1;

  ui->actionAdd->setEnabled(EditMode);
  ui->actionDel->setEnabled(anySelection && EditMode);
  ui->actionOpen->setEnabled(singleSelection);
  ui->actionSave->setEnabled(anySelection);  

  bool vCardVaild = false;
  bool existContact = false;
  if (anySelection)
  {
    AAttachmentItem* item = selection.front();
    QString fileName = item->GetDisplayedFileName();

    QByteArray contactData;
    if (TFileAttachmentWidget::isValidContactvCard(fileName, *item, &contactData))
    {
      vCardVaild = true;

      ContactvCard converter(contactData);
      std::string publicKeyString = converter.getPublicKey().toStdString();

      fc::ecc::public_key parsedKey;
      if (public_key_address::convert(publicKeyString, &parsedKey))
      {
        if(Utils::matchContact(parsedKey, &_clickedContact))
        {
          existContact = true;
        }
      }
      //else
      //Warning: public key can be invalid but enable option "AddContact".
      //User can correct public key
    }    
  }

  ui->actionAddContact->setEnabled(singleSelection && !EditMode && vCardVaild && !existContact);
  ui->actionFindContact->setEnabled(singleSelection && !EditMode && vCardVaild && existContact);
  ui->actionRename->setEnabled(singleSelection && EditMode && !vCardVaild);
  if (singleSelection)
  {
    //disable Import contact when contact already exist
    ui->actionImportContact->setEnabled(!EditMode && vCardVaild && !existContact);
  }
  else
  {
    //Don't check existContact flag when there are selected more contacts.
    //If some contact already exist there will be displayed message with containing list of ignored .vcf files
    ui->actionImportContact->setEnabled(anySelection && !EditMode && vCardVaild);
  }
  
}
void TSelectionHandle::popSelection()
{
	if (m_selectionStack.size() > 1)
		m_selectionStack.pop_back();
	TSelection *selection = getSelection();
	if (selection)
		selection->enableCommands();
}
void TFileAttachmentWidget::onRenameTriggered()
  {
  TSelection selection;
  RetrieveSelection(&selection);

  assert(selection.size() == 1 &&
    "Bad code in command update ui (onAttachementTableSelectionChanged)");

  AAttachmentItem* item = selection.front();
  ui->attachmentTable->editItem(item);
  }
void TFileAttachmentWidget::onOpenTriggered()
  {
  TSelection selection;
  RetrieveSelection(&selection);

  assert(selection.size() == 1 &&
    "Bad code in command update ui (onAttachementTableSelectionChanged)");

  const AAttachmentItem* item = selection.front();
  item->Open();
  }
void SceneViewerContextMenu::enterVectorImageGroup() {
  if (m_groupIndexToBeEntered == -1) return;

  TVectorImageP vi =
      (TVectorImageP)TTool::getImage(false);  // getCurrentImage();
  if (!vi) return;
  vi->enterGroup(m_groupIndexToBeEntered);
  TSelection *selection = TSelection::getCurrent();
  if (selection) selection->selectNone();
  m_viewer->update();
}
void TSelectionHandle::setSelection(TSelection *selection)
{
	if (getSelection() == selection)
		return;
	TSelection *oldSelection = getSelection();
	if (oldSelection) {
		oldSelection->selectNone();
		// disable selection related commands
		CommandManager *commandManager = CommandManager::instance();
		int i;
		for (i = 0; i < (int)m_enabledCommandIds.size(); i++)
			commandManager->setHandler(m_enabledCommandIds[i].c_str(), 0);
		m_enabledCommandIds.clear();
	}
	m_selectionStack.back() = selection;
	if (selection)
		selection->enableCommands();
	emit selectionSwitched(oldSelection, selection);
}
void TFileAttachmentWidget::onAddContactTriggered()
{
  TSelection selection;
  RetrieveSelection(&selection);

  assert(selection.size() == 1 &&
    "Bad code in command update ui (onAttachementTableSelectionChanged)");

  const AAttachmentItem* item = selection.front();

  QByteArray contactData;
  item->getContactData(contactData);
  assert (contactData.size() > 0);  

  std::list<Contact> contacts;
  Contact contact;
  ContactvCard vCard(contactData);
  // Public key can be invalid, but user can correct key in the "Add contact" dialog
  vCard.convert(&contact);
  contacts.push_back(contact);

  getKeyhoteeWindow()->addToContacts(false, contacts);
  getKeyhoteeWindow()->activateMainWindow();
}
bool TFileAttachmentWidget::saveAttachments()
{
  TSelection selection;
  RetrieveSelection(&selection);

  QString docPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

  if(selection.size() > 1)
  {
    /// If multiple items are selected ask for directory where files should be written to.
    QString storageDir = QFileDialog::getExistingDirectory(this,
      tr("Save selected file attachments..."), docPath, QFileDialog::ShowDirsOnly);
    if(storageDir.length() == 0)
      return false;
    QDir dir(storageDir);
    bool savedOK = true;
    for(const AAttachmentItem* attachmentItem : selection)
    {
      QFileInfo fi(dir, attachmentItem->GetDisplayedFileName());
      if (SaveAttachmentItem(attachmentItem, fi, true) == false)
        savedOK = false;
    }
    return savedOK;
  }
  else
  {
    /// If single item is selected just ask for direct path to save it
    QString filePath = QFileDialog::getSaveFileName(this, "Save attachment file...", QDir(docPath).filePath(selection.front()->GetDisplayedFileName()));
    if(filePath.length() == 0)
      return false;
    const AAttachmentItem* attachmentItem = selection.front();
    QFileInfo fi(filePath);
    /// Here we don't need to check for file overwrite - it was done by QFileDialog.
    return SaveAttachmentItem(attachmentItem, fi, false);
  }
}