fxos/shell/session.h

112 lines
3.1 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 ===//
/* Get the current project. This can only be null during startup and while
switching projects. It is an application invariant that there is always
a project open (even if temporary/unsaved). */
Project *project()
{
return m_project.get();
}
/* 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;
};
#endif /* FXOS_SESSION_H */