Removed requests for synchronization of account when account is changed or created
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateFolderOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 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
22 import org.apache.commons.httpclient.HttpStatus;
23 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
24
25 import com.owncloud.android.Log_OC;
26 import com.owncloud.android.datamodel.FileDataStorageManager;
27 import com.owncloud.android.datamodel.OCFile;
28
29
30 import eu.alefzero.webdav.WebdavClient;
31 import eu.alefzero.webdav.WebdavUtils;
32
33 /**
34 * Remote operation performing the creation of a new folder in the ownCloud server.
35 *
36 * @author David A. Velasco
37 */
38 public class CreateFolderOperation extends RemoteOperation {
39
40 private static final String TAG = CreateFolderOperation.class.getSimpleName();
41
42 private static final int READ_TIMEOUT = 10000;
43 private static final int CONNECTION_TIMEOUT = 5000;
44
45 protected String mRemotePath;
46 protected boolean mCreateFullPath;
47 protected FileDataStorageManager mStorageManager;
48
49 /**
50 * Constructor
51 *
52 * @param remotePath Full path to the new directory to create in the remote server.
53 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
54 * @param storageManager Reference to the local database corresponding to the account where the file is contained.
55 */
56 public CreateFolderOperation(String remotePath, boolean createFullPath, FileDataStorageManager storageManager) {
57 mRemotePath = remotePath;
58 mCreateFullPath = createFullPath;
59 mStorageManager = storageManager;
60 }
61
62
63 /**
64 * Performs the operation
65 *
66 * @param client Client object to communicate with the remote ownCloud server.
67 */
68 @Override
69 protected RemoteOperationResult run(WebdavClient client) {
70 RemoteOperationResult result = null;
71 MkColMethod mkcol = null;
72 try {
73 mkcol = new MkColMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
74 int status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT);
75 if (!mkcol.succeeded() && mkcol.getStatusCode() == HttpStatus.SC_CONFLICT && mCreateFullPath) {
76 result = createParentFolder(getParentPath(), client);
77 status = client.executeMethod(mkcol, READ_TIMEOUT, CONNECTION_TIMEOUT); // second (and last) try
78 }
79 if (mkcol.succeeded()) {
80 // Save new directory in local database
81 OCFile newDir = new OCFile(mRemotePath);
82 newDir.setMimetype("DIR");
83 long parentId = mStorageManager.getFileByPath(getParentPath()).getFileId();
84 newDir.setParentId(parentId);
85 newDir.setModificationTimestamp(System.currentTimeMillis());
86 mStorageManager.saveFile(newDir);
87 }
88 result = new RemoteOperationResult(mkcol.succeeded(), status, mkcol.getResponseHeaders());
89 Log_OC.d(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage());
90 client.exhaustResponse(mkcol.getResponseBodyAsStream());
91
92 } catch (Exception e) {
93 result = new RemoteOperationResult(e);
94 Log_OC.e(TAG, "Create directory " + mRemotePath + ": " + result.getLogMessage(), e);
95
96 } finally {
97 if (mkcol != null)
98 mkcol.releaseConnection();
99 }
100 return result;
101 }
102
103
104 private String getParentPath() {
105 String parentPath = new File(mRemotePath).getParent();
106 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
107 return parentPath;
108 }
109
110
111 private RemoteOperationResult createParentFolder(String parentPath, WebdavClient client) {
112 RemoteOperation operation = new CreateFolderOperation( parentPath,
113 mCreateFullPath,
114 mStorageManager );
115 return operation.execute(client);
116 }
117
118 }