replace ::cast_dynamic() with relevant ActionManager::get_*_action() calls
[ardour.git] / gtk2_ardour / pingback.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3     Inspired by code from Ben Loftis @ Harrison Consoles
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <string>
22 #include <cstring>
23
24 #ifdef PLATFORM_WINDOWS
25 #include <windows.h>
26 #include <glibmm.h>
27 #else
28 #include <sys/utsname.h>
29 #endif
30
31 #include "pbd/gstdio_compat.h"
32 #include <glibmm/miscutils.h>
33
34 #include "pbd/compose.h"
35 #include "pbd/pthread_utils.h"
36
37 #include "ardour/filesystem_paths.h"
38 #include "ardour/rc_configuration.h"
39
40 #include "ardour_http.h"
41 #include "pingback.h"
42 #include "utils.h"
43
44 using std::string;
45 using namespace ARDOUR;
46
47 struct ping_call {
48     std::string version;
49     std::string announce_path;
50
51     ping_call (const std::string& v, const std::string& a)
52             : version (v), announce_path (a) {}
53 };
54
55 #ifdef PLATFORM_WINDOWS
56 static bool
57 _query_registry (const char *regkey, const char *regval, std::string &rv) {
58         HKEY key;
59         DWORD size = PATH_MAX;
60         char tmp[PATH_MAX+1];
61
62         if (   (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ, &key))
63             && (ERROR_SUCCESS == RegQueryValueExA (key, regval, 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
64                  )
65         {
66                 rv = Glib::locale_to_utf8 (tmp);
67                 return true;
68         }
69
70         if (   (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ | KEY_WOW64_32KEY, &key))
71             && (ERROR_SUCCESS == RegQueryValueExA (key, regval, 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
72                  )
73         {
74                 rv = Glib::locale_to_utf8 (tmp);
75                 return true;
76         }
77
78         return false;
79 }
80 #endif
81
82
83 static void*
84 _pingback (void *arg)
85 {
86         ArdourCurl::HttpGet h;
87
88         ping_call* cm = static_cast<ping_call*> (arg);
89         string return_str;
90         //initialize curl
91
92         string url;
93
94 #ifdef __APPLE__
95         url = Config->get_osx_pingback_url ();
96 #elif defined PLATFORM_WINDOWS
97         url = Config->get_windows_pingback_url ();
98 #else
99         url = Config->get_linux_pingback_url ();
100 #endif
101
102         if (url.compare (0, 4, "http") != 0) {
103                 delete cm;
104                 return 0;
105         }
106
107         char* v = h.escape (cm->version.c_str(), cm->version.length());
108         url += v;
109         url += '?';
110         h.free (v);
111
112 #ifndef PLATFORM_WINDOWS
113         struct utsname utb;
114
115         if (uname (&utb)) {
116                 delete cm;
117                 return 0;
118         }
119
120         //string uts = string_compose ("%1 %2 %3 %4", utb.sysname, utb.release, utb.version, utb.machine);
121         string s;
122         char* query;
123
124         query = h.escape (utb.sysname, strlen (utb.sysname));
125         s = string_compose ("s=%1", query);
126         url += s;
127         url += '&';
128         h.free (query);
129
130         query = h.escape (utb.release, strlen (utb.release));
131         s = string_compose ("r=%1", query);
132         url += s;
133         url += '&';
134         h.free (query);
135
136         query = h.escape (utb.machine, strlen (utb.machine));
137         s = string_compose ("m=%1", query);
138         url += s;
139         h.free (query);
140 #else
141         std::string val;
142         if (_query_registry("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "ProductName", val)) {
143                 char* query = h.escape (val.c_str(), strlen (val.c_str()));
144                 url += "r=";
145                 url += query;
146                 url += '&';
147                 h.free (query);
148         } else {
149                 url += "r=&";
150         }
151
152         if (_query_registry("Hardware\\Description\\System\\CentralProcessor\\0", "Identifier", val)) {
153                 // remove "Family X Model YY Stepping Z" tail
154                 size_t cut = val.find (" Family ");
155                 if (string::npos != cut) {
156                         val = val.substr (0, cut);
157                 }
158                 char* query = h.escape (val.c_str(), strlen (val.c_str()));
159                 url += "m=";
160                 url += query;
161                 url += '&';
162                 h.free (query);
163         } else {
164                 url += "m=&";
165         }
166
167 # if ( defined(__x86_64__) || defined(_M_X64) )
168         url += "s=Windows64";
169 # else
170         url += "s=Windows32";
171 # endif
172
173 #endif /* PLATFORM_WINDOWS */
174
175         return_str = h.get (url, false);
176
177         if (!return_str.empty ()) {
178                 if ( return_str.length() > 140 ) { // like a tweet :)
179                         std::cerr << "Announcement string is too long (probably behind a proxy)." << std::endl;
180                 } else {
181                         std::cerr << "Announcement is: " << return_str << std::endl;
182
183                         //write announcements to local file, even if the
184                         //announcement is empty
185
186                         FILE* fout = g_fopen (cm->announce_path.c_str(), "wb");
187
188                         if (fout) {
189                                 fwrite (return_str.c_str(), sizeof(char), return_str.length (), fout);
190                                 fclose (fout);
191                         }
192                 }
193         } else {
194 #ifndef NDEBUG
195                 std::cerr << "pingback: " << h.error () << std::endl;
196 #endif
197         }
198
199         delete cm;
200         return 0;
201 }
202
203 namespace ARDOUR {
204
205 void pingback (const string& version, const string& announce_path)
206 {
207         if (ARDOUR_UI_UTILS::running_from_source_tree ()) {
208                 /* we don't ping under these conditions, because the user is
209                    probably just paul or robin :)
210                 */
211                 return;
212         }
213
214         ping_call* cm = new ping_call (version, announce_path);
215         pthread_t thread;
216
217         pthread_create_and_store ("pingback", &thread, _pingback, cm);
218 }
219
220 }