Merge pull request #354 from LukeOwncloud/added_more_git_notes_to_setup_md
[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.lib.network.OnDatatransferProgressListener;
27 import com.owncloud.android.lib.network.OwnCloudClient;
28 import com.owncloud.android.lib.operations.common.RemoteOperation;
29 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
30 import com.owncloud.android.lib.operations.remote.DownloadRemoteFileOperation;
31 import com.owncloud.android.utils.FileStorageUtils;
32 import com.owncloud.android.utils.Log_OC;
33
34 import android.accounts.Account;
35 import android.webkit.MimeTypeMap;
36
37 /**
38 * Remote mDownloadOperation performing the download of a file to an ownCloud server
39 *
40 * @author David A. Velasco
41 * @author masensio
42 */
43 public class DownloadFileOperation extends RemoteOperation {
44
45 private static final String TAG = DownloadFileOperation.class.getSimpleName();
46
47 private Account mAccount;
48 private OCFile mFile;
49 private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
50 private long mModificationTimestamp = 0;
51
52 private DownloadRemoteFileOperation mDownloadOperation;
53
54
55 public DownloadFileOperation(Account account, OCFile file) {
56 if (account == null)
57 throw new IllegalArgumentException("Illegal null account in DownloadFileOperation creation");
58 if (file == null)
59 throw new IllegalArgumentException("Illegal null file in DownloadFileOperation creation");
60
61 mAccount = account;
62 mFile = file;
63
64 }
65
66
67 public Account getAccount() {
68 return mAccount;
69 }
70
71 public OCFile getFile() {
72 return mFile;
73 }
74
75 public String getSavePath() {
76 String path = mFile.getStoragePath(); // re-downloads should be done over the original file
77 if (path != null && path.length() > 0) {
78 return path;
79 }
80 return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
81 }
82
83 public String getTmpPath() {
84 return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
85 }
86
87 public String getTmpFolder() {
88 return FileStorageUtils.getTemporalPath(mAccount.name);
89 }
90
91 public String getRemotePath() {
92 return mFile.getRemotePath();
93 }
94
95 public String getMimeType() {
96 String mimeType = mFile.getMimetype();
97 if (mimeType == null || mimeType.length() <= 0) {
98 try {
99 mimeType = MimeTypeMap.getSingleton()
100 .getMimeTypeFromExtension(
101 mFile.getRemotePath().substring(mFile.getRemotePath().lastIndexOf('.') + 1));
102 } catch (IndexOutOfBoundsException e) {
103 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + mFile.getRemotePath());
104 }
105 }
106 if (mimeType == null) {
107 mimeType = "application/octet-stream";
108 }
109 return mimeType;
110 }
111
112 public long getSize() {
113 return mFile.getFileLength();
114 }
115
116 public long getModificationTimestamp() {
117 return (mModificationTimestamp > 0) ? mModificationTimestamp : mFile.getModificationTimestamp();
118 }
119
120 @Override
121 protected RemoteOperationResult run(OwnCloudClient client) {
122 RemoteOperationResult result = null;
123 File newFile = null;
124 boolean moved = true;
125
126 /// download will be performed to a temporal file, then moved to the final location
127 File tmpFile = new File(getTmpPath());
128
129 String tmpFolder = getTmpFolder();
130
131 /// perform the download
132 mDownloadOperation = new DownloadRemoteFileOperation(mFile.getRemotePath(), tmpFolder);
133 Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
134 while (listener.hasNext()) {
135 mDownloadOperation.addDatatransferProgressListener(listener.next());
136 }
137 result = mDownloadOperation.execute(client);
138
139 if (result.isSuccess()) {
140 mModificationTimestamp = mDownloadOperation.getModificationTimestamp();
141 newFile = new File(getSavePath());
142 newFile.getParentFile().mkdirs();
143 moved = tmpFile.renameTo(newFile);
144
145 if (!moved)
146 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
147 }
148 Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
149
150
151 return result;
152 }
153
154 public void cancel() {
155 mDownloadOperation.cancel();
156 }
157
158
159 public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
160 synchronized (mDataTransferListeners) {
161 mDataTransferListeners.add(listener);
162 }
163 }
164
165 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
166 synchronized (mDataTransferListeners) {
167 mDataTransferListeners.remove(listener);
168 }
169 }
170
171 }