Skip to main content
summaryrefslogtreecommitdiffstats
blob: a65a3656d50451057d33b06f29777982a87a002a (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
package org.eclipse.ecf.internal.provider.xmpp.ui;

import java.io.File;

import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.filetransfer.IFileTransferListener;
import org.eclipse.ecf.filetransfer.IOutgoingFileTransferContainerAdapter;
import org.eclipse.ecf.filetransfer.OutgoingFileTransferException;
import org.eclipse.ecf.filetransfer.events.IFileTransferEvent;
import org.eclipse.ecf.filetransfer.events.IOutgoingFileTransferResponseEvent;
import org.eclipse.ecf.internal.provider.xmpp.XMPPContainer;
import org.eclipse.ecf.internal.ui.Messages;
import org.eclipse.ecf.presence.IPresence;
import org.eclipse.ecf.presence.roster.IRosterEntry;
import org.eclipse.ecf.presence.ui.roster.AbstractRosterEntryContributionItem;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

public class XMPPCompoundContributionItem extends
		AbstractRosterEntryContributionItem {

	private static final IContributionItem[] EMPTY_ARRAY = new IContributionItem[0];

	private Shell shell;

	protected IContributionItem[] getContributionItems() {
		Object selection = getSelection();
		if (!(selection instanceof IRosterEntry)) {
			return EMPTY_ARRAY;
		}
		final IRosterEntry entry = (IRosterEntry) selection;
		IContainer container = getContainerForRosterEntry(entry);
		if (container instanceof XMPPContainer) {
			shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
					.getShell();
			IContributionItem[] contributions = new IContributionItem[2];
			final IOutgoingFileTransferContainerAdapter ioftca = (IOutgoingFileTransferContainerAdapter) container
					.getAdapter(IOutgoingFileTransferContainerAdapter.class);
			IPresence presence = entry.getPresence();
			boolean type = (presence == null) ? false : presence.getType()
					.equals(IPresence.Type.AVAILABLE);
			boolean mode = (presence == null) ? false : presence.getMode()
					.equals(IPresence.Mode.AVAILABLE);
			if (!(ioftca != null && type && mode)) {
				return EMPTY_ARRAY;
			}
			IAction fileSendAction = new Action() {
				public void run() {
					sendFileToTarget(ioftca, entry.getUser().getID());
				}
			};
			fileSendAction.setText("Send File");
			fileSendAction.setImageDescriptor(PlatformUI.getWorkbench()
					.getSharedImages().getImageDescriptor(
							ISharedImages.IMG_OBJ_FILE));
			contributions[0] = new ActionContributionItem(fileSendAction);
			contributions[1] = new Separator();
			return contributions;
		} else {
			return EMPTY_ARRAY;
		}
	}

	private void sendFileToTarget(
			IOutgoingFileTransferContainerAdapter fileTransfer,
			final ID targetID) {
		FileDialog fd = new FileDialog(shell, SWT.OPEN);
		// XXX this should be some default path set by preferences
		fd.setFilterPath(System.getProperty("user.home"));
		fd.setText(NLS.bind(Messages.RosterView_SendFile_title, targetID
				.getName()));
		final String res = fd.open();
		if (res != null) {
			File aFile = new File(res);
			try {
				fileTransfer.sendOutgoingRequest(targetID, aFile,
						new IFileTransferListener() {
							public void handleTransferEvent(
									final IFileTransferEvent event) {
								Display.getDefault().asyncExec(new Runnable() {
									public void run() {
										// XXX This should be handled more
										// gracefully/with better UI (progress
										// bar?)
										if (event instanceof IOutgoingFileTransferResponseEvent) {
											if (!((IOutgoingFileTransferResponseEvent) event)
													.requestAccepted())
												MessageDialog
														.openInformation(
																shell,
																Messages.RosterView_SendFile_response_title,
																NLS
																		.bind(
																				Messages.RosterView_SendFile_response_message,
																				res,
																				targetID
																						.getName()));
										}
									}
								});
							}
						}, null);
			} catch (OutgoingFileTransferException e) {
				MessageDialog
						.openError(
								shell,
								Messages.RosterView_SendFile_requestexception_title,
								NLS
										.bind(
												Messages.RosterView_SendFile_requestexception_message,
												new Object[] { res,
														e.getLocalizedMessage() }));
			}
		}
	}

}

Back to the top