Commit c864267f authored by Jean Vincent's avatar Jean Vincent Committed by Albert Vaca Cintora

Replace Q_FOREACH with C++11 range-for

Summary:
The use of Q_FOREACH is advised against (https://doc.qt.io/qt-5/qtglobal.html#Q_FOREACH) since Qt 5.7 and will eventually be removed from Qt.

I replaced all occurrences with the range-for loop introduced in C++11 (except for the one in daemon.cpp in deviceIdByName which might have a bug / typo in it).

I added const to the container or casted it with qAsConst when appropriate to avoid unnecessary copies.

(This is my first submission. I did all the unit tests, and they all passed but I don't know how to show it here.)

Reviewers: #kde_connect, nicolasfella, apol

Reviewed By: #kde_connect, nicolasfella, apol

Subscribers: albertvaka, apol, nicolasfella

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D6724
parent aa307803
......@@ -90,7 +90,7 @@ int main(int argc, char** argv)
}
const QStringList devices = blockOnReply<QStringList>(iface.devices(paired, reachable));
Q_FOREACH (const QString& id, devices) {
for (const QString& id : devices) {
if (parser.isSet(QStringLiteral("id-only"))) {
QTextStream(stdout) << id << endl;
} else {
......
......@@ -114,9 +114,9 @@ void BluetoothLinkProvider::addLink(BluetoothDeviceLink* deviceLink, const QStri
//I'm the new device and I found an existing device
void BluetoothLinkProvider::serviceDiscoveryFinished()
{
QList<QBluetoothServiceInfo> services = mServiceDiscoveryAgent->discoveredServices();
const QList<QBluetoothServiceInfo> services = mServiceDiscoveryAgent->discoveredServices();
Q_FOREACH (QBluetoothServiceInfo info, services) {
for (QBluetoothServiceInfo info : services) {
if (mSockets.contains(info.device().address())) {
continue;
}
......
......@@ -78,12 +78,12 @@ Daemon::Daemon(QObject *parent, bool testMode)
//Read remebered paired devices
const QStringList& list = KdeConnectConfig::instance()->trustedDevices();
Q_FOREACH (const QString& id, list) {
for (const QString& id : list) {
addDevice(new Device(this, id));
}
//Listen to new devices
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
for (LinkProvider* a : qAsConst(d->mLinkProviders)) {
connect(a, &LinkProvider::onConnectionReceived,
this, &Daemon::onNewDeviceLink);
a->onStart();
......@@ -127,7 +127,7 @@ void Daemon::removeDevice(Device* device)
void Daemon::cleanDevices()
{
Q_FOREACH (Device* device, d->mDevices) {
for (Device* device : qAsConst(d->mDevices)) {
if (device->isTrusted()) {
continue;
}
......@@ -142,14 +142,14 @@ void Daemon::cleanDevices()
void Daemon::forceOnNetworkChange()
{
qCDebug(KDECONNECT_CORE) << "Sending onNetworkChange to " << d->mLinkProviders.size() << " LinkProviders";
Q_FOREACH (LinkProvider* a, d->mLinkProviders) {
for (LinkProvider* a : qAsConst(d->mLinkProviders)) {
a->onNetworkChange();
}
}
Device*Daemon::getDevice(const QString& deviceId)
{
Q_FOREACH (Device* device, d->mDevices) {
for (Device* device : qAsConst(d->mDevices)) {
if (device->id() == deviceId) {
return device;
}
......@@ -160,7 +160,7 @@ Device*Daemon::getDevice(const QString& deviceId)
QStringList Daemon::devices(bool onlyReachable, bool onlyTrusted) const
{
QStringList ret;
Q_FOREACH (Device* device, d->mDevices) {
for (Device* device : qAsConst(d->mDevices)) {
if (onlyReachable && !device->isReachable()) continue;
if (onlyTrusted && !device->isTrusted()) continue;
ret.append(device->id());
......@@ -244,9 +244,9 @@ bool Daemon::isDiscoveringDevices() const
QString Daemon::deviceIdByName(const QString &name) const
{
Q_FOREACH (Device* d, d->mDevices) {
if (d->name() == name && d->isTrusted())
return d->id();
for (Device* device : qAsConst(d->mDevices)) {
if (device->name() == name && device->isTrusted())
return device->id();
}
return {};
}
......
......@@ -102,7 +102,7 @@ void Device::reloadPlugins()
PluginLoader* loader = PluginLoader::instance();
Q_FOREACH (const QString& pluginName, m_supportedPlugins) {
for (const QString& pluginName : qAsConst(m_supportedPlugins)) {
const KPluginMetaData service = loader->getPluginInfo(pluginName);
const bool pluginEnabled = isPluginEnabled(pluginName);
......@@ -116,7 +116,7 @@ void Device::reloadPlugins()
}
Q_ASSERT(plugin);
Q_FOREACH (const QString& interface, incomingCapabilities) {
for (const QString& interface : incomingCapabilities) {
newPluginsByIncomingCapability.insert(interface, plugin);
}
......@@ -134,7 +134,7 @@ void Device::reloadPlugins()
m_pluginsByIncomingCapability = newPluginsByIncomingCapability;
QDBusConnection bus = QDBusConnection::sessionBus();
Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) {
for (KdeConnectPlugin* plugin : qAsConst(m_plugins)) {
//TODO: see how it works in Android (only done once, when created)
plugin->connected();
......@@ -165,14 +165,14 @@ void Device::requestPair()
return;
}
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
for (DeviceLink* dl : qAsConst(m_deviceLinks)) {
dl->userRequestsPair();
}
}
void Device::unpair()
{
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
for (DeviceLink* dl : qAsConst(m_deviceLinks)) {
dl->userRequestsUnpair();
}
KdeConnectConfig::instance()->removeTrustedDevice(id());
......@@ -184,7 +184,7 @@ void Device::pairStatusChanged(DeviceLink::PairStatus status)
if (status == DeviceLink::NotPaired) {
KdeConnectConfig::instance()->removeTrustedDevice(id());
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
for (DeviceLink* dl : qAsConst(m_deviceLinks)) {
if (dl != sender()) {
dl->setPairStatus(DeviceLink::NotPaired);
}
......@@ -325,7 +325,7 @@ bool Device::sendPackage(NetworkPackage& np)
Q_ASSERT(isTrusted());
//Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
for (DeviceLink* dl : qAsConst(m_deviceLinks)) {
if (dl->sendPackage(np)) return true;
}
......@@ -340,7 +340,7 @@ void Device::privateReceivedPackage(const NetworkPackage& np)
if (plugins.isEmpty()) {
qWarning() << "discarding unsupported package" << np.type() << "for" << name();
}
Q_FOREACH (KdeConnectPlugin* plugin, plugins) {
for (KdeConnectPlugin* plugin : plugins) {
plugin->receivePackage(np);
}
} else {
......@@ -359,7 +359,7 @@ QStringList Device::availableLinks() const
{
QStringList sl;
sl.reserve(m_deviceLinks.size());
Q_FOREACH(DeviceLink* dl, m_deviceLinks) {
for (DeviceLink* dl : qAsConst(m_deviceLinks)) {
sl.append(dl->provider()->name());
}
return sl;
......
......@@ -36,8 +36,8 @@ PluginLoader* PluginLoader::instance()
PluginLoader::PluginLoader()
{
QVector<KPluginMetaData> data = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/"));
Q_FOREACH (const KPluginMetaData& metadata, data) {
const QVector<KPluginMetaData> data = KPluginLoader::findPlugins(QStringLiteral("kdeconnect/"));
for (const KPluginMetaData& metadata : data) {
plugins[metadata.pluginId()] = metadata;
}
}
......@@ -86,7 +86,7 @@ KdeConnectPlugin* PluginLoader::instantiatePluginForDevice(const QString& plugin
QStringList PluginLoader::incomingCapabilities() const
{
QSet<QString> ret;
Q_FOREACH (const KPluginMetaData& service, plugins) {
for (const KPluginMetaData& service : qAsConst(plugins)) {
ret += KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPackageType")).toSet();
}
return ret.toList();
......@@ -95,7 +95,7 @@ QStringList PluginLoader::incomingCapabilities() const
QStringList PluginLoader::outgoingCapabilities() const
{
QSet<QString> ret;
Q_FOREACH (const KPluginMetaData& service, plugins) {
for (const KPluginMetaData& service : qAsConst(plugins)) {
ret += KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-OutgoingPackageType")).toSet();
}
return ret.toList();
......@@ -105,7 +105,7 @@ QSet<QString> PluginLoader::pluginsForCapabilities(const QSet<QString>& incoming
{
QSet<QString> ret;
Q_FOREACH (const KPluginMetaData& service, plugins) {
for (const KPluginMetaData& service : qAsConst(plugins)) {
const QSet<QString> pluginIncomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPackageType")).toSet();
const QSet<QString> pluginOutgoingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-OutgoingPackageType")).toSet();
......
......@@ -55,7 +55,7 @@ QList<QAction*> SendFileItemAction::actions(const KFileItemListProperties& fileI
QDBusPendingReply<QStringList> reply = iface.devices(true, true);
reply.waitForFinished();
const QStringList devices = reply.value();
Q_FOREACH (const QString& id, devices) {
for (const QString& id : devices) {
DeviceDbusInterface deviceIface(id);
if (!deviceIface.isValid()) {
continue;
......@@ -87,9 +87,9 @@ QList<QAction*> SendFileItemAction::actions(const KFileItemListProperties& fileI
void SendFileItemAction::sendFile()
{
QList<QUrl> urls = sender()->property("urls").value<QList<QUrl>>();
const QList<QUrl> urls = sender()->property("urls").value<QList<QUrl>>();
QString id = sender()->property("id").toString();
Q_FOREACH (const QUrl& url, urls) {
for (const QUrl& url : urls) {
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"), "/modules/kdeconnect/devices/"+id+"/share", QStringLiteral("org.kde.kdeconnect.device.share"), QStringLiteral("shareUrl"));
msg.setArguments(QVariantList() << url.toString());
QDBusConnection::sessionBus().call(msg);
......
......@@ -203,7 +203,7 @@ void DevicesModel::receivedDeviceList(QDBusPendingCallWatcher* watcher)
return;
beginInsertRows(QModelIndex(), 0, deviceIds.count()-1);
Q_FOREACH(const QString& id, deviceIds) {
for (const QString& id : deviceIds) {
appendDevice(new DeviceDbusInterface(id, this));
}
endInsertRows();
......
......@@ -155,7 +155,7 @@ void NotificationsModel::receivedNotifications(QDBusPendingCallWatcher* watcher)
}
beginInsertRows(QModelIndex(), 0, notificationIds.size() - 1);
Q_FOREACH (const QString& notificationId, notificationIds) {
for (const QString& notificationId : notificationIds) {
NotificationDbusInterface* dbusInterface = new NotificationDbusInterface(m_deviceId, notificationId, this);
m_notificationList.append(dbusInterface);
}
......@@ -229,7 +229,7 @@ int NotificationsModel::rowCount(const QModelIndex& parent) const
bool NotificationsModel::isAnyDimissable() const
{
Q_FOREACH (NotificationDbusInterface* notification, m_notificationList) {
for (NotificationDbusInterface* notification : qAsConst(m_notificationList)) {
if (notification->dismissable()) {
return true;
}
......@@ -239,7 +239,7 @@ bool NotificationsModel::isAnyDimissable() const
void NotificationsModel::dismissAll()
{
Q_FOREACH (NotificationDbusInterface* notification, m_notificationList) {
for (NotificationDbusInterface* notification : qAsConst(m_notificationList)) {
if (notification->dismissable()) {
notification->dismiss();
}
......
......@@ -84,9 +84,9 @@ void KioKdeconnect::listAllDevices()
infoMessage(i18n("Listing devices..."));
//TODO: Change to all devices and show different icons for connected and disconnected?
QStringList devices = m_dbusInterface->devices(true, true);
const QStringList devices = m_dbusInterface->devices(true, true);
Q_FOREACH(const QString& deviceId, devices) {
for (const QString& deviceId : devices) {
DeviceDbusInterface interface(deviceId);
......
......@@ -117,7 +117,7 @@ void DBusAsyncResponse::onTimeout()
const QDBusPendingCall* DBusResponseWaiter::extractPendingCall(QVariant& variant) const
{
Q_FOREACH(int type, m_registered)
for (int type : qAsConst(m_registered))
{
if (variant.canConvert(QVariant::Type(type)))
{
......
......@@ -48,8 +48,8 @@ MprisControlPlugin::MprisControlPlugin(QObject* parent, const QVariantList& args
connect(QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceOwnerChanged, this, &MprisControlPlugin::serviceOwnerChanged);
//Add existing interfaces
QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
Q_FOREACH (const QString& service, services) {
const QStringList services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
for (const QString& service : services) {
// The string doesn't matter, it just needs to be empty/non-empty
serviceOwnerChanged(service, QLatin1String(""), QStringLiteral("1"));
}
......
......@@ -65,7 +65,7 @@ void NotificationsDbusInterface::processPackage(const NetworkPackage& np)
id = id.mid(id.indexOf(QLatin1String("::")) + 2);
removeNotification(id);
} else if (np.get<bool>(QStringLiteral("isRequest"))) {
Q_FOREACH (const auto& n, mNotifications) {
for (const auto& n : qAsConst(mNotifications)) {
NetworkPackage np(PACKAGE_TYPE_NOTIFICATION_REQUEST, {
{"id", n->internalId()},
{"appName", n->appName()},
......
......@@ -90,8 +90,8 @@ bool PauseMusicPlugin::receivePackage(const NetworkPackage& np)
if (pause) {
//Search for interfaces currently playing
QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
Q_FOREACH (const QString& iface, interfaces) {
const QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
for (const QString& iface : interfaces) {
if (iface.startsWith(QLatin1String("org.mpris.MediaPlayer2"))) {
QDBusInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QStringLiteral("org.mpris.MediaPlayer2.Player"));
QString status = mprisInterface.property("PlaybackStatus").toString();
......@@ -120,7 +120,7 @@ bool PauseMusicPlugin::receivePackage(const NetworkPackage& np)
}
if (pause && !pausedSources.empty()) {
Q_FOREACH (const QString& iface, pausedSources) {
for (const QString& iface : qAsConst(pausedSources)) {
QDBusInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QStringLiteral("org.mpris.MediaPlayer2.Player"));
mprisInterface.asyncCall(QStringLiteral("PlayPause"));
}
......
......@@ -70,7 +70,8 @@ void RunCommandConfig::load()
QJsonDocument jsonDocument = QJsonDocument::fromJson(config()->get<QByteArray>(QStringLiteral("commands"), "{}"));
QJsonObject jsonConfig = jsonDocument.object();
Q_FOREACH (const QString &key, jsonConfig.keys()) {
const QStringList keys = jsonConfig.keys();
for (const QString& key : keys) {
const QJsonObject entry = jsonConfig[key].toObject();
const QString name = entry[QStringLiteral("name")].toString();
const QString command = entry[QStringLiteral("command")].toString();
......
......@@ -97,8 +97,8 @@ void NotificationsListener::setTranslatedAppName()
void NotificationsListener::loadApplications()
{
applications.clear();
QVariantList list = mPlugin->config()->getList(QStringLiteral("applications"));
Q_FOREACH (const auto& a, list) {
const QVariantList list = mPlugin->config()->getList(QStringLiteral("applications"));
for (const auto& a : list) {
NotifyingApplication app = a.value<NotifyingApplication>();
if (!applications.contains(app.name))
applications.insert(app.name, app);
......@@ -204,7 +204,7 @@ uint NotificationsListener::Notify(const QString &appName, uint replacesId,
applications.insert(app.name, app);
// update config:
QVariantList list;
Q_FOREACH (const auto& a, applications)
for (const auto& a : qAsConst(applications))
list << QVariant::fromValue<NotifyingApplication>(a);
mPlugin->config()->setList(QStringLiteral("applications"), list);
//qCDebug(KDECONNECT_PLUGIN_SENDNOTIFICATION) << "Added new application to config:" << app;
......
......@@ -48,7 +48,8 @@ class PluginLoadTest : public QObject
void testPlugins() {
Device* d = nullptr;
mDaemon->acquireDiscoveryMode(QStringLiteral("plugintest"));
Q_FOREACH(Device* id, mDaemon->devicesList()) {
const QList<Device*> devicesList = mDaemon->devicesList();
for (Device* id : devicesList) {
if (id->isReachable()) {
if (!id->isTrusted())
id->requestPair();
......
......@@ -52,7 +52,8 @@ class TestSendFile : public QObject
void testSend() {
mDaemon->acquireDiscoveryMode(QStringLiteral("test"));
Device* d = nullptr;
Q_FOREACH(Device* id, mDaemon->devicesList()) {
const QList<Device*> devicesList = mDaemon->devicesList();
for (Device* id : devicesList) {
if (id->isReachable()) {
if (!id->isTrusted())
id->requestPair();
......
......@@ -354,7 +354,8 @@ void TestNotificationListener::testNotify()
QStringList iconPaths;
// appIcon
int count = 0;
Q_FOREACH (const auto& iconName, KIconLoader::global()->queryIcons(-KIconLoader::SizeEnormous, KIconLoader::Application)) {
const QStringList icons = KIconLoader::global()->queryIcons(-KIconLoader::SizeEnormous, KIconLoader::Application);
for (const auto& iconName : icons) {
if (!iconName.endsWith(QLatin1String(".png")))
continue;
if (count++ > 3) // max 3 iterations
......
......@@ -67,7 +67,7 @@ void TestSocketLineReader::socketLineReader()
{
QList<QByteArray> dataToSend;
dataToSend << "foobar\n" << "barfoo\n" << "foobar?\n" << "\n" << "barfoo!\n" << "panda\n";
Q_FOREACH(const QByteArray &line, dataToSend) {
for (const QByteArray& line : dataToSend) {
mConn->write(line);
}
mConn->flush();
......
......@@ -138,7 +138,7 @@ void TestSslSocketLineReader::testTrustedDevice()
QList<QByteArray> dataToSend;
dataToSend << "foobar\n" << "barfoo\n" << "foobar?\n" << "\n" << "barfoo!\n" << "panda\n";
Q_FOREACH(const QByteArray &line, dataToSend) {
for (const QByteArray &line : dataToSend) {
mClientSocket->write(line);
}
mClientSocket->flush();
......@@ -197,7 +197,7 @@ void TestSslSocketLineReader::testUntrustedDevice()
QList<QByteArray> dataToSend;
dataToSend << "foobar\n" << "barfoo\n" << "foobar?\n" << "\n" << "barfoo!\n" << "panda\n";
Q_FOREACH(const QByteArray &line, dataToSend) {
for (const QByteArray &line : dataToSend) {
mClientSocket->write(line);
}
mClientSocket->flush();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment