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