[tracking] Refactor creation of jobs

- Let the jobs handle their own styling and handling, simplify
  the ViewStep code.
This commit is contained in:
Adriaan de Groot 2020-05-19 10:42:25 +02:00
parent 309b2f872d
commit 49e66b11a2
4 changed files with 86 additions and 36 deletions

View File

@ -18,6 +18,8 @@
#include "TrackingJobs.h" #include "TrackingJobs.h"
#include "Config.h"
#include "network/Manager.h" #include "network/Manager.h"
#include "utils/CalamaresUtilsSystem.h" #include "utils/CalamaresUtilsSystem.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@ -72,6 +74,44 @@ TrackingInstallJob::exec()
return Calamares::JobResult::ok(); return Calamares::JobResult::ok();
} }
void
TrackingInstallJob::addJob( Calamares::JobList& list, InstallTrackingConfig* config )
{
if ( config->isEnabled() )
{
QString installUrl = config->installTrackingUrl();
const auto* s = CalamaresUtils::System::instance();
QString memory, disk;
memory.setNum( s->getTotalMemoryB().first );
disk.setNum( s->getTotalDiskB() );
installUrl.replace( "$CPU", s->getCpuDescription() ).replace( "$MEMORY", memory ).replace( "$DISK", disk );
cDebug() << Logger::SubEntry << "install-tracking URL" << installUrl;
list.append( Calamares::job_ptr( new TrackingInstallJob( installUrl ) ) );
}
}
void
TrackingMachineJob::addJob( Calamares::JobList& list, MachineTrackingConfig* config )
{
if ( config->isEnabled() )
{
const auto style = config->machineTrackingStyle();
if ( style == "neon" )
{
list.append( Calamares::job_ptr( new TrackingMachineNeonJob() ) );
}
else
{
cWarning() << "Unsupported machine tracking style" << style;
}
}
}
QString QString
TrackingMachineNeonJob::prettyName() const TrackingMachineNeonJob::prettyName() const
{ {

View File

@ -16,13 +16,35 @@
* along with Calamares. If not, see <http://www.gnu.org/licenses/>. * along with Calamares. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef TRACKINGJOBS #ifndef TRACKING_TRACKINGJOBS_H
#define TRACKINGJOBS #define TRACKING_TRACKINGJOBS_H
#include "Job.h" #include "Job.h"
class InstallTrackingConfig;
class MachineTrackingConfig;
class QSemaphore; class QSemaphore;
/** @section Tracking Jobs
*
* The tracking jobs do the actual work of configuring tracking on the
* target machine. Tracking jobs may have *styles*, variations depending
* on the distro or environment of the target system. At the root of
* each family of tracking jobs (installation, machine, user) there is
* a class with static method `addJob()` that takes the configuration
* information from the relevant Config sub-object and optionally
* adds the right job (subclass!) to the list of jobs.
*/
/** @brief Install-tracking job (gets a URL)
*
* The install-tracking job (there is only one kind) does a GET
* on a configured URL with some additional information about
* the machine (if configured into the URL).
*
* No persistent tracking is done.
*/
class TrackingInstallJob : public Calamares::Job class TrackingInstallJob : public Calamares::Job
{ {
Q_OBJECT Q_OBJECT
@ -35,11 +57,25 @@ public:
QString prettyStatusMessage() const override; QString prettyStatusMessage() const override;
Calamares::JobResult exec() override; Calamares::JobResult exec() override;
static void addJob( Calamares::JobList& list, InstallTrackingConfig* config );
private: private:
const QString m_url; const QString m_url;
}; };
class TrackingMachineNeonJob : public Calamares::Job /** @brief Base class for machine-tracking jobs
*
* Machine-tracking configuraiton depends on the distro / style of machine
* being tracked, so it has subclasses to switch on the relevant kind
* of tracking. A machine is tracked persistently.
*/
class TrackingMachineJob : public Calamares::Job
{
public:
static void addJob( Calamares::JobList& list, MachineTrackingConfig* config );
};
class TrackingMachineNeonJob : public TrackingMachineJob
{ {
Q_OBJECT Q_OBJECT
public: public:

View File

@ -165,7 +165,7 @@ TrackingPage::setTrackingPolicy( TrackingType t, QString url )
} }
else else
{ {
connect( button, &QToolButton::clicked, [url] { QDesktopServices::openUrl( url ); } ); connect( button, &QToolButton::clicked, [ url ] { QDesktopServices::openUrl( url ); } );
cDebug() << "Tracking policy" << int( t ) << "set to" << url; cDebug() << "Tracking policy" << int( t ) << "set to" << url;
} }
else else
@ -186,7 +186,7 @@ TrackingPage::setGeneralPolicy( QString url )
ui->generalPolicyLabel->show(); ui->generalPolicyLabel->show();
ui->generalPolicyLabel->setTextInteractionFlags( Qt::TextBrowserInteraction ); ui->generalPolicyLabel->setTextInteractionFlags( Qt::TextBrowserInteraction );
ui->generalPolicyLabel->show(); ui->generalPolicyLabel->show();
connect( ui->generalPolicyLabel, &QLabel::linkActivated, [url] { QDesktopServices::openUrl( url ); } ); connect( ui->generalPolicyLabel, &QLabel::linkActivated, [ url ] { QDesktopServices::openUrl( url ); } );
} }
} }

View File

@ -106,45 +106,19 @@ void
TrackingViewStep::onLeave() TrackingViewStep::onLeave()
{ {
cDebug() << "Install tracking:" << m_config->installTracking()->isEnabled(); cDebug() << "Install tracking:" << m_config->installTracking()->isEnabled();
cDebug() << "Machine tracking:" << m_config->machineTracking()->isEnabled(); cDebug() << Logger::SubEntry << "Machine tracking:" << m_config->machineTracking()->isEnabled();
cDebug() << " User tracking:" << m_config->userTracking()->isEnabled(); cDebug() << Logger::SubEntry << " User tracking:" << m_config->userTracking()->isEnabled();
} }
Calamares::JobList Calamares::JobList
TrackingViewStep::jobs() const TrackingViewStep::jobs() const
{ {
Calamares::JobList l;
cDebug() << "Creating tracking jobs .."; cDebug() << "Creating tracking jobs ..";
if ( m_config->installTracking()->isEnabled() )
{
QString installUrl = m_config->installTracking()->installTrackingUrl();
const auto* s = CalamaresUtils::System::instance();
QString memory, disk; Calamares::JobList l;
memory.setNum( s->getTotalMemoryB().first ); TrackingInstallJob::addJob( l, m_config->installTracking() );
disk.setNum( s->getTotalDiskB() ); TrackingMachineJob::addJob( l, m_config->machineTracking() );
installUrl.replace( "$CPU", s->getCpuDescription() ).replace( "$MEMORY", memory ).replace( "$DISK", disk );
cDebug() << Logger::SubEntry << "install-tracking URL" << installUrl;
l.append( Calamares::job_ptr( new TrackingInstallJob( installUrl ) ) );
}
if ( m_config->machineTracking()->isEnabled() )
{
const auto style = m_config->machineTracking()->machineTrackingStyle();
if ( style == "neon" )
{
l.append( Calamares::job_ptr( new TrackingMachineNeonJob() ) );
}
else
{
cWarning() << "Unsupported machine tracking style" << style;
}
}
return l; return l;
} }