f377a047d40a1efbc0bdb8996ba953159ed9ce2f
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
;
21 import java
.util
.Vector
;
23 import eu
.alefzero
.owncloud
.db
.ProviderMeta
.ProviderTableMeta
;
24 import android
.accounts
.Account
;
25 import android
.content
.ContentProvider
;
26 import android
.content
.ContentValues
;
27 import android
.database
.Cursor
;
28 import android
.net
.Uri
;
29 import android
.util
.Log
;
32 private static String TAG
= "OCFile";
36 private long creation_timestamp_
;
37 private long modified_timestamp_
;
39 private String storage_path_
;
40 private String mimetype_
;
42 private ContentProvider cp_
;
43 private Account account_
;
45 public OCFile(ContentProvider cp
, Account account
, long id
) {
48 Cursor c
= cp_
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
50 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
51 ProviderTableMeta
._ID
+ "=?",
52 new String
[]{account_
.name
, String
.valueOf(id
)},
57 public OCFile(ContentProvider cp
, Account account
, String path
) {
60 Cursor c
= cp_
.query(ProviderTableMeta
.CONTENT_URI_FILE
,
62 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " +
63 ProviderTableMeta
.FILE_PATH
+ "=?",
64 new String
[]{account_
.name
, path
},
67 if (path_
!= null
) path_
= path
;
70 public long getFileId() { return id_
; }
72 public String
getPath() { return path_
; }
74 public boolean fileExtist() { return id_
!= -1; }
76 public boolean isDirectory() { return mimetype_
!= null
&& mimetype_
.equals("dir"); }
78 public boolean isDownloaded() { return storage_path_
!= null
; }
80 public String
getStoragePath() { return storage_path_
; }
81 public void setStoragePath(String storage_path
) { storage_path_
= storage_path
; }
83 public long getCreationTimestamp() { return creation_timestamp_
; }
84 public void setCreationTimestamp(long creation_timestamp
) { creation_timestamp_
= creation_timestamp
; }
86 public long getModificationTimestamp() { return modified_timestamp_
; }
87 public void setModificationTimestamp(long modification_timestamp
) { modified_timestamp_
= modification_timestamp
; }
90 ContentValues cv
= new ContentValues();
91 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, modified_timestamp_
);
92 cv
.put(ProviderTableMeta
.FILE_CREATION
, creation_timestamp_
);
93 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, length_
);
94 cv
.put(ProviderTableMeta
.CONTENT_TYPE
, mimetype_
);
96 Uri new_entry
= cp_
.insert(ProviderTableMeta
.CONTENT_URI
, cv
);
98 id_
= Integer
.parseInt(new_entry
.getEncodedPath());
99 } catch (NumberFormatException e
) {
100 Log
.e(TAG
, "Can't retrieve file id from uri: " + new_entry
.toString() +
101 ", reason: " + e
.getMessage());
106 public Vector
<OCFile
> getDirectoryContent() {
107 if (isDirectory() && id_
!= -1) {
108 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
110 Uri req_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, String
.valueOf(id_
));
111 Cursor c
= cp_
.query(req_uri
, null
, null
, null
, null
);
115 long id
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
));
116 OCFile child
= new OCFile(cp_
, account_
, id
);
118 } while (c
.moveToNext());
125 private void setFileData(Cursor c
) {
128 storage_path_
= null
;
131 creation_timestamp_
= 0;
132 modified_timestamp_
= 0;
133 if (c
!= null
&& c
.moveToFirst()) {
134 id_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
));
135 path_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
));
136 storage_path_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
137 mimetype_
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
));
138 length_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
));
139 creation_timestamp_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_CREATION
));
140 modified_timestamp_
= c
.getLong(c
.getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
));