Search in sources :

Example 11 with Repo

use of com.orgzly.android.repos.Repo in project orgzly-android by orgzly.

the class Shelf method renameBook.

public void renameBook(Book book, String name) throws IOException {
    String oldName = book.getName();
    /* Make sure there is no notebook with this name. */
    if (getBook(name) != null) {
        throw new IOException("Notebook with that name already exists");
    }
    /* Make sure link's repo is the same as sync book repo. */
    if (book.hasLink() && book.getLastSyncedToRook() != null) {
        if (!book.getLinkRepo().equals(book.getLastSyncedToRook().getRepoUri())) {
            String s = BookSyncStatus.ROOK_AND_VROOK_HAVE_DIFFERENT_REPOS.toString();
            setBookStatus(book, s, new BookAction(BookAction.Type.ERROR, s));
            return;
        }
    }
    /* Do not rename if there are local changes. */
    if (book.getLastSyncedToRook() != null) {
        if (book.isModifiedAfterLastSync()) {
            throw new IOException("Notebook is not synced");
        }
    }
    /* Prefer link. */
    if (book.getLastSyncedToRook() != null) {
        VersionedRook vrook = book.getLastSyncedToRook();
        Repo repo = RepoFactory.getFromUri(mContext, vrook.getRepoUri());
        VersionedRook movedVrook = repo.renameBook(vrook.getUri(), name);
        book.setLastSyncedToRook(movedVrook);
        BooksClient.saved(mContext, book.getId(), movedVrook);
    }
    if (BooksClient.updateName(mContext, book.getId(), name) != 1) {
        String msg = mContext.getString(R.string.failed_renaming_book);
        setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, msg));
        throw new IOException(msg);
    }
    setBookStatus(book, null, new BookAction(BookAction.Type.INFO, mContext.getString(R.string.renamed_book_from, oldName)));
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Repo(com.orgzly.android.repos.Repo) IOException(java.io.IOException)

Aggregations

Repo (com.orgzly.android.repos.Repo)11 VersionedRook (com.orgzly.android.repos.VersionedRook)5 IOException (java.io.IOException)4 Uri (android.net.Uri)3 DirectoryRepo (com.orgzly.android.repos.DirectoryRepo)2 OrgParsedFile (com.orgzly.org.parser.OrgParsedFile)2 File (java.io.File)2 Activity (android.app.Activity)1 AlertDialog (android.app.AlertDialog)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Configuration (android.content.res.Configuration)1 TypedArray (android.content.res.TypedArray)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 NonNull (android.support.annotation.NonNull)1 StringRes (android.support.annotation.StringRes)1