9946ba7e586d6b80c79c7f38b54d19b47ab6dae0
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / OCShare.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-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.datamodel;
19
20 import com.owncloud.android.lib.operations.common.ShareRemoteFile;
21 import com.owncloud.android.lib.operations.common.ShareType;
22 import com.owncloud.android.utils.Log_OC;
23
24 import android.os.Parcel;
25 import android.os.Parcelable;
26
27 public class OCShare implements Parcelable{
28
29 private static final String TAG = OCShare.class.getSimpleName();
30
31 private long mId;
32 private long mFileSource;
33 private long mItemSource;
34 private ShareType mShareType;
35 private String mShareWith;
36 private String mPath;
37 private int mPermissions;
38 private long mSharedDate;
39 private long mExpirationDate;
40 private String mToken;
41 private String mSharedWithDisplayName;
42 private boolean mIsDirectory;
43 private long mUserId;
44 private long mIdRemoteShared;
45
46
47 /**
48 * Create new {@link OCShare} with given path.
49 *
50 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
51 *
52 * @param path The remote path of the file.
53 */
54 public OCShare(String path) {
55 resetData();
56 if (path == null || path.length() <= 0 || !path.startsWith(OCFile.PATH_SEPARATOR)) {
57 Log_OC.e(TAG, "Trying to create a OCShare with a non valid path");
58 throw new IllegalArgumentException("Trying to create a OCShare with a non valid path: " + path);
59 }
60 mPath = path;
61 }
62
63 public OCShare(ShareRemoteFile remoteFile) {
64 mId = -1;
65
66 String path = remoteFile.getPath();
67 if (path == null || path.length() <= 0 || !path.startsWith(OCFile.PATH_SEPARATOR)) {
68 Log_OC.e(TAG, "Trying to create a OCShare with a non valid path");
69 throw new IllegalArgumentException("Trying to create a OCShare with a non valid path: " + path);
70 }
71 mPath = path;
72
73 mFileSource = remoteFile.getFileSource();
74 mItemSource = remoteFile.getItemSource();
75 mShareType = remoteFile.getShareType();
76 mShareWith = remoteFile.getShareWith();
77 mPermissions = remoteFile.getPermissions();
78 mSharedDate = remoteFile.getSharedDate();
79 mExpirationDate = remoteFile.getExpirationDate();
80 mToken = remoteFile.getToken();
81 mSharedWithDisplayName = remoteFile.getSharedWithDisplayName();
82 mIsDirectory = remoteFile.isDirectory();
83 mUserId = remoteFile.getUserId();
84 mIdRemoteShared = remoteFile.getIdRemoteShared();
85 }
86
87 /**
88 * Used internally. Reset all file properties
89 */
90 private void resetData() {
91 mId = -1;
92 mFileSource = 0;
93 mItemSource = 0;
94 mShareType = ShareType.NO_SHARED;
95 mShareWith = null;
96 mPath = null;
97 mPermissions = -1;
98 mSharedDate = 0;
99 mExpirationDate = 0;
100 mToken = null;
101 mSharedWithDisplayName = null;
102 mIsDirectory = false;
103 mUserId = -1;
104 mIdRemoteShared = -1;
105
106 }
107
108 /// Getters and Setters
109 public long getFileSource() {
110 return mFileSource;
111 }
112
113 public void setFileSource(long fileSource) {
114 this.mFileSource = fileSource;
115 }
116
117 public long getItemSource() {
118 return mItemSource;
119 }
120
121 public void setItemSource(long itemSource) {
122 this.mItemSource = itemSource;
123 }
124
125 public ShareType getShareType() {
126 return mShareType;
127 }
128
129 public void setShareType(ShareType shareType) {
130 this.mShareType = shareType;
131 }
132
133 public String getShareWith() {
134 return mShareWith;
135 }
136
137 public void setShareWith(String shareWith) {
138 this.mShareWith = shareWith;
139 }
140
141 public String getPath() {
142 return mPath;
143 }
144
145 public void setPath(String path) {
146 this.mPath = path;
147 }
148
149 public int getPermissions() {
150 return mPermissions;
151 }
152
153 public void setPermissions(int permissions) {
154 this.mPermissions = permissions;
155 }
156
157 public long getSharedDate() {
158 return mSharedDate;
159 }
160
161 public void setSharedDate(long sharedDate) {
162 this.mSharedDate = sharedDate;
163 }
164
165 public long getExpirationDate() {
166 return mExpirationDate;
167 }
168
169 public void setExpirationDate(long expirationDate) {
170 this.mExpirationDate = expirationDate;
171 }
172
173 public String getToken() {
174 return mToken;
175 }
176
177 public void setToken(String token) {
178 this.mToken = token;
179 }
180
181 public String getSharedWithDisplayName() {
182 return mSharedWithDisplayName;
183 }
184
185 public void setSharedWithDisplayName(String sharedWithDisplayName) {
186 this.mSharedWithDisplayName = sharedWithDisplayName;
187 }
188
189 public boolean isDirectory() {
190 return mIsDirectory;
191 }
192
193 public void setIsDirectory(boolean isDirectory) {
194 this.mIsDirectory = isDirectory;
195 }
196
197 public long getUserId() {
198 return mUserId;
199 }
200
201 public void setUserId(long userId) {
202 this.mUserId = userId;
203 }
204
205 public long getIdRemoteShared() {
206 return mIdRemoteShared;
207 }
208
209 public void setIdRemoteShared(long idRemoteShared) {
210 this.mIdRemoteShared = idRemoteShared;
211 }
212
213 public long getId() {
214 return mId;
215 }
216
217 public void setId(long id){
218 mId = id;
219 }
220
221 /**
222 * Parcelable Methods
223 */
224 public static final Parcelable.Creator<OCShare> CREATOR = new Parcelable.Creator<OCShare>() {
225 @Override
226 public OCShare createFromParcel(Parcel source) {
227 return new OCShare(source);
228 }
229
230 @Override
231 public OCShare[] newArray(int size) {
232 return new OCShare[size];
233 }
234 };
235
236 /**
237 * Reconstruct from parcel
238 *
239 * @param source The source parcel
240 */
241 private OCShare(Parcel source) {
242 mId = source.readLong();
243 mFileSource = source.readLong();
244 mItemSource = source.readLong();
245 try {
246 mShareType = ShareType.valueOf(source.readString());
247 } catch (IllegalArgumentException x) {
248 mShareType = ShareType.NO_SHARED;
249 }
250 mShareWith = source.readString();
251 mPath = source.readString();
252 mPermissions = source.readInt();
253 mSharedDate = source.readLong();
254 mExpirationDate = source.readLong();
255 mToken = source.readString();
256 mSharedWithDisplayName = source.readString();
257 mIsDirectory = source.readInt() == 0;
258 mUserId = source.readLong();
259 mIdRemoteShared = source.readLong();
260 }
261
262 @Override
263 public int describeContents() {
264 return this.hashCode();
265 }
266
267 @Override
268 public void writeToParcel(Parcel dest, int flags) {
269 dest.writeLong(mId);
270 dest.writeLong(mFileSource);
271 dest.writeLong(mItemSource);
272 dest.writeString((mShareType == null) ? "" : mShareType.name());
273 dest.writeString(mShareWith);
274 dest.writeString(mPath);
275 dest.writeInt(mPermissions);
276 dest.writeLong(mSharedDate);
277 dest.writeLong(mExpirationDate);
278 dest.writeString(mToken);
279 dest.writeString(mSharedWithDisplayName);
280 dest.writeInt(mIsDirectory ? 1 : 0);
281 dest.writeLong(mUserId);
282 dest.writeLong(mIdRemoteShared);
283 }
284 }