diff --git a/src/plugins/miscellaneous/Core/i18n/Core_fr.ts b/src/plugins/miscellaneous/Core/i18n/Core_fr.ts index 4574208d54..ef78abf176 100644 --- a/src/plugins/miscellaneous/Core/i18n/Core_fr.ts +++ b/src/plugins/miscellaneous/Core/i18n/Core_fr.ts @@ -1,25 +1,6 @@ - - OpenCOR::Core - - Open File - Ouvrir Fichier - - - Open Remote File - Ouvrir Fichier À Distance - - - <strong>%1</strong> could not be opened. - <strong>%1</strong> n'a pas pu être ouvert. - - - '%1' could not be opened. - '%1' n'a pas pu être ouvert. - - OpenCOR::Core::CentralWidget @@ -62,18 +43,6 @@ Open Ouvrir - - '%1' could not be created. - '%1' n'a pas pu être créé. - - - <strong>%1</strong> could not be opened (%2). - <strong>%1</strong> n'a pas pu être ouvert (%2). - - - '%1' could not be opened (%2). - '%1' n'a pas pu être ouvert (%2). - File Modified Fichier Modifié @@ -545,5 +514,21 @@ Please choose an empty directory. Veuillez choisir un répertoire vide. + + Open File + Ouvrir Fichier + + + Open Remote File + Ouvrir Fichier À Distance + + + <strong>%1</strong> could not be opened. + <strong>%1</strong> n'a pas pu être ouvert. + + + <strong>%1</strong> could not be opened (%2). + <strong>%1</strong> n'a pas pu être ouvert (%2). + diff --git a/src/plugins/miscellaneous/Core/src/centralwidget.cpp b/src/plugins/miscellaneous/Core/src/centralwidget.cpp index ef0a973347..84b5df0683 100644 --- a/src/plugins/miscellaneous/Core/src/centralwidget.cpp +++ b/src/plugins/miscellaneous/Core/src/centralwidget.cpp @@ -782,84 +782,9 @@ void CentralWidget::openFile() //============================================================================== -QString CentralWidget::openRemoteFile(const QString &pUrl, bool pShowWarning) -{ - // Note: this method is used by the GUI and should be kept in sync with that - // of openRemoteFile() in - // src/plugins/miscellaneous/Core/src/corecliutils.cpp... - - // Make sure that pUrl really refers to a remote file - - bool isLocalFile; - QString fileNameOrUrl; - - checkFileNameOrUrl(pUrl, isLocalFile, fileNameOrUrl); - - if (isLocalFile) { - // It looks like the user tried to open a local file using a URL, e.g. - // file:///home/me/mymodel.cellml - // rather than a local file name, e.g. - // /home/me/mymodel.cellml - // so open the file as a local file and leave - - return openFile(fileNameOrUrl, File::Type::Local, QString(), pShowWarning); - } - - // Check whether the remote file is already opened and if so select it, - // otherwise retrieve its contents - - FileManager *fileManagerInstance = FileManager::instance(); - QString fileName = fileManagerInstance->fileName(fileNameOrUrl); - - if (fileName.isEmpty()) { - // The remote file isn't already opened, so download its contents - - QByteArray fileContents; - QString errorMessage; - - showBusyWidget(); - // Note: we don't subsequently hide our busy widget in case we are - // loading a SED-ML file / COMBINE archive. Indeed, such files may - // require further initialisation (in the case of the Simulation - // Experiment view, for example). So, instead, our busy widget - // will get hidden in updateGui()... - - if (readFile(fileNameOrUrl, fileContents, &errorMessage)) { - // We were able to retrieve the contents of the remote file, so ask - // our file manager to create a new remote file - - FileManager::Status status = fileManagerInstance->create(fileNameOrUrl, fileContents); - - // Make sure that the file has indeed been created - - if (status != FileManager::Status::Created) { -#ifdef QT_DEBUG - qFatal("FATAL ERROR | %s:%d: '%s' could not be created.", __FILE__, __LINE__, qPrintable(fileNameOrUrl)); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay, cppcoreguidelines-pro-type-vararg) -#else - return tr("'%1' could not be created.").arg(fileNameOrUrl); -#endif - } - - return {}; - } - - // We were not able to retrieve the contents of the remote file, so let - // the user know about it, after having hidden our busy widget - - hideBusyWidget(); - - if (pShowWarning) { - warningMessageBox(tr("Open Remote File"), - tr("%1 could not be opened (%2).").arg(fileNameOrUrl, - formatMessage(errorMessage))); - } - - return tr("'%1' could not be opened (%2).").arg(fileNameOrUrl, - formatMessage(errorMessage)); - } - - return openFile(fileName, File::Type::Remote, fileNameOrUrl, pShowWarning); -} +#define GUI_SUPPORT + #include "openremotefile.cpp.inl" +#undef GUI_SUPPORT //============================================================================== diff --git a/src/plugins/miscellaneous/Core/src/corecliutils.cpp b/src/plugins/miscellaneous/Core/src/corecliutils.cpp index e0888dffc8..9bf520a2aa 100644 --- a/src/plugins/miscellaneous/Core/src/corecliutils.cpp +++ b/src/plugins/miscellaneous/Core/src/corecliutils.cpp @@ -578,65 +578,7 @@ void doNothing(const quint64 *pMax, const bool *pStopped) //============================================================================== -QString openRemoteFile(const QString &pUrl) -{ - // Note: this method is used by our Python wrapper and should be kept in - // sync with that of CentralWidget::openRemoteFile() in - // src/plugins/miscellaneous/Core/src/centralwidget.cpp... - - // Make sure that pUrl really refers to a remote file - - bool isLocalFile; - QString fileNameOrUrl; - - checkFileNameOrUrl(pUrl, isLocalFile, fileNameOrUrl); - - if (isLocalFile) { - // It looks like the user tried to open a local file using a URL, e.g. - // file:///home/me/mymodel.cellml - // rather than a local file name, e.g. - // /home/me/mymodel.cellml - // so open the file as a local file and leave - - return openFile(fileNameOrUrl); - } - - // Check whether the remote file is already opened and if so select it, - // otherwise retrieve its contents - - FileManager *fileManagerInstance = FileManager::instance(); - QString fileName = fileManagerInstance->fileName(fileNameOrUrl); - - if (fileName.isEmpty()) { - // The remote file isn't already opened, so download its contents - - QByteArray fileContents; - QString errorMessage; - - if (readFile(fileNameOrUrl, fileContents, &errorMessage)) { - // We were able to retrieve the contents of the remote file, so save - // it locally - - fileName = Core::temporaryFileName(); - - if (!writeFile(fileName, fileContents)) { -#ifdef QT_DEBUG - qFatal("FATAL ERROR | %s:%d: '%s' could not be created.", __FILE__, __LINE__, qPrintable(fileNameOrUrl)); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay, cppcoreguidelines-pro-type-vararg) -#else - return QObject::tr("'%1' could not be created.").arg(fileNameOrUrl); -#endif - } - } else { - // We were not able to retrieve the contents of the remote file, so let - // the user know about it - - return QObject::tr("'%1' could not be opened (%2).").arg(fileNameOrUrl, - formatMessage(errorMessage)); - } - } - - return openFile(fileName, File::Type::Remote, fileNameOrUrl); -} +#include "openremotefile.cpp.inl" //============================================================================== diff --git a/src/plugins/miscellaneous/Core/src/openfile.cpp.inl b/src/plugins/miscellaneous/Core/src/openfile.cpp.inl index 7a2b6c6551..2d161ec2d5 100644 --- a/src/plugins/miscellaneous/Core/src/openfile.cpp.inl +++ b/src/plugins/miscellaneous/Core/src/openfile.cpp.inl @@ -16,16 +16,16 @@ QString openFile(const QString &pFileName, const File::Type &pType, if (pShowWarning && mainWindow()->isVisible()) { warningMessageBox(pUrl.isEmpty()? - tr("Open File"): - tr("Open Remote File"), - tr("%1 could not be opened.").arg(pUrl.isEmpty()? - QDir::toNativeSeparators(pFileName): - pFileName)); + QObject::tr("Open File"): + QObject::tr("Open Remote File"), + QObject::tr("%1 could not be opened.").arg(pUrl.isEmpty()? + QDir::toNativeSeparators(pFileName): + pFileName)); } - return tr("'%1' could not be opened.").arg(pUrl.isEmpty()? - QDir::toNativeSeparators(pFileName): - pFileName); + return QObject::tr("'%1' could not be opened.").arg(pUrl.isEmpty()? + QDir::toNativeSeparators(pFileName): + pFileName); } #endif diff --git a/src/plugins/miscellaneous/Core/src/openremotefile.cpp.inl b/src/plugins/miscellaneous/Core/src/openremotefile.cpp.inl new file mode 100644 index 0000000000..23cac50ff3 --- /dev/null +++ b/src/plugins/miscellaneous/Core/src/openremotefile.cpp.inl @@ -0,0 +1,111 @@ +#ifdef GUI_SUPPORT +QString CentralWidget::openRemoteFile(const QString &pUrl, bool pShowWarning) +#else +QString openRemoteFile(const QString &pUrl) +#endif +{ + // Make sure that pUrl really refers to a remote file + + bool isLocalFile; + QString fileNameOrUrl; + + checkFileNameOrUrl(pUrl, isLocalFile, fileNameOrUrl); + + if (isLocalFile) { + // It looks like the user tried to open a local file using a URL, e.g. + // file:///home/me/mymodel.cellml + // rather than a local file name, e.g. + // /home/me/mymodel.cellml + // so open the file as a local file and leave + +#ifdef GUI_SUPPORT + return openFile(fileNameOrUrl, File::Type::Local, QString(), pShowWarning); +#else + return openFile(fileNameOrUrl); +#endif + } + + // Check whether the remote file is already opened and if so select it, + // otherwise retrieve its contents + + FileManager *fileManagerInstance = FileManager::instance(); + QString fileName = fileManagerInstance->fileName(fileNameOrUrl); + + if (fileName.isEmpty()) { + // The remote file isn't already opened, so download its contents + + QByteArray fileContents; + QString errorMessage; + +#ifdef GUI_SUPPORT + showBusyWidget(); + // Note: we don't subsequently hide our busy widget in case we are + // loading a SED-ML file / COMBINE archive. Indeed, such files may + // require further initialisation (in the case of the Simulation + // Experiment view, for example). So, instead, our busy widget + // will get hidden in updateGui()... +#endif + + if (readFile(fileNameOrUrl, fileContents, &errorMessage)) { +#ifdef GUI_SUPPORT + // We were able to retrieve the contents of the remote file, so ask + // our file manager to create a new remote file + + FileManager::Status status = fileManagerInstance->create(fileNameOrUrl, fileContents); + + // Make sure that the file has indeed been created + + if (status != FileManager::Status::Created) { +#ifdef QT_DEBUG + qFatal("FATAL ERROR | %s:%d: '%s' could not be created.", __FILE__, __LINE__, qPrintable(fileNameOrUrl)); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay, cppcoreguidelines-pro-type-vararg) +#else + return QObject::tr("'%1' could not be created.").arg(fileNameOrUrl); +#endif + } + + return {}; +#else + // We were able to retrieve the contents of the remote file, so save + // it locally + + fileName = Core::temporaryFileName(); + + if (!writeFile(fileName, fileContents)) { +#ifdef QT_DEBUG + qFatal("FATAL ERROR | %s:%d: '%s' could not be created.", __FILE__, __LINE__, qPrintable(fileNameOrUrl)); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay, cppcoreguidelines-pro-type-vararg) +#else + return QObject::tr("'%1' could not be created.").arg(fileNameOrUrl); +#endif + } + } else { + // We were not able to retrieve the contents of the remote file, so let + // the user know about it + + return QObject::tr("'%1' could not be opened (%2).").arg(fileNameOrUrl, + formatMessage(errorMessage)); +#endif + } + + // We were not able to retrieve the contents of the remote file, so let + // the user know about it, after having hidden our busy widget + +#ifdef GUI_SUPPORT + hideBusyWidget(); + + if (pShowWarning) { + warningMessageBox(QObject::tr("Open Remote File"), + QObject::tr("%1 could not be opened (%2).").arg(fileNameOrUrl, + formatMessage(errorMessage))); + } + + return QObject::tr("'%1' could not be opened (%2).").arg(fileNameOrUrl, + formatMessage(errorMessage)); +#endif + } + +#ifdef GUI_SUPPORT + return openFile(fileName, File::Type::Remote, fileNameOrUrl, pShowWarning); +#else + return openFile(fileName, File::Type::Remote, fileNameOrUrl); +#endif +}