1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.datamodel
;
22 import java
.util
.Vector
;
24 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
25 import android
.accounts
.Account
;
26 import android
.content
.ContentProviderClient
;
27 import android
.content
.ContentResolver
;
28 import android
.content
.ContentValues
;
29 import android
.database
.Cursor
;
30 import android
.net
.Uri
;
31 import android
.os
.RemoteException
;
32 import android
.util
.Log
;
35 private static String TAG
= "OCFile";
38 private long parent_id_
;
40 private long creation_timestamp_
;
41 private long modified_timestamp_
;
43 private String storage_path_
;
44 private String mimetype_
;
46 private ContentResolver cr_
;
47 private ContentProviderClient cp_
;
48 private Account account_
;
50 public static OCFile
createNewFile(ContentProviderClient cr
, Account account
,
51 String path
, long length
, long creation_timestamp
,
52 long modified_timestamp
, String mimetype
, long parent_id
) {
53 OCFile new_file
= new OCFile(cr
, account
);
56 Cursor c
= new_file
.cp_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
57 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
58 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{new_file
.account_
.name
,
61 new_file
.setFileData(c
);
63 } catch (RemoteException e
) {
64 Log
.e(TAG
, e
.getMessage());
67 new_file
.path_
= path
;
68 new_file
.length_
= length
;
69 new_file
.creation_timestamp_
= creation_timestamp
;
70 new_file
.modified_timestamp_
= modified_timestamp
;
71 new_file
.mimetype_
= mimetype
;
72 new_file
.parent_id_
= parent_id
;
77 public static OCFile
createNewFile(ContentResolver contentResolver
, Account a
,
78 String path
, int length
, int creation_timestamp
, int modified_timestamp
,
79 String mimetype
, long parent_id
) {
80 OCFile new_file
= new OCFile(contentResolver
, a
);
81 Cursor c
= new_file
.cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
82 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
83 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{new_file
.account_
.name
,
86 new_file
.setFileData(c
);
89 new_file
.path_
= path
;
90 new_file
.length_
= length
;
91 new_file
.creation_timestamp_
= creation_timestamp
;
92 new_file
.modified_timestamp_
= modified_timestamp
;
93 new_file
.mimetype_
= mimetype
;
94 new_file
.parent_id_
= parent_id
;
99 public OCFile(ContentResolver cr
, Account account
, long id
) {
102 Cursor c
= cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
103 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
104 + ProviderTableMeta
._ID
+ "=?",
105 new String
[]{account_
.name
, String
.valueOf(id
)}, null
);
110 public OCFile(ContentResolver cr
, Account account
, String path
) {
114 Cursor c
= cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
115 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
116 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{account_
.name
,
118 if (c
.moveToFirst()) {
125 public long getFileId() {
129 public String
getPath() {
133 public boolean fileExtist() {
137 public boolean isDirectory() {
138 return mimetype_
!= null
&& mimetype_
.equals("DIR");
141 public boolean isDownloaded() {
142 return storage_path_
!= null
;
145 public String
getStoragePath() {
146 return storage_path_
;
148 public void setStoragePath(String storage_path
) {
149 storage_path_
= storage_path
;
152 public long getCreationTimestamp() {
153 return creation_timestamp_
;
155 public void setCreationTimestamp(long creation_timestamp
) {
156 creation_timestamp_
= creation_timestamp
;
159 public long getModificationTimestamp() {
160 return modified_timestamp_
;
162 public void setModificationTimestamp(long modification_timestamp
) {
163 modified_timestamp_
= modification_timestamp
;
166 public String
getFileName() {
168 File f
= new File(path_
);
169 return f
.getName().equals("") ?
"/" : f
.getName();
174 public String
getMimetype() {
179 ContentValues cv
= new ContentValues();
180 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, modified_timestamp_
);
181 cv
.put(ProviderTableMeta
.FILE_CREATION
, creation_timestamp_
);
182 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, length_
);
183 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, mimetype_
);
184 cv
.put(ProviderTableMeta
.FILE_NAME
, getFileName());
186 cv
.put(ProviderTableMeta
.FILE_PARENT
, parent_id_
);
187 cv
.put(ProviderTableMeta
.FILE_PATH
, path_
);
188 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, storage_path_
);
189 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, account_
.name
);
194 cp_
.update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
195 + "=?", new String
[]{String
.valueOf(id_
)});
196 } catch (RemoteException e
) {
197 Log
.e(TAG
, e
.getMessage());
201 cr_
.update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
202 + "=?", new String
[]{String
.valueOf(id_
)});
205 Uri new_entry
= null
;
208 new_entry
= cp_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
209 } catch (RemoteException e
) {
210 Log
.e(TAG
, e
.getMessage());
215 new_entry
= cr_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
218 String p
= new_entry
.getEncodedPath();
219 id_
= Integer
.parseInt(p
.substring(p
.lastIndexOf('/')+1));
220 } catch (NumberFormatException e
) {
221 Log
.e(TAG
, "Can't retrieve file id from uri: " + new_entry
.toString()
222 + ", reason: " + e
.getMessage());
228 public Vector
<OCFile
> getDirectoryContent() {
229 if (isDirectory() && id_
!= -1) {
230 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
232 Uri req_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
,
233 String
.valueOf(id_
));
237 c
= cp_
.query(req_uri
, null
, null
, null
, null
);
238 } catch (RemoteException e
) {
239 Log
.e(TAG
, e
.getMessage());
243 c
= cr_
.query(req_uri
, null
, null
, null
, null
);
248 OCFile child
= new OCFile(cp_
, account_
);
249 child
.setFileData(c
);
251 } while (c
.moveToNext());
259 public void addFile(OCFile file
) {
260 file
.parent_id_
= id_
;
264 private OCFile(ContentProviderClient cp
, Account account
) {
270 private OCFile(ContentResolver cr
, Account account
) {
276 private void resetData() {
280 storage_path_
= null
;
283 creation_timestamp_
= 0;
284 modified_timestamp_
= 0;
287 private void setFileData(Cursor c
) {
290 id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
));
291 path_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
));
292 parent_id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
));
293 storage_path_
= c
.getString(c
294 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
295 mimetype_
= c
.getString(c
296 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
));
297 length_
= c
.getLong(c
298 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
299 creation_timestamp_
= c
.getLong(c
300 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
));
301 modified_timestamp_
= c
.getLong(c
302 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
));