From 238173e860937095cebf588c961c538c31fea593 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 25 Jul 2019 14:09:22 +0800 Subject: [PATCH] feat: volume signals, file create from uri (#1) --- gio-qt/dgiofile.cpp | 23 +++++++++++++++++++++ gio-qt/dgiofile.h | 1 + gio-qt/dgiovolumemanager.cpp | 40 ++++++++++++++++++++++++++++++++++++ gio-qt/dgiovolumemanager.h | 3 +++ 4 files changed, 67 insertions(+) diff --git a/gio-qt/dgiofile.cpp b/gio-qt/dgiofile.cpp index dec6298..ad8dfa8 100644 --- a/gio-qt/dgiofile.cpp +++ b/gio-qt/dgiofile.cpp @@ -65,6 +65,8 @@ DGioFile::~DGioFile() * This operation never fails since Gio::File::create_for_path never fails, but the returned * object might not support any I/O operation if path is malformed. * + * Caller take the ownership of the created object, you can also provide a \a parent object. + * * \return the created DGioFile instance */ DGioFile *DGioFile::createFromPath(QString path, QObject *parent) @@ -78,6 +80,27 @@ DGioFile *DGioFile::createFromPath(QString path, QObject *parent) return new DGioFile(gmmFile.release(), parent); } +/*! + * \brief Create a DGioFile instance by given \a uri + * + * This operation never fails since Gio::File::create_for_uri never fails, but the returned + * object might not support any I/O operation if uri is malformed. + * + * Caller take the ownership of the created object, you can also provide a \a parent object. + * + * \return the created DGioFile instance + */ +DGioFile *DGioFile::createFromUri(QString uri, QObject *parent) +{ + // ensure GIO got initialized + Gio::init(); + + // File::create_for_path never falls. + Glib::RefPtr gmmFile = File::create_for_uri(uri.toStdString()); + + return new DGioFile(gmmFile.release(), parent); +} + QString DGioFile::basename() const { Q_D(const DGioFile); diff --git a/gio-qt/dgiofile.h b/gio-qt/dgiofile.h index 8b76f28..60b4c63 100644 --- a/gio-qt/dgiofile.h +++ b/gio-qt/dgiofile.h @@ -18,6 +18,7 @@ public: ~DGioFile(); static DGioFile * createFromPath(QString path, QObject *parent = nullptr); + static DGioFile * createFromUri(QString uri, QObject *parent = nullptr); QString basename() const; QString path() const; diff --git a/gio-qt/dgiovolumemanager.cpp b/gio-qt/dgiovolumemanager.cpp index c39f9d4..a9f4332 100644 --- a/gio-qt/dgiovolumemanager.cpp +++ b/gio-qt/dgiovolumemanager.cpp @@ -22,6 +22,9 @@ private: void slot_mountRemoved(const Glib::RefPtr< Mount >& gmmMount); void slot_mountPreRemoved(const Glib::RefPtr< Mount >& gmmMount); void slot_mountChanged(const Glib::RefPtr< Mount >& gmmMount); + void slot_volumeAdded(const Glib::RefPtr< Volume >& gmmVolume); + void slot_volumeRemoved(const Glib::RefPtr< Volume >& gmmVolume); + void slot_volumeChanged(const Glib::RefPtr< Volume >& gmmVolume); Q_DECLARE_PUBLIC(DGioVolumeManager) }; @@ -39,6 +42,10 @@ DGioVolumeManagerPrivate::DGioVolumeManagerPrivate(DGioVolumeManager *qq) m_gmmVolumeMonitorPtr->signal_mount_removed().connect(sigc::mem_fun(*this, &DGioVolumeManagerPrivate::slot_mountRemoved)); m_gmmVolumeMonitorPtr->signal_mount_pre_unmount().connect(sigc::mem_fun(*this, &DGioVolumeManagerPrivate::slot_mountPreRemoved)); m_gmmVolumeMonitorPtr->signal_mount_changed().connect(sigc::mem_fun(*this, &DGioVolumeManagerPrivate::slot_mountChanged)); + + m_gmmVolumeMonitorPtr->signal_volume_added().connect(sigc::mem_fun(*this, &DGioVolumeManagerPrivate::slot_volumeAdded)); + m_gmmVolumeMonitorPtr->signal_volume_removed().connect(sigc::mem_fun(*this, &DGioVolumeManagerPrivate::slot_volumeRemoved)); + m_gmmVolumeMonitorPtr->signal_volume_changed().connect(sigc::mem_fun(*this, &DGioVolumeManagerPrivate::slot_volumeChanged)); } void DGioVolumeManagerPrivate::slot_mountAdded(const Glib::RefPtr &gmmMount) @@ -85,6 +92,39 @@ void DGioVolumeManagerPrivate::slot_mountChanged(const Glib::RefPtr &gmmM Q_EMIT q->mountChanged(mount); } +void DGioVolumeManagerPrivate::slot_volumeAdded(const Glib::RefPtr &gmmVolume) +{ + Q_Q(DGioVolumeManager); + + Glib::RefPtr copy(gmmVolume); + + QExplicitlySharedDataPointer volume(new DGioVolume(copy.release())); + + Q_EMIT q->volumeAdded(volume); +} + +void DGioVolumeManagerPrivate::slot_volumeRemoved(const Glib::RefPtr &gmmVolume) +{ + Q_Q(DGioVolumeManager); + + Glib::RefPtr copy(gmmVolume); + + QExplicitlySharedDataPointer volume(new DGioVolume(copy.release())); + + Q_EMIT q->volumeRemoved(volume); +} + +void DGioVolumeManagerPrivate::slot_volumeChanged(const Glib::RefPtr &gmmVolume) +{ + Q_Q(DGioVolumeManager); + + Glib::RefPtr copy(gmmVolume); + + QExplicitlySharedDataPointer volume(new DGioVolume(copy.release())); + + Q_EMIT q->volumeChanged(volume); +} + // ------------------------------------------------------------- DGioVolumeManager::DGioVolumeManager(QObject *parent) diff --git a/gio-qt/dgiovolumemanager.h b/gio-qt/dgiovolumemanager.h index 2898fcf..8d2a2e5 100644 --- a/gio-qt/dgiovolumemanager.h +++ b/gio-qt/dgiovolumemanager.h @@ -22,6 +22,9 @@ Q_SIGNALS: void mountRemoved(QExplicitlySharedDataPointer mount); void mountPreRemoved(QExplicitlySharedDataPointer mount); void mountChanged(QExplicitlySharedDataPointer mount); + void volumeAdded(QExplicitlySharedDataPointer volume); + void volumeRemoved(QExplicitlySharedDataPointer volume); + void volumeChanged(QExplicitlySharedDataPointer volume); private: QScopedPointer d_ptr;