Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: The Clipboard operations in the Wayland #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ install(TARGETS ${BIN_NAME} DESTINATION bin)
set(BIN_NAME dde-clipboard-daemon)

file(GLOB_RECURSE dde-clipboard-daemon_SCRS
"dde-clipboard-daemon/serviceflow/*.h"
"dde-clipboard-daemon/serviceflow/*.cpp"
"dde-clipboard-daemon/*.h"
"dde-clipboard-daemon/*.cpp"
)
Expand Down
1 change: 0 additions & 1 deletion dde-clipboard-daemon/clipboardloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ ClipboardLoader::ClipboardLoader(QObject *parent)
if (qEnvironmentVariable("XDG_SESSION_TYPE").contains("wayland")) {
#ifdef USE_DEEPIN_KF5_WAYLAND
m_waylandCopyClient = new WaylandCopyClient(this);
m_waylandCopyClient->init();

connect(m_waylandCopyClient, &WaylandCopyClient::dataChanged, this, [this] {
this->doWork(WAYLAND_PROTOCOL);
Expand Down
76 changes: 76 additions & 0 deletions dde-clipboard-daemon/serviceflow/command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "command.h"
#include "servicetool.h"

Command::Command(CommandService *service)
: m_service(service)
{
setAutoDelete(true);
}

void Command::exit()
{
m_isExit = true;
}

Command::State Command::state() const
{
return m_state;
}

CommandService *Command::service() const
{
return m_service;
}

void Command::installMessageExtractor(Extractor *extractor)
{
Q_ASSERT(extractor);
switch (extractor->direction()) {
case Extractor::CommandToTransporter: {
if (m_outEx) {
qWarning("This command already has the out direction Extractor.");
return;
}
m_outEx = extractor;
}
break;
case Extractor::TransporterToCommand: {
if (m_inEx) {
qWarning("This command already has the in direction Extractor.");
return;
}
m_inEx = extractor;
}
break;
}
}

void Command::run()
{
Q_ASSERT(m_inEx);
while (true) {
m_state = Preparing;
CommandMessage *msg = m_inEx->takeFromTransporter();
if (m_isExit) {
delete msg;
break;
}

m_state = Running;
QList<CommandMessage *> destMsgs = doExecute(msg);

// 允许复用同一个 msg 的情况
if (!destMsgs.contains(msg))
delete msg;

if (m_outEx) {
for (auto destMsg : destMsgs)
m_outEx->pushToTransporter(destMsg);
}
}
m_state = Preparing;
}
41 changes: 41 additions & 0 deletions dde-clipboard-daemon/serviceflow/command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef COMMAND
#define COMMAND

#include <QRunnable>

class CommandService;
class CommandMessage;
class Extractor;
class Command : public QRunnable
{
public:
enum State {
Preparing,
Running
};
Command(CommandService *service = nullptr);

virtual QList<CommandMessage *> doExecute(CommandMessage *) = 0;
virtual QString name() const = 0;

void exit();
State state() const;

CommandService *service() const;
void installMessageExtractor(Extractor *extractor);

private:
void run() override;

Extractor * m_inEx = nullptr;
Extractor * m_outEx = nullptr;
bool m_isExit = false;
State m_state = Preparing;
CommandService *m_service = nullptr;
};

#endif // COMMANDMESSAGE
31 changes: 31 additions & 0 deletions dde-clipboard-daemon/serviceflow/commandmessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef COMMANDMESSAGE
#define COMMANDMESSAGE

class CommandMessage
{
public:
enum Error {
NoError,
ExecuteError
};

CommandMessage() = default;
virtual ~CommandMessage() {}

inline Error error() const {
return m_error;
}

inline void setError(Error err) {
m_error = err;
}

protected:
Error m_error = NoError;
};

#endif // COMMANDMESSAGE
25 changes: 25 additions & 0 deletions dde-clipboard-daemon/serviceflow/commandservice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "commandservice.h"

CommandService::CommandService()
{

}

CommandService::~CommandService()
{

}

CommandService *CommandService::nextService() const
{
return this->m_nextService;
}

void CommandService::setNextService(CommandService *service)
{
this->m_nextService = service;
}
25 changes: 25 additions & 0 deletions dde-clipboard-daemon/serviceflow/commandservice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2011 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef COMMANDSERVICE
#define COMMANDSERVICE

#include "command.h"

class CommandService
{
public:
CommandService();
virtual ~CommandService();

virtual QList<Command *> commands() = 0;

CommandService *nextService() const;
void setNextService(CommandService* service);

private:
CommandService* m_nextService;
};

#endif // COMMANDSERVICE
Loading