file info and mount operation support

This commit is contained in:
Gary Wang 2019-07-23 15:30:14 +08:00
parent 7dfe0ef097
commit 93e9f529af
14 changed files with 379 additions and 12 deletions

View File

@ -11,11 +11,16 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_FLAGS "-g -Wall")
set(QT_MINIMUM_VERSION "5.6.3")
# Install settings
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX /usr)
endif ()
# 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,
pkg_check_modules(GIOMM giomm-2.4) # look into FindPkgConfig.cmake,
add_subdirectory (gio-qt)

View File

@ -2,16 +2,20 @@
# TODO: portable headers?
set (QGIO_PUBLIC_HEADER_FILES
dgiovolumemanager.h
dgiomountoperation.h
dgiomount.h
dgiovolume.h
dgiofile.h
dgiofileinfo.h
)
set (QGIO_PRIVATE_CPP_FILES
dgiovolumemanager.cpp
dgiomountoperation.cpp
dgiomount.cpp
dgiovolume.cpp
dgiofile.cpp
dgiofileinfo.cpp
)
# Library
@ -22,7 +26,7 @@ add_library (gio-qt STATIC
target_include_directories(gio-qt
PRIVATE
${GTKMM_INCLUDE_DIRS}
${GIOMM_INCLUDE_DIRS}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)

View File

@ -1,10 +1,13 @@
#include "dgiofile.h"
#include "dgiofileinfo.h"
#include <glibmm/refptr.h>
#include <giomm/init.h>
#include <giomm/file.h>
#include <QDebug>
using namespace Gio;
class DGioFilePrivate
@ -69,6 +72,7 @@ DGioFile *DGioFile::createFromPath(QString path, QObject *parent)
// ensure GIO got initialized
Gio::init();
// File::create_for_path never falls.
Glib::RefPtr<File> gmmFile = File::create_for_path(path.toStdString());
return new DGioFile(gmmFile.release(), parent);
@ -94,3 +98,20 @@ QString DGioFile::uri() const
return d->uri();
}
QExplicitlySharedDataPointer<DGioFileInfo> DGioFile::createFileSystemInfo()
{
Q_D(DGioFile);
try {
Glib::RefPtr<FileInfo> gmmFileInfo = d->getGmmFileInstance()->query_filesystem_info("filesystem::*");
if (gmmFileInfo) {
QExplicitlySharedDataPointer<DGioFileInfo> fileInfoPtr(new DGioFileInfo(gmmFileInfo.release()));
return fileInfoPtr;
}
} catch (Glib::Error error) {
qDebug() << QString::fromStdString(error.what().raw());
}
return QExplicitlySharedDataPointer<DGioFileInfo>(nullptr);
}

View File

@ -8,6 +8,7 @@ namespace Gio {
class File;
}
class DGioFileInfo;
class DGioFilePrivate;
class DGioFile : public QObject, public QSharedData
{
@ -21,6 +22,7 @@ public:
QString basename() const;
QString path() const;
QString uri() const;
QExplicitlySharedDataPointer<DGioFileInfo> createFileSystemInfo();
private:
QScopedPointer<DGioFilePrivate> d_ptr;

89
gio-qt/dgiofileinfo.cpp Normal file
View File

@ -0,0 +1,89 @@
#include "dgiofileinfo.h"
#include <giomm/fileinfo.h>
using namespace Gio;
class DGioFileInfoPrivate
{
public:
DGioFileInfoPrivate(DGioFileInfo *qq, FileInfo *gmmFileInfoPtr);
Glib::RefPtr<FileInfo> getGmmFileInfoInstance() const;
bool getAttributeBoolean(const std::string &attribute) const;
quint64 getAttributeUint64(const std::string &attribute) const;
private:
Glib::RefPtr<FileInfo> m_gmmFileInfoPtr;
QString uri() const;
DGioFileInfo *q_ptr;
Q_DECLARE_PUBLIC(DGioFileInfo)
};
DGioFileInfoPrivate::DGioFileInfoPrivate(DGioFileInfo *qq, FileInfo *gmmFileInfoPtr)
: m_gmmFileInfoPtr(gmmFileInfoPtr)
, q_ptr(qq)
{
}
Glib::RefPtr<FileInfo> DGioFileInfoPrivate::getGmmFileInfoInstance() const
{
return m_gmmFileInfoPtr;
}
bool DGioFileInfoPrivate::getAttributeBoolean(const std::string &attribute) const
{
return m_gmmFileInfoPtr->get_attribute_boolean(attribute);
}
quint64 DGioFileInfoPrivate::getAttributeUint64(const std::string &attribute) const
{
return m_gmmFileInfoPtr->get_attribute_uint64(attribute);
}
// -------------------------------------------------------------
DGioFileInfo::DGioFileInfo(FileInfo *gmmFileInfoInfoPtr, QObject *parent)
: QObject(parent)
, d_ptr(new DGioFileInfoPrivate(this, gmmFileInfoInfoPtr))
{
}
DGioFileInfo::~DGioFileInfo()
{
}
bool DGioFileInfo::isReadOnly() const
{
Q_D(const DGioFileInfo);
return d->getAttributeBoolean(G_FILE_ATTRIBUTE_FILESYSTEM_READONLY);
}
quint64 DGioFileInfo::fsTotalBytes() const
{
Q_D(const DGioFileInfo);
return d->getAttributeUint64(G_FILE_ATTRIBUTE_FILESYSTEM_SIZE);
}
quint64 DGioFileInfo::fsUsedBytes() const
{
Q_D(const DGioFileInfo);
return d->getAttributeUint64(G_FILE_ATTRIBUTE_FILESYSTEM_USED);
}
quint64 DGioFileInfo::fsFreeBytes() const
{
Q_D(const DGioFileInfo);
return d->getAttributeUint64(G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
}

31
gio-qt/dgiofileinfo.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef DGIOFILEINFO_H
#define DGIOFILEINFO_H
#include <QObject>
#include <QSharedData>
namespace Gio {
class FileInfo;
}
class DGioFileInfoPrivate;
class DGioFileInfo : public QObject, public QSharedData
{
Q_OBJECT
public:
explicit DGioFileInfo(Gio::FileInfo *gmmFileInfoInfoPtr, QObject *parent = nullptr);
~DGioFileInfo();
bool isReadOnly() const;
quint64 fsTotalBytes() const;
quint64 fsUsedBytes() const;
quint64 fsFreeBytes() const;
private:
QScopedPointer<DGioFileInfoPrivate> d_ptr;
Q_DECLARE_PRIVATE(DGioFileInfo)
};
#endif // DGIOFILEINFO_H

View File

@ -1,5 +1,6 @@
#include "dgiomount.h"
#include "dgiovolume.h"
#include "dgiofile.h"
#include <glibmm/refptr.h>
@ -198,13 +199,36 @@ void DGioMount::unmount(bool forceUnmount)
return d->getGmmMountInstance()->unmount(forceUnmount ? MOUNT_UNMOUNT_FORCE : MOUNT_UNMOUNT_NONE);
}
QExplicitlySharedDataPointer<DGioFile> DGioMount::getRootFile()
{
Q_D(const DGioMount);
Glib::RefPtr<File> file = d->getGmmMountInstance()->get_root();
QExplicitlySharedDataPointer<DGioFile> filePtr(new DGioFile(file.release()));
return filePtr;
}
QExplicitlySharedDataPointer<DGioFile> DGioMount::getDefaultLocationFile()
{
Q_D(const DGioMount);
Glib::RefPtr<File> file = d->getGmmMountInstance()->get_default_location();
QExplicitlySharedDataPointer<DGioFile> filePtr(new DGioFile(file.release()));
return filePtr;
}
QExplicitlySharedDataPointer<DGioVolume> DGioMount::getVolume()
{
Q_D(const DGioMount);
Glib::RefPtr<Volume> vol = d->getGmmMountInstance()->get_volume();
QExplicitlySharedDataPointer<DGioVolume> volPtr(new DGioVolume(vol.release()));
if (vol) {
QExplicitlySharedDataPointer<DGioVolume> volPtr(new DGioVolume(vol.release()));
return volPtr;
}
return volPtr;
return QExplicitlySharedDataPointer<DGioVolume>(nullptr);
}

View File

@ -8,6 +8,7 @@ namespace Gio {
class Mount;
}
class DGioFile;
class DGioVolume;
class DGioMountPrivate;
class DGioMount : public QObject, public QSharedData
@ -28,6 +29,8 @@ public:
void unmount(bool forceUnmount = false);
QExplicitlySharedDataPointer<DGioFile> getRootFile();
QExplicitlySharedDataPointer<DGioFile> getDefaultLocationFile();
QExplicitlySharedDataPointer<DGioVolume> getVolume();
private:

View File

@ -0,0 +1,105 @@
#include "dgiomountoperation.h"
#include <giomm/mountoperation.h>
using namespace Gio;
class DGioMountOperationPrivate
{
DGioMountOperationPrivate(DGioMountOperation *qq);
Glib::RefPtr<MountOperation> getGmmMountOperationInstance() const;
QString username() const;
void slot_askPassword(const Glib::ustring& message, const Glib::ustring& default_user, const Glib::ustring& default_domain, AskPasswordFlags flags);
void slot_askQuestion(const Glib::ustring& message, const Glib::StringArrayHandle& choices);
void slot_showUnmountProgress(const Glib::ustring &message, gint64 time_left, gint64 bytes_left);
private:
Glib::RefPtr<MountOperation> m_gmmMountOperationPtr;
DGioMountOperation *q_ptr;
Q_DECLARE_PUBLIC(DGioMountOperation)
};
DGioMountOperationPrivate::DGioMountOperationPrivate(DGioMountOperation *qq)
: q_ptr(qq)
{
m_gmmMountOperationPtr = Gio::MountOperation::create();
m_gmmMountOperationPtr->signal_ask_password().connect(sigc::mem_fun(*this, &DGioMountOperationPrivate::slot_askPassword));
m_gmmMountOperationPtr->signal_ask_question().connect(sigc::mem_fun(*this, &DGioMountOperationPrivate::slot_askQuestion));
m_gmmMountOperationPtr->signal_show_unmount_progress().connect(sigc::mem_fun(*this, &DGioMountOperationPrivate::slot_showUnmountProgress));
}
Glib::RefPtr<MountOperation> DGioMountOperationPrivate::getGmmMountOperationInstance() const
{
return m_gmmMountOperationPtr;
}
QString DGioMountOperationPrivate::username() const
{
return QString::fromStdString(m_gmmMountOperationPtr->get_username().raw());
}
void DGioMountOperationPrivate::slot_askPassword(const Glib::ustring &message, const Glib::ustring &default_user, const Glib::ustring &default_domain, AskPasswordFlags flags)
{
Q_Q(DGioMountOperation);
QString msg = QString::fromStdString(message.raw());
QString defaultUser = QString::fromStdString(default_user.raw());
QString defaultDomain = QString::fromStdString(default_domain.raw());
DGioAskPasswordFlags askPasswordFlags = static_cast<DGioAskPasswordFlags>(flags);
Q_EMIT q->askPassword(msg, defaultUser, defaultDomain, askPasswordFlags);
}
void DGioMountOperationPrivate::slot_askQuestion(const Glib::ustring &message, const Glib::StringArrayHandle &choices)
{
Q_Q(DGioMountOperation);
QString msg = QString::fromStdString(message.raw());
QStringList choiceList;
for (auto oneChoice : choices) {
choiceList.append(QString::fromStdString(oneChoice.raw()));
}
Q_EMIT q->askQuestion(msg, choiceList);
}
void DGioMountOperationPrivate::slot_showUnmountProgress(const Glib::ustring& message, gint64 time_left, gint64 bytes_left)
{
Q_Q(DGioMountOperation);
Q_EMIT q->showUnmountProgress(QString::fromStdString(message.raw()), time_left, bytes_left);
}
// -------------------------------------------------------------
DGioMountOperation::DGioMountOperation(QObject *parent)
: QObject(parent)
, d_ptr(new DGioMountOperationPrivate(this))
{
}
DGioMountOperation::~DGioMountOperation()
{
}
QString DGioMountOperation::username() const
{
Q_D(const DGioMountOperation);
return d->username();
}
void DGioMountOperation::reply(DGioMountOperationResult result)
{
Q_D(DGioMountOperation);
d->getGmmMountOperationInstance()->reply(static_cast<MountOperationResult>(result));
}

View File

@ -0,0 +1,47 @@
#ifndef DGIOMOUNTOPERATION_H
#define DGIOMOUNTOPERATION_H
#include <QObject>
enum DGioAskPasswordFlag
{
ASK_PASSWORD_NEED_PASSWORD = (1 << 0),
ASK_PASSWORD_NEED_USERNAME = (1 << 1),
ASK_PASSWORD_NEED_DOMAIN = (1 << 2),
ASK_PASSWORD_SAVING_SUPPORTED = (1 << 3),
ASK_PASSWORD_ANONYMOUS_SUPPORTED = (1 << 4)
};
Q_DECLARE_FLAGS(DGioAskPasswordFlags, DGioAskPasswordFlag)
enum DGioMountOperationResult
{
MOUNT_OPERATION_HANDLED,
MOUNT_OPERATION_ABORTED,
MOUNT_OPERATION_UNHANDLED
};
Q_ENUMS(DGioMountOperationResult);
class DGioMountOperationPrivate;
class DGioMountOperation : public QObject
{
Q_OBJECT
public:
explicit DGioMountOperation(QObject *parent);
~DGioMountOperation();
QString username() const;
void reply(DGioMountOperationResult result);
Q_SIGNALS:
void askPassword(QString message, QString defaultUser, QString defaultDomain, DGioAskPasswordFlags flags);
void askQuestion(QString message, QStringList choices);
void showUnmountProgress(QString message, qint64 timeLeftMs, qint64 bytesLeft);
private:
QScopedPointer<DGioMountOperationPrivate> d_ptr;
Q_DECLARE_PRIVATE(DGioMountOperation)
};
#endif // DGIOMOUNTOPERATION_H

View File

@ -1,3 +1,4 @@
#include "dgiomount.h"
#include "dgiovolume.h"
#include <glibmm/refptr.h>
@ -12,7 +13,7 @@ class DGioVolumePrivate
public:
DGioVolumePrivate(DGioVolume *qq, Volume *gmmVolumePtr);
Glib::RefPtr<Mount> getGmmVolumeInstance() const;
Glib::RefPtr<Volume> getGmmVolumeInstance() const;
QString name() const;
@ -28,7 +29,12 @@ DGioVolumePrivate::DGioVolumePrivate(DGioVolume *qq, Volume *gmmVolumePtr)
: m_gmmVolumePtr(gmmVolumePtr)
, q_ptr(qq)
{
// m_gvolumePtr = Glib::wrap(gvolumePtr);
// m_gvolumePtr = Glib::wrap(gvolumePtr);
}
Glib::RefPtr<Volume> DGioVolumePrivate::getGmmVolumeInstance() const
{
return m_gmmVolumePtr;
}
QString DGioVolumePrivate::name() const
@ -57,3 +63,14 @@ QString DGioVolume::name() const
return d->name();
}
// Return value can be nullptr
QExplicitlySharedDataPointer<DGioMount> DGioVolume::getMount()
{
Q_D(DGioVolume);
Glib::RefPtr<Mount> mnt = d->getGmmVolumeInstance()->get_mount();
QExplicitlySharedDataPointer<DGioMount> mntPtr(new DGioMount(mnt.release()));
return mntPtr;
}

View File

@ -8,6 +8,7 @@ namespace Gio {
class Volume;
}
class DGioMount;
class DGioVolumePrivate;
class DGioVolume : public QObject, public QSharedData
{
@ -18,6 +19,8 @@ public:
QString name() const;
QExplicitlySharedDataPointer<DGioMount> getMount();
private:
QScopedPointer<DGioVolumePrivate> d_ptr;

View File

@ -85,6 +85,7 @@ void DGioVolumeManagerPrivate::slot_mountChanged(const Glib::RefPtr<Mount> &gmmM
Q_EMIT q->mountChanged(mount);
}
// -------------------------------------------------------------
DGioVolumeManager::DGioVolumeManager(QObject *parent)
: QObject(parent)

View File

@ -6,26 +6,41 @@
#include <dgiomount.h>
#include <dgiovolume.h>
#include <dgiovolumemanager.h>
#include <dgiofileinfo.h>
int main(int argc, char * argv[])
{
DGioMount * m = DGioMount::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa");
if (m) {
qDebug() << m->name() << m->themedIconNames();
delete m;
}
DGioFile * f = DGioFile::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa");
if (f) {
qDebug() << f->basename() << f->path() << f->uri();
QExplicitlySharedDataPointer<DGioFileInfo> fi = f->createFileSystemInfo();
if (fi) {
qDebug() << fi->fsFreeBytes() << fi->fsUsedBytes() << fi->fsTotalBytes();
}
delete f;
}
qDebug() << "----------------------";
DGioMount * m = DGioMount::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa");
if (m) {
QExplicitlySharedDataPointer<DGioFile> f = m->getRootFile();
QExplicitlySharedDataPointer<DGioFile> f2 = m->getDefaultLocationFile();
qDebug() << m->name() << m->themedIconNames() << f->createFileSystemInfo()->fsTotalBytes() << f->uri() << f2->uri();
qDebug() << m->name() << m->themedIconNames() << f->createFileSystemInfo()->fsTotalBytes() << f->uri() << f2->uri();
// m->unmount();
delete m;
}
qDebug() << "----------------------";
const QList<QExplicitlySharedDataPointer<DGioMount> > mnts = DGioVolumeManager::getMounts();
for (const QExplicitlySharedDataPointer<DGioMount> &p : mnts) {
QExplicitlySharedDataPointer<DGioFile> f = p->getRootFile();
QExplicitlySharedDataPointer<DGioFile> f2 = p->getDefaultLocationFile();
qDebug() << f->uri() << f2->uri() << f->path() << f2->path();
qDebug() << f->uri() << f2->uri();
qDebug() << p->name() << p->uuid() << p->canUnmount() << p->themedIconNames() << p->themedIconNames();
}