udisks2 0.3.1

Unofficial crate for interacting with the UDisks2 API
Documentation
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
use std::ffi::CString;

use gettextrs::{gettext, pgettext};

use crate::{
    block,
    drive::{self, RotationRate},
    error, mdraid,
    gettext::{dpgettext, gettext_f, pgettext_f},
    media::{self, DriveType},
    partition, r#loop, Client, Object,
};

/// Icon
///
/// Represents an icon that can be looked up from an icon theme.
/// An icon may have an symbolic version as well.
#[derive(Debug, Default, Clone)]
pub struct Icon {
    name: Option<String>,
    name_symbolic: Option<String>,
}

impl Icon {
    fn new(name: Option<String>, name_symbolic: Option<String>) -> Self {
        Self {
            name,
            name_symbolic,
        }
    }

    fn set_if_none(&mut self, icon: String, icon_symbolic: String) {
        self.name.get_or_insert(icon);
        self.name_symbolic.get_or_insert(icon_symbolic);
    }

    /// Name of the icon.
    ///
    /// If the [`Object`] has no associated icon, None is returned.
    pub fn name(&self) -> Option<&String> {
        self.name.as_ref()
    }

    /// Name of the symbolic icon.
    ///
    /// If the [`Object`] has no associated symbolic icon, None is returned.
    pub fn name_symbolic(&self) -> Option<&String> {
        self.name_symbolic.as_ref()
    }
}

/// Detailed information about the D-Bus interfaces (such as [`block::BlockProxy`] and [`drive::DriveProxy`])
/// on a [`Object`] that is suitable to display in an user interface.
#[derive(Debug, Clone)]
pub struct ObjectInfo<'a> {
    /// The [`Object`] that the info is about
    pub object: &'a Object,

    /// Name of the object
    pub name: Option<String>,

    /// Description of the object
    pub description: Option<String>,

    /// Icon associated with the object
    ///
    /// The returned icon may be influenced by [`block::BlockProxy::hint_name()`].
    pub icon: Icon,

    /// Description of media associated with the object
    pub media_description: Option<String>,

    /// Icon associated with media
    ///
    /// The returned icon may be influenced by [`block::BlockProxy::hint_name()`].
    pub media_icon: Icon,

    /// Single-line description
    ///
    /// A single line string, containing enough detail to be used as a comprehensive
    /// representation of the `object`. For instance, in the case of block devices
    /// or drives, it includes critical information like the device's special file
    /// path, such as `/dev/sda`.
    pub one_liner: Option<String>,

    /// Sort key
    ///
    /// This can be used to sort objects.
    pub sort_key: Option<String>,
}

impl<'a> ObjectInfo<'a> {
    pub(crate) fn new(object: &'a Object) -> Self {
        Self {
            object,
            name: None,
            description: None,
            icon: Icon::default(),
            media_description: None,
            media_icon: Icon::default(),
            one_liner: None,
            sort_key: None,
        }
    }

    pub(crate) async fn info_for_block(
        &mut self,
        client: &Client,
        block: block::BlockProxy<'_>,
        partition: Option<partition::PartitionProxy<'_>>,
    ) {
        self.icon = Icon::new(
            Some("drive-removable-media".to_owned()),
            Some("drive-removable-media-symbolic".to_owned()),
        );
        self.name = block
            .preferred_device()
            .await
            .ok()
            .and_then(|dev| CString::from_vec_with_nul(dev).ok())
            .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok());

        let size = block.size().await;
        if let Ok(size) = size {
            let size = client.size_for_display(size, false, false);
            self.description = Some(gettext_f("{} Block Device", [size]));
        } else {
            self.description = Some(gettext("Block Device"));
        }

        let mut partition_number = None;
        if let Some(partition) = partition {
            //TODO: we're expecting it here to to be fine to load,
            //but further down we handle the error???
            partition_number = partition.number().await.ok();

            // Translators: Used to describe a partition of a block device.
            //              The %u is the partition number.
            //              The %s is the description for the block device (e.g. "5 GB Block Device").
            //TODO: pgettext_f does not support non-string C placeholders, so we replace it
            //manually to maintain compatibilty
            self.description = Some(
                pgettext_f(
                    "part-block",
                    "Partition %u of {}",
                    [
                        //Safe to unwrap, we have previously set this
                        self.description.as_ref().unwrap(),
                    ],
                )
                .replace(
                    "%u",
                    &partition_number
                        .expect("Failed to read partition number")
                        .to_string(),
                ),
            );
        }

        // Translators: String used for one-liner description of a block device.
        //              The first %s is the description of the object (e.g. "50 GB Block Device").
        //              The second %s is the special device file (e.g. "/dev/sda2").
        //TODO: C version calls preferred_device again, instead of using name, why?
        self.one_liner = Some(pgettext_f(
            "one-liner-block",
            "{} ({})",
            [
                self.description.as_ref().unwrap(),
                self.name.as_ref().unwrap(),
            ],
        ));

        self.sort_key = Some(format!(
            "02_block_{}_{}",
            // safe to unwrap, object path always have at least one `/`
            self.object.object_path().split('/').last().unwrap(),
            //TODO: use async closure when stable
            partition_number.unwrap_or(0)
        ))
    }

    pub(crate) async fn info_for_loop(
        &mut self,
        client: &Client,
        loop_proxy: r#loop::LoopProxy<'_>,
        block: block::BlockProxy<'_>,
        partition: Option<partition::PartitionProxy<'_>>,
    ) {
        self.icon = Icon::new(
            Some("drive-removable-media".to_owned()),
            Some("drive-removable-media-symbolic".to_owned()),
        );
        self.name = loop_proxy
            .backing_file()
            .await
            .ok()
            .and_then(|dev| CString::from_vec_with_nul(dev).ok())
            .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok());

        let size = block.size().await;
        if let Ok(size) = size {
            let size = client.size_for_display(size, false, false);
            self.description = Some(gettext_f("{} Loop Device", [size]));
        } else {
            self.description = Some(gettext("Loop Device"));
        }

        let mut partition_number = None;
        if let Some(partition) = partition {
            //TODO: we're expecting it here to to be fine to load,
            //but further down we handle the error???
            partition_number = partition.number().await.ok();

            // Translators: Used to describe a partition of a loop device.
            //              The %u is the partition number.
            //              The %s is the description for the block device (e.g. "5 GB Loop Device").
            //TODO: pgettext_f does not support non-string C placeholders, so we replace it
            //manually to maintain compatibilty
            self.description = Some(
                pgettext_f(
                    "part-loop",
                    "Partition %u of {}",
                    [
                        //Safe to unwrap, we have previously set this
                        self.description.as_ref().unwrap(),
                    ],
                )
                .replace(
                    "%u",
                    &partition_number
                        .expect("Failed to read partition number")
                        .to_string(),
                ),
            );
        }

        // Translators: String used for one-liner description of a loop device.
        //              The first %s is the description of the object (e.g. "2 GB Loop Device").
        //              The second %s is the name of the backing file (e.g. "/home/davidz/file.iso").
        //              The third %s is the special device file (e.g. "/dev/loop2").
        self.one_liner = Some(pgettext_f(
            "one-liner-loop",
            "{} — {} ({})",
            [
                self.description.as_ref().unwrap(),
                //safe to unwrap, has been set previously
                self.name.as_ref().unwrap(),
                &block
                    .preferred_device()
                    .await
                    .ok()
                    .and_then(|dev| CString::from_vec_with_nul(dev).ok())
                    .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok())
                    .unwrap_or_default(),
            ],
        ));

        self.sort_key = Some(format!(
            "03_loop_{}_{}",
            // safe to unwrap, object path always have at least one `/`
            self.object.object_path().split('/').last().unwrap(),
            //TODO: use async closure when stable
            partition_number.unwrap_or(0)
        ));
    }

    pub(crate) async fn info_for_mdraid(
        &mut self,
        client: &Client,
        mdraid: mdraid::MDRaidProxy<'_>,
        partition: Option<partition::PartitionProxy<'_>>,
    ) {
        let name = mdraid.name().await.unwrap_or_default();
        self.name = Some(name.split(':').last().unwrap_or_else(|| &name).to_string());
        self.icon = Icon::new(
            Some("drive-multidisk".to_owned()),
            Some("drive-multidisk-symbolic".to_owned()),
        );

        let level = mdraid.level().await;
        let size = mdraid.size().await;
        if let Ok(size) = size {
            let size = client.size_for_display(size, false, false);
            // Translators: Used to format the description for a RAID array.
            //              The first %s is the size (e.g. '42.0 GB').
            //              The second %s is the level (e.g. 'RAID-5 Array').
            self.description = Some(pgettext_f(
                "mdraid-desc",
                "{} {}",
                [size, self.format_level(level)],
            ));
        } else {
            self.description = Some(self.format_level(level));
        }

        let mut partition_number = None;
        if let Some(partition) = partition {
            //TODO: we're expecting it here to to be fine to load,
            //but further down we handle the error???
            partition_number = partition.number().await.ok();
            // Translators: Used to describe a partition of a RAID Array.
            //              The %u is the partition number.
            //              The %s is the description for the drive (e.g. "2 TB RAID-5").
            //TODO: pgettext_f does not support non-string C placeholders, so we replace it
            //manually to maintain compatibilty
            self.description = Some(
                pgettext_f(
                    "part-raid",
                    "Partition %u of {}",
                    [
                        &partition_number
                            .expect("Failed to read partition number")
                            .to_string(),
                        //Safe to unwrap, we have previously set this
                        self.description.as_ref().unwrap(),
                    ],
                )
                .replace(
                    "%u",
                    &partition_number
                        .expect("Failed to read partition number")
                        .to_string(),
                ),
            );
        }

        let block = client.block_for_mdraid(&mdraid).await;
        if self.name.as_deref().is_some_and(|name| !name.is_empty()) {
            if let Some(block) = block {
                let preferred_device = block
                    .preferred_device()
                    .await
                    .ok()
                    .and_then(|dev| CString::from_vec_with_nul(dev).ok())
                    .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok())
                    .expect("Failed to get preferred device");

                // Translators: String used for one-liner description of running RAID array.
                //              The first %s is the array name (e.g. "AlphaGo").
                //              The second %s is the size and level (e.g. "2 TB RAID-5").
                //              The third %s is the special device file (e.g. "/dev/sda").
                self.one_liner = Some(pgettext_f(
                    "one-liner-mdraid-running",
                    "{} — {} ({})",
                    [
                        self.name.as_deref().unwrap(),
                        self.description.as_deref().unwrap_or_default(),
                        &preferred_device,
                    ],
                ));
            } else {
                // Translators: String used for one-liner description of non-running RAID array.
                //              The first %s is the array name (e.g. "AlphaGo").
                //              The second %s is the size and level (e.g. "2 TB RAID-5").
                self.one_liner = Some(pgettext_f(
                    "one-liner-mdraid-not-running",
                    "{} — {}",
                    [
                        self.name.as_deref().unwrap_or_default(),
                        self.description.as_deref().unwrap_or_default(),
                    ],
                ));
            }
        } else if let Some(block) = block {
            let preferred_device = block
                .preferred_device()
                .await
                .ok()
                .and_then(|dev| CString::from_vec_with_nul(dev).ok())
                .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok())
                .expect("Failed to get preferred device");

            // Translators: String used for one-liner description of running RAID array.
            //              The first %s is the array name (e.g. "AlphaGo").
            //              The second %s is the size and level (e.g. "2 TB RAID-5").
            //              The third %s is the special device file (e.g. "/dev/sda").
            self.one_liner = Some(pgettext_f(
                "one-liner-mdraid-no-name-running",
                "{} — {}",
                [
                    self.description.as_deref().unwrap_or_default(),
                    &preferred_device,
                ],
            ));
        } else {
            // Translators: String used for one-liner description of non-running RAID array.
            //              The first %s is the array name (e.g. "AlphaGo").
            //              The second %s is the size and level (e.g. "2 TB RAID-5").
            self.one_liner = Some(pgettext_f(
                "one-liner-mdraid-no-name-not-running",
                "{}",
                [self.description.as_deref().unwrap_or_default()],
            ));
        }

        self.sort_key = Some(format!(
            "01_mdraid_{}_{}",
            mdraid.uuid().await.expect("Failed to get mdraid uuid"),
            //TODO: use async closure when stable
            partition_number.unwrap_or(0)
        ));
    }

    pub(crate) async fn info_for_drive(
        &mut self,
        client: &Client,
        drive: &drive::DriveProxy<'_>,
        partition: Option<partition::PartitionProxy<'_>>,
    ) {
        let vendor = drive.vendor().await.unwrap_or_default();
        // "%vendor $model"
        self.name = Some(format!(
            "{}{}{}",
            vendor,
            if vendor.is_empty() { "" } else { " " },
            drive.model().await.unwrap_or_default()
        ));

        let media_removable = drive.media_removable().await.unwrap_or_default();
        let media_available = drive.media_available().await.unwrap_or_default();
        let media = drive.media().await.unwrap();
        let media_compat = drive.media_compatibility().await.unwrap_or_default();

        let mut desc = String::new();
        let mut desc_type = None;
        for media_data in media::MEDIA_DATA {
            if media_compat.contains(&media_data.id) {
                self.icon.set_if_none(
                    media_data.drive_icon.to_owned(),
                    media_data.drive_icon_symbolic.to_owned(),
                );
                if !desc.contains(media_data.media_family) {
                    if !desc.is_empty() {
                        desc.push('/');
                    }
                    desc.push_str(&pgettext("media-type", media_data.media_family));
                }
                desc_type = Some(media_data.media_type);
            }

            if media_removable && media_available {
                //media
                if media == media_data.id {
                    if self.media_description.is_none() {
                        self.media_description = Some(match media_data.media_type {
                            media::DriveType::Drive => {
                                //Translators: Used to describe drive without removable media. The %s is the type, e.g. 'Thumb'
                                pgettext_f(
                                    "drive-with-fixed-media",
                                    "{} Drive",
                                    [dpgettext("media-type", media_data.media_name)],
                                )
                            }
                            media::DriveType::Disk => {
                                //Translators: Used to describe generic media. The %s is the type, e.g. 'Zip' or 'Floppy'
                                pgettext_f(
                                    "drive-with-generic-media",
                                    "{} Disk",
                                    [dpgettext("media-type", media_data.media_name)],
                                )
                            }
                            media::DriveType::Card => {
                                //Translators: Used to describe flash media. The %s is the type, e.g. 'SD' or 'CompactFlash'
                                pgettext_f(
                                    "flash-media",
                                    "{} Card",
                                    [dpgettext("media-type", media_data.media_name)],
                                )
                            }
                            media::DriveType::Disc => {
                                //Translators: Used to describe optical discs. The %s is the type, e.g. 'CD-R' or 'DVD-ROM'
                                pgettext_f(
                                    "optical-media",
                                    "{} Disc",
                                    [dpgettext("media-type", media_data.media_name)],
                                )
                            }
                        });
                    }

                    self.media_icon.set_if_none(
                        media_data.media_icon.to_owned(),
                        media_data.media_icon_symbolic.to_owned(),
                    );
                }
            }
        }

        let size = drive
            .size()
            .await
            .ok()
            .map(|size| client.size_for_display(size, false, false));
        let rotation_rate = drive.rotation_rate().await.unwrap_or_default();
        self.description = Some(match desc_type {
            None => {
                if media_removable {
                    if let Some(size) = size {
                        // Translators: Used to describe a drive. The %s is the size, e.g. '20 GB'
                        pgettext_f("drive-with-size", "{} Drive", [size])
                    } else {
                        //Translators: Used to describe a drive we know very little about (removable media or size not known)
                        pgettext("generic-drive", "Drive")
                    }
                } else if rotation_rate == RotationRate::NonRotating {
                    if let Some(size) = size {
                        // Translators: Used to describe a non-rotating drive (rotation rate either unknown
                        // or it's a solid-state drive). The %s is the size, e.g. '20 GB'.
                        pgettext_f("disk-non-rotational", "{} Disk", [size])
                    } else {
                        // Translators: Used to describe a non-rotating drive (rotation rate either unknown
                        // or it's a solid-state drive). The drive is either using removable media or its
                        // size not known.
                        pgettext("disk-non-rotational", "Disk")
                    }
                } else if let Some(size) = size {
                    // Translators: Used to describe a hard-disk drive (HDD). The %s is the size, e.g. '20 GB'.
                    pgettext_f("disk-hdd", "{} Hard Disk", [size])
                } else {
                    // Translators: Used to describe a hard-disk drive (HDD) (removable media or size not known)
                    pgettext("disk-hdd", "Hard Disk")
                }
            }
            Some(DriveType::Card) => {
                // Translators: Used to describe a card reader. The %s is the card type e.g. 'CompactFlash'.
                pgettext_f("drive-card-reader", "{} Card Reader", [desc])
            }
            Some(DriveType::Drive) | Some(DriveType::Disk) | Some(DriveType::Disc) => {
                if size.as_ref().is_some_and(|_| !media_removable) {
                    // Translators: Used to describe drive. The first %s is the size e.g. '20 GB' and the
                    // second %s is the drive type e.g. 'Thumb'.
                    pgettext_f(
                        "drive-with-size-and-type",
                        "{} {} Drive",
                        [size.unwrap(), desc],
                    )
                } else {
                    //Translators: Used to describe drive. The first %s is the drive type e.g. 'Thumb'.
                    pgettext_f("drive-with-type", "{} Drive", [desc])
                }
            }
        });

        let hyphenated_connection_bus = drive
            .connection_bus()
            .await
            .ok()
            .filter(|bus| !bus.is_empty())
            .map(|bus| format!("-{}", bus))
            .unwrap_or_default();

        //fallback for icon
        let icon_fallback = if media_removable {
            format!("drive-removable-media{}", hyphenated_connection_bus)
        } else if rotation_rate == RotationRate::NonRotating {
            format!("drive-harddisk-solidstate{}", hyphenated_connection_bus)
        } else {
            format!("drive-harddisk{}", hyphenated_connection_bus)
        };

        let icon_symbolic_fallback = if media_removable {
            format!(
                "drive-removable-media{}-symbolic",
                hyphenated_connection_bus
            )
        } else if rotation_rate == RotationRate::NonRotating {
            format!(
                "drive-harddisk-solidstate{}-symbolic",
                hyphenated_connection_bus
            )
        } else {
            format!("drive-harddisk{}-symbolic", hyphenated_connection_bus)
        };
        self.icon.set_if_none(icon_fallback, icon_symbolic_fallback);

        //fallback for media_icon
        if media_available {
            let media_icon_fallback = if media_removable {
                format!("drive-removable-media{}", hyphenated_connection_bus)
            } else if rotation_rate == RotationRate::NonRotating {
                format!("drive-harddisk-solidstate{}", hyphenated_connection_bus)
            } else {
                format!("drive-harddisk{}", hyphenated_connection_bus)
            };

            let media_icon_symbolic_fallback = if media_removable {
                format!(
                    "drive-removable-media{}-symbolic",
                    hyphenated_connection_bus
                )
            } else if rotation_rate == RotationRate::NonRotating {
                format!(
                    "drive-harddisk-solidstate{}-symbolic",
                    hyphenated_connection_bus
                )
            } else {
                format!("drive-harddisk{}-symbolic", hyphenated_connection_bus)
            };

            self.media_icon
                .set_if_none(media_icon_fallback, media_icon_symbolic_fallback);
        }

        //TODO: refactor
        //prepend a qualifier to the media description, based on the disc state
        if drive.optical_blank().await.unwrap_or_default() {
            // Translators: String used for a blank disc. The %s is the disc type e.g. "CD-RW Disc"
            self.media_description = Some(pgettext_f(
                "optical-media",
                "Blank {}",
                [self.media_description.as_deref().unwrap_or_default()],
            ));
        } else if drive
            .optical_num_audio_tracks()
            .await
            .is_ok_and(|tracks| tracks > 0)
            && drive
                .optical_num_data_tracks()
                .await
                .is_ok_and(|tracks| tracks > 0)
        {
            // Translators: String used for a mixed disc. The %s is the disc type e.g. "CD-ROM Disc"
            self.media_description = Some(pgettext_f(
                "optical-media",
                "Mixed {}",
                [self.media_description.as_deref().unwrap_or_default()],
            ));
        } else if drive
            .optical_num_audio_tracks()
            .await
            .is_ok_and(|tracks| tracks > 0)
            && drive
                .optical_num_data_tracks()
                .await
                .is_ok_and(|tracks| tracks == 0)
        {
            // Translators: String used for an audio disc. The %s is the disc type e.g. "CD-ROM Disc"
            self.media_description = Some(pgettext_f(
                "optical-media",
                "Audio {}",
                [self.media_description.as_deref().unwrap_or_default()],
            ));
        }

        // Apply UDISKS_NAME, UDISKS_ICON_NAME, UDISKS_SYMBOLIC_ICON_NAME hints, if available
        let block = client.block_for_drive(drive, true).await;
        if let Some(ref block) = block {
            if let Ok(hint) = block.hint_name().await {
                if !hint.is_empty() {
                    self.description = Some(hint.clone());
                    self.media_description = Some(hint);
                }
            }
            if let Ok(hint_icon) = block.hint_icon_name().await {
                if !hint_icon.is_empty() {
                    self.icon.name = Some(hint_icon.clone());
                    self.media_icon.name = Some(hint_icon);
                }
            }
            if let Ok(hint_icon_symbolic) = block.hint_symbolic_icon_name().await {
                if !hint_icon_symbolic.is_empty() {
                    self.icon.name_symbolic = Some(hint_icon_symbolic.clone());
                    self.media_icon.name_symbolic = Some(hint_icon_symbolic);
                }
            }
        }

        let mut block_for_partition = None;
        if let Some(ref partition) = partition {
            // safe to unwrap as the table's object path does not need to be converted
            let object = client.object(partition.inner().path().clone()).unwrap();
            block_for_partition = object.block().await.ok();
        }
        block_for_partition = block_for_partition.or_else(|| block.clone());

        if let Some(partition) = partition {
            // Translators: Used to describe a partition of a drive.
            //                  The %u is the partition number.
            //                  The %s is the description for the drive (e.g. "2 GB Thumb Drive").
            //TODO: pgettext_f does not support non-string C placeholders, so we replace it
            //manually to maintain compatibilty
            self.description = Some(
                pgettext_f(
                    "part-drive",
                    "Partition %u of {}",
                    [self.description.as_deref().unwrap_or_default()],
                )
                .replace(
                    "%u",
                    &partition.number().await.unwrap_or_default().to_string(),
                ),
            )
        }

        //calculate and set one-liner
        if let Some(block) = block {
            if let Ok(drive_revision) = drive.revision().await {
                // Translators: String used for one-liner description of drive.
                //  The first %s is the description of the object (e.g. "80 GB Disk" or "Partition 2 of 2 GB Thumb Drive").
                //  The second %s is the name of the object (e.g. "INTEL SSDSA2MH080G1GC").
                //  The third %s is the fw revision (e.g "45ABX21").
                //  The fourth %s is the special device file (e.g. "/dev/sda").
                self.one_liner = Some(pgettext_f(
                    "one-liner-drive",
                    "{} — {} [{}] ({})",
                    [
                        self.description.as_deref().unwrap_or_default(),
                        self.name.as_deref().unwrap_or_default(),
                        &drive_revision,
                        &block
                            .preferred_device()
                            .await
                            .ok()
                            .and_then(|dev| CString::from_vec_with_nul(dev).ok())
                            .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok())
                            .unwrap_or_default(),
                    ],
                ));
            } else {
                // Translators: String used for one-liner description of drive w/o known fw revision.
                //    The first %s is the description of the object (e.g. "80 GB Disk").
                //    The second %s is the name of the object (e.g. "INTEL SSDSA2MH080G1GC").
                //    The third %s is the special device file (e.g. "/dev/sda").
                self.one_liner = Some(pgettext_f(
                    "one-liner-drive",
                    "{} — {} ({})",
                    [
                        self.description.as_deref().unwrap_or_default(),
                        self.name.as_deref().unwrap_or_default(),
                        //safe to unwrap has been set before if it was none
                        &block_for_partition
                            .unwrap()
                            .preferred_device()
                            .await
                            .ok()
                            .and_then(|dev| CString::from_vec_with_nul(dev).ok())
                            .and_then(|dev| dev.to_str().map(|p| p.to_string()).ok())
                            .unwrap_or_default(),
                    ],
                ));
            }
        }

        self.sort_key = Some(format!(
            "00_drive_{}",
            drive.sort_key().await.unwrap_or_default(),
        ));
    }

    fn format_level(&self, level: error::Result<String>) -> String {
        pgettext(
            "mdraid-desc",
            match level.as_deref() {
                Ok("raid0") => "RAID-0 Array",
                Ok("raid1") => "RAID-1 Array",
                Ok("raid4") => "RAID-4 Array",
                Ok("raid5") => "RAID-5 Array",
                Ok("raid6") => "RAID-6 Array",
                Ok("raid10") => "RAID-10 Array",
                _ => "RAID Array",
            },
        )
    }
}