Skip to main content
summaryrefslogtreecommitdiffstats
blob: bd4d43847ffe6ffc3e72ac85bffbeb18555cb82c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
 * Copyright (c) 2004 - 2006 Mylar committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.actions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.internal.monitor.core.util.StatusManager;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.IAttachmentHandler;
import org.eclipse.mylyn.tasks.core.RepositoryAttachment;
import org.eclipse.mylyn.tasks.core.RepositoryStatus;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;

/**
 * @author Steffen Pingel
 */
public class DownloadAttachmentJob extends Job {

	private final RepositoryAttachment attachment;

	private final File targetFile;

	public DownloadAttachmentJob(RepositoryAttachment attachment, File targetFile) {
		super("Downloading Attachment");

		if (attachment == null) {
			throw new IllegalArgumentException("attachment must not be null");
		}
		if (targetFile == null) {
			throw new IllegalArgumentException("target must not be null");
		}

		this.attachment = attachment;
		this.targetFile = targetFile;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(attachment.getRepositoryKind(),
				this.attachment.getRepositoryUrl());
		AbstractRepositoryConnector connector = TasksUiPlugin.getRepositoryManager().getRepositoryConnector(
				this.attachment.getRepositoryKind());
		IAttachmentHandler handler = connector.getAttachmentHandler();
		if (handler == null) {
			return new RepositoryStatus(repository, IStatus.INFO, TasksUiPlugin.PLUGIN_ID, RepositoryStatus.ERROR_INTERNAL,
					"The repository does not support attachments.");
		}

		FileOutputStream out = null;
		try {
			out = new FileOutputStream(this.targetFile);
			handler.downloadAttachment(repository, attachment, out, monitor);
		} catch (final CoreException e) {
			StatusManager.displayStatus("Download Attachment", e.getStatus());
			return Status.OK_STATUS;
		} catch (IOException e) {
			return new RepositoryStatus(repository, IStatus.WARNING, TasksUiPlugin.PLUGIN_ID, RepositoryStatus.ERROR_IO,
					"Error while writing to attachment file.", e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					StatusManager.fail(e, "Could not close attachment file: " + this.targetFile.getAbsolutePath(),
							false);
				}
			}
		}

		return Status.OK_STATUS;
	}

}

Back to the top