Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cbfeafcd8883520ff9bed73639b67cd84c758dd (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
/*******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.internal.github.ui;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

/**
 * Stores Avatars
 */
public class AvatarStore implements Serializable, ISchedulingRule {

	/**
	 * Callback interface for avatar image data loaded
	 */
	public interface IAvatarCallback {

		/**
		 * Avatar loaded
		 *
		 * @param data
		 * @param store
		 */
		void loaded(ImageData data, AvatarStore store);

	}

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7791322302733784441L;

	/**
	 * TIMEOUT
	 */
	public static final int TIMEOUT = 30 * 1000;

	/**
	 * BUFFER_SIZE
	 */
	public static final int BUFFER_SIZE = 8192;

	private Map<String, byte[]> avatars = new HashMap<>();

	/**
	 * Get cached avatar image data
	 *
	 * @param url
	 * @return image data or null if not present in store
	 */
	public ImageData getAvatar(String url) {
		ImageData data = null;
		url = normalize(url);
		if (url != null) {
			byte[] cached = this.avatars.get(url);
			if (cached != null)
				data = getData(cached);
		}
		return data;
	}

	/**
	 * Normalize url removing query parameters
	 *
	 * @param url
	 * @return normalized
	 */
	protected String normalize(String url) {
		try {
			URL parsed = new URL(url);
			parsed = new URL(parsed.getProtocol(), parsed.getHost(),
					parsed.getPath());
			return parsed.toExternalForm();
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * Load avatar
	 *
	 * @param url
	 * @param callback
	 */
	public void loadAvatar(final String url, final IAvatarCallback callback) {
		Job job = new Job(MessageFormat.format("Loading avatar for {0}", url)) { //$NON-NLS-1$

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				try {
					ImageData data = loadAvatar(url);
					if (data != null)
						callback.loaded(data, AvatarStore.this);
				} catch (IOException ignore) {
					// Ignored
				}
				return Status.OK_STATUS;
			}
		};
		job.schedule();
	}

	/**
	 * Load avatar image data from url
	 *
	 * @param url
	 * @return avatar image data
	 * @throws IOException
	 */
	public ImageData loadAvatar(String url) throws IOException {
		url = normalize(url);
		URL parsed = new URL(url);

		byte[] data = this.avatars.get(url);
		if (data != null)
			return getData(data);

		URLConnection connection = parsed.openConnection();
		connection.setConnectTimeout(TIMEOUT);

		ByteArrayOutputStream output = new ByteArrayOutputStream();
		InputStream input = connection.getInputStream();
		try {
			byte[] buffer = new byte[BUFFER_SIZE];
			int read = input.read(buffer);
			while (read != -1) {
				output.write(buffer, 0, read);
				read = input.read(buffer);
			}
		} finally {
			try {
				input.close();
			} catch (IOException ignore) {
				// Ignored
			}
			try {
				output.close();
			} catch (IOException ignore) {
				// Ignored
			}
		}
		data = output.toByteArray();
		this.avatars.put(url, data);
		return getData(data);
	}

	/**
	 * Get scaled image
	 *
	 * @param size
	 * @param data
	 * @return image data scaled to specified size
	 */
	public Image getScaledImage(int size, ImageData data) {
		Display display = PlatformUI.getWorkbench().getDisplay();
		Image image = new Image(display, data);
		Rectangle sourceBounds = image.getBounds();

		// Return original image and don't scale if size matches request
		if (sourceBounds.width == size)
			return image;

		Image scaled = new Image(display, size, size);
		GC gc = new GC(scaled);
		try {
			gc.setAntialias(SWT.ON);
			gc.setInterpolation(SWT.HIGH);
			Rectangle targetBounds = scaled.getBounds();
			gc.drawImage(image, 0, 0, sourceBounds.width, sourceBounds.height,
					0, 0, targetBounds.width, targetBounds.height);
		} finally {
			gc.dispose();
			image.dispose();
		}
		return scaled;
	}

	/**
	 * Get avatar image data
	 *
	 * @param bytes
	 * @return image data
	 */
	public ImageData getData(byte[] bytes) {
		ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
		try {
			ImageData[] images = new ImageLoader().load(stream);
			if (images.length > 0)
				return images[0];
		} finally {
			try {
				stream.close();
			} catch (IOException ignore) {
				// Ignored
			}
		}
		return null;
	}

	/**
	 * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
	 */
	@Override
	public boolean contains(ISchedulingRule rule) {
		return this == rule;
	}

	/**
	 * @see org.eclipse.core.runtime.jobs.ISchedulingRule#isConflicting(org.eclipse.core.runtime.jobs.ISchedulingRule)
	 */
	@Override
	public boolean isConflicting(ISchedulingRule rule) {
		return this == rule;
	}
}

Back to the top