Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d35c515168190f9090952358160751b806d3961b (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 *
 * 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
 *******************************************************************************/
package org.eclipse.cdt.internal.qt.core;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.cdt.core.build.IToolChain;
import org.eclipse.cdt.qt.core.IQtInstall;
import org.eclipse.cdt.qt.core.IQtInstallListener;
import org.eclipse.cdt.qt.core.IQtInstallManager;
import org.eclipse.cdt.qt.core.IQtInstallProvider;
import org.eclipse.cdt.qt.core.QtInstallEvent;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

public class QtInstallManager implements IQtInstallManager {

	private Map<Path, IQtInstall> installs;
	private Map<String, IConfigurationElement> toolChainMap;
	private List<IQtInstallListener> listeners = new LinkedList<>();

	private Preferences getPreferences() {
		return InstanceScope.INSTANCE.getNode(Activator.ID).node("qtInstalls"); //$NON-NLS-1$
	}

	private void initInstalls() {
		if (installs == null) {
			installs = new HashMap<>();
			try {
				Preferences prefs = getPreferences();
				for (String key : prefs.keys()) {
					QtInstall install = new QtInstall(Paths.get(prefs.get(key, "/"))); //$NON-NLS-1$
					installs.put(install.getQmakePath(), install);
				}
			} catch (BackingStoreException e) {
				Activator.log(e);
			}

			// Auto installs
			IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(Activator.ID,
					"qtInstallProvider"); //$NON-NLS-1$
			for (IConfigurationElement element : point.getConfigurationElements()) {
				try {
					IQtInstallProvider provider = (IQtInstallProvider) element.createExecutableExtension("class"); //$NON-NLS-1$
					for (IQtInstall install : provider.getInstalls()) {
						installs.put(install.getQmakePath(), install);
					}
				} catch (CoreException e) {
					Activator.log(e);
				}
			}
		}
	}

	private void saveInstalls() {
		try {
			Preferences prefs = getPreferences();

			// Remove ones that aren't valid
			for (String key : prefs.keys()) {
				if (installs.get(Paths.get(key)) == null) {
					prefs.remove(key);
				}
			}

			// Add new ones
			for (Path path : installs.keySet()) {
				String key = path.toString();
				if (prefs.get(key, null) == null) {
					prefs.put(key, installs.get(path).getQmakePath().toString());
				}
			}

			prefs.flush();
		} catch (BackingStoreException e) {
			Activator.log(e);
		}
	}

	@Override
	public Collection<IQtInstall> getInstalls() {
		initInstalls();
		return Collections.unmodifiableCollection(installs.values());
	}

	@Override
	public void addInstall(IQtInstall qt) {
		initInstalls();
		installs.put(qt.getQmakePath(), qt);
		saveInstalls();
	}

	@Override
	public IQtInstall getInstall(Path qmakePath) {
		initInstalls();
		return installs.get(qmakePath);
	}

	@Override
	public Collection<IQtInstall> getInstall(String spec) {
		initInstalls();
		List<IQtInstall> installList = new ArrayList<>();
		for (IQtInstall install : installs.values()) {
			if (install.getSpec().equals(spec)) {
				installList.add(install);
			}
		}
		return installList;
	}

	@Override
	public void removeInstall(IQtInstall install) {
		fireEvent(new QtInstallEvent(QtInstallEvent.REMOVED, install));
		installs.remove(install.getQmakePath());
		saveInstalls();
	}

	@Override
	public boolean supports(IQtInstall install, IToolChain toolChain) {
		if (toolChainMap == null) {
			toolChainMap = new HashMap<>();
			IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(Activator.ID,
					"qtToolChainMapper"); //$NON-NLS-1$
			for (IConfigurationElement element : point.getConfigurationElements()) {
				if (element.getName().equals("mapping")) { //$NON-NLS-1$
					String spec = element.getAttribute("spec"); //$NON-NLS-1$
					toolChainMap.put(spec, element);
				}
			}
		}

		IConfigurationElement element = toolChainMap.get(install.getSpec());
		if (element != null) {
			for (IConfigurationElement property : element.getChildren("property")) { //$NON-NLS-1$
				String key = property.getAttribute("key"); //$NON-NLS-1$
				String value = property.getAttribute("value"); //$NON-NLS-1$
				if (!value.equals(toolChain.getProperty(key))) {
					return false;
				}
			}

			for (Entry<String, String> property : install.getProperties().entrySet()) {
				if (!property.getValue().equals(toolChain.getProperty(property.getKey()))) {
					return false;
				}
			}

			return true;
		} else {
			// Don't know so returning false
			return false;
		}
	}

	@Override
	public void addListener(IQtInstallListener listener) {
		listeners.add(listener);
	}

	@Override
	public void removeListener(IQtInstallListener listener) {
		listeners.remove(listener);
	}

	private void fireEvent(QtInstallEvent event) {
		for (IQtInstallListener listener : listeners) {
			listener.installChanged(event);
		}
	}

}

Back to the top