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