Skip to content

Commit

Permalink
refactor: change preview plugin to an app
Browse files Browse the repository at this point in the history
1.mv preview plugin to apps dir
2.add dde-file-manager-preview exec project
3.change package info

Log: add file preview exec
  • Loading branch information
Lighto-Ku committed Sep 13, 2024
1 parent cb4e6d5 commit 2c26a81
Show file tree
Hide file tree
Showing 143 changed files with 10,159 additions and 10 deletions.
8 changes: 4 additions & 4 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Depends:
dde-file-manager-daemon-plugins,
dde-file-manager-server-plugins,
dde-file-manager-common-plugins,
dde-file-manager-preview-plugins,
dde-file-manager-preview,
libblockdev-crypto2 | libblockdev-crypto3,
qml6-module-qtquick-controls,
qml6-module-qtquick-layouts,
Expand Down Expand Up @@ -148,11 +148,11 @@ Depends: ${shlibs:Depends}, ${misc:Depends}
Description: deepin desktop-environment - services edge plugins module
Deepin Desktop Environment (DDE) - services edge plugins module.

Package: dde-file-manager-preview-plugins
Package: dde-file-manager-preview
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: deepin desktop-environment - filemanager preview plugins module
Deepin Desktop Environment (DDE) - filemanager preview edge plugins module.
Description: deepin desktop-environment - filemanager preview module
Deepin Desktop Environment (DDE) - filemanager preview edge module.

Package: dde-disk-mount-plugin
Architecture: any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
usr/lib/*/dde-file-manager/plugins/previews/*.so
usr/share/mime/packages/dtk-dci.xml

usr/libexec/dde-file-manager-preview
1 change: 1 addition & 0 deletions src/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if(OPT_ENABLE_QML)
else()
add_subdirectory(dde-file-manager)
add_subdirectory(dde-file-manager-daemon)
add_subdirectory(dde-file-manager-preview)
add_subdirectory(dde-file-dialog)
add_subdirectory(dde-file-dialog-x11)
add_subdirectory(dde-file-dialog-wayland)
Expand Down
9 changes: 9 additions & 0 deletions src/apps/dde-file-manager-preview/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.10)

if(NOT DEFINED DFM_PLUGIN_PREVIEW_DIR)
set(DFM_PLUGIN_PREVIEW_DIR ${DFM_PLUGIN_DIR}/previews)
endif()

add_subdirectory(filepreview)
add_subdirectory(libdfm-preview)
add_subdirectory(pluginpreviews)
29 changes: 29 additions & 0 deletions src/apps/dde-file-manager-preview/filepreview/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.10)

set(BIN_NAME "dde-file-manager-preview")

configure_file(
"../../config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/config.h"
)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Dtk COMPONENTS Widget REQUIRED)

FILE(GLOB_RECURSE FILEPREVIEW_FILES CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
)

add_executable(${BIN_NAME}
${FILEPREVIEW_FILES}
)

target_link_libraries(${BIN_NAME} PRIVATE
Qt5::Core
Dtk::Widget
DFM::base
)

install(TARGETS ${BIN_NAME} DESTINATION ${CMAKE_INSTALL_LIBEXECDIR})
32 changes: 32 additions & 0 deletions src/apps/dde-file-manager-preview/filepreview/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "previewsingleapplication.h"

#ifdef DFM_ORGANIZATION_NAME
# define ORGANIZATION_NAME DFM_ORGANIZATION_NAME
#else
# define ORGANIZATION_NAME "deepin"
#endif

int main(int argc, char *argv[]) {
// singlentan process
PreviewSingleApplication app(argc, argv);

app.setQuitOnLastWindowClosed(false);
app.setOrganizationName(ORGANIZATION_NAME);

Check warning on line 18 in src/apps/dde-file-manager-preview/filepreview/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Skipping configuration 'DFM_ORGANIZATION_NAME' since the value of 'DFM_ORGANIZATION_NAME' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.

Check warning on line 18 in src/apps/dde-file-manager-preview/filepreview/main.cpp

View workflow job for this annotation

GitHub Actions / static-check / Call-CppCheck

Skipping configuration 'DFM_ORGANIZATION_NAME' since the value of 'DFM_ORGANIZATION_NAME' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
app.setApplicationName("Deepin File Preview");
app.setApplicationVersion("v1.0");

QString uniqueKey(app.applicationName());
bool isSinglentanApp = app.setSingleInstance(uniqueKey);

if (isSinglentanApp) {
PreviewSingleApplication::processArgs(app.arguments());
return app.exec();
} else {
app.handleNewClient(uniqueKey);
return 0;
}
}
79 changes: 79 additions & 0 deletions src/apps/dde-file-manager-preview/filepreview/previewlibrary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "previewlibrary.h"
#include "config.h"

#include <QLibrary>
#include <QDir>

DFM_LOG_REISGER_CATEGORY(DFMPREVIEW_LOG_CG)

PreviewLibrary::PreviewLibrary(QObject *parent)
: QObject(parent)
{}

bool PreviewLibrary::load()
{
if (previewLib)
return true;

QString libPath;
{
//默认路径
#ifdef QT_DEBUG
char path[PATH_MAX] = {0};
const char *defaultPath = realpath("../libdfm-preview", path);
#else
auto defaultPath = DFM_PLUGIN_COMMON_EDGE_DIR;
#endif
static_assert(std::is_same<decltype(defaultPath), const char *>::value, "DFM_PLUGIN_COMMON_EDGE_DIR is not a string.");

QDir dir(defaultPath);
libPath = dir.absoluteFilePath("libdfm-preview.so");
}

previewLib = new QLibrary(libPath, this);
if (!previewLib->load()) {
fmCritical() << "Can not load libdfm-preview.so" << previewLib->errorString();
delete previewLib;
previewLib = nullptr;
return false;
}

initFunc = (InitializePreview)previewLib->resolve("initFilePreview");
if (!initFunc) {
fmCritical() << "No such api initFilePreview in libdfm-preview.so";
delete previewLib;
previewLib = nullptr;
return false;
}

showFunc = (ShowPreviewDialog)previewLib->resolve("showFilePreviewDialog");
if (!showFunc) {
fmCritical() << "No such api showFilePreviewDialog in libdfm-preview.so";
delete previewLib;
previewLib = nullptr;
return false;
}

initFunc();
return true;
}

void PreviewLibrary::unload()
{
if (previewLib) {
previewLib->unload();
delete previewLib;
previewLib = nullptr;
showFunc = nullptr;
}
}

void PreviewLibrary::showPreview(quint64 windowId, const QList<QUrl> &selecturls, const QList<QUrl> dirUrl)
{
if (showFunc)
showFunc(windowId, selecturls, dirUrl);
}
36 changes: 36 additions & 0 deletions src/apps/dde-file-manager-preview/filepreview/previewlibrary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef PREVIEWLIBRARY_H
#define PREVIEWLIBRARY_H

#include <dfm-base/dfm_log_defines.h>

#include <QObject>
#include <QUrl>

#define DFMPREVIEW_LOG_CG dde_file_manager_preview
DFM_LOG_USE_CATEGORY(DFMPREVIEW_LOG_CG)

Check warning on line 14 in src/apps/dde-file-manager-preview/filepreview/previewlibrary.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If DFM_LOG_USE_CATEGORY is a macro then please configure it.

Check warning on line 14 in src/apps/dde-file-manager-preview/filepreview/previewlibrary.h

View workflow job for this annotation

GitHub Actions / static-check / Call-CppCheck

There is an unknown macro here somewhere. Configuration is required. If DFM_LOG_USE_CATEGORY is a macro then please configure it.

typedef int (*ShowPreviewDialog)(quint64 windowId, const QList<QUrl> &selecturls, const QList<QUrl> dirUrl);
typedef int (*InitializePreview)();
class QLibrary;

class PreviewLibrary: public QObject
{
Q_OBJECT
public:
explicit PreviewLibrary(QObject *parent = nullptr);

bool load();
void unload();
void showPreview(quint64 windowId, const QList<QUrl> &selecturls, const QList<QUrl> dirUrl);

protected:
ShowPreviewDialog showFunc = nullptr;
InitializePreview initFunc = nullptr;
QLibrary *previewLib = nullptr;
};

#endif // PREVIEWLIBRARY_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "previewsingleapplication.h"
#include "previewlibrary.h"

#include <dfm-base/utils/windowutils.h>
#include <dfm-base/utils/finallyutil.h>

#include <QLocalServer>
#include <QLocalSocket>
#include <QStandardPaths>
#include <QTranslator>
#include <QFile>
#include <QDir>
#include <QProcess>

#include <linux/limits.h>

DFMBASE_USE_NAMESPACE
// DFM_LOG_REISGER_CATEGORY(DFMPREVIEW_LOG_CG)

PreviewSingleApplication::PreviewSingleApplication(int &argc, char **argv, int)
: DApplication(argc, argv), localServer(new QLocalServer)
{
initConnect();
}

PreviewSingleApplication::~PreviewSingleApplication()
{
closeServer();
}

void PreviewSingleApplication::handleNewClient(const QString &uniqueKey)
{
QByteArray data { nullptr };

for (const QString &arg : arguments()) {
data.append(arg.toLocal8Bit().toBase64());
data.append(' ');
}

if (!data.isEmpty())
data.chop(1);

QLocalSocket *socket = getNewClientConnect(uniqueKey, data);
if (socket) {
socket->waitForReadyRead();

for (const QByteArray &i : socket->readAll().split(' '))
fmInfo() << QString::fromLocal8Bit(QByteArray::fromBase64(i));

socket->close();
socket->deleteLater();
}
}

void PreviewSingleApplication::processArgs(const QStringList &list)
{
// parse args
if (list.count() < 4)
return;

quint64 winId = list[1].toULongLong();

QStringList selectUrlsStr = list[2].split(";");
QStringList dirUrlsStr = list[3].split(";");

QList<QUrl> selectUrls(QUrl::fromStringList(selectUrlsStr));
QList<QUrl> dirUrls(QUrl::fromStringList(dirUrlsStr));

PreviewLibrary lib;
if (!lib.load())
return;

lib.showPreview(winId, selectUrls, dirUrls);
}

void PreviewSingleApplication::initConnect()
{
connect(localServer, &QLocalServer::newConnection, this, &PreviewSingleApplication::handleConnection);
}

QLocalSocket *PreviewSingleApplication::getNewClientConnect(const QString &key, const QByteArray &message)
{
QLocalSocket *localSocket = new QLocalSocket;
localSocket->connectToServer(userServerName(key));
if (localSocket->waitForConnected(1000)) {
if (localSocket->state() == QLocalSocket::ConnectedState) {
if (localSocket->isValid()) {
localSocket->write(message);
localSocket->flush();
}
}
} else {
fmDebug() << localSocket->errorString();
}

return localSocket;
}

QString PreviewSingleApplication::userServerName(const QString &key)
{
QString userKey = QString("%1/%2").arg(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation), key);
if (userKey.isEmpty()) {
userKey = QString("%1/%2").arg(QStandardPaths::writableLocation(QStandardPaths::TempLocation), key);
}
return userKey;
}

bool PreviewSingleApplication::setSingleInstance(const QString &key)
{
QString userKey = userServerName(key);

QLocalSocket localSocket;
localSocket.connectToServer(userKey);

// if connect success, another instance is running.
bool result = localSocket.waitForConnected(1000);

if (result)
return false;

localServer->removeServer(userKey);

bool f = localServer->listen(userKey);

return f;
}

void PreviewSingleApplication::handleConnection()
{
fmDebug() << "New connection is coming";
QLocalSocket *nextPendingConnection = localServer->nextPendingConnection();
connect(nextPendingConnection, SIGNAL(readyRead()), this, SLOT(readData()));
}

void PreviewSingleApplication::closeServer()
{
if (localServer) {
localServer->removeServer(localServer->serverName());
localServer->close();
delete localServer;
localServer = nullptr;
}
}

void PreviewSingleApplication::readData()
{
QLocalSocket *socket = qobject_cast<QLocalSocket *>(sender());

if (!socket)
return;

QStringList arguments;
for (const QByteArray &arg_base64 : socket->readAll().split(' ')) {
const QByteArray &arg = QByteArray::fromBase64(arg_base64.simplified());

if (arg.isEmpty())
continue;

QString argstr = QString::fromLocal8Bit(arg);
arguments << argstr;
}

processArgs(arguments);
}
Loading

0 comments on commit 2c26a81

Please sign in to comment.