Skip to content

Commit

Permalink
11402 Save copy of project file to a backup database when saving proj…
Browse files Browse the repository at this point in the history
…ect file
  • Loading branch information
magnesj committed May 1, 2024
1 parent 3cd80f0 commit 4617bcc
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 11 deletions.
9 changes: 9 additions & 0 deletions ApplicationExeCode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ if(Qt5Core_FOUND)
Concurrent
PrintSupport
Svg
Sql
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
Expand All @@ -45,6 +46,7 @@ if(Qt5Core_FOUND)
Qt5::Concurrent
Qt5::PrintSupport
Qt5::Svg
Qt5::Sql
)
if(Qt5Charts_FOUND)
list(APPEND QT_LIBRARIES Qt5::Charts)
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion ApplicationLibCode/Application/RiaPreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -983,6 +986,14 @@ bool RiaPreferences::loggerTrapSignalAndFlush() const
return m_loggerTrapSignalAndFlush();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaPreferences::storeBackupOfProjectFiles() const
{
return m_storeBackupOfProjectFile();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions ApplicationLibCode/Application/RiaPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class RiaPreferences : public caf::PdmObject
QString loggerFilename() const;
int loggerFlushInterval() const;
bool loggerTrapSignalAndFlush() const;
bool storeBackupOfProjectFiles() const;

RiaPreferencesGeoMech* geoMechPreferences() const;
RiaPreferencesSummary* summaryPreferences() const;
Expand Down Expand Up @@ -212,6 +213,8 @@ class RiaPreferences : public caf::PdmObject
caf::PdmField<int> m_loggerFlushInterval;
caf::PdmField<bool> m_loggerTrapSignalAndFlush;

caf::PdmField<bool> m_storeBackupOfProjectFile;

// Surface Import
caf::PdmField<double> m_surfaceImportResamplingDistance;

Expand Down
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/Tools/CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
109 changes: 109 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
Original file line number Diff line number Diff line change
@@ -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 );
}
2 changes: 2 additions & 0 deletions ApplicationLibCode/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ if(Qt5Core_FOUND)
Concurrent
PrintSupport
Svg
Sql
OPTIONAL_COMPONENTS Charts
)
set(QT_LIBRARIES
Expand All @@ -49,6 +50,7 @@ if(Qt5Core_FOUND)
Qt5::Concurrent
Qt5::PrintSupport
Qt5::Svg
Qt5::Sql
)
if(Qt5Charts_FOUND)
list(APPEND QT_LIBRARIES Qt5::Charts)
Expand Down
18 changes: 16 additions & 2 deletions ApplicationLibCode/ProjectDataModel/RimProject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

//--------------------------------------------------------------------------------------------------
Expand Down
39 changes: 31 additions & 8 deletions Fwk/AppFwk/cafProjectDataModel/cafPdmDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions Fwk/AppFwk/cafProjectDataModel/cafPdmDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "cafPdmField.h"
#include "cafPdmObject.h"

class QXmlStreamWriter;

namespace caf
{
//==================================================================================================
Expand All @@ -57,13 +59,18 @@ class PdmDocument : public PdmObject
void readFile();
bool writeFile();

QString documentAsString();

void readFile( QIODevice* device );
void writeFile( QIODevice* device );

static void updateUiIconStateRecursively( PdmObjectHandle* root );

protected:
virtual void beforeInitAfterRead();

private:
void writeDocumentToXmlStream( QXmlStreamWriter& xmlStream );
};

} // End of namespace caf

0 comments on commit 4617bcc

Please sign in to comment.