1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.operations
;
21 import java
.util
.HashSet
;
22 import java
.util
.Iterator
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.oc_framework
.network
.webdav
.OnDatatransferProgressListener
;
27 import com
.owncloud
.android
.oc_framework
.network
.webdav
.WebdavClient
;
28 import com
.owncloud
.android
.oc_framework
.operations
.RemoteFile
;
29 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperation
;
30 import com
.owncloud
.android
.oc_framework
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.oc_framework
.operations
.remote
.DownloadRemoteFileOperation
;
32 import com
.owncloud
.android
.utils
.FileStorageUtils
;
33 import com
.owncloud
.android
.utils
.Log_OC
;
35 import android
.accounts
.Account
;
36 import android
.webkit
.MimeTypeMap
;
39 * Remote mDownloadOperation performing the download of a file to an ownCloud server
41 * @author David A. Velasco
44 public class DownloadFileOperation
extends RemoteOperation
{
46 private static final String TAG
= DownloadFileOperation
.class.getSimpleName();
48 private Account mAccount
;
50 private Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
51 private long mModificationTimestamp
= 0;
53 private DownloadRemoteFileOperation mDownloadOperation
;
56 public DownloadFileOperation(Account account
, OCFile file
) {
58 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
60 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
68 public Account
getAccount() {
72 public OCFile
getFile() {
76 public String
getSavePath() {
77 String path
= mFile
.getStoragePath(); // re-downloads should be done over the original file
78 if (path
!= null
&& path
.length() > 0) {
81 return FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, mFile
);
84 public String
getTmpPath() {
85 return FileStorageUtils
.getTemporalPath(mAccount
.name
) + mFile
.getRemotePath();
88 public String
getTmpFolder() {
89 return FileStorageUtils
.getTemporalPath(mAccount
.name
);
92 public String
getRemotePath() {
93 return mFile
.getRemotePath();
96 public String
getMimeType() {
97 String mimeType
= mFile
.getMimetype();
98 if (mimeType
== null
|| mimeType
.length() <= 0) {
100 mimeType
= MimeTypeMap
.getSingleton()
101 .getMimeTypeFromExtension(
102 mFile
.getRemotePath().substring(mFile
.getRemotePath().lastIndexOf('.') + 1));
103 } catch (IndexOutOfBoundsException e
) {
104 Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + mFile
.getRemotePath());
107 if (mimeType
== null
) {
108 mimeType
= "application/octet-stream";
113 public long getSize() {
114 return mFile
.getFileLength();
117 public long getModificationTimestamp() {
118 return (mModificationTimestamp
> 0) ? mModificationTimestamp
: mFile
.getModificationTimestamp();
122 protected RemoteOperationResult
run(WebdavClient client
) {
123 RemoteOperationResult result
= null
;
125 boolean moved
= true
;
127 /// download will be performed to a temporal file, then moved to the final location
128 File tmpFile
= new File(getTmpPath());
130 String tmpFolder
= getTmpFolder();
131 RemoteFile remoteFile
= FileStorageUtils
.fillRemoteFile(mFile
);
133 /// perform the download
134 mDownloadOperation
= new DownloadRemoteFileOperation(remoteFile
, tmpFolder
);
135 Iterator
<OnDatatransferProgressListener
> listener
= mDataTransferListeners
.iterator();
136 while (listener
.hasNext()) {
137 mDownloadOperation
.addDatatransferProgressListener(listener
.next());
139 result
= mDownloadOperation
.execute(client
);
141 if (result
.isSuccess()) {
142 newFile
= new File(getSavePath());
143 newFile
.getParentFile().mkdirs();
144 moved
= tmpFile
.renameTo(newFile
);
147 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.LOCAL_STORAGE_NOT_MOVED
);
149 Log_OC
.i(TAG
, "Download of " + mFile
.getRemotePath() + " to " + getSavePath() + ": " + result
.getLogMessage());
155 public void cancel() {
156 mDownloadOperation
.cancel();
160 public void addDatatransferProgressListener (OnDatatransferProgressListener listener
) {
161 synchronized (mDataTransferListeners
) {
162 mDataTransferListeners
.add(listener
);
166 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
167 synchronized (mDataTransferListeners
) {
168 mDataTransferListeners
.remove(listener
);