Skip to content

Commit

Permalink
Save temporary files under temp/ManiVault/<session_id>/
Browse files Browse the repository at this point in the history
Create temporary /temp/ManiVault/<session_id>/ directory at application startup
Create ManiVault TemporaryDir class which refers to the correct application session based temorary directory
Remove comments from serializable class
  • Loading branch information
ThomasKroes committed Dec 1, 2023
1 parent 9946c5c commit 7ace407
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 49 deletions.
2 changes: 2 additions & 0 deletions HDPS/cmake/CMakeMvSourcesPublic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ set(PUBLIC_UTIL_HEADERS
src/util/Version.h
src/util/DockWidgetPermission.h
src/util/NumericalRange.h
src/util/TemporaryDir.h
)

if(APPLE)
Expand Down Expand Up @@ -494,6 +495,7 @@ set(PUBLIC_UTIL_SOURCES
src/util/Version.cpp
src/util/DockWidgetPermission.cpp
src/util/NumericalRange.cpp
src/util/TemporaryDir.cpp
)

if(APPLE)
Expand Down
5 changes: 3 additions & 2 deletions HDPS/src/AbstractProjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include "actions/RecentFilesAction.h"
#include "actions/ToggleAction.h"

#include "util/TemporaryDir.h"

#include <QObject>
#include <QMenu>
#include <QTemporaryDir>

namespace mv {

Expand Down Expand Up @@ -160,7 +161,7 @@ class AbstractProjectManager : public AbstractManager
* @param filePath Relative file path of the file that needs to be extracted
* @return File path of the extracted file, empty string if extraction failed
*/
virtual QString extractFileFromManiVaultProject(const QString& maniVaultFilePath, QTemporaryDir& temporaryDir, const QString& filePath) = 0;
virtual QString extractFileFromManiVaultProject(const QString& maniVaultFilePath, mv::util::TemporaryDir& temporaryDir, const QString& filePath) = 0;

/**
* Get preview image of the project workspace
Expand Down
14 changes: 14 additions & 0 deletions HDPS/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <QDebug>
#include <QMessageBox>
#include <QMainWindow>
#include <QUuid>
#include <QDir>

using namespace mv::gui;
using namespace mv::util;
Expand All @@ -27,6 +29,7 @@ namespace mv {

Application::Application(int& argc, char** argv) :
QApplication(argc, argv),
_id(QUuid::createUuid().toString(QUuid::WithoutBraces)),
_core(nullptr),
_version({ 0, 9 }),
_iconFonts(),
Expand All @@ -38,6 +41,12 @@ Application::Application(int& argc, char** argv) :
_startupProjectMetaAction(nullptr),
_startupTask(nullptr)
{
const auto tempDirectory = QDir::cleanPath(QDir::tempPath() + QDir::separator() + "ManiVault" + QDir::separator() + _id);

QDir dir(tempDirectory);

dir.mkdir(tempDirectory);

_iconFonts.add(QSharedPointer<IconFont>(new FontAwesome(5, 14, {
//":/IconFonts/FontAwesomeBrandsRegular-5.14.otf",
//":/IconFonts/FontAwesomeRegular-5.14.otf",
Expand Down Expand Up @@ -205,4 +214,9 @@ ApplicationStartupTask& Application::getStartupTask()
return *_startupTask;
}

QString Application::getId() const
{
return _id;
}

}
4 changes: 4 additions & 0 deletions HDPS/src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class Application final : public QApplication

public: // Miscellaneous

/** Get the globally unique identifier of the application instance */
QString getId() const;

/** Returns a pointer to the current HDPS application (if the current application derives from mv::Application) */
static Application* current();

Expand Down Expand Up @@ -200,6 +203,7 @@ class Application final : public QApplication
void mainWindowInitialized();

protected:
QString _id; /** Globally unique identifier of the application instance */
CoreInterface* _core; /** Pointer to HDPS core */
const util::Version _version; /** Application version */
IconFonts _iconFonts; /** Icon fonts resource */
Expand Down
5 changes: 3 additions & 2 deletions HDPS/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "private/Archiver.h"
#include "private/Core.h"

#include "util/TemporaryDir.h"

#include <Application.h>
#include <ProjectMetaAction.h>

Expand All @@ -17,7 +19,6 @@
#include <QProxyStyle>
#include <QQuickWindow>
#include <QCommandLineParser>
#include <QTemporaryDir>

using namespace mv;
using namespace mv::util;
Expand All @@ -40,7 +41,7 @@ class NoFocusProxyStyle : public QProxyStyle {
ProjectMetaAction* getStartupProjectMetaAction(const QString& startupProjectFilePath)
{
try {
QTemporaryDir temporaryDir;
TemporaryDir temporaryDir;

const QString metaJsonFilePath("meta.json");

Expand Down
5 changes: 2 additions & 3 deletions HDPS/src/Project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include "Set.h"

#include "util/Serialization.h"

#include <QTemporaryDir>
#include "util/TemporaryDir.h"

using namespace mv::gui;
using namespace mv::util;
Expand Down Expand Up @@ -187,7 +186,7 @@ ProjectMetaAction& Project::getProjectMetaAction()

QSharedPointer<ProjectMetaAction> Project::getProjectMetaActionFromProjectFilePath(const QString& projectFilePath)
{
QTemporaryDir temporaryDir;
TemporaryDir temporaryDir;

const auto projectMetaJsonFilePath = projects().extractFileFromManiVaultProject(projectFilePath, temporaryDir, "meta.json");

Expand Down
11 changes: 5 additions & 6 deletions HDPS/src/private/ProjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <QStandardPaths>
#include <QGridLayout>
#include <QEventLoop>
#include <QTemporaryDir>

#include <exception>

Expand Down Expand Up @@ -338,7 +337,7 @@ void ProjectManager::openProject(QString filePath /*= ""*/, bool importDataOnly
if (QFileInfo(filePath).isDir())
throw std::runtime_error("Project file path may not be a directory");

QTemporaryDir temporaryDirectory;
TemporaryDir temporaryDirectory;

const auto temporaryDirectoryPath = temporaryDirectory.path();

Expand Down Expand Up @@ -547,7 +546,7 @@ void ProjectManager::saveProject(QString filePath /*= ""*/, const QString& passw
if (QFileInfo(filePath).isDir())
throw std::runtime_error("Project file path may not be a directory");

QTemporaryDir temporaryDirectory;
TemporaryDir temporaryDirectory;

const auto temporaryDirectoryPath = temporaryDirectory.path();

Expand Down Expand Up @@ -742,7 +741,7 @@ void ProjectManager::publishProject(QString filePath /*= ""*/)
if (QFileInfo(filePath).isDir())
throw std::runtime_error("Project file path may not be a directory");

QTemporaryDir temporaryDirectory;
TemporaryDir temporaryDirectory;

const auto temporaryDirectoryPath = temporaryDirectory.path();

Expand Down Expand Up @@ -899,7 +898,7 @@ mv::Project* ProjectManager::getCurrentProject()
return _project.get();
}

QString ProjectManager::extractFileFromManiVaultProject(const QString& maniVaultFilePath, QTemporaryDir& temporaryDir, const QString& filePath)
QString ProjectManager::extractFileFromManiVaultProject(const QString& maniVaultFilePath, TemporaryDir& temporaryDir, const QString& filePath)
{
const auto temporaryDirectoryPath = temporaryDir.path();

Expand Down Expand Up @@ -933,7 +932,7 @@ QImage ProjectManager::getWorkspacePreview(const QString& projectFilePath, const
{
try
{
QTemporaryDir temporaryDir;
TemporaryDir temporaryDir;

const auto workspacePreviewFilePath = projects().extractFileFromManiVaultProject(projectFilePath, temporaryDir, "workspace.jpg");

Expand Down
3 changes: 1 addition & 2 deletions HDPS/src/private/ProjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <QObject>
#include <QPointer>
#include <QTemporaryDir>

/**
* Project manager class
Expand Down Expand Up @@ -106,7 +105,7 @@ class ProjectManager final : public mv::AbstractProjectManager
* @param filePath Relative file path of the file that needs to be extracted
* @return File path of the extracted file, empty string if extraction failed
*/
QString extractFileFromManiVaultProject(const QString& maniVaultFilePath, QTemporaryDir& temporaryDir, const QString& filePath) override;
QString extractFileFromManiVaultProject(const QString& maniVaultFilePath, mv::util::TemporaryDir& temporaryDir, const QString& filePath) override;

/**
* Get preview image of the project workspace
Expand Down
13 changes: 6 additions & 7 deletions HDPS/src/private/WorkspaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
#include "Archiver.h"
#include "WorkspaceSettingsDialog.h"

#include <Application.h>
#include <CoreInterface.h>
#include "Application.h"
#include "CoreInterface.h"

#include <actions/StringsAction.h>
#include "actions/StringsAction.h"

#include <util/Serialization.h>
#include <util/Icon.h>
#include "util/Serialization.h"
#include "util/Icon.h"

#include <QMainWindow>
#include <QToolButton>
#include <QPainter>
#include <QFileDialog>
#include <QStandardPaths>
#include <QTemporaryDir>
#include <QBuffer>
#include <QOpenGLWidget>
#include <QEventLoop>
Expand Down Expand Up @@ -344,7 +343,7 @@ void WorkspaceManager::loadWorkspace(QString filePath /*= ""*/, bool addToRecent

void WorkspaceManager::importWorkspaceFromProjectFile(QString projectFilePath /*= ""*/, bool addToRecentWorkspaces /*= true*/)
{
QTemporaryDir temporaryDirectory;
TemporaryDir temporaryDirectory;

const auto temporaryDirectoryPath = temporaryDirectory.path();

Expand Down
27 changes: 0 additions & 27 deletions HDPS/src/util/Serializable.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,32 +118,6 @@ class Serializable
*/
static QString createId();

//public: // State
//
// /**
// * Get the state of the serializable object (the state is not automatically set)
// * @return State of the serializable object
// */
// virtual State getState() const final;
//
// /**
// * Set the state of the serializable object to \p state
// * @param state State of the serializable object
// */
// virtual void setState(const State& state) final;
//
// /**
// * Get whether the serializable object is currently being read
// * @return Whether the serializable object is currently being read
// */
// virtual bool isReading() const final;
//
// /**
// * Get whether the serializable object is currently being written
// * @return Whether the serializable object is currently being written
// */
// virtual bool isWriting() const final;

protected: // Serialization

/**
Expand Down Expand Up @@ -204,7 +178,6 @@ class Serializable
private:
QString _id; /** Globally unique identifier of the serializable object */
QString _serializationName; /** Serialization name */
// State _state; /** Determines the state of the serializable object (the state is not automatically set) */
};

}
Expand Down
18 changes: 18 additions & 0 deletions HDPS/src/util/TemporaryDir.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// A corresponding LICENSE file is located in the root directory of this source tree
// Copyright (C) 2023 BioVault (Biomedical Visual Analytics Unit LUMC - TU Delft)

#include "TemporaryDir.h"
#include "Application.h"

#include <QDir>

namespace mv::util {

TemporaryDir::TemporaryDir() :
QTemporaryDir(QDir::cleanPath(QDir::tempPath() + QDir::separator() + "ManiVault" + QDir::separator() + Application::current()->getId()))
{
qDebug() << QDir::cleanPath(QDir::tempPath() + QDir::separator() + "ManiVault" + QDir::separator() + Application::current()->getId());
}

}
32 changes: 32 additions & 0 deletions HDPS/src/util/TemporaryDir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// A corresponding LICENSE file is located in the root directory of this source tree
// Copyright (C) 2023 BioVault (Biomedical Visual Analytics Unit LUMC - TU Delft)

#pragma once

#include <QTemporaryDir>

namespace mv::util {

/**
* Temporary directory class
*
* Wrapper around QTemporaryDir to ensure that all ManiVault temporary data will reside somewhere in /temp/ManiVault
*
* @author Thomas Kroes
*/
class TemporaryDir final : public QTemporaryDir
{
public:

/**
* Construct temporary dir
* Constructs the QTemporaryDir with a temp/ManiVault template path
*/
TemporaryDir();

/** Do not allow users of the temporary dir to construct a temporary dir with their own template path */
TemporaryDir(const QString& templatePath) = delete;
};

}

0 comments on commit 7ace407

Please sign in to comment.