Store successful DCP encodes.
[dcpomatic.git] / src / lib / analytics.cc
1 /*
2     Copyright (C) 2018 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 "analytics.h"
22 #include "exceptions.h"
23 #include <dcp/raw_convert.h>
24 #include <libcxml/cxml.h>
25 #include <libxml++/libxml++.h>
26 #include <boost/filesystem.hpp>
27 #include <boost/algorithm/string.hpp>
28
29 using std::string;
30 using dcp::raw_convert;
31 using boost::algorithm::trim;
32
33 Analytics* Analytics::_instance;
34 int const Analytics::_current_version = 1;
35
36 Analytics::Analytics ()
37         : _successful_dcp_encodes (0)
38 {
39
40 }
41
42 void
43 Analytics::successful_dcp_encode ()
44 {
45         ++_successful_dcp_encodes;
46         write ();
47 }
48
49 void
50 Analytics::write () const
51 {
52         xmlpp::Document doc;
53         xmlpp::Element* root = doc.create_root_node ("Analytics");
54
55         root->add_child("Version")->add_child_text(raw_convert<string>(_current_version));
56         root->add_child("SuccessfulDCPEncodes")->add_child_text(raw_convert<string>(_successful_dcp_encodes));
57
58         try {
59                 doc.write_to_file_formatted(path("analytics.xml").string());
60         } catch (xmlpp::exception& e) {
61                 string s = e.what ();
62                 trim (s);
63                 throw FileError (s, path("analytics.xml"));
64         }
65 }
66
67 void
68 Analytics::read ()
69 try
70 {
71         cxml::Document f ("Analytics");
72         f.read_file (path("analytics.xml"));
73         _successful_dcp_encodes = f.number_child<int>("SuccessfulDCPEncodes");
74 } catch (...) {
75         /* Never mind */
76 }
77
78 Analytics*
79 Analytics::instance ()
80 {
81         if (!_instance) {
82                 _instance = new Analytics();
83                 _instance->read();
84         }
85
86         return _instance;
87 }