neighbor/udisks: move struct UdisksObject to UDisks2.hxx

This commit is contained in:
Max Kellermann 2018-06-04 17:08:04 +02:00
parent a92c694f1b
commit 640de5518b
2 changed files with 46 additions and 38 deletions

View File

@ -20,7 +20,38 @@
#ifndef UDISKS2_HXX #ifndef UDISKS2_HXX
#define UDISKS2_HXX #define UDISKS2_HXX
#include <string>
#define UDISKS2_PATH "/org/freedesktop/UDisks2" #define UDISKS2_PATH "/org/freedesktop/UDisks2"
#define UDISKS2_INTERFACE "org.freedesktop.UDisks2" #define UDISKS2_INTERFACE "org.freedesktop.UDisks2"
namespace UDisks2 {
struct Object {
const std::string path;
std::string drive_id, block_id;
bool is_filesystem = false;
explicit Object(const char *_path) noexcept
:path(_path) {}
bool IsValid() const noexcept {
return is_filesystem &&
(!drive_id.empty() || !block_id.empty());
}
std::string GetUri() const noexcept {
if (!drive_id.empty())
return "udisks://" + drive_id;
else if (!block_id.empty())
return "udisks://" + block_id;
else
return {};
}
};
} // namespace UDisks2
#endif #endif

View File

@ -46,34 +46,11 @@
static constexpr Domain udisks_domain("udisks"); static constexpr Domain udisks_domain("udisks");
struct UdisksObject { static NeighborInfo
const std::string path; ToNeighborInfo(const UDisks2::Object &o) noexcept
{
std::string drive_id, block_id; return {o.GetUri(), o.path};
}
bool is_filesystem = false;
explicit UdisksObject(const char *_path) noexcept
:path(_path) {}
bool IsValid() const noexcept {
return is_filesystem &&
(!drive_id.empty() || !block_id.empty());
}
std::string GetUri() const noexcept {
if (!drive_id.empty())
return "udisks://" + drive_id;
else if (!block_id.empty())
return "udisks://" + block_id;
else
return {};
}
NeighborInfo ToNeighborInfo() const noexcept {
return {GetUri(), path};
}
};
class UdisksNeighborExplorer final class UdisksNeighborExplorer final
: public NeighborExplorer { : public NeighborExplorer {
@ -115,7 +92,7 @@ private:
void DoOpen(); void DoOpen();
void DoClose() noexcept; void DoClose() noexcept;
void Insert(UdisksObject &&o) noexcept; void Insert(UDisks2::Object &&o) noexcept;
void Remove(const std::string &path) noexcept; void Remove(const std::string &path) noexcept;
void OnListNotify(ODBus::Message reply) noexcept; void OnListNotify(ODBus::Message reply) noexcept;
@ -199,7 +176,7 @@ CheckString(I &&i) noexcept
} }
static void static void
ParseDriveDictEntry(UdisksObject &o, const char *name, ParseDriveDictEntry(UDisks2::Object &o, const char *name,
ODBus::ReadMessageIter &&value_i) noexcept ODBus::ReadMessageIter &&value_i) noexcept
{ {
if (StringIsEqual(name, "Id")) { if (StringIsEqual(name, "Id")) {
@ -210,7 +187,7 @@ ParseDriveDictEntry(UdisksObject &o, const char *name,
} }
static void static void
ParseBlockDictEntry(UdisksObject &o, const char *name, ParseBlockDictEntry(UDisks2::Object &o, const char *name,
ODBus::ReadMessageIter &&value_i) noexcept ODBus::ReadMessageIter &&value_i) noexcept
{ {
if (StringIsEqual(name, "Id")) { if (StringIsEqual(name, "Id")) {
@ -221,7 +198,7 @@ ParseBlockDictEntry(UdisksObject &o, const char *name,
} }
static void static void
ParseInterface(UdisksObject &o, const char *interface, ParseInterface(UDisks2::Object &o, const char *interface,
ODBus::ReadMessageIter &&i) noexcept ODBus::ReadMessageIter &&i) noexcept
{ {
using namespace std::placeholders; using namespace std::placeholders;
@ -237,7 +214,7 @@ ParseInterface(UdisksObject &o, const char *interface,
} }
static void static void
ParseInterfaceDictEntry(UdisksObject &o, ODBus::ReadMessageIter &&i) noexcept ParseInterfaceDictEntry(UDisks2::Object &o, ODBus::ReadMessageIter &&i) noexcept
{ {
if (i.GetArgType() != DBUS_TYPE_STRING) if (i.GetArgType() != DBUS_TYPE_STRING)
return; return;
@ -252,7 +229,7 @@ ParseInterfaceDictEntry(UdisksObject &o, ODBus::ReadMessageIter &&i) noexcept
} }
static bool static bool
ParseObject(UdisksObject &o, ODBus::ReadMessageIter &&i) noexcept ParseObject(UDisks2::Object &o, ODBus::ReadMessageIter &&i) noexcept
{ {
i.ForEach(DBUS_TYPE_DICT_ENTRY, [&o](auto &&j){ i.ForEach(DBUS_TYPE_DICT_ENTRY, [&o](auto &&j){
ParseInterfaceDictEntry(o, j.Recurse()); ParseInterfaceDictEntry(o, j.Recurse());
@ -274,11 +251,11 @@ UdisksNeighborExplorer::GetList() const noexcept
} }
void void
UdisksNeighborExplorer::Insert(UdisksObject &&o) noexcept UdisksNeighborExplorer::Insert(UDisks2::Object &&o) noexcept
{ {
assert(o.IsValid()); assert(o.IsValid());
const NeighborInfo info = o.ToNeighborInfo(); const NeighborInfo info = ToNeighborInfo(o);
{ {
const std::lock_guard<Mutex> protect(mutex); const std::lock_guard<Mutex> protect(mutex);
@ -330,7 +307,7 @@ UdisksNeighborExplorer::OnListNotify(ODBus::Message reply) noexcept
} }
ForEachInterface(i.Recurse(), [this](const char *path, auto &&j){ ForEachInterface(i.Recurse(), [this](const char *path, auto &&j){
UdisksObject o(path); UDisks2::Object o(path);
if (ParseObject(o, std::move(j)) && o.IsValid()) if (ParseObject(o, std::move(j)) && o.IsValid())
Insert(std::move(o)); Insert(std::move(o));
}); });
@ -345,7 +322,7 @@ UdisksNeighborExplorer::HandleMessage(DBusConnection *, DBusMessage *message) no
"InterfacesAdded") && "InterfacesAdded") &&
dbus_message_has_signature(message, InterfacesAddedType::value)) { dbus_message_has_signature(message, InterfacesAddedType::value)) {
RecurseInterfaceDictEntry(ReadMessageIter(*message), [this](const char *path, auto &&i){ RecurseInterfaceDictEntry(ReadMessageIter(*message), [this](const char *path, auto &&i){
UdisksObject o(path); UDisks2::Object o(path);
if (ParseObject(o, std::move(i)) && o.IsValid()) if (ParseObject(o, std::move(i)) && o.IsValid())
Insert(std::move(o)); Insert(std::move(o));
}); });