Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / threading / TransferCollectionBackgroundAction.java
blobd5b02bed867a32e44d54e50a95b71f4440a81b81
1 package ch.cyberduck.core.threading;
3 /*
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
20 import ch.cyberduck.core.Controller;
21 import ch.cyberduck.core.PathCache;
22 import ch.cyberduck.core.ProgressListener;
23 import ch.cyberduck.core.Session;
24 import ch.cyberduck.core.TranscriptListener;
25 import ch.cyberduck.core.TransferCollection;
26 import ch.cyberduck.core.exception.BackgroundException;
27 import ch.cyberduck.core.exception.ConnectionCanceledException;
28 import ch.cyberduck.core.preferences.PreferencesFactory;
29 import ch.cyberduck.core.transfer.Transfer;
30 import ch.cyberduck.core.transfer.TransferListener;
31 import ch.cyberduck.core.transfer.TransferOptions;
32 import ch.cyberduck.core.transfer.TransferQueue;
33 import ch.cyberduck.core.transfer.TransferQueueFactory;
35 import org.apache.log4j.Logger;
37 /**
38 * @version $Id$
40 public class TransferCollectionBackgroundAction extends TransferBackgroundAction {
41 private static final Logger log = Logger.getLogger(TransferCollectionBackgroundAction.class);
43 private TransferQueue queue = TransferQueueFactory.get();
45 private Transfer transfer;
47 private Session session;
49 private ProgressListener progressListener;
51 public TransferCollectionBackgroundAction(final Controller controller,
52 final Session session,
53 final TransferListener transferListener,
54 final ProgressListener progressListener,
55 final TranscriptListener transcriptListener,
56 final Transfer transfer,
57 final TransferOptions options) {
58 super(controller, session, new PathCache(PreferencesFactory.get().getInteger("transfer.cache.size")),
59 transferListener, progressListener, transcriptListener, transfer, options);
60 this.transfer = transfer;
61 this.session = session;
62 this.progressListener = progressListener;
65 @Override
66 public void prepare() throws ConnectionCanceledException {
67 // Update status to running
68 super.prepare();
69 // Wait for slot in queue
70 queue.add(transfer, progressListener);
73 @Override
74 public void cancel() {
75 if(log.isDebugEnabled()) {
76 log.debug(String.format("Cancel background action for transfer %s", transfer));
78 queue.remove(transfer);
79 super.cancel();
82 @Override
83 public void finish() {
84 if(log.isDebugEnabled()) {
85 log.debug(String.format("Finish background action for transfer %s", transfer));
87 queue.remove(transfer);
88 super.finish();
91 @Override
92 public Boolean run() throws BackgroundException {
93 if(super.run()) {
94 // We have our own session independent of any browser.
95 this.close(session);
96 return true;
98 return false;
101 @Override
102 public void cleanup() {
103 if(log.isDebugEnabled()) {
104 log.debug(String.format("Cleanup background action for transfer %s", transfer));
106 super.cleanup();
107 final TransferCollection collection = TransferCollection.defaultCollection();
108 if(PreferencesFactory.get().getBoolean("queue.removeItemWhenComplete")
109 && transfer.isReset() && transfer.isComplete()) {
110 collection.remove(transfer);
112 else {
113 collection.collectionItemChanged(transfer);
117 @Override
118 public Object lock() {
119 // No synchronization with other tasks
120 return null;