2024-04-21 16:56:39 +02:00
/*
2025-01-15 20:30:48 +01:00
* helper . cpp , part of VCMI engine
2024-04-21 16:56:39 +02:00
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
# include "StdInc.h"
# include "helper.h"
2025-03-17 21:58:31 +01:00
# include "mainwindow_moc.h"
# include "settingsView/csettingsview_moc.h"
# include "modManager/cmodlistview_moc.h"
2024-04-21 16:56:39 +02:00
# include "../lib/CConfigHandler.h"
2024-05-01 21:05:01 +03:00
# include <QObject>
# include <QScroller>
2025-01-15 20:30:48 +01:00
# ifdef VCMI_ANDROID
# include <QAndroidJniObject>
# include <QtAndroid>
# endif
2025-02-15 00:35:08 +01:00
# ifdef VCMI_IOS
# include "ios/revealdirectoryinfiles.h"
# endif
2024-06-08 15:56:20 +00:00
# ifdef VCMI_MOBILE
static QScrollerProperties generateScrollerProperties ( )
{
QScrollerProperties result ;
result . setScrollMetric ( QScrollerProperties : : OvershootDragResistanceFactor , 0.25 ) ;
result . setScrollMetric ( QScrollerProperties : : OvershootDragDistanceFactor , 0.25 ) ;
result . setScrollMetric ( QScrollerProperties : : HorizontalOvershootPolicy , QScrollerProperties : : OvershootAlwaysOff ) ;
return result ;
}
# endif
2024-05-01 21:05:01 +03:00
namespace Helper
{
void loadSettings ( )
2024-04-21 16:56:39 +02:00
{
settings . init ( " config/settings.json " , " vcmi:settings " ) ;
2024-06-20 21:33:45 +02:00
persistentStorage . init ( " config/persistentStorage.json " , " " ) ;
2024-04-21 16:56:39 +02:00
}
2024-05-01 21:05:01 +03:00
2025-03-17 21:58:31 +01:00
void reLoadSettings ( )
{
loadSettings ( ) ;
for ( const auto widget : qApp - > allWidgets ( ) )
if ( auto settingsView = qobject_cast < CSettingsView * > ( widget ) )
2025-03-23 13:09:03 +01:00
{
2025-03-17 21:58:31 +01:00
settingsView - > loadSettings ( ) ;
2025-03-23 13:09:03 +01:00
break ;
}
2025-03-17 21:58:31 +01:00
getMainWindow ( ) - > updateTranslation ( ) ;
getMainWindow ( ) - > getModView ( ) - > reload ( ) ;
}
2024-05-01 21:05:01 +03:00
void enableScrollBySwiping ( QObject * scrollTarget )
{
# ifdef VCMI_MOBILE
QScroller : : grabGesture ( scrollTarget , QScroller : : LeftMouseButtonGesture ) ;
2024-06-08 15:56:20 +00:00
QScroller * scroller = QScroller : : scroller ( scrollTarget ) ;
scroller - > setScrollerProperties ( generateScrollerProperties ( ) ) ;
2024-05-01 21:05:01 +03:00
# endif
}
2025-01-15 20:30:48 +01:00
QString getRealPath ( QString path )
{
# ifdef VCMI_ANDROID
if ( path . contains ( " content:// " , Qt : : CaseInsensitive ) )
{
auto str = QAndroidJniObject : : fromString ( path ) ;
return QAndroidJniObject : : callStaticObjectMethod ( " eu/vcmi/vcmi/util/FileUtil " , " getFilenameFromUri " , " (Ljava/lang/String;Landroid/content/Context;)Ljava/lang/String; " , str . object < jstring > ( ) , QtAndroid : : androidContext ( ) . object ( ) ) . toString ( ) ;
}
else
return path ;
# else
return path ;
# endif
}
void performNativeCopy ( QString src , QString dst )
{
# ifdef VCMI_ANDROID
2025-09-03 20:48:57 +02:00
// %-encode unencoded parts of string.
2025-09-06 15:36:07 +02:00
// This is needed because Qt returns a mixed content url with %-encoded and unencoded parts. On Android >= 13 this causes problems reading these files, when using spaces and unicode characters in folder or filename.
// Only these should be encoded (other typically %-encoded chars should not be encoded because this leads to errors).
2025-09-03 20:48:57 +02:00
// Related, but seems not completly fixed (at least in our setup): https://bugreports.qt.io/browse/QTBUG-114435
2025-08-31 02:52:34 +02:00
auto safeEncode = [ & ] ( QString uri ) - > QString
{
2025-08-31 11:22:44 +02:00
if ( ! uri . startsWith ( " content:// " , Qt : : CaseInsensitive ) )
2025-08-31 02:52:34 +02:00
return uri ;
2025-09-06 12:34:15 +02:00
return QString : : fromUtf8 ( QUrl : : toPercentEncoding ( uri , " !#$&'()*+,/:;=?@[]<>{} \" `^~% " ) ) ;
2025-08-31 02:52:34 +02:00
} ;
auto srcStr = QAndroidJniObject : : fromString ( safeEncode ( src ) ) ;
auto dstStr = QAndroidJniObject : : fromString ( safeEncode ( dst ) ) ;
2025-01-15 20:30:48 +01:00
QAndroidJniObject : : callStaticObjectMethod ( " eu/vcmi/vcmi/util/FileUtil " , " copyFileFromUri " , " (Ljava/lang/String;Ljava/lang/String;Landroid/content/Context;)V " , srcStr . object < jstring > ( ) , dstStr . object < jstring > ( ) , QtAndroid : : androidContext ( ) . object ( ) ) ;
# else
2025-01-17 09:24:09 +03:00
QFile : : copy ( src , dst ) ;
2025-01-15 20:30:48 +01:00
# endif
}
2025-02-15 00:35:08 +01:00
void revealDirectoryInFileBrowser ( QString path )
{
const auto dirUrl = QUrl : : fromLocalFile ( QFileInfo { path } . absoluteFilePath ( ) ) ;
# ifdef VCMI_IOS
iOS_utils : : revealDirectoryInFiles ( dirUrl ) ;
# else
QDesktopServices : : openUrl ( dirUrl ) ;
# endif
}
2025-03-17 21:58:31 +01:00
MainWindow * getMainWindow ( )
{
foreach ( QWidget * w , qApp - > allWidgets ( ) )
if ( auto mainWin = qobject_cast < MainWindow * > ( w ) )
return mainWin ;
return nullptr ;
}
2024-05-01 21:05:01 +03:00
}