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