Extract part of analyse_media_path to cross_common for tests.
[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_log.h"
25 #include "warnings.h"
26 #include <dcp/raw_convert.h>
27 DCPOMATIC_DISABLE_WARNINGS
28 #include <libxml++/libxml++.h>
29 DCPOMATIC_ENABLE_WARNINGS
30 #include <boost/algorithm/string.hpp>
31 #include <iostream>
32
33 #include "i18n.h"
34
35
36 using std::string;
37 using std::vector;
38 using boost::optional;
39
40
41 Drive::Drive (string xml)
42 {
43         cxml::Document doc;
44         doc.read_string (xml);
45         _device = doc.string_child("Device");
46         for (auto i: doc.node_children("MountPoint")) {
47                 _mount_points.push_back (i->content());
48         }
49         _size = doc.number_child<uint64_t>("Size");
50         _vendor = doc.optional_string_child("Vendor");
51         _model = doc.optional_string_child("Model");
52 }
53
54
55 string
56 Drive::as_xml () const
57 {
58         xmlpp::Document doc;
59         auto root = doc.create_root_node ("Drive");
60         root->add_child("Device")->add_child_text(_device);
61         for (auto i: _mount_points) {
62                 root->add_child("MountPoint")->add_child_text(i.string());
63         }
64         root->add_child("Size")->add_child_text(dcp::raw_convert<string>(_size));
65         if (_vendor) {
66                 root->add_child("Vendor")->add_child_text(*_vendor);
67         }
68         if (_model) {
69                 root->add_child("Model")->add_child_text(*_model);
70         }
71
72         return doc.write_to_string("UTF-8");
73 }
74
75
76 string
77 Drive::description () const
78 {
79         char gb[64];
80         snprintf(gb, 64, "%.1f", _size / 1000000000.0);
81
82         string name;
83         if (_vendor) {
84                 name += *_vendor;
85         }
86         if (_model) {
87                 if (name.size() > 0) {
88                         name += " " + *_model;
89                 } else {
90                         name = *_model;
91                 }
92         }
93         if (name.size() == 0) {
94                 name = _("Unknown");
95         }
96
97         return String::compose(_("%1 (%2 GB) [%3]"), name, gb, _device);
98 }
99
100
101 string
102 Drive::log_summary () const
103 {
104         string mp;
105         for (auto i: _mount_points) {
106                 mp += i.string() + ",";
107         }
108         if (mp.empty()) {
109                 mp = "[none]";
110         } else {
111                 mp = mp.substr (0, mp.length() - 1);
112         }
113
114         return String::compose(
115                 "Device %1 mounted on %2 size %3 vendor %4 model %5",
116                 _device, mp, _size, _vendor.get_value_or("[none]"), _model.get_value_or("[none]")
117                         );
118 }
119
120
121
122 /* This is in _common so we can use it in unit tests */
123 optional<OSXMediaPath>
124 analyse_osx_media_path (string path)
125 {
126         using namespace boost::algorithm;
127
128         if (path.find("/IOHDIXController") != string::npos) {
129                 /* This is a disk image, so we completely ignore it */
130                 LOG_DISK_NC("Ignoring this as it seems to be a disk image");
131                 return {};
132         }
133
134         OSXMediaPath mp;
135         if (starts_with(path, "IODeviceTree:")) {
136                 mp.real = true;
137         } else if (starts_with(path, "IOService:")) {
138                 mp.real = false;
139         } else {
140                 return {};
141         }
142
143         vector<string> bits;
144         split(bits, path, boost::is_any_of("/"));
145         for (auto i: bits) {
146                 if (starts_with(i, "PRT")) {
147                         mp.prt = i;
148                 }
149         }
150
151         return mp;
152 }
153
154