Fixed lack of observance for files that are set as favourites when never were downloa...
[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.files.OwnCloudFileObserver.FileObserverStatusListener.Status;
28 import com.owncloud.android.files.services.FileUploader;
29 import com.owncloud.android.network.OwnCloudClientUtils;
30 import com.owncloud.android.operations.RemoteOperationResult;
31 import com.owncloud.android.operations.SynchronizeFileOperation;
32
33 import eu.alefzero.webdav.WebdavClient;
34
35 import android.accounts.Account;
36 import android.content.Context;
37 import android.content.Intent;
38 import android.os.FileObserver;
39 import android.util.Log;
40
41 public class OwnCloudFileObserver extends FileObserver {
42
43 public static int CHANGES_ONLY = CLOSE_WRITE;
44
45 private static String TAG = OwnCloudFileObserver.class.getSimpleName();
46 private String mPath;
47 private int mMask;
48 private DataStorageManager mStorage;
49 private Account mOCAccount;
50 private OCFile mFile;
51 private Context mContext;
52 private List<FileObserverStatusListener> mListeners;
53
54 public OwnCloudFileObserver(String path) {
55 this(path, ALL_EVENTS);
56 }
57
58 public OwnCloudFileObserver(String path, int mask) {
59 super(path, mask);
60 mPath = path;
61 mMask = mask;
62 mListeners = new LinkedList<FileObserverStatusListener>();
63 }
64
65 public void setAccount(Account account) {
66 mOCAccount = account;
67 }
68
69 public void setStorageManager(DataStorageManager manager) {
70 mStorage = manager;
71 }
72
73 public void setOCFile(OCFile file) {
74 mFile = file;
75 }
76
77 public void setContext(Context context) {
78 mContext = context;
79 }
80
81 public String getPath() {
82 return mPath;
83 }
84
85 public String getRemotePath() {
86 return mFile.getRemotePath();
87 }
88
89 public void addObserverStatusListener(FileObserverStatusListener listener) {
90 mListeners.add(listener);
91 }
92
93 @Override
94 public void onEvent(int event, String path) {
95 Log.d(TAG, "Got file modified with event " + event + " and path " + mPath + ((path != null) ? File.separator + path : ""));
96 if ((event & mMask) == 0) {
97 Log.wtf(TAG, "Incorrect event " + event + " sent for file " + mPath + ((path != null) ? File.separator + path : "") +
98 " with registered for " + mMask + " and original path " +
99 mPath);
100 for (FileObserverStatusListener l : mListeners)
101 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.INCORRECT_MASK);
102 return;
103 }
104 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mOCAccount, mContext);
105 SynchronizeFileOperation sfo = new SynchronizeFileOperation(mFile.getRemotePath(), mStorage, mOCAccount);
106 RemoteOperationResult result = sfo.execute(wc);
107
108 if (result.getExtraData() == Boolean.TRUE) {
109 // inform user about conflict and let him decide what to do
110 for (FileObserverStatusListener l : mListeners)
111 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.CONFLICT);
112 return;
113 }
114
115 for (FileObserverStatusListener l : mListeners)
116 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.SENDING_TO_UPLOADER);
117
118 Intent i = new Intent(mContext, FileUploader.class);
119 i.putExtra(FileUploader.KEY_ACCOUNT, mOCAccount);
120 i.putExtra(FileUploader.KEY_REMOTE_FILE, mFile.getRemotePath());
121 i.putExtra(FileUploader.KEY_LOCAL_FILE, mPath);
122 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
123 i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
124 mContext.startService(i);
125 }
126
127 public interface FileObserverStatusListener {
128 public enum Status {
129 SENDING_TO_UPLOADER,
130 CONFLICT,
131 INCORRECT_MASK
132 }
133
134 public void OnObservedFileStatusUpdate(String localPath,
135 String remotePath,
136 Account account,
137 FileObserverStatusListener.Status status);
138 }
139
140 public OCFile getOCFile() {
141 return mFile;
142 }
143
144 public Account getAccount() {
145 return mOCAccount;
146 }
147
148 }