d2e0b11864cbc4c9a1f559a0bf5d13d769391ee7
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / db / DbHandler.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 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 eu.alefzero.owncloud.db;
19
20 import java.util.Vector;
21
22 import eu.alefzero.owncloud.OwnCloudSession;
23
24 import android.content.ContentValues;
25 import android.content.Context;
26 import android.database.Cursor;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.database.sqlite.SQLiteOpenHelper;
29 import android.util.Log;
30
31 /**
32 * Custom database helper for ownCloud
33 * @author Bartek Przybylski
34 *
35 */
36 public class DbHandler {
37 private SQLiteDatabase mDB;
38 private OpenerHepler mHelper;
39 private final String mDatabaseName = "ownCloud";
40 private final String TABLE_SESSIONS = "sessions";
41 private final int mDatabaseVersion = 1;
42
43 public DbHandler(Context context) {
44 mHelper = new OpenerHepler(context);
45 mDB = mHelper.getWritableDatabase();
46 }
47
48 public Vector<OwnCloudSession> getSessionList() {
49 Cursor c = mDB.query(TABLE_SESSIONS, null, null, null, null, null, null);
50 Vector<OwnCloudSession> v = new Vector<OwnCloudSession>();
51 if (!c.moveToFirst()) {
52 return v;
53 }
54 while (!c.isAfterLast()) {
55 v.add(new OwnCloudSession(c.getString(c.getColumnIndex("sessionName")),
56 c.getString(c.getColumnIndex("sessionUrl")),
57 c.getInt(c.getColumnIndex("_id"))));
58 c.moveToNext();
59 }
60 c.close();
61 return v;
62 }
63
64 public void addSession(String sessionName, String uri) {
65 ContentValues cv = new ContentValues();
66 cv.put("sessionName", sessionName);
67 cv.put("sessionUrl", uri);
68 mDB.insert(TABLE_SESSIONS, null, cv);
69 }
70
71 public void removeSessionWithId(int sessionId) {
72 mDB.delete(TABLE_SESSIONS, "_id = ?", new String[] {String.valueOf(sessionId)});
73 }
74
75 public void changeSessionFields(int id, String hostname, String uri) {
76 ContentValues cv = new ContentValues();
77 cv.put("sessionName", hostname);
78 cv.put("sessionUrl", uri);
79 mDB.update(TABLE_SESSIONS, cv, "_id = ?", new String[] {String.valueOf(id)});
80 }
81
82 public void close() {
83 mDB.close();
84 }
85
86 private class OpenerHepler extends SQLiteOpenHelper {
87 public OpenerHepler(Context context) {
88 super(context, mDatabaseName, null, mDatabaseVersion);
89 }
90
91 @Override
92 public void onCreate(SQLiteDatabase db) {
93 db.execSQL("CREATE TABLE " + TABLE_SESSIONS + " (" +
94 " _id INTEGER PRIMARY KEY, " +
95 " sessionName TEXT, " +
96 " sessionUrl TEXT);");
97 }
98
99 @Override
100 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
101 }
102 }
103 }