private void onRenameFileOperationFinish(
      RenameFileOperation operation, RemoteOperationResult result) {
    boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
    getActivity()
        .dismissDialog(
            (inDisplayActivity)
                ? FileDisplayActivity.DIALOG_SHORT_WAIT
                : FileDetailActivity.DIALOG_SHORT_WAIT);

    if (result.isSuccess()) {
      updateFileDetails(((RenameFileOperation) operation).getFile(), mAccount);
      mContainerActivity.onFileStateChanged();

    } else {
      if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
        Toast msg =
            Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG);
        msg.show();
        // TODO throw again the new rename dialog
      } else {
        Toast msg =
            Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG);
        msg.show();
        if (result.isSslRecoverableException()) {
          // TODO show the SSL warning dialog
        }
      }
    }
  }
  private void onSynchronizeFileOperationFinish(
      SynchronizeFileOperation operation, RemoteOperationResult result) {
    boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
    getActivity()
        .dismissDialog(
            (inDisplayActivity)
                ? FileDisplayActivity.DIALOG_SHORT_WAIT
                : FileDetailActivity.DIALOG_SHORT_WAIT);

    if (!result.isSuccess()) {
      if (result.getCode() == ResultCode.SYNC_CONFLICT) {
        Intent i = new Intent(getActivity(), ConflictsResolveActivity.class);
        i.putExtra(ConflictsResolveActivity.EXTRA_FILE, mFile);
        i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount);
        startActivity(i);

      } else {
        Toast msg = Toast.makeText(getActivity(), R.string.sync_file_fail_msg, Toast.LENGTH_LONG);
        msg.show();
      }

      if (mFile.isDown()) {
        setButtonsForDown();

      } else {
        setButtonsForRemote();
      }

    } else {
      if (operation.transferWasRequested()) {
        setButtonsForTransferring();
        mContainerActivity
            .onFileStateChanged(); // this is not working; FileDownloader won't do NOTHING at all
                                   // until this method finishes, so
        // checking the service to see if the file is downloading results in FALSE
      } else {
        Toast msg =
            Toast.makeText(getActivity(), R.string.sync_file_nothing_to_do_msg, Toast.LENGTH_LONG);
        msg.show();
        if (mFile.isDown()) {
          setButtonsForDown();

        } else {
          setButtonsForRemote();
        }
      }
    }
  }
  private boolean tryConnection(WebdavClient wc, String urlSt) {
    boolean retval = false;
    GetMethod get = null;
    try {
      get = new GetMethod(urlSt);
      int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
      String response = get.getResponseBodyAsString();
      if (status == HttpStatus.SC_OK) {
        JSONObject json = new JSONObject(response);
        if (!json.getBoolean("installed")) {
          mLatestResult =
              new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
        } else {
          mOCVersion = new OwnCloudVersion(json.getString("version"));
          if (!mOCVersion.isVersionValid()) {
            mLatestResult =
                new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);

          } else {
            mLatestResult =
                new RemoteOperationResult(
                    urlSt.startsWith("https://")
                        ? RemoteOperationResult.ResultCode.OK_SSL
                        : RemoteOperationResult.ResultCode.OK_NO_SSL);

            retval = true;
          }
        }

      } else {
        mLatestResult = new RemoteOperationResult(false, status, get.getResponseHeaders());
      }

    } catch (JSONException e) {
      mLatestResult =
          new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);

    } catch (Exception e) {
      mLatestResult = new RemoteOperationResult(e);

    } finally {
      if (get != null) get.releaseConnection();
    }

    if (mLatestResult.isSuccess()) {
      Log_OC.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());

    } else if (mLatestResult.getException() != null) {
      Log_OC.e(
          TAG,
          "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(),
          mLatestResult.getException());

    } else {
      Log_OC.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
    }

    return retval;
  }
  private void onRemoveFileOperationFinish(
      RemoveFileOperation operation, RemoteOperationResult result) {
    boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
    getActivity()
        .dismissDialog(
            (inDisplayActivity)
                ? FileDisplayActivity.DIALOG_SHORT_WAIT
                : FileDetailActivity.DIALOG_SHORT_WAIT);

    if (result.isSuccess()) {
      Toast msg =
          Toast.makeText(
              getActivity().getApplicationContext(),
              R.string.remove_success_msg,
              Toast.LENGTH_LONG);
      msg.show();
      if (inDisplayActivity) {
        // double pane
        FragmentTransaction transaction =
            getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(
            R.id.file_details_container,
            new FileDetailFragment(null, null)); // empty FileDetailFragment
        transaction.commit();
        mContainerActivity.onFileStateChanged();
      } else {
        getActivity().finish();
      }

    } else {
      Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG);
      msg.show();
      if (result.isSslRecoverableException()) {
        // TODO show the SSL warning dialog
      }
    }
  }
  @Override
  protected RemoteOperationResult run(WebdavClient client) {
    if (!isOnline()) {
      return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
    }
    if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
      tryConnection(client, mUrl + AccountUtils.STATUS_PATH);

    } else {
      client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
      boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
      if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
        Log_OC.d(TAG, "establishing secure connection failed, trying non secure connection");
        client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
        tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
      }
    }
    return mLatestResult;
  }
  @Override
  protected RemoteOperationResult run(WebdavClient client) {
    RemoteOperationResult result = null;

    // code before in FileSyncAdapter.fetchData
    PropFindMethod query = null;
    try {
      Log.d(TAG, "Synchronizing " + mAccount.name + ", fetching files in " + mRemotePath);

      // remote request
      query = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
      int status = client.executeMethod(query);

      // check and process response   - /// TODO take into account all the possible status per
      // child-resource
      if (isMultiStatus(status)) {
        MultiStatus resp = query.getResponseBodyAsMultiStatus();

        // synchronize properties of the parent folder, if necessary
        if (mParentId == DataStorageManager.ROOT_PARENT_ID) {
          WebdavEntry we = new WebdavEntry(resp.getResponses()[0], client.getBaseUri().getPath());
          OCFile parent = fillOCFile(we);
          parent.setParentId(mParentId);
          mStorageManager.saveFile(parent);
          mParentId = parent.getFileId();
        }

        // read contents in folder
        List<OCFile> updatedFiles = new Vector<OCFile>(resp.getResponses().length - 1);
        for (int i = 1; i < resp.getResponses().length; ++i) {
          WebdavEntry we = new WebdavEntry(resp.getResponses()[i], client.getBaseUri().getPath());
          OCFile file = fillOCFile(we);
          file.setParentId(mParentId);
          OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
          if (oldFile != null) {
            if (oldFile.keepInSync()
                && file.getModificationTimestamp() > oldFile.getModificationTimestamp()) {
              disableObservance(
                  file); // first disable observer so we won't get file upload right after download
              requestContentDownload(file);
            }
            file.setKeepInSync(oldFile.keepInSync());
          }

          updatedFiles.add(file);
        }

        // save updated contents in local database; all at once, trying to get a best performance in
        // database update (not a big deal, indeed)
        mStorageManager.saveFiles(updatedFiles);

        // removal of obsolete files
        mChildren = mStorageManager.getDirectoryContent(mStorageManager.getFileById(mParentId));
        OCFile file;
        String currentSavePath = FileDownloader.getSavePath(mAccount.name);
        for (int i = 0; i < mChildren.size(); ) {
          file = mChildren.get(i);
          if (file.getLastSyncDate() != mCurrentSyncTime) {
            Log.d(TAG, "removing file: " + file);
            mStorageManager.removeFile(
                file, (file.isDown() && file.getStoragePath().startsWith(currentSavePath)));
            mChildren.remove(i);
          } else {
            i++;
          }
        }

      } else {
        client.exhaustResponse(query.getResponseBodyAsStream());
      }

      // prepare result object
      result = new RemoteOperationResult(isMultiStatus(status), status);
      Log.i(
          TAG,
          "Synchronizing "
              + mAccount.name
              + ", folder "
              + mRemotePath
              + ": "
              + result.getLogMessage());

    } catch (Exception e) {
      result = new RemoteOperationResult(e);
      Log.e(
          TAG,
          "Synchronizing "
              + mAccount.name
              + ", folder "
              + mRemotePath
              + ": "
              + result.getLogMessage(),
          result.getException());

    } finally {
      if (query != null)
        query.releaseConnection(); // let the connection available for other methods
    }

    return result;
  }
  @Override
  protected RemoteOperationResult run(WebdavClient client) {
    AccountManager accountMngr = AccountManager.get(mContext);
    String statUrl = accountMngr.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
    statUrl += AccountUtils.STATUS_PATH;
    RemoteOperationResult result = null;
    GetMethod get = null;
    try {
      get = new GetMethod(statUrl);
      int status = client.executeMethod(get);
      if (status != HttpStatus.SC_OK) {
        client.exhaustResponse(get.getResponseBodyAsStream());
        result = new RemoteOperationResult(false, status);

      } else {
        String response = get.getResponseBodyAsString();
        if (response != null) {
          JSONObject json = new JSONObject(response);
          if (json != null && json.getString("version") != null) {
            OwnCloudVersion ocver = new OwnCloudVersion(json.getString("version"));
            if (ocver.isVersionValid()) {
              accountMngr.setUserData(
                  mAccount, AccountAuthenticator.KEY_OC_VERSION, ocver.toString());
              Log.d(TAG, "Got new OC version " + ocver.toString());
              result = new RemoteOperationResult(ResultCode.OK);

            } else {
              Log.w(
                  TAG, "Invalid version number received from server: " + json.getString("version"));
              result = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
            }
          }
        }
        if (result == null) {
          result =
              new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
        }
      }
      Log.i(
          TAG,
          "Check for update of ownCloud server version at "
              + client.getBaseUri()
              + ": "
              + result.getLogMessage());

    } catch (JSONException e) {
      result = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
      Log.e(
          TAG,
          "Check for update of ownCloud server version at "
              + client.getBaseUri()
              + ": "
              + result.getLogMessage(),
          e);

    } catch (Exception e) {
      result = new RemoteOperationResult(e);
      Log.e(
          TAG,
          "Check for update of ownCloud server version at "
              + client.getBaseUri()
              + ": "
              + result.getLogMessage(),
          e);

    } finally {
      if (get != null) get.releaseConnection();
    }
    return result;
  }