/** Need to remove failed uploaded keys from local storage if they can't be uploaded to github */
  private void getFailedKey(final String host) {
    service.getAllKeys(
        new AsyncRequestCallback<Array<KeyItem>>(
            dtoUnmarshallerFactory.newArrayUnmarshaller(KeyItem.class)) {
          @Override
          public void onSuccess(Array<KeyItem> result) {
            loader.hide(constant.loaderGetSshKeysMessage());
            for (int i = 0; i < result.size(); i++) {
              KeyItem key = result.get(i);
              if (key.getHost().equals(host)) {
                removeFailedKey(key);
                return;
              }
            }
            refreshKeys();
          }

          @Override
          public void onFailure(Throwable exception) {
            loader.hide(constant.loaderGetSshKeysMessage());
            refreshKeys();
            notificationManager.showError(exception.getMessage());
            eventBus.fireEvent(new ExceptionThrownEvent(exception));
          }
        });
  }
 /** {@inheritDoc} */
 @Override
 public void onGenerateGithubKeyClicked() {
   CurrentUser user = appContext.getCurrentUser();
   if (user != null && service.getSshKeyProviders().containsKey(GITHUB_HOST)) {
     generateGithubKey(user);
   } else {
     notificationManager.showError(constant.sshKeysProviderNotFound(GITHUB_HOST));
   }
 }
  /** Refresh ssh keys. */
  private void refreshKeys() {
    service.getAllKeys(
        new AsyncRequestCallback<Array<KeyItem>>(
            dtoUnmarshallerFactory.newArrayUnmarshaller(KeyItem.class)) {
          @Override
          public void onSuccess(Array<KeyItem> result) {
            loader.hide(constant.loaderGetSshKeysMessage());
            view.setKeys(result);
          }

          @Override
          public void onFailure(Throwable exception) {
            loader.hide(constant.loaderGetSshKeysMessage());
            notificationManager.showError(exception.getMessage());
            eventBus.fireEvent(new ExceptionThrownEvent(exception));
          }
        });
  }
  /**
   * Remove failed key.
   *
   * @param key failed key
   */
  private void removeFailedKey(@Nonnull final KeyItem key) {
    service.deleteKey(
        key,
        new AsyncRequestCallback<Void>() {
          @Override
          public void onFailure(Throwable caught) {
            loader.hide(constant.loaderDeleteSshKeyMessage(key.getHost()));
            notificationManager.showError(constant.deleteSshKeyFailed());
            refreshKeys();
          }

          @Override
          public void onSuccess(Void result) {
            loader.hide(constant.loaderDeleteSshKeyMessage(key.getHost()));
            refreshKeys();
          }
        });
  }
  private void generateGithubKey(CurrentUser user) {
    service
        .getSshKeyProviders()
        .get(GITHUB_HOST)
        .generateKey(
            user.getProfile().getId(),
            new AsyncCallback<Void>() {
              @Override
              public void onSuccess(Void result) {
                refreshKeys();
              }

              @Override
              public void onFailure(Throwable exception) {
                getFailedKey(GITHUB_HOST);
              }
            });
  }
  private void deleteKey(final KeyItem key) {
    service.deleteKey(
        key,
        new AsyncRequestCallback<Void>() {
          @Override
          public void onSuccess(Void result) {
            loader.hide(constant.loaderDeleteSshKeyMessage(key.getHost()));
            refreshKeys();
          }

          @Override
          public void onFailure(Throwable exception) {
            loader.hide(constant.loaderDeleteSshKeyMessage(key.getHost()));
            notificationManager.showError(
                SafeHtmlUtils.fromString(exception.getMessage()).asString());
            eventBus.fireEvent(new ExceptionThrownEvent(exception));
          }
        });
  }
  /** {@inheritDoc} */
  @Override
  public void onGenerateClicked() {
    String host = Window.prompt(constant.hostNameField(), "");
    if (!host.isEmpty()) {
      service.generateKey(
          host,
          new AsyncRequestCallback<Void>() {
            @Override
            protected void onSuccess(Void result) {
              refreshKeys();
            }

            @Override
            protected void onFailure(Throwable exception) {
              notificationManager.showError(
                  SafeHtmlUtils.fromString(exception.getMessage()).asString());
              eventBus.fireEvent(new ExceptionThrownEvent(exception));
            }
          });
    }
  }
  /** {@inheritDoc} */
  @Override
  public void onViewClicked(@Nonnull final KeyItem key) {
    service.getPublicKey(
        key,
        new AsyncRequestCallback<PublicKey>(
            dtoUnmarshallerFactory.newUnmarshaller(PublicKey.class)) {
          @Override
          public void onSuccess(PublicKey result) {
            loader.hide(constant.loaderGetPublicSshKeyMessage(key.getHost()));
            dialogFactory
                .createMessageDialog(
                    constant.publicSshKeyField() + key.getHost(), result.getKey(), null)
                .show();
          }

          @Override
          public void onFailure(Throwable exception) {
            loader.hide(constant.loaderGetPublicSshKeyMessage(key.getHost()));
            notificationManager.showError(
                SafeHtmlUtils.fromString(exception.getMessage()).asString());
            eventBus.fireEvent(new ExceptionThrownEvent(exception));
          }
        });
  }