Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "RiaLogging.h"

#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QVariant>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
Expand Down Expand Up @@ -137,4 +141,96 @@ bool appendTextToDatabase( const QString& databaseFilePath, int maximumRecordCou
return true;
}

//--------------------------------------------------------------------------------------------------
/// Restore all previous revisions of a project file stored in the backup database to the destination folder.
/// Each revision is written to a separate file named by its timestamp.
//--------------------------------------------------------------------------------------------------
bool extractVersionsToFolder( const QString& databaseFilePath, const QString& destinationFolder, int* numberOfFilesWritten, QString* errorMessage )
{
if ( numberOfFilesWritten ) *numberOfFilesWritten = 0;

auto setError = [errorMessage]( const QString& message )
{
if ( errorMessage ) *errorMessage = message;
RiaLogging::error( message.toStdString() );
};

if ( !QFile::exists( databaseFilePath ) )
{
setError( "Backup database file does not exist: " + databaseFilePath );
return false;
}

QDir dir;
if ( !dir.mkpath( destinationFolder ) )
{
setError( "Not able to create destination folder: " + destinationFolder );
return false;
}

const QString databaseType = "QSQLITE";
if ( !QSqlDatabase::isDriverAvailable( databaseType ) )
{
setError( "sqlite database driver is not available." );
return false;
}

// Use a dedicated named connection to avoid interfering with the default connection used when writing backups.
const QString connectionName = "RiaProjectBackupToolsRestoreConnection";

bool success = true;
{
QSqlDatabase db = QSqlDatabase::addDatabase( databaseType, connectionName );
db.setDatabaseName( databaseFilePath );

if ( !db.open() )
{
setError( "Error opening backup database: " + db.lastError().text() );
QSqlDatabase::removeDatabase( connectionName );
return false;
}

QSqlQuery query( db );
query.prepare( "SELECT timestamp, content FROM file_versions ORDER BY timestamp ASC" );
if ( !query.exec() )
{
setError( "Error retrieving content from backup database: " + query.lastError().text() );
success = false;
}
else
{
const QString dateFormat = "yyyy-MM-dd_hh-mm-ss";
int fileCount = 0;

while ( query.next() )
{
QDateTime timestamp = query.value( 0 ).toDateTime();
QString projectContent = query.value( 1 ).toString();

QString filename = destinationFolder + "/" + timestamp.toString( dateFormat ) + ".rsp";
QFile file( filename );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
setError( "Error opening file for writing: " + filename );
success = false;
break;
}

QTextStream out( &file );
out << projectContent;
file.close();

fileCount++;
}

if ( numberOfFilesWritten ) *numberOfFilesWritten = fileCount;
}

db.close();
}
QSqlDatabase::removeDatabase( connectionName );

return success;
}

} // namespace RiaProjectBackupTools
5 changes: 5 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaProjectBackupTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@
namespace RiaProjectBackupTools
{
bool appendTextToDatabase( const QString& databaseFilePath, int maximumRecordCount, const QString& content );

bool extractVersionsToFolder( const QString& databaseFilePath,
const QString& destinationFolder,
int* numberOfFilesWritten = nullptr,
QString* errorMessage = nullptr );
} // namespace RiaProjectBackupTools
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicOpenInTextEditorFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicShowMemoryReportFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicSumoDataFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicRestoreProjectVersionsUi.cpp
${CMAKE_CURRENT_LIST_DIR}/RicRestoreProjectVersionsFeature.cpp
)

list(APPEND COMMAND_CODE_SOURCE_FILES ${SOURCE_GROUP_SOURCE_FILES})
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026- 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 "RicRestoreProjectVersionsFeature.h"
#include "RicRestoreProjectVersionsUi.h"

#include "RiaApplication.h"
#include "RiaProjectBackupTools.h"

#include "RimProject.h"

#include "RiuMainWindow.h"
#include "RiuMessageDialog.h"
#include "RiuRecentFileActionProvider.h"

#include "cafPdmUiPropertyViewDialog.h"

#include <QAction>
#include <QFile>
#include <QFileInfo>
#include <QIcon>
#include <QMessageBox>

CAF_CMD_SOURCE_INIT( RicRestoreProjectVersionsFeature, "RicRestoreProjectVersionsFeature" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicRestoreProjectVersionsFeature::onActionTriggered( bool isChecked )
{
RicRestoreProjectVersionsUi featureUi;

RimProject* project = RiaApplication::instance()->project();

QString projectFileName = ( project ) ? project->fileName() : QString();
if ( projectFileName.isEmpty() )
{
projectFileName = RiuRecentFileActionProvider::lastUsedProjectFileName();
}

if ( !projectFileName.isEmpty() )
{
featureUi.setProjectFile( projectFileName );
featureUi.setExportFolder( QFileInfo( projectFileName ).absolutePath() );
}

caf::PdmUiPropertyViewDialog propertyDialog( RiuMainWindow::instance(),
&featureUi,
"Restore Previous Project Versions",
"",
QDialogButtonBox::Ok | QDialogButtonBox::Cancel );
propertyDialog.resize( QSize( 560, 260 ) );

if ( propertyDialog.exec() != QDialog::Accepted ) return;

QString projectFile = featureUi.projectFile();
QString exportFolder = featureUi.exportFolder();
if ( projectFile.isEmpty() || exportFolder.isEmpty() ) return;

QString databaseFilePath = projectFile + "db";
if ( !QFile::exists( databaseFilePath ) )
{
RiuMessageDialog::showError( RiuMainWindow::instance(),
"Restore Previous Project Versions",
"No backup database was found for the selected project file.\n\n" + databaseFilePath );
return;
}

int numberOfFilesWritten = 0;
QString errorMessage;
if ( !RiaProjectBackupTools::extractVersionsToFolder( databaseFilePath, exportFolder, &numberOfFilesWritten, &errorMessage ) )
{
RiuMessageDialog::showError( RiuMainWindow::instance(), "Restore Previous Project Versions", errorMessage );
return;
}

QMessageBox::information( RiuMainWindow::instance(),
"Restore Previous Project Versions",
QString( "Restored %1 project version(s) to:\n%2" ).arg( numberOfFilesWritten ).arg( exportFolder ) );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicRestoreProjectVersionsFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Restore Previous Project Versions..." );
actionToSetup->setIcon( QIcon( ":/OpenFolder.svg" ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026- 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 "cafCmdFeature.h"

//==================================================================================================
///
//==================================================================================================
class RicRestoreProjectVersionsFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;

protected:
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};
Loading