sysd-manager 2.23.0

Application to empower user to manage their <b>systemd units</b> via Graphical User Interface. Not only are you able to make changes to the enablement and running status of each of the units, but you will also be able to view and modify their unit files and check the journal logs.
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
mod creator_page_mount;
mod creator_page_service;
mod creator_page_timer;
//pub mod dropdown;
mod first_page;
mod imp;
mod launch_creator_page;
pub mod mydropdown;
pub mod navigation_row;
pub mod suggestion;
mod unit_file;
mod unit_file_creator_page;

use crate::{
    consts::{ERROR_CSS, WARNING_CSS},
    format2,
    widget::app_window::AppWindow,
};
use adw::{prelude::PreferencesRowExt, subclass::prelude::ObjectSubclassIsExt};
use gettextrs::pgettext;
use glib::object::IsA;
use gtk::glib::{self};
use std::{
    cell::Ref,
    collections::HashSet,
    path::{Path, PathBuf},
};
use systemd::errors::SystemdErrors;
use tracing::{error, warn};

glib::wrapper! {

    pub struct UnitCreatorWindow(ObjectSubclass<imp::UnitCreatorWindowImp>)
    @extends adw::Window, gtk::Window, gtk::Widget,
    @implements gtk::Accessible,  gtk::Buildable,  gtk::ConstraintTarget,
    gtk::Native, gtk::Root, gtk::ShortcutManager;
}

impl UnitCreatorWindow {
    pub fn new(app_window: &AppWindow) -> Self {
        let obj: UnitCreatorWindow = glib::Object::new();
        let _ = obj.imp().app_window.set(app_window.clone());
        obj
    }

    pub fn action_group(&self) -> gio::SimpleActionGroup {
        self.imp().action_group.borrow().clone()
    }

    pub fn set_creation_type(&self, unit_type: UnitCreateType) {
        self.imp().set_creation_unit_type(unit_type);
    }

    pub fn creation_type(&self) -> UnitCreateType {
        self.imp().creation_type.get()
    }

    pub fn system_file_list(&self) -> Ref<'_, HashSet<String>> {
        self.imp().system_file_list.borrow()
    }

    pub fn session_file_list(&self) -> Ref<'_, HashSet<String>> {
        self.imp().session_file_list.borrow()
    }

    pub fn add_toast_message(
        &self,
        message: &str,
        markup: bool,
        action: Option<(&str, String, bool)>,
    ) {
        self.imp().add_toast_message(message, markup, action);
    }

    pub fn app_window(&self) -> Option<&AppWindow> {
        self.imp().app_window.get()
    }

    fn unit_name(&self, create_type: UnitCreateType) -> Option<String> {
        self.imp().unit_name(create_type)
    }

    pub fn file_path(&self) -> Option<PathBuf> {
        self.imp().file_path(None)
    }

    pub(crate) fn update_unit_prefix(&self, escaped_prefix: String) -> CreateUnitErr {
        self.imp().update_unit_prefix(escaped_prefix)
    }
}

pub const VALID_UNIT_NAME: &str = r"^\s*[a-zA-Z0-9._:\-\\]+@?\s*$";
pub const ACTION_CREATOR_UNIT_BUS: &str = "creator.create-unit-bus-selection";
pub const ACTION_CREATOR_UNIT_TYPE_SELECTION: &str = "creator.create-unit-type-selection";
pub const ACTION_CREATOR_NEXT: &str = "creator.next";
pub const ACTION_CREATOR_FILE: &str = "creator.file";
pub const ACTION_CREATOR_CREATE: &str = "creator.create";
pub const ACTION_CREATOR_PREVIOUS: &str = "creator.previous";
pub const ACTION_CREATOR_AVANCED_MODE: &str = "creator.creator-advanced-mode";
pub const PAGE_FIRST: &str = "first-page";
pub const PAGE_LAUNCH: &str = "launch-page";
pub const PAGE_TIMER: &str = "timer-page";
pub const PAGE_SERVICE: &str = "service-page";
pub const PAGE_MOUNT: &str = "mount-page";

#[derive(Debug, Copy, Clone, PartialEq, Eq, glib::Enum, Default, Hash)]
#[enum_type(name = "UnitCreateType")]
pub enum UnitCreateType {
    #[default]
    Service,
    Timer,
    TimerService,
    Mount,
}

impl UnitCreateType {
    pub fn max_suffix_len(&self) -> usize {
        self.dot_suffix().len()
    }

    pub fn dot_suffix(&self) -> &str {
        match self {
            UnitCreateType::Service | UnitCreateType::TimerService => ".service",
            UnitCreateType::Timer => ".timer",
            UnitCreateType::Mount => ".mount",
        }
    }

    pub fn suffix(&self) -> &str {
        &self.dot_suffix()[1..]
    }

    pub fn full_name(&self, prefix: &str) -> String {
        let dot_suffix = self.dot_suffix();
        let mut s = String::with_capacity(prefix.len() + dot_suffix.len());
        s.push_str(prefix);
        s.push_str(dot_suffix);
        s
    }

    fn title(&self) -> String {
        match self {
            //Create Unit title
            UnitCreateType::Service => pgettext("create", "Service"),
            //Create Unit title
            UnitCreateType::Timer => pgettext("create", "Timer"),
            //Create Unit title
            UnitCreateType::TimerService => pgettext("create", "Timer with Service"),
            //Create Unit title
            UnitCreateType::Mount => pgettext("create", "Mount"),
        }
    }
}

impl From<&glib::Variant> for UnitCreateType {
    fn from(value: &glib::Variant) -> Self {
        match value.get::<String>() {
            Some(s) => s.into(),
            None => {
                warn!("Unkown type None",);
                UnitCreateType::Service
            }
        }
    }
}

impl From<glib::GString> for UnitCreateType {
    fn from(value: glib::GString) -> Self {
        value.as_str().into()
    }
}

impl From<&str> for UnitCreateType {
    fn from(value: &str) -> Self {
        match value {
            "service" => UnitCreateType::Service,
            "timer" => UnitCreateType::Timer,
            "timer_service" => UnitCreateType::TimerService,
            "mount" => UnitCreateType::Mount,

            other => {
                warn!("Unkown type {:?}", other);
                UnitCreateType::Service
            }
        }
    }
}

impl From<String> for UnitCreateType {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

#[derive(Debug)]
pub enum SaveUnit {
    Created,
    CreateError(SystemdErrors),
}

#[derive(Debug, PartialEq)]
pub(crate) enum CreateUnitErr {
    NoErr,
    WrongChar,
    FileExits,
    Empty,
    FileNotExits,
    NotFile,
    NotExecutable,
    Malformed,
    NotAbsolute,
    NotDir,
    NoPath,
    TooShort(usize),
    TooLong(usize),
    Mandatory,
    Unknown,
}

impl CreateUnitErr {
    fn title_err(&self, prefix: &str) -> String {
        match self {
            CreateUnitErr::WrongChar => format!("{prefix} - Invalid character"),
            CreateUnitErr::FileExits => format!("{prefix} - Unit File already exists"),
            CreateUnitErr::Empty => format!("{prefix} -  Empty"),
            CreateUnitErr::FileNotExits => format!("{prefix} - File not exists"),
            CreateUnitErr::NotFile => format!("{prefix} - Not a File"),
            CreateUnitErr::NotExecutable => format!("{prefix} - Not an executable"),
            CreateUnitErr::Malformed => format!("{prefix} - Malformed"),
            CreateUnitErr::NoErr => prefix.to_owned(),
            CreateUnitErr::NotAbsolute => {
                format2!(pgettext("validator", "{} - Not absolute path"), prefix)
            }
            CreateUnitErr::NotDir => {
                format2!(pgettext("validator", "{} - Not a directory"), prefix)
            }
            CreateUnitErr::NoPath => {
                format2!(pgettext("validator", "{} - No path specified"), prefix)
            }
            CreateUnitErr::TooShort(limit) => format2!(
                pgettext(
                    "validator",
                    "{} - Too short, under the limit of {} characters"
                ),
                prefix,
                limit
            ),
            CreateUnitErr::TooLong(limit) => format2!(
                pgettext(
                    "validator",
                    "{} - Too long, over the limit of {} characters"
                ),
                prefix,
                limit
            ),
            CreateUnitErr::Mandatory => format2!(pgettext("validator", "{} - Mandatory"), prefix),
            CreateUnitErr::Unknown => format2!(pgettext("validator", "{} - Unknown"), prefix),
        }
    }

    fn apply_validation_result(
        entry: &(impl IsA<adw::PreferencesRow> + gtk::prelude::WidgetExt),
        name_err: &CreateUnitErr,
        prefix: &str,
    ) {
        Self::apply_validation_result_error(entry, name_err, prefix, false);
    }

    fn apply_validation_result_error(
        entry: &(impl IsA<adw::PreferencesRow> + gtk::prelude::WidgetExt),
        name_err: &CreateUnitErr,
        prefix: &str,
        error: bool,
    ) -> bool {
        entry.set_title(&name_err.title_err(prefix));
        match name_err {
            CreateUnitErr::NoErr => {
                entry.remove_css_class(WARNING_CSS);
                entry.remove_css_class(ERROR_CSS);
                true
            }
            _ => {
                if error {
                    entry.add_css_class(ERROR_CSS);
                } else {
                    entry.add_css_class(WARNING_CSS);
                }
                false
            }
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default, glib::Enum)]
#[enum_type(name = "PageType")]
pub enum PageType {
    #[default]
    Start,
    Service,
    ServiceFile,
    Timer,
    TimerFile,
    Launch,
    Mount,
    MountFile,
}

const FILE_PAGE_SERVICE: &str = "service-file-page";
const FILE_PAGE_MOUNT: &str = "mount-file-page";
const FILE_PAGE_TIMER: &str = "timer-file-page";

impl PageType {
    fn id(&self) -> &str {
        match self {
            PageType::Start => PAGE_FIRST,
            PageType::Service => PAGE_SERVICE,
            PageType::ServiceFile => FILE_PAGE_SERVICE,
            PageType::Timer => PAGE_TIMER,
            PageType::TimerFile => FILE_PAGE_TIMER,
            PageType::Launch => PAGE_LAUNCH,
            PageType::Mount => PAGE_MOUNT,
            PageType::MountFile => FILE_PAGE_MOUNT,
        }
    }

    fn next(&self, creation_type: UnitCreateType) -> Option<&'static str> {
        match (self, creation_type) {
            (PageType::Start, UnitCreateType::Timer) => Some(PAGE_TIMER),
            (PageType::Start, UnitCreateType::Mount) => Some(PAGE_MOUNT),
            (PageType::Start, _) => Some(PAGE_SERVICE),
            (PageType::Service, UnitCreateType::TimerService) => Some(PAGE_TIMER),
            (PageType::Service, _) => Some(PageType::Launch.id()),
            (PageType::ServiceFile, UnitCreateType::TimerService) => Some(PAGE_TIMER),
            (PageType::ServiceFile, _) => Some(PageType::Launch.id()),
            (PageType::Timer, _) => Some(PageType::Launch.id()),
            (PageType::TimerFile, _) => Some(PageType::Launch.id()),
            (PageType::Mount, _) => Some(PageType::Launch.id()),
            (PageType::MountFile, _) => Some(PageType::Launch.id()),
            (PageType::Launch, _) => None,
        }
    }
}

impl From<Option<&str>> for PageType {
    fn from(value: Option<&str>) -> Self {
        match value {
            Some(PAGE_FIRST) => PageType::Start,
            Some(PAGE_TIMER) => PageType::Timer,
            Some(PAGE_SERVICE) => PageType::Service,
            Some(PAGE_MOUNT) => PageType::Mount,
            Some(FILE_PAGE_SERVICE) => PageType::ServiceFile,
            Some(FILE_PAGE_TIMER) => PageType::TimerFile,
            Some(FILE_PAGE_MOUNT) => PageType::MountFile,
            Some(PAGE_LAUNCH) => PageType::Launch,
            Some(tag) => {
                warn!("Unkown TAG {tag}");
                PageType::Launch
            }
            None => {
                error!("Missing Tag");
                PageType::Start
            }
        }
    }
}

pub fn set_initial_folder(file_dialog: &gtk::FileDialog) {
    if let Ok(home) = std::env::var("HOME") {
        let path = Path::new(&home);
        let dir = gio::File::for_path(path);
        file_dialog.set_initial_folder(Some(&dir));
    }
}

fn get_file_path(text: &str) -> Result<&str, CreateUnitErr> {
    let text = text.trim_start();
    let mut begin = 0;
    let mut end = text.len();
    let mut in_quotes = false;

    for (idx, char) in text.char_indices() {
        if char.is_whitespace() && !in_quotes {
            end = idx;
            break;
        } else if char == '"' {
            if idx == 0 {
                in_quotes = true;
                begin = 1;
            } else {
                end = idx;
                in_quotes = false;
                break;
            }
        }
    }
    if in_quotes {
        return Err(CreateUnitErr::Malformed);
    }
    Ok(&text[begin..end])
}

#[cfg(test)]
mod tests {
    use test_base::init_logs;

    use super::*;

    #[test]
    fn test_get_file() {
        init_logs();
        assert_eq!(get_file_path("text"), Ok("text"));
        assert_eq!(get_file_path("  text"), Ok("text"));
        assert_eq!(get_file_path("  text   "), Ok("text"));
        assert_eq!(get_file_path("  text -f  "), Ok("text"));
        assert_eq!(get_file_path(r#""text asdf" xxx"#), Ok("text asdf"));
        assert_eq!(get_file_path("\"\"text"), Ok(""));

        assert_eq!(
            get_file_path("/home/plr/bin/AppDir/etc"),
            Ok("/home/plr/bin/AppDir/etc")
        );
    }
}