Init commit

This commit is contained in:
Gary Wang 2019-07-18 19:53:39 +08:00
commit eb1e0416af
11 changed files with 331 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# User
*.user

24
CMakeLists.txt Normal file
View File

@ -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()

32
gio-qt/CMakeLists.txt Normal file
View File

@ -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
)

109
gio-qt/dgiomount.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "dgiomount.h"
#include <glibmm/refptr.h>
#include <giomm/file.h>
#include <QDebug>
using namespace Gio;
class DGioMountPrivate
{
public:
DGioMountPrivate(DGioMount *qq, Mount *gmountPtr);
Glib::RefPtr<Mount> getGMountInstance() const;
QString name() const;
QString uuid() const;
bool canUnmount() const;
bool canEject() const;
private:
Glib::RefPtr<Mount> m_gmountPtr;
DGioMount *q_ptr;
Q_DECLARE_PUBLIC(DGioMount)
};
DGioMountPrivate::DGioMountPrivate(DGioMount *qq, Mount *gmountPtr)
: m_gmountPtr(gmountPtr)
, q_ptr(qq)
{
}
Glib::RefPtr<Mount> 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<File> gfile = File::create_for_path(path.toStdString());
try {
Glib::RefPtr<Mount> 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();
}

32
gio-qt/dgiomount.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef DGIOMOUNT_H
#define DGIOMOUNT_H
#include <QObject>
#include <QSharedData>
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<DGioMountPrivate> d_ptr;
Q_DECLARE_PRIVATE(DGioMount)
};
#endif // DGIOMOUNT_H

6
gio-qt/dgiovolume.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "dgiovolume.h"
DGioVolume::DGioVolume()
{
}

11
gio-qt/dgiovolume.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef DGIOVOLUME_H
#define DGIOVOLUME_H
class DGioVolume
{
public:
DGioVolume();
};
#endif // DGIOVOLUME_H

View File

@ -0,0 +1,58 @@
#include "dgiomount.h"
#include "dgiovolumemanager.h"
#include <glibmm.h>
#include <giomm.h>
#include <glibmm/refptr.h>
#include <giomm/volumemonitor.h>
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<QExplicitlySharedDataPointer<DGioMount> > DGioVolumeManager::getMounts()
{
QList<QExplicitlySharedDataPointer<DGioMount> > mounts;
Glib::RefPtr<VolumeMonitor> vm = Gio::VolumeMonitor::get();
Glib::ListHandle<Glib::RefPtr<Mount>> mnt = vm->get_mounts();
for (Glib::RefPtr<Mount> oneMnt : mnt) {
QExplicitlySharedDataPointer<DGioMount> mntPtr(new DGioMount(oneMnt.release()));
mounts.append(mntPtr);
}
return mounts;
}

View File

@ -0,0 +1,29 @@
#ifndef DGIOVOLUMEMANAGER_H
#define DGIOVOLUMEMANAGER_H
#include <QExplicitlySharedDataPointer>
#include <QObject>
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<QExplicitlySharedDataPointer<DGioMount> > getMounts();
private:
QScopedPointer<DGioVolumeManagerPrivate> d_ptr;
Q_DECLARE_PRIVATE(DGioVolumeManager)
};
#endif // DGIOVOLUMEMANAGER_H

View File

@ -0,0 +1,6 @@
# QXdgDesktopEntryTest
add_executable (qgio-tools
main.cpp
)
target_link_libraries (qgio-tools gio-qt)

22
qgio-tools/main.cpp Normal file
View File

@ -0,0 +1,22 @@
#include <QDebug>
#include <QExplicitlySharedDataPointer>
#include <dgiomount.h>
#include <dgiovolumemanager.h>
int main()
{
DGioMount * m = DGioMount::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa");
if (m) {
qDebug() << m->name();
}
DGioVolumeManager mgr;
const QList<QExplicitlySharedDataPointer<DGioMount> > lst = mgr.getMounts();
for (const QExplicitlySharedDataPointer<DGioMount> &p : lst) {
qDebug() << p->name() << m->uuid() << m->canUnmount();
}
return 0;
}