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