Surveyer.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #include "Surveyer.h"
16 
17 #include <iostream>
18 
19 #include <QNetworkAccessManager>
20 
21 #include <QSettings>
22 #include <QMessageBox>
23 #include <QDesktopServices>
24 #include <QPushButton>
25 #include <QUrl>
26 
27 #include "../version.h"
28 
29 #include "transform/TransformFactory.h"
30 #include "plugin/PluginIdentifier.h"
31 
33  m_config(config),
34  m_httpFailed(false),
35  m_reply(nullptr),
36  m_nm(new QNetworkAccessManager)
37 {
38  QSettings settings;
39  settings.beginGroup("Survey");
40  if (!settings.contains(m_config.countdownKey)) {
41  settings.setValue(m_config.countdownKey, m_config.countdownFrom);
42  settings.endGroup();
43  return;
44  }
45  int countdown = settings.value(m_config.countdownKey).toInt();
46  if (countdown == 0) {
47  // The countdown value will now remain 0 until we have
48  // successfully tested for a survey and offered it to the
49  // user. If the survey doesn't exist any more, then we'll
50  // simply never present it to the user and the countdown will
51  // remain 0 forever. If the survey does exist, then we offer
52  // the user the chance to respond to it and (regardless of
53  // whether they want to or not) set the countdown to -1 so
54  // that it is never offered again.
55  QUrl url(QString("http://%1/%2")
56  .arg(m_config.hostname).arg(m_config.testPath));
57  SVDEBUG << "Surveyer: Test URL is " << url << endl;
58  m_reply = m_nm->get(QNetworkRequest(url));
59  connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
60  this, SLOT(error(QNetworkReply::NetworkError)));
61  connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
62  } else if (countdown > 0) {
63  settings.setValue(m_config.countdownKey, countdown-1);
64  }
65  settings.endGroup();
66 }
67 
69 {
70  if (m_reply) {
71  m_reply->abort();
72  m_reply->deleteLater();
73  }
74  delete m_nm;
75 }
76 
77 void
78 Surveyer::error(QNetworkReply::NetworkError)
79 {
80  SVDEBUG << "Surveyer: error: " << m_reply->errorString() << endl;
81  m_httpFailed = true;
82 }
83 
84 void
86 {
87  if (m_httpFailed) return;
88 
89  QMessageBox mb(dynamic_cast<QWidget *>(parent()));
90  mb.setWindowTitle(m_config.title);
91  mb.setText(m_config.text);
92 
93  QPushButton *yes =
94  mb.addButton(m_config.acceptLabel, QMessageBox::ActionRole);
95  mb.addButton(m_config.rejectLabel, QMessageBox::RejectRole);
96 
97  mb.exec();
98 
99  QSettings settings;
100  settings.beginGroup("Survey");
101  settings.setValue(m_config.countdownKey, -1);
102  settings.endGroup();
103 
104  if (mb.clickedButton() == yes) {
105  QString svarg = SV_VERSION;
106  QString platformarg = "unknown";
107 #ifdef Q_OS_WIN32
108  platformarg = "win32";
109 #else
110 #ifdef Q_OS_MAC
111  platformarg = "osx";
112 #else
113  platformarg = "posix";
114 #endif
115 #endif
116  QString plugsarg;
117  TransformFactory *tf = TransformFactory::getInstance();
118  if (tf) {
119  TransformList tl = tf->getAllTransformDescriptions();
120  std::set<QString> packages;
121  for (size_t i = 0; i < tl.size(); ++i) {
122  TransformId id = tl[i].identifier;
123  Transform t;
124  t.setIdentifier(id);
125  QString plugid = t.getPluginIdentifier();
126  QString type, soname, label;
127  PluginIdentifier::parseIdentifier(plugid, type, soname, label);
128  if (type == "vamp") packages.insert(soname);
129  }
130  for (std::set<QString>::const_iterator i = packages.begin();
131  i != packages.end(); ++i) {
132  if (plugsarg != "") plugsarg = plugsarg + ",";
133  plugsarg = plugsarg + *i;
134  }
135  }
136 
138  QDesktopServices::openUrl(QUrl(QString("http://%1/%2?sv=%3&plugs=%4&platform=%5").arg(m_config.hostname).arg(m_config.surveyPath).arg(svarg).arg(plugsarg).arg(platformarg)));
139  } else {
140  QDesktopServices::openUrl(QUrl(QString("http://%1/%2?sv=%3").arg(m_config.hostname).arg(m_config.surveyPath).arg(svarg)));
141  }
142  }
143 }
144 
145 
QString rejectLabel
Definition: Surveyer.h:39
bool m_httpFailed
Definition: Surveyer.h:52
QString acceptLabel
Definition: Surveyer.h:38
void finished()
Definition: Surveyer.cpp:85
void error(QNetworkReply::NetworkError)
Definition: Surveyer.cpp:78
QString countdownKey
Definition: Surveyer.h:34
QNetworkReply * m_reply
Definition: Surveyer.h:53
Surveyer(Config config)
Definition: Surveyer.cpp:32
bool includeSystemInfo
Definition: Surveyer.h:40
QNetworkAccessManager * m_nm
Definition: Surveyer.h:54
Config m_config
Definition: Surveyer.h:51
QString title
Definition: Surveyer.h:36
QString text
Definition: Surveyer.h:37
virtual ~Surveyer()
Definition: Surveyer.cpp:68
QString testPath
Definition: Surveyer.h:32
QString hostname
Definition: Surveyer.h:31
QString surveyPath
Definition: Surveyer.h:33