Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49f61844c6f00973b145803d66fb1e37c56f971a (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
/*******************************************************************************
 * Copyright (c) 2011 Red Hat 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:
 *     David Green <david.green@tasktop.com> - initial contribution
 *     Christian Trutz <christian.trutz@gmail.com> - initial contribution
 *     Chris Aniszczyk <caniszczyk@gmail.com> - initial contribution
 *******************************************************************************/
package org.eclipse.mylyn.internal.github.ui;

import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.github.ui.pr.PullRequestContextSynchronizer;
import org.eclipse.mylyn.tasks.core.ITaskActivityManager;
import org.eclipse.mylyn.tasks.ui.TasksUi;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * GitHub UI plug-in
 */
public class GitHubUi extends AbstractUIPlugin {

	/**
	 * BUNDLE_ID
	 */
	public static final String BUNDLE_ID = "org.eclipse.mylyn.github.ui";

	/**
	 * STORE_NAME
	 */
	public static final String STORE_NAME = "avatars.ser"; //$NON-NLS-1$

	private PullRequestContextSynchronizer prSynchronize = new PullRequestContextSynchronizer();

	/**
	 * Create status
	 *
	 * @param severity
	 * @param message
	 * @return status
	 */
	public static IStatus createStatus(int severity, String message) {
		return new Status(severity, BUNDLE_ID, message);
	}

	/**
	 * Create status
	 *
	 * @param severity
	 * @param message
	 * @param e
	 * @return status
	 */
	public static IStatus createStatus(int severity, String message, Throwable e) {
		return new Status(severity, BUNDLE_ID, message, e);
	}

	/**
	 * Create error status from message
	 *
	 * @param message
	 * @return status
	 */
	public static IStatus createErrorStatus(String message) {
		return createStatus(IStatus.ERROR, message);
	}

	/**
	 * Create error status from message and throwable
	 *
	 * @param message
	 * @param t
	 * @return status
	 */
	public static IStatus createErrorStatus(String message, Throwable t) {
		return createStatus(IStatus.ERROR, message, t);
	}

	/**
	 * Create error status from throwable
	 *
	 * @param e
	 * @return status
	 */
	public static IStatus createErrorStatus(Throwable e) {
		return createStatus(IStatus.ERROR,
				"Unexpected error: " + e.getMessage(), e);
	}

	/**
	 * Log message and throwable as error
	 *
	 * @param message
	 * @param t
	 */
	public static void logError(String message, Throwable t) {
		INSTANCE.getLog().log(createErrorStatus(message, t));
	}

	/**
	 * Log throwable as error
	 *
	 * @param t
	 */
	public static void logError(Throwable t) {
		INSTANCE.getLog().log(createErrorStatus(t.getMessage(), t));
	}

	private static GitHubUi INSTANCE;

	/**
	 * Get default activator
	 *
	 * @return plug-in
	 */
	public static GitHubUi getDefault() {
		return INSTANCE;
	}

	private AvatarStore store = null;

	/**
	 * Create plug-in
	 */
	public GitHubUi() {

	}

	/**
	 * Get avatar store
	 *
	 * @return avatar store
	 */
	public AvatarStore getStore() {
		return store;
	}

	/**
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		INSTANCE = this;
		loadAvatars(context);
		ITaskActivityManager activityManager = TasksUi.getTaskActivityManager();
		if (activityManager != null)
			activityManager.addActivationListener(prSynchronize);
	}

	/**
	 * Load avatars
	 *
	 * @param context
	 */
	protected void loadAvatars(BundleContext context) {
		IPath location = Platform.getStateLocation(context.getBundle());
		File file = location.append(STORE_NAME).toFile();
		if (file.exists()) {
			ObjectInputStream stream = null;
			try {
				stream = new ObjectInputStream(Files.newInputStream(file.toPath()));
				store = (AvatarStore) stream.readObject();
			} catch (IOException e) {
				logError("Error reading avatar store", e); //$NON-NLS-1$
			} catch (ClassNotFoundException cnfe) {
				logError("Error reading avatar store", cnfe); //$NON-NLS-1$
			} finally {
				if (stream != null)
					try {
						stream.close();
					} catch (IOException ignore) {
						// Ignored
					}
			}
		}
		if (store == null)
			store = new AvatarStore();
	}

	/**
	 * Save avatars
	 *
	 * @param context
	 */
	protected void saveAvatars(BundleContext context) {
		// TODO need to avoid Platform. use the context
		IPath location = Platform.getStateLocation(context.getBundle());
		File file = location.append(STORE_NAME).toFile();

		try (ObjectOutputStream stream = new ObjectOutputStream(Files.newOutputStream(file.toPath()))) {
			stream.writeObject(this.store);
		} catch (IOException e) {
			logError("Error writing avatar store", e); //$NON-NLS-1$
		}
	}

	/**
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		INSTANCE = null;
		saveAvatars(context);
		ITaskActivityManager activityManager = TasksUi.getTaskActivityManager();
		if (activityManager != null)
			activityManager.removeActivationListener(prSynchronize);
	}
}

Back to the top