diff options
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | src/lib/dbus/UDisks2.cxx | 101 | ||||
-rw-r--r-- | src/lib/dbus/UDisks2.hxx | 5 | ||||
-rw-r--r-- | src/neighbor/plugins/UdisksNeighborPlugin.cxx | 81 |
4 files changed, 111 insertions, 78 deletions
diff --git a/Makefile.am b/Makefile.am index f94136999..1c5d38e31 100644 --- a/Makefile.am +++ b/Makefile.am @@ -293,7 +293,7 @@ libodbus_a_SOURCES = \ src/lib/dbus/PendingCall.hxx \ src/lib/dbus/ReadIter.hxx \ src/lib/dbus/ObjectManager.hxx \ - src/lib/dbus/UDisks2.hxx \ + src/lib/dbus/UDisks2.cxx src/lib/dbus/UDisks2.hxx \ src/lib/dbus/ScopeMatch.cxx src/lib/dbus/ScopeMatch.hxx \ src/lib/dbus/Types.hxx \ src/lib/dbus/Values.hxx \ diff --git a/src/lib/dbus/UDisks2.cxx b/src/lib/dbus/UDisks2.cxx new file mode 100644 index 000000000..1cb231951 --- /dev/null +++ b/src/lib/dbus/UDisks2.cxx @@ -0,0 +1,101 @@ +/* + * Copyright 2003-2018 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "UDisks2.hxx" +#include "ReadIter.hxx" +#include "util/StringAPI.hxx" +#include "Compiler.h" + +#include <functional> + +namespace UDisks2 { + +template<typename I> +gcc_pure +static const char * +CheckString(I &&i) noexcept +{ + if (i.GetArgType() != DBUS_TYPE_STRING) + return nullptr; + + return i.GetString(); +} + +static void +ParseDriveDictEntry(Object &o, const char *name, + ODBus::ReadMessageIter &&value_i) noexcept +{ + if (StringIsEqual(name, "Id")) { + const char *value = CheckString(value_i); + if (value != nullptr && o.drive_id.empty()) + o.drive_id = value; + } +} + +static void +ParseBlockDictEntry(Object &o, const char *name, + ODBus::ReadMessageIter &&value_i) noexcept +{ + if (StringIsEqual(name, "Id")) { + const char *value = CheckString(value_i); + if (value != nullptr && o.block_id.empty()) + o.block_id = value; + } +} + +static void +ParseInterface(Object &o, const char *interface, + ODBus::ReadMessageIter &&i) noexcept +{ + using namespace std::placeholders; + if (StringIsEqual(interface, "org.freedesktop.UDisks2.Drive")) { + i.ForEachProperty(std::bind(ParseDriveDictEntry, + std::ref(o), _1, _2)); + } else if (StringIsEqual(interface, "org.freedesktop.UDisks2.Block")) { + i.ForEachProperty(std::bind(ParseBlockDictEntry, + std::ref(o), _1, _2)); + } else if (StringIsEqual(interface, "org.freedesktop.UDisks2.Filesystem")) { + o.is_filesystem = true; + } +} + +static void +ParseInterfaceDictEntry(Object &o, ODBus::ReadMessageIter &&i) noexcept +{ + if (i.GetArgType() != DBUS_TYPE_STRING) + return; + + const char *interface = i.GetString(); + i.Next(); + + if (i.GetArgType() != DBUS_TYPE_ARRAY) + return; + + ParseInterface(o, interface, i.Recurse()); +} + +void +ParseObject(Object &o, ODBus::ReadMessageIter &&i) noexcept +{ + i.ForEach(DBUS_TYPE_DICT_ENTRY, [&o](auto &&j){ + ParseInterfaceDictEntry(o, j.Recurse()); + }); +} + +} // namespace UDisks2 diff --git a/src/lib/dbus/UDisks2.hxx b/src/lib/dbus/UDisks2.hxx index 12b9449a3..23d7fe599 100644 --- a/src/lib/dbus/UDisks2.hxx +++ b/src/lib/dbus/UDisks2.hxx @@ -25,6 +25,8 @@ #define UDISKS2_PATH "/org/freedesktop/UDisks2" #define UDISKS2_INTERFACE "org.freedesktop.UDisks2" +namespace ODBus { class ReadMessageIter; } + namespace UDisks2 { struct Object { @@ -52,6 +54,9 @@ struct Object { } }; +void +ParseObject(Object &o, ODBus::ReadMessageIter &&i) noexcept; + } // namespace UDisks2 #endif diff --git a/src/neighbor/plugins/UdisksNeighborPlugin.cxx b/src/neighbor/plugins/UdisksNeighborPlugin.cxx index 2087c92df..43e7fb056 100644 --- a/src/neighbor/plugins/UdisksNeighborPlugin.cxx +++ b/src/neighbor/plugins/UdisksNeighborPlugin.cxx @@ -35,7 +35,6 @@ #include "thread/Mutex.hxx" #include "thread/SafeSingleton.hxx" #include "util/Domain.hxx" -#include "util/StringAPI.hxx" #include "util/Manual.hxx" #include "Log.hxx" @@ -164,80 +163,6 @@ UdisksNeighborExplorer::Close() noexcept BlockingCall(GetEventLoop(), [this](){ DoClose(); }); } -template<typename I> -gcc_pure -static const char * -CheckString(I &&i) noexcept -{ - if (i.GetArgType() != DBUS_TYPE_STRING) - return nullptr; - - return i.GetString(); -} - -static void -ParseDriveDictEntry(UDisks2::Object &o, const char *name, - ODBus::ReadMessageIter &&value_i) noexcept -{ - if (StringIsEqual(name, "Id")) { - const char *value = CheckString(value_i); - if (value != nullptr && o.drive_id.empty()) - o.drive_id = value; - } -} - -static void -ParseBlockDictEntry(UDisks2::Object &o, const char *name, - ODBus::ReadMessageIter &&value_i) noexcept -{ - if (StringIsEqual(name, "Id")) { - const char *value = CheckString(value_i); - if (value != nullptr && o.block_id.empty()) - o.block_id = value; - } -} - -static void -ParseInterface(UDisks2::Object &o, const char *interface, - ODBus::ReadMessageIter &&i) noexcept -{ - using namespace std::placeholders; - if (StringIsEqual(interface, "org.freedesktop.UDisks2.Drive")) { - i.ForEachProperty(std::bind(ParseDriveDictEntry, - std::ref(o), _1, _2)); - } else if (StringIsEqual(interface, "org.freedesktop.UDisks2.Block")) { - i.ForEachProperty(std::bind(ParseBlockDictEntry, - std::ref(o), _1, _2)); - } else if (StringIsEqual(interface, "org.freedesktop.UDisks2.Filesystem")) { - o.is_filesystem = true; - } -} - -static void -ParseInterfaceDictEntry(UDisks2::Object &o, ODBus::ReadMessageIter &&i) noexcept -{ - if (i.GetArgType() != DBUS_TYPE_STRING) - return; - - const char *interface = i.GetString(); - i.Next(); - - if (i.GetArgType() != DBUS_TYPE_ARRAY) - return; - - ParseInterface(o, interface, i.Recurse()); -} - -static bool -ParseObject(UDisks2::Object &o, ODBus::ReadMessageIter &&i) noexcept -{ - i.ForEach(DBUS_TYPE_DICT_ENTRY, [&o](auto &&j){ - ParseInterfaceDictEntry(o, j.Recurse()); - }); - - return true; -} - NeighborExplorer::List UdisksNeighborExplorer::GetList() const noexcept { @@ -308,7 +233,8 @@ UdisksNeighborExplorer::OnListNotify(ODBus::Message reply) noexcept ForEachInterface(i.Recurse(), [this](const char *path, auto &&j){ UDisks2::Object o(path); - if (ParseObject(o, std::move(j)) && o.IsValid()) + UDisks2::ParseObject(o, std::move(j)); + if (o.IsValid()) Insert(std::move(o)); }); } @@ -323,7 +249,8 @@ UdisksNeighborExplorer::HandleMessage(DBusConnection *, DBusMessage *message) no dbus_message_has_signature(message, InterfacesAddedType::value)) { RecurseInterfaceDictEntry(ReadMessageIter(*message), [this](const char *path, auto &&i){ UDisks2::Object o(path); - if (ParseObject(o, std::move(i)) && o.IsValid()) + UDisks2::ParseObject(o, std::move(i)); + if (o.IsValid()) Insert(std::move(o)); }); |