diff --git a/declarative/declarativessudeviceinfo.cpp b/declarative/declarativessudeviceinfo.cpp index 23c49f5..1c61fb3 100644 --- a/declarative/declarativessudeviceinfo.cpp +++ b/declarative/declarativessudeviceinfo.cpp @@ -26,7 +26,7 @@ QString DeclarativeSsuDeviceInfo::deviceModel() return info.deviceModel(); } -QString DeclarativeSsuDeviceInfo::displayName(const int type) +QString DeclarativeSsuDeviceInfo::displayName(int type) { return info.displayName(type); } diff --git a/declarative/declarativessudeviceinfo.h b/declarative/declarativessudeviceinfo.h index 631f5e4..ed2e776 100644 --- a/declarative/declarativessudeviceinfo.h +++ b/declarative/declarativessudeviceinfo.h @@ -32,16 +32,19 @@ class DeclarativeSsuDeviceInfo : public QObject * multithreaded environment, unless you like funny results. */ Q_INVOKABLE QString deviceFamily(); + /** * Try to find the device variant for the system this is running on. * If the device is not a variant it will return an empty string. If * fallback is set to true it return the device model in this case. */ Q_INVOKABLE QString deviceVariant(bool fallback = false); + /** * Try to find out ond what kind of system this is running */ Q_INVOKABLE QString deviceModel(); + /** * Return a string suitable for display in dialogs, ... * @@ -52,7 +55,7 @@ class DeclarativeSsuDeviceInfo : public QObject * If no manufacturer is found UNKNOWN is returned. * For an invalid type an empty string is returned. */ - Q_INVOKABLE QString displayName(const int type); + Q_INVOKABLE QString displayName(int type); private: SsuDeviceInfo info; diff --git a/libssu/sandbox.cpp b/libssu/sandbox.cpp index 75e86a1..8e5a0c9 100644 --- a/libssu/sandbox.cpp +++ b/libssu/sandbox.cpp @@ -79,7 +79,7 @@ Sandbox::~Sandbox() } if (!m_tempDir.isEmpty() && QFileInfo(m_tempDir).exists()) { - if (QProcess::execute("rm", QStringList() << "-rf" << m_tempDir) != 0) { + if (!QDir(m_tempDir).removeRecursively()) { qWarning("%s: Failed to remove temporary directory", Q_FUNC_INFO); } } @@ -276,7 +276,7 @@ bool Sandbox::prepare() QString Sandbox::createTmpDir(const QString &nameTemplate) { - static const int REASONABLE_REPEAT_COUNT = 10; + const int REASONABLE_REPEAT_COUNT = 10; for (int i = 0; i < REASONABLE_REPEAT_COUNT; ++i) { QString path; diff --git a/libssu/ssu.cpp b/libssu/ssu.cpp index 82975ef..80c65a3 100644 --- a/libssu/ssu.cpp +++ b/libssu/ssu.cpp @@ -37,7 +37,8 @@ static void restoreUid() } } -Ssu::Ssu(): QObject() +Ssu::Ssu() + : QObject() { errorFlag = false; pendingRequests = 0; @@ -75,13 +76,13 @@ Ssu::Ssu(): QObject() // FIXME, the whole credentials stuff needs reworking // should probably be part of repo handling instead of core configuration -QPair Ssu::credentials(QString scope) +QPair Ssu::credentials(const QString &scope) { SsuCoreConfig *settings = SsuCoreConfig::instance(); return settings->credentials(scope); } -QString Ssu::credentialsScope(QString repoName, bool rndRepo) +QString Ssu::credentialsScope(const QString &repoName, bool rndRepo) { SsuCoreConfig *settings = SsuCoreConfig::instance(); SsuSettings repoSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat); @@ -124,7 +125,7 @@ QString Ssu::credentialsScope(QString repoName, bool rndRepo) return settings->credentialsScope(repoName, rndRepo); } -QString Ssu::credentialsUrl(QString scope) +QString Ssu::credentialsUrl(const QString &scope) { SsuCoreConfig *settings = SsuCoreConfig::instance(); return settings->credentialsUrl(scope); @@ -178,19 +179,19 @@ void Ssu::setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode editMode) settings->setDeviceMode(mode, editMode); } -void Ssu::setFlavour(QString flavour) +void Ssu::setFlavour(const QString &flavour) { SsuCoreConfig *settings = SsuCoreConfig::instance(); settings->setFlavour(flavour); } -void Ssu::setRelease(QString release, bool rnd) +void Ssu::setRelease(const QString &release, bool rnd) { SsuCoreConfig *settings = SsuCoreConfig::instance(); settings->setRelease(release, rnd); } -void Ssu::setDomain(QString domain) +void Ssu::setDomain(const QString &domain) { SsuCoreConfig *settings = SsuCoreConfig::instance(); settings->setDomain(domain); @@ -223,8 +224,9 @@ bool Ssu::registerDevice(QDomDocument *response) settings->setValue("registered", false); setError("Certificate is invalid"); return false; - } else + } else { settings->setValue("certificate", certificate.toPem()); + } QString privateKeyString = response->elementsByTagName("privateKey").at(0).toElement().text(); QSslKey privateKey(privateKeyString.toLatin1(), QSsl::Rsa); @@ -233,8 +235,9 @@ bool Ssu::registerDevice(QDomDocument *response) settings->setValue("registered", false); setError("Private key is invalid"); return false; - } else + } else { settings->setValue("privateKey", privateKey.toPem()); + } // oldUser is just for reference purposes, in case we want to notify // about owner changes for the device @@ -256,7 +259,7 @@ bool Ssu::registerDevice(QDomDocument *response) // RND repos have flavour (devel, testing, release), and release (latest, next) // Release repos only have release (latest, next, version number) -QString Ssu::repoUrl(QString repoName, bool rndRepo, +QString Ssu::repoUrl(const QString &repoName, bool rndRepo, QHash repoParameters, QHash parametersOverride) { @@ -289,7 +292,7 @@ void Ssu::requestFinished(QNetworkReply *reply) /// @TODO: indicate that the device is not registered if there's a 404 on credentials update url if (settings->contains("home-url")) { - QString homeUrl = settings->value("home-url").toString().arg(""); + QString homeUrl = settings->value("home-url").toString().arg(QString()); homeUrl.remove(QRegExp("//+$")); if (request.url().toString().startsWith(homeUrl, Qt::CaseInsensitive)) { @@ -353,11 +356,11 @@ void Ssu::requestFinished(QNetworkReply *reply) } } -void Ssu::sendRegistration(QString usernameDomain, QString password) +void Ssu::sendRegistration(const QString &usernameDomain, const QString &password) { errorFlag = false; - QString ssuCaCertificate, ssuRegisterUrl; + QString ssuRegisterUrl; QString username, domainName; SsuLog *ssuLog = SsuLog::instance(); @@ -381,7 +384,7 @@ void Ssu::sendRegistration(QString usernameDomain, QString password) setDomain(settings->value("default-rnd-domain").toString()); } - ssuCaCertificate = SsuRepoManager::caCertificatePath(); + QString ssuCaCertificate = SsuRepoManager::caCertificatePath(); if (ssuCaCertificate.isEmpty()) { setError("CA certificate for ssu not set ('_ca-certificate in domain')"); return; @@ -393,11 +396,12 @@ void Ssu::sendRegistration(QString usernameDomain, QString password) setError("URL for ssu registration not set (config key 'register-url')"); return; } - } else + } else { ssuRegisterUrl = settings->value("register-url").toString(); + } QString IMEI = deviceInfo.deviceUid(); - if (IMEI == "") { + if (IMEI.isEmpty()) { setError("No valid UID available for your device. For phones: is your modem online?"); return; } @@ -432,10 +436,8 @@ void Ssu::sendRegistration(QString usernameDomain, QString password) ssuLog->print(LOG_DEBUG, QString("Sending request to %1") .arg(request.url().url())); - QNetworkReply *reply; - pendingRequests++; - reply = manager->post(request, form.query(QUrl::FullyEncoded).toStdString().c_str()); + manager->post(request, form.query(QUrl::FullyEncoded).toStdString().c_str()); // we could expose downloadProgress() from reply in case we want progress info QString homeUrl = settings->value("home-url").toString().arg(username); @@ -482,7 +484,7 @@ bool Ssu::setCredentials(QDomDocument *response) credentialScopes.append(scope); } } else { - setError(""); + setError(QString()); return false; } } @@ -494,7 +496,7 @@ bool Ssu::setCredentials(QDomDocument *response) return true; } -void Ssu::setError(QString errorMessage) +void Ssu::setError(const QString &errorMessage) { errorFlag = true; errorString = errorMessage; @@ -509,7 +511,7 @@ void Ssu::setError(QString errorMessage) emit done(); } -void Ssu::storeAuthorizedKeys(QByteArray data) +void Ssu::storeAuthorizedKeys(const QByteArray &data) { QDir dir; SsuLog *ssuLog = SsuLog::instance(); @@ -537,8 +539,9 @@ void Ssu::storeAuthorizedKeys(QByteArray data) ssuLog->print(LOG_DEBUG, QString("Dropping to %1/%2 for writing authorized keys") .arg(uid_min) .arg(pw->pw_gid)); - } else + } else { return; + } homePath = Sandbox::map(homePath); @@ -549,13 +552,14 @@ void Ssu::storeAuthorizedKeys(QByteArray data) return; } - if (!dir.exists(homePath + "/.ssh")) + if (!dir.exists(homePath + "/.ssh")) { if (!dir.mkdir(homePath + "/.ssh")) { ssuLog->print(LOG_DEBUG, QString("Unable to create .ssh in %1") .arg(homePath)); restoreUid(); return; } + } QFile::setPermissions(homePath + "/.ssh", QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); @@ -579,7 +583,7 @@ void Ssu::updateCredentials(bool force) SsuLog *ssuLog = SsuLog::instance(); - if (deviceInfo.deviceUid() == "") { + if (deviceInfo.deviceUid().isEmpty()) { setError("No valid UID available for your device. For phones: is your modem online?"); return; } @@ -597,8 +601,9 @@ void Ssu::updateCredentials(bool force) setError("URL for credentials update not set (config key 'credentials-url')"); return; } - } else + } else { ssuCredentialsUrl = settings->value("credentials-url").toString(); + } if (!isRegistered()) { setError("Device is not registered."); @@ -661,8 +666,9 @@ void Ssu::updateStoreCredentials() if (settings->value("ignore-credential-errors").toBool() == true) { ssuLog->print(LOG_WARNING, QString("Warning: ignore-credential-errors is set, passing auth errors down to libzypp")); ssuLog->print(LOG_WARNING, QString("Store credentials not received. %1").arg(reply.error().message())); - } else + } else { setError(QString("Store credentials not received. %1").arg(reply.error().message())); + } } else { SsuCoreConfig *settings = SsuCoreConfig::instance(); settings->beginGroup("credentials-store"); diff --git a/libssu/ssu.h b/libssu/ssu.h index 15c4067..2ad61fd 100644 --- a/libssu/ssu.h +++ b/libssu/ssu.h @@ -85,7 +85,7 @@ class Ssu: public QObject * Find a username/password pair for the given scope * @return a QPair with username and password, or an empty QPair if scope is invalid */ - QPair credentials(QString scope); + QPair credentials(const QString &scope); /** * Get the scope for a repository, taking into account different scopes for * release and RnD repositories @@ -95,11 +95,11 @@ class Ssu: public QObject * * @return a string containing the scope; it can be used to look up login credentials using credentials() */ - QString credentialsScope(QString repoName, bool rndRepo = false); + QString credentialsScope(const QString &repoName, bool rndRepo = false); /** * Return the URL for which credentials scope is valid */ - QString credentialsUrl(QString scope); + QString credentialsUrl(const QString &scope); /** * Returns if the last operation was successful * @retval true last operation was successful @@ -116,7 +116,7 @@ class Ssu: public QObject * Resolve a repository url * @return the repository URL on success, an empty string on error */ - QString repoUrl(QString repoName, bool rndRepo = false, + QString repoUrl(const QString &repoName, bool rndRepo = false, QHash repoParameters = QHash(), QHash parametersOverride = QHash()); /** @@ -146,11 +146,11 @@ class Ssu: public QObject /// See SsuCoreConfig::setDeviceMode Q_INVOKABLE void setDeviceMode(DeviceModeFlags mode, enum EditMode editMode = Replace); /// See SsuCoreConfig::setFlavour - Q_INVOKABLE void setFlavour(QString flavour); + Q_INVOKABLE void setFlavour(const QString &flavour); /// See SsuCoreConfig::setRelease - Q_INVOKABLE void setRelease(QString release, bool rnd = false); + Q_INVOKABLE void setRelease(const QString &release, bool rnd = false); /// See SsuCoreConfig::setDomain - Q_INVOKABLE void setDomain(QString domain); + Q_INVOKABLE void setDomain(const QString &domain); /// See SsuCoreConfig::useSslVerify Q_INVOKABLE bool useSslVerify(); @@ -162,7 +162,7 @@ class Ssu: public QObject bool registerDevice(QDomDocument *response); bool setCredentials(QDomDocument *response); bool verifyResponse(QDomDocument *response); - void storeAuthorizedKeys(QByteArray data); + void storeAuthorizedKeys(const QByteArray &data); private slots: void requestFinished(QNetworkReply *reply); @@ -170,7 +170,7 @@ private slots: * Set errorString returned by lastError to errorMessage, set * errorFlag returned by error() to true, and emit done() */ - void setError(QString errorMessage); + void setError(const QString &errorMessage); public slots: /** @@ -182,7 +182,7 @@ public slots: * error() to check if an error occured, and use lastError() to retrieve the last * error message. */ - void sendRegistration(QString username, QString password); + void sendRegistration(const QString &username, const QString &password); /** * Try to update the RND repository credentials. The device needs to be registered * for this to work. updateCredentials remembers the time of the last credentials diff --git a/libssu/ssucoreconfig.cpp b/libssu/ssucoreconfig.cpp index 153af12..cd1c353 100644 --- a/libssu/ssucoreconfig.cpp +++ b/libssu/ssucoreconfig.cpp @@ -25,7 +25,7 @@ SsuCoreConfig *SsuCoreConfig::instance() return ssuCoreConfig; } -QPair SsuCoreConfig::credentials(QString scope) +QPair SsuCoreConfig::credentials(const QString &scope) { QPair ret; beginGroup("credentials-" + scope); @@ -35,7 +35,7 @@ QPair SsuCoreConfig::credentials(QString scope) return ret; } -QString SsuCoreConfig::credentialsScope(QString repoName, bool rndRepo) +QString SsuCoreConfig::credentialsScope(const QString &repoName, bool rndRepo) { Q_UNUSED(repoName) Q_UNUSED(rndRepo) @@ -46,7 +46,7 @@ QString SsuCoreConfig::credentialsScope(QString repoName, bool rndRepo) return "your-configuration-is-broken-and-does-not-contain-credentials-scope"; } -QString SsuCoreConfig::credentialsUrl(QString scope) +QString SsuCoreConfig::credentialsUrl(const QString &scope) { if (contains("credentials-url-" + scope)) return value("credentials-url-" + scope).toString(); @@ -77,8 +77,9 @@ QString SsuCoreConfig::domain(bool pretty) return value("domain").toString().replace(":", "-"); else return value("domain").toString(); - } else - return ""; + } else { + return QString(); + } } bool SsuCoreConfig::isRegistered() @@ -111,14 +112,15 @@ void SsuCoreConfig::setDeviceMode(Ssu::DeviceModeFlags mode, enum Ssu::EditMode oldMode |= mode; } else if ((editMode & Ssu::Remove) == Ssu::Remove) { oldMode &= ~mode; - } else + } else { oldMode = mode; + } setValue("deviceMode", oldMode); sync(); } -void SsuCoreConfig::setFlavour(QString flavour) +void SsuCoreConfig::setFlavour(const QString &flavour) { setValue("flavour", flavour); // flavour is RnD only, so enable RnD mode @@ -126,7 +128,7 @@ void SsuCoreConfig::setFlavour(QString flavour) sync(); } -void SsuCoreConfig::setRelease(QString release, bool rnd) +void SsuCoreConfig::setRelease(const QString &release, bool rnd) { if (rnd) { setValue("rndRelease", release); @@ -139,11 +141,11 @@ void SsuCoreConfig::setRelease(QString release, bool rnd) sync(); } -void SsuCoreConfig::setDomain(QString domain) +void SsuCoreConfig::setDomain(const QString &domain) { // - in domain messes with default section autodetection, // so change it to : - setValue("domain", domain.replace("-", ":")); + setValue("domain", QString(domain).replace("-", ":")); sync(); } diff --git a/libssu/ssucoreconfig_p.h b/libssu/ssucoreconfig_p.h index 1b92a23..e2f238d 100644 --- a/libssu/ssucoreconfig_p.h +++ b/libssu/ssucoreconfig_p.h @@ -34,7 +34,7 @@ class SsuCoreConfig: public SsuSettings * Find a username/password pair for the given scope * @return a QPair with username and password, or an empty QPair if scope is invalid */ - QPair credentials(QString scope); + QPair credentials(const QString &scope); /** * Get the scope for a repository, taking into account different scopes for * release and RnD repositories @@ -44,11 +44,11 @@ class SsuCoreConfig: public SsuSettings * * @return a string containing the scope; it can be used to look up login credentials using credentials() */ - QString credentialsScope(QString repoName, bool rndRepo = false); + QString credentialsScope(const QString &repoName, bool rndRepo = false); /** * Return the URL for which credentials scope is valid */ - QString credentialsUrl(QString scope); + QString credentialsUrl(const QString &scope); /** * Get the current flavour when RnD repositories are used * @return current flavour (usually something like testing, release, ..) @@ -87,15 +87,15 @@ class SsuCoreConfig: public SsuSettings /** * Set the flavour used when resolving RND repositories */ - Q_INVOKABLE void setFlavour(QString flavour); + Q_INVOKABLE void setFlavour(const QString &flavour); /** * Set the release version string for either a release, or a RnD snapshot */ - Q_INVOKABLE void setRelease(QString release, bool rnd = false); + Q_INVOKABLE void setRelease(const QString &release, bool rnd = false); /** * Set the domain string (usually something like nemo, jolla, ..) */ - Q_INVOKABLE void setDomain(QString domain); + Q_INVOKABLE void setDomain(const QString &domain); /** * Return configuration settings regarding ssl verification * @retval true SSL verification must be used; that's the default if not configured diff --git a/libssu/ssudeviceinfo.cpp b/libssu/ssudeviceinfo.cpp index 64ef858..a02b182 100644 --- a/libssu/ssudeviceinfo.cpp +++ b/libssu/ssudeviceinfo.cpp @@ -23,7 +23,8 @@ #include "../constants.h" -SsuDeviceInfo::SsuDeviceInfo(QString model): QObject() +SsuDeviceInfo::SsuDeviceInfo(const QString &model) + : QObject() { boardMappings = new SsuSettings(SSU_BOARD_MAPPING_CONFIGURATION, SSU_BOARD_MAPPING_CONFIGURATION_DIR); if (!model.isEmpty()) @@ -101,9 +102,9 @@ QString SsuDeviceInfo::adaptationVariables(const QString &adaptationName, QHash< void SsuDeviceInfo::clearCache() { - cachedFamily = ""; - cachedModel = ""; - cachedVariant = ""; + cachedFamily.clear(); + cachedModel.clear(); + cachedVariant.clear(); } bool SsuDeviceInfo::contains(const QString &model) @@ -148,13 +149,11 @@ QString SsuDeviceInfo::deviceVariant(bool fallback) if (!cachedVariant.isEmpty()) return cachedVariant; - cachedVariant = ""; - if (boardMappings->contains("variants/" + deviceModel())) { cachedVariant = boardMappings->value("variants/" + deviceModel()).toString(); } - if (cachedVariant == "" && fallback) + if (cachedVariant.isEmpty() && fallback) return deviceModel(); return cachedVariant; @@ -162,19 +161,16 @@ QString SsuDeviceInfo::deviceVariant(bool fallback) QString SsuDeviceInfo::deviceModel() { - QDir dir; - QFile procCpuinfo; - QStringList keys; - if (!cachedModel.isEmpty()) return cachedModel; boardMappings->beginGroup("file.exists"); - keys = boardMappings->allKeys(); + QStringList keys = boardMappings->allKeys(); // check if the device can be identified by testing for a file foreach (const QString &key, keys) { QString value = boardMappings->value(key).toString(); + QDir dir; if (dir.exists(Sandbox::map(value))) { cachedModel = key; break; @@ -184,6 +180,7 @@ QString SsuDeviceInfo::deviceModel() if (!cachedModel.isEmpty()) return cachedModel; // check if the device can be identified by a string in /proc/cpuinfo + QFile procCpuinfo; procCpuinfo.setFileName(Sandbox::map("/proc/cpuinfo")); procCpuinfo.open(QIODevice::ReadOnly | QIODevice::Text); if (procCpuinfo.isOpen()) { @@ -328,7 +325,7 @@ QString SsuDeviceInfo::deviceUid() } ssuLog->print(LOG_CRIT, "Could not read fallback UID - returning empty string"); - return ""; + return QString(); } QStringList SsuDeviceInfo::disabledRepos() @@ -343,13 +340,12 @@ QStringList SsuDeviceInfo::disabledRepos() return result; } -QString SsuDeviceInfo::displayName(const int type) +QString SsuDeviceInfo::displayName(int type) { QString model = deviceModel(); QString variant = deviceVariant(false); QString value, key; - switch (type) { case Ssu::DeviceManufacturer: key = "/deviceManufacturer"; @@ -361,7 +357,7 @@ QString SsuDeviceInfo::displayName(const int type) key = "/deviceDesignation"; break; default: - return ""; + return QString(); } /* @@ -374,7 +370,7 @@ QString SsuDeviceInfo::displayName(const int type) if (boardMappings->contains(model + key)) value = boardMappings->value(model + key).toString(); - else if (variant != "" && boardMappings->contains(variant + key)) + else if (!variant.isEmpty() && boardMappings->contains(variant + key)) value = boardMappings->value(variant + key).toString(); else if (boardMappings->contains(key)) value = boardMappings->value(key).toString(); @@ -433,33 +429,35 @@ QStringList SsuDeviceInfo::repos(bool rnd, int filter) return result; } -QVariant SsuDeviceInfo::variable(QString section, const QString &key) +QVariant SsuDeviceInfo::variable(const QString §ion, const QString &key) { /// @todo compat-setting as ssudeviceinfo guaranteed to prepend sections with var-; /// SsuVariables does not have this guarantee. Remove from here as well. - if (!section.startsWith("var-")) - section = "var-" + section; + QString varSection(section); + if (!varSection.startsWith("var-")) + varSection = "var-" + varSection; - return SsuVariables::variable(boardMappings, section, key); + return SsuVariables::variable(boardMappings, varSection, key); } -void SsuDeviceInfo::variableSection(QString section, QHash *storageHash) +void SsuDeviceInfo::variableSection(const QString §ion, QHash *storageHash) { - if (!section.startsWith("var-")) - section = "var-" + section; + QString varSection(section); + if (!varSection.startsWith("var-")) + varSection = "var-" + varSection; - SsuVariables::variableSection(boardMappings, section, storageHash); + SsuVariables::variableSection(boardMappings, varSection, storageHash); } -void SsuDeviceInfo::setDeviceModel(QString model) +void SsuDeviceInfo::setDeviceModel(const QString &model) { - if (model == "") - cachedModel = ""; + if (model.isEmpty()) + cachedModel.clear(); else cachedModel = model; - cachedFamily = ""; - cachedVariant = ""; + cachedFamily.clear(); + cachedVariant.clear(); } QVariant SsuDeviceInfo::value(const QString &key, const QVariant &value) diff --git a/libssu/ssudeviceinfo.h b/libssu/ssudeviceinfo.h index 21ea161..8b3ff7a 100644 --- a/libssu/ssudeviceinfo.h +++ b/libssu/ssudeviceinfo.h @@ -22,7 +22,7 @@ class SsuDeviceInfo: public QObject /** * Initialize with device to override autodetection */ - SsuDeviceInfo(QString model = ""); + SsuDeviceInfo(const QString &model = QString()); virtual ~SsuDeviceInfo(); @@ -30,43 +30,51 @@ class SsuDeviceInfo: public QObject * Return the list of adaptations used for the set model */ QStringList adaptationRepos(); + /** * Resolve adaptation-specific variables for adaptationName, and store them in storageHash * Returns "adaptation" if a valid adaptation was found, adaptationName otherwise */ QString adaptationVariables(const QString &adaptationName, QHash *storageHash); + /** * Check if a given model is available in the deviceinfo database, either directly, * or as variant. If no model is provided as argument the autodetected or previously * set model is used. */ - bool contains(const QString &model = ""); + bool contains(const QString &model = QString()); + /** * Try to find the device family for the system this is running on. This function * temporarily changes the detected model, and therefore should not be used in a * multithreaded environment, unless you like funny results. */ Q_INVOKABLE QString deviceFamily(); + /** * Try to find the device variant for the system this is running on. * If the device is not a variant it will return an empty string. If * fallback is set to true it return the device model in this case. */ Q_INVOKABLE QString deviceVariant(bool fallback = false); + /** * Try to find out ond what kind of system this is running */ Q_INVOKABLE QString deviceModel(); + /** * Calculate the device ID used in ssu requests * @return The first imei from oFono ModemManager API, if available, or WLAN mac address, or device uid fallback code similar to QDeviceInfo::uniqueDeviceID() */ Q_INVOKABLE QString deviceUid(); + /** * Return the list of repositories explicitely disabled for this device * This does not include repositories only disabled in the user configuration. */ QStringList disabledRepos(); + /** * Return a string suitable for display in dialogs, ... * @@ -77,7 +85,8 @@ class SsuDeviceInfo: public QObject * If no manufacturer is found UNKNOWN is returned. * For an invalid type an empty string is returned. */ - Q_INVOKABLE QString displayName(const int type); + Q_INVOKABLE QString displayName(int type); + /** * Return the complete list of repositories configured for a device. * Depending on the filter options, all repostories (user and board), @@ -85,22 +94,26 @@ class SsuDeviceInfo: public QObject * Disabled repositories are excluded depending on filter settings. */ QStringList repos(bool rnd = false, int filter = Ssu::NoFilter); + /** * Override device model autodetection */ - Q_INVOKABLE void setDeviceModel(QString model = ""); + Q_INVOKABLE void setDeviceModel(const QString &model = QString()); + /** * Return a variable from the given variable section. 'var'- is automatically * prepended to the section name if not specified already. Recursive search * through several variable sections (specified in the section) is supported, * returned will be the first occurence of the variable. */ - QVariant variable(QString section, const QString &key); + QVariant variable(const QString §ion, const QString &key); + /** * Return the requested variable section. 'var-' is automatically * prepended to the section name if not specified already. */ - void variableSection(QString section, QHash *storageHash); + void variableSection(const QString §ion, QHash *storageHash); + /** * Return a value from an adaptation section. Returns an empty string * or a given default value if key does not exist. @@ -111,7 +124,6 @@ class SsuDeviceInfo: public QObject */ QVariant value(const QString &key, const QVariant &value = QVariant()); - private: SsuSettings *boardMappings; QString cachedFamily, cachedModel, cachedVariant; diff --git a/libssu/ssufeaturemanager.cpp b/libssu/ssufeaturemanager.cpp index 42baee5..6121aa7 100644 --- a/libssu/ssufeaturemanager.cpp +++ b/libssu/ssufeaturemanager.cpp @@ -18,7 +18,8 @@ #include "../constants.h" -SsuFeatureManager::SsuFeatureManager(): QObject() +SsuFeatureManager::SsuFeatureManager() + : QObject() { featureSettings = new SsuSettings(SSU_FEATURE_CONFIGURATION, SSU_FEATURE_CONFIGURATION_DIR); } @@ -57,7 +58,7 @@ QStringList SsuFeatureManager::repos(bool rndRepo, int filter) return r; } -QString SsuFeatureManager::url(QString repo, bool rndRepo) +QString SsuFeatureManager::url(const QString &repo, bool rndRepo) { QString repoHeader = QString("repositories-%1/") .arg(rndRepo ? "rnd" : "release"); @@ -67,5 +68,5 @@ QString SsuFeatureManager::url(QString repo, bool rndRepo) else if (featureSettings->contains("repositories/" + repo)) return featureSettings->value("repositories/" + repo).toString(); - return ""; + return QString(); } diff --git a/libssu/ssufeaturemanager.h b/libssu/ssufeaturemanager.h index c9e4544..8495b1f 100644 --- a/libssu/ssufeaturemanager.h +++ b/libssu/ssufeaturemanager.h @@ -22,7 +22,7 @@ class SsuFeatureManager: public QObject public: SsuFeatureManager(); QStringList repos(bool rndRepo, int filter = Ssu::NoFilter); - QString url(QString repo, bool rndRepo); + QString url(const QString &repo, bool rndRepo); private: SsuSettings *featureSettings; diff --git a/libssu/ssufeaturemodel.cpp b/libssu/ssufeaturemodel.cpp index d46aad9..bf302af 100644 --- a/libssu/ssufeaturemodel.cpp +++ b/libssu/ssufeaturemodel.cpp @@ -51,7 +51,7 @@ class SsuFeatureModelPrivate if (keys.contains("version")) { feature.insert("version", settings.value("version").toString()); } else { - feature.insert("version", QString("")); + feature.insert("version", QString()); } features.append(feature); } diff --git a/libssu/ssulog.cpp b/libssu/ssulog.cpp index 64b395c..bc8bd09 100644 --- a/libssu/ssulog.cpp +++ b/libssu/ssulog.cpp @@ -24,11 +24,8 @@ SsuLog *SsuLog::instance() return ssuLog; } -void SsuLog::print(int priority, QString message) +void SsuLog::print(int priority, const QString &message) { - QByteArray ba = message.toUtf8(); - const char *ca = ba.constData(); - // directly go through qsettings here to avoid recursive invocation // of coreconfig / ssulog if (ssuLogLevel == -1) { @@ -45,7 +42,10 @@ void SsuLog::print(int priority, QString message) if (priority > ssuLogLevel) return; - if (sd_journal_print(priority, "ssu: %s", ca) < 0 && fallbackLogPath != "") { + QByteArray ba = message.toUtf8(); + const char *ca = ba.constData(); + + if (sd_journal_print(priority, "ssu: %s", ca) < 0 && !fallbackLogPath.isEmpty()) { QFile logfile; QTextStream logstream; logfile.setFileName(fallbackLogPath); diff --git a/libssu/ssulog_p.h b/libssu/ssulog_p.h index 250ba6b..c78193d 100644 --- a/libssu/ssulog_p.h +++ b/libssu/ssulog_p.h @@ -20,7 +20,7 @@ class SsuLog /** * Print a message to systemds journal, or to a text log file, if a fallback is defined */ - void print(int priority, QString message); + void print(int priority, const QString &message); private: SsuLog() {} diff --git a/libssu/ssurepomanager.cpp b/libssu/ssurepomanager.cpp index 4774d8b..9b246ae 100644 --- a/libssu/ssurepomanager.cpp +++ b/libssu/ssurepomanager.cpp @@ -21,12 +21,12 @@ #include "../constants.h" -SsuRepoManager::SsuRepoManager(): QObject() +SsuRepoManager::SsuRepoManager() + : QObject() { - } -int SsuRepoManager::add(QString repo, QString repoUrl) +int SsuRepoManager::add(const QString &repo, const QString &repoUrl) { SsuCoreConfig *ssuSettings = SsuCoreConfig::instance(); @@ -38,7 +38,7 @@ int SsuRepoManager::add(QString repo, QString repoUrl) if ((ssuSettings->deviceMode() & Ssu::AppInstallMode) == Ssu::AppInstallMode) return -1; - if (repoUrl == "") { + if (repoUrl.isEmpty()) { // just enable a repository which has URL in repos.ini QStringList enabledRepos; if (ssuSettings->contains("enabled-repos")) @@ -47,22 +47,21 @@ int SsuRepoManager::add(QString repo, QString repoUrl) enabledRepos.append(repo); enabledRepos.removeDuplicates(); ssuSettings->setValue("enabled-repos", enabledRepos); - } else + } else { ssuSettings->setValue("repository-urls/" + repo, repoUrl); + } ssuSettings->sync(); return 0; } -QString SsuRepoManager::caCertificatePath(QString domain) +QString SsuRepoManager::caCertificatePath(const QString &domain) { SsuCoreConfig *settings = SsuCoreConfig::instance(); SsuSettings repoSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat); - if (domain.isEmpty()) - domain = settings->domain(); - - QString ca = SsuVariables::variable(&repoSettings, domain + "-domain", + QString ca = SsuVariables::variable(&repoSettings, + (domain.isEmpty() ? settings->domain() : domain) + "-domain", "_ca-certificate").toString(); if (!ca.isEmpty()) return ca; @@ -71,10 +70,10 @@ QString SsuRepoManager::caCertificatePath(QString domain) if (settings->contains("ca-certificate")) return settings->value("ca-certificate").toString(); - return ""; + return QString(); } -int SsuRepoManager::disable(QString repo) +int SsuRepoManager::disable(const QString &repo) { SsuCoreConfig *ssuSettings = SsuCoreConfig::instance(); QStringList disabledRepos; @@ -91,7 +90,7 @@ int SsuRepoManager::disable(QString repo) return 0; } -int SsuRepoManager::enable(QString repo) +int SsuRepoManager::enable(const QString &repo) { SsuCoreConfig *ssuSettings = SsuCoreConfig::instance(); QStringList disabledRepos; @@ -108,7 +107,7 @@ int SsuRepoManager::enable(QString repo) return 0; } -int SsuRepoManager::remove(QString repo) +int SsuRepoManager::remove(const QString &repo) { SsuCoreConfig *ssuSettings = SsuCoreConfig::instance(); @@ -190,8 +189,7 @@ QStringList SsuRepoManager::repos(bool rnd, SsuDeviceInfo &deviceInfo, int filte appInstallMode = true; } - if (filter == Ssu::NoFilter || - filter == Ssu::UserFilter) { + if (filter == Ssu::NoFilter || filter == Ssu::UserFilter) { // user defined repositories, or ones overriding URLs for default ones // -> in update mode we need to check for each of those if it already // exists. If it exists, keep it, if it does not, disable it @@ -242,23 +240,19 @@ void SsuRepoManager::update() // - delete all non-ssu managed repositories (missing ssu_ prefix) // - create list of ssu-repositories for current adaptation // - go through ssu_* repositories, delete all which are not in the list; write others - - SsuDeviceInfo deviceInfo; - QStringList ssuFilters; - SsuCoreConfig *ssuSettings = SsuCoreConfig::instance(); int deviceMode = ssuSettings->deviceMode(); SsuLog *ssuLog = SsuLog::instance(); - // if device is misconfigured, always assume release mode - bool rndMode = false; - if ((deviceMode & Ssu::DisableRepoManager) == Ssu::DisableRepoManager) { ssuLog->print(LOG_INFO, "Repo management requested, but not enabled (option 'deviceMode')"); return; } + // if device is misconfigured, always assume release mode + bool rndMode = false; + if ((deviceMode & Ssu::RndMode) == Ssu::RndMode) rndMode = true; @@ -280,10 +274,11 @@ void SsuRepoManager::update() } // ... delete all ssu-managed repositories not valid for this device ... + QStringList ssuFilters; ssuFilters.append("ssu_*"); QDirIterator it(Sandbox::map(ZYPP_REPO_PATH), ssuFilters); while (it.hasNext()) { - QString f = it.next(); + it.next(); QStringList parts = it.fileName().split("_"); // repo file structure is ssu__.repo -> splits to 3 parts @@ -291,8 +286,9 @@ void SsuRepoManager::update() if (!repositoryList.contains(parts.at(1)) || parts.at(2) != (rndMode ? "rnd.repo" : "release.repo" )) QFile(it.filePath()).remove(); - } else + } else { QFile(it.filePath()).remove(); + } } // ... and create all repositories required for this device @@ -311,7 +307,7 @@ void SsuRepoManager::update() .arg(repo) .arg(rndMode ? "rnd" : "release"); - if (url(repoName, rndMode) == "") { + if (url(repoName, rndMode).isEmpty()) { // TODO, repositories should only be disabled if they're not required // for this machine. For required repositories error is better QTextStream qerr(stderr); @@ -391,15 +387,10 @@ QStringList SsuRepoManager::repoVariables(QHash *storageHash, // RND repos have flavour (devel, testing, release), and release (latest, next) // Release repos only have release (latest, next, version number) -QString SsuRepoManager::url(QString repoName, bool rndRepo, +QString SsuRepoManager::url(const QString &repoName, bool rndRepo, QHash repoParameters, QHash parametersOverride) { - QString r; - QStringList configSections; - SsuVariables var; - SsuCoreConfig *settings = SsuCoreConfig::instance(); - SsuSettings repoSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat); SsuDeviceInfo deviceInfo; // set debugSplit for incorrectly configured debuginfo repositories (debugSplit @@ -408,8 +399,7 @@ QString SsuRepoManager::url(QString repoName, bool rndRepo, if (repoName.endsWith("-debuginfo") && !repoParameters.contains("debugSplit")) repoParameters.insert("debugSplit", "debug"); - configSections = repoVariables(&repoParameters, rndRepo); - + QStringList configSections = repoVariables(&repoParameters, rndRepo); // Override device model (and therefore all the family, ... stuff) if (parametersOverride.contains("model")) @@ -418,19 +408,22 @@ QString SsuRepoManager::url(QString repoName, bool rndRepo, repoParameters.insert("deviceFamily", deviceInfo.deviceFamily()); repoParameters.insert("deviceModel", deviceInfo.deviceModel()); - repoName = deviceInfo.adaptationVariables(repoName, &repoParameters); - + QString adaptationRepoName = deviceInfo.adaptationVariables(repoName, &repoParameters); + SsuCoreConfig *settings = SsuCoreConfig::instance(); QString domain; + if (parametersOverride.contains("domain")) { domain = parametersOverride.value("domain"); domain.replace("-", ":"); - } else + } else { domain = settings->domain(); + } // variableSection does autodetection for the domain default section - var.variableSection(&repoSettings, - domain + "-domain", &repoParameters); + SsuSettings repoSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat); + SsuVariables var; + var.variableSection(&repoSettings, domain + "-domain", &repoParameters); // override arbitrary variables, mostly useful for generating mic URLs QHash::const_iterator i = parametersOverride.constBegin(); @@ -445,16 +438,17 @@ QString SsuRepoManager::url(QString repoName, bool rndRepo, // 3. URLs from repos.ini SsuFeatureManager featureManager; + QString r; - if (settings->contains("repository-urls/" + repoName)) - r = settings->value("repository-urls/" + repoName).toString(); - else if (featureManager.url(repoName, rndRepo) != "") - r = featureManager.url(repoName, rndRepo); - else { + if (settings->contains("repository-urls/" + adaptationRepoName)) { + r = settings->value("repository-urls/" + adaptationRepoName).toString(); + } else if (!featureManager.url(adaptationRepoName, rndRepo).isEmpty()) { + r = featureManager.url(adaptationRepoName, rndRepo); + } else { foreach (const QString §ion, configSections) { repoSettings.beginGroup(section); - if (repoSettings.contains(repoName)) { - r = repoSettings.value(repoName).toString(); + if (repoSettings.contains(adaptationRepoName)) { + r = repoSettings.value(adaptationRepoName).toString(); repoSettings.endGroup(); break; } diff --git a/libssu/ssurepomanager.h b/libssu/ssurepomanager.h index 064fe0e..5ef448d 100644 --- a/libssu/ssurepomanager.h +++ b/libssu/ssurepomanager.h @@ -29,7 +29,7 @@ class SsuRepoManager: public QObject * * If the device is in UpdateMode this function does nothing. */ - int add(QString repo, QString repoUrl = ""); + int add(const QString &repo, const QString &repoUrl = QString()); /** * Return the path to the CA certificate to be used for the given domain, * or default domain, if omitted @@ -38,7 +38,7 @@ class SsuRepoManager: public QObject * @retval -1 Repository not added because device is in update mode * @retval -2 Repository not added because third party repositories are disabled */ - static QString caCertificatePath(QString domain = ""); + static QString caCertificatePath(const QString &domain = QString()); /** * Disable a repository * @@ -46,7 +46,7 @@ class SsuRepoManager: public QObject * @retval -1 Request ignored because device is in update mode * @retval -2 Request ignored because 3rd party repositories are disabled */ - int disable(QString repo); + int disable(const QString &repo); /** * Enable a repository, given it's not disabled by board configuration * @@ -54,7 +54,7 @@ class SsuRepoManager: public QObject * @retval -1 Request ignored because device is in update mode * @retval -2 Request ignored because 3rd party repositories are disabled */ - int enable(QString repo); + int enable(const QString &repo); /** * Remove a repository * @@ -62,7 +62,7 @@ class SsuRepoManager: public QObject * @retval -1 Request ignored because device is in update mode * @retval -2 Request ignored because 3rd party repositories are disabled */ - int remove(QString repo); + int remove(const QString &repo); /** * Collect the list of repositories from different submodules */ @@ -94,7 +94,7 @@ class SsuRepoManager: public QObject * Resolve a repository url * @return the repository URL on success, an empty string on error */ - QString url(QString repoName, bool rndRepo = false, + QString url(const QString &repoName, bool rndRepo = false, QHash repoParameters = QHash(), QHash parametersOverride = QHash()); diff --git a/libssu/ssusettings.cpp b/libssu/ssusettings.cpp index 602fcf3..da8ad4c 100644 --- a/libssu/ssusettings.cpp +++ b/libssu/ssusettings.cpp @@ -14,26 +14,25 @@ #include "ssusettings_p.h" #include "ssulog_p.h" -SsuSettings::SsuSettings(): QSettings() +SsuSettings::SsuSettings() + : QSettings() { - } -SsuSettings::SsuSettings(const QString &fileName, Format format, QObject *parent): - QSettings(Sandbox::map(fileName), format, parent) +SsuSettings::SsuSettings(const QString &fileName, Format format, QObject *parent) + : QSettings(Sandbox::map(fileName), format, parent) { - } -SsuSettings::SsuSettings(const QString &fileName, Format format, const QString &defaultFileName, QObject *parent): - QSettings(Sandbox::map(fileName), format, parent) +SsuSettings::SsuSettings(const QString &fileName, Format format, const QString &defaultFileName, QObject *parent) + : QSettings(Sandbox::map(fileName), format, parent) { defaultSettingsFile = Sandbox::map(defaultFileName); upgrade(); } -SsuSettings::SsuSettings(const QString &fileName, const QString &settingsDirectory, QObject *parent): - QSettings(Sandbox::map(fileName), QSettings::IniFormat, parent) +SsuSettings::SsuSettings(const QString &fileName, const QString &settingsDirectory, QObject *parent) + : QSettings(Sandbox::map(fileName), QSettings::IniFormat, parent) { settingsd = Sandbox::map(settingsDirectory); merge(); @@ -41,7 +40,7 @@ SsuSettings::SsuSettings(const QString &fileName, const QString &settingsDirecto void SsuSettings::merge(bool keepOld) { - if (settingsd == "") + if (settingsd.isEmpty()) return; bool skipMerge = true; @@ -112,14 +111,14 @@ void SsuSettings::merge(QSettings *masterSettings, const QStringList &settingsFi */ void SsuSettings::upgrade() { + if (defaultSettingsFile.isEmpty()) + return; + int configVersion = 0; int defaultConfigVersion = 0; SsuLog *ssuLog = SsuLog::instance(); - if (defaultSettingsFile == "") - return; - QSettings defaultSettings(defaultSettingsFile, QSettings::IniFormat); if (contains("configVersion")) @@ -133,13 +132,13 @@ void SsuSettings::upgrade() .arg(defaultConfigVersion)); for (int i = configVersion + 1; i <= defaultConfigVersion; i++) { - QStringList defaultKeys; QString currentSection = QString("%1/").arg(i); ssuLog->print(LOG_DEBUG, QString("Processing configuration version %1").arg(i)); defaultSettings.beginGroup(currentSection); - defaultKeys = defaultSettings.allKeys(); + QStringList defaultKeys = defaultSettings.allKeys(); defaultSettings.endGroup(); + foreach (const QString &key, defaultKeys) { // Default keys support both commands and new keys if (key.compare("cmd-remove", Qt::CaseSensitive) == 0) { diff --git a/libssu/ssuvariables.cpp b/libssu/ssuvariables.cpp index 8a14666..1b763c7 100644 --- a/libssu/ssuvariables.cpp +++ b/libssu/ssuvariables.cpp @@ -14,12 +14,12 @@ #include "../constants.h" -SsuVariables::SsuVariables(): QObject() +SsuVariables::SsuVariables() + : QObject() { - } -QString SsuVariables::defaultSection(SsuSettings *settings, QString section) +QString SsuVariables::defaultSection(SsuSettings *settings, const QString §ion) { QStringList parts = section.split("-"); @@ -33,10 +33,10 @@ QString SsuVariables::defaultSection(SsuSettings *settings, QString section) if (settings->childGroups().contains(key)) return key; else - return ""; + return QString(); } -QString SsuVariables::resolveString(QString pattern, QHash *variables, int recursionDepth) +QString SsuVariables::resolveString(const QString &pattern, QHash *variables, int recursionDepth) { if (recursionDepth >= SSU_MAX_RECURSION) { return "maximum-recursion-level-reached"; @@ -46,13 +46,15 @@ QString SsuVariables::resolveString(QString pattern, QHash *va regex.setMinimal(true); int pos = 0; - while ((pos = regex.indexIn(pattern, pos)) != -1) { + QString result(pattern); + + while ((pos = regex.indexIn(result, pos)) != -1) { QString match = regex.cap(0); if (match.contains(":")) { // variable is special, resolve before replacing QString variable = resolveVariable(match, variables); - pattern.replace(match, variable); + result.replace(match, variable); pos += variable.length(); } else { // look up variable name in hashmap, and replace it with stored value, @@ -61,37 +63,39 @@ QString SsuVariables::resolveString(QString pattern, QHash *va variableName.remove(0, 2); variableName.chop(1); if (variables->contains(variableName)) { - pattern.replace(match, variables->value(variableName)); + result.replace(match, variables->value(variableName)); pos += variables->value(variableName).length(); - } else - pattern.replace(match, ""); + } else { + result.replace(match, ""); + } } } // check if string still contains variables, and recurse - if (regex.indexIn(pattern, 0) != -1) - pattern = resolveString(pattern, variables, recursionDepth + 1); + if (regex.indexIn(result, 0) != -1) + result = resolveString(result, variables, recursionDepth + 1); - return pattern; + return result; } -QString SsuVariables::resolveVariable(QString variable, QHash *variables) +QString SsuVariables::resolveVariable(const QString &variable, QHash *variables) { - QString variableValue = ""; + QString variableValue; + QString filteredVariable(variable); - if (variable.endsWith(")")) - variable.chop(1); - if (variable.startsWith("%(")) - variable.remove(0, 2); + if (filteredVariable.endsWith(")")) + filteredVariable.chop(1); + if (filteredVariable.startsWith("%(")) + filteredVariable.remove(0, 2); // hunt for your colon - int magic = variable.indexOf(":"); + int magic = filteredVariable.indexOf(":"); // seems you misplaced your colon - if (magic == -1) return variable; + if (magic == -1) return filteredVariable; - QStringRef variableName(&variable, 0, magic); - QStringRef variableSub(&variable, magic + 2, variable.length() - magic - 2); + QStringRef variableName(&filteredVariable, 0, magic); + QStringRef variableSub(&filteredVariable, magic + 2, filteredVariable.length() - magic - 2); // Fill in variable value for later tests, if it exists if (variables->contains(variableName.toString())) @@ -99,18 +103,18 @@ QString SsuVariables::resolveVariable(QString variable, QHash // find the operator who's after your colon QChar op; - if (variable.length() > magic + 1) - op = variable.at(magic + 1); + if (filteredVariable.length() > magic + 1) + op = filteredVariable.at(magic + 1); switch (op.toLatin1()) { case '-': // substitute default value if variable is empty - if (variableValue == "") + if (variableValue.isEmpty()) return variableSub.toString(); break; case '+': // substitute default value if variable is not empty - if (variableValue != "") + if (!variableValue.isEmpty()) return variableSub.toString(); break; case '=': { @@ -143,7 +147,7 @@ SsuSettings *SsuVariables::settings() } /// @todo add override capability with an override-section in ssu.ini -QVariant SsuVariables::variable(QString section, const QString &key) +QVariant SsuVariables::variable(const QString §ion, const QString &key) { if (m_settings != NULL) return variable(m_settings, section, key); @@ -151,11 +155,9 @@ QVariant SsuVariables::variable(QString section, const QString &key) return QVariant(); } -QVariant SsuVariables::variable(SsuSettings *settings, QString section, const QString &key) +QVariant SsuVariables::variable(SsuSettings *settings, const QString §ion, const QString &key) { - QVariant value; - - value = readVariable(settings, section, key, 0); + QVariant value = readVariable(settings, section, key, 0); // first check if the value is defined in the main section, and fall back // to default sections @@ -168,19 +170,18 @@ QVariant SsuVariables::variable(SsuSettings *settings, QString section, const QS return value; } -void SsuVariables::variableSection(QString section, QHash *storageHash) +void SsuVariables::variableSection(const QString §ion, QHash *storageHash) { if (m_settings != NULL) variableSection(m_settings, section, storageHash); } -void SsuVariables::variableSection(SsuSettings *settings, QString section, QHash *storageHash) +void SsuVariables::variableSection(SsuSettings *settings, const QString §ion, QHash *storageHash) { - QString dSection = defaultSection(settings, section); - if (dSection.isEmpty()) + if (dSection.isEmpty()) { readSection(settings, section, storageHash, 0); - else { + } else { readSection(settings, dSection, storageHash, 0); readSection(settings, section, storageHash, 0, false); } @@ -190,7 +191,7 @@ void SsuVariables::variableSection(SsuSettings *settings, QString section, QHash // variables which exist in more than one section will get overwritten when discovered // again // the section itself gets evaluated at the end, again having a chance to overwrite variables -void SsuVariables::readSection(SsuSettings *settings, QString section, +void SsuVariables::readSection(SsuSettings *settings, const QString §ion, QHash *storageHash, int recursionDepth, bool logOverride) { @@ -250,7 +251,7 @@ void SsuVariables::readSection(SsuSettings *settings, QString section, settings->endGroup(); } -QVariant SsuVariables::readVariable(SsuSettings *settings, QString section, const QString &key, +QVariant SsuVariables::readVariable(SsuSettings *settings, const QString §ion, const QString &key, int recursionDepth, bool logOverride) { Q_UNUSED(logOverride) diff --git a/libssu/ssuvariables_p.h b/libssu/ssuvariables_p.h index 5a9c92e..7f68d6e 100644 --- a/libssu/ssuvariables_p.h +++ b/libssu/ssuvariables_p.h @@ -26,15 +26,15 @@ class SsuVariables: public QObject * the first token (or second token for "var-" sections) replaced with * "default". You should therefore avoid "-" in section names. */ - static QString defaultSection(SsuSettings *settings, QString section); + static QString defaultSection(SsuSettings *settings, const QString §ion); /** * Resolve a whole string, containing several variables. Variables inside variables are allowed */ - static QString resolveString(QString pattern, QHash *variables, int recursionDepth = 0); + static QString resolveString(const QString &pattern, QHash *variables, int recursionDepth = 0); /** * Resolve variables; variable can be passed as %(var) or var */ - static QString resolveVariable(QString variable, QHash *variables); + static QString resolveVariable(const QString &variable, QHash *variables); /** * Set the settings object to use */ @@ -49,8 +49,8 @@ class SsuVariables: public QObject * through several variable sections (specified in the section) is supported, * returned will be the last occurence of the variable. */ - QVariant variable(QString section, const QString &key); - static QVariant variable(SsuSettings *settings, QString section, const QString &key); + QVariant variable(const QString §ion, const QString &key); + static QVariant variable(SsuSettings *settings, const QString §ion, const QString &key); /** * Return the requested variable section, recursively looking up all variable * sections referenced inside with the 'variable' keyword. 'var-' is automatically @@ -64,15 +64,15 @@ class SsuVariables: public QObject * This function tries to identify a default configuration section, and merges * the default section with the requested section. */ - void variableSection(QString section, QHash *storageHash); - static void variableSection(SsuSettings *settings, QString section, + void variableSection(const QString §ion, QHash *storageHash); + static void variableSection(SsuSettings *settings, const QString §ion, QHash *storageHash); private: - static void readSection(SsuSettings *settings, QString section, + static void readSection(SsuSettings *settings, const QString §ion, QHash *storageHash, int recursionDepth, bool logOverride = true); - static QVariant readVariable(SsuSettings *settings, QString section, const QString &key, + static QVariant readVariable(SsuSettings *settings, const QString §ion, const QString &key, int recursionDepth, bool logOverride = true); SsuSettings *m_settings; }; diff --git a/ssucli/ssucli.cpp b/ssucli/ssucli.cpp index bce565d..23cf14e 100644 --- a/ssucli/ssucli.cpp +++ b/ssucli/ssucli.cpp @@ -19,7 +19,8 @@ #include "ssucli.h" -SsuCli::SsuCli(): QObject() +SsuCli::SsuCli() + : QObject() { connect(this, SIGNAL(done()), QCoreApplication::instance(), SLOT(quit()), Qt::DirectConnection); @@ -171,7 +172,6 @@ void SsuCli::optMode(QStringList opt) void SsuCli::optModel(QStringList opt) { QTextStream qout(stdout); - QTextStream qerr(stderr); SsuDeviceInfo deviceInfo; if (opt.count() == 3 && opt.at(2) == "-s") { @@ -186,7 +186,6 @@ void SsuCli::optModel(QStringList opt) void SsuCli::optModifyRepo(enum Actions action, QStringList opt) { SsuRepoManager repoManager; - QTextStream qout(stdout); QTextStream qerr(stderr); if (opt.count() == 3) { @@ -341,7 +340,7 @@ void SsuCli::optRepos(QStringList opt) SsuRepoManager repoManager; SsuDeviceInfo deviceInfo; QHash repoParameters, repoOverride; - QString device = ""; + QString device; bool rndRepo = false; int micMode = 0, flagStart = 0; @@ -398,7 +397,7 @@ void SsuCli::optRepos(QStringList opt) rndRepo = false; } - if (device != "") { + if (!device.isEmpty()) { deviceInfo.setDeviceModel(device); repoOverride.insert("model", device); } @@ -414,8 +413,9 @@ void SsuCli::optRepos(QStringList opt) if (repo.endsWith("-debuginfo")) { repoName = repo.left(repo.size() - 10); repoParameters.insert("debugSplit", "debug"); - } else if (repoParameters.value("debugSplit") == "debug") + } else if (repoParameters.value("debugSplit") == "debug") { repoParameters.remove("debugSplit"); + } QString repoUrl = ssu.repoUrl(repoName, rndRepo, repoParameters, repoOverride); qout << "repo --name=" << repo << "-" @@ -427,9 +427,9 @@ void SsuCli::optRepos(QStringList opt) return; } - if (device.isEmpty()) + if (device.isEmpty()) { repos = repoManager.repos(rndRepo, deviceInfo, Ssu::BoardFilterUserBlacklist); - else { + } else { qout << "Printing repository configuration for '" << device << "'" << endl << endl; repos = repoManager.repos(rndRepo, deviceInfo, Ssu::BoardFilter); } @@ -459,7 +459,7 @@ void SsuCli::optRepos(QStringList opt) } if (i == 0) { - if (device != "") { + if (!device.isEmpty()) { repos.clear(); continue; } @@ -473,7 +473,7 @@ void SsuCli::optRepos(QStringList opt) qout << endl << "Disabled repositories (global): " << endl; } else if (i == 2) { repos.clear(); - if (device != "") + if (!device.isEmpty()) continue; if (ssuSettings->contains("disabled-repos")) repos.append(ssuSettings->value("disabled-repos").toStringList()); @@ -553,7 +553,7 @@ void SsuCli::optStatus(QStringList opt) qout << "Device model: " << deviceInfo.displayName(Ssu::DeviceModel) << " (" << deviceInfo.deviceModel() << " / " << deviceInfo.displayName(Ssu::DeviceDesignation) << ")" << endl; - if (deviceInfo.deviceVariant() != "") + if (!deviceInfo.deviceVariant().isEmpty()) qout << "Device variant: " << deviceInfo.deviceVariant() << endl; qout << "Device UID: " << deviceUid << endl; if ((ssu.deviceMode() & Ssu::RndMode) == Ssu::RndMode) @@ -602,7 +602,6 @@ void SsuCli::optUpdateRepos(QStringList opt) void SsuCli::run() { - QTextStream qout(stdout); QTextStream qerr(stderr); QStringList arguments = QCoreApplication::arguments(); @@ -684,18 +683,15 @@ void SsuCli::run() usage(); } -void SsuCli::uidWarning(QString message) +void SsuCli::uidWarning() { - if (message.isEmpty()) - message = "Run 'ssu ur' as root to recreate repository files"; - if (geteuid() != 0) { QTextStream qout(stderr); - qout << "You're not root. " << message << endl; + qout << "You're not root. Run 'ssu ur' as root to recreate repository files" << endl; } } -void SsuCli::usage(QString message) +void SsuCli::usage(const QString &message) { QTextStream qout(stderr); qout << "\nUsage: ssu [-command-options] [arguments]" << endl diff --git a/ssucli/ssucli.h b/ssucli/ssucli.h index d9de605..924557a 100644 --- a/ssucli/ssucli.h +++ b/ssucli/ssucli.h @@ -32,8 +32,8 @@ public slots: SsuProxy *ssuProxy; QSettings settings; int state; - void usage(QString message = ""); - void uidWarning(QString message = ""); + void usage(const QString &message = QString()); + void uidWarning(); void optDomain(QStringList opt); void optFlavour(QStringList opt); void optMode(QStringList opt); @@ -47,10 +47,10 @@ public slots: void optUpdateRepos(QStringList opt); enum Actions { - Remove = 0, - Add = 1, - Disable = 2, - Enable = 3, + Remove, + Add, + Disable, + Enable }; void optModifyRepo(enum Actions action, QStringList opt); diff --git a/ssuconfperm/ssuconfperm.c b/ssuconfperm/ssuconfperm.c index 1442d36..c751e41 100644 --- a/ssuconfperm/ssuconfperm.c +++ b/ssuconfperm/ssuconfperm.c @@ -14,11 +14,12 @@ #include "../constants.h" -int main(int argc, char **argv){ - struct stat sb; +int main(int argc, char **argv) +{ + struct stat sb; - if (!stat(SSU_CONFIGURATION, &sb)){ - chown(SSU_CONFIGURATION, 0, SSU_GROUP_ID); - chmod(SSU_CONFIGURATION, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); - } + if (!stat(SSU_CONFIGURATION, &sb)){ + chown(SSU_CONFIGURATION, 0, SSU_GROUP_ID); + chmod(SSU_CONFIGURATION, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); + } } diff --git a/ssud/ssud.cpp b/ssud/ssud.cpp index fc9d8fa..1e410a6 100644 --- a/ssud/ssud.cpp +++ b/ssud/ssud.cpp @@ -16,7 +16,8 @@ const char *Ssud::SERVICE_NAME = "org.nemo.ssu"; const char *Ssud::OBJECT_PATH = "/org/nemo/ssu"; -Ssud::Ssud(QObject *parent): QObject(parent) +Ssud::Ssud(QObject *parent) + : QObject(parent) { QDBusConnection connection = QDBusConnection::systemBus(); if (!connection.registerObject(OBJECT_PATH, this)) { diff --git a/ssuks/ssukickstarter.cpp b/ssuks/ssukickstarter.cpp index 08b1f92..37fee75 100644 --- a/ssuks/ssukickstarter.cpp +++ b/ssuks/ssukickstarter.cpp @@ -64,13 +64,13 @@ QStringList SsuKickstarter::commandSection(const QString §ion, const QString .arg(SSU_DATA_DIR) .arg(section))); - if (dir.exists(replaceSpaces(deviceModel.toLower()))) + if (dir.exists(replaceSpaces(deviceModel.toLower()))) { commandFile = replaceSpaces(deviceModel.toLower()); - else if (dir.exists(replaceSpaces(deviceInfo.deviceVariant(true).toLower()))) + } else if (dir.exists(replaceSpaces(deviceInfo.deviceVariant(true).toLower()))) { commandFile = replaceSpaces(deviceInfo.deviceVariant(true).toLower()); - else if (dir.exists("default")) + } else if (dir.exists("default")) { commandFile = "default"; - else { + } else { if (description.isEmpty()) result.append("## No suitable configuration found in " + dir.path()); else @@ -112,7 +112,7 @@ QStringList SsuKickstarter::repos() foreach (const QString &repo, repos) { QString repoUrl = ssu.repoUrl(repo, rndMode, QHash(), repoOverride); - if (repoUrl == "") { + if (repoUrl.isEmpty()) { qerr << "Repository " << repo << " does not have an URL, ignoring" << endl; continue; } @@ -124,26 +124,27 @@ QStringList SsuKickstarter::repos() .arg(repo) .arg(replaceSpaces(deviceModel)) .arg((rndMode ? repoOverride.value("rndRelease") - : repoOverride.value("release"))) + : repoOverride.value("release"))) .arg((rndMode ? "-" + repoOverride.value("flavourName") - : "")) + : QString())) .arg(repoUrl) ); - } else + } else { result.append(QString("repo --name=%1-%2%3 --baseurl=%4") .arg(repo) .arg((rndMode ? repoOverride.value("rndRelease") - : repoOverride.value("release"))) + : repoOverride.value("release"))) .arg((rndMode ? "-" + repoOverride.value("flavourName") - : "")) + : QString())) .arg(repoUrl) ); + } } return result; } -QStringList SsuKickstarter::packagesSection(QString name) +QStringList SsuKickstarter::packagesSection(const QString &name) { QStringList result; @@ -166,7 +167,7 @@ QStringList SsuKickstarter::packagesSection(QString name) } // we intentionally don't support device-specific post scriptlets -QStringList SsuKickstarter::scriptletSection(QString name, int flags) +QStringList SsuKickstarter::scriptletSection(const QString &name, int flags) { QStringList result; QString path; @@ -226,14 +227,9 @@ void SsuKickstarter::setRepoParameters(QHash parameters) deviceModel = repoOverride.value("model"); } -bool SsuKickstarter::write(QString kickstart) +bool SsuKickstarter::write(const QString &kickstart) { - QFile ks; - QTextStream kout; QTextStream qerr(stderr); - SsuDeviceInfo deviceInfo(deviceModel); - SsuRepoManager repoManager; - SsuVariables var; QStringList commandSections; // initialize with default 'part' for compatibility, as partitions @@ -252,9 +248,11 @@ bool SsuKickstarter::write(QString kickstart) QHash defaults; // get generic repo variables; domain and adaptation specific bits are not interesting // in the kickstart + SsuRepoManager repoManager; repoManager.repoVariables(&defaults, rndMode); // overwrite with kickstart defaults + SsuDeviceInfo deviceInfo(deviceModel); deviceInfo.variableSection("kickstart-defaults", &defaults); if (deviceInfo.variable("kickstart-defaults", "commandSections") .canConvert(QMetaType::QStringList)) { @@ -308,7 +306,11 @@ bool SsuKickstarter::write(QString kickstart) QString outputDir = repoOverride.value("outputdir"); if (!outputDir.isEmpty()) outputDir.append("/"); + QFile ks; + if (kickstart.isEmpty()) { + SsuVariables var; + if (repoOverride.contains("filename")) { QString fileName = QString("%1%2") .arg(outputDir) @@ -321,9 +323,9 @@ bool SsuKickstarter::write(QString kickstart) qerr << "No filename specified, and no default filename configured" << endl; return false; } - } else if (kickstart == "-") + } else if (kickstart == "-") { opened = ks.open(stdout, QIODevice::WriteOnly); - else { + } else { ks.setFileName(outputDir + kickstart); opened = ks.open(QIODevice::WriteOnly); } @@ -331,8 +333,9 @@ bool SsuKickstarter::write(QString kickstart) if (!opened) { qerr << "Unable to write output file " << ks.fileName() << ": " << ks.errorString() << endl; return false; - } else if (!ks.fileName().isEmpty()) + } else if (!ks.fileName().isEmpty()) { qerr << "Writing kickstart to " << ks.fileName() << endl; + } QString displayName = QString("# DisplayName: %1 %2/%3 (%4) %5") .arg(repoOverride.value("brand")) @@ -365,6 +368,7 @@ bool SsuKickstarter::write(QString kickstart) QString kickstartType = QString("# KickstartType: %1") .arg((rndMode ? "rnd" : "release")); + QTextStream kout; kout.setDevice(&ks); kout << displayName << endl; kout << kickstartType << endl; diff --git a/ssuks/ssukickstarter.h b/ssuks/ssukickstarter.h index 8233303..09bfde9 100644 --- a/ssuks/ssukickstarter.h +++ b/ssuks/ssukickstarter.h @@ -20,7 +20,7 @@ class SsuKickstarter public: SsuKickstarter(); void setRepoParameters(QHash parameters); - bool write(QString kickstart = ""); + bool write(const QString &kickstart = QString()); enum ScriptletFlags { /// Chroot is not useful, but helps in making the code more readable @@ -36,11 +36,11 @@ class SsuKickstarter QString deviceModel; QStringList commands(); /// read a command section from file system - QStringList commandSection(const QString §ion, const QString &description = ""); - QStringList packagesSection(QString name); + QStringList commandSection(const QString §ion, const QString &description = QString()); + QStringList packagesSection(const QString &name); QString replaceSpaces(const QString &value); QStringList repos(); - QStringList scriptletSection(QString name, int flags = Chroot); + QStringList scriptletSection(const QString &name, int flags = Chroot); }; #endif diff --git a/ssuurlresolver/ssuurlresolver.cpp b/ssuurlresolver/ssuurlresolver.cpp index 7b77283..e2f55f8 100644 --- a/ssuurlresolver/ssuurlresolver.cpp +++ b/ssuurlresolver/ssuurlresolver.cpp @@ -16,14 +16,15 @@ #include "libssu/sandbox_p.h" #include "libssu/ssulog_p.h" -SsuUrlResolver::SsuUrlResolver(): QObject() +SsuUrlResolver::SsuUrlResolver() + : QObject() { QObject::connect(this, SIGNAL(done()), QCoreApplication::instance(), SLOT(quit()), Qt::QueuedConnection); } -void SsuUrlResolver::error(QString message) +void SsuUrlResolver::error(const QString &message) { SsuLog *ssuLog = SsuLog::instance(); ssuLog->print(LOG_WARNING, message); @@ -34,7 +35,7 @@ void SsuUrlResolver::error(QString message) QCoreApplication::exit(1); } -bool SsuUrlResolver::writeZyppCredentialsIfNeeded(QString credentialsScope) +bool SsuUrlResolver::writeZyppCredentialsIfNeeded(const QString &credentialsScope) { QString filePath = Sandbox::map("/etc/zypp/credentials.d/" + credentialsScope); QFileInfo credentialsFileInfo(filePath); @@ -52,7 +53,7 @@ bool SsuUrlResolver::writeZyppCredentialsIfNeeded(QString credentialsScope) QPair credentials = ssu.credentials(credentialsScope); SsuLog *ssuLog = SsuLog::instance(); - if (credentials.first == "" || credentials.second == "") { + if (credentials.first.isEmpty() || credentials.second.isEmpty()) { ssuLog->print(LOG_WARNING, "Returned credentials are empty, skip writing"); return false; } @@ -74,7 +75,7 @@ bool SsuUrlResolver::writeZyppCredentialsIfNeeded(QString credentialsScope) void SsuUrlResolver::run() { QHash repoParameters; - QString resolvedUrl, repo; + QString repo; bool isRnd = false; SsuLog *ssuLog = SsuLog::instance(); @@ -130,11 +131,12 @@ void SsuUrlResolver::run() if (ssu.error()) { error(ssu.lastError()); } - } else + } else { ssuLog->print(LOG_DEBUG, "Device not registered -- skipping credential update"); + } // resolve base url - resolvedUrl = ssu.repoUrl(repo, isRnd, repoParameters); + QString resolvedUrl = ssu.repoUrl(repo, isRnd, repoParameters); QString credentialsScope = ssu.credentialsScope(repo, isRnd); // only do credentials magic on secure connections @@ -155,8 +157,9 @@ void SsuUrlResolver::run() } headerList.append(QString("credentials=%1").arg(credentialsScope)); writeZyppCredentialsIfNeeded(credentialsScope); - } else + } else { ssuLog->print(LOG_DEBUG, QString("Skipping credential for %1 with scope %2").arg(repo).arg(credentialsScope)); + } if (!headerList.isEmpty() && !resolvedUrl.isEmpty()) { QUrl url(resolvedUrl); diff --git a/ssuurlresolver/ssuurlresolver.h b/ssuurlresolver/ssuurlresolver.h index c904664..fee5f96 100644 --- a/ssuurlresolver/ssuurlresolver.h +++ b/ssuurlresolver/ssuurlresolver.h @@ -58,9 +58,8 @@ class SsuUrlResolver: public QObject private: Ssu ssu; - void error(QString message); - void printJournal(int priority, QString message); - bool writeZyppCredentialsIfNeeded(QString credentialsScope); + void error(const QString &message); + bool writeZyppCredentialsIfNeeded(const QString &credentialsScope); public slots: void run();