2017-10-09 08:35:48 +02:00
/*****************************************************************************
2018-05-24 22:32:00 +02:00
* gta5view Grand Theft Auto V Profile Viewer
2021-01-29 19:46:01 +01:00
* Copyright ( C ) 2016 - 2021 Syping
2017-10-09 08:35:48 +02:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program 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
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "config.h"
# include "AppEnv.h"
# include "StringParser.h"
# include "StandardPaths.h"
# include <QtGlobal>
# include <QStringBuilder>
# include <QApplication>
# include <QSettings>
# include <QScreen>
# include <QDebug>
# include <QRect>
# include <QDir>
2020-09-28 05:33:04 +02:00
# if QT_VERSION < 0x050000
# include <QDesktopWidget>
# endif
2017-10-09 08:35:48 +02:00
AppEnv : : AppEnv ( )
{
}
2018-01-11 08:41:00 +01:00
// Build Stuff
QString AppEnv : : getBuildDateTime ( )
{
return GTA5SYNC_BUILDDATETIME ;
}
2018-02-07 10:20:59 +01:00
QString AppEnv : : getBuildCode ( )
{
return GTA5SYNC_BUILDCODE ;
}
2017-10-09 08:35:48 +02:00
// Folder Stuff
QString AppEnv : : getGameFolder ( bool * ok )
{
QDir dir ;
QString GTAV_FOLDER = QString : : fromUtf8 ( qgetenv ( " GTAV_FOLDER " ) ) ;
2021-01-29 19:46:01 +01:00
if ( GTAV_FOLDER ! = " " ) {
2017-10-09 08:35:48 +02:00
dir . setPath ( GTAV_FOLDER ) ;
2021-01-29 19:46:01 +01:00
if ( dir . exists ( ) ) {
if ( ok ! = NULL )
* ok = true ;
2017-10-09 08:35:48 +02:00
qputenv ( " GTAV_FOLDER " , dir . absolutePath ( ) . toUtf8 ( ) ) ;
return dir . absolutePath ( ) ;
}
}
2021-04-02 19:51:05 +02:00
const QString GTAV_defaultFolder = StandardPaths : : documentsLocation ( ) % " /Rockstar Games/GTA V " ;
2017-10-09 08:35:48 +02:00
QString GTAV_returnFolder = GTAV_defaultFolder ;
QSettings settings ( GTA5SYNC_APPVENDOR , GTA5SYNC_APPSTR ) ;
settings . beginGroup ( " dir " ) ;
bool forceDir = settings . value ( " force " , false ) . toBool ( ) ;
GTAV_returnFolder = settings . value ( " dir " , GTAV_defaultFolder ) . toString ( ) ;
settings . endGroup ( ) ;
2021-01-29 19:46:01 +01:00
if ( forceDir ) {
2017-10-09 08:35:48 +02:00
dir . setPath ( GTAV_returnFolder ) ;
2021-01-29 19:46:01 +01:00
if ( dir . exists ( ) ) {
if ( ok ! = 0 )
* ok = true ;
2017-10-09 08:35:48 +02:00
qputenv ( " GTAV_FOLDER " , dir . absolutePath ( ) . toUtf8 ( ) ) ;
return dir . absolutePath ( ) ;
}
}
dir . setPath ( GTAV_defaultFolder ) ;
2021-01-29 19:46:01 +01:00
if ( dir . exists ( ) ) {
if ( ok ! = 0 )
* ok = true ;
2017-10-09 08:35:48 +02:00
qputenv ( " GTAV_FOLDER " , dir . absolutePath ( ) . toUtf8 ( ) ) ;
return dir . absolutePath ( ) ;
}
2021-01-29 19:46:01 +01:00
if ( ! forceDir ) {
2017-10-09 08:35:48 +02:00
dir . setPath ( GTAV_returnFolder ) ;
2021-01-29 19:46:01 +01:00
if ( dir . exists ( ) ) {
if ( ok ! = 0 )
* ok = true ;
2017-10-09 08:35:48 +02:00
qputenv ( " GTAV_FOLDER " , dir . absolutePath ( ) . toUtf8 ( ) ) ;
return dir . absolutePath ( ) ;
}
}
2021-01-29 19:46:01 +01:00
if ( ok ! = 0 )
* ok = false ;
return QString ( ) ;
2017-10-09 08:35:48 +02:00
}
bool AppEnv : : setGameFolder ( QString gameFolder )
{
QDir dir ;
dir . setPath ( gameFolder ) ;
2021-01-29 19:46:01 +01:00
if ( dir . exists ( ) ) {
2017-10-09 08:35:48 +02:00
qputenv ( " GTAV_FOLDER " , dir . absolutePath ( ) . toUtf8 ( ) ) ;
return true ;
}
return false ;
}
QString AppEnv : : getExLangFolder ( )
{
return StringParser : : convertBuildedString ( GTA5SYNC_LANG ) ;
}
QString AppEnv : : getInLangFolder ( )
{
# ifdef GTA5SYNC_QCONF
# ifdef GTA5SYNC_INLANG
return StringParser : : convertBuildedString ( GTA5SYNC_INLANG ) ;
# else
2021-04-02 19:51:05 +02:00
return StringParser : : convertBuildedString ( GTA5SYNC_SHARE % QLatin1String ( " /APPNAME:/translations " ) ) ;
2017-10-09 08:35:48 +02:00
# endif
# else
# ifdef GTA5SYNC_INLANG
return StringParser : : convertBuildedString ( GTA5SYNC_INLANG ) ;
# else
2021-04-02 19:51:05 +02:00
return QLatin1String ( " :/tr " ) ;
2017-10-09 08:35:48 +02:00
# endif
# endif
}
QString AppEnv : : getPluginsFolder ( )
{
return StringParser : : convertBuildedString ( GTA5SYNC_PLUG ) ;
}
2021-04-02 19:51:05 +02:00
QString AppEnv : : getImagesFolder ( )
{
# if defined(GTA5SYNC_QCONF) && defined(GTA5SYNC_CMAKE)
return StringParser : : convertBuildedString ( GTA5SYNC_SHARE % QLatin1String ( " /APPNAME:/resources " ) ) ;
# else
return QLatin1String ( " :/img " ) ;
# endif
}
QString AppEnv : : getShareFolder ( )
{
return StringParser : : convertBuildedString ( GTA5SYNC_SHARE ) ;
}
2017-10-09 08:35:48 +02:00
// Web Stuff
QByteArray AppEnv : : getUserAgent ( )
{
2018-06-24 02:25:34 +02:00
# if QT_VERSION >= 0x050400
2020-09-26 16:12:55 +02:00
# ifdef Q_OS_WIN
2018-06-24 02:25:34 +02:00
QString kernelVersion = QSysInfo : : kernelVersion ( ) ;
const QStringList & kernelVersionList = kernelVersion . split ( " . " ) ;
2021-01-29 19:46:01 +01:00
if ( kernelVersionList . length ( ) > 2 ) {
2018-06-24 02:25:34 +02:00
kernelVersion = kernelVersionList . at ( 0 ) % " . " % kernelVersionList . at ( 1 ) ;
}
QString runArch = QSysInfo : : buildCpuArchitecture ( ) ;
2021-01-29 19:46:01 +01:00
if ( runArch = = " x86_64 " ) {
2018-06-24 02:25:34 +02:00
runArch = " Win64; x64 " ;
}
2021-01-29 19:46:01 +01:00
else if ( runArch = = " i686 " ) {
2018-06-24 02:25:34 +02:00
const QString & curArch = QSysInfo : : currentCpuArchitecture ( ) ;
2021-01-29 19:46:01 +01:00
if ( curArch = = " x86_64 " ) {
2018-06-24 02:25:34 +02:00
runArch = " WOW64 " ;
}
2021-01-29 19:46:01 +01:00
else if ( curArch = = " i686 " ) {
2018-06-24 02:25:34 +02:00
runArch = " Win32; x86 " ;
}
}
return QString ( " Mozilla/5.0 (Windows NT %1; %2) %3/%4 " ) . arg ( kernelVersion , runArch , GTA5SYNC_APPSTR , GTA5SYNC_APPVER ) . toUtf8 ( ) ;
# else
return QString ( " Mozilla/5.0 (%1; %2) %3/%4 " ) . arg ( QSysInfo : : kernelType ( ) , QSysInfo : : kernelVersion ( ) , GTA5SYNC_APPSTR , GTA5SYNC_APPVER ) . toUtf8 ( ) ;
# endif
# else
return QString ( " Mozilla/5.0 %1/%2 " ) . arg ( GTA5SYNC_APPSTR , GTA5SYNC_APPVER ) . toUtf8 ( ) ;
# endif
2017-10-09 08:35:48 +02:00
}
QUrl AppEnv : : getCrewFetchingUrl ( QString crewID )
{
return QUrl ( QString ( " https://socialclub.rockstargames.com/crew/%1/%1 " ) . arg ( crewID ) ) ;
}
QUrl AppEnv : : getPlayerFetchingUrl ( QString crewID , QString pageNumber )
{
2018-06-29 02:50:00 +02:00
return QUrl ( QString ( " https://socialclub.rockstargames.com/crewsapi/GetMembersList?crewId=%1&pageNumber=%2&pageSize=5000 " ) . arg ( crewID , pageNumber ) ) ;
2017-10-09 08:35:48 +02:00
}
QUrl AppEnv : : getPlayerFetchingUrl ( QString crewID , int pageNumber )
{
return getPlayerFetchingUrl ( crewID , QString : : number ( pageNumber ) ) ;
}
2018-07-28 04:55:55 +02:00
// Game Stuff
GameVersion AppEnv : : getGameVersion ( )
{
2020-09-26 16:12:55 +02:00
# ifdef Q_OS_WIN
2018-07-28 04:55:55 +02:00
QString argumentValue ;
# ifdef _WIN64
argumentValue = " \\ WOW6432Node " ;
# endif
QSettings registrySettingsSc ( QString ( " HKEY_LOCAL_MACHINE \\ SOFTWARE%1 \\ Rockstar Games \\ Grand Theft Auto V " ) . arg ( argumentValue ) , QSettings : : NativeFormat ) ;
QString installFolderSc = registrySettingsSc . value ( " InstallFolder " , " " ) . toString ( ) ;
QDir installFolderScDir ( installFolderSc ) ;
bool scVersionInstalled = false ;
2021-01-29 19:46:01 +01:00
if ( ! installFolderSc . isEmpty ( ) & & installFolderScDir . exists ( ) ) {
2018-07-28 04:55:55 +02:00
# ifdef GTA5SYNC_DEBUG
qDebug ( ) < < " gameVersionFoundSocialClubVersion " ;
# endif
scVersionInstalled = true ;
}
QSettings registrySettingsSteam ( QString ( " HKEY_LOCAL_MACHINE \\ SOFTWARE%1 \\ Rockstar Games \\ GTAV " ) . arg ( argumentValue ) , QSettings : : NativeFormat ) ;
QString installFolderSteam = registrySettingsSteam . value ( " installfoldersteam " , " " ) . toString ( ) ;
2021-01-29 19:46:01 +01:00
if ( installFolderSteam . right ( 5 ) = = " \\ GTAV " ) {
2018-07-28 04:55:55 +02:00
installFolderSteam = installFolderSteam . remove ( installFolderSteam . length ( ) - 5 , 5 ) ;
}
QDir installFolderSteamDir ( installFolderSteam ) ;
bool steamVersionInstalled = false ;
2021-01-29 19:46:01 +01:00
if ( ! installFolderSteam . isEmpty ( ) & & installFolderSteamDir . exists ( ) ) {
2018-07-28 04:55:55 +02:00
# ifdef GTA5SYNC_DEBUG
qDebug ( ) < < " gameVersionFoundSteamVersion " ;
# endif
steamVersionInstalled = true ;
}
2021-01-29 19:46:01 +01:00
if ( scVersionInstalled & & steamVersionInstalled ) {
2018-07-28 04:55:55 +02:00
return GameVersion : : BothVersions ;
}
2021-01-29 19:46:01 +01:00
else if ( scVersionInstalled ) {
2018-07-28 04:55:55 +02:00
return GameVersion : : SocialClubVersion ;
}
2021-01-29 19:46:01 +01:00
else if ( steamVersionInstalled ) {
2018-07-28 04:55:55 +02:00
return GameVersion : : SteamVersion ;
}
2021-01-29 19:46:01 +01:00
else {
2018-07-28 04:55:55 +02:00
return GameVersion : : NoVersion ;
}
# else
return GameVersion : : NoVersion ;
# endif
}
GameLanguage AppEnv : : getGameLanguage ( GameVersion gameVersion )
{
2021-01-29 19:46:01 +01:00
if ( gameVersion = = GameVersion : : SocialClubVersion ) {
2020-09-26 16:12:55 +02:00
# ifdef Q_OS_WIN
2018-07-28 04:55:55 +02:00
QString argumentValue ;
# ifdef _WIN64
argumentValue = " \\ WOW6432Node " ;
# endif
QSettings registrySettingsSc ( QString ( " HKEY_LOCAL_MACHINE \\ SOFTWARE%1 \\ Rockstar Games \\ Grand Theft Auto V " ) . arg ( argumentValue ) , QSettings : : NativeFormat ) ;
QString languageSc = registrySettingsSc . value ( " Language " , " " ) . toString ( ) ;
return gameLanguageFromString ( languageSc ) ;
# else
return GameLanguage : : Undefined ;
# endif
}
2021-01-29 19:46:01 +01:00
else if ( gameVersion = = GameVersion : : SteamVersion ) {
2020-09-26 16:12:55 +02:00
# ifdef Q_OS_WIN
2018-07-28 04:55:55 +02:00
QString argumentValue ;
# ifdef _WIN64
argumentValue = " \\ WOW6432Node " ;
# endif
QSettings registrySettingsSteam ( QString ( " HKEY_LOCAL_MACHINE \\ SOFTWARE%1 \\ Rockstar Games \\ Grand Theft Auto V Steam " ) . arg ( argumentValue ) , QSettings : : NativeFormat ) ;
QString languageSteam = registrySettingsSteam . value ( " Language " , " " ) . toString ( ) ;
return gameLanguageFromString ( languageSteam ) ;
# else
return GameLanguage : : Undefined ;
# endif
}
2021-01-29 19:46:01 +01:00
return GameLanguage : : Undefined ;
2018-07-28 04:55:55 +02:00
}
GameLanguage AppEnv : : gameLanguageFromString ( QString gameLanguage )
{
2021-01-29 19:46:01 +01:00
if ( gameLanguage = = " en-US " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : English ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " fr-FR " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : French ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " it-IT " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Italian ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " de-DE " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : German ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " es-ES " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Spanish ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " es-MX " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Mexican ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " pt-BR " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Brasilian ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " ru-RU " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Russian ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " pl-PL " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Polish ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " ja-JP " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : Japanese ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " zh-CHS " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : SChinese ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " zh-CHT " ) {
2018-07-28 04:55:55 +02:00
return GameLanguage : : TChinese ;
}
2021-01-29 19:46:01 +01:00
else if ( gameLanguage = = " ko-KR " ) {
return GameLanguage : : Korean ;
2018-07-28 04:55:55 +02:00
}
2021-01-29 19:46:01 +01:00
return GameLanguage : : Undefined ;
2018-07-28 04:55:55 +02:00
}
QString AppEnv : : gameLanguageToString ( GameLanguage gameLanguage )
{
2021-01-29 19:46:01 +01:00
switch ( gameLanguage ) {
case GameLanguage : : English :
2018-07-28 04:55:55 +02:00
return " en-US " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : French :
2018-07-28 04:55:55 +02:00
return " fr-FR " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Italian :
2018-07-28 04:55:55 +02:00
return " it-IT " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : German :
2018-07-28 04:55:55 +02:00
return " de-DE " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Spanish :
2018-07-28 04:55:55 +02:00
return " es-ES " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Mexican :
2018-07-28 04:55:55 +02:00
return " es-MX " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Brasilian :
2018-07-28 04:55:55 +02:00
return " pt-BR " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Polish :
2018-07-28 04:55:55 +02:00
return " pl-PL " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Japanese :
2018-07-28 04:55:55 +02:00
return " ja-JP " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : SChinese :
2018-07-28 04:55:55 +02:00
return " zh-CHS " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : TChinese :
2018-07-28 04:55:55 +02:00
return " zh-CHT " ;
2021-01-29 19:46:01 +01:00
case GameLanguage : : Korean :
2018-07-28 04:55:55 +02:00
return " ko-KR " ;
2021-01-29 19:46:01 +01:00
default :
2018-07-28 04:55:55 +02:00
return " Undefinied " ;
}
}
bool AppEnv : : setGameLanguage ( GameVersion gameVersion , GameLanguage gameLanguage )
{
bool socialClubVersion = false ;
bool steamVersion = false ;
2021-01-29 19:46:01 +01:00
if ( gameVersion = = GameVersion : : SocialClubVersion ) {
2018-07-28 04:55:55 +02:00
socialClubVersion = true ;
}
2021-01-29 19:46:01 +01:00
else if ( gameVersion = = GameVersion : : SteamVersion ) {
2018-07-28 04:55:55 +02:00
steamVersion = true ;
}
2021-01-29 19:46:01 +01:00
else if ( gameVersion = = GameVersion : : BothVersions ) {
2018-07-28 04:55:55 +02:00
socialClubVersion = true ;
steamVersion = true ;
}
2021-01-29 19:46:01 +01:00
else {
2018-07-28 04:55:55 +02:00
return false ;
}
2021-01-29 19:46:01 +01:00
if ( socialClubVersion ) {
2020-09-26 16:12:55 +02:00
# ifdef Q_OS_WIN
2018-07-28 04:55:55 +02:00
QString argumentValue ;
# ifdef _WIN64
argumentValue = " \\ WOW6432Node " ;
# endif
QSettings registrySettingsSc ( QString ( " HKEY_LOCAL_MACHINE \\ SOFTWARE%1 \\ Rockstar Games \\ Grand Theft Auto V " ) . arg ( argumentValue ) , QSettings : : NativeFormat ) ;
2021-01-29 19:46:01 +01:00
if ( gameLanguage ! = GameLanguage : : Undefined ) {
2018-07-28 04:55:55 +02:00
registrySettingsSc . setValue ( " Language " , gameLanguageToString ( gameLanguage ) ) ;
}
2021-01-29 19:46:01 +01:00
else {
2018-07-28 04:55:55 +02:00
registrySettingsSc . remove ( " Language " ) ;
}
registrySettingsSc . sync ( ) ;
2021-01-29 19:46:01 +01:00
if ( registrySettingsSc . status ( ) ! = QSettings : : NoError ) {
2018-07-28 04:55:55 +02:00
return false ;
}
2018-08-05 15:29:30 +02:00
# else
Q_UNUSED ( gameLanguage )
2018-07-28 04:55:55 +02:00
# endif
}
2021-01-29 19:46:01 +01:00
if ( steamVersion ) {
2020-09-26 16:12:55 +02:00
# ifdef Q_OS_WIN
2018-07-28 04:55:55 +02:00
QString argumentValue ;
# ifdef _WIN64
argumentValue = " \\ WOW6432Node " ;
# endif
QSettings registrySettingsSteam ( QString ( " HKEY_LOCAL_MACHINE \\ SOFTWARE%1 \\ Rockstar Games \\ Grand Theft Auto V Steam " ) . arg ( argumentValue ) , QSettings : : NativeFormat ) ;
2021-01-29 19:46:01 +01:00
if ( gameLanguage ! = GameLanguage : : Undefined ) {
2018-07-28 04:55:55 +02:00
registrySettingsSteam . setValue ( " Language " , gameLanguageToString ( gameLanguage ) ) ;
}
2021-01-29 19:46:01 +01:00
else {
2018-07-28 04:55:55 +02:00
registrySettingsSteam . remove ( " Language " ) ;
}
registrySettingsSteam . sync ( ) ;
2021-01-29 19:46:01 +01:00
if ( registrySettingsSteam . status ( ) ! = QSettings : : NoError ) {
2018-07-28 04:55:55 +02:00
return false ;
}
2018-08-05 15:29:30 +02:00
# else
Q_UNUSED ( gameLanguage )
2018-07-28 04:55:55 +02:00
# endif
}
return true ;
}
// Screen Stuff
2017-10-09 08:35:48 +02:00
qreal AppEnv : : screenRatio ( )
{
# if QT_VERSION >= 0x050000
2020-04-24 05:36:11 +02:00
qreal dpi = QApplication : : primaryScreen ( ) - > logicalDotsPerInch ( ) ;
2017-10-09 08:35:48 +02:00
# else
2020-04-24 05:36:11 +02:00
qreal dpi = QApplication : : desktop ( ) - > logicalDpiX ( ) ;
2017-10-09 08:35:48 +02:00
# endif
# ifdef Q_OS_MAC
return ( dpi / 72 ) ;
# else
return ( dpi / 96 ) ;
# endif
}
2019-01-13 14:32:12 +01:00
qreal AppEnv : : screenRatioPR ( )
{
# if QT_VERSION >= 0x050600
2020-04-24 05:36:11 +02:00
return QApplication : : primaryScreen ( ) - > devicePixelRatio ( ) ;
2019-01-13 14:32:12 +01:00
# else
return 1 ;
# endif
}