dtkio
dtk library for dtkio.
载入中...
搜索中...
未找到
dtkio

dtk 文件基本业务模块

项目介绍

dtkio是为 deepin/UOS 系统上的通用 I/O程序功能提供有用类,为应用层开发者提供了文件操作常见业务功能,其目的是在于方便第三方开发者轻松且快速的调用接口进行开发。
接口文档

项目结构

对外暴露出 dfileerror.hdfilefuture.hdenumerator.hdfile.hdfileinfo.hdfilemonitor.hdfileoperator.hdtkio_global.hdtkiotypes.h这9 个头文件。

如何使用项目

如果要使用此项目,可以阅读相关文档再参照以下示例进行调用。

使用示例

可以参考以下 Demo:

main.cpp

// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QMetaProperty>
#include <QDebug>
#include <DFile>
#include <DFileInfo>
#include <DFileOperator>
DIO_USE_NAMESPACE
QUrl localFileUrl(const QString &filename)
{
QString filePath = QCoreApplication::applicationDirPath() + "/" + filename;
return QUrl::fromLocalFile(filePath);
}
bool exist(const QUrl &url)
{
DFile *file = new DFile(url);
if (!file)
return false;
auto expected = file->exists();
if (expected) {
if (!expected.value()) {
qInfo() << "file does not exist";
} else {
delete file;
return true;
}
} else {
qWarning() << expected.error();
}
delete file;
return false;
}
void hanldeResult(const DTK_CORE_NAMESPACE::DExpected<bool> &expected)
{
if (expected) {
if (expected.value())
qInfo() << "Success";
else
qInfo() << "Fail";
} else {
qWarning() << expected.error();
}
}
// copy file
void copyfile(const QString &fromFile, const QString &toFile)
{
QUrl fromurl = localFileUrl(fromFile);
QUrl toUrl = localFileUrl(toFile);
DFileOperator *fileOperator = new DFileOperator(fromurl);
if (!fileOperator)
return;
auto expected = fileOperator->copyFile(toUrl, CopyFlag::None);
hanldeResult(&expected);
delete fileOperator;
}
// delete file
void deletefile(const QString &file)
{
QUrl url = localFileUrl(file);
if (!exist(url))
return;
DFileOperator *fileOperator = new DFileOperator(url);
if (!fileOperator)
return;
auto expected = fileOperator->deleteFile();
hanldeResult(&expected);
delete fileOperator;
}
// rename file
void renamefile(const QString &file, const QString &newName)
{
QUrl url = localFileUrl(file);
if (!exist(url))
return;
DFileOperator *fileOperator = new DFileOperator(url);
if (!fileOperator)
return;
auto expected = fileOperator->renameFile(newName);
hanldeResult(&expected);
delete fileOperator;
}
// cat file
void catfile(const QString &fileName)
{
QUrl url = localFileUrl(fileName);
if (!exist(url))
return;
DFile *file = new DFile(url);
if (!file)
return;
file->open(OpenFlag::ReadOnly);
auto expected = file->readAll();
file->close();
if (expected) {
qInfo() << expected.value();
} else {
qWarning() << expected.error();
}
delete file;
}
// move file
void movefile(const QString &file, const QString &toUrl)
{
QUrl url = localFileUrl(file);
QUrl destUrl = QUrl::fromLocalFile(toUrl);
if (!exist(url))
return;
DFileOperator *fileOperator = new DFileOperator(url);
if (!fileOperator)
return;
auto expected = fileOperator->moveFile(destUrl, CopyFlag::None);
hanldeResult(&expected);
delete fileOperator;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
// 创建命令行解析对象
QCommandLineParser parser;
// 定义帮助信息
parser.setApplicationDescription(QString("%1 helper").arg(QCoreApplication::applicationName()));
// 定义参数, addOption() 方法
parser.addOption(QCommandLineOption("cat", "Cat files."));
parser.addOption(QCommandLineOption("copy", "Copy files."));
parser.addOption(QCommandLineOption("remove", "Delete files."));
parser.addOption(QCommandLineOption("rename", "Rename files."));
parser.addOption(QCommandLineOption("move", "Move files."));
parser.addHelpOption();
// 解析参数,parse() 方法
parser.process(app);
if (argc == 1)
parser.showHelp();
//执行IO相关操作
{
if (parser.isSet("remove")) {
deletefile(argv[2]);
exit(0);
}
if (parser.isSet("copy")) {
copyfile(argv[2], argv[3]);
exit(0);
}
if (parser.isSet("move")) {
movefile(argv[2], argv[3]);
exit(0);
}
if (parser.isSet("cat")) {
catfile(argv[2]);
exit(0);
}
if (parser.isSet("rename")) {
renamefile(argv[2], argv[3]);
exit(0);
}
parser.showHelp();
}
return app.exec();
}