Skip to main content
summaryrefslogtreecommitdiffstats
blob: a498bf56f7a8253fea3c689b90895c9682126b7b (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
/*******************************************************************************
 * Copyright (c) 2010, 2013 Peter Stibrany 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:
 *     Peter Stibrany - initial API and implementation
 *******************************************************************************/

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

import java.text.MessageFormat;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialogWithToggle;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.mylyn.commons.workbench.WorkbenchUtil;
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
import org.eclipse.mylyn.tasks.core.ITaskAttachment;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IWorkbenchPage;

/**
 * @author Peter Stibrany
 */
public class TaskAttachmentEditorViewer implements ITaskAttachmentViewer {

	private static final String PREF_DO_NOT_WARN_BEFORE_OPENING_ATTACHMENTS = "do.not.warn.before.opening.attachments"; //$NON-NLS-1$

	private final IEditorDescriptor descriptor;

	private final boolean isWorkbenchDefault;

	private final boolean isSystem;

	TaskAttachmentEditorViewer(IEditorDescriptor descriptor) {
		this(descriptor, false);
	}

	TaskAttachmentEditorViewer(IEditorDescriptor descriptor, boolean isWorkbenchDefault) {
		this(descriptor, isWorkbenchDefault, false);
	}

	TaskAttachmentEditorViewer(IEditorDescriptor descriptor, boolean isWorkbenchDefault, boolean isSystem) {
		this.descriptor = descriptor;
		this.isWorkbenchDefault = isWorkbenchDefault;
		this.isSystem = isSystem;
	}

	public String getId() {
		return descriptor.getId();
	}

	public String getLabel() {
		return descriptor.getLabel();
	}

	public void openAttachment(final IWorkbenchPage page, final ITaskAttachment attachment) throws CoreException {
		if (promptToConfirmOpen(attachment)) {
			DownloadAndOpenTaskAttachmentJob job = new DownloadAndOpenTaskAttachmentJob(MessageFormat.format(
					Messages.TaskAttachmentEditorViewer_openingAttachment,
					AttachmentUtil.getAttachmentFilename(attachment)), attachment, page, descriptor.getId());
			WorkbenchUtil.busyCursorWhile(job);
		}
	}

	private boolean promptToConfirmOpen(final ITaskAttachment attachment) {
		if (isSystem()) {
			IPreferenceStore store = TasksUiPlugin.getDefault().getPreferenceStore();
			if (!store.getBoolean(PREF_DO_NOT_WARN_BEFORE_OPENING_ATTACHMENTS)) {
				MessageDialogWithToggle dialog = MessageDialogWithToggle.openYesNoQuestion(
						WorkbenchUtil.getShell(),
						Messages.TaskAttachmentEditorViewer_Open_Attachment,
						NLS.bind(Messages.TaskAttachmentEditorViewer_Some_files_can_harm_your_computer,
								attachment.getFileName()), Messages.TaskAttachmentEditorViewer_Do_not_warn_me_again,
						false, null, null);
				if (dialog.getReturnCode() == IDialogConstants.YES_ID) {
					if (dialog.getToggleState()) {
						store.setValue(PREF_DO_NOT_WARN_BEFORE_OPENING_ATTACHMENTS, true);
						TasksUiPlugin.getDefault().savePluginPreferences();
					}
				} else {
					return false;
				}
			}
		}
		return true;
	}

	public boolean isWorkbenchDefault() {
		return isWorkbenchDefault;
	}

	protected boolean isSystem() {
		return isSystem;
	}
}

Back to the top