889d1dbff768f2fb58f7d4e56eca591d10ea8c9f
[pub/Android/ownCloud.git] / src / com / owncloud / android / db / DbHandler.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011-2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
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
28 /**
29 * Custom database helper for ownCloud
30 *
31 * @author Bartek Przybylski
32 *
33 */
34 public class DbHandler {
35 private SQLiteDatabase mDB;
36 private OpenerHelper mHelper;
37 private final String mDatabaseName = "ownCloud";
38 private final int mDatabaseVersion = 3;
39
40 private final String TABLE_INSTANT_UPLOAD = "instant_upload";
41
42 public static final int UPLOAD_STATUS_UPLOAD_LATER = 0;
43 public static final int UPLOAD_STATUS_UPLOAD_FAILED = 1;
44
45 public DbHandler(Context context) {
46 mHelper = new OpenerHelper(context);
47 mDB = mHelper.getWritableDatabase();
48 }
49
50 public void close() {
51 mDB.close();
52 }
53
54 public boolean putFileForLater(String filepath, String account, String message) {
55 ContentValues cv = new ContentValues();
56 cv.put("path", filepath);
57 cv.put("account", account);
58 cv.put("attempt", UPLOAD_STATUS_UPLOAD_LATER);
59 cv.put("message", message);
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, String message) {
66 ContentValues cv = new ContentValues();
67 cv.put("attempt", status);
68 cv.put("message", message);
69 int result = mDB.update(TABLE_INSTANT_UPLOAD, cv, "path=?", new String[] { filepath });
70 Log_OC.d(TABLE_INSTANT_UPLOAD, "updateFileState returns with: " + result + " for file: " + filepath);
71 return result;
72 }
73
74 public Cursor getAwaitingFiles() {
75 return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt=" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
76 }
77
78 public Cursor getFailedFiles() {
79 return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt>" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
80 }
81
82 public void clearFiles() {
83 mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
84 }
85
86 /**
87 *
88 * @param localPath
89 * @return true when one or more pending files was removed
90 */
91 public boolean removeIUPendingFile(String localPath) {
92 long result = mDB.delete(TABLE_INSTANT_UPLOAD, "path = ?", new String[] { localPath });
93 Log_OC.d(TABLE_INSTANT_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
94 return result != 0;
95
96 }
97
98 private class OpenerHelper extends SQLiteOpenHelper {
99 public OpenerHelper(Context context) {
100 super(context, mDatabaseName, null, mDatabaseVersion);
101 }
102
103 @Override
104 public void onCreate(SQLiteDatabase db) {
105 db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
106 + " account TEXT,attempt INTEGER,message TEXT);");
107 }
108
109 @Override
110 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
111 if (oldVersion < 2) {
112 db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN attempt INTEGER;");
113 }
114 db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN message TEXT;");
115
116 }
117 }
118 }