comparison main/Surveyer.cpp @ 662:6f06094daba0

Restore and update old Surveyer class (survey itself is not there yet)
author Chris Cannam
date Tue, 26 Nov 2013 11:41:12 +0000
parents
children 55efa5a18814
comparison
equal deleted inserted replaced
661:9d6eadfd390e 662:6f06094daba0
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 /*
16 This is a modified version of a source file from the
17 Rosegarden MIDI and audio sequencer and notation editor.
18 This file copyright 2000-2009 Chris Cannam.
19 */
20
21 #include "Surveyer.h"
22
23 #include <iostream>
24
25 #include <QNetworkAccessManager>
26
27 #include <QSettings>
28 #include <QMessageBox>
29 #include <QDesktopServices>
30 #include <QPushButton>
31 #include <QUrl>
32
33 #include "version.h"
34
35 #include "transform/TransformFactory.h"
36 #include "plugin/PluginIdentifier.h"
37
38 Surveyer::Surveyer(QString hostname, QString testPath, QString surveyPath) :
39 m_httpFailed(false),
40 m_hostname(hostname),
41 m_testPath(testPath),
42 m_surveyPath(surveyPath),
43 m_nm(new QNetworkAccessManager)
44 {
45 QSettings settings;
46 settings.beginGroup("Survey");
47 if (!settings.contains("countdown")) {
48 settings.setValue("countdown", 15);
49 settings.endGroup();
50 return;
51 }
52 int countdown = settings.value("countdown").toInt();
53 if (countdown == 0) {
54 // The countdown value will now remain 0 until we have
55 // successfully tested for a survey and offered it to the
56 // user. If the survey doesn't exist any more, then we'll
57 // simply never present it to the user and the countdown will
58 // remain 0 forever. If the survey does exist, then we offer
59 // the user the chance to respond to it and (regardless of
60 // whether they want to or not) set the countdown to -1 so
61 // that it is never offered again.
62 QUrl url(QString("http://%1/%2").arg(m_hostname).arg(m_testPath));
63 std::cerr << "Surveyer: Test URL is " << url << std::endl;
64 m_reply = m_nm->get(QNetworkRequest(url));
65 connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)),
66 this, SLOT(error(QNetworkReply::NetworkError)));
67 connect(m_reply, SIGNAL(finished()), this, SLOT(finished()));
68 } else if (countdown > 0) {
69 settings.setValue("countdown", countdown-1);
70 }
71 settings.endGroup();
72 }
73
74 Surveyer::~Surveyer()
75 {
76 if (m_reply) {
77 m_reply->abort();
78 m_reply->deleteLater();
79 }
80 delete m_nm;
81 }
82
83 void
84 Surveyer::error(QNetworkReply::NetworkError)
85 {
86 std::cerr << "Surveyer: error: " << m_reply->errorString() << std::endl;
87 m_httpFailed = true;
88 }
89
90 void
91 Surveyer::finished()
92 {
93 if (m_httpFailed) return;
94
95 QString title = "Sonic Visualiser - User Survey";
96 QString text = "<h3>Sonic Visualiser: Take part in our survey!</h3><p>We at Queen Mary, University of London are running a short survey for users of Sonic Visualiser. We are trying to find out how useful Sonic Visualiser is to people, and what we can do to improve it.</p><p>We do not ask for any personal information, and it should only take five minutes.</p><p>Would you like to take part?</p>";
97
98 QMessageBox mb(dynamic_cast<QWidget *>(parent()));
99 mb.setWindowTitle(title);
100 mb.setText(text);
101
102 QPushButton *yes = mb.addButton(tr("Yes! Take me to the survey"), QMessageBox::ActionRole);
103 mb.addButton(tr("No, thanks"), QMessageBox::RejectRole);
104
105 mb.exec();
106
107 QSettings settings;
108 settings.beginGroup("Survey");
109 settings.setValue("countdown", -1);
110 settings.endGroup();
111
112 if (mb.clickedButton() == yes) {
113 QString svarg = SV_VERSION;
114 QString platformarg = "unknown";
115 #ifdef Q_OS_WIN32
116 platformarg = "win32";
117 #else
118 #ifdef Q_OS_MAC
119 platformarg = "osx";
120 #else
121 platformarg = "posix";
122 #endif
123 #endif
124 QString plugsarg;
125 TransformFactory *tf = TransformFactory::getInstance();
126 if (tf) {
127 TransformList tl = tf->getAllTransformDescriptions();
128 std::set<QString> packages;
129 for (size_t i = 0; i < tl.size(); ++i) {
130 TransformId id = tl[i].identifier;
131 Transform t;
132 t.setIdentifier(id);
133 QString plugid = t.getPluginIdentifier();
134 QString type, soname, label;
135 PluginIdentifier::parseIdentifier(plugid, type, soname, label);
136 if (type == "vamp") packages.insert(soname);
137 }
138 for (std::set<QString>::const_iterator i = packages.begin();
139 i != packages.end(); ++i) {
140 if (plugsarg != "") plugsarg = plugsarg + ",";
141 plugsarg = plugsarg + *i;
142 }
143 }
144 QDesktopServices::openUrl(QUrl(QString("http://%1/%2?sv=%3&plugs=%4&platform=%5").arg(m_hostname).arg(m_surveyPath).arg(svarg).arg(plugsarg).arg(platformarg)));
145 }
146 }
147
148