Search in sources :

Example 11 with VersionedRook

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

the class Shelf method loadBookFromRepo.

public Book loadBookFromRepo(Uri repoUri, String fileName) throws IOException {
    Book book;
    Repo repo = RepoFactory.getFromUri(mContext, repoUri);
    if (repo == null) {
        throw new IOException("Unsupported repository URL \"" + repoUri + "\"");
    }
    File tmpFile = getTempBookFile();
    try {
        /* Download from repo. */
        VersionedRook vrook = repo.retrieveBook(fileName, tmpFile);
        BookName bookName = BookName.fromFileName(fileName);
        /* Store from file to Shelf. */
        book = loadBookFromFile(bookName.getName(), bookName.getFormat(), tmpFile, vrook);
    } finally {
        tmpFile.delete();
    }
    return book;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Repo(com.orgzly.android.repos.Repo) IOException(java.io.IOException) File(java.io.File) OrgParsedFile(com.orgzly.org.parser.OrgParsedFile)

Example 12 with VersionedRook

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

the class LocalDbRepoClient method getAll.

/**
 * Select only those belonging to this repo's name.
 */
public static List<VersionedRook> getAll(Context context, Uri repoUri) {
    Cursor cursor = context.getContentResolver().query(ProviderContract.LocalDbRepo.ContentUri.dbRepos(), null, ProviderContract.LocalDbRepo.Param.REPO_URL + "=?", new String[] { repoUri.toString() }, null);
    List<VersionedRook> result = new ArrayList<>();
    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.URL)));
            String revision = cursor.getString(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.REVISION));
            long mtime = cursor.getLong(cursor.getColumnIndex(ProviderContract.LocalDbRepo.Param.MTIME));
            result.add(new VersionedRook(repoUri, uri, revision, mtime));
        }
    } finally {
        cursor.close();
    }
    return result;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 13 with VersionedRook

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

the class Shelf method groupAllNotebooksByName.

/**
 * Compares every local book with every remote one and calculates the status for each link.
 *
 * @return number of links (unique book names)
 * @throws IOException
 */
public Map<String, BookNamesake> groupAllNotebooksByName() throws IOException {
    if (BuildConfig.LOG_DEBUG)
        LogUtils.d(TAG, "Collecting all local and remote books ...");
    Map<String, Repo> repos = ReposClient.getAll(mContext);
    List<Book> localBooks = getBooks();
    List<VersionedRook> versionedRooks = getBooksFromAllRepos(repos);
    /* Group local and remote books by name. */
    Map<String, BookNamesake> namesakes = BookNamesake.getAll(mContext, localBooks, versionedRooks);
    /* If there is no local book, create empty "dummy" one. */
    for (BookNamesake namesake : namesakes.values()) {
        if (namesake.getBook() == null) {
            Book book = createDummyBook(namesake.getName());
            namesake.setBook(book);
        }
        namesake.updateStatus(repos.size());
    }
    return namesakes;
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) Repo(com.orgzly.android.repos.Repo) BookNamesake(com.orgzly.android.sync.BookNamesake)

Example 14 with VersionedRook

use of com.orgzly.android.repos.VersionedRook 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)

Example 15 with VersionedRook

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

the class ShelfTestUtils method setupRook.

/**
 * Overwrites existing repoUrl / url combinations (due to table definition).
 */
public void setupRook(String repoUrl, String url, String content, String rev, long mtime) {
    try {
        VersionedRook vrook = new VersionedRook(Uri.parse(repoUrl), Uri.parse(url), rev, mtime);
        LocalDbRepoClient.insert(context, vrook, content);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.toString());
    }
// RemoteBookRevision remoteBookRevision = new RemoteBookRevision(repoUrl, url, rev, mtime);
// RemoteBooksHelper.updateOrInsert(testContext, remoteBookRevision);
}
Also used : VersionedRook(com.orgzly.android.repos.VersionedRook) IOException(java.io.IOException)

Aggregations

VersionedRook (com.orgzly.android.repos.VersionedRook)15 Repo (com.orgzly.android.repos.Repo)5 IOException (java.io.IOException)5 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 Book (com.orgzly.android.Book)3 ArrayList (java.util.ArrayList)3 OrgParsedFile (com.orgzly.org.parser.OrgParsedFile)2 File (java.io.File)2 HashMap (java.util.HashMap)2 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 OperationApplicationException (android.content.OperationApplicationException)1 RemoteException (android.os.RemoteException)1 BookAction (com.orgzly.android.BookAction)1 OrgzlyTest (com.orgzly.android.OrgzlyTest)1 BookNamesake (com.orgzly.android.sync.BookNamesake)1 CircularArrayList (com.orgzly.android.util.CircularArrayList)1 Test (org.junit.Test)1