Use a simpler way to decide what devices we could write to on macOS.
[dcpomatic.git] / src / lib / cross_common.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "cross.h"
23 #include "compose.hpp"
24 #include "dcpomatic_assert.h"
25 #include "dcpomatic_log.h"
26 #include <dcp/raw_convert.h>
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <libxml++/libxml++.h>
30 LIBDCP_ENABLE_WARNINGS
31 #include <boost/algorithm/string.hpp>
32 #include <iostream>
33
34 #include "i18n.h"
35
36
37 using std::map;
38 using std::string;
39 using std::vector;
40 using boost::optional;
41
42
43 Drive::Drive (string xml)
44 {
45         cxml::Document doc;
46         doc.read_string (xml);
47         _device = doc.string_child("Device");
48         for (auto i: doc.node_children("MountPoint")) {
49                 _mount_points.push_back (i->content());
50         }
51         _size = doc.number_child<uint64_t>("Size");
52         _vendor = doc.optional_string_child("Vendor");
53         _model = doc.optional_string_child("Model");
54 }
55
56
57 string
58 Drive::as_xml () const
59 {
60         xmlpp::Document doc;
61         auto root = doc.create_root_node ("Drive");
62         root->add_child("Device")->add_child_text(_device);
63         for (auto i: _mount_points) {
64                 root->add_child("MountPoint")->add_child_text(i.string());
65         }
66         root->add_child("Size")->add_child_text(dcp::raw_convert<string>(_size));
67         if (_vendor) {
68                 root->add_child("Vendor")->add_child_text(*_vendor);
69         }
70         if (_model) {
71                 root->add_child("Model")->add_child_text(*_model);
72         }
73
74         return doc.write_to_string("UTF-8");
75 }
76
77
78 string
79 Drive::description () const
80 {
81         char gb[64];
82         snprintf(gb, 64, "%.1f", _size / 1000000000.0);
83
84         string name;
85         if (_vendor) {
86                 name += *_vendor;
87         }
88         if (_model) {
89                 if (name.size() > 0) {
90                         name += " " + *_model;
91                 } else {
92                         name = *_model;
93                 }
94         }
95         if (name.size() == 0) {
96                 name = _("Unknown");
97         }
98
99         return String::compose(_("%1 (%2 GB) [%3]"), name, gb, _device);
100 }
101
102
103 string
104 Drive::log_summary () const
105 {
106         string mp;
107         for (auto i: _mount_points) {
108                 mp += i.string() + ",";
109         }
110         if (mp.empty()) {
111                 mp = "[none]";
112         } else {
113                 mp = mp.substr (0, mp.length() - 1);
114         }
115
116         return String::compose(
117                 "Device %1 mounted on %2 size %3 vendor %4 model %5",
118                 _device, mp, _size, _vendor.get_value_or("[none]"), _model.get_value_or("[none]")
119                         );
120 }
121