@Override
  public void onDestroy() {
    Log_OC.v(TAG, "Destroying service");
    // Saving cookies
    try {
      OwnCloudClientManagerFactory.getDefaultSingleton()
          .saveAllClients(this, MainApp.getAccountType());

      // TODO - get rid of these exceptions
    } catch (AccountNotFoundException e) {
      e.printStackTrace();
    } catch (AuthenticatorException e) {
      e.printStackTrace();
    } catch (OperationCanceledException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    mUndispatchedFinishedOperations.clear();

    mOperationsBinder = null;

    mOperationsHandler.getLooper().quit();
    mOperationsHandler = null;

    mSyncFolderHandler.getLooper().quit();
    mSyncFolderHandler = null;

    super.onDestroy();
  }
    /** Performs the next operation in the queue */
    private void nextOperation() {

      // Log_OC.e(TAG, "nextOperation init" );

      Pair<Target, RemoteOperation> next = null;
      synchronized (mPendingOperations) {
        next = mPendingOperations.peek();
      }

      if (next != null) {

        mCurrentOperation = next.second;
        RemoteOperationResult result = null;
        try {
          /// prepare client object to send the request to the ownCloud server
          if (mLastTarget == null || !mLastTarget.equals(next.first)) {
            mLastTarget = next.first;
            if (mLastTarget.mAccount != null) {
              OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount, mService);
              mOwnCloudClient =
                  OwnCloudClientManagerFactory.getDefaultSingleton()
                      .getClientFor(ocAccount, mService);

              OwnCloudVersion version =
                  com.owncloud.android.authentication.AccountUtils.getServerVersion(
                      mLastTarget.mAccount);
              mOwnCloudClient.setOwnCloudVersion(version);

              mStorageManager =
                  new FileDataStorageManager(mLastTarget.mAccount, mService.getContentResolver());
            } else {
              OwnCloudCredentials credentials = null;
              if (mLastTarget.mCookie != null && mLastTarget.mCookie.length() > 0) {
                // just used for GetUserName
                // TODO refactor to run GetUserName as AsyncTask in the context of
                // AuthenticatorActivity
                credentials =
                    OwnCloudCredentialsFactory.newSamlSsoCredentials(
                        null, // unknown
                        mLastTarget.mCookie); // SAML SSO
              }
              OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mServerUrl, credentials);
              mOwnCloudClient =
                  OwnCloudClientManagerFactory.getDefaultSingleton()
                      .getClientFor(ocAccount, mService);
              mStorageManager = null;
            }
          }

          /// perform the operation
          if (mCurrentOperation instanceof SyncOperation) {
            result = ((SyncOperation) mCurrentOperation).execute(mOwnCloudClient, mStorageManager);
          } else {
            result = mCurrentOperation.execute(mOwnCloudClient);
          }

        } catch (AccountsException e) {
          if (mLastTarget.mAccount == null) {
            Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e);
          } else {
            Log_OC.e(
                TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e);
          }
          result = new RemoteOperationResult(e);

        } catch (IOException e) {
          if (mLastTarget.mAccount == null) {
            Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e);
          } else {
            Log_OC.e(
                TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e);
          }
          result = new RemoteOperationResult(e);
        } catch (Exception e) {
          if (mLastTarget.mAccount == null) {
            Log_OC.e(TAG, "Unexpected error for a NULL account", e);
          } else {
            Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e);
          }
          result = new RemoteOperationResult(e);

        } finally {
          synchronized (mPendingOperations) {
            mPendingOperations.poll();
          }
        }

        // sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
        mService.dispatchResultToOperationListeners(mCurrentOperation, result);
      }
    }