ddd004c237569a31308a253374d2fc0b1df0ec83
[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 3 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
26 /**
27 * Custom database helper for ownCloud
28 *
29 * @author Bartek Przybylski
30 *
31 */
32 public class DbHandler {
33 private SQLiteDatabase mDB;
34 private OpenerHepler mHelper;
35 private final String mDatabaseName = "ownCloud";
36 private final int mDatabaseVersion = 1;
37
38 private final String TABLE_INSTANT_UPLOAD = "instant_upload";
39
40 public DbHandler(Context context) {
41 mHelper = new OpenerHepler(context);
42 mDB = mHelper.getWritableDatabase();
43 }
44
45 public void close() {
46 mDB.close();
47 }
48
49 public boolean putFileForLater(String filepath, String account) {
50 ContentValues cv = new ContentValues();
51 cv.put("path", filepath);
52 cv.put("account", account);
53 return mDB.insert(TABLE_INSTANT_UPLOAD, null, cv) != -1;
54 }
55
56 public Cursor getAwaitingFiles() {
57 return mDB.query(TABLE_INSTANT_UPLOAD, null, null, null, null, null, null);
58 }
59
60 public void clearFiles() {
61 mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
62 }
63
64 /**
65 *
66 * @param localPath
67 * @param accountName
68 * @return true when one or more pendin files was removed
69 */
70 public boolean removeIUPendingFile(String localPath, String accountName) {
71 return mDB.delete(TABLE_INSTANT_UPLOAD,
72 "path = ?",
73 new String[]{ localPath }) != 0;
74
75 }
76
77 private class OpenerHepler extends SQLiteOpenHelper {
78 public OpenerHepler(Context context) {
79 super(context, mDatabaseName, null, mDatabaseVersion);
80 }
81
82 @Override
83 public void onCreate(SQLiteDatabase db) {
84 db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " ("
85 + " _id INTEGER PRIMARY KEY, "
86 + " path TEXT,"
87 + " account TEXT);");
88 }
89
90 @Override
91 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
92 }
93 }
94 }