Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 091f54abcb8320bef8b55ca7224a22f4237c0a22 (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
/*
 * Copyright (c) 2014 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.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.CProjectDescriptionEvent;
import org.eclipse.cdt.core.settings.model.ICDescriptionDelta;
import org.eclipse.cdt.core.settings.model.ICProjectDescriptionListener;
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.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

/**
 * Represents a management of QMakeProjectInfo instances and manages life-cycle of all QMakeProjectInfo instances.
 */
public class QMakeProjectInfoManager {

	private static final PDListener PD_LISTENER = new PDListener();
	private static final RCListener RC_LISTENER = new RCListener();

	// sync object for CACHE field
	private static final Object CACHE_SYNC = new Object();
	// a list of all QMakeProjectInfo instances
	private static Map<IProject,QMakeProjectInfo> CACHE;

	// called by QtPlugin activator to setup this class
	public static final void start() {
		synchronized (CACHE_SYNC) {
			CACHE = new HashMap<IProject,QMakeProjectInfo>();
		}
		CoreModel.getDefault().addCProjectDescriptionListener(PD_LISTENER, CProjectDescriptionEvent.LOADED | CProjectDescriptionEvent.APPLIED);
		ResourcesPlugin.getWorkspace().addResourceChangeListener(RC_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(RC_LISTENER);
		CoreModel.getDefault().removeCProjectDescriptionListener(PD_LISTENER);
		List<QMakeProjectInfo> infos;
		synchronized (CACHE_SYNC) {
			infos = new ArrayList<QMakeProjectInfo>(CACHE.values());
			CACHE = null;
		}
		for (QMakeProjectInfo info : infos) {
			if (info != null) {
				info.destroy();
			}
		}
	}

	/**
	 * 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) {
		return getQMakeProjectInfoFor(project, true);
	}
	
	private static QMakeProjectInfo getQMakeProjectInfoFor(IProject project, boolean create) {
		QMakeProjectInfo info;
		synchronized (CACHE_SYNC) {
			// If the cache is null then this must be a late notification after shutdown.  We
			// can't do anything so don't try.
			if (CACHE == null)
				return null;

			info = CACHE.get(project);
			if (info != null) {
				return info;
			}
			if (! create) {
				// do not create, just return null
				return null;
			}
			info = new QMakeProjectInfo(project);
			CACHE.put(project, info);
		}
		info.updateState();
		return info;
	}

	// removes the project from the CACHE
	private static void removeProjectFromCache(IResource project) {
		QMakeProjectInfo info;
		synchronized (CACHE_SYNC) {
			// If the cache is null then this must be a late notification after shutdown.  We
			// can't do anything so don't try.
			if (CACHE == null)
				return;

			info = CACHE.remove(project);
		}
		if (info != null) {
			info.destroy();
		}
	}

	private static final class PDListener implements ICProjectDescriptionListener {

		// called on active project configuration change
		@Override
		public void handleEvent(CProjectDescriptionEvent event) {
			ICDescriptionDelta projectDelta = event.getProjectDelta();
			if (projectDelta != null) {
				if ((projectDelta.getChangeFlags() & ICDescriptionDelta.ACTIVE_CFG) != 0) {
					QMakeProjectInfo info = getQMakeProjectInfoFor(event.getProject(), false);
					if (info != null) {
						info.updateState();
					}
				}
			}
		}

	}

	/**
	 * 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<IResource> projectsToUpdate = 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:
					switch (delta.getKind()) {
					case IResourceDelta.CHANGED:
						if ((delta.getFlags() & IResourceDelta.DESCRIPTION) != 0) {
							addProjectToUpdate(resource);
						}
						return true;
					case IResourceDelta.REMOVED:
						addProjectToDelete(resource);
						return false;
					}
					break;
				}
			}
			return true;
		}

		private void addProjectToUpdate(IResource project) {
			projectsToUpdate.add(project);
		}

		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 (CACHE_SYNC) {
				// If the cache is null then this must be a late notification after shutdown.  We
				// can't do anything so don't try.
				if (CACHE == null)
					return;

				infos = new ArrayList<QMakeProjectInfo>(CACHE.values());
			}
			for (QMakeProjectInfo info : infos) {
				// checking if any project description change or any of the changed files affect QMakeProjectInfo
				if (projectsToUpdate.contains(info.getProject()) || info.containsAnySensitiveFile(changedFiles)) {
					// if so then updating
					info.updateState();
				}
			}
		}

	}

}

Back to the top