Skip to content

Commit

Permalink
Python support: prevent duplicated code (opencor#2225).
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 26, 2019
1 parent d37e7b4 commit 38b9c7b
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 176 deletions.
47 changes: 16 additions & 31 deletions src/plugins/miscellaneous/Core/i18n/Core_fr.ts
@@ -1,25 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="fr_FR" sourcelanguage="en_GB">
<context>
<name>OpenCOR::Core</name>
<message>
<source>Open File</source>
<translation>Ouvrir Fichier</translation>
</message>
<message>
<source>Open Remote File</source>
<translation>Ouvrir Fichier À Distance</translation>
</message>
<message>
<source>&lt;strong&gt;%1&lt;/strong&gt; could not be opened.</source>
<translation>&lt;strong&gt;%1&lt;/strong&gt; n&apos;a pas pu être ouvert.</translation>
</message>
<message>
<source>&apos;%1&apos; could not be opened.</source>
<translation>&apos;%1&apos; n&apos;a pas pu être ouvert.</translation>
</message>
</context>
<context>
<name>OpenCOR::Core::CentralWidget</name>
<message>
Expand Down Expand Up @@ -62,18 +43,6 @@
<source>Open</source>
<translation>Ouvrir</translation>
</message>
<message>
<source>&apos;%1&apos; could not be created.</source>
<translation>&apos;%1&apos; n&apos;a pas pu être créé.</translation>
</message>
<message>
<source>&lt;strong&gt;%1&lt;/strong&gt; could not be opened (%2).</source>
<translation>&lt;strong&gt;%1&lt;/strong&gt; n&apos;a pas pu être ouvert (%2).</translation>
</message>
<message>
<source>&apos;%1&apos; could not be opened (%2).</source>
<translation>&apos;%1&apos; n&apos;a pas pu être ouvert (%2).</translation>
</message>
<message>
<source>File Modified</source>
<translation>Fichier Modifié</translation>
Expand Down Expand Up @@ -545,5 +514,21 @@
<source>Please choose an empty directory.</source>
<translation>Veuillez choisir un répertoire vide.</translation>
</message>
<message>
<source>Open File</source>
<translation>Ouvrir Fichier</translation>
</message>
<message>
<source>Open Remote File</source>
<translation>Ouvrir Fichier À Distance</translation>
</message>
<message>
<source>&lt;strong&gt;%1&lt;/strong&gt; could not be opened.</source>
<translation>&lt;strong&gt;%1&lt;/strong&gt; n&apos;a pas pu être ouvert.</translation>
</message>
<message>
<source>&lt;strong&gt;%1&lt;/strong&gt; could not be opened (%2).</source>
<translation>&lt;strong&gt;%1&lt;/strong&gt; n&apos;a pas pu être ouvert (%2).</translation>
</message>
</context>
</TS>
81 changes: 3 additions & 78 deletions src/plugins/miscellaneous/Core/src/centralwidget.cpp
Expand Up @@ -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("<strong>%1</strong> 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

//==============================================================================

Expand Down
60 changes: 1 addition & 59 deletions src/plugins/miscellaneous/Core/src/corecliutils.cpp
Expand Up @@ -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"

//==============================================================================

Expand Down
16 changes: 8 additions & 8 deletions src/plugins/miscellaneous/Core/src/openfile.cpp.inl
Expand Up @@ -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("<strong>%1</strong> could not be opened.").arg(pUrl.isEmpty()?
QDir::toNativeSeparators(pFileName):
pFileName));
QObject::tr("Open File"):
QObject::tr("Open Remote File"),
QObject::tr("<strong>%1</strong> 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

Expand Down
111 changes: 111 additions & 0 deletions 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("<strong>%1</strong> 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
}

0 comments on commit 38b9c7b

Please sign in to comment.