From eb1e0416af70563550ced70118eb6de47a5b8234 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Thu, 18 Jul 2019 19:53:39 +0800 Subject: [PATCH] Init commit --- .gitignore | 2 + CMakeLists.txt | 24 ++++++++ gio-qt/CMakeLists.txt | 32 ++++++++++ gio-qt/dgiomount.cpp | 109 +++++++++++++++++++++++++++++++++++ gio-qt/dgiomount.h | 32 ++++++++++ gio-qt/dgiovolume.cpp | 6 ++ gio-qt/dgiovolume.h | 11 ++++ gio-qt/dgiovolumemanager.cpp | 58 +++++++++++++++++++ gio-qt/dgiovolumemanager.h | 29 ++++++++++ qgio-tools/CMakeLists.txt | 6 ++ qgio-tools/main.cpp | 22 +++++++ 11 files changed, 331 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 gio-qt/CMakeLists.txt create mode 100644 gio-qt/dgiomount.cpp create mode 100644 gio-qt/dgiomount.h create mode 100644 gio-qt/dgiovolume.cpp create mode 100644 gio-qt/dgiovolume.h create mode 100644 gio-qt/dgiovolumemanager.cpp create mode 100644 gio-qt/dgiovolumemanager.h create mode 100644 qgio-tools/CMakeLists.txt create mode 100644 qgio-tools/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..743eec4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# User +*.user diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d453899 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +project(gio-qt) + +cmake_minimum_required(VERSION 3.9.5) + +option(BUILD_UTILS "Build utilities" ON) + +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) +# Instruct CMake to run moc automatically when needed +set(CMAKE_AUTOMOC ON) +set(CMAKE_CXX_FLAGS "-g -Wall") +set(QT_MINIMUM_VERSION "5.6.3") + +# Find the QtWidgets library +find_package(Qt5 ${QT_MINIMUM_VERSION} CONFIG REQUIRED Core) +find_package(PkgConfig) + +pkg_check_modules(GTKMM gtkmm-3.0) # look into FindPkgConfig.cmake, + +add_subdirectory (gio-qt) + +if (BUILD_UTILS) + add_subdirectory(qgio-tools) +endif() diff --git a/gio-qt/CMakeLists.txt b/gio-qt/CMakeLists.txt new file mode 100644 index 0000000..6507491 --- /dev/null +++ b/gio-qt/CMakeLists.txt @@ -0,0 +1,32 @@ +# Populate a CMake variable with the sources +# TODO: portable headers? +set (QGIO_PUBLIC_HEADER_FILES + dgiovolumemanager.h + dgiomount.h + dgiovolume.h +) + +set (QGIO_PRIVATE_CPP_FILES + dgiovolumemanager.cpp + dgiomount.cpp + dgiovolume.cpp +) + +# Library +add_library (gio-qt STATIC + ${QGIO_PUBLIC_HEADER_FILES} + ${QGIO_PRIVATE_CPP_FILES} +) + +target_include_directories(gio-qt +PRIVATE + ${GTKMM_INCLUDE_DIRS} +PUBLIC + ${CMAKE_CURRENT_LIST_DIR} +) + +target_link_libraries (gio-qt Qt5::Core ${GTKMM_LIBRARIES}) + +target_compile_definitions(gio-qt PRIVATE + QT_NO_KEYWORDS +) diff --git a/gio-qt/dgiomount.cpp b/gio-qt/dgiomount.cpp new file mode 100644 index 0000000..e384269 --- /dev/null +++ b/gio-qt/dgiomount.cpp @@ -0,0 +1,109 @@ +#include "dgiomount.h" + +#include +#include + +#include + +using namespace Gio; + +class DGioMountPrivate +{ +public: + DGioMountPrivate(DGioMount *qq, Mount *gmountPtr); + + Glib::RefPtr getGMountInstance() const; + + QString name() const; + QString uuid() const; + bool canUnmount() const; + bool canEject() const; + +private: + Glib::RefPtr m_gmountPtr; + + DGioMount *q_ptr; + + Q_DECLARE_PUBLIC(DGioMount) +}; + +DGioMountPrivate::DGioMountPrivate(DGioMount *qq, Mount *gmountPtr) + : m_gmountPtr(gmountPtr) + , q_ptr(qq) +{ + +} + +Glib::RefPtr DGioMountPrivate::getGMountInstance() const +{ + return m_gmountPtr; +} + +QString DGioMountPrivate::name() const +{ + return QString::fromStdString(m_gmountPtr->get_name()); +} + +QString DGioMountPrivate::uuid() const +{ + return QString::fromStdString(m_gmountPtr->get_uuid()); +} + +// ------------------------------------------------------------- + +DGioMount::DGioMount(Mount* gmountPtr, QObject *parent) + : QObject(parent) + , d_ptr(new DGioMountPrivate(this, gmountPtr)) +{ + // gmountPtr must be vaild; + Q_CHECK_PTR(gmountPtr); +} + +DGioMount::~DGioMount() +{ + +} + +DGioMount *DGioMount::createFromPath(QString path, QObject *parent) +{ + Glib::RefPtr gfile = File::create_for_path(path.toStdString()); + try { + Glib::RefPtr gmount = gfile->find_enclosing_mount(); + if (gmount) { + return new DGioMount(gmount.release(), parent); + } + } catch (Glib::Error error) { + qDebug() << QString::fromStdString(error.what().raw()); + } + + return nullptr; +} + +QString DGioMount::name() const +{ + Q_D(const DGioMount); + + return d->name(); +} + +QString DGioMount::uuid() const +{ + Q_D(const DGioMount); + + return d->uuid(); +} + +bool DGioMount::canUnmount() const +{ + Q_D(const DGioMount); + + return d->getGMountInstance()->can_unmount(); +} + +bool DGioMount::canEject() const +{ + Q_D(const DGioMount); + + return d->getGMountInstance()->can_eject(); +} + diff --git a/gio-qt/dgiomount.h b/gio-qt/dgiomount.h new file mode 100644 index 0000000..025c0ee --- /dev/null +++ b/gio-qt/dgiomount.h @@ -0,0 +1,32 @@ +#ifndef DGIOMOUNT_H +#define DGIOMOUNT_H + +#include +#include + +namespace Gio { +class Mount; +} + +class DGioMountPrivate; +class DGioMount : public QObject, public QSharedData +{ + Q_OBJECT +public: + explicit DGioMount(Gio::Mount *gmountPtr, QObject *parent = nullptr); + ~DGioMount(); + + static DGioMount * createFromPath(QString path, QObject *parent = nullptr); + + QString name() const; + QString uuid() const; + bool canUnmount() const; + bool canEject() const; + +private: + QScopedPointer d_ptr; + + Q_DECLARE_PRIVATE(DGioMount) +}; + +#endif // DGIOMOUNT_H diff --git a/gio-qt/dgiovolume.cpp b/gio-qt/dgiovolume.cpp new file mode 100644 index 0000000..0c2fd53 --- /dev/null +++ b/gio-qt/dgiovolume.cpp @@ -0,0 +1,6 @@ +#include "dgiovolume.h" + +DGioVolume::DGioVolume() +{ + +} diff --git a/gio-qt/dgiovolume.h b/gio-qt/dgiovolume.h new file mode 100644 index 0000000..088b8b5 --- /dev/null +++ b/gio-qt/dgiovolume.h @@ -0,0 +1,11 @@ +#ifndef DGIOVOLUME_H +#define DGIOVOLUME_H + + +class DGioVolume +{ +public: + DGioVolume(); +}; + +#endif // DGIOVOLUME_H diff --git a/gio-qt/dgiovolumemanager.cpp b/gio-qt/dgiovolumemanager.cpp new file mode 100644 index 0000000..e8f3e37 --- /dev/null +++ b/gio-qt/dgiovolumemanager.cpp @@ -0,0 +1,58 @@ +#include "dgiomount.h" +#include "dgiovolumemanager.h" + +#include +#include + +#include +#include + +using namespace Gio; + +class DGioVolumeManagerPrivate +{ + DGioVolumeManagerPrivate(DGioVolumeManager *qq); + +private: + DGioVolumeManager *q_ptr; + + Q_DECLARE_PUBLIC(DGioVolumeManager) +}; + +DGioVolumeManagerPrivate::DGioVolumeManagerPrivate(DGioVolumeManager *qq) + : q_ptr(qq) +{ + +} + + +DGioVolumeManager::DGioVolumeManager(QObject *parent) + : QObject(parent) + , d_ptr(new DGioVolumeManagerPrivate(this)) +{ + // Do Gio's init or things like Gio::VolumeMonitor::get() won't working + // can we init it multiple times? + Glib::init(); + Gio::init(); +} + +DGioVolumeManager::~DGioVolumeManager() +{ + // +} + +const QList > DGioVolumeManager::getMounts() +{ + QList > mounts; + + Glib::RefPtr vm = Gio::VolumeMonitor::get(); + + Glib::ListHandle> mnt = vm->get_mounts(); + for (Glib::RefPtr oneMnt : mnt) { + + QExplicitlySharedDataPointer mntPtr(new DGioMount(oneMnt.release())); + mounts.append(mntPtr); + } + + return mounts; +} diff --git a/gio-qt/dgiovolumemanager.h b/gio-qt/dgiovolumemanager.h new file mode 100644 index 0000000..56a012f --- /dev/null +++ b/gio-qt/dgiovolumemanager.h @@ -0,0 +1,29 @@ +#ifndef DGIOVOLUMEMANAGER_H +#define DGIOVOLUMEMANAGER_H + +#include +#include + +namespace Gio { +class Mount; +} + +class DGioMount; +class DGioVolume; +class DGioVolumeManagerPrivate; +class DGioVolumeManager : public QObject +{ + Q_OBJECT +public: + explicit DGioVolumeManager(QObject *parent = nullptr); + ~DGioVolumeManager(); + + const QList > getMounts(); + +private: + QScopedPointer d_ptr; + + Q_DECLARE_PRIVATE(DGioVolumeManager) +}; + +#endif // DGIOVOLUMEMANAGER_H diff --git a/qgio-tools/CMakeLists.txt b/qgio-tools/CMakeLists.txt new file mode 100644 index 0000000..d87181a --- /dev/null +++ b/qgio-tools/CMakeLists.txt @@ -0,0 +1,6 @@ +# QXdgDesktopEntryTest +add_executable (qgio-tools + main.cpp +) + +target_link_libraries (qgio-tools gio-qt) diff --git a/qgio-tools/main.cpp b/qgio-tools/main.cpp new file mode 100644 index 0000000..dea2481 --- /dev/null +++ b/qgio-tools/main.cpp @@ -0,0 +1,22 @@ +#include +#include + +#include +#include + +int main() +{ + DGioMount * m = DGioMount::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa"); + if (m) { + qDebug() << m->name(); + } + + DGioVolumeManager mgr; + const QList > lst = mgr.getMounts(); + + for (const QExplicitlySharedDataPointer &p : lst) { + qDebug() << p->name() << m->uuid() << m->canUnmount(); + } + + return 0; +}