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
|
/*
* jd_commands.h - plugin
* Copyright (C) 2011 Evgeny Khryukin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <QDomElement>
#include <QEventLoop>
#include <QStringList>
#include <QTimer>
#include "jabberdiskcontroller.h"
#include "jd_commands.h"
#define TIMER_INTERVAL 5000
JDCommands::JDCommands(int account, const QString &jid, QObject *p) :
QObject(p), account_(account), jid_(jid), jdc(JabberDiskController::instance()), timer_(new QTimer(this)),
eventLoop_(new QEventLoop(this)), lastCommand_(CommandNoCommand)
{
timer_->setInterval(TIMER_INTERVAL);
connect(jdc, &JabberDiskController::stanza, this, &JDCommands::incomingStanza);
connect(timer_, &QTimer::timeout, this, &JDCommands::timeOut);
}
JDCommands::~JDCommands() { timeOut(); }
void JDCommands::incomingStanza(int account, const QDomElement &xml)
{
if (account != account_ || xml.attribute("from").split("/").at(0).toLower() != jid_)
return;
emit incomingMessage(xml.firstChildElement("body").text(), lastCommand_);
lastCommand_ = CommandNoCommand;
timeOut();
}
void JDCommands::get(const QString &file) { sendStanza("get " + file, CommandGet); }
void JDCommands::cd(const QString &dir) { sendStanza("cd " + dir, CommandCd); }
void JDCommands::help() { sendStanza("help", CommandHelp); }
void JDCommands::intro() { sendStanza("intro", CommandIntro); }
void JDCommands::hash(const QString &file) { sendStanza("hash " + file, CommandHash); }
void JDCommands::rm(const QString &path) { sendStanza("rm " + path, CommandRm); }
void JDCommands::du() { sendStanza("du", CommandDu); }
void JDCommands::mkDir(const QString &dir) { sendStanza("mkdir " + dir, CommandMkDir); }
void JDCommands::lang(const QString &l) { sendStanza("lang " + l, CommandLang); }
void JDCommands::pwd() { sendStanza("pwd", CommandPwd); }
void JDCommands::ls(const QString &dir)
{
QString txt = "ls";
if (!dir.isEmpty())
txt += " " + dir;
sendStanza(txt, CommandLs);
}
void JDCommands::send(const QString &toJid, const QString &file)
{
sendStanza("send " + toJid + " " + file, CommandSend);
}
void JDCommands::mv(const QString &oldFile, const QString &newFile)
{
sendStanza("mv " + oldFile + " " + newFile, CommandMv);
}
void JDCommands::link(const QString &file) { sendStanza("link " + file, CommandLink); }
void JDCommands::sendStanzaDirect(const QString &text)
{
emit outgoingMessage(text);
QString id;
jdc->sendStanza(account_, jid_, text, &id);
}
void JDCommands::sendStanza(const QString &text, Command c)
{
emit outgoingMessage(text);
lastCommand_ = c;
QString id;
jdc->sendStanza(account_, jid_, text, &id);
timer_->start();
eventLoop_->exec();
}
void JDCommands::timeOut()
{
if (timer_->isActive())
timer_->stop();
if (eventLoop_->isRunning())
eventLoop_->quit();
}
bool JDCommands::isReady() const { return !eventLoop_->isRunning(); }
|