Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f0c0e4875f33a6e1cfc4bb0aeda8998c4420ef8 (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
/*******************************************************************************
 * 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.tasks.ui;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.mylar.internal.tasks.ui.util.TaskDataExportJob;
import org.eclipse.mylar.internal.tasks.ui.wizards.TaskDataExportWizard;
import org.eclipse.mylar.tasks.ui.TasksUiPlugin;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;

/**
 * @author Rob Elves
 */
public class TaskListBackupManager implements IPropertyChangeListener {

	private static final String TITLE_TASKLIST_BACKUP = "Tasklist Backup";

	private static final String BACKUP_JOB_NAME = "Scheduled task data backup";

	public static final String BACKUP_FAILURE_MESSAGE = "Could not backup task data. Check backup preferences.\n";

	private static final long SECOND = 1000;

	private static final long MINUTE = 60 * SECOND;

	private static final long HOUR = 60 * MINUTE;

	private static final long DAY = 24 * HOUR;

	private Timer timer;

	public TaskListBackupManager() {
//		boolean enabled = MylarTaskListPlugin.getMylarCorePrefs().getBoolean(
//				TaskListPreferenceConstants.BACKUP_AUTOMATICALLY);
//		if (enabled) {
		int days = TasksUiPlugin.getDefault().getPreferenceStore().getInt(TaskListPreferenceConstants.BACKUP_SCHEDULE);
		if (days > 0) {
			start(MINUTE);
		}
	}

	public void start(long delay) {
		timer = new Timer();
		timer.schedule(new CheckBackupRequired(), delay, HOUR);
	}

	public void stop() {
		timer.cancel();
	}

	public void propertyChange(PropertyChangeEvent event) {
//		if (event.getProperty().equals(TaskListPreferenceConstants.BACKUP_AUTOMATICALLY)) {
//			if ((Boolean) event.getNewValue() == true) {
//				start(MINUTE);
//			} else {
//				stop();
//			}
//		}
	}

	public void backupNow(boolean synchronous) {
//		String destination = MylarTaskListPlugin.getMylarCorePrefs().getString(
//				TaskListPreferenceConstants.BACKUP_FOLDER);
		String destination = TasksUiPlugin.getDefault().getBackupFolderPath();
		
		File backupFolder = new File(destination);
		if (!backupFolder.exists()) {
			backupFolder.mkdir();
		}

		removeOldBackups(backupFolder);

		String fileName = TaskDataExportWizard.getZipFileName();

		if (!synchronous) {

			ExportJob export = new ExportJob(destination, fileName);
			export.schedule();

		} else {

			final TaskDataExportJob backupJob = new TaskDataExportJob(destination, true, fileName);

			IProgressService service = PlatformUI.getWorkbench().getProgressService();
			try {
				service.run(true, false, backupJob);
				TasksUiPlugin.getDefault().getPreferenceStore().setValue(TaskListPreferenceConstants.BACKUP_LAST,
						new Date().getTime());
			} catch (InterruptedException e) {
				// ignore
			} catch (InvocationTargetException e) {
				MessageDialog.openError(null, TITLE_TASKLIST_BACKUP, BACKUP_FAILURE_MESSAGE);
			}

		}
	}

	/** public for testing purposes */
	public void removeOldBackups(File folder) {

		int maxBackups = TasksUiPlugin.getDefault().getPreferenceStore().getInt(TaskListPreferenceConstants.BACKUP_MAXFILES);

		File[] files = folder.listFiles();
		ArrayList<File> backupFiles = new ArrayList<File>();
		for (File file : files) {
			if (file.getName().startsWith(TaskDataExportWizard.ZIP_FILE_PREFIX)) {
				backupFiles.add(file);
			}
		}

		File[] backupFileArray = backupFiles.toArray(new File[backupFiles.size()]);

		if (backupFileArray != null && backupFileArray.length > 0) {
			Arrays.sort(backupFileArray, new Comparator<File>() {
				public int compare(File file1, File file2) {
					return new Long((file1).lastModified()).compareTo(new Long((file2).lastModified()));
				}

			});

			int toomany = backupFileArray.length - maxBackups;
			if (toomany > 0) {
				for (int x = 0; x < toomany; x++) {
					if(backupFileArray[x] != null) {
						backupFileArray[x].delete();
					}
				}
			}
		}
	}
	
//	public File getMostRecentBackup() {
//		String destination = TasksUiPlugin.getDefault().getBackupFolderPath();
//
//		File backupFolder = new File(destination);
//		ArrayList<File> backupFiles = new ArrayList<File>();
//		if (backupFolder.exists()) {
//			File[] files = backupFolder.listFiles();
//			for (File file : files) {
//				if (file.getName().startsWith(TaskDataExportWizard.ZIP_FILE_PREFIX)) {
//					backupFiles.add(file);
//				}
//			}
//		}
//
//		File[] backupFileArray = backupFiles.toArray(new File[backupFiles.size()]);
//
//		if (backupFileArray != null && backupFileArray.length > 0) {
//			Arrays.sort(backupFileArray, new Comparator<File>() {
//				public int compare(File file1, File file2) {
//					return (new Long((file1).lastModified()).compareTo(new Long((file2).lastModified()))) * -1;
//				}
//
//			});
//		}
//		if (backupFileArray != null && backupFileArray.length > 0) {
//			return backupFileArray[0];
//		}
//		
//		return null;
//	}

	class CheckBackupRequired extends TimerTask {

		@Override
		public void run() {
			if (!Platform.isRunning() || TasksUiPlugin.getDefault() == null) {
				return;
			} else {
				long lastBackup = TasksUiPlugin.getDefault().getPreferenceStore().getLong(
						TaskListPreferenceConstants.BACKUP_LAST);
				int days = TasksUiPlugin.getDefault().getPreferenceStore().getInt(TaskListPreferenceConstants.BACKUP_SCHEDULE);
				long waitPeriod = days * DAY;
				final long now = new Date().getTime();

				if ((now - lastBackup) > waitPeriod) {
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						public void run() {
							backupNow(false);
						}
					});
				}
			}
		}
	}

	static class ExportJob extends Job {

		final TaskDataExportJob backupJob;

		public ExportJob(String destination, String filename) {
			super(BACKUP_JOB_NAME);
			backupJob = new TaskDataExportJob(destination, true, filename);
		}

		@Override
		protected IStatus run(IProgressMonitor monitor) {
			try {
				if (Platform.isRunning()) {
					backupJob.run(monitor);
					TasksUiPlugin.getDefault().getPreferenceStore().setValue(TaskListPreferenceConstants.BACKUP_LAST,
							new Date().getTime());
				}
			} catch (InvocationTargetException e) {
				MessageDialog
						.openError(null, BACKUP_JOB_NAME,
								"Error occured during scheduled tasklist backup.\nCheck settings on Tasklist preferences page.");
			} catch (InterruptedException e) {
				return Status.CANCEL_STATUS;
			}
			return Status.OK_STATUS;
		}

	}
}

Back to the top