Merge pull request #453 from owncloud/improve_action_bar_pr_392_with_develop
[pub/Android/ownCloud.git] / src / com / owncloud / android / db / DbHandler.java
index 01518ba..89864fe 100644 (file)
-/* ownCloud Android client application\r
- *   Copyright (C) 2011-2012  Bartek Przybylski\r
- *\r
- *   This program is free software: you can redistribute it and/or modify\r
- *   it under the terms of the GNU General Public License as published by\r
- *   the Free Software Foundation, either version 3 of the License, or\r
- *   (at your option) any later version.\r
- *\r
- *   This program is distributed in the hope that it will be useful,\r
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- *   GNU General Public License for more details.\r
- *\r
- *   You should have received a copy of the GNU General Public License\r
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-package com.owncloud.android.db;\r
-\r
-import android.content.ContentValues;\r
-import android.content.Context;\r
-import android.database.Cursor;\r
-import android.database.sqlite.SQLiteDatabase;\r
-import android.database.sqlite.SQLiteOpenHelper;\r
-\r
-/**\r
- * Custom database helper for ownCloud\r
- * \r
- * @author Bartek Przybylski\r
- * \r
- */\r
-public class DbHandler {\r
-    private SQLiteDatabase mDB;\r
-    private OpenerHepler mHelper;\r
-    private final String mDatabaseName = "ownCloud";\r
-    private final int mDatabaseVersion = 1;\r
-    \r
-    private final String TABLE_INSTANT_UPLOAD = "instant_upload";\r
-\r
-    public DbHandler(Context context) {\r
-        mHelper = new OpenerHepler(context);\r
-        mDB = mHelper.getWritableDatabase();\r
-    }\r
-\r
-    public void close() {\r
-        mDB.close();\r
-    }\r
-\r
-    public boolean putFileForLater(String filepath, String account) {\r
-        ContentValues cv = new ContentValues();\r
-        cv.put("path", filepath);\r
-        cv.put("account", account);\r
-        return mDB.insert(TABLE_INSTANT_UPLOAD, null, cv) != -1;\r
-    }\r
-    \r
-    public Cursor getAwaitingFiles() {\r
-        return mDB.query(TABLE_INSTANT_UPLOAD, null, null, null, null, null, null);\r
-    }\r
-    \r
-    public void clearFiles() {\r
-        mDB.delete(TABLE_INSTANT_UPLOAD, null, null);\r
-    }\r
-    \r
-    /**\r
-     * \r
-     * @param localPath\r
-     * @param accountName\r
-     * @return true when one or more pendin files was removed\r
-     */\r
-    public boolean removeIUPendingFile(String localPath, String accountName) {\r
-        return mDB.delete(TABLE_INSTANT_UPLOAD,\r
-                          "path = ?",\r
-                          new String[]{ localPath }) != 0;\r
-        \r
-    }\r
-    \r
-    private class OpenerHepler extends SQLiteOpenHelper {\r
-        public OpenerHepler(Context context) {\r
-            super(context, mDatabaseName, null, mDatabaseVersion);\r
-        }\r
-\r
-        @Override\r
-        public void onCreate(SQLiteDatabase db) {\r
-            db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " ("\r
-                       + " _id INTEGET PRIMARY KEY, "\r
-                       + " path TEXT,"\r
-                       + " account TEXT);");\r
-        }\r
-\r
-        @Override\r
-        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\r
-        }\r
-    }\r
-}\r
+/* ownCloud Android client application
+ *   Copyright (C) 2011-2012  Bartek Przybylski
+ *   Copyright (C) 2012-2013 ownCloud Inc.
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License version 2,
+ *   as published by the Free Software Foundation.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package com.owncloud.android.db;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.utils.Log_OC;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+
+/**
+ * Custom database helper for ownCloud
+ * 
+ * @author Bartek Przybylski
+ * 
+ */
+public class DbHandler {
+    private SQLiteDatabase mDB;
+    private OpenerHelper mHelper;
+    private final String mDatabaseName;
+    private final int mDatabaseVersion = 3;
+
+    private final String TABLE_INSTANT_UPLOAD = "instant_upload";
+
+    public static final int UPLOAD_STATUS_UPLOAD_LATER = 0;
+    public static final int UPLOAD_STATUS_UPLOAD_FAILED = 1;
+
+    public DbHandler(Context context) {
+        mDatabaseName = MainApp.getDBName();
+        mHelper = new OpenerHelper(context);
+        mDB = mHelper.getWritableDatabase();
+    }
+
+    public void close() {
+        mDB.close();
+    }
+
+    public boolean putFileForLater(String filepath, String account, String message) {
+        ContentValues cv = new ContentValues();
+        cv.put("path", filepath);
+        cv.put("account", account);
+        cv.put("attempt", UPLOAD_STATUS_UPLOAD_LATER);
+        cv.put("message", message);
+        long result = mDB.insert(TABLE_INSTANT_UPLOAD, null, cv);
+        Log_OC.d(TABLE_INSTANT_UPLOAD, "putFileForLater returns with: " + result + " for file: " + filepath);
+        return result != -1;
+    }
+
+    public int updateFileState(String filepath, Integer status, String message) {
+        ContentValues cv = new ContentValues();
+        cv.put("attempt", status);
+        cv.put("message", message);
+        int result = mDB.update(TABLE_INSTANT_UPLOAD, cv, "path=?", new String[] { filepath });
+        Log_OC.d(TABLE_INSTANT_UPLOAD, "updateFileState returns with: " + result + " for file: " + filepath);
+        return result;
+    }
+
+    public Cursor getAwaitingFiles() {
+        return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt=" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
+    }
+
+    public Cursor getFailedFiles() {
+        return mDB.query(TABLE_INSTANT_UPLOAD, null, "attempt>" + UPLOAD_STATUS_UPLOAD_LATER, null, null, null, null);
+    }
+
+    public void clearFiles() {
+        mDB.delete(TABLE_INSTANT_UPLOAD, null, null);
+    }
+
+    /**
+     * 
+     * @param localPath
+     * @return true when one or more pending files was removed
+     */
+    public boolean removeIUPendingFile(String localPath) {
+        long result = mDB.delete(TABLE_INSTANT_UPLOAD, "path = ?", new String[] { localPath });
+        Log_OC.d(TABLE_INSTANT_UPLOAD, "delete returns with: " + result + " for file: " + localPath);
+        return result != 0;
+
+    }
+
+    private class OpenerHelper extends SQLiteOpenHelper {
+        public OpenerHelper(Context context) {
+            super(context, mDatabaseName, null, mDatabaseVersion);
+        }
+
+        @Override
+        public void onCreate(SQLiteDatabase db) {
+            db.execSQL("CREATE TABLE " + TABLE_INSTANT_UPLOAD + " (" + " _id INTEGER PRIMARY KEY, " + " path TEXT,"
+                    + " account TEXT,attempt INTEGER,message TEXT);");
+        }
+
+        @Override
+        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+            if (oldVersion < 2) {
+                db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN attempt INTEGER;");
+            }
+            db.execSQL("ALTER TABLE " + TABLE_INSTANT_UPLOAD + " ADD COLUMN message TEXT;");
+
+        }
+    }
+}