Fix missing session invalid error for user session & enhance NPPM_GETNBSESSIONFILES

New specs of NPPM_GETNBSESSIONFILES (which is retro-compatible):

======================
NPPM_GETNBSESSIONFILES

Retrieves the number of files to load in the session sessionFileName. sessionFileName should be a full path name of an xml file.

Parameters:

wParam [out]
BOOL* isValidXML, if this pointer is null, then this parameter will be ignored. TRUE if XML is valid, otherwise FALSE.

lParam [in]
const TCHAR * sessionFileName

Return value:
Returns 0 if sessionFileName is an empty string/NULL, or XML session file is corrupted/invalid, else the number of files.
======================

Other minor improvements:
- checking also for a possible ShellExecute errors
- removing the isAllSuccessful boolean, as it is no longer needed.

Fix #14228, close #14232
This commit is contained in:
xomx 2023-10-14 04:21:42 +02:00 committed by Don Ho
parent 08794510be
commit 72c5175b33
4 changed files with 33 additions and 26 deletions

View File

@ -1295,13 +1295,22 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
case NPPM_GETNBSESSIONFILES: case NPPM_GETNBSESSIONFILES:
{ {
const TCHAR *sessionFileName = reinterpret_cast<const TCHAR *>(lParam); size_t nbSessionFiles = 0;
if ((!sessionFileName) || (sessionFileName[0] == '\0')) const TCHAR* sessionFileName = reinterpret_cast<const TCHAR*>(lParam);
return 0; BOOL* pbIsValidXML = reinterpret_cast<BOOL*>(wParam);
Session session2Load; if (pbIsValidXML)
if (nppParam.loadSession(session2Load, sessionFileName)) *pbIsValidXML = false;
return session2Load.nbMainFiles() + session2Load.nbSubFiles(); if (sessionFileName && (sessionFileName[0] != '\0'))
return 0; {
Session session2Load;
if (nppParam.loadSession(session2Load, sessionFileName, true))
{
if (pbIsValidXML)
*pbIsValidXML = true;
nbSessionFiles = session2Load.nbMainFiles() + session2Load.nbSubFiles();
}
}
return nbSessionFiles;
} }
case NPPM_GETSESSIONFILES: case NPPM_GETSESSIONFILES:
@ -1313,7 +1322,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa
return FALSE; return FALSE;
Session session2Load; Session session2Load;
if (nppParam.loadSession(session2Load, sessionFileName)) if (nppParam.loadSession(session2Load, sessionFileName, true))
{ {
size_t i = 0; size_t i = 0;
for ( ; i < session2Load.nbMainFiles() ; ) for ( ; i < session2Load.nbMainFiles() ; )

View File

@ -2449,7 +2449,6 @@ bool Notepad_plus::fileLoadSession(const TCHAR *fn)
sessionFileName = fn; sessionFileName = fn;
} }
NppParameters& nppParam = NppParameters::getInstance(); NppParameters& nppParam = NppParameters::getInstance();
const NppGUI & nppGUI = nppParam.getNppGUI(); const NppGUI & nppGUI = nppParam.getNppGUI();
if (!sessionFileName.empty()) if (!sessionFileName.empty())
@ -2466,36 +2465,26 @@ bool Notepad_plus::fileLoadSession(const TCHAR *fn)
TCHAR nppFullPath[MAX_PATH]{}; TCHAR nppFullPath[MAX_PATH]{};
::GetModuleFileName(NULL, nppFullPath, MAX_PATH); ::GetModuleFileName(NULL, nppFullPath, MAX_PATH);
generic_string args = TEXT("-multiInst -nosession -openSession "); generic_string args = TEXT("-multiInst -nosession -openSession ");
args += TEXT("\""); args += TEXT("\"");
args += sessionFileName; args += sessionFileName;
args += TEXT("\""); args += TEXT("\"");
::ShellExecute(_pPublicInterface->getHSelf(), TEXT("open"), nppFullPath, args.c_str(), TEXT("."), SW_SHOW); if (::ShellExecute(_pPublicInterface->getHSelf(), TEXT("open"), nppFullPath, args.c_str(), TEXT("."), SW_SHOW) > (HINSTANCE)32)
result = true; result = true;
} }
else else
{ {
bool isAllSuccessful = true;
Session session2Load; Session session2Load;
if (nppParam.loadSession(session2Load, sessionFileName.c_str())) if (nppParam.loadSession(session2Load, sessionFileName.c_str()))
{ {
const bool isSnapshotMode = false; const bool isSnapshotMode = false;
isAllSuccessful = loadSession(session2Load, isSnapshotMode, sessionFileName.c_str()); result = loadSession(session2Load, isSnapshotMode, sessionFileName.c_str());
result = true;
if (isEmptyNpp && nppGUI._multiInstSetting == multiInstOnSession) if (isEmptyNpp && nppGUI._multiInstSetting == multiInstOnSession)
nppParam.setLoadedSessionFilePath(sessionFileName); nppParam.setLoadedSessionFilePath(sessionFileName);
} }
} }
if (result == false)
{
_nativeLangSpeaker.messageBox("SessionFileInvalidError",
NULL,
TEXT("Session file is either corrupted or not valid."),
TEXT("Could not Load Session"),
MB_OK);
}
} }
return result; return result;

View File

@ -2264,13 +2264,22 @@ void NppParameters::setWorkingDir(const TCHAR * newPath)
} }
} }
bool NppParameters::loadSession(Session & session, const TCHAR *sessionFileName) bool NppParameters::loadSession(Session& session, const TCHAR* sessionFileName, const bool bSuppressErrorMsg)
{ {
TiXmlDocument *pXmlSessionDocument = new TiXmlDocument(sessionFileName); TiXmlDocument* pXmlSessionDocument = new TiXmlDocument(sessionFileName);
bool loadOkay = pXmlSessionDocument->LoadFile(); bool loadOkay = pXmlSessionDocument->LoadFile();
if (loadOkay) if (loadOkay)
loadOkay = getSessionFromXmlTree(pXmlSessionDocument, session); loadOkay = getSessionFromXmlTree(pXmlSessionDocument, session);
if (!loadOkay && !bSuppressErrorMsg)
{
_pNativeLangSpeaker->messageBox("SessionFileInvalidError",
NULL,
TEXT("Session file is either corrupted or not valid."),
TEXT("Could not Load Session"),
MB_OK);
}
delete pXmlSessionDocument; delete pXmlSessionDocument;
return loadOkay; return loadOkay;
} }

View File

@ -1659,7 +1659,7 @@ public:
return _doPrintAndExit; return _doPrintAndExit;
}; };
bool loadSession(Session & session, const TCHAR *sessionFileName); bool loadSession(Session& session, const TCHAR* sessionFileName, const bool bSuppressErrorMsg = false);
void setLoadedSessionFilePath(const std::wstring & loadedSessionFilePath) { void setLoadedSessionFilePath(const std::wstring & loadedSessionFilePath) {
_loadedSessionFullFilePath = loadedSessionFilePath; _loadedSessionFullFilePath = loadedSessionFilePath;