Skip to main content
summaryrefslogtreecommitdiffstats
blob: c9013a480d5dd0b43addb9dc86235809995c3cce (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
/*******************************************************************************
 * 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
 *******************************************************************************/

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

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.mylyn.commons.net.Policy;
import org.eclipse.mylyn.internal.commons.core.ZipFileUtil;
import org.eclipse.mylyn.internal.tasks.core.ITasksCoreConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;

/**
 * Zips task data up to specified directly and filename.
 * 
 * @author Wesley Coelho
 * @author Mik Kersten
 * @author Rob Elves TODO: Move into internal.tasks.core
 */
@SuppressWarnings("restriction")
public class TaskDataExportOperation implements IRunnableWithProgress {

	private static final String EXPORT_JOB_LABEL = Messages.TaskDataExportOperation_exporting_task_data;

	private static final Pattern excludePattern = Pattern.compile("(?:^\\.|^monitor-log.xml\\z|^tasklist.xml.zip\\z|attachments\\z|backup\\z)"); //$NON-NLS-1$

	private final String destinationDirectory;

	private final String destinationFilename;

	public TaskDataExportOperation(String destinationDirectory, String destinationFilename) {
		this.destinationFilename = destinationFilename;
		this.destinationDirectory = destinationDirectory;
	}

	public void run(IProgressMonitor monitor) throws InvocationTargetException {
		monitor = Policy.monitorFor(monitor);

		Set<File> filesToExport = new HashSet<File>();
		selectFiles(filesToExport);

		if (filesToExport.size() > 0 && Platform.isRunning()) {
			try {
				monitor.beginTask(EXPORT_JOB_LABEL, filesToExport.size() + 1);

				Job.getJobManager().beginRule(ITasksCoreConstants.ROOT_SCHEDULING_RULE,
						new SubProgressMonitor(monitor, 1));

				ZipFileUtil.createZipFile(getDestinationFile(), new ArrayList<File>(filesToExport),
						TasksUiPlugin.getDefault().getDataDirectory(), monitor);
			} catch (IOException e) {
				throw new InvocationTargetException(e);
			} finally {
				Job.getJobManager().endRule(ITasksCoreConstants.ROOT_SCHEDULING_RULE);
				monitor.done();
			}
		}
	}

	public File getDestinationFile() {
		return new File(destinationDirectory + File.separator + destinationFilename);
	}

	protected void selectFiles(Set<File> filesToExport) {
		Set<Pattern> exclusionPatterns = new HashSet<Pattern>();
		exclusionPatterns.add(excludePattern);
		String dataRoot = TasksUiPlugin.getDefault().getDataDirectory();
		File dataFolder = new File(dataRoot);
		for (File file : dataFolder.listFiles()) {
			boolean exclude = false;
			for (Pattern pattern : exclusionPatterns) {
				if (pattern.matcher(file.getName()).find()) {
					exclude = true;
					break;
				}
			}
			if (!exclude) {
				filesToExport.add(file);
			}
		}

	}

	protected File getSourceFolder() {
		return new File(TasksUiPlugin.getDefault().getDataDirectory());
	}
}

Back to the top