Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0587b13e30d328529b1fea63d586d3c745627b46 (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
/*******************************************************************************
 * 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.util;

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.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.context.core.ContextCorePlugin;
import org.eclipse.mylar.context.core.MylarStatusHandler;
import org.eclipse.mylar.internal.context.core.MylarContextManager;
import org.eclipse.mylar.internal.tasks.ui.ITasksUiConstants;
import org.eclipse.mylar.tasks.core.AbstractTaskContainer;
import org.eclipse.mylar.tasks.core.ITask;
import org.eclipse.mylar.tasks.core.ITaskListChangeListener;
import org.eclipse.mylar.tasks.ui.TaskListManager;
import org.eclipse.mylar.tasks.ui.TasksUiPlugin;
import org.eclipse.ui.PlatformUI;

/**
 * @author Mik Kersten
 * @author Eugene Kuleshov
 */
public class TaskListSaveManager implements ITaskListChangeListener, IBackgroundSaveListener {

	private final static int DEFAULT_SAVE_INTERVAL = 5 * 60 * 1000;

	private BackgroundSaveTimer saveTimer;

	private TaskListSaverJob taskListSaverJob;

	private boolean initializationWarningDialogShow = false;

	/**
	 * Fort testing.
	 */
	private boolean forceBackgroundSave = false;

	public TaskListSaveManager() {
		saveTimer = new BackgroundSaveTimer(this);
		saveTimer.setSaveIntervalMillis(DEFAULT_SAVE_INTERVAL);
		saveTimer.start();
		
		taskListSaverJob = new TaskListSaverJob();
		taskListSaverJob.schedule();
	}

	/**
	 * Called periodically by the save timer
	 */
	public void saveRequested() {
		if (TasksUiPlugin.getDefault() != null && TasksUiPlugin.getDefault().isShellActive() || forceBackgroundSave) {
			try {
				saveTaskList(true, true);
			} catch (Exception e) {
				MylarStatusHandler.fail(e, "Could not auto save task list", false);
			}
		}
	}

	public void saveTaskList(boolean saveContext) {
		saveTaskList(saveContext, false);
	}
	
	public void saveTaskList(boolean saveContext, boolean async) {
		if (TasksUiPlugin.getDefault() != null && TasksUiPlugin.getDefault().isInitialized()) {
			TaskListManager taskListManager = TasksUiPlugin.getTaskListManager();
			if(async) {
				if (saveContext) {
					for (ITask task : taskListManager.getTaskList().getActiveTasks()) {
						taskListSaverJob.addTaskContext(task);
					}
				}
				taskListSaverJob.requestSave();
			} else {
				taskListSaverJob.waitSaveCompleted();
				MylarContextManager contextManager = ContextCorePlugin.getContextManager();
				if (saveContext) {
					for (ITask task : new ArrayList<ITask>(taskListManager.getTaskList()
							.getActiveTasks())) {
						contextManager.saveContext(task.getHandleIdentifier());
					}
				}
				internalSaveTaskList();
			}
		} else if (PlatformUI.getWorkbench() != null && !PlatformUI.getWorkbench().isClosing()) {
			MylarStatusHandler.log("Possible task list initialization failure, not saving list.", this);
			if (!initializationWarningDialogShow) {
				initializationWarningDialogShow = true;
				PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
					public void run() {
						if (PlatformUI.getWorkbench() != null && PlatformUI.getWorkbench().getDisplay() != null) {
							MessageDialog
									.openInformation(
											PlatformUI.getWorkbench().getDisplay().getActiveShell(),
											ITasksUiConstants.TITLE_DIALOG,
											"If task list is blank, Mylar Task List may have failed to initialize.\n\n"
													+ "First, try restarting to see if that corrects the problem.\n\n"
													+ "Then, check the Error Log view for messages, and the FAQ for solutions.\n\n"
													+ ITasksUiConstants.URL_HOMEPAGE);
						}
					}
				});
			}
		}
	}
	
	private void internalSaveTaskList() {
		TaskListManager taskListManager = TasksUiPlugin.getTaskListManager();
		taskListManager.getTaskListWriter().writeTaskList(taskListManager.getTaskList(),
				taskListManager.getTaskListFile());
	}

	/**
	 * Copies all files in the current data directory to the specified folder.
	 * Will overwrite.
	 */
	public void copyDataDirContentsTo(String targetFolderPath) {
		saveTaskList(true, false);
		
		File mainDataDir = new File(TasksUiPlugin.getDefault().getDataDirectory());

		for (File currFile : mainDataDir.listFiles()) {
			if (currFile.isFile()) {
				File destFile = new File(targetFolderPath + File.separator + currFile.getName());
				copy(currFile, destFile);
			} else if (currFile.isDirectory()) {
				File destDir = new File(targetFolderPath + File.separator + currFile.getName());
				if (!destDir.exists()) {
					if (!destDir.mkdir()) {
						MylarStatusHandler.log("Unable to create destination context folder: "
								+ destDir.getAbsolutePath(), this);
						continue;
					}
				}
				for (File file : currFile.listFiles()) {
					File destFile = new File(destDir, file.getName());
					if (destFile.exists()) {
						destFile.delete();
					}
					copy(file, destFile);
				}
			}
		}
	}

	// public void createTaskListBackupFile() {
	// String path = TasksUiPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + TasksUiPlugin.DEFAULT_TASK_LIST_FILE;
	// File taskListFile = new File(path);
	// String backup = path.substring(0, path.indexOf('.')) +
	// FILE_SUFFIX_BACKUP;
	// copy(taskListFile, new File(backup));
	// }
	//
	// public String getBackupFilePath() {
	// String path = TasksUiPlugin.getDefault().getDataDirectory() +
	// File.separator
	// + TasksUiPlugin.DEFAULT_TASK_LIST_FILE;
	// return path.substring(0, path.indexOf('.')) + FILE_SUFFIX_BACKUP;
	// }
	//
	// public void reverseBackup() {
	// String path = TasksUiPlugin.getDefault().getBackupFolderPath() +
	// File.separator
	// + TasksUiPlugin.DEFAULT_TASK_LIST_FILE;
	// File taskListFile = new File(path);
	// String backup = path.substring(0, path.indexOf('.')) +
	// FILE_SUFFIX_BACKUP;
	// copy(new File(backup), taskListFile);
	// }

	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;
		}
	}

	public void taskActivated(ITask task) {
		// ignore
	}

	public void tasksActivated(List<ITask> tasks) {
		// ignore
	}

	public void taskDeactivated(ITask task) {
		saveTaskList(true, true);
	}

	public void localInfoChanged(ITask task) {
		saveTaskList(false, true);
	}

	public void repositoryInfoChanged(ITask task) {
		// ignore
	}

	public void tasklistRead() {
		// ignore
	}

	/**
	 * For testing.
	 */
	public void setForceBackgroundSave(boolean on) {
		forceBackgroundSave = on;
		saveTimer.setForceSyncExec(on);
	}

	public void taskMoved(ITask task, AbstractTaskContainer fromContainer, AbstractTaskContainer toContainer) {
		saveTaskList(false, true);
	}

	public void taskDeleted(ITask task) {
		saveTaskList(false, true);
	}

	public void containerAdded(AbstractTaskContainer container) {
		saveTaskList(false, true);
	}

	public void containerDeleted(AbstractTaskContainer container) {
		saveTaskList(false, true);
	}

	public void taskAdded(ITask task) {
		saveTaskList(false, true);
	}

	/** For testing only * */
	public BackgroundSaveTimer getSaveTimer() {
		return saveTimer;
	}

	public void containerInfoChanged(AbstractTaskContainer container) {
		saveTaskList(false, true);
	}

	public void synchronizationCompleted() {
		// ignore
	}
	
	private class TaskListSaverJob extends Job {

		private final Queue<ITask> taskQueue = new LinkedList<ITask>();

		private volatile boolean saveRequested = false;

		private volatile boolean saveCompleted = true; 
		
		
		TaskListSaverJob() {
			super("Task List Saver");
			setPriority(Job.LONG);
			setSystem(true);
		}

		protected IStatus run(IProgressMonitor monitor) {
			while(true) {
				if(saveRequested) {
					saveRequested = false;
					saveCompleted = false;
					MylarContextManager contextManager = ContextCorePlugin.getContextManager();
					while(!taskQueue.isEmpty()) {
						ITask task = taskQueue.poll();
						if(task!=null) {
							contextManager.saveContext(task.getHandleIdentifier());
						}
					}
					internalSaveTaskList();
				}
				
				if(!saveRequested) {
					synchronized (this) {
						saveCompleted = true;
						notifyAll();
						try {
							wait();
						} catch (InterruptedException ex) {
							// ignore
						}
					}
				}
			}
		}
		
		void addTaskContext(ITask task) {
			taskQueue.add(task);
		}
		
		void requestSave() {
			saveRequested = true;
			synchronized (this) {
				notifyAll();
			}
		}

		void waitSaveCompleted() {
			while(!saveCompleted) {
				synchronized (this) {
					try {
						wait();
					} catch (InterruptedException ex) {
						// ignore
					}
				}
			}
		}
	}
	
}

Back to the top