|
@@ -16,6 +16,7 @@ using namespace std;
|
|
|
namespace CmdManager {
|
|
|
QMLHandler *qmlHandler;
|
|
|
map<string, void (*)(Json::Value)> cmdmap;
|
|
|
+map<string, struct fileEntry> filemap;
|
|
|
} // namespace CmdManager
|
|
|
|
|
|
void CmdManager::init() {
|
|
@@ -37,10 +38,69 @@ void CmdManager::init() {
|
|
|
cmdmap["notifications"] = &CmdManager::handleNotifications;
|
|
|
cmdmap["queue"] = &CmdManager::handleQueue;
|
|
|
cmdmap["dequeue"] = &CmdManager::handleDequeue;
|
|
|
+ cmdmap["extendedstatus"] = &CmdManager::handleExtendedStatus;
|
|
|
+
|
|
|
+ filemap.clear();
|
|
|
}
|
|
|
|
|
|
void CmdManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
|
|
|
|
|
|
+void CmdManager::updateInternalFile(string name, string type, string method, int progress, float speed) {
|
|
|
+ map<string, struct fileEntry>::iterator it = filemap.find(name);
|
|
|
+ if (it != filemap.end()) {
|
|
|
+ it->second.type = type;
|
|
|
+ it->second.method = method;
|
|
|
+ it->second.progress = progress;
|
|
|
+ it->second.speed = speed;
|
|
|
+ it->second.dirty = true;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CmdManager::emitFileList() {
|
|
|
+ map<string, struct fileEntry>::iterator it;
|
|
|
+ char speedbuf[256];
|
|
|
+ string progstr;
|
|
|
+
|
|
|
+ for (it = filemap.begin(); it != filemap.end(); it++) {
|
|
|
+ // This is a transfer
|
|
|
+ if (it->second.type.size()) {
|
|
|
+ // using a covert channel
|
|
|
+ if (it->second.method.size()) {
|
|
|
+ snprintf(speedbuf, 256, "%.2f B/s", it->second.speed);
|
|
|
+ progstr = it->second.type + " via " + it->second.method + "\n" + std::to_string(it->second.progress) + "%" + " @ " + speedbuf;
|
|
|
+ }
|
|
|
+ else progstr = it->second.type + "\n" + std::to_string(it->second.progress) + "%";
|
|
|
+ emit qmlHandler->receivingUpdateFile(it->first.c_str(), progstr.c_str(), it->second.type == "Queued" || it->second.type == "Sending");
|
|
|
+ }
|
|
|
+ // else emit plain entry
|
|
|
+ else
|
|
|
+ emit qmlHandler->receivingUpdateFile(it->first.c_str(), "", false);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void CmdManager::cleanInternalList() {
|
|
|
+ map<string, struct fileEntry>::iterator it;
|
|
|
+
|
|
|
+ for (it = filemap.begin(); it != filemap.end(); it++) {
|
|
|
+ if (!it->second.dirty) {
|
|
|
+ it->second.type.clear();
|
|
|
+ it->second.method.clear();
|
|
|
+ it->second.progress = 0;
|
|
|
+ it->second.speed = 0;
|
|
|
+ } else
|
|
|
+ it->second.dirty = false;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void printInternalList() {
|
|
|
+ map<string, struct fileEntry>::iterator it;
|
|
|
+
|
|
|
+ for (it = CmdManager::filemap.begin(); it != CmdManager::filemap.end(); it++)
|
|
|
+ emit CmdManager::qmlHandler->log((string("IFL ENTRY ") + it->first + " " + it->second.type + " " + it->second.method + " " +
|
|
|
+ std::to_string(it->second.progress) + " " + std::to_string(it->second.speed) + " " + std::to_string(it->second.dirty))
|
|
|
+ .c_str());
|
|
|
+}
|
|
|
+
|
|
|
void CmdManager::executeCmd(string cmd, Json::Value root) {
|
|
|
map<string, void (*)(Json::Value)>::iterator it = cmdmap.find(cmd);
|
|
|
if (it == cmdmap.end()) {
|
|
@@ -60,12 +120,14 @@ void CmdManager::handleList(Json::Value root) {
|
|
|
snprintf(sizebuf, 256, "%.2f", 4.2f);
|
|
|
if (root["accept"] == true) {
|
|
|
emit qmlHandler->receivingClearFileList();
|
|
|
+ filemap.clear();
|
|
|
|
|
|
// Get the array of file Names
|
|
|
auto fileNames = root["names"];
|
|
|
for (int i = 0; i < fileNames.size(); i++) {
|
|
|
emit qmlHandler->receivingListFile(QString::fromStdString(fileNames[i].asString()), QString::fromStdString(sizebuf), QString("Decryptable"),
|
|
|
boost::filesystem::exists(fileNames[i].asString()));
|
|
|
+ filemap[fileNames[i].asString()] = {false, "", "", 0, 0};
|
|
|
}
|
|
|
} else {
|
|
|
emit qmlHandler->log(root["error"].asString().c_str());
|
|
@@ -76,6 +138,7 @@ void CmdManager::handleExtendedList(Json::Value root) {
|
|
|
char sizebuf[256];
|
|
|
if (root["accept"] == true) {
|
|
|
emit qmlHandler->receivingClearFileList();
|
|
|
+ filemap.clear();
|
|
|
|
|
|
// Get the array of file Names
|
|
|
auto files = root["files"];
|
|
@@ -83,6 +146,7 @@ void CmdManager::handleExtendedList(Json::Value root) {
|
|
|
snprintf(sizebuf, 256, "%.2f", f["size"].asFloat());
|
|
|
emit qmlHandler->receivingListFile(QString::fromStdString(f["name"].asString()), QString::fromStdString(sizebuf),
|
|
|
QString::fromStdString(f["encrypted"].asString()), boost::filesystem::exists(f["name"].asString()));
|
|
|
+ filemap[f["name"].asString()] = {false, "", "", 0, 0};
|
|
|
}
|
|
|
} else {
|
|
|
emit qmlHandler->log(root["error"].asString().c_str());
|
|
@@ -208,3 +272,33 @@ void CmdManager::handleDequeue(Json::Value root) {
|
|
|
emit qmlHandler->log(errorMessage);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+void CmdManager::handleExtendedStatus(Json::Value root) {
|
|
|
+ Json::Value cs, ss;
|
|
|
+ string typestring;
|
|
|
+ if (!root["accept"].asBool()) {
|
|
|
+ QString errorMessage = QString::fromStdString(string("Error when loading status " + root["error"].asString()));
|
|
|
+ emit qmlHandler->log(errorMessage);
|
|
|
+ } else {
|
|
|
+ cs = root["transfersclientserver"];
|
|
|
+ ss = root["transfersserverserver"];
|
|
|
+ if (!cs.isNull()) {
|
|
|
+ for (Json::Value t : cs) {
|
|
|
+ updateInternalFile(t["file"].asString(), t["upload"].asBool() ? "Upload" : "Download", "", t["progress"].asInt(), 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!ss.isNull()) {
|
|
|
+ for (Json::Value t : ss) {
|
|
|
+ if (t["type"] == "download")
|
|
|
+ typestring = "Receiving";
|
|
|
+ else if (t["type"] == "upload")
|
|
|
+ typestring = "Sending";
|
|
|
+ else if (t["type"] == "queued")
|
|
|
+ typestring = "Queued";
|
|
|
+ updateInternalFile(t["file"].asString(), typestring, t["method"].asString(), t["progress"].asInt(), t["speed"].asFloat());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cleanInternalList();
|
|
|
+ emitFileList();
|
|
|
+ }
|
|
|
+}
|