~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.ui/src/com/bdaum/zoom/ui/internal/job/FolderWatchJob.java

  • Committer: bdaum
  • Date: 2015-12-26 10:21:51 UTC
  • Revision ID: berthold.daum@bdaum.de-20151226102151-44f1j5113167thb9
VersionĀ 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import java.util.HashMap;
26
26
import java.util.List;
27
27
import java.util.Map;
28
 
import java.util.StringTokenizer;
29
28
 
30
29
import org.eclipse.core.runtime.IProgressMonitor;
31
30
import org.eclipse.core.runtime.IStatus;
39
38
import com.bdaum.zoom.cat.model.meta.WatchedFolder;
40
39
import com.bdaum.zoom.cat.model.meta.WatchedFolderImpl;
41
40
import com.bdaum.zoom.core.Constants;
42
 
import com.bdaum.zoom.core.Core;
43
41
import com.bdaum.zoom.core.IRecipeDetector;
44
42
import com.bdaum.zoom.core.IVolumeManager;
45
43
import com.bdaum.zoom.core.internal.CoreActivator;
47
45
import com.bdaum.zoom.core.internal.FileWatchManager;
48
46
import com.bdaum.zoom.core.internal.IPreferenceUpdater;
49
47
import com.bdaum.zoom.fileMonitor.internal.filefilter.FilterChain;
50
 
import com.bdaum.zoom.image.ImageConstants;
51
 
import com.bdaum.zoom.program.BatchConstants;
52
48
import com.bdaum.zoom.ui.internal.PreferencesUpdater;
53
49
import com.bdaum.zoom.ui.internal.UiActivator;
54
50
import com.bdaum.zoom.ui.internal.UiUtilities;
56
52
@SuppressWarnings("restriction")
57
53
public class FolderWatchJob extends SynchronizeCatJob {
58
54
 
 
55
        private static final String XMPEXTENSION = ".xmp"; //$NON-NLS-1$
 
56
        private static final int XMPEXTLEN = XMPEXTENSION.length();
59
57
        private FileNameExtensionFilter filter;
60
 
        private FileNameExtensionFilter xmpfilter;
61
58
        private WatchedFolder[] watchedFolders;
 
59
        private CoreActivator activator;
62
60
 
63
61
        public FolderWatchJob(WatchedFolder[] watchedFolders) {
64
62
                super(Messages.FolderWatchJob_watching_folders);
65
63
                this.watchedFolders = watchedFolders;
66
 
                filter = new FileNameExtensionFilter(
67
 
                                ImageConstants.getSupportedImageFileExtensions(true));
68
 
                xmpfilter = new FileNameExtensionFilter(BatchConstants.XMPEXTENSION,
69
 
                                false);
70
64
        }
71
65
 
72
66
        /*
84
78
 
85
79
        @Override
86
80
        protected IStatus run(IProgressMonitor monitor) {
87
 
                CoreActivator activator = CoreActivator.getDefault();
 
81
                activator = CoreActivator.getDefault();
 
82
                filter = activator.getFilenameExtensionFilter();
88
83
                if (activator.getDbManager().getFile() != null) {
89
84
                        FileWatchManager fileWatchManager = activator.getFileWatchManager();
90
85
                        List<IRecipeDetector> activeRecipeDetectors = activator
101
96
        private void watchFolders(IProgressMonitor monitor,
102
97
                        List<IRecipeDetector> activeRecipeDetectors,
103
98
                        FileWatchManager fileWatchManager) {
104
 
                CoreActivator activator = CoreActivator.getDefault();
105
99
                Meta meta = activator.getDbManager().getMeta(true);
106
100
                if (meta.getPauseFolderWatch())
107
101
                        return;
154
148
                        List<IRecipeDetector> activeRecipeDetectors,
155
149
                        FileWatchManager fileWatchManager) {
156
150
                try {
157
 
                        File[] members = folder.listFiles(filter);
 
151
                        File[] members = folder.listFiles();
158
152
                        if (members != null && members.length > 0) {
 
153
                                List<File> outdatedFiles = new ArrayList<File>();
 
154
                                List<File> newFiles = new ArrayList<File>();
159
155
                                int incr = work / members.length;
160
156
                                monitor.subTask(folder.getPath());
161
 
                                File[] xmpmembers = folder.listFiles(xmpfilter);
162
157
                                Map<String, File> xmpMap = null;
163
 
                                if (xmpmembers != null && xmpmembers.length > 0) {
164
 
                                        xmpMap = new HashMap<String, File>(
165
 
                                                        xmpmembers.length * 3 / 2);
166
 
                                        for (File member : xmpmembers) {
167
 
                                                String xmpName = member.getName();
168
 
                                                xmpMap.put(xmpName.substring(0, xmpName.length()
169
 
                                                                - (BatchConstants.XMPEXTENSION.length + 1)),
170
 
                                                                member);
171
 
                                        }
172
 
                                }
173
 
                                CoreActivator activator = CoreActivator.getDefault();
174
 
                                List<File> outdatedFiles = new ArrayList<File>();
175
 
                                List<File> newFiles = new ArrayList<File>();
 
158
                                boolean xmp = false;
176
159
                                for (File member : members) {
177
160
                                        if (monitor.isCanceled())
178
161
                                                break;
188
171
                                                                                System.currentTimeMillis(),
189
172
                                                                                observedMember, filterChain,
190
173
                                                                                activeRecipeDetectors, fileWatchManager);
191
 
                                                        work -= incr;
192
174
                                                }
193
175
                                        } else {
194
176
                                                monitor.worked(incr);
195
 
                                                work -= incr;
196
 
                                                if (fileWatchManager != null
197
 
                                                                && fileWatchManager.isFileIgnored(member))
198
 
                                                        continue;
199
 
                                                if ((filterChain == null || filterChain.accept(member,
200
 
                                                                false))) {
201
 
                                                        yield();
202
 
                                                        if (observedFolder.getTransfer())
203
 
                                                                newFiles.add(member);
204
 
                                                        else
205
 
                                                                activator.classifyFile(member, newFiles,
206
 
                                                                                outdatedFiles, xmpMap,
207
 
                                                                                activeRecipeDetectors);
 
177
                                                if (filter.accept(member)) {
 
178
                                                        if (fileWatchManager != null
 
179
                                                                        && fileWatchManager.isFileIgnored(member))
 
180
                                                                continue;
 
181
                                                        if ((filterChain == null || filterChain.accept(
 
182
                                                                        member, false))) {
 
183
                                                                yield();
 
184
                                                                if (!xmp) {
 
185
                                                                        xmp = true;
 
186
                                                                        xmpMap = new HashMap<String, File>(
 
187
                                                                                        members.length * 3 / 2);
 
188
                                                                        for (File xmpMember : members) {
 
189
                                                                                String xmpName = xmpMember.getName();
 
190
                                                                                if (xmpName.toLowerCase().endsWith(
 
191
                                                                                                XMPEXTENSION))
 
192
                                                                                        xmpMap.put(xmpName.substring(0,
 
193
                                                                                                        xmpName.length()
 
194
                                                                                                                        - XMPEXTLEN),
 
195
                                                                                                        xmpMember);
 
196
                                                                        }
 
197
                                                                }
 
198
                                                                if (observedFolder.getTransfer())
 
199
                                                                        newFiles.add(member);
 
200
                                                                else
 
201
                                                                        activator.classifyFile(member, newFiles,
 
202
                                                                                        outdatedFiles, xmpMap,
 
203
                                                                                        activeRecipeDetectors);
 
204
                                                        }
208
205
                                                }
209
206
                                        }
 
207
                                        work -= incr;
210
208
                                }
211
209
                                if (!newFiles.isEmpty() || !outdatedFiles.isEmpty()) {
212
210
                                        new ChangeProcessor(newFiles, outdatedFiles, null,
217
215
                        }
218
216
                        observedFolder.setLastObservation(timeOfUpdate);
219
217
                } catch (Throwable t) {
220
 
                        UiActivator activator = UiActivator.getDefault();
221
 
                        if (activator != null)
222
 
                                activator
 
218
                        UiActivator ui = UiActivator.getDefault();
 
219
                        if (ui != null)
 
220
                                ui
223
221
                                                .logError(
224
222
                                                                Messages.FolderWatchJob_internal_error_synchronizing,
225
223
                                                                t);
242
240
                return super.getAdapter(adapter);
243
241
        }
244
242
 
245
 
        protected List<String> parseString(String stringList) {
246
 
                IVolumeManager volumeManager = Core.getCore().getVolumeManager();
247
 
                StringTokenizer st = new StringTokenizer(stringList, File.pathSeparator
248
 
                                + "\n\r");//$NON-NLS-1$
249
 
                ArrayList<String> v = new ArrayList<String>();
250
 
                String dir = null;
251
 
                int i = 0;
252
 
                while (st.hasMoreTokens()) {
253
 
                        if ((i++ % 2) == 0)
254
 
                                dir = st.nextToken();
255
 
                        else {
256
 
                                String volume = st.nextToken();
257
 
                                while (volume.startsWith("{")) //$NON-NLS-1$
258
 
                                        volume = volume.substring(1);
259
 
                                File root = volumeManager.volumeToRoot(volume);
260
 
                                if (root != null) {
261
 
                                        File file = new File(dir);
262
 
                                        v.add(new File(root, file.getAbsolutePath().substring(
263
 
                                                        volumeManager.getRootFile(file).getAbsolutePath()
264
 
                                                                        .length())).getAbsolutePath());
265
 
                                } else
266
 
                                        v.add(dir);
267
 
                        }
268
 
                }
269
 
                return v;
270
 
        }
 
243
//      protected List<String> parseString(String stringList) {
 
244
//              IVolumeManager volumeManager = Core.getCore().getVolumeManager();
 
245
//              StringTokenizer st = new StringTokenizer(stringList, File.pathSeparator
 
246
//                              + "\n\r");//$NON-NLS-1$
 
247
//              ArrayList<String> v = new ArrayList<String>();
 
248
//              String dir = null;
 
249
//              int i = 0;
 
250
//              while (st.hasMoreTokens()) {
 
251
//                      if ((i++ % 2) == 0)
 
252
//                              dir = st.nextToken();
 
253
//                      else {
 
254
//                              String volume = st.nextToken();
 
255
//                              while (volume.startsWith("{")) //$NON-NLS-1$
 
256
//                                      volume = volume.substring(1);
 
257
//                              File root = volumeManager.volumeToRoot(volume);
 
258
//                              if (root != null) {
 
259
//                                      File file = new File(dir);
 
260
//                                      v.add(new File(root, file.getAbsolutePath().substring(
 
261
//                                                      volumeManager.getRootFile(file).getAbsolutePath()
 
262
//                                                                      .length())).getAbsolutePath());
 
263
//                              } else
 
264
//                                      v.add(dir);
 
265
//                      }
 
266
//              }
 
267
//              return v;
 
268
//      }
271
269
 
272
270
}