Skip to content

Commit

Permalink
Implements data root reading for Linux (#1158)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 3, 2024
1 parent dc2b1b0 commit ceef556
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 105 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ version = "0.10.2"
features = ["async-std", "raw_handle"]
default-features = false

[target.'cfg(target_os = "linux")'.dependencies]
xdg = "2.5.2"

[target.'cfg(target_os = "macos")'.dependencies]
applevisor-sys = "0.1.3"
core-graphics-types = "0.1.3"
Expand Down
87 changes: 0 additions & 87 deletions gui/initialize_wizard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,86 +16,11 @@
#define FIELD_GAMES_LOCATION "gamesLocation"

enum PageId {
PageSystem,
PageGame,
PageFirmware,
PageConclusion
};

class SystemPage : public QWizardPage {
public:
SystemPage() : m_input(nullptr)
{
auto layout = new QVBoxLayout();

// Page properties.
setTitle("Location for system files");
setSubTitle(
"The selected directory will be used for everything except games (e.g. save "
"data and firmware files).");

// Widgets.
layout->addLayout(setupInputRow());

setLayout(layout);
}

bool validatePage() override
{
auto path = m_input->text();

if (!QDir::isAbsolutePath(path)) {
QMessageBox::critical(this, "Error", "The location must be an absolute path.");
return false;
}

if (!QDir(path).exists()) {
QMessageBox::critical(this, "Error", "The location does not exist.");
return false;
}

return true;
}
private:
QLayout *setupInputRow()
{
auto layout = new QHBoxLayout();

// Label.
auto label = new QLabel("&Location:");
layout->addWidget(label);

// Input.
m_input = new QLineEdit();
m_input->setText(readSystemDirectorySetting());

label->setBuddy(m_input);
layout->addWidget(m_input);

registerField(FIELD_SYSTEM_LOCATION "*", m_input);

// Browse.
auto browse = new QPushButton("...");

connect(browse, &QPushButton::clicked, this, &SystemPage::browseDirectory);

layout->addWidget(browse);

return layout;
}

void browseDirectory()
{
auto path = QFileDialog::getExistingDirectory(this, "Location for system files");

if (!path.isEmpty()) {
m_input->setText(QDir::toNativeSeparators(path));
}
}

QLineEdit *m_input;
};

class GamePage : public QWizardPage {
public:
GamePage() : m_input(nullptr)
Expand Down Expand Up @@ -247,11 +172,6 @@ class ConclusionPage : public QWizardPage {
{
auto wizard = this->wizard();

if (wizard->hasVisitedPage(PageSystem)) {
auto path = field(FIELD_SYSTEM_LOCATION).toString();
writeSystemDirectorySetting(QDir::toNativeSeparators(path));
}

if (wizard->hasVisitedPage(PageGame)) {
auto path = field(FIELD_GAMES_LOCATION).toString();
writeGamesDirectorySetting(QDir::toNativeSeparators(path));
Expand All @@ -272,7 +192,6 @@ InitializeWizard::InitializeWizard()
#endif

// Pages.
setPage(PageSystem, new SystemPage());
setPage(PageGame, new GamePage());
setPage(PageFirmware, new FirmwarePage());
setPage(PageConclusion, new ConclusionPage());
Expand All @@ -285,12 +204,6 @@ InitializeWizard::~InitializeWizard()
int InitializeWizard::nextId() const
{
switch (currentId()) {
case PageSystem:
if (!hasGamesDirectorySetting()) {
return PageGame;
}

[[fallthrough]];
case PageGame:
return PageFirmware;
case PageFirmware:
Expand Down
17 changes: 0 additions & 17 deletions gui/src/setup/data.rs

This file was deleted.

34 changes: 34 additions & 0 deletions gui/src/setup/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::io::ErrorKind;
use std::path::PathBuf;
use thiserror::Error;
use xdg::BaseDirectories;

pub fn read_data_root() -> Result<Option<PathBuf>, DataRootError> {
let file = get_config_path()?;

match std::fs::read_to_string(&file) {
Ok(v) => Ok(Some(v.trim().into())),
Err(e) if e.kind() == ErrorKind::NotFound => Ok(None),
Err(e) => Err(DataRootError::ReadFile(file, e)),
}
}

fn get_config_path() -> Result<PathBuf, DataRootError> {
BaseDirectories::new()
.map(|xdg| {
let mut p = xdg.get_config_home();
p.push("obliteration.conf");
p
})
.map_err(DataRootError::XdgBaseDirectory)
}

/// Represents an error when read or write data root fails.
#[derive(Debug, Error)]
pub enum DataRootError {
#[error("couldn't load XDG Base Directory")]
XdgBaseDirectory(#[source] xdg::BaseDirectoriesError),

#[error("couldn't read {0}")]
ReadFile(PathBuf, #[source] std::io::Error),
}
10 changes: 10 additions & 0 deletions gui/src/setup/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::path::PathBuf;
use thiserror::Error;

pub fn read_data_root() -> Result<Option<PathBuf>, DataRootError> {
todo!()
}

/// Represents an error when read or write data root fails.
#[derive(Debug, Error)]
pub enum DataRootError {}
10 changes: 9 additions & 1 deletion gui/src/setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use self::data::DataRootError;

use self::data::read_data_root;
use crate::data::{DataError, DataMgr};
use crate::dialogs::{open_file, FileType};
Expand All @@ -12,11 +14,14 @@ use std::path::PathBuf;
use std::rc::Rc;
use thiserror::Error;

#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(target_os = "macos", path = "macos.rs")]
#[cfg_attr(target_os = "windows", path = "windows.rs")]
mod data;

pub fn run_setup() -> Result<Option<DataMgr>, SetupError> {
// Load data root.
let root = read_data_root()?;
let root = read_data_root().map_err(SetupError::ReadDataRoot)?;

if let Some(p) = root.as_ref().filter(|p| p.is_dir()) {
// Check if root partition exists.
Expand Down Expand Up @@ -115,6 +120,9 @@ fn install_firmware(win: SetupWizard) {
/// Represents an error when [`run_setup()`] fails.
#[derive(Debug, Error)]
pub enum SetupError {
#[error("couldn't read data location")]
ReadDataRoot(#[source] DataRootError),

#[error("couldn't create data manager on {0}")]
DataManager(PathBuf, #[source] DataError),

Expand Down
10 changes: 10 additions & 0 deletions gui/src/setup/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::path::PathBuf;
use thiserror::Error;

pub fn read_data_root() -> Result<Option<PathBuf>, DataRootError> {
todo!()
}

/// Represents an error when read or write data root fails.
#[derive(Debug, Error)]
pub enum DataRootError {}

0 comments on commit ceef556

Please sign in to comment.