Download button in file details view upgraded to sync file content in both directions
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / OwnCloudFileObserver.java
1 /* ownCloud Android client application
2 * Copyright (C) 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
19 package com.owncloud.android.files;
20
21 import java.io.File;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import com.owncloud.android.datamodel.DataStorageManager;
26 import com.owncloud.android.datamodel.OCFile;
27 import com.owncloud.android.network.OwnCloudClientUtils;
28 import com.owncloud.android.operations.RemoteOperationResult;
29 import com.owncloud.android.operations.SynchronizeFileOperation;
30
31 import eu.alefzero.webdav.WebdavClient;
32
33 import android.accounts.Account;
34 import android.content.Context;
35 import android.os.FileObserver;
36 import android.util.Log;
37
38 public class OwnCloudFileObserver extends FileObserver {
39
40 public static int CHANGES_ONLY = CLOSE_WRITE;
41
42 private static String TAG = OwnCloudFileObserver.class.getSimpleName();
43 private String mPath;
44 private int mMask;
45 private DataStorageManager mStorage;
46 private Account mOCAccount;
47 private OCFile mFile;
48 private Context mContext;
49 private List<FileObserverStatusListener> mListeners;
50
51 public OwnCloudFileObserver(String path) {
52 this(path, ALL_EVENTS);
53 }
54
55 public OwnCloudFileObserver(String path, int mask) {
56 super(path, mask);
57 mPath = path;
58 mMask = mask;
59 mListeners = new LinkedList<FileObserverStatusListener>();
60 }
61
62 public void setAccount(Account account) {
63 mOCAccount = account;
64 }
65
66 public void setStorageManager(DataStorageManager manager) {
67 mStorage = manager;
68 }
69
70 public void setOCFile(OCFile file) {
71 mFile = file;
72 }
73
74 public void setContext(Context context) {
75 mContext = context;
76 }
77
78 public String getPath() {
79 return mPath;
80 }
81
82 public String getRemotePath() {
83 return mFile.getRemotePath();
84 }
85
86 public void addObserverStatusListener(FileObserverStatusListener listener) {
87 mListeners.add(listener);
88 }
89
90 @Override
91 public void onEvent(int event, String path) {
92 Log.d(TAG, "Got file modified with event " + event + " and path " + mPath + ((path != null) ? File.separator + path : ""));
93 if ((event & mMask) == 0) {
94 Log.wtf(TAG, "Incorrect event " + event + " sent for file " + mPath + ((path != null) ? File.separator + path : "") +
95 " with registered for " + mMask + " and original path " +
96 mPath);
97 /* Unexpected event that will be ignored; no reason to propagate it
98 for (FileObserverStatusListener l : mListeners)
99 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.INCORRECT_MASK);
100 */
101 return;
102 }
103 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mOCAccount, mContext);
104 SynchronizeFileOperation sfo = new SynchronizeFileOperation(getRemotePath(), mStorage, mOCAccount, true, false, mContext);
105 RemoteOperationResult result = sfo.execute(wc);
106 for (FileObserverStatusListener l : mListeners) {
107 l.onObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, result);
108 }
109
110 }
111
112 public interface FileObserverStatusListener {
113 public void onObservedFileStatusUpdate(String localPath,
114 String remotePath,
115 Account account,
116 RemoteOperationResult result);
117 }
118
119 public OCFile getOCFile() {
120 return mFile;
121 }
122
123 public Account getAccount() {
124 return mOCAccount;
125 }
126
127 }