WSL/SLF GitLab Repository

os.cc 6.83 KB
Newer Older
1
2
3
4
5
/*****************************************************************************/
/*  Copyright 2020 WSL Institute for Snow and Avalanche Research  SLF-DAVOS  */
/*****************************************************************************/
/* This file is part of INIshell.
   INIshell is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
8
9
10
11
12
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   INIshell is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14

15
16
   You should have received a copy of the GNU General Public License
   along with INIshell.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
20
*/

#include "os.h"

21
#include <QCoreApplication>
22
#include <QDir>
23
#include <QKeySequence>
24
#include <QPalette>
25
#include <QSettings>
26
27
28
29
30
#include <QStandardPaths>
#include <QtGlobal>

namespace os {

31
32
33
34
35
36
37
38
39
/**
 * @brief Choose a number of locations to look for applications at.
 * @param[out] locations List of locations which will have the system specific ones appended.
 */
void getSystemLocations(QStringList &locations)
{
#if defined Q_OS_WIN
	locations << "../.."; //this is useful for some out of tree builds
	locations << QCoreApplication::applicationDirPath(); //directory that contains the application executable
40
	locations << QCoreApplication::applicationDirPath() + "/../share";
41
42
43
44
45
46
47
48
49
	locations << QCoreApplication::applicationDirPath() + "/..";
	locations << QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation); //location where persistent application data can be stored
#endif
#if defined Q_OS_MAC
	locations << "./../../../.."; //this is useful for some out of tree builds: we must get out of the bundle
	locations << QCoreApplication::applicationDirPath();
50
	locations << QCoreApplication::applicationDirPath() + "/../share";
51
52
53
54
55
56
57
58
59
	locations << QCoreApplication::applicationDirPath() + "/..";
	locations << QCoreApplication::applicationDirPath() + "/../../../.."; //we must get out of the bundle
	locations << QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
	locations << QDir::homePath() + "/usr/share";
#endif
#if !defined Q_OS_WIN && !defined Q_OS_MAC
	locations << QCoreApplication::applicationDirPath(); //directory that contains the application executable
60
	locations << QCoreApplication::applicationDirPath() + "/../share";
61
62
63
64
65
66
67
68
69
	locations << QCoreApplication::applicationDirPath() + "/..";
	locations << QStandardPaths::standardLocations(QStandardPaths::DesktopLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); //$HOME/Documents
	locations << QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
	locations << QStandardPaths::standardLocations(QStandardPaths::DataLocation);
	locations << QDir::homePath() + "/usr/share";
#endif
}

70
71
72
73
74
75
/**
 * @brief Check if we're running on KDE Desktop environment.
 * @details On a well-tuned KDE, things should be the smoothest.
 * @return True if on KDE.
 */
bool isKDE() {
76
	const QString DE( QString::fromLocal8Bit(qgetenv("XDG_CURRENT_DESKTOP")) );
77
78
79
80
81
82
83
84
85
86
87
88
89
	return (DE == "KDE");
}

/**
 * @brief Looking at the default palette, return true if it is a dark theme
 * @return true if the current color theme is dark
 */
bool isDarkTheme()
{
#ifdef Q_OS_WIN
	QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings::NativeFormat);
	return (settings.value("AppsUseLightTheme")==0);
#else
90
	const int bg_lightness = QPalette().color(QPalette::Window).lightness();
91
	return (bg_lightness < 128);
92
93
94
95
#endif
}

/**
96
97
 * @brief Extended search paths for executables, to preprend or append to a PATH variable.
 * @param[in] appname application's name.
98
 */
99
QString getExtraPath(const QString& appname)
100
{
101
	const QString own_path( QCoreApplication::applicationDirPath() ); //so exe copied next to inishell are found, this is usefull for packaging
Mathias Bavay's avatar
Mathias Bavay committed
102
103
	const QString home( QDir::homePath() );
	const QString desktop( QStandardPaths::standardLocations(QStandardPaths::DesktopLocation).at(0) ); //DesktopLocation always returns 1 element
104

105
#if defined Q_OS_WIN
106
	QString extra_path( ";" + desktop+"\\"+appname+"\\bin;" + home+"\\src\\"+appname+"\\bin;" + "D:\\src\\"+appname+"\\bin;" + "C:\\Program Files\\"+appname+"\\bin;" + "C:\\Program Files (x86)\\"+appname+"\\bin;" + own_path);
107

108
109
110
111
	const QString reg_key("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + appname + "\\UninstallString");
	QSettings settings;
	const QString uninstallExe( settings.value(reg_key).toString() );
	if (!uninstallExe.isEmpty()) {
112
		const QString installPath( QFileInfo( uninstallExe ).absolutePath() );
113
		if (!installPath.isEmpty())
Mathias Bavay's avatar
Mathias Bavay committed
114
			extra_path.append( ";"+installPath );
115
	}
116

117
	return extra_path;
118
119
#endif
#if defined Q_OS_MAC
120
	const QString extra_path( ":" +home+"/bin:" + home+"/usr/bin:" + home+"/src/"+appname+"/bin:" + desktop+"/"+appname+"/bin:" + "/Applications/"+appname+".app/Contents/bin:" + own_path);
121
	return extra_path;
122
123
#endif
#if !defined Q_OS_WIN && !defined Q_OS_MAC
124
	const QString extra_path( ":" + home+"/bin:" + home+"/usr/bin:" + home+"/src/"+appname+"/bin:" + desktop+"/"+appname+"/bin:" + "/opt/"+appname+"/bin:" + own_path );
125
	return extra_path;
126
127
128
#endif
}

129
130
131
132
133
134
135
136
137
138
139
/**
 * @brief Extend path where to search for executables, ie a proper initialization for a PATH variable.
 * @param[in] appname application's name.
 */
void setSystemPath(const QString& appname)
{
	static const QString root_path( QString::fromLocal8Bit(qgetenv("PATH")) ); //original PATH content with platform specific additions
	QString env_path( root_path + getExtraPath(appname) );
	qputenv("PATH", env_path.toLocal8Bit());
}

140
141
142
143
144
145
/**
 * @brief Return native help keyboard shortcut as string to use in guidance texts.
 * @return Help key sequence as string.
 */
QString getHelpSequence() {
	QKeySequence help_sequence(QKeySequence::HelpContents);
146
	return help_sequence.toString(QKeySequence::NativeText); //display as is on the machine
147
148
}

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * @brief Return the user's logname of the computer running the current process.
 * @return current username or empty string
 */
QString getLogName()
{
	char *tmp;

	if ((tmp=getenv("USERNAME"))==NULL) { //Windows & Unix
		if ((tmp=getenv("LOGNAME"))==NULL) { //Unix
			tmp=getenv("USER"); //Windows & Unix
		}
	}

	if (tmp==NULL) return QString();
	return QString(tmp);
}

167
} //namespace os