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