main fix: when transport stops, clear per-region per-playlist note trackers even...
[ardour.git] / libs / ardour / callback.cc
1 #include <iostream>
2 #include <string>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <cstdlib>
9 #include <sys/utsname.h>
10 #include <curl/curl.h>
11
12 #include <glibmm/fileutils.h>
13 #include <glibmm/miscutils.h>
14
15 #include "pbd/compose.h"
16 #include "pbd/strsplit.h"
17 #include "pbd/convert.h"
18
19 #include "ardour/callback.h"
20 #include "ardour/filesystem_paths.h"
21
22 using namespace std;
23
24 #define PING_URL "http://ardour.org/pingback/versioncheck"
25 #define OFF_THE_HOOK ".offthehook"
26
27 static size_t
28 curl_write_data (char *bufptr, size_t size, size_t nitems, void *ptr)
29 {
30         /* we know its a string */
31
32         string* sptr = (string*) ptr;
33
34         for (size_t i = 0; i < nitems; ++i) {
35                 for (size_t n = 0; n < size; ++n) {
36                         if (*bufptr == '\n') {
37                                 break;
38                         }
39
40                         (*sptr) += *bufptr++;
41                 }
42         }
43
44         return size * nitems;
45 }
46
47 static string
48 watermark ()
49 {
50         return string();
51 }
52
53 void
54 block_mothership ()
55 {
56         string hangup = Glib::build_filename (ARDOUR::user_config_directory().to_string(), OFF_THE_HOOK);
57         int fd;
58         if ((fd = ::open (hangup.c_str(), O_RDWR|O_CREAT, 0600)) >= 0) {
59                 close (fd);
60         }
61 }
62
63 void
64 unblock_mothership ()
65 {
66         string hangup = Glib::build_filename (ARDOUR::user_config_directory().to_string(), OFF_THE_HOOK);
67         ::unlink (hangup.c_str());
68 }
69
70 bool
71 mothership_blocked ()
72 {
73         string hangup = Glib::build_filename (ARDOUR::user_config_directory().to_string(), OFF_THE_HOOK);
74         return Glib::file_test (hangup, Glib::FILE_TEST_EXISTS);
75 }
76
77 void
78 call_the_mothership (const string& version)
79 {
80         /* check if the user says never to do this
81          */
82
83         if (mothership_blocked()) {
84                 return;
85         }
86
87         CURL* c;
88         struct utsname utb;
89         std::string versionstr;
90
91         if (uname (&utb)) {
92                 return;
93         }
94
95         curl_global_init (CURL_GLOBAL_NOTHING);
96
97         c = curl_easy_init ();
98
99         string data;
100         string wm;
101
102         data = string_compose ("version=%1&platform=%2 %3 %4", version, utb.sysname, utb.release, utb.machine);
103
104         wm = watermark();
105         if (!wm.empty()) {
106                 data += string_compose ("&watermark=%1", wm);
107         }
108
109         curl_easy_setopt(c, CURLOPT_POSTFIELDS, data.c_str());
110         curl_easy_setopt(c, CURLOPT_URL, PING_URL);
111         curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, curl_write_data);
112         curl_easy_setopt(c, CURLOPT_WRITEDATA, &versionstr);
113
114         std::cerr << "Callback to ardour.org ...\n";
115
116         char errbuf[CURL_ERROR_SIZE];
117         curl_easy_setopt(c, CURLOPT_ERRORBUFFER, errbuf);
118
119         if (curl_easy_perform (c) == 0) {
120                 cerr << "Current release is " << versionstr << endl;
121
122                 vector<string> ours;
123                 vector<string> current;
124
125                 split (version, ours, '.');
126                 split (versionstr, current, '.');
127
128                 if (ours.size() == 3 && current.size() == 3) {
129
130                         int ours_n[3];
131                         int current_n[3];
132
133                         using namespace PBD;
134
135                         ours_n[0] = atoi (ours[0]);
136                         ours_n[1] = atoi (ours[1]);
137                         ours_n[2] = atoi (ours[2]);
138
139                         current_n[0] = atoi (current[0]);
140                         current_n[1] = atoi (current[1]);
141                         current_n[2] = atoi (current[2]);
142
143                         if (ours_n[0] < current_n[0] ||
144                             ((ours_n[0] == current_n[0]) && (ours_n[1] < current_n[1])) ||
145                             ((ours_n[0] == current_n[0]) && (ours_n[1] == current_n[1]) && (ours_n[2] < current_n[2]))) {
146                                 cerr << "TOO OLD\n";
147                         } else {
148                                 cerr << "CURRENT\n";
149                         }
150                 } else {
151                         cerr << "Unusual local version: " << version << endl;
152                 }
153         }
154
155         curl_easy_cleanup (c);
156 }