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 OCFile(ContentResolver cr
, Account account
, long id
) {
80 Cursor c
= cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
81 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
82 + ProviderTableMeta
._ID
+ "=?",
83 new String
[]{account_
.name
, String
.valueOf(id
)}, null
);
88 public OCFile(ContentResolver cr
, Account account
, String path
) {
92 Cursor c
= cr_
.query(ProviderTableMeta
.CONTENT_URI_FILE
, null
,
93 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND "
94 + ProviderTableMeta
.FILE_PATH
+ "=?", new String
[]{account_
.name
,
96 if (c
.moveToFirst()) {
103 public long getFileId() {
107 public String
getPath() {
111 public boolean fileExtist() {
115 public boolean isDirectory() {
116 return mimetype_
!= null
&& mimetype_
.equals("DIR");
119 public boolean isDownloaded() {
120 return storage_path_
!= null
;
123 public String
getStoragePath() {
124 return storage_path_
;
126 public void setStoragePath(String storage_path
) {
127 storage_path_
= storage_path
;
130 public long getCreationTimestamp() {
131 return creation_timestamp_
;
133 public void setCreationTimestamp(long creation_timestamp
) {
134 creation_timestamp_
= creation_timestamp
;
137 public long getModificationTimestamp() {
138 return modified_timestamp_
;
140 public void setModificationTimestamp(long modification_timestamp
) {
141 modified_timestamp_
= modification_timestamp
;
144 public String
getFileName() {
146 File f
= new File(path_
);
147 return f
.getName().equals("") ?
"/" : f
.getName();
152 public String
getMimetype() {
157 ContentValues cv
= new ContentValues();
158 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, modified_timestamp_
);
159 cv
.put(ProviderTableMeta
.FILE_CREATION
, creation_timestamp_
);
160 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, length_
);
161 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, mimetype_
);
162 cv
.put(ProviderTableMeta
.FILE_NAME
, getFileName());
164 cv
.put(ProviderTableMeta
.FILE_PARENT
, parent_id_
);
165 cv
.put(ProviderTableMeta
.FILE_PATH
, path_
);
166 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, storage_path_
);
167 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, account_
.name
);
172 cp_
.update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
173 + "=?", new String
[]{String
.valueOf(id_
)});
174 } catch (RemoteException e
) {
175 Log
.e(TAG
, e
.getMessage());
179 cr_
.update(ProviderTableMeta
.CONTENT_URI
, cv
, ProviderTableMeta
._ID
180 + "=?", new String
[]{String
.valueOf(id_
)});
183 Uri new_entry
= null
;
186 new_entry
= cp_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
187 } catch (RemoteException e
) {
188 Log
.e(TAG
, e
.getMessage());
193 new_entry
= cr_
.insert(ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
196 String p
= new_entry
.getEncodedPath();
197 id_
= Integer
.parseInt(p
.substring(p
.lastIndexOf('/')+1));
198 } catch (NumberFormatException e
) {
199 Log
.e(TAG
, "Can't retrieve file id from uri: " + new_entry
.toString()
200 + ", reason: " + e
.getMessage());
206 public Vector
<OCFile
> getDirectoryContent() {
207 if (isDirectory() && id_
!= -1) {
208 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
210 Uri req_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
,
211 String
.valueOf(id_
));
215 c
= cp_
.query(req_uri
, null
, null
, null
, null
);
216 } catch (RemoteException e
) {
217 Log
.e(TAG
, e
.getMessage());
221 c
= cr_
.query(req_uri
, null
, null
, null
, null
);
226 OCFile child
= new OCFile(cp_
, account_
);
227 child
.setFileData(c
);
229 } while (c
.moveToNext());
237 public void addFile(OCFile file
) {
238 file
.parent_id_
= id_
;
242 private OCFile(ContentProviderClient cp
, Account account
) {
248 private void resetData() {
252 storage_path_
= null
;
255 creation_timestamp_
= 0;
256 modified_timestamp_
= 0;
259 private void setFileData(Cursor c
) {
262 id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
));
263 path_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
));
264 parent_id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_PARENT
));
265 storage_path_
= c
.getString(c
266 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
267 mimetype_
= c
.getString(c
268 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
));
269 length_
= c
.getLong(c
270 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
271 creation_timestamp_
= c
.getLong(c
272 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
));
273 modified_timestamp_
= c
.getLong(c
274 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
));