fxos/shell/session.h

137 lines
3.9 KiB
C++

//---
// fxos-shell.session: All the data used in an interactive session
//---
#ifndef FXOS_SESSION_H
#define FXOS_SESSION_H
#include "legacy.h"
#include <fxos/vspace.h>
#include <fxos/project.h>
#include <map>
#include <memory>
#include <string>
#include <filesystem>
using namespace FxOS;
namespace fs = std::filesystem;
struct Session
{
/* Empty session with an empty project. */
Session();
/* Load a configuration file (no error if it doesn't exist). */
void loadConfig(fs::path const &configFile);
/* Save the session info into the configuration file, if one was loaded.
This does not save projects, see Project::save() or the ps command. */
void saveConfig() const;
//=== Environment ===//
/* Search path, folders from FXOS_LIBRARY essentially */
std::vector<fs::path> path;
/* Find file by name by searching through the path */
fs::path file(std::string name);
//=== Projects ===//
/* Whether there is currently a project loaded. This can only be false
during startup and while switching: it is an application invariant that
there is always exactly one project open (even if temporary/unsaved). */
bool hasProject()
{
return m_project.get() != nullptr;
}
/* Get the current project. An exception is raised if there's none. */
Project &project()
{
if(!hasProject())
throw std::runtime_error("No current project! o(x_x)o");
return *m_project.get();
}
/* Get the current binary name within the current project. This can be an
empty string if the project has no binaries. */
std::string const &currentBinaryName() const
{
return m_currentBinaryName;
}
/* Get the current binary within the current project. This can be null if
the project has no binaries. */
Binary *currentBinary();
Binary const *currentBinary() const;
/* Select binary by name. */
void selectBinary(std::string const &name);
/* Whether the current project is open. This can be used to show warnings
when attempting to close the program or load another project. */
bool isProjectDirty() const;
/* Ask for confirmation to discard unsaved project data if the project is
currently dirty. Returns true if the project is either currently saved,
or the user opts to discard unsaved data. This function does not itself
save the project. */
bool confirmProjectUnload() const;
/* Replace the current project with a fresh one. Any unsaved data is lost;
check isProjectDirty() before calling. */
void switchToNewProject(std::string const &name = "",
std::string const &path = "");
/* Replace the current project by loading an existing one. On success,
unloads the current project; any unsaved data will be lost. Check
isProjectDirty() before calling. On error, returns false and does *not*
unload the current project. */
bool loadProject(std::string const &path);
/* Recent projects. */
RecentProjects &recentProjects()
{
return m_recent;
}
//=== Legacy (0.x) virtual space info for migrations ===//
std::map<std::string, LegacyVspace> legacySpaces;
std::string legacyCurrentSpace;
std::string legacyGenerateSpaceName(std::string prefix,
bool force_suffix=false);
//---
// Virtual spaces
// TODO: To be replaced with legacy descriptions
//---
/* Virtual spaces organized by name */
std::map<std::string, std::unique_ptr<VirtualSpace>> spaces;
/* Find a virtual space by name */
VirtualSpace *get_space(std::string name);
/* Current virtual space */
VirtualSpace *current_space;
/* Find an unused name from this prefix. If force_suffix is set, always
adds a suffix even if the name itself is free */
std::string generate_space_name(std::string prefix,
bool force_suffix=false);
private:
/* Path to configuration file. */
fs::path m_config;
/* List of recent projects. */
RecentProjects m_recent;
/* Current project. */
std::unique_ptr<Project> m_project;
/* Current binary name within the project. */
std::string m_currentBinaryName;
};
#endif /* FXOS_SESSION_H */