Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ThemeInfo.cpp « mumble « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58abaf705c096783e19db533e3dd55457f5545c7 (plain)
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
// Copyright 2005-2019 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "mumble_pch.hpp"

#include "ThemeInfo.h"

QFileInfo ThemeInfo::StyleInfo::getPlatformQss() const {
#if defined(Q_OS_WIN)
	return qssFiles.value(QLatin1String("WIN"), defaultQss);
#elif defined(Q_OS_LINUX)
	return qssFiles.value(QLatin1String("LINUX"), defaultQss);
#elif defined(Q_OS_MAC)
	return qssFiles.value(QLatin1String("MAC"), defaultQss);
#else
	return defaultQss;
#endif
}

boost::optional<ThemeInfo::StyleInfo> readStyleFromConfig(QSettings &themeConfig, const QString &styleId, const ThemeInfo &theme, const QDir &themeDir) {
	QRegExp qssPlatformRegex(QString::fromLatin1("^%1/qss_(.*)").arg(styleId));
	
	ThemeInfo::StyleInfo style;
	
	style.name = themeConfig.value(QString::fromLatin1("%1/name").arg(styleId)).toString();
	style.themeName = theme.name;
	style.defaultQss = QFileInfo(themeDir.filePath(themeConfig.value(QString::fromLatin1("%1/qss").arg(styleId)).toString()));
	
	if (style.name.isNull()) {
		qWarning() << "Style " << styleId << " of theme" << theme.name << " has no name, skipping theme";
		return boost::none;
	}
	
	if (!style.defaultQss.exists() || !style.defaultQss.isFile()) {
		qWarning() << "Style " << style.name << " of theme " << theme.name << " references invalid qss " << style.defaultQss.filePath() << ", skipping theme";
		return boost::none;
	}
	
	foreach (const QString &platformQssConfig, themeConfig.allKeys().filter(qssPlatformRegex)) {
		qssPlatformRegex.indexIn(platformQssConfig);
		const QString platform = qssPlatformRegex.cap(1);
		
		QFileInfo platformQss = (themeDir.filePath(themeConfig.value(platformQssConfig).toString()));
		if (!platformQss.exists() || !platformQss.isFile()) {
			qWarning() << "Style" << style.name << " of theme " << theme.name << " references invalid qss " << platformQss.filePath() << " for platform " << platform << ", skipping theme";
			return boost::none;
		}
		
		style.qssFiles.insert(platform, platformQss);
	}
	
	return style;
}

boost::optional<ThemeInfo> loadLegacyThemeInfo(const QDir &themeDirectory) {
	ThemeInfo theme;
	theme.name = themeDirectory.dirName();
	
	QStringList filters;
	filters << QLatin1String("*.qss");
	
	foreach (const QFileInfo &qssFile, themeDirectory.entryInfoList(filters, QDir::Files)) {
		ThemeInfo::StyleInfo style;
		style.name = qssFile.baseName();
		style.themeName = theme.name;
		style.defaultQss = qssFile;
		
		theme.styles.insert(style.name, style);
	}
	
	if (theme.styles.isEmpty()) {
		qWarning() << themeDirectory.absolutePath() << " does not seem to contain a old-style theme, skipping";
		return boost::none;
	}
	
	theme.defaultStyle = theme.styles.begin()->name;

	return theme;
}

boost::optional<ThemeInfo> ThemeInfo::load(const QDir &themeDirectory) {
	QFile themeFile(themeDirectory.absoluteFilePath(QLatin1String("theme.ini")));
	if (!themeFile.exists()) {
		qWarning() << "Directory " << themeDirectory.absolutePath() << " has no theme.ini, trying fallback";
		
		return loadLegacyThemeInfo(themeDirectory);
	}
	
	QSettings themeConfig(themeFile.fileName(), QSettings::IniFormat);
	if (themeConfig.status() != QSettings::NoError) {
		qWarning() << "Failed to load theme config from " << themeFile.fileName() << ", skipping";
		return boost::none;
	}
	
	ThemeInfo theme;
	QStringList styleIds;
	
	theme.name = themeConfig.value(QLatin1String("theme/name")).toString();
	styleIds = themeConfig.value(QLatin1String("theme/styles")).toStringList();
	
	if (theme.name.isNull()) {
		qWarning() << "Theme in " << themeFile.fileName() << " does not have a name, skipping";
		return boost::none;
	}
	
	if (styleIds.isEmpty()) {
		qWarning() << "Theme " << theme.name << " doesn't have any styles, skipping";
		return boost::none;
	}
	
	foreach (const QString &styleId, styleIds) {
		boost::optional<ThemeInfo::StyleInfo> style = readStyleFromConfig(themeConfig, styleId, theme, themeDirectory);
		if (!style) {
			return boost::none;
		}
		theme.styles.insert(style->name, *style);
		
	}
	
	theme.defaultStyle = theme.styles.begin()->name;
	
	return theme;
}

ThemeInfo::StyleInfo ThemeInfo::getStyle(QString name_) const {
	Q_ASSERT(styles.contains(defaultStyle));
	
	return styles.value(name_, styles.value(defaultStyle));
}


ThemeMap ThemeInfo::scanDirectory(const QDir &themesDirectory) {
	ThemeMap themes;
	
	foreach (const QFileInfo &subdirInfo, themesDirectory.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot)) {
		QDir subdir(subdirInfo.absoluteFilePath());
		
		boost::optional<ThemeInfo> theme = ThemeInfo::load(subdir);
		if (!theme) {
			continue;
		}
		
		themes.insert(theme->name, *theme);
	};
	
	return themes;
}

ThemeMap ThemeInfo::scanDirectories(const QVector<QDir> &themesDirectories) {
	ThemeMap themes;
	
	foreach (const QDir &themesDirectory, themesDirectories) {
		foreach (const ThemeInfo &theme, scanDirectory(themesDirectory)) {
			themes.insert(theme.name, theme);
		}
	}
	
	return themes;
}