Skip to main content
summaryrefslogtreecommitdiffstats
blob: 134b5ddbbf080d9e9f6fbe59e7d22cc650d0c375 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.internal.tasklist.ui.wizards;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.internal.core.MylarContextManager;
import org.eclipse.mylar.internal.core.util.MylarStatusHandler;
import org.eclipse.mylar.internal.core.util.ZipFileUtil;
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.internal.tasklist.TaskList;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.ITaskCategory;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

/**
 * Wizard for exporting tasklist data files to the file system. This wizard uses
 * a single page: TaskDataExportWizardPage
 * 
 * @author Wesley Coelho
 * @author Mik Kersten
 */
public class TaskDataExportWizard extends Wizard implements IExportWizard {

	/**
	 * The name of the dialog store's section associated with the task data
	 * export wizard
	 */
	private final static String SETTINGS_SECTION = "org.eclipse.mylar.tasklist.ui.exportWizard";

	private final static String ZIP_FILE_PREFIX = "mylardata";

	private final static String ZIP_FILE_EXTENSION = ".zip";

	private final static String WINDOW_TITLE = "Export";

	private TaskDataExportWizardPage exportPage = null;

	public static String getZipFileName() {
		String fomratString = "yyyy-MM-dd";
		SimpleDateFormat format = new SimpleDateFormat(fomratString, Locale.ENGLISH);
		String date = format.format(new Date());
		return ZIP_FILE_PREFIX + "-" + date + ZIP_FILE_EXTENSION;
	}

	public TaskDataExportWizard() {
		IDialogSettings masterSettings = MylarTaskListPlugin.getDefault().getDialogSettings();
		setDialogSettings(getSettingsSection(masterSettings));
		setNeedsProgressMonitor(true);
		setWindowTitle(WINDOW_TITLE);
	}

	/**
	 * Finds or creates a dialog settings section that is used to make the
	 * dialog control settings persistent
	 */
	public IDialogSettings getSettingsSection(IDialogSettings master) {
		IDialogSettings settings = master.getSection(SETTINGS_SECTION);
		if (settings == null) {
			settings = master.addNewSection(SETTINGS_SECTION);
		}
		return settings;
	}

	public void addPages() {
		exportPage = new TaskDataExportWizardPage();
		exportPage.setWizard(this);
		addPage(exportPage);
	}

	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// no initialization needed
	}

	public boolean canFinish() {
		return exportPage.isPageComplete();
	}

	/**
	 * Called when the user clicks finish. Saves the task data. Waits until all
	 * overwrite decisions have been made before starting to save files. If any
	 * overwrite is canceled, no files are saved and the user must adjust the
	 * dialog.
	 */
	public boolean performFinish() {
		boolean overwrite = exportPage.overwrite();
		boolean zip = exportPage.zip();

		// Get file paths to check for existence
		String destDir = exportPage.getDestinationDirectory();
		final File destDirFile = new File(destDir);
		if (!destDirFile.exists() || !destDirFile.isDirectory()) {
			// This should never happen
			MylarStatusHandler.fail(new Exception("File Export Exception"),
					"Could not export data because specified location does not exist or is not a folder", true);
			return false;
		}

		final File destTaskListFile = new File(destDir + File.separator + MylarTaskListPlugin.DEFAULT_TASK_LIST_FILE);
		final File destActivationHistoryFile = new File(destDir + File.separator
				+ MylarContextManager.CONTEXT_HISTORY_FILE_NAME + MylarContextManager.CONTEXT_FILE_EXTENSION);
		final File destZipFile = new File(destDir + File.separator + getZipFileName());

		// Prompt the user to confirm if ANY of the save operations will cause
		// an overwrite
		if (!overwrite) {

			if (zip) {
				if (destZipFile.exists()) {
					if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace", "The zip file "
							+ destZipFile.getPath() + " already exists. Do you want to overwrite it?")) {
						return false;
					}
				}
			} else {
				if (exportPage.exportTaskList() && destTaskListFile.exists()) {
					if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace", "The task list file "
							+ destTaskListFile.getPath() + " already exists. Do you want to overwrite it?")) {
						return false;
					}
				}

				if (exportPage.exportActivationHistory() && destActivationHistoryFile.exists()) {
					if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace",
							"The task activation history file " + destActivationHistoryFile.getPath()
									+ " already exists. Do you want to overwrite it?")) {
						return false;
					}
				}

				if (exportPage.exportTaskContexts()) {
					for (ITask task : getAllTasks()) {
						File contextFile = MylarPlugin.getContextManager()
								.getFileForContext(task.getHandleIdentifier());
						File destTaskFile = new File(destDir + File.separator + contextFile.getName());
						if (destTaskFile.exists()) {
							if (!MessageDialog.openConfirm(getShell(), "Confirm File Replace",
									"Task context files already exist in " + destDir
											+ ". Do you want to overwrite them?")) {
								return false;
							} else {
								break;
							}
						}
					}
				}
			}
		}

		FileCopyJob job = new FileCopyJob(destZipFile, destTaskListFile, destActivationHistoryFile);
		IProgressService service = PlatformUI.getWorkbench().getProgressService();

		try {
			service.run(true, false, job);
		} catch (InvocationTargetException e) {
			MylarStatusHandler.fail(e, "Could not export files", true);
		} catch (InterruptedException e) {
			MylarStatusHandler.fail(e, "Could not export files", true);
		}

		exportPage.saveSettings();
		return true;
	}

	/** Job that performs the file copying and zipping */
	class FileCopyJob implements IRunnableWithProgress {

		private static final String JOB_LABEL = "Exporting Data";

		private File destZipFile = null;

		private File destTaskListFile = null;

		private File destActivationHistoryFile = null;

		private boolean zip;

		private boolean exportTaskList;

		private boolean exportActivationHistory;

		private boolean exportTaskContexts;

		private String destinationDirectory;

		public FileCopyJob(File destZipFile, File destTaskListFile, File destActivationHistoryFile) {
			this.destZipFile = destZipFile;
			this.destTaskListFile = destTaskListFile;
			this.destActivationHistoryFile = destActivationHistoryFile;

			// Get parameters here to avoid accessing the UI thread
			this.zip = exportPage.zip();
			this.exportTaskList = exportPage.exportTaskList();
			this.exportActivationHistory = exportPage.exportActivationHistory();
			this.exportTaskContexts = exportPage.exportTaskContexts();
			this.destinationDirectory = exportPage.getDestinationDirectory();
		}

		public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
			List<ITask> tasks = getAllTasks();
			monitor.beginTask(JOB_LABEL, tasks.size() + 2);

			// List of files to add to the zip archive
			List<File> filesToZip = new ArrayList<File>();

			// Map of file paths used to avoid duplicates
			Map<String, String> filesToZipMap = new HashMap<String, String>();

			if (exportTaskList) {
				MylarTaskListPlugin.getTaskListManager().saveTaskList();

				String sourceTaskListPath = MylarPlugin.getDefault().getDataDirectory() + File.separator
						+ MylarTaskListPlugin.DEFAULT_TASK_LIST_FILE;
				File sourceTaskListFile = new File(sourceTaskListPath);

				if (zip) {
					filesToZip.add(sourceTaskListFile);
				} else {
					if (!copy(sourceTaskListFile, destTaskListFile)) {
						MylarStatusHandler.fail(new Exception("Export Exception"), "Could not export task list file.",
								false);
					}
					monitor.worked(1);
				}

			}

			if (exportActivationHistory) {
				try {
					File sourceActivationHistoryFile = new File(MylarPlugin.getDefault().getDataDirectory()
							+ File.separator + MylarContextManager.CONTEXT_HISTORY_FILE_NAME
							+ MylarContextManager.CONTEXT_FILE_EXTENSION);

					MylarPlugin.getContextManager().saveActivityHistoryContext();

					if (zip) {
						filesToZip.add(sourceActivationHistoryFile);
					} else {
						copy(sourceActivationHistoryFile, destActivationHistoryFile);
						monitor.worked(1);
					}
				} catch (RuntimeException e) {
					MylarStatusHandler.fail(e, "Could not export activity history context file", true);
				}
			}

			if (exportTaskContexts) {
				boolean errorDisplayed = false; // Prevent many repeated error
				// messages
				for (ITask task : tasks) {

					if (!MylarPlugin.getContextManager().hasContext(task.getHandleIdentifier())) {
						continue; // Tasks without a context have no file to
						// copy
					}

					File contextFile = MylarPlugin.getContextManager().getFileForContext(task.getHandleIdentifier());

					File destTaskFile = new File(destinationDirectory + File.separator + contextFile.getName());
					File sourceTaskFile = contextFile;
					// new File(MylarPlugin.getDefault().getDataDirectory() +
					// File.separator + task.getContextPath()
					// + MylarContextManager.CONTEXT_FILE_EXTENSION);

					if (zip) {
						if (!filesToZipMap.containsKey(task.getHandleIdentifier())) {
							filesToZip.add(sourceTaskFile);
							filesToZipMap.put(task.getHandleIdentifier(), null);
						}
					} else {
						if (!copy(sourceTaskFile, destTaskFile) && !errorDisplayed) {
							MylarStatusHandler.fail(new Exception("Export Exception: " + sourceTaskFile.getPath()
									+ " -> " + destTaskFile.getPath()),
									"Could not export one or more task context files.", true);
							errorDisplayed = true;
						}
						monitor.worked(1);
					}
				}
			}

			if (zip) {
				try {
					if (destZipFile.exists()) {
						destZipFile.delete();
					}
					ZipFileUtil.createZipFile(destZipFile, filesToZip, monitor);
				} catch (Exception e) {
					MylarStatusHandler.fail(e, "Could not create zip file.", true);
				}
			}
			monitor.done();

		}
	}

	/** Returns all tasks in the task list root or a category in the task list */
	protected List<ITask> getAllTasks() {
		List<ITask> allTasks = new ArrayList<ITask>();
		TaskList taskList = MylarTaskListPlugin.getTaskListManager().getTaskList();

		allTasks.addAll(taskList.getRootTasks());

		for (ITaskCategory category : taskList.getCategories()) {
			allTasks.addAll(category.getChildren());
		}

		return allTasks;
	}

	// Note: Copied from MylarTaskListPlugin
	private boolean copy(File src, File dst) {
		try {
			InputStream in = new FileInputStream(src);
			OutputStream out = new FileOutputStream(dst);

			// Transfer bytes from in to out
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return true;
		} catch (IOException ioe) {
			return false;
		}
	}

}

Back to the top