Trying to create export audio encoders with between 9 and 15 channels
[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 "compose.hpp"
24 #include "warnings.h"
25 #include <dcp/raw_convert.h>
26 #include <libcxml/cxml.h>
27 DCPOMATIC_DISABLE_WARNINGS
28 #include <libxml++/libxml++.h>
29 DCPOMATIC_ENABLE_WARNINGS
30 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string.hpp>
32
33 #include "i18n.h"
34
35 using std::string;
36 using dcp::raw_convert;
37 using boost::algorithm::trim;
38
39 Analytics* Analytics::_instance;
40 int const Analytics::_current_version = 1;
41
42 Analytics::Analytics ()
43         : _successful_dcp_encodes (0)
44 {
45
46 }
47
48 void
49 Analytics::successful_dcp_encode ()
50 {
51         ++_successful_dcp_encodes;
52         write ();
53
54         if (_successful_dcp_encodes == 20) {
55                 emit (
56                         boost::bind(
57                                 boost::ref(Message),
58                                 _("Congratulations!"),
59                                 String::compose (_(
60                                         "<h2>You have made %1 DCPs with DCP-o-matic!</h2>"
61                                         "<img width=\"20%%\" src=\"memory:me.jpg\" align=\"center\">"
62                                         "<p>Hello. I'm Carl and I'm the "
63                                         "developer of DCP-o-matic. I work on it in my spare time (with the help "
64                                         "of a fine volunteer team of testers and translators) and I release it "
65                                         "as free software."
66
67                                         "<p>If you find DCP-o-matic useful, please consider a donation to the "
68                                         "project. Financial support will help me to spend more "
69                                         "time developing DCP-o-matic and making it better!"
70
71                                         "<p><ul>"
72                                         "<li><a href=\"https://dcpomatic.com/donate_amount?amount=40\">Go to Paypal to donate €40</a>"
73                                         "<li><a href=\"https://dcpomatic.com/donate_amount?amount=20\">Go to Paypal to donate €20</a>"
74                                         "<li><a href=\"https://dcpomatic.com/donate_amount?amount=10\">Go to Paypal to donate €10</a>"
75                                         "</ul>"
76
77                                         "<p>Thank you!"), _successful_dcp_encodes
78                                         )
79                                 )
80                         );
81         }
82 }
83
84 void
85 Analytics::write () const
86 {
87         xmlpp::Document doc;
88         xmlpp::Element* root = doc.create_root_node ("Analytics");
89
90         root->add_child("Version")->add_child_text(raw_convert<string>(_current_version));
91         root->add_child("SuccessfulDCPEncodes")->add_child_text(raw_convert<string>(_successful_dcp_encodes));
92
93         try {
94                 doc.write_to_file_formatted(path("analytics.xml").string());
95         } catch (xmlpp::exception& e) {
96                 string s = e.what ();
97                 trim (s);
98                 throw FileError (s, path("analytics.xml"));
99         }
100 }
101
102 void
103 Analytics::read ()
104 try
105 {
106         cxml::Document f ("Analytics");
107         f.read_file (path("analytics.xml"));
108         _successful_dcp_encodes = f.number_child<int>("SuccessfulDCPEncodes");
109 } catch (...) {
110         /* Never mind */
111 }
112
113 Analytics*
114 Analytics::instance ()
115 {
116         if (!_instance) {
117                 _instance = new Analytics();
118                 _instance->read();
119         }
120
121         return _instance;
122 }