annotate main/Surveyer.cpp @ 2535:62b1a3a242ee

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