feat: DGioFileIterator and more function implementation

(#6)
This commit is contained in:
Gary Wang 2019-07-29 15:22:51 +08:00 committed by GitHub
parent ce4a9dfb26
commit 31087054fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 223 additions and 4 deletions

View File

@ -11,6 +11,7 @@ set (QGIO_PUBLIC_HEADER_FILES
dgiofile.h dgiofile.h
dgiofileinfo.h dgiofileinfo.h
dgioutils.h dgioutils.h
dgiofileiterator.h
) )
set (QGIO_PRIVATE_CPP_FILES set (QGIO_PRIVATE_CPP_FILES
@ -21,6 +22,7 @@ set (QGIO_PRIVATE_CPP_FILES
dgiofile.cpp dgiofile.cpp
dgiofileinfo.cpp dgiofileinfo.cpp
dgioutils.cpp dgioutils.cpp
dgiofileiterator.cpp
) )
# Library # Library

View File

@ -1,10 +1,12 @@
#include "dgiofile.h" #include "dgiofile.h"
#include "dgiofileinfo.h" #include "dgiofileinfo.h"
#include "dgiofileiterator.h"
#include <glibmm/refptr.h> #include <glibmm/refptr.h>
#include <giomm/init.h> #include <giomm/init.h>
#include <giomm/file.h> #include <giomm/file.h>
#include <giomm/fileenumerator.h>
#include <QDebug> #include <QDebug>
@ -122,6 +124,13 @@ QString DGioFile::uri() const
return d->uri(); return d->uri();
} }
/*!
* \brief Obtains information about the filesystem the file is on.
*
* Wrapper of Gio::File::query_filesystem_info("filesystem::*").
*
* \return the created file system info object, or nullptr if create failed.
*/
QExplicitlySharedDataPointer<DGioFileInfo> DGioFile::createFileSystemInfo() QExplicitlySharedDataPointer<DGioFileInfo> DGioFile::createFileSystemInfo()
{ {
Q_D(DGioFile); Q_D(DGioFile);
@ -138,3 +147,37 @@ QExplicitlySharedDataPointer<DGioFileInfo> DGioFile::createFileSystemInfo()
return QExplicitlySharedDataPointer<DGioFileInfo>(nullptr); return QExplicitlySharedDataPointer<DGioFileInfo>(nullptr);
} }
/*!
* \brief Gets the requested information about the files in a directory.
*
* Wrapper of Gio::File::enumerate_children()
*
* The attribute value is a string that specifies the file attributes that should be gathered.
* It is not an error if its not possible to read a particular requested attribute from a file,
* it just won't be set. attribute should be a comma-separated list of attribute or attribute
* wildcards. The wildcard "*" means all attributes, and a wildcard like "standard::*" means all
* attributes in the standard namespace. An example attribute query be "standard::*,owner::user".
* The standard attributes are available as defines, like FILE_ATTRIBUTE_STANDARD_NAME.
*
* \param attr An attribute query string.
* \param queryInfoFlags
* \return the created DGioFileIterator object, or nullptr if failed.
*/
QExplicitlySharedDataPointer<DGioFileIterator> DGioFile::createFileIterator(QString attr, DGioFileQueryInfoFlags queryInfoFlags)
{
Q_D(DGioFile);
try {
unsigned int flagValue = queryInfoFlags;
FileQueryInfoFlags flags = static_cast<FileQueryInfoFlags>(flagValue);
Glib::RefPtr<FileEnumerator> gmmFileEnumerator = d->getGmmFileInstance()->enumerate_children(attr.toStdString(), flags);
QExplicitlySharedDataPointer<DGioFileIterator> fileIterPtr(new DGioFileIterator(gmmFileEnumerator.release()));
return fileIterPtr;
} catch (const Glib::Error &error) {
qDebug() << QString::fromStdString(error.what().raw());
}
return QExplicitlySharedDataPointer<DGioFileIterator>(nullptr);
}

View File

@ -8,7 +8,16 @@ namespace Gio {
class File; class File;
} }
enum DGioFileQueryInfoFlag
{
FILE_QUERY_INFO_NONE = 0x0,
FILE_QUERY_INFO_NOFOLLOW_SYMLINKS = (1 << 0)
};
Q_DECLARE_FLAGS(DGioFileQueryInfoFlags, DGioFileQueryInfoFlag)
class DGioFileInfo; class DGioFileInfo;
class DGioFileIterator;
class DGioFilePrivate; class DGioFilePrivate;
class DGioFile : public QObject, public QSharedData class DGioFile : public QObject, public QSharedData
{ {
@ -24,6 +33,7 @@ public:
QString path() const; QString path() const;
QString uri() const; QString uri() const;
QExplicitlySharedDataPointer<DGioFileInfo> createFileSystemInfo(); QExplicitlySharedDataPointer<DGioFileInfo> createFileSystemInfo();
QExplicitlySharedDataPointer<DGioFileIterator> createFileIterator(QString attr, DGioFileQueryInfoFlags queryInfoFlags = FILE_QUERY_INFO_NONE);
private: private:
QScopedPointer<DGioFilePrivate> d_ptr; QScopedPointer<DGioFilePrivate> d_ptr;

View File

@ -13,6 +13,7 @@ public:
bool getAttributeBoolean(const std::string &attribute) const; bool getAttributeBoolean(const std::string &attribute) const;
quint64 getAttributeUint64(const std::string &attribute) const; quint64 getAttributeUint64(const std::string &attribute) const;
QString getAttributeString(const std::string &attribute) const;
private: private:
Glib::RefPtr<FileInfo> m_gmmFileInfoPtr; Glib::RefPtr<FileInfo> m_gmmFileInfoPtr;
@ -46,6 +47,11 @@ quint64 DGioFileInfoPrivate::getAttributeUint64(const std::string &attribute) co
return m_gmmFileInfoPtr->get_attribute_uint64(attribute); return m_gmmFileInfoPtr->get_attribute_uint64(attribute);
} }
QString DGioFileInfoPrivate::getAttributeString(const std::string &attribute) const
{
return QString::fromStdString(m_gmmFileInfoPtr->get_attribute_string(attribute));
}
// ------------------------------------------------------------- // -------------------------------------------------------------
DGioFileInfo::DGioFileInfo(FileInfo *gmmFileInfoInfoPtr, QObject *parent) DGioFileInfo::DGioFileInfo(FileInfo *gmmFileInfoInfoPtr, QObject *parent)

View File

@ -0,0 +1,72 @@
#include "dgiofileiterator.h"
#include "dgiofileinfo.h"
#include <glibmm/refptr.h>
#include <giomm/init.h>
#include <giomm/file.h>
#include <QDebug>
using namespace Gio;
class DGioFileIteratorPrivate
{
public:
DGioFileIteratorPrivate(DGioFileIterator *qq, FileEnumerator *gmmFileEnumeratorPtr);
Glib::RefPtr<FileEnumerator> getGmmFileEnumeratorInstance() const;
private:
Glib::RefPtr<FileEnumerator> m_gmmFileEnumeratorPtr;
DGioFileIterator *q_ptr;
Q_DECLARE_PUBLIC(DGioFileIterator)
};
DGioFileIteratorPrivate::DGioFileIteratorPrivate(DGioFileIterator *qq, FileEnumerator *gmmFileEnumeratorPtr)
: m_gmmFileEnumeratorPtr(gmmFileEnumeratorPtr)
, q_ptr(qq)
{
}
Glib::RefPtr<FileEnumerator> DGioFileIteratorPrivate::getGmmFileEnumeratorInstance() const
{
return m_gmmFileEnumeratorPtr;
}
// -------------------------------------------------------------
DGioFileIterator::DGioFileIterator(FileEnumerator* gmmFileEnumeratorPtr, QObject *parent)
: QObject(parent)
, d_ptr(new DGioFileIteratorPrivate(this, gmmFileEnumeratorPtr))
{
// gmmFileEnumeratorPtr must be valid;
Q_CHECK_PTR(gmmFileEnumeratorPtr);
}
DGioFileIterator::~DGioFileIterator()
{
}
QExplicitlySharedDataPointer<DGioFileInfo> DGioFileIterator::next()
{
Q_D(DGioFileIterator);
try {
// can return nullptr if there is no file left.
Glib::RefPtr<FileInfo> gmmFileInfoPtr = d->getGmmFileEnumeratorInstance()->next_file();
if (gmmFileInfoPtr) {
QExplicitlySharedDataPointer<DGioFileInfo> fileInfo(new DGioFileInfo(gmmFileInfoPtr.release()));
return fileInfo;
}
} catch (const Glib::Error &error) {
qDebug() << QString::fromStdString(error.what().raw());
}
return QExplicitlySharedDataPointer<DGioFileInfo>(nullptr);
}

28
gio-qt/dgiofileiterator.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef DGIOFILEITERATOR_H
#define DGIOFILEITERATOR_H
#include <QObject>
#include <QSharedData>
namespace Gio {
class FileEnumerator;
}
class DGioFileInfo;
class DGioFileIteratorPrivate;
class DGioFileIterator : public QObject, public QSharedData
{
Q_OBJECT
public:
explicit DGioFileIterator(Gio::FileEnumerator *gmmFileEnumeratorPtr, QObject *parent = nullptr);
~DGioFileIterator();
QExplicitlySharedDataPointer<DGioFileInfo> next();
private:
QScopedPointer<DGioFileIteratorPrivate> d_ptr;
Q_DECLARE_PRIVATE(DGioFileIterator)
};
#endif // DGIOFILEITERATOR_H

View File

@ -192,6 +192,31 @@ QStringList DGioMount::themedIconNames() const
return iconNames; return iconNames;
} }
QStringList DGioMount::themedSymbolicIconNames() const
{
Q_D(const DGioMount);
QStringList iconNames;
Glib::RefPtr<const Icon> icon = d->getGmmMountInstance()->get_symbolic_icon();
Glib::RefPtr<const ThemedIcon> themedIcon = Glib::RefPtr<const ThemedIcon>::cast_dynamic(icon);
if (themedIcon) {
QStringList iconNames;
char **names;
char **iter;
names = NULL;
g_object_get(G_THEMED_ICON(themedIcon->gobj()), "names", &names, NULL);
for (iter = names; *iter; iter++) {
iconNames.append(QString(*iter));
}
g_strfreev(names);
return iconNames;
}
return iconNames;
}
void DGioMount::unmount(bool forceUnmount) void DGioMount::unmount(bool forceUnmount)
{ {
Q_D(const DGioMount); Q_D(const DGioMount);

View File

@ -26,6 +26,7 @@ public:
bool canUnmount() const; bool canUnmount() const;
bool canEject() const; bool canEject() const;
QStringList themedIconNames() const; QStringList themedIconNames() const;
QStringList themedSymbolicIconNames() const;
void unmount(bool forceUnmount = false); void unmount(bool forceUnmount = false);

View File

@ -51,7 +51,7 @@ void DGioMountOperationPrivate::slot_askPassword(const Glib::ustring &message, c
QString msg = QString::fromStdString(message.raw()); QString msg = QString::fromStdString(message.raw());
QString defaultUser = QString::fromStdString(default_user.raw()); QString defaultUser = QString::fromStdString(default_user.raw());
QString defaultDomain = QString::fromStdString(default_domain.raw()); QString defaultDomain = QString::fromStdString(default_domain.raw());
DGioAskPasswordFlags askPasswordFlags = static_cast<DGioAskPasswordFlags>(flags); DGioAskPasswordFlags askPasswordFlags(static_cast<DGioAskPasswordFlag>(flags));
Q_EMIT q->askPassword(msg, defaultUser, defaultDomain, askPasswordFlags); Q_EMIT q->askPassword(msg, defaultUser, defaultDomain, askPasswordFlags);
} }

View File

@ -54,3 +54,19 @@ QString DGioUtils::userDataDir()
{ {
return QString::fromStdString(Glib::get_user_data_dir()); return QString::fromStdString(Glib::get_user_data_dir());
} }
/*!
* \brief Gets the directory to use for temporary files.
*
* Wrapper of Glib::get_user_data_dir(), behavior similar (but not the same) to QDir::temp() or
* QStandardPaths::writableLocation(QStandardPaths::TempLocation).
*
* This is found from inspecting the environment variables TMPDIR, TMP, and TEMP in that order. If none of those are defined
* "/tmp" is returned on UNIX and "C:\\" on Windows.
*
* \return The directory to use for temporary files.
*/
QString DGioUtils::tmpDir()
{
return QString::fromStdString(Glib::get_tmp_dir());
}

View File

@ -23,6 +23,7 @@ public:
static QString userSpecialDir(DGioUserDirectory userDirectory); static QString userSpecialDir(DGioUserDirectory userDirectory);
static QStringList systemDataDirs(); static QStringList systemDataDirs();
static QString userDataDir(); static QString userDataDir();
static QString tmpDir();
}; };
#endif // DGIOUTILS_H #endif // DGIOUTILS_H

View File

@ -8,6 +8,7 @@
#include <dgiovolume.h> #include <dgiovolume.h>
#include <dgiovolumemanager.h> #include <dgiovolumemanager.h>
#include <dgiofileinfo.h> #include <dgiofileinfo.h>
#include <dgiofileiterator.h>
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
@ -15,19 +16,33 @@ int main(int argc, char * argv[])
qDebug() << "----------------------"; qDebug() << "----------------------";
DGioFile * f = DGioFile::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa"); DGioFile *networkFile = DGioFile::createFromUri("network:///");
if (networkFile) {
QExplicitlySharedDataPointer<DGioFileIterator> iter = networkFile->createFileIterator("standard::*,mountable::can-mount");
if (iter) {
while (QExplicitlySharedDataPointer<DGioFileInfo> fi = iter->next()) {
if (fi) {
qDebug() << fi->displayName() << fi->fileType();
}
}
}
}
qDebug() << "----------------------";
DGioFile * f = DGioFile::createFromPath("/media/wzc/_dde_data");
if (f) { if (f) {
qDebug() << f->basename() << f->path() << f->uri(); qDebug() << f->basename() << f->path() << f->uri();
QExplicitlySharedDataPointer<DGioFileInfo> fi = f->createFileSystemInfo(); QExplicitlySharedDataPointer<DGioFileInfo> fi = f->createFileSystemInfo();
if (fi) { if (fi) {
qDebug() << fi->fsFreeBytes() << fi->fsUsedBytes() << fi->fsTotalBytes(); qDebug() << fi->fsFreeBytes() << fi->fsUsedBytes() << fi->fsTotalBytes() << fi->displayName() << fi->displayName();
} }
delete f; delete f;
} }
qDebug() << "----------------------"; qDebug() << "----------------------";
DGioMount * m = DGioMount::createFromPath("/media/wzc/aaaaaaaaaaaaaaaa"); DGioMount * m = DGioMount::createFromPath("/media/wzc/_dde_data");
if (m) { if (m) {
QExplicitlySharedDataPointer<DGioFile> f = m->getRootFile(); QExplicitlySharedDataPointer<DGioFile> f = m->getRootFile();
QExplicitlySharedDataPointer<DGioFile> f2 = m->getDefaultLocationFile(); QExplicitlySharedDataPointer<DGioFile> f2 = m->getDefaultLocationFile();