Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d95d6e1515f4338853daf610ba81f83946dd96b (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
/*
 * 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.qt.internal.core.index;

import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.qt.core.QtPlugin;
import org.eclipse.cdt.qt.core.index.QtIndex;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;

public class QtFactory {

	private static final char[] QT_VERSION = "QT_VERSION".toCharArray();

	public static QtIndex create(IProject project) {
		CDTIndex cdtIndex = getCDTIndex(project);
		if (cdtIndex == null) {
			QtPlugin.log("could not get CDT index from project " + project.getName());
			return null;
		}

		QtVersion qtVersion = cdtIndex.get(QtVersionAccessor);
		if (qtVersion == null) {
			QtPlugin.log("could not find Qt version in CDT index from project " + project.getName());
			return null;
		}

		if (qtVersion.major == 4 && qtVersion.minor == 8)
			return new QtIndexImpl(cdtIndex);

		// Qt 4.8 is the default implementation, 5.0 support will need to come soon
		return new QtIndexImpl(cdtIndex);
	}

	private static CDTIndex getCDTIndex(IProject project) {
		if (project == null)
			return null;

		ICProject cProject = CoreModel.getDefault().create(project);
		if (cProject == null)
			return null;

		IIndex index = null;
		try {
			index = CCorePlugin.getIndexManager().getIndex(cProject);
		} catch( CoreException e ) {
			QtPlugin.log(e);
			return null;
		}

		return index == null ? null : new CDTIndex(index);
	}

	/**
	 * A small wrapper to hold the result of index lookups for the Qt version.
	 */
	private static class QtVersion {
		public final int major;
		public final int minor;
		@SuppressWarnings("unused")
		public final int patch;

		// QT_VERSION looks like 0x040805
		private static final Pattern Version_regex = Pattern.compile( "0x([a-fA-F\\d]{1,2})([a-fA-F\\d]{2})([a-fA-F\\d]{2})" );

		public static QtVersion create(String version) {
			Matcher m = Version_regex.matcher(version);
			if (!m.matches())
				return null;

			try {
				int major = Integer.parseInt(m.group(1), 16);
				int minor = Integer.parseInt(m.group(2), 16);
				int patch = Integer.parseInt(m.group(3), 16);
				return new QtVersion(major, minor, patch);
			} catch(NumberFormatException e) {
				QtPlugin.log(e);
			}
			return null;
		}

		private QtVersion(int major, int minor, int patch) {
			this.major = major;
			this.minor = minor;
			this.patch = patch;
		}
	}

	private static final CDTIndex.Accessor<QtVersion> QtVersionAccessor = new CDTIndex.Accessor<QtVersion>() {
		@Override
		public QtVersion access(IIndex index) throws CoreException {
			// Multiple macros might be found, sort the values and choose the highest version.
			SortedSet<String> versions = new TreeSet<String>();
			try {
				for(IIndexMacro macro : index.findMacros(QT_VERSION, IndexFilter.ALL, null))
					versions.add(new String(macro.getExpansion()).toLowerCase());
			} catch( CoreException e ) { }

			// don't create the Qt index if there is no Qt information in the CDT index
			if (versions.size() <= 0)
				return null;

			// the highest version has been sorted to the last position
			return QtVersion.create(versions.last());
		}
	};
}

Back to the top