From ece5de2a1b55f056020b6c2fea7a2397e2cac82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 28 Dec 2023 21:44:10 -0300 Subject: [PATCH] ecode: openURI check if scheme has been provided. ecode: Add portable mode. --- src/eepp/window/engine.cpp | 4 +++ src/tools/ecode/ecode.cpp | 52 +++++++++++++++++++++++++++++++-- src/tools/ecode/ecode.hpp | 6 +++- src/tools/uieditor/uieditor.cpp | 2 +- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/eepp/window/engine.cpp b/src/eepp/window/engine.cpp index b8ca5af5f..356743def 100644 --- a/src/eepp/window/engine.cpp +++ b/src/eepp/window/engine.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -395,6 +396,9 @@ bool Engine::openURI( const std::string& url ) { if ( nullptr == getPlatformHelper() ) return false; + if ( !LuaPattern::matches( url, "^%w+://" ) ) + return openURI( "file://" + url ); + if ( String::startsWith( url, "file://" ) ) { std::string absolutePath( FileSystem::getCurrentWorkingDirectory() ); FileSystem::dirAddSlashAtEnd( absolutePath ); diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 80ab97c11..89fb3547f 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -442,9 +442,40 @@ void App::initPluginManager() { mPluginManager->registerPlugin( GitPlugin::Definition() ); } +std::pair App::generateConfigPath() { + if ( mPortableMode ) { + std::string path( Sys::getProcessPath() + "config" ); + FileSystem::dirAddSlashAtEnd( path ); + bool fileExists = FileSystem::fileExists( path ); + + if ( fileExists && !FileSystem::isDirectory( path ) ) { + if ( FileSystem::fileRemove( path ) ) + fileExists = false; + else + return { false, Sys::getConfigPath( "ecode" ) }; + } + + if ( !fileExists && !FileSystem::makeDir( path ) ) { + path = Sys::getTempPath(); + path += "ecode_portable"; + path += FileSystem::getOSSlash(); + if ( !FileSystem::fileExists( path ) && !FileSystem::makeDir( path ) ) + return { false, Sys::getConfigPath( "ecode" ) }; + } + + return { true, path }; + } + + return { true, Sys::getConfigPath( "ecode" ) }; +} + bool App::loadConfig( const LogLevel& logLevel, const Sizeu& displaySize, bool sync, bool stdOutLogs, bool disableFileLogs ) { - mConfigPath = Sys::getConfigPath( "ecode" ); + if ( !mPortableMode ) + mPortableMode = FileSystem::fileExists( Sys::getProcessPath() + "portable_mode.txt" ); + auto [ok, configPath] = generateConfigPath(); + mConfigPath = std::move( configPath ); + mPortableModeFailed = !ok; bool firstRun = false; if ( !FileSystem::fileExists( mConfigPath ) ) { FileSystem::makeDir( mConfigPath ); @@ -555,6 +586,16 @@ void App::onReady() { // to run. mPluginManager->setPluginReloadEnabled( true ); Log::info( "App Ready" ); + + if ( mPortableModeFailed ) { + UIMessageBox* msg = UIMessageBox::New( + UIMessageBox::OK, + i18n( "portable_mode_failed", "Portable Mode failed.\nPlease check that the " + "application directory has write permissions." ) ); + msg->setTitle( "Error" ); + msg->setCloseShortcut( { KEY_ESCAPE, 0 } ); + msg->showWhenReady(); + } } void App::mainLoop() { @@ -3203,9 +3244,10 @@ void App::init( const LogLevel& logLevel, std::string file, const Float& pidelDe const std::string& colorScheme, bool terminal, bool frameBuffer, bool benchmarkMode, const std::string& css, bool health, const std::string& healthLang, FeaturesHealth::OutputFormat healthFormat, const std::string& fileToOpen, - bool stdOutLogs, bool disableFileLogs, bool openClean ) { + bool stdOutLogs, bool disableFileLogs, bool openClean, bool portable ) { DisplayManager* displayManager = Engine::instance()->getDisplayManager(); Display* currentDisplay = displayManager->getDisplayIndex( 0 ); + mPortableMode = portable; mDisplayDPI = currentDisplay->getDPI(); mUseFrameBuffer = frameBuffer; mBenchmarkMode = benchmarkMode; @@ -3711,6 +3753,10 @@ EE_MAIN_FUNC int main( int argc, char* argv[] ) { { "benchmark-mode" } ); args::Flag verbose( parser, "verbose", "Redirects all logs to stdout.", { 'v', "verbose" } ); args::Flag version( parser, "version", "Prints version information", { 'V', "version" } ); + args::Flag portable( + parser, "portable", + "Portable Mode (it will save the configuration files within the ecode main folder)", + { 'p', "portable" } ); args::ValueFlag jobs( parser, "jobs", "Sets the number of background jobs that the application will spawn " @@ -3814,7 +3860,7 @@ EE_MAIN_FUNC int main( int argc, char* argv[] ) { prefersColorScheme ? prefersColorScheme.Get() : "", terminal.Get(), fb.Get(), benchmarkMode.Get(), css.Get(), health || healthLang, healthLang.Get(), healthFormat.Get(), file.Get(), verbose.Get(), disableFileLogs.Get(), - openClean.Get() ); + openClean.Get(), portable.Get() ); eeSAFE_DELETE( appInstance ); Engine::destroySingleton(); diff --git a/src/tools/ecode/ecode.hpp b/src/tools/ecode/ecode.hpp index 92fbe7fa1..287c4516f 100644 --- a/src/tools/ecode/ecode.hpp +++ b/src/tools/ecode/ecode.hpp @@ -39,7 +39,7 @@ class App : public UICodeEditorSplitter::Client { const std::string& colorScheme, bool terminal, bool frameBuffer, bool benchmarkMode, const std::string& css, bool health, const std::string& healthLang, ecode::FeaturesHealth::OutputFormat healthFormat, const std::string& fileToOpen, - bool stdOutLogs, bool disableFileLogs, bool openClean ); + bool stdOutLogs, bool disableFileLogs, bool openClean, bool portable ); void createWidgetInspector(); @@ -430,6 +430,8 @@ class App : public UICodeEditorSplitter::Client { void saveProject(); + std::pair generateConfigPath(); + protected: std::vector mArgs; EE::Window::Window* mWindow{ nullptr }; @@ -476,6 +478,8 @@ class App : public UICodeEditorSplitter::Client { bool mDirTreeReady{ false }; bool mUseFrameBuffer{ false }; bool mBenchmarkMode{ false }; + bool mPortableMode{ false }; + bool mPortableModeFailed{ false }; Time mFrameTime{ Time::Zero }; Clock mLastRender; Clock mSecondsCounter; diff --git a/src/tools/uieditor/uieditor.cpp b/src/tools/uieditor/uieditor.cpp index c5def392b..4048801f8 100644 --- a/src/tools/uieditor/uieditor.cpp +++ b/src/tools/uieditor/uieditor.cpp @@ -1370,7 +1370,7 @@ void App::init( const Float& pixelDensityConf, const bool& useAppTheme, const st createAppMenu(); - mConfigPath = Sys::getConfigPath( "ecode" ); + mConfigPath = Sys::getConfigPath( "eepp-uieditor" ); mColorSchemesPath = mConfigPath + "colorschemes"; auto colorSchemes( SyntaxColorScheme::loadFromFile( mResPath + "colorschemes/colorschemes.conf" ) );