20e30fe2e96d7d686e969cef2990fb40fc48e0f0
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.owncloud.datamodel;
20
21 import java.io.File;
22 import java.util.Vector;
23
24 import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
25 import android.accounts.Account;
26 import android.content.ContentResolver;
27 import android.content.ContentValues;
28 import android.database.Cursor;
29 import android.net.Uri;
30 import android.util.Log;
31
32 public class OCFile {
33 private static String TAG = "OCFile";
34
35 private long id_;
36 private long parent_id_;
37 private long length_;
38 private long creation_timestamp_;
39 private long modified_timestamp_;
40 private String path_;
41 private String storage_path_;
42 private String mimetype_;
43
44 private ContentResolver cp_;
45 private Account account_;
46
47 public OCFile(ContentResolver cp, Account account, long id) {
48 cp_ = cp;
49 account_ = account;
50 Cursor c = cp_.query(ProviderTableMeta.CONTENT_URI_FILE,
51 null,
52 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
53 ProviderTableMeta._ID + "=?",
54 new String[]{account_.name, String.valueOf(id)},
55 null);
56 if (c.moveToFirst())
57 setFileData(c);
58 }
59
60 public OCFile(ContentResolver cp, Account account, String path) {
61 cp_ = cp;
62 account_ = account;
63 Cursor c = cp_.query(ProviderTableMeta.CONTENT_URI_FILE,
64 null,
65 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
66 ProviderTableMeta.FILE_PATH + "=?",
67 new String[]{account_.name, path},
68 null);
69 if (c.moveToFirst()) {
70 setFileData(c);
71 if (path_ != null) path_ = path;
72 }
73 }
74
75 public long getFileId() { return id_; }
76
77 public String getPath() { return path_; }
78
79 public boolean fileExtist() { return id_ != -1; }
80
81 public boolean isDirectory() { return mimetype_ != null && mimetype_.equals("dir"); }
82
83 public boolean isDownloaded() { return storage_path_ != null; }
84
85 public String getStoragePath() { return storage_path_; }
86 public void setStoragePath(String storage_path) { storage_path_ = storage_path; }
87
88 public long getCreationTimestamp() { return creation_timestamp_; }
89 public void setCreationTimestamp(long creation_timestamp) { creation_timestamp_ = creation_timestamp; }
90
91 public long getModificationTimestamp() { return modified_timestamp_; }
92 public void setModificationTimestamp(long modification_timestamp) { modified_timestamp_ = modification_timestamp; }
93
94 public String getFileName() {
95 if (path_ != null) {
96 File f = new File(path_);
97 return f.getName();
98 }
99 return null;
100 }
101
102 public void save() {
103 ContentValues cv = new ContentValues();
104 cv.put(ProviderTableMeta.FILE_MODIFIED, modified_timestamp_);
105 cv.put(ProviderTableMeta.FILE_CREATION, creation_timestamp_);
106 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, length_);
107 cv.put(ProviderTableMeta.CONTENT_TYPE, mimetype_);
108 cv.put(ProviderTableMeta.FILE_NAME, getFileName());
109 cv.put(ProviderTableMeta.FILE_PARENT, parent_id_);
110 cv.put(ProviderTableMeta.FILE_PATH, path_);
111 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, storage_path_);
112 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account_.name);
113
114 if (fileExtist()) {
115 cp_.update(ProviderTableMeta.CONTENT_URI,
116 cv,
117 ProviderTableMeta._ID + "=?", new String[]{String.valueOf(id_)});
118 } else {
119 Uri new_entry = cp_.insert(ProviderTableMeta.CONTENT_URI, cv);
120 try {
121 id_ = Integer.parseInt(new_entry.getEncodedPath());
122 } catch (NumberFormatException e) {
123 Log.e(TAG, "Can't retrieve file id from uri: " + new_entry.toString() +
124 ", reason: " + e.getMessage());
125 id_ = -1;
126 }
127 }
128 }
129
130 public Vector<OCFile> getDirectoryContent() {
131 if (isDirectory() && id_ != -1) {
132 Vector<OCFile> ret = new Vector<OCFile>();
133
134 Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(id_));
135 Cursor c = cp_.query(req_uri, null, null, null, null);
136
137 if (c.moveToFirst())
138 do {
139 OCFile child = new OCFile(cp_, account_);
140 child.setFileData(c);
141 ret.add(child);
142 } while (c.moveToNext());
143
144 return ret;
145 }
146 return null;
147 }
148
149 public void addFile(OCFile file) {
150 file.parent_id_ = id_;
151 file.save();
152 }
153
154 private OCFile(ContentResolver cp, Account account) {
155 account_ = account;
156 cp_ = cp;
157 }
158
159 private void setFileData(Cursor c) {
160 id_ = -1;
161 path_ = null;
162 storage_path_ = null;
163 mimetype_ = null;
164 length_ = 0;
165 creation_timestamp_ = 0;
166 modified_timestamp_ = 0;
167 if (c != null) {
168 id_ = c.getLong(c.getColumnIndex(ProviderTableMeta._ID));
169 path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
170 storage_path_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
171 mimetype_ = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE));
172 length_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
173 creation_timestamp_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION));
174 modified_timestamp_ = c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED));
175 }
176 }
177 }