Merge pull request #330 from LukeOwncloud/update_jackrabbit
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / DownloadFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.operations;
19
20 import java.io.File;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Set;
24
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;
34
35 import android.accounts.Account;
36 import android.webkit.MimeTypeMap;
37
38 /**
39 * Remote mDownloadOperation performing the download of a file to an ownCloud server
40 *
41 * @author David A. Velasco
42 * @author masensio
43 */
44 public class DownloadFileOperation extends RemoteOperation {
45
46 private static final String TAG = DownloadFileOperation.class.getSimpleName();
47
48 private Account mAccount;
49 private OCFile mFile;
50 private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
51 private long mModificationTimestamp = 0;
52
53 private DownloadRemoteFileOperation mDownloadOperation;
54
55
56 public DownloadFileOperation(Account account, OCFile file) {
57 if (account == null)
58 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
59 if (file == null)
60 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
61
62 mAccount = account;
63 mFile = file;
64
65 }
66
67
68 public Account getAccount() {
69 return mAccount;
70 }
71
72 public OCFile getFile() {
73 return mFile;
74 }
75
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) {
79 return path;
80 }
81 return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
82 }
83
84 public String getTmpPath() {
85 return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
86 }
87
88 public String getTmpFolder() {
89 return FileStorageUtils.getTemporalPath(mAccount.name);
90 }
91
92 public String getRemotePath() {
93 return mFile.getRemotePath();
94 }
95
96 public String getMimeType() {
97 String mimeType = mFile.getMimetype();
98 if (mimeType == null || mimeType.length() <= 0) {
99 try {
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());
105 }
106 }
107 if (mimeType == null) {
108 mimeType = "application/octet-stream";
109 }
110 return mimeType;
111 }
112
113 public long getSize() {
114 return mFile.getFileLength();
115 }
116
117 public long getModificationTimestamp() {
118 return (mModificationTimestamp > 0) ? mModificationTimestamp : mFile.getModificationTimestamp();
119 }
120
121 @Override
122 protected RemoteOperationResult run(WebdavClient client) {
123 RemoteOperationResult result = null;
124 File newFile = null;
125 boolean moved = true;
126
127 /// download will be performed to a temporal file, then moved to the final location
128 File tmpFile = new File(getTmpPath());
129
130 String tmpFolder = getTmpFolder();
131 RemoteFile remoteFile = FileStorageUtils.fillRemoteFile(mFile);
132
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());
138 }
139 result = mDownloadOperation.execute(client);
140
141 if (result.isSuccess()) {
142 newFile = new File(getSavePath());
143 newFile.getParentFile().mkdirs();
144 moved = tmpFile.renameTo(newFile);
145
146 if (!moved)
147 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
148 }
149 Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
150
151
152 return result;
153 }
154
155 public void cancel() {
156 mDownloadOperation.cancel();
157 }
158
159
160 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
161 synchronized (mDataTransferListeners) {
162 mDataTransferListeners.add(listener);
163 }
164 }
165
166 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
167 synchronized (mDataTransferListeners) {
168 mDataTransferListeners.remove(listener);
169 }
170 }
171
172 }