Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2004, 2008 Tasktop Technologies and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Tasktop Technologies - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.mylyn.internal.tasks.ui.util; |
13 |
|
14 |
import java.io.File; |
15 |
import java.util.List; |
16 |
|
17 |
import org.eclipse.core.runtime.preferences.ConfigurationScope; |
18 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
19 |
import org.eclipse.jface.action.Action; |
20 |
import org.eclipse.jface.dialogs.MessageDialog; |
21 |
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; |
22 |
import org.eclipse.mylyn.tasks.core.ITaskAttachment; |
23 |
import org.eclipse.osgi.util.NLS; |
24 |
import org.eclipse.swt.SWT; |
25 |
import org.eclipse.swt.widgets.DirectoryDialog; |
26 |
import org.eclipse.swt.widgets.FileDialog; |
27 |
import org.eclipse.swt.widgets.Shell; |
28 |
import org.osgi.service.prefs.BackingStoreException; |
29 |
|
30 |
final class SaveAttachmentsAction extends Action { |
31 |
private static final String DEFAULT_DIRECTORY = "saveAttachments.defaultDirectory"; //$NON-NLS-1$ |
32 |
|
33 |
private static final String ATTACHMENT_DEFAULT_NAME = "attachment"; //$NON-NLS-1$ |
34 |
|
35 |
private static final String CTYPE_ZIP = "zip"; //$NON-NLS-1$ |
36 |
|
37 |
private static final String CTYPE_OCTET_STREAM = "octet-stream"; //$NON-NLS-1$ |
38 |
|
39 |
private static final String CTYPE_TEXT = "text"; //$NON-NLS-1$ |
40 |
|
41 |
private static final String CTYPE_HTML = "html"; //$NON-NLS-1$ |
42 |
|
43 |
SaveAttachmentsAction(String text) { |
44 |
super(text); |
45 |
} |
46 |
|
47 |
@Override |
48 |
public void run() { |
49 |
List<ITaskAttachment> attachments = AttachmentUtil.getSelectedAttachments(); |
50 |
if (attachments.isEmpty()) { |
51 |
return; |
52 |
} |
53 |
|
54 |
if (attachments.size() == 1) { |
55 |
saveSingleAttachment(attachments.get(0)); |
56 |
} else { |
57 |
saveAllAttachments(attachments); |
58 |
} |
59 |
} |
60 |
|
61 |
/* |
62 |
* Single attachment saving -- displays Save File dialog, then downloads and saves single attachment |
63 |
*/ |
64 |
private void saveSingleAttachment(ITaskAttachment attachment) { |
65 |
/* Launch Browser */ |
66 |
FileDialog fileChooser = new FileDialog(TasksUiInternal.getShell(), SWT.SAVE); |
67 |
fileChooser.setFileName(getAttachmentFilename(attachment)); |
68 |
|
69 |
File initDirectory = getInitialDirectory(); |
70 |
if (initDirectory != null) { |
71 |
fileChooser.setFilterPath(initDirectory.getAbsolutePath()); |
72 |
} |
73 |
|
74 |
String filePath = fileChooser.open(); |
75 |
|
76 |
// check if the dialog was canceled or an error occurred |
77 |
if (filePath == null) { |
78 |
return; |
79 |
} |
80 |
|
81 |
File file = new File(filePath); |
82 |
if (file.exists()) { |
83 |
if (!MessageDialog.openConfirm(TasksUiInternal.getShell(), Messages.TasksUiMenus_File_exists_, |
84 |
Messages.TasksUiMenus_Overwrite_existing_file_ + file.getName())) { |
85 |
return; |
86 |
} |
87 |
} |
88 |
|
89 |
initDirectory = file.getParentFile(); |
90 |
if (initDirectory != null) { |
91 |
saveInitialDirectory(initDirectory.getAbsolutePath()); |
92 |
} |
93 |
|
94 |
DownloadAttachmentJob job = new DownloadAttachmentJob(attachment, file); |
95 |
job.setUser(true); |
96 |
job.schedule(); |
97 |
} |
98 |
|
99 |
private void saveAllAttachments(List<ITaskAttachment> attachments) { |
100 |
DirectoryDialog dialog = new DirectoryDialog(TasksUiInternal.getShell()); |
101 |
dialog.setText(Messages.SaveAttachmentsAction_selectDirectory); |
102 |
dialog.setMessage(Messages.SaveAttachmentsAction_selectDirectoryHint); |
103 |
|
104 |
File initDirectory = getInitialDirectory(); |
105 |
if (initDirectory != null) { |
106 |
dialog.setFilterPath(initDirectory.getAbsolutePath()); |
107 |
} |
108 |
|
109 |
String directoryPath = dialog.open(); |
110 |
if (directoryPath == null) { |
111 |
return; |
112 |
} |
113 |
|
114 |
saveInitialDirectory(directoryPath); |
115 |
|
116 |
final File directory = new File(directoryPath); |
117 |
if (!directory.exists()) { |
118 |
MessageDialog.openError(TasksUiInternal.getShell(), Messages.SaveAttachmentsAction_directoryDoesntExist, |
119 |
NLS.bind(Messages.SaveAttachmentsAction_directoryDoesntExist0, directoryPath)); |
120 |
return; |
121 |
} |
122 |
|
123 |
for (ITaskAttachment a : attachments) { |
124 |
String filename = getAttachmentFilename(a); |
125 |
|
126 |
File file = getTargetFile(TasksUiInternal.getShell(), directory, filename); |
127 |
|
128 |
DownloadAttachmentJob job = new DownloadAttachmentJob(a, file); |
129 |
job.setUser(true); |
130 |
job.schedule(); |
131 |
} |
132 |
} |
133 |
|
134 |
private File getTargetFile(Shell shell, File directory, String filename) { |
135 |
File attachFile = new File(directory, filename); |
136 |
|
137 |
while (true) { |
138 |
if (!attachFile.exists()) { |
139 |
return attachFile; |
140 |
} |
141 |
|
142 |
boolean overwrite = MessageDialog.openQuestion(shell, NLS.bind( |
143 |
Messages.SaveAttachmentsAction_overwriteFile0, attachFile.getName()), NLS.bind( |
144 |
Messages.SaveAttachmentsAction_fileExists_doYouWantToOverwrite0, attachFile.getAbsolutePath())); |
145 |
|
146 |
if (overwrite) { |
147 |
return attachFile; |
148 |
} |
149 |
|
150 |
FileDialog fileDialog = new FileDialog(shell, SWT.SAVE); |
151 |
fileDialog.setFilterPath(directory.getAbsolutePath()); |
152 |
filename = fileDialog.open(); |
153 |
|
154 |
if (filename == null) { |
155 |
return null; |
156 |
} |
157 |
|
158 |
attachFile = new File(filename); |
159 |
} |
160 |
} |
161 |
|
162 |
private String getAttachmentFilename(ITaskAttachment attachment) { |
163 |
String fname = attachment.getFileName(); |
164 |
// default name if none is found |
165 |
if (fname.equals("")) { //$NON-NLS-1$ |
166 |
String ctype = attachment.getContentType(); |
167 |
if (ctype.endsWith(CTYPE_HTML)) { |
168 |
fname = ATTACHMENT_DEFAULT_NAME + ".html"; //$NON-NLS-1$ |
169 |
} else if (ctype.startsWith(CTYPE_TEXT)) { |
170 |
fname = ATTACHMENT_DEFAULT_NAME + ".txt"; //$NON-NLS-1$ |
171 |
} else if (ctype.endsWith(CTYPE_OCTET_STREAM)) { |
172 |
fname = ATTACHMENT_DEFAULT_NAME; |
173 |
} else if (ctype.endsWith(CTYPE_ZIP)) { |
174 |
fname = ATTACHMENT_DEFAULT_NAME + "." + CTYPE_ZIP; //$NON-NLS-1$ |
175 |
} else { |
176 |
fname = ATTACHMENT_DEFAULT_NAME + "." + ctype.substring(ctype.indexOf("/") + 1); //$NON-NLS-1$ //$NON-NLS-2$ |
177 |
} |
178 |
} |
179 |
return fname; |
180 |
} |
181 |
|
182 |
private File getInitialDirectory() { |
183 |
IEclipsePreferences prefs = new ConfigurationScope().getNode(TasksUiPlugin.ID_PLUGIN); |
184 |
String dir = prefs.get(DEFAULT_DIRECTORY, null); |
185 |
|
186 |
if (dir == null) { |
187 |
return null; |
188 |
} |
189 |
|
190 |
File file = new File(dir).getAbsoluteFile(); |
191 |
while (file != null && !file.exists()) { |
192 |
file = file.getParentFile(); |
193 |
} |
194 |
|
195 |
return file; |
196 |
} |
197 |
|
198 |
private void saveInitialDirectory(String directory) { |
199 |
IEclipsePreferences prefs = new ConfigurationScope().getNode(TasksUiPlugin.ID_PLUGIN); |
200 |
prefs.put(DEFAULT_DIRECTORY, directory); |
201 |
try { |
202 |
prefs.flush(); |
203 |
} catch (BackingStoreException e) { |
204 |
// ignore |
205 |
} |
206 |
} |
207 |
} |