summaryrefslogtreecommitdiff
path: root/src/xdg.rs
blob: d5c9d41daafae94b5801e3493dff86d3c0db3c97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2022 Maxime “pep” Buquet <pep@bouah.net>
//
// This program 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.
//
// This program 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
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::cell::LazyCell;

use directories::ProjectDirs;
use pyo3::{
    marker::Python,
    prelude::{pyclass, pymethods, PyObject, PyResult},
    IntoPy,
};

/// Project qualifier
pub const QUALIFIER: &'static str = "io";
/// Project organization
pub const ORGANIZATION: &'static str = "poez";
/// Project appname
pub const APPNAME: &'static str = "Poezio";

/// Project directories
pub const PROJECT: LazyCell<ProjectDirs> = LazyCell::new(|| {
    ProjectDirs::from(QUALIFIER, ORGANIZATION, APPNAME).expect("HOME dir should be available.")
});

#[pyclass(name = "XDG")]
pub struct PyProject(ProjectDirs);

fn get_path(py: Python<'_>) -> PyResult<PyObject> {
    // TODO: Stop importing pathlib all the time
    let pathlib = py.import("pathlib")?;
    let path = pathlib.getattr("Path")?;
    Ok(path.into_py(py))
}

impl PyProject {
    pub fn new(dirs: ProjectDirs) -> Self {
        PyProject(dirs)
    }
}

#[pymethods]
impl PyProject {
    #[getter]
    pub fn cache_dir(&self, py: Python<'_>) -> PyResult<PyObject> {
        Ok(get_path(py)?.call1(py, (self.0.cache_dir(),))?)
    }

    #[getter]
    pub fn config_dir(&self, py: Python<'_>) -> PyResult<PyObject> {
        Ok(get_path(py)?.call1(py, (self.0.config_dir(),))?)
    }

    #[getter]
    pub fn data_dir(&self, py: Python<'_>) -> PyResult<PyObject> {
        Ok(get_path(py)?.call1(py, (self.0.data_dir(),))?)
    }
}