2013-09-10 16:45:01 +02:00
# include "adjusterwidget.hpp"
2022-06-08 23:25:50 +02:00
# include <filesystem>
2013-09-10 16:45:01 +02:00
2022-08-03 00:00:54 +02:00
# include <components/misc/strings/lower.hpp>
2019-02-23 16:55:49 +03:00
2013-09-10 16:45:01 +02:00
# include <QHBoxLayout>
# include <QLabel>
# include <QStyle>
CSVDoc : : AdjusterWidget : : AdjusterWidget ( QWidget * parent )
2013-10-27 20:21:19 -05:00
: QWidget ( parent ) , mValid ( false ) , mAction ( ContentAction_Undefined )
2013-09-10 16:45:01 +02:00
{
QHBoxLayout * layout = new QHBoxLayout ( this ) ;
mIcon = new QLabel ( this ) ;
layout - > addWidget ( mIcon , 0 ) ;
mMessage = new QLabel ( this ) ;
mMessage - > setWordWrap ( true ) ;
mMessage - > setSizePolicy ( QSizePolicy ( QSizePolicy : : Minimum , QSizePolicy : : Minimum ) ) ;
layout - > addWidget ( mMessage , 1 ) ;
setName ( " " , false ) ;
setLayout ( layout ) ;
}
2013-10-27 20:21:19 -05:00
void CSVDoc : : AdjusterWidget : : setAction ( ContentAction action )
{
mAction = action ;
}
2022-06-08 23:25:50 +02:00
void CSVDoc : : AdjusterWidget : : setLocalData ( const std : : filesystem : : path & localData )
2013-09-10 16:45:01 +02:00
{
mLocalData = localData ;
}
2022-06-08 23:25:50 +02:00
std : : filesystem : : path CSVDoc : : AdjusterWidget : : getPath ( ) const
2013-09-10 16:45:01 +02:00
{
if ( ! mValid )
throw std : : logic_error ( " invalid content file path " ) ;
return mResultPath ;
}
2013-09-22 23:52:53 -05:00
bool CSVDoc : : AdjusterWidget : : isValid ( ) const
{
return mValid ;
}
2013-10-31 18:12:13 -05:00
void CSVDoc : : AdjusterWidget : : setFilenameCheck ( bool doCheck )
{
mDoFilenameCheck = doCheck ;
}
2013-09-10 16:45:01 +02:00
void CSVDoc : : AdjusterWidget : : setName ( const QString & name , bool addon )
{
QString message ;
2013-10-31 18:12:13 -05:00
mValid = ( ! name . isEmpty ( ) ) ;
2015-08-06 12:58:52 +02:00
bool warning = false ;
2013-10-27 20:21:19 -05:00
2013-10-31 18:12:13 -05:00
if ( ! mValid )
2013-09-10 16:45:01 +02:00
{
message = " No name. " ;
}
else
{
2022-06-19 13:28:33 +02:00
std : : filesystem : : path path ( name . toUtf8 ( ) . data ( ) ) ; //TODO(Project579): Replace with char8_t in C++20
2013-09-10 16:45:01 +02:00
2022-06-19 13:28:33 +02:00
std : : string extension = Misc : : StringUtils : : lowerCase ( path . extension ( ) . string ( ) ) ; //TODO(Project579): let's hope unicode characters are never used in these extensions on windows or this will break
2015-02-16 14:27:25 +11:00
bool isLegacyPath = ( extension = = " .esm " | |
extension = = " .esp " ) ;
2013-10-31 18:12:13 -05:00
2022-06-19 13:28:33 +02:00
bool isFilePathChanged = ( path . parent_path ( ) ! = mLocalData ) ;
2013-11-03 00:02:46 -05:00
2013-10-31 18:12:13 -05:00
if ( isLegacyPath )
path . replace_extension ( addon ? " .omwaddon " : " .omwgame " ) ;
2013-09-10 16:45:01 +02:00
2013-10-31 18:12:13 -05:00
//if the file came from data-local and is not a legacy file to be converted,
//don't worry about doing a file check.
if ( ! isFilePathChanged & & ! isLegacyPath )
2013-09-10 16:45:01 +02:00
{
// path already points to the local data directory
2022-06-19 13:28:33 +02:00
message = QString : : fromUtf8 ( ( " Will be saved as: " + path . string ( ) ) . c_str ( ) ) ; //TODO(Project579): This is probably broken on windows with unicode paths
2013-09-10 16:45:01 +02:00
mResultPath = path ;
}
2013-10-31 18:12:13 -05:00
//in all other cases, ensure the path points to data-local and do an existing file check
2013-09-10 16:45:01 +02:00
else
{
// path points somewhere else or is a leaf name.
2013-10-31 18:12:13 -05:00
if ( isFilePathChanged )
path = mLocalData / path . filename ( ) ;
2013-09-10 16:45:01 +02:00
2022-06-19 13:28:33 +02:00
message = QString : : fromUtf8 ( ( " Will be saved as: " + path . string ( ) ) . c_str ( ) ) ; //TODO(Project579): This is probably broken on windows with unicode paths
2013-09-10 16:45:01 +02:00
mResultPath = path ;
2022-06-08 23:25:50 +02:00
if ( std : : filesystem : : exists ( path ) )
2013-09-10 16:45:01 +02:00
{
/// \todo add an user setting to make this an error.
2013-10-31 18:12:13 -05:00
message + = " <p>A file with the same name already exists. If you continue, it will be overwritten. " ;
2015-08-06 12:58:52 +02:00
warning = true ;
2013-09-10 16:45:01 +02:00
}
}
}
mMessage - > setText ( message ) ;
mIcon - > setPixmap ( style ( ) - > standardIcon (
2015-08-06 12:58:52 +02:00
mValid ? ( warning ? QStyle : : SP_MessageBoxWarning : QStyle : : SP_MessageBoxInformation ) : QStyle : : SP_MessageBoxCritical ) .
2013-09-10 16:45:01 +02:00
pixmap ( QSize ( 16 , 16 ) ) ) ;
emit stateChanged ( mValid ) ;
2013-09-22 23:52:53 -05:00
}