11402 Save copy of project file to a backup database when saving project file

This commit is contained in:
Magne Sjaastad 2024-05-01 09:46:22 +02:00 committed by GitHub
parent 3cd80f0871
commit 4617bcc673
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 220 additions and 11 deletions

View File

@ -33,6 +33,7 @@ if(Qt5Core_FOUND)
Concurrent
PrintSupport
Svg
Sql
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
@ -45,6 +46,7 @@ if(Qt5Core_FOUND)
Qt5::Concurrent
Qt5::PrintSupport
Qt5::Svg
Qt5::Sql
)
if(Qt5Charts_FOUND)
list(APPEND QT_LIBRARIES Qt5::Charts)
@ -484,6 +486,13 @@ if(RESINSIGHT_PRIVATE_INSTALL)
OPTIONAL
)
# Required sql driver
install(
FILES ${QT_PLUGIN_PATH}/sqldrivers/libqsqlite.so
DESTINATION ${RESINSIGHT_INSTALL_FOLDER}/sqldrivers/
OPTIONAL
)
install(FILES qt.conf DESTINATION ${RESINSIGHT_INSTALL_FOLDER}/)
endif(RESINSIGHT_QT5_BUNDLE_LIBRARIES)

View File

@ -140,6 +140,8 @@ RiaPreferences::RiaPreferences()
CAF_PDM_InitField( &m_loggerFlushInterval, "loggerFlushInterval", 500, "Logging Flush Interval [ms]" );
CAF_PDM_InitField( &m_loggerTrapSignalAndFlush, "loggerTrapSignalAndFlush", false, "Trap SIGNAL and Flush File Logs" );
CAF_PDM_InitField( &m_storeBackupOfProjectFile, "storeBackupOfProjectFile", true, "Store Backup of Project Files" );
CAF_PDM_InitField( &ssihubAddress, "ssihubAddress", QString( "http://" ), "SSIHUB Address" );
ssihubAddress.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::TOP );
@ -477,7 +479,8 @@ void RiaPreferences::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering&
otherGroup->add( &m_gridCalculationExpressionFolder );
otherGroup->add( &m_summaryCalculationExpressionFolder );
caf::PdmUiGroup* loggingGroup = uiOrdering.addNewGroup( "Logging" );
caf::PdmUiGroup* loggingGroup = uiOrdering.addNewGroup( "Logging and Backup" );
loggingGroup->add( &m_storeBackupOfProjectFile );
loggingGroup->add( &m_loggerFilename );
loggingGroup->add( &m_loggerFlushInterval );
loggingGroup->add( &m_loggerTrapSignalAndFlush );
@ -983,6 +986,14 @@ bool RiaPreferences::loggerTrapSignalAndFlush() const
return m_loggerTrapSignalAndFlush();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaPreferences::storeBackupOfProjectFiles() const
{
return m_storeBackupOfProjectFile();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -120,6 +120,7 @@ public:
QString loggerFilename() const;
int loggerFlushInterval() const;
bool loggerTrapSignalAndFlush() const;
bool storeBackupOfProjectFiles() const;
RiaPreferencesGeoMech* geoMechPreferences() const;
RiaPreferencesSummary* summaryPreferences() const;
@ -212,6 +213,8 @@ private:
caf::PdmField<int> m_loggerFlushInterval;
caf::PdmField<bool> m_loggerTrapSignalAndFlush;
caf::PdmField<bool> m_storeBackupOfProjectFile;
// Surface Import
caf::PdmField<double> m_surfaceImportResamplingDistance;

View File

@ -54,6 +54,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.h
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.h
${CMAKE_CURRENT_LIST_DIR}/RiaProjectBackupTools.h
)
set(SOURCE_GROUP_SOURCE_FILES
@ -105,6 +106,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RiaNumericalTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaRegressionTextTools.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaFileLogger.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaProjectBackupTools.cpp
)
list(APPEND CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})

View File

@ -0,0 +1,109 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaProjectBackupTools.h"
#include "RiaLogging.h"
#include <QDateTime>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>
namespace RiaProjectBackupTools
{
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool createTableIfNeeded()
{
QSqlQuery query;
if ( !query.exec( "CREATE TABLE IF NOT EXISTS file_versions ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"timestamp DATETIME,"
"content TEXT)" ) )
{
QString txt = "Error creating table:" + query.lastError().text();
RiaLogging::error( txt );
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool insertContent( const QString& content )
{
QSqlQuery query;
query.prepare( "INSERT INTO file_versions (timestamp, content) "
"VALUES (:timestamp, :content)" );
query.bindValue( ":timestamp", QDateTime::currentDateTime() );
query.bindValue( ":content", content );
if ( !query.exec() )
{
QString txt = "Error saving file content to database:" + query.lastError().text();
RiaLogging::error( txt );
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool appendTextToDatabase( const QString& databaseFilePath, const QString& content )
{
const QString databaseType = "QSQLITE";
if ( !QSqlDatabase::isDriverAvailable( databaseType ) )
{
RiaLogging::error( "sqlite database is not available." );
return false;
}
// Try to open the SQLITE database
QSqlDatabase db = QSqlDatabase::database();
if ( !db.isValid() || !db.open() )
{
RiaLogging::info( "Adding database" );
// Add the SQLITE database, and it it required to do this once per session. The database will be available during the lifetime of
// the application, and can be accessed using QSqlDatabase::database()
db = QSqlDatabase::addDatabase( databaseType );
}
if ( !db.open() )
{
QString txt = "Error opening database:" + db.lastError().text();
RiaLogging::error( txt );
return false;
}
// Set the file name for the database. The database will be created if it does not exist.
db.setDatabaseName( databaseFilePath );
if ( !createTableIfNeeded() ) return false;
if ( !insertContent( content ) ) return false;
return true;
}
} // namespace RiaProjectBackupTools

View File

@ -0,0 +1,29 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <QString>
//==================================================================================================
//
//==================================================================================================
namespace RiaProjectBackupTools
{
bool appendTextToDatabase( const QString& databaseFilePath, const QString& content );
}

View File

@ -37,6 +37,7 @@ if(Qt5Core_FOUND)
Concurrent
PrintSupport
Svg
Sql
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
@ -49,6 +50,7 @@ if(Qt5Core_FOUND)
Qt5::Concurrent
Qt5::PrintSupport
Qt5::Svg
Qt5::Sql
)
if(Qt5Charts_FOUND)
list(APPEND QT_LIBRARIES Qt5::Charts)

View File

@ -24,6 +24,8 @@
#include "RiaFieldHandleTools.h"
#include "RiaFilePathTools.h"
#include "RiaGuiApplication.h"
#include "RiaPreferences.h"
#include "RiaProjectBackupTools.h"
#include "RiaProjectFileVersionTools.h"
#include "RiaTextStringTools.h"
#include "RiaVersionInfo.h"
@ -405,10 +407,22 @@ RimMainPlotCollection* RimProject::mainPlotCollection() const
bool RimProject::writeProjectFile()
{
transferPathsToGlobalPathList();
bool couldOpenFile = writeFile();
QFile xmlFile( fileName );
if ( !xmlFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) return false;
QString content = documentAsString();
xmlFile.write( content.toUtf8() );
if ( RiaPreferences::current()->storeBackupOfProjectFiles() )
{
QString backupFilename = fileName + "db";
RiaProjectBackupTools::appendTextToDatabase( backupFilename, content );
}
distributePathsFromGlobalPathList();
return couldOpenFile;
return true;
}
//--------------------------------------------------------------------------------------------------

View File

@ -83,7 +83,7 @@ void PdmDocument::readFile( QIODevice* xmlFile )
}
}
// Ask all objects to initialize and set up internal datastructure and pointers
// Ask all objects to initialize and set up internal data structures and pointers
// after everything is read from file
resolveReferencesRecursively();
@ -113,16 +113,22 @@ void PdmDocument::writeFile( QIODevice* xmlFile )
setupBeforeSaveRecursively();
QXmlStreamWriter xmlStream( xmlFile );
xmlStream.setAutoFormatting( true );
writeDocumentToXmlStream( xmlStream );
}
xmlStream.writeStartDocument();
QString className = classKeyword();
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QString PdmDocument::documentAsString()
{
// Ask all objects to make them ready to write themselves to file
setupBeforeSaveRecursively();
xmlStream.writeStartElement( "", className );
writeFields( xmlStream );
xmlStream.writeEndElement();
QString content;
QXmlStreamWriter xmlStream( &content );
writeDocumentToXmlStream( xmlStream );
xmlStream.writeEndDocument();
return content;
}
//--------------------------------------------------------------------------------------------------
@ -162,4 +168,21 @@ void PdmDocument::beforeInitAfterRead()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmDocument::writeDocumentToXmlStream( QXmlStreamWriter& xmlStream )
{
xmlStream.setAutoFormatting( true );
xmlStream.writeStartDocument();
QString className = classKeyword();
xmlStream.writeStartElement( "", className );
writeFields( xmlStream );
xmlStream.writeEndElement();
xmlStream.writeEndDocument();
}
} // End of namespace caf

View File

@ -39,6 +39,8 @@
#include "cafPdmField.h"
#include "cafPdmObject.h"
class QXmlStreamWriter;
namespace caf
{
//==================================================================================================
@ -57,6 +59,8 @@ public:
void readFile();
bool writeFile();
QString documentAsString();
void readFile( QIODevice* device );
void writeFile( QIODevice* device );
@ -64,6 +68,9 @@ public:
protected:
virtual void beforeInitAfterRead();
private:
void writeDocumentToXmlStream( QXmlStreamWriter& xmlStream );
};
} // End of namespace caf