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