Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05afe1abcf26fba0cbad87cd758be7d47b07cd2a (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*******************************************************************************
 * Copyright (c) 2004, 2010 Tasktop Technologies 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
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *     Peter Stibrany - improvements for bug 271197
 *******************************************************************************/

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

import java.io.File;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylyn.internal.provisional.commons.ui.WorkbenchUtil;
import org.eclipse.mylyn.internal.tasks.ui.ITasksUiPreferenceConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
import org.eclipse.mylyn.internal.tasks.ui.util.DownloadAttachmentJob;
import org.eclipse.mylyn.internal.tasks.ui.util.Messages;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

/**
 * @author Peter Stibrany
 */
public class SaveAttachmentsAction extends Action {

	public SaveAttachmentsAction(String text) {
		super(text);
	}

	@Override
	public void run() {
		List<ITaskAttachment> attachments = AttachmentUtil.getSelectedAttachments(null);
		if (attachments.isEmpty()) {
			return;
		} else if (attachments.size() == 1) {
			saveSingleAttachment(attachments.get(0));
		} else {
			saveAllAttachments(attachments);
		}
	}

	/**
	 * Displays Save File dialog, then downloads and saves single attachment.
	 */
	private void saveSingleAttachment(ITaskAttachment attachment) {
		FileDialog fileChooser = new FileDialog(WorkbenchUtil.getShell(), SWT.SAVE);
		fileChooser.setFileName(AttachmentUtil.getAttachmentFilename(attachment));

		File initDirectory = getInitialDirectory();
		if (initDirectory != null) {
			fileChooser.setFilterPath(initDirectory.getAbsolutePath());
		}

		String filePath = fileChooser.open();

		// check if the dialog was canceled or an error occurred
		if (filePath == null) {
			return;
		}

		File file = new File(filePath);
		if (file.exists()) {
			if (!MessageDialog.openConfirm(WorkbenchUtil.getShell(), Messages.TasksUiMenus_File_exists_,
					Messages.TasksUiMenus_Overwrite_existing_file_ + file.getName())) {
				return;
			}
		}

		initDirectory = file.getParentFile();
		if (initDirectory != null) {
			saveInitialDirectory(initDirectory.getAbsolutePath());
		}

		DownloadAttachmentJob job = new DownloadAttachmentJob(attachment, file);
		job.setUser(true);
		job.schedule();
	}

	private void saveAllAttachments(List<ITaskAttachment> attachments) {
		DirectoryDialog dialog = new DirectoryDialog(WorkbenchUtil.getShell());
		dialog.setText(Messages.SaveAttachmentsAction_selectDirectory);
		dialog.setMessage(Messages.SaveAttachmentsAction_selectDirectoryHint);

		File initDirectory = getInitialDirectory();
		if (initDirectory != null) {
			dialog.setFilterPath(initDirectory.getAbsolutePath());
		}

		String directoryPath = dialog.open();
		if (directoryPath == null) {
			return;
		}

		saveInitialDirectory(directoryPath);

		final File directory = new File(directoryPath);
		if (!directory.exists()) {
			MessageDialog.openError(WorkbenchUtil.getShell(), Messages.SaveAttachmentsAction_directoryDoesntExist,
					NLS.bind(Messages.SaveAttachmentsAction_directoryDoesntExist0, directoryPath));
			return;
		}

		for (ITaskAttachment attachment : attachments) {
			String filename = AttachmentUtil.getAttachmentFilename(attachment);
			File file = getTargetFile(WorkbenchUtil.getShell(), directory, filename);
			if (file != null) {
				DownloadAttachmentJob job = new DownloadAttachmentJob(attachment, file);
				job.setUser(true);
				job.schedule();
			}
		}
	}

	private File getTargetFile(Shell shell, File directory, String filename) {
		File attachFile = new File(directory, filename);
		while (true) {
			if (!attachFile.exists()) {
				return attachFile;
			}

			boolean overwrite = MessageDialog.openQuestion(
					shell,
					NLS.bind(Messages.SaveAttachmentsAction_overwriteFile0, attachFile.getName()),
					NLS.bind(Messages.SaveAttachmentsAction_fileExists_doYouWantToOverwrite0,
							attachFile.getAbsolutePath()));
			if (overwrite) {
				return attachFile;
			}

			FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
			fileDialog.setFilterPath(directory.getAbsolutePath());
			fileDialog.setFileName(attachFile.getName());

			filename = fileDialog.open();
			if (filename == null) {
				return null;
			}

			attachFile = new File(filename);
		}
	}

	private File getInitialDirectory() {
		String dirName = TasksUiPlugin.getDefault()
				.getPreferenceStore()
				.getString(ITasksUiPreferenceConstants.DEFAULT_ATTACHMENTS_DIRECTORY);

		if (dirName == null || dirName.trim().length() == 0) {
			return null;
		}

		File dirFile = new File(dirName).getAbsoluteFile();

		// if file
		while (dirFile != null && !dirFile.exists()) {
			dirFile = dirFile.getParentFile();
		}

		return dirFile;
	}

	private void saveInitialDirectory(String directory) {
		TasksUiPlugin.getDefault()
				.getPreferenceStore()
				.putValue(ITasksUiPreferenceConstants.DEFAULT_ATTACHMENTS_DIRECTORY, directory);
	}
}

Back to the top