[OpenDocString] kdeconnect-kde (cpp)
findthisdevice_config.cpp
FindThisDeviceConfig::FindThisDeviceConfig(QWidget *parent, const QVariantList &args)
    : KdeConnectPluginKcm(parent, args, QStringLiteral("kdeconnect_findthisdevice"))
    , m_ui(new Ui::FindThisDeviceConfigUi())
{
    m_ui->setupUi(this);

    const QStringList soundDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("sounds"), QStandardPaths::LocateDirectory);
    if (!soundDirs.isEmpty()) {
        m_ui->soundFileRequester->setStartDir(QUrl::fromLocalFile(soundDirs.last()));
    }

    connect(m_ui->playSoundButton, &QToolButton::clicked, this, &FindThisDeviceConfig::playSound);
    connect(m_ui->soundFileRequester, &KUrlRequester::textChanged, this, &FindThisDeviceConfig::markAsChanged);
}
This constructor builds a find this device config object and sets up signal/slot connections for the sound file requester and play button. It also sets up signal/slot connections for text change events.
FindThisDeviceConfig::~FindThisDeviceConfig()
{
    delete m_ui;
}
Deletes the ui object upon destruction.
void FindThisDeviceConfig::defaults()
{
    KCModule::defaults();

    m_ui->soundFileRequester->setText(defaultSound());

    Q_EMIT changed(true);
}
Sets the default values of the module to their internal values. First it sets the sound file requester text to the default sound and emits the signal.
void FindThisDeviceConfig::load()
{
    KCModule::load();

    const QString ringTone = config()->getString(QStringLiteral("ringtone"), defaultSound());
    m_ui->soundFileRequester->setText(ringTone);

    Q_EMIT changed(false);
}
This loads the ringtone config from the internal configuration object. It sets the sound file requester to the ring tone string by reading it from the configuration file and setting it on the ui object.
void FindThisDeviceConfig::save()
{
    config()->set(QStringLiteral("ringtone"), m_ui->soundFileRequester->text());

    KCModule::save();

    Q_EMIT changed(false);
}
Saves the ringtone config to the internal configuration object. It first sets the ringtone config value and then saves the module.
void FindThisDeviceConfig::playSound()
{
    const QUrl soundURL = m_ui->soundFileRequester->url();

    if (soundURL.isEmpty()) {
        qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
    } else {
        QMediaPlayer *player = new QMediaPlayer;
        player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
        player->setMedia(soundURL);
        player->setVolume(100);
        player->play();
        connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
    }
}
This code plays the sound of the current device based on its url. It takes the url of the sound fileRequester and sets the audio role to notification and sets the volume to 100% of the sound. It also connects the signal'stateChanged'.