C++11 tidying.
[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 <iostream>
31
32 #include "i18n.h"
33
34
35 using std::string;
36
37
38 Drive::Drive (string xml)
39 {
40         cxml::Document doc;
41         doc.read_string (xml);
42         _device = doc.string_child("Device");
43         for (auto i: doc.node_children("MountPoint")) {
44                 _mount_points.push_back (i->content());
45         }
46         _size = doc.number_child<uint64_t>("Size");
47         _vendor = doc.optional_string_child("Vendor");
48         _model = doc.optional_string_child("Model");
49 }
50
51
52 string
53 Drive::as_xml () const
54 {
55         xmlpp::Document doc;
56         auto root = doc.create_root_node ("Drive");
57         root->add_child("Device")->add_child_text(_device);
58         for (auto i: _mount_points) {
59                 root->add_child("MountPoint")->add_child_text(i.string());
60         }
61         root->add_child("Size")->add_child_text(dcp::raw_convert<string>(_size));
62         if (_vendor) {
63                 root->add_child("Vendor")->add_child_text(*_vendor);
64         }
65         if (_model) {
66                 root->add_child("Model")->add_child_text(*_model);
67         }
68
69         return doc.write_to_string("UTF-8");
70 }
71
72
73 string
74 Drive::description () const
75 {
76         char gb[64];
77         snprintf(gb, 64, "%.1f", _size / 1000000000.0);
78
79         string name;
80         if (_vendor) {
81                 name += *_vendor;
82         }
83         if (_model) {
84                 if (name.size() > 0) {
85                         name += " " + *_model;
86                 } else {
87                         name = *_model;
88                 }
89         }
90         if (name.size() == 0) {
91                 name = _("Unknown");
92         }
93
94         return String::compose(_("%1 (%2 GB) [%3]"), name, gb, _device);
95 }
96
97
98 string
99 Drive::log_summary () const
100 {
101         string mp;
102         for (auto i: _mount_points) {
103                 mp += i.string() + ",";
104         }
105         if (mp.empty()) {
106                 mp = "[none]";
107         } else {
108                 mp = mp.substr (0, mp.length() - 1);
109         }
110
111         return String::compose(
112                 "Device %1 mounted on %2 size %3 vendor %4 model %5",
113                 _device, mp, _size, _vendor.get_value_or("[none]"), _model.get_value_or("[none]")
114                         );
115 }