Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 331e662235e69466b7c303858dabb2ad3beb415d (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 * Copyright (c) 2013 QNX Software Systems 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
 */
package org.eclipse.cdt.internal.qt.core.index;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICDescriptionDelta;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionListener;
import org.eclipse.cdt.qt.core.QtNature;
import org.eclipse.cdt.qt.core.index.IQMakeEnv;
import org.eclipse.cdt.qt.core.index.IQMakeEnvProvider;
import org.eclipse.cdt.qt.core.index.IQMakeProjectInfo;
import org.eclipse.cdt.qt.core.index.IQMakeProjectInfoListener;
import org.eclipse.cdt.qt.core.index.QMakeEnvInfo;
import org.eclipse.cdt.qt.core.index.IQMakeInfo;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

/**
 * Represents a QMake project information that is based on an activate project configuration of a specified related IProject.
 * Allows to resolve actual information and listen its change.
 * Manages life-cycle of all QMakeProjectInfo instances.
 */
public final class QMakeProjectInfo implements IQMakeProjectInfo, ICProjectDescriptionListener {

	private static final RCListener LISTENER = new RCListener();
	// sync object for CACHE field
	private static final Object SYNC = new Object();
	// a map of all QMakeProjectInfo instances
	private static final Map<IProject,QMakeProjectInfo> CACHE = new HashMap<IProject,QMakeProjectInfo>();

	// called by QtPlugin activator to setup this class
	public static final void start() {
		ResourcesPlugin.getWorkspace().addResourceChangeListener(LISTENER, IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.PRE_CLOSE | IResourceChangeEvent.PRE_DELETE);
	}

	// called by QtPlugin activator to clean up this class
	public static final void stop() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(LISTENER);
		synchronized (SYNC) {
			while (true) {
				Iterator<IProject> iterator = CACHE.keySet().iterator();
				if (!iterator.hasNext()) {
					break;
				}
				removeProjectFromCache(iterator.next());
			}
		}
	}

	/**
	 * Returns a QMakeProjectInfo for an active project configuration of a specified project.
	 *
	 * @param project the project
	 * @return the QMakeProjectInfo; or null if the project does not have QtNature
	 */
	public static QMakeProjectInfo getQMakeProjectInfoFor(IProject project) {
		synchronized (SYNC) {
			QMakeProjectInfo info = CACHE.get(project);
			if (info == null) {
				if (QtNature.hasNature(project)) {
					info = new QMakeProjectInfo(project);
					CACHE.put(project,info);
				}
			}
			return info;
		}
	}

	// removes the project from the CACHE
	private static void removeProjectFromCache(IResource project) {
		QMakeProjectInfo info;
		synchronized (SYNC) {
			info = CACHE.remove(project);
		}
		if (info != null) {
			info.destroy();
		}
	}

	private final ICProjectDescription projectDescription;

	// sync object for all mutable fields
	private final Object sync = new Object();
	// true if this instance still registered in CACHE
	private boolean live = true;
	// a set of sensitive files that might affects actual QMake information
	private final SensitiveSet sensitiveFilePathSet = new SensitiveSet();
	// an active project configuration
	private ControllerImpl activeController;
	// the last calculated QMake info; null if not calculated
	private IQMakeInfo qmakeInfo = null;
	// listeners
	private final List<IQMakeProjectInfoListener> listeners = new CopyOnWriteArrayList<IQMakeProjectInfoListener>();

	private QMakeProjectInfo(IProject project) {
		projectDescription = CoreModel.getDefault().getProjectDescriptionManager().getProjectDescription(project);
		CoreModel.getDefault().addCProjectDescriptionListener(this, ICDescriptionDelta.ACTIVE_CFG);
		updateActiveConfiguration();
	}

	// called from removeProjectFromCache only
	private void destroy() {
		synchronized (sync) {
			if (! live) {
				return;
			}
			live = false;
			CoreModel.getDefault().removeCProjectDescriptionListener(this);
			removeActiveConfiguration();
			qmakeInfo = QMakeInfo.INVALID;
		}
	}

	// removes active configuration
	private void removeActiveConfiguration() {
		ControllerImpl previous = activeController;
		activeController = null;
		if (previous != null) {
			previous.destroy();
		}
	}

	// updates active configuration
	private void updateActiveConfiguration() {
		ICConfigurationDescription configuration = projectDescription.getActiveConfiguration();
		if (configuration != null) {
			activeController = new ControllerImpl(configuration);
		}
		scheduleFetchQMakeInfo();
	}

	// called on active project configuration change
	@Override
	public void handleEvent(CProjectDescriptionEvent event) {
		synchronized (sync) {
			removeActiveConfiguration();
			updateActiveConfiguration();
		}
	}

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

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

	// calculates (if does not exist) and returns actual QMake info
	@Override
	public IQMakeInfo getActualInfo() {
		synchronized (sync) {
			if (! live) {
				return QMakeInfo.INVALID;
			}
			if (qmakeInfo == null) {
				fetchQMakeInfo();
			}
			return qmakeInfo;
		}
	}

	// calculates actual QMake info
	private void fetchQMakeInfo() {
		// retrieves IQMakeEnvInfo from IQMakeEnv
		IQMakeEnv qmakeEnv = activeController != null ? activeController.getQMakeEnv() : null;
		QMakeEnvInfo qmakeEnvInfo = qmakeEnv != null ? qmakeEnv.getQMakeEnvInfo() : null;

		// retrieves .pro file path
		String proFilePath = toFilePath(qmakeEnvInfo != null ? qmakeEnvInfo.getProFile() : null);
		// retrieves qmake executable path
		String qmakeFilePath = qmakeEnvInfo != null ? qmakeEnvInfo.getQMakeFilePath() : null;
		// retries environment
		List<String> envList = new ArrayList<String>();
		Map<String, String> envMap = qmakeEnvInfo != null ? qmakeEnvInfo.getEnvironment() : Collections.<String,String>emptyMap();
		for (Map.Entry<String,String> entry : envMap.entrySet()) {
			envList.add(entry.getKey() + "=" + entry.getValue());
		}

		// calculates actual QMake info
		qmakeInfo = QMakeInfo.create(proFilePath, qmakeFilePath, envList.toArray(new String[envList.size()]));

		// calculates a new set of sensitive file paths
		sensitiveFilePathSet.clear();
		Set<IFile> envSensFiles = qmakeEnvInfo != null ? qmakeEnvInfo.getSensitiveFiles() : Collections.<IFile>emptySet();
		for (IFile sensitiveFile : envSensFiles) {
			if (sensitiveFile != null) {
				sensitiveFilePathSet.addSensitiveFile(sensitiveFile);
			}
		}
		if (proFilePath != null) {
			sensitiveFilePathSet.addSensitiveFile(proFilePath);
		}
		List<String> sensitiveFiles = qmakeInfo.getInvolvedQMakeFiles();
		if (sensitiveFiles != null) {
			for (String sensitiveFile : sensitiveFiles) {
				sensitiveFilePathSet.addSensitiveFile(sensitiveFile);
			}
		}
	}

	// converts IFile to absolute path
	private static String toFilePath(IFile file) {
		if (file != null) {
			IPath rawLocation = file.getRawLocation();
			if (rawLocation != null) {
				rawLocation = rawLocation.makeAbsolute();
				if (rawLocation != null) {
					File f = rawLocation.toFile();
					if (f != null) {
						return f.getAbsolutePath();
					}
				}
			}
		}
		return null;
	}

	// checks if any of the specified files is a sensitive file
	private boolean containsAnySensitiveFile(Set<IPath> files) {
		synchronized (sync) {
			if (live) {
				for (Iterator<IPath> iterator = files.iterator(); iterator.hasNext();) {
					IPath path = iterator.next();
					if (sensitiveFilePathSet.contains(path)) {
						return true;
					}
				}
			}
			return false;
		}
	}

	// resets actual QMake info and notifies all listeners that QMake information has changes
	private void scheduleFetchQMakeInfo() {
		synchronized (sync) {
			if (! live) {
				return;
			}
			qmakeInfo = null;
		}
		for (IQMakeProjectInfoListener listener : listeners) {
			listener.qmakeInfoChanged();
		}
	}

	/**
	 * Represents a project configuration.
	 */
	private final class ControllerImpl implements IQMakeEnvProvider.IController {

		private final ICConfigurationDescription configuration;
		private final IQMakeEnv qmakeEnv;

		public ControllerImpl(ICConfigurationDescription configuration) {
			this.configuration = configuration;
			// qmakeEnv created from registry of qmakeEnvProvider extensions
			this.qmakeEnv = QMakeEnvProviderManager.getInstance().createEnv(this);
		}

		public void destroy() {
			qmakeEnv.destroy();
		}

		public IQMakeEnv getQMakeEnv() {
			return qmakeEnv;
		}

		@Override
		public ICConfigurationDescription getConfiguration() {
			return configuration;
		}

		@Override
		public void scheduleUpdate() {
			scheduleFetchQMakeInfo();
		}

	}

	/**
	 * Listens on Eclipse file system changes.
	 */
	private static final class RCListener implements IResourceChangeListener {

		@Override
		public void resourceChanged(IResourceChangeEvent event) {
			RDVisitor visitor = new RDVisitor();

			// collect project to delete and changed files
			switch (event.getType()) {
			case IResourceChangeEvent.PRE_CLOSE:
			case IResourceChangeEvent.PRE_DELETE:
				IResource project = event.getResource();
				if (project != null && project.getType() == IResource.PROJECT) {
					visitor.addProjectToDelete(project);
				}
				break;
			case IResourceChangeEvent.POST_CHANGE:
				IResourceDelta delta = event.getDelta();
				if (delta != null) {
					try {
						delta.accept(visitor);
					} catch (CoreException e) {
						// empty
					}
				}
				break;
			}

			// process collected data
			visitor.process();
		}

	}

	private static final class RDVisitor implements IResourceDeltaVisitor {

		private final Set<IResource> projectsToDelete = new HashSet<IResource>();
		private final Set<IPath> changedFiles = new HashSet<IPath>();

		@Override
		public boolean visit(IResourceDelta delta) throws CoreException {
			IResource resource = delta.getResource();
			if (resource != null) {
				switch (resource.getType()) {
				case IResource.FILE:
					addChangedFile(resource);
					return false;
				case IResource.PROJECT:
					if (delta.getKind() == IResourceDelta.REMOVED) {
						addProjectToDelete(resource);
						return false;
					}
					break;
				}
			}
			return true;
		}

		private void addProjectToDelete(IResource project) {
			projectsToDelete.add(project);
		}

		private void addChangedFile(IResource file) {
			IPath fullPath = file.getFullPath();
			if (fullPath != null) {
				changedFiles.add(fullPath);
			}
		}

		public void process() {
			// removing projects from CACHE
			for(IResource project : projectsToDelete) {
				removeProjectFromCache(project);
			}

			List<QMakeProjectInfo> infos;
			synchronized (SYNC) {
				infos = new ArrayList<QMakeProjectInfo>(CACHE.values());
			}
			for (QMakeProjectInfo info : infos) {
				// checking if any of the changed files affects QMakeProjectInfo
				if (info.containsAnySensitiveFile(changedFiles)) {
					// if so then scheduling update
					info.scheduleFetchQMakeInfo();
				}
			}
		}

	}

	private static final class SensitiveSet extends HashSet<IPath> {

		// adds a sensitive file in form of a specified absolute path
		private void addSensitiveFile(String sensitiveFile) {
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IFile[] files = root.findFilesForLocation(Path.fromOSString(sensitiveFile));
			if (files != null && files.length > 0) {
				IFile file = files[0];
				addSensitiveFile(file);
			}
		}

		// adds a sensitive file in form of a IFile
		private void addSensitiveFile(IFile file) {
			IPath fullPath = file.getFullPath();
			if (fullPath != null) {
				add(fullPath);
			}
		}

	}

}

Back to the top