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