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