private boolean updateNotes() {
    ArrayList<AppNote> changed = mDbHelper.fetchNotes(NotesDbAdapter.CHANGED_NOTES_SELECTION);
    boolean allUpdated = true;

    for (AppNote appNote : changed) {
      try {
        link.update(appNote.getNote());
        mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true);
      } catch (LinkItemNotFound e) {
        e.printStackTrace();
        try {
          link.add(appNote.getNote());
          mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true);
        } catch (LibOperaLinkException e1) {
          e1.printStackTrace();
          allUpdated = false;
        }
      } catch (LinkAccessDeniedException e) {
        e.printStackTrace();
        isConnected = false;
        allUpdated = false;
      } catch (LibOperaLinkException e) {
        e.printStackTrace();
        allUpdated = false;
      }
    }

    return allUpdated;
  }
  /**
   * Obtains OAuth access tokens which can be used during entire communication process with Opera
   * Link server and invokes first synchronisation
   */
  public void grantAccess() {

    Uri uri = this.getIntent().getData();
    if (uri == null) {
      return;
    }

    String verifier = uri.getQueryParameter(LinkClient.OAUTH_VERIFIER);
    if (verifier == null) {
      return;
    }
    try {
      link.grantAccess(verifier);
      Log.i(SYNC_TAG, "Access granted");

      accessToken = link.getAccessToken();
      tokenSecret = link.getTokenSecret();
      Editor prefEditor = pref.edit();
      prefEditor.putString(ACCESS_TOKEN_PREF_KEY, accessToken);
      prefEditor.putString(TOKEN_SECRET_PREF_KEY, tokenSecret);

      prefEditor.commit();
      isConnected = true;

      syncItems();
    } catch (LinkAccessDeniedException e) {
      showToast("Access forbidden for access token " + accessToken);
      e.printStackTrace();
    } catch (LibOperaLinkException e) {
      e.printStackTrace();
    }
  }
 /**
  * Change notes at the server
  *
  * @return true if succeeded
  */
 private boolean uploadNewNotes() {
   ArrayList<AppNote> newNotes = mDbHelper.fetchNotes(NotesDbAdapter.NEW_NOTES_SELECTION);
   boolean allUpdated = true;
   try {
     for (AppNote appNote : newNotes) {
       link.add(appNote.getNote());
       mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true);
     }
   } catch (LinkAccessDeniedException e) {
     e.printStackTrace();
     allUpdated = false;
     isConnected = false;
   } catch (LibOperaLinkException e) {
     e.printStackTrace();
     allUpdated = false;
   }
   return allUpdated;
 }
  /**
   * Get new notes and updates from Opera Link server and insert them into database
   *
   * @return true if operation succeeded
   */
  private boolean getServerNotes() {
    ArrayList<NoteFolderEntry> notes = null;
    try {
      notes = link.getRootNotes(true);
    } catch (LinkAccessDeniedException e) {
      e.printStackTrace();
      isConnected = false;
      return false;
    } catch (LibOperaLinkException e) {
      e.printStackTrace();
      return false;
    }
    int newNotes = 0;
    ArrayList<Note> allNotes = new ArrayList<Note>();
    flattenNoteFolders(notes, allNotes);

    for (Note note : allNotes) {
      // try to get note from the database
      AppNote appNote = mDbHelper.fetchOperaNote(note.getId());
      if (appNote == null) { // no such note in database, add a new one
        ++newNotes;
        mDbHelper.importAndAddNote(note);
      } else if (!appNote.getNote().content.equals(note.content)) {
        // update already added note
        if (appNote.isSynced()) {
          mDbHelper.updateNote(appNote.getId(), note, true);
        } else { // note content conflicted, add new note leaving the
          // old one
          mDbHelper.createNote(note);
          // set that the old one is synchronised
          mDbHelper.updateNote(appNote.getId(), appNote.getNote(), true);
        }
      }
    }

    showToast(String.valueOf(newNotes) + " " + getString(R.string.got_notes));
    return true;
  }
  /**
   * Delete notes which were marked as those to be deleted, delete them also from the Opera Link
   * server
   *
   * @return true if deleting notes at the server succeeded
   */
  private boolean deleteNotes() {
    ArrayList<AppNote> toDelete = mDbHelper.fetchNotes(NotesDbAdapter.TO_DELETE_SELECTION);
    boolean allDeleted = true;
    for (AppNote appNote : toDelete) {
      try {
        if (appNote.getOpera_id() != null) {
          link.delete(appNote.getNote());
        }
        mDbHelper.deleteNote(appNote.getId());
      } catch (LinkItemNotFound e) {
        e.printStackTrace();
        mDbHelper.deleteNote(appNote.getId());
      } catch (LinkAccessDeniedException e) {
        e.printStackTrace();
        isConnected = false;
        allDeleted = false;
      } catch (LibOperaLinkException e) {
        e.printStackTrace();
        allDeleted = false;
      }
    }

    return allDeleted;
  }