1 /***************************************************************************
2 *                                                                         *
3 *   This program is free software; you can redistribute it and/or modify  *
4 *   it under the terms of the GNU General Public License as published by  *
5 *   the Free Software Foundation; either version 3 of the License, or     *
6 *   (at your option) any later version.                                   *
7 *                                                                         *
8 ***************************************************************************/
9 
10 #include "ClientManagerScript.h"
11 #include "WulforUtil.h"
12 
13 #include "dcpp/CID.h"
14 #include "dcpp/User.h"
15 
toQStringList(const dcpp::StringList & list)16 static QStringList toQStringList(const dcpp::StringList &list){
17     QStringList ret;
18     for (auto it = list.begin(); it != list.end(); ++it){
19         ret.push_back(_q(*it));
20     }
21 
22     return ret;
23 }
24 
ClientManagerScript(QObject * parent)25 ClientManagerScript::ClientManagerScript(QObject *parent) :
26     QObject(parent)
27 {
28     CM = dcpp::ClientManager::getInstance();
29 
30     CM->addListener(this);
31 }
32 
~ClientManagerScript()33 ClientManagerScript::~ClientManagerScript(){
34     CM->removeListener(this);
35 }
36 
37 
sendPM(const QString & cid,const QString & hubUrl,const QString & msg)38 void ClientManagerScript::sendPM(const QString& cid, const QString& hubUrl, const QString& msg){
39     dcpp::UserPtr user = CM->findUser(CID(_tq(cid)));
40 
41     if (user && user->isOnline()){
42         if (msg.isEmpty() || msg == "\n")
43             return;
44 
45         CM->privateMessage(HintedUser(user, _tq(hubUrl)), _tq(msg), false);
46     }
47 }
48 
49 
getUserCount() const50 quint64 ClientManagerScript::getUserCount() const{
51     return CM->getUserCount();
52 }
53 
getAvailable() const54 quint64 ClientManagerScript::getAvailable() const{
55     return CM->getAvailable();
56 }
57 
getHubs(const QString & cid) const58 QStringList ClientManagerScript::getHubs(const QString& cid) const{
59     return toQStringList(CM->getHubs(dcpp::CID(_tq(cid)), ""));
60 }
61 
getHubNames(const QString & cid) const62 QStringList ClientManagerScript::getHubNames(const QString& cid) const{
63     return getHubNames(cid, "");
64 }
65 
getHubNames(const QString & cid,const QString & hubUrl) const66 QStringList ClientManagerScript::getHubNames(const QString& cid, const QString& hubUrl) const{
67     StringList hubs = ClientManager::getInstance()->getHubNames(dcpp::CID(_tq(cid)), _tq(hubUrl));
68 
69     if (hubs.empty())
70         return QStringList();
71     else
72         return toQStringList(hubs);
73 }
74 
75 
getNicks(const QString & cid) const76 QStringList ClientManagerScript::getNicks(const QString& cid) const{
77     const dcpp::Identity &user = CM->getOnlineUserIdentity(CM->getUser(dcpp::CID(_tq(cid))));
78     QStringList lst;
79     lst.push_back(_q(user.getNick()));
80 
81     return lst;
82 }
83 
getConnectedHubs() const84 QStringList ClientManagerScript::getConnectedHubs() const {
85     QStringList ret;
86     CM->lock();
87     dcpp::Client::List& clients = CM->getClients();
88 
89     for(auto it = clients.begin(); it != clients.end(); ++it) {
90         Client* client = *it;
91 
92         if(!client->isConnected())
93             continue;
94 
95         ret.push_back(_q(client->getHubUrl()));
96     }
97 
98     return ret;
99 }
100 
getConnection(const QString & cid) const101 QString ClientManagerScript::getConnection(const QString& cid) const{
102     return _q(CM->getConnection(dcpp::CID(_tq(cid))));
103 }
104 
isConnected(const QString & aUrl) const105 bool ClientManagerScript::isConnected(const QString& aUrl) const{
106     return CM->isConnected(_tq(aUrl));
107 }
108 
isActive() const109 bool ClientManagerScript::isActive() const{
110     return CM->isActive();
111 }
112 
getMyCID() const113 QString ClientManagerScript::getMyCID() const{
114     return _q(CM->getMyCID().toBase32());
115 }
116 
on(ClientConnected,dcpp::Client * cl)117 void ClientManagerScript::on(ClientConnected, dcpp::Client *cl) throw(){
118     emit connected(_q(cl->getHubUrl()));
119 }
120 
on(ClientUpdated,dcpp::Client * cl)121 void ClientManagerScript::on(ClientUpdated, dcpp::Client *cl) throw(){
122     emit updated(_q(cl->getHubUrl()));
123 }
124 
on(ClientDisconnected,dcpp::Client * cl)125 void ClientManagerScript::on(ClientDisconnected, dcpp::Client *cl) throw(){
126     emit disconnected(_q(cl->getHubUrl()));
127 }
128