use of com.orgzly.android.BookAction in project orgzly-android by orgzly.
the class BooksClient method fromCursor.
public static Book fromCursor(Cursor cursor) {
Book book = new Book(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.NAME)), cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.PREFACE)), cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.MTIME)), cursor.getInt(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.IS_DUMMY)) == 1);
book.getOrgFileSettings().setTitle(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.TITLE)));
book.getOrgFileSettings().setIndented(cursor.getInt(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.IS_INDENTED)) == 1);
book.setId(cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param._ID)));
book.setSyncStatus(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNC_STATUS)));
book.setDetectedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.DETECTED_ENCODING)));
book.setSelectedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SELECTED_ENCODING)));
book.setUsedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.USED_ENCODING)));
/* Set link. */
int linkRepoUriColumn = cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LINK_REPO_URL);
if (!cursor.isNull(linkRepoUriColumn)) {
Uri uri = Uri.parse(cursor.getString(linkRepoUriColumn));
book.setLinkRepo(uri);
}
/* Set versioned rook. */
if (!cursor.isNull(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_URL))) {
Uri syncRookUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_URL)));
Uri syncRepoUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_REPO_URL)));
String rev = cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_REVISION));
long mtime = cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_MTIME));
VersionedRook vrook = new VersionedRook(syncRepoUri, syncRookUri, rev, mtime);
book.setLastSyncedToRook(vrook);
}
/* Set last action. */
String lastActionMessage = cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION));
if (!TextUtils.isEmpty(lastActionMessage)) {
BookAction.Type lastActionType = BookAction.Type.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION_TYPE)));
long lastActionTimestamp = cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION_TIMESTAMP));
BookAction lastAction = new BookAction(lastActionType, lastActionMessage, lastActionTimestamp);
book.setLastAction(lastAction);
}
return book;
}
use of com.orgzly.android.BookAction in project orgzly-android by orgzly.
the class SyncFragment method forceSaveBook.
/**
* Saves book to its linked remote book, or to the one-and-only repository .
*/
@SuppressLint("StaticFieldLeak")
public void forceSaveBook(final long bookId) {
new AsyncTask<Void, Void, Object>() {
@Override
protected Object doInBackground(Void... params) {
Book book = BooksClient.get(getActivity(), bookId);
if (book != null) {
try {
String repoUrl;
String fileName;
/* Prefer link. */
if (book.hasLink()) {
repoUrl = book.getLinkRepo().toString();
} else {
repoUrl = repoForSavingBook();
}
fileName = BookName.getFileName(getContext(), book);
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.PROGRESS, resources.getString(R.string.force_saving_to_uri, repoUrl)));
return mShelf.saveBookToRepo(repoUrl, fileName, book, BookName.Format.ORG);
} catch (Exception e) {
e.printStackTrace();
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, resources.getString(R.string.force_saving_failed, e.getLocalizedMessage())));
return e;
}
} else {
return new IOException(resources.getString(R.string.book_does_not_exist_anymore));
}
}
@Override
protected void onPostExecute(Object result) {
if (mListener != null) {
if (result instanceof Book) {
Book book = (Book) result;
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.INFO, resources.getString(R.string.force_saved_to_uri, book.getLastSyncedToRook().getUri())));
mListener.onBookSaved(book);
} else {
mListener.onBookForceSavingFailed((Exception) result);
}
} else {
Log.w(TAG, "Listener not set, not handling saveBookToRepo result");
}
}
}.execute();
}
use of com.orgzly.android.BookAction in project orgzly-android by orgzly.
the class SyncFragment method renameBook.
@SuppressLint("StaticFieldLeak")
public void renameBook(final Book book, final String value) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
mShelf.renameBook(book, value);
} catch (Exception e) {
e.printStackTrace();
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, resources.getString(R.string.failed_renaming_book_with_reason, e.getLocalizedMessage())));
// return e;
}
return null;
}
}.execute();
}
use of com.orgzly.android.BookAction in project orgzly-android by orgzly.
the class SyncFragment method forceLoadBook.
/**
* Load book from repository.
*/
@SuppressLint("StaticFieldLeak")
public void forceLoadBook(final long bookId) {
new AsyncTask<Void, Object, Object>() {
@Override
protected Object doInBackground(Void... params) {
Book book = BooksClient.get(getActivity(), bookId);
try {
if (book == null) {
throw new IOException(resources.getString(R.string.book_does_not_exist_anymore));
}
if (book.getLinkRepo() == null) {
throw new IOException(resources.getString(R.string.message_book_has_no_link));
}
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.PROGRESS, resources.getString(R.string.force_loading_from_uri, book.getLinkRepo())));
return mShelf.loadBookFromRepo(book.getLinkRepo(), BookName.getFileName(getContext(), book));
} catch (Exception e) {
e.printStackTrace();
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, resources.getString(R.string.force_loading_failed, e.getLocalizedMessage())));
return e;
}
}
@Override
protected void onPostExecute(Object result) {
if (mListener != null) {
if (result instanceof Book) {
Book book = (Book) result;
// TODO: Do in bg
mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.INFO, resources.getString(R.string.force_loaded_from_uri, book.getLastSyncedToRook().getUri())));
}
}
}
}.execute();
}
Aggregations