void TransfersSyncronizer::load(std::istream& is) {
  m_sync.load(is);

  StdInputStream inputStream(is);
  CryptoNote::BinaryInputStreamSerializer s(inputStream);
  uint32_t version = 0;

  s(version, "version");

  if (version > TRANSFERS_STORAGE_ARCHIVE_VERSION) {
    throw std::runtime_error("TransfersSyncronizer version mismatch");
  }


  struct ConsumerState {
    PublicKey viewKey;
    std::string state;
    std::vector<std::pair<AccountPublicAddress, std::string>> subscriptionStates;
  };

  std::vector<ConsumerState> updatedStates;

  try {
    size_t subscriptionCount = 0;
    s.beginArray(subscriptionCount, "consumers");

    while (subscriptionCount--) {
      s.beginObject("");
      PublicKey viewKey;
      s(viewKey, "view_key");

      std::string blob;
      s(blob, "state");

      auto subIter = m_consumers.find(viewKey);
      if (subIter != m_consumers.end()) {
        auto consumerState = m_sync.getConsumerState(subIter->second.get());
        assert(consumerState);

        {
          // store previous state
          auto prevConsumerState = getObjectState(*consumerState);
          // load consumer state
          setObjectState(*consumerState, blob);
          updatedStates.push_back(ConsumerState{ viewKey, std::move(prevConsumerState) });
        }

        // load subscriptions
        size_t subCount = 0;
        s.beginArray(subCount, "subscriptions");

        while (subCount--) {
          s.beginObject("");

          AccountPublicAddress acc;
          std::string state;

          s(acc, "address");
          s(state, "state");

          auto sub = subIter->second->getSubscription(acc);

          if (sub != nullptr) {
            auto prevState = getObjectState(sub->getContainer());
            setObjectState(sub->getContainer(), state);
            updatedStates.back().subscriptionStates.push_back(std::make_pair(acc, prevState));
          } else {
            m_logger(Logging::DEBUGGING) << "Subscription not found: " << m_currency.accountAddressAsString(acc);
          }

          s.endObject();
        }

        s.endArray();
      } else {
        m_logger(Logging::DEBUGGING) << "Consumer not found: " << viewKey;
      }

      s.endObject();
    }

    s.endArray();

  } catch (...) {
    // rollback state
    for (const auto& consumerState : updatedStates) {
      auto consumer = m_consumers.find(consumerState.viewKey)->second.get();
      setObjectState(*m_sync.getConsumerState(consumer), consumerState.state);
      for (const auto& sub : consumerState.subscriptionStates) {
        setObjectState(consumer->getSubscription(sub.first)->getContainer(), sub.second);
      }
    }
    throw;
  }

}
LocationItem *LocationContextDialog::exec()
{
	if (remoteHostItem)
	{
		// Inject values
		ui.remoteHost->setText(remoteHostItem->getRemoteHost());
		ui.autoConnection->setChecked(remoteHostItem->isAutoConnection());

		if (remoteHostItem->isStomp())
		{
			ui.remotePort->setCurrentIndex(StompIndex);
		}
		else if (remoteHostItem->isOpenWire())
		{
			ui.remotePort->setCurrentIndex(OpenWireIndex);
		}
		else if (remoteHostItem->isHttp())
		{
			ui.remotePort->setCurrentIndex(HttpIndex);
		}
		else if (remoteHostItem->isHttps())
		{
			ui.remotePort->setCurrentIndex(HttpsIndex);
		}
		else
		{
			ui.remotePort->setEditText(remoteHostItem->getRemotePort());
		}
	}

	if (subscriptionItem)
	{
		// Inject values
		if (subscriptionItem->isTopic())
		{
			ui.subscriptionType->setCurrentIndex(TopicIndex);
		}
		else if (subscriptionItem->isQueue())
		{
			ui.subscriptionType->setCurrentIndex(QueueIndex);
		}

		ui.subscription->setText(subscriptionItem->getSubscription());
		ui.autoSubscription->setCheckable(subscriptionItem->isAutoSubscription());
	}

	// Set enabled groups
	setRemoteHostGroupEnabled(remoteHostItem == 0);
	setSubscriptionGroupEnabled(remoteHostItem != 0);

	// Can the dialog be accepted?
	updateAcceptable();

	// Set focus
	if (managementRole == AddRemoteHost)
		ui.remoteHost->setFocus();
	else if (managementRole == AddSubscription)
		ui.subscription->setFocus();

	// Call the superclass exec method
	if (QDialog::exec() != QDialog::Accepted)
		return 0;

	// Create a new location item
	LocationItem *item = new LocationItem();
	item->setRemoteHost(ui.remoteHost->text());
	item->setRemotePort(getRemotePort());
	item->setAutoConnection(ui.autoConnection->isChecked());
	item->setSubscription(getSubscription());
	item->setAutoSubscription(ui.autoSubscription->isChecked());

	// Update the visible data
	item->setText(LocationItem::DescriptionColumn, item->getDisplayText());

	// Cleanup
	ui.autoConnection->setChecked(false);
	ui.remoteHost->setText(QString::null);
	ui.remotePort->setEditText(QString::null);
	ui.remotePort->setCurrentIndex(0);
	ui.autoSubscription->setChecked(false);
	ui.subscriptionType->setCurrentIndex(TopicIndex);
	ui.subscription->setText(QString::null);

	// Done.
	return item;
}