-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
187 lines (137 loc) · 6.64 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "ClipboardManager.h"
#include "NetworkConnection.h"
#include <iostream>
#include <thread>
#include <shellapi.h>
#include <regex>
bool containsSpecialCharacters(const std::string &str);
void removeNullCharacter(std::string &str);
void updateClipboard(HWND hWindow, const std::string &serverClipboard);
void backgroundClipboardUpdater(const std::string &username, const std::string &deviceID);
std::string getClipboardFromSever(NetworkConnection *conn, const std::string &username);
int main() {
std::string serverIPAddress;
std::string apiServerIPAddress;
bool flag = true;
auto ipPattern = std::regex("^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$");
auto hostNamePattern = std::regex("^[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*\\.local$");
while (flag) {
std::cout << "Please input the server IP (ipv4) address:" << std::endl;
std::cin >> serverIPAddress;
if (std::regex_match(serverIPAddress, ipPattern) || std::regex_match(serverIPAddress,hostNamePattern)) {
flag = false;
} else {
std::cout << "Sorry that isn't a valid IP (ipv4) address." << std::endl;
}
}
apiServerIPAddress = "http://" + serverIPAddress + ":4800";
serverIPAddress = "http://" + serverIPAddress + ":8080";
NetworkConnection::setIP(apiServerIPAddress);
std::string username = "Benz"; // admin
std::string deviceID = "1";
ShellExecute(nullptr, "open", serverIPAddress.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
std::thread thread_obj(backgroundClipboardUpdater, username, deviceID);
// TODO: Add Qt code start here
// backgroundClipboardUpdater(username, deviceID);
// Wait for the thread to finish
thread_obj.join();
return 0;
}
void backgroundClipboardUpdater(const std::string &username, const std::string &deviceID) {
HWND hWindow = ClipboardManager::GetClipboardWindowHandle();
auto *conn = new NetworkConnection();
nlohmann::json jsonData = {
{"username", username},
{"deviceID", deviceID},
};
while (true) {
std::string serverClipboardText = getClipboardFromSever(conn, username);
bool clipboardChanged = false;
// Send the local clipboard to the server if it changed
if (ClipboardManager::HasClipboardChanged()) { // TODO: Also compare with the server contents
std::string updateTime = ClipboardManager::GetLastUpdateTime();
std::cout << std::endl << "Clipboard content was last updated at: " << updateTime << std::endl;
ClipboardContent content = ClipboardManager::GetClipboardContent(hWindow);
content.setUpdateTime(updateTime);
jsonData["lastUpdatedTime"] = ClipboardManager::GetLastUpdateTime();
std::string localClipboardConverted;
switch (content.getDataType()) {
case ClipboardDataType::Text:
std::wcout << L"Clipboard data type: text" << std::endl;
std::wcout << L"Content: " << content.getTextData() << std::endl;
localClipboardConverted = ClipboardManager::convertToUtf8(content.getTextData());
removeNullCharacter(localClipboardConverted);
if (!localClipboardConverted.empty() && localClipboardConverted != serverClipboardText) {
jsonData["content"] = localClipboardConverted;
conn->postData("/api/clipboard/send", jsonData);
clipboardChanged = true;
} else {
std::wcout << L"The contents are the same as the server " << std::endl;
}
break;
case ClipboardDataType::Image:
std::wcout << L"Clipboard data type: image" << std::endl;
break;
case ClipboardDataType::File:
std::cout << "Clipboard data type: file." << std::endl;
break;
default:
std::cout << "Unknown format/empty clipboard." << std::endl;
break;
}
}
if (!clipboardChanged) {
updateClipboard(hWindow, serverClipboardText);
}
std::this_thread::sleep_for(std::chrono::seconds(1)); // Delay to prevent excessive CPU usage
}
}
std::string getClipboardFromSever(NetworkConnection *conn, const std::string &username) {
nlohmann::json serverResponse = conn->getData(
"/api/clipboard/receive?username=" + username); // TODO: Need to escape?
// Return if the server has no connection
if (serverResponse["status"] != 200) {
if (serverResponse["status"] == 504) {
std::cerr << std::endl << "GET: Unable to reach server " << std::endl;
} else {
std::cout << "Status: " << serverResponse["status"] << std::endl;
std::cout << "Body: " << serverResponse["body"] << std::endl;
}
return "";
}
if (serverResponse.at("body").contains("content")) {
std::string serverClipboard = serverResponse.at("body").at("content");
return serverClipboard;
} else {
std::cerr << "Unable to update local clipboard as the response is missing \"content\"" << std::endl;
return "";
}
}
// Updates the local clipboard
void updateClipboard(HWND hWindow, const std::string &serverClipboard) {
ClipboardContent localClipboard = ClipboardManager::GetClipboardContent(hWindow);
if (localClipboard.getDataType() == ClipboardDataType::Text) {
std::string localClipboardConverted = ClipboardManager::convertToUtf8(localClipboard.getTextData());
removeNullCharacter(localClipboardConverted);
if (!serverClipboard.empty() && localClipboardConverted != serverClipboard) {
std::cout << "Updating local clipboard: " << serverClipboard << std::endl;
ClipboardManager::PushStringToClipboard(serverClipboard);
}
}
}
bool containsSpecialCharacters(const std::string &str) {
for (char ch: str) {
// Check if the character is not printable or is a control character
if (!std::isprint(static_cast<unsigned char>(ch)) || std::iscntrl(static_cast<unsigned char>(ch))) {
return true; // String contains a special character
}
}
return false; // String does not contain special characters
}
void removeNullCharacter(std::string &str) {
size_t pos = str.find('\u0000');
while (pos != std::string::npos) {
str.erase(pos, 1); // Erase 1 character at the found position
pos = str.find('\u0000', pos); // Find the next occurrence
}
}