Notify the download cancelation for updating the screen and remove the yellow arrow...
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / CreateFolderOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 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 com.owncloud.android.datamodel.OCFile;
21 import com.owncloud.android.lib.common.OwnCloudClient;
22 import com.owncloud.android.lib.resources.files.CreateRemoteFolderOperation;
23 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
24 import com.owncloud.android.lib.common.operations.RemoteOperation;
25 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
26 import com.owncloud.android.lib.common.utils.Log_OC;
27 import com.owncloud.android.operations.common.SyncOperation;
28 import com.owncloud.android.utils.FileStorageUtils;
29
30
31 /**
32 * Access to remote operation performing the creation of a new folder in the ownCloud server.
33 * Save the new folder in Database
34 *
35 * @author David A. Velasco
36 * @author masensio
37 */
38 public class CreateFolderOperation extends SyncOperation implements OnRemoteOperationListener{
39
40 private static final String TAG = CreateFolderOperation.class.getSimpleName();
41
42 protected String mRemotePath;
43 protected boolean mCreateFullPath;
44
45 /**
46 * Constructor
47 *
48 * @param createFullPath 'True' means that all the ancestor folders should be created if don't exist yet.
49 */
50 public CreateFolderOperation(String remotePath, boolean createFullPath) {
51 mRemotePath = remotePath;
52 mCreateFullPath = createFullPath;
53
54 }
55
56
57 @Override
58 protected RemoteOperationResult run(OwnCloudClient client) {
59 CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
60 RemoteOperationResult result = operation.execute(client);
61
62 if (result.isSuccess()) {
63 saveFolderInDB();
64 } else {
65 Log_OC.e(TAG, mRemotePath + "hasn't been created");
66 }
67
68 return result;
69 }
70
71 @Override
72 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
73 if (operation instanceof CreateRemoteFolderOperation) {
74 onCreateRemoteFolderOperationFinish((CreateRemoteFolderOperation)operation, result);
75 }
76 }
77
78
79 private void onCreateRemoteFolderOperationFinish(CreateRemoteFolderOperation operation, RemoteOperationResult result) {
80 if (result.isSuccess()) {
81 saveFolderInDB();
82 } else {
83 Log_OC.e(TAG, mRemotePath + "hasn't been created");
84 }
85 }
86
87 /**
88 * Save new directory in local database
89 */
90 public void saveFolderInDB() {
91 if (mCreateFullPath && getStorageManager().
92 getFileByPath(FileStorageUtils.getParentPath(mRemotePath)) == null){// When parent
93 // of remote path
94 // is not created
95 String[] subFolders = mRemotePath.split("/");
96 String composedRemotePath = "/";
97
98 // For each antecesor folders create them recursively
99 for (int i=0; i<subFolders.length; i++) {
100 String subFolder = subFolders[i];
101 if (!subFolder.isEmpty()) {
102 composedRemotePath = composedRemotePath + subFolder + "/";
103 mRemotePath = composedRemotePath;
104 saveFolderInDB();
105 }
106 }
107 } else { // Create directory on DB
108 OCFile newDir = new OCFile(mRemotePath);
109 newDir.setMimetype("DIR");
110 long parentId = getStorageManager().
111 getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId();
112 newDir.setParentId(parentId);
113 newDir.setModificationTimestamp(System.currentTimeMillis());
114 getStorageManager().saveFile(newDir);
115
116 Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database");
117 }
118 }
119 }