Skip to content

Commit

Permalink
Export QIF as zip files so as to include multiple files generated for…
Browse files Browse the repository at this point in the history
… multicurrency transactions
  • Loading branch information
codinguser committed Apr 25, 2017
1 parent 7132435 commit 5630786
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
39 changes: 29 additions & 10 deletions app/src/main/java/org/gnucash/android/export/ExportAsyncTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.gnucash.android.export;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
Expand All @@ -27,7 +26,6 @@
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
Expand Down Expand Up @@ -79,6 +77,8 @@
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* Asynchronous task for exporting transactions.
Expand Down Expand Up @@ -280,21 +280,38 @@ private void moveExportToUri() throws Exporter.ExporterException {
return;
}

//we only support exporting to a single file
String exportedFile = mExportedFiles.get(0);
try {
moveFile(exportedFile, mContext.getContentResolver().openOutputStream(exportUri));
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Error moving export file to: " + exportUri);
Crashlytics.logException(e);
if (mExportedFiles.size() > 0){
try {
OutputStream outputStream = mContext.getContentResolver().openOutputStream(exportUri);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
byte[] buffer = new byte[1024];
for (String exportedFile : mExportedFiles) {
File file = new File(exportedFile);
FileInputStream fileInputStream = new FileInputStream(file);
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));

int length;
while ((length = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.closeEntry();
fileInputStream.close();
}
zipOutputStream.close();
} catch (IOException ex) {
Log.e(TAG, "Error when zipping QIF files for export");
ex.printStackTrace();
Crashlytics.logException(ex);
}
}
}

/**
* Move the exported files to a GnuCash folder on Google Drive
* @throws Exporter.ExporterException
* @deprecated Explicit Google Drive integration is deprecated, use Storage Access Framework. See {@link #moveExportToUri()}
*/
@Deprecated
private void moveExportToGoogleDrive() throws Exporter.ExporterException {
Log.i(TAG, "Moving exported file to Google Drive");
final GoogleApiClient googleApiClient = BackupPreferenceFragment.getGoogleApiClient(GnuCashApplication.getAppContext());
Expand Down Expand Up @@ -421,7 +438,9 @@ private void moveExportToOwnCloud() throws Exporter.ExporterException {
* Moves the exported files from the internal storage where they are generated to
* external storage, which is accessible to the user.
* @return The list of files moved to the SD card.
* @deprecated Use the Storage Access Framework to save to SD card. See {@link #moveExportToUri()}
*/
@Deprecated
private List<String> moveExportToSDCard() throws Exporter.ExporterException {
Log.i(TAG, "Moving exported file to external storage");
new File(Exporter.getExportFolderPath(mExporter.mBookUID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,17 @@ private void selectExportFile() {
Intent createIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
createIntent.setType("text/*").addCategory(Intent.CATEGORY_OPENABLE);
String bookName = BooksDbAdapter.getInstance().getActiveBookDisplayName();
createIntent.putExtra(Intent.EXTRA_TITLE, Exporter.buildExportFilename(mExportFormat, bookName));

if (mExportFormat == ExportFormat.XML || mExportFormat == ExportFormat.QIF) {
createIntent.setType("application/zip");
}

String filename = Exporter.buildExportFilename(mExportFormat, bookName);
if (mExportTarget == ExportParams.ExportTarget.URI && mExportFormat == ExportFormat.QIF){
filename += ".zip";
}

createIntent.putExtra(Intent.EXTRA_TITLE, filename);
startActivityForResult(createIntent, REQUEST_EXPORT_FILE);
}

Expand Down

0 comments on commit 5630786

Please sign in to comment.