Revert mac rdisk vs disk stuff as I'm not convinced it makes much difference.
[dcpomatic.git] / src / lib / cross_common.cc
1 /*
2     Copyright (C) 2012-2020 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 #include "cross.h"
22 #include "compose.hpp"
23 #include "dcpomatic_log.h"
24 #include <dcp/raw_convert.h>
25 #include <boost/foreach.hpp>
26 #include <libxml++/libxml++.h>
27 #include <iostream>
28
29 #include "i18n.h"
30
31 using std::string;
32
33 Drive::Drive (string xml)
34 {
35         cxml::Document doc;
36         doc.read_string (xml);
37         _device = doc.string_child("Device");
38         BOOST_FOREACH (cxml::ConstNodePtr i, doc.node_children("MountPoint")) {
39                 _mount_points.push_back (i->content());
40         }
41         _size = doc.number_child<uint64_t>("Size");
42         _vendor = doc.optional_string_child("Vendor");
43         _model = doc.optional_string_child("Model");
44 }
45
46
47 string
48 Drive::as_xml () const
49 {
50         xmlpp::Document doc;
51         xmlpp::Element* root = doc.create_root_node ("Drive");
52         root->add_child("Device")->add_child_text(_device);
53         BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
54                 root->add_child("MountPoint")->add_child_text(i.string());
55         }
56         root->add_child("Size")->add_child_text(dcp::raw_convert<string>(_size));
57         if (_vendor) {
58                 root->add_child("Vendor")->add_child_text(*_vendor);
59         }
60         if (_model) {
61                 root->add_child("Model")->add_child_text(*_model);
62         }
63
64         std::cout << "xml is " << doc.write_to_string("UTF-8") << "\n";
65         return doc.write_to_string("UTF-8");
66 }
67
68
69 string
70 Drive::description () const
71 {
72         char gb[64];
73         snprintf(gb, 64, "%.1f", _size / 1000000000.0);
74
75         string name;
76         if (_vendor) {
77                 name += *_vendor;
78         }
79         if (_model) {
80                 if (name.size() > 0) {
81                         name += " " + *_model;
82                 } else {
83                         name = *_model;
84                 }
85         }
86         if (name.size() == 0) {
87                 name = _("Unknown");
88         }
89
90         return String::compose(_("%1 (%2 GB) [%3]"), name, gb, _device);
91 }
92
93 string
94 Drive::log_summary () const
95 {
96         string mp;
97         BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
98                 mp += i.string() + ",";
99         }
100         if (mp.empty()) {
101                 mp = "[none]";
102         } else {
103                 mp = mp.substr (0, mp.length() - 1);
104         }
105
106         return String::compose(
107                 "Device %1 mounted on %2 size %3 vendor %4 model %5",
108                 _device, mp, _size, _vendor.get_value_or("[none]"), _model.get_value_or("[none]")
109                         );
110 }