Cleaned stuff , add a history delete button and renamed
[pub/Android/ownCloud.git] / src / com / owncloud / android / db / DbHandler.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011-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 2 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 package com.owncloud.android.db;
19
20 import com.owncloud.android.Log_OC;
21
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.database.Cursor;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteOpenHelper;
27 import android.util.Log;
28
29 /**
30 * Custom database helper for ownCloud
31 *
32 * @author Bartek Przybylski
33 *
34 */
35 public class DbHandler {
36 private SQLiteDatabase mDB;
37 private OpenerHelper mHelper;
38 private final String mDatabaseName = "ownCloud";
39 private final int mDatabaseVersion = 3;
40
41 private final String TABLE_INSTANT_UPLOAD = "instant_upload";
42
43 public static final int UPLOAD_STATUS_UPLOAD_LATER = 0;
44 public static final int UPLOAD_STATUS_UPLOAD_FAILED = 1;
45
46 public DbHandler(Context context) {
47 mHelper = new OpenerHelper(context);
48 mDB = mHelper.getWritableDatabase();
49 }
50
51 public void close() {
52 mDB.close();
53 }
54
55 public boolean putFileForLater(String filepath, String account) {
56 ContentValues cv = new ContentValues();
57 cv.put("path", filepath);
58 cv.put("account", account);
59 cv.put("attempt", UPLOAD_STATUS_UPLOAD_LATER);
60 long result = mDB.insert(TABLE_INSTANT_UPLOAD, null, cv);
61 Log_OC.d(TABLE_INSTANT_UPLOAD, "putFileForLater returns with: " + result + " for file: " + filepath);
62 return result != -1;
63 }
64
65 public int updateFileState(String filepath, Integer status) {
66 ContentValues cv = new ContentValues();
67 cv.put("attempt", status);
68 int result = mDB.update(TABLE_INSTANT_UPLOAD, cv, "path=?", new String[] { filepath });
69 Log_OC.d(TABLE_INSTANT_UPLOAD, "updateFileState returns with: " + result + " for file: " + filepath);
70 return result;
71 }
72
73 public Cursor getAwaitingFiles() {
74 return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt=" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
75 }
76
77 public Cursor getFailedFiles() {
78 return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt>" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
79 }
80
81 public void clearFiles() {
82 mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
83 }
84
85 /**
86 *
87 * @param localPath
88 * @return true when one or more pending files was removed
89 */
90 public boolean removeIUPendingFile(String localPath) {
91 long result = mDB.delete(TABLE_INSTANT_UPLOAD, "path = ?", new String[] { localPath });
92 Log_OC.d(TABLE_INSTANT_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
93 return result != 0;
94
95 }
96
97 private class OpenerHelper extends SQLiteOpenHelper {
98 public OpenerHelper(Context context) {
99 super(context, mDatabaseName, null, mDatabaseVersion);
100 }
101
102 @Override
103 public void onCreate(SQLiteDatabase db) {
104 db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
105 + " account TEXT,attempt INTEGER);");
106 }
107
108 @Override
109 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
110 db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN attempt;");
111
112 }
113 }
114 }