⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatcore.cpp

📁 用qt4 编写的局域网聊天工具
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************** *   Copyright (C) 2007 by Anistratov Oleg                                 * *   ower@users.sourceforge.net                                            * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License version 2        * *   as published by the Free Software Foundation;                         * *                                                                         * *   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.                          * *                                                                         * ***************************************************************************/#include "chatcore.h"#include <assert.h>#include <string.h>#include <time.h>#include <QBuffer>#include <QFile>#include <QDateTime>#include <QTextBlock>#include <QCryptographicHash>#include "chatwgt.h"#include "channelwgt.h"#include "userinfo.h"#include "userwgt.h"#include "qchatsettings.h"#include "receiverthread.h"#include "senderthread.h"#include "largedatagram.h"#include "edituserinfodlg.h"#include "nlamarok.h"#include "userprofile.h"#include "smileswgt.h"#include "chattextwgt.h"#include "userstatistics.h"#include "singlemessage.h"#include "singlemsgshistory.h"#include "pluginsinterfaces.h"#include "pluginmanager.h"#include "tcpreceiverthread.h"#include "messagefilter.h"QTimer* ChatCore::m_chatTimer = new QTimer;ChatCore::ChatCore(QObject* parent) : QThread(parent),  AbstractChatCore(),  m_loggedIn        (false),  m_needCheckIp     (0),  m_nowListening    (""),  m_nlIsNew         (false),  m_chatWgt         (NULL),  m_myInfo          (0),  m_currentProfile  (0),  m_chatTime        (0),  m_mode            (Serverless),  m_uid             (0){  m_sender      = new SenderThread  ();  m_udpReceiver = new ReceiverThread();  m_tcpReceiver = new TcpReceiverThread();  m_udpReceiver->setObjectName("udpReceiver");  m_tcpReceiver->setObjectName("tcpReceiver");  m_receiver = m_udpReceiver;  m_settings = new QChatSettings;  m_smhModel = new SingleMsgsHistoryModel;#if defined(Q_OS_LINUX)  m_nlAmarok = new NLAmarok(this);#endif  UserInfo::setMyInfo(m_myInfo);  QChatSettings::setSettings(m_settings);  m_pluginManager = new PluginManager;  m_statusTimer = new QTimer(this);  m_chatTimer->start(1000);  connect(m_chatTimer  , SIGNAL(timeout()), this, SLOT(slot_updateChatTime()));  connect(m_tcpReceiver, SIGNAL(disconnected()), this, SLOT(slot_disconnectedFromServer()));#if defined(Q_OS_LINUX)  connect(m_nlAmarok, SIGNAL(updated   (const QString&, const QString&, const QString&)),          this      , SLOT  (slot_setNl(const QString&, const QString&, const QString&)));#endif}ChatCore::~ChatCore(){  qDebug("[~ChatCore]");  stopThreads();  delete m_settings;  delete m_smhModel;  delete m_pluginManager;  m_sender     ->deleteLater();  m_udpReceiver->deleteLater();  m_tcpReceiver->deleteLater();  qDebug("[~ChatCore]: end");}//\*****************************************************************************void ChatCore::sendData(quint64 uid){//   qDebug("sending to %u %s", uid, QHostAddress(uid).toString().toAscii().data());  if(largeDtgrm())  {    quint32 id = m_sender->getValidID();    if(id)    {      emit wantSendLargeData(header(), headerSize(), data(), dataSize(), uid, id);      setNULLdataAndHeader();    }  }  else  {    //*********************    // Compression    setCompressed(outputBuffer(), settings()->boolOption("UseCompression"));    if(settings()->boolOption("UseCompression") && protocolVersion() >= 4)    {      char* buf;      uint  size = outputBufferSize();      buf = compress(outputBuffer(), size);      if(buf && size < (uint)outputBufferSize())        setOutputBuffer(buf, size);      else        setCompressed(outputBuffer(), false);      free(buf);    }    // Compression    //*********************    quint16 size = outputBufferSize() <= MAX_PACKET_LEN ? outputBufferSize() : (MAX_PACKET_LEN);    if(m_settings->intOption("ProtocolVersion") < 4 && m_receiver == m_udpReceiver)    {    }    else    {      setPacketSize(size);    }    int bs = -2;    if(m_mode == Serverless && QChatSettings::settings()->boolOption("UseIPList") && (QHostAddress(uid) == QChatSettings::broadcast()))    {      foreach(QHostAddress addr, QChatSettings::ipList())        m_sender->send(outputBuffer(), size, addr);      m_sender->send(outputBuffer(), size, QHostAddress(QChatSettings::settings()->hostAddressOption("IP")));    }    else      bs = m_sender->send(outputBuffer(), size, QHostAddress(uid));    qDebug("[ChatCore::sendData]: dtgrm size = %d, sent = %d\n", size, bs);  }  clearParametrs();}//\*****************************************************************************void ChatCore::slot_prepareAndSend(const QString & ch_name_id, quint64 uid, AbstractChatCore::DataType data_type, const QString & msg, AbstractChatCore::ChannelType ch_type, QByteArray* add_pars){  qDebug("[ChatCore::slot_prepareAndSend]: type = %u", data_type);  qDebug("[ChatCore::slot_prepareAndSend]: 'channel_name' = '%s'", ch_name_id.toLocal8Bit().data());  qDebug("[ChatCore::slot_prepareAndSend]: 'msg' = '%s'", msg.toLocal8Bit().data());  if(data_type != AbstractChatCore::SINGLE_MESSAGE)    addParametr("Channel", ch_name_id.toUtf8());  if(add_pars)  {    parametrs().append(*add_pars);    add_pars->clear();  }  if(data_type == AbstractChatCore::MESSAGE)  {    addParametr("HTML", QByteArray());    addParametr("Color", QByteArray().append(m_settings->myColor().red  ())                                     .append(m_settings->myColor().green())                                     .append(m_settings->myColor().blue ()));    processSmiles(msg);    bool send_nl = m_nlIsNew && !m_nowListening.isEmpty() && (m_settings->nlMode() & 1);    if(send_nl)    {      m_nowListening = m_settings->strOption("NLFormat");      m_nowListening.replace("%a", m_nlArtist).replace("%b", m_nlAlbum).replace("%t", m_nlTitle);    }    prepareDatagramWrapper(AbstractChatCore::MESSAGE,                    uid,                    send_nl ? msg + "\n" + m_nowListening : msg,                    ch_type);    m_nlIsNew = (m_nlIsNew && !send_nl);  }  else    prepareDatagramWrapper(data_type, uid, msg, ch_type);  sendData(uid);}//\*****************************************************************************void ChatCore::slot_singleMessage(const QString & msg, quint64 uid, bool important/*, quint32 type*/){  qDebug("[ChatCore::slot_singleMessage]: addr = '%s'", QHostAddress(uid).toString().toLocal8Bit().data());  qDebug("[ChatCore::slot_singleMessage]: msg  = '%s'", msg.toLocal8Bit().data());  if(important)    addParametr("Important", QByteArray());  prepareDatagramWrapper(AbstractChatCore::SINGLE_MESSAGE,                  uid,                  msg,                  Common);  {    // FIXME this is not optimal solution :)    //***************    setInputBuffer(outputBuffer(), outputBufferSize());    fillHeader();    UserInfo* info = getUserInfo(uid);    hdr()->src_ip         = hdr()->dest_ip;    if(info)    {      hdr()->name           = info->nickname();      hdr()->comp_name      = info->compName();      hdr()->programVersion = info->programVerID();    }    else      hdr()->name           = "*";      hdr()->comp_name      = "*";      hdr()->programVersion = Globals::VersionID;    //***************    SingleMessage* msg = new SingleMessage(hdr(), info ? info->ip() : uid);    m_smhModel->addMessage(msg);  }  sendData(uid);}//\*****************************************************************************void ChatCore::slot_statusAnswer(const QString & name_id, quint64 uid, ChannelType channel_type, bool changed, bool send_icon){  if(m_myInfo->status(name_id) == Globals::INVISIBLE)    return;  if(send_icon)  {    QByteArray data;    QBuffer    buffer;    const QImage* icon = m_myInfo->newIconImg();    buffer.setBuffer(&data);    if(icon)    {      icon->save(&buffer, "PNG", 100);      delete icon;    }    addParametr("Icon"   , data);  }  addParametr("Status"   , QByteArray().append((char)m_myInfo->status(name_id)));  addParametr("Channel"  , name_id.toUtf8());  addParametr("Version"  , Globals::VersionStr.toUtf8());  addParametr("StatusDescription", UserInfo::myInfo()->statusDescription(name_id).toUtf8());  addParametr("Gender"   , QString().setNum(m_myInfo->gender()).toUtf8());  addParametr("OS"       , UserStatistics::OsString().toUtf8());  addParametr("Uptime"   , QString().setNum(UserStatistics::getUptime()).toUtf8());  addParametr("ChatTime" , QString().setNum(m_chatTime).toUtf8());  addParametr("FirstName", m_myInfo->firstName().toUtf8());  addParametr("IpAddress", QString().setNum(m_settings->hostAddressOption("IP").toIPv4Address()).toUtf8());  if(!changed)    prepareDatagramWrapper(AbstractChatCore::STATUS_ANSWER,                           uid,                           QString(""),                           channel_type);  else  {    QString str = "Status Changed : " + Globals::StatusStr[m_myInfo->status(name_id)];    if(!UserInfo::myInfo()->statusDescription(name_id).isEmpty())      str += " (" + UserInfo::myInfo()->statusDescription(name_id) + ")";    prepareDatagramWrapper(AbstractChatCore::INF_STATUS_CHNGD,                           QChatSettings::settings()->broadcast().toIPv4Address(),                           str);  }  qDebug("[ChatCore::slot_statusAnswer] ip = %s, ip = %u", QHostAddress(uid).toString().toLocal8Bit().data(), (int)uid);  sendData(uid);}//\*****************************************************************************void ChatCore::slot_infStatusChanged(const QString & name_id){  addParametr("Channel", name_id.toUtf8());  addParametr("Status" , QByteArray().append((char)m_myInfo->status(name_id)));  prepareDatagramWrapper(AbstractChatCore::INF_STATUS_CHNGD,                         QChatSettings::settings()->broadcast().toIPv4Address(),                         QString("STATUS CHANGED")                        );  sendData(QChatSettings::settings()->broadcast().toIPv4Address());}//\*****************************************************************************void ChatCore::slot_infoAnswer(const QString & name_id, quint64 uid, ChannelType type, uchar pics_ok){  if(m_myInfo->status(name_id) == Globals::INVISIBLE)    return;  qDebug("\n[ChatCore::slot_infoAnswer] channel_name = %s, pics_ok = %d\n", name_id.toLocal8Bit().data(), pics_ok);  QByteArray data;  QBuffer    buffer;  QImage*    pix;  addParametr("LastName"   , m_myInfo->lastName   ().toUtf8());  addParametr("FirstName"  , m_myInfo->firstName  ().toUtf8());  addParametr("SecondName" , m_myInfo->secondName ().toUtf8());  addParametr("DateOfBorn" , m_myInfo->dateOfBorn ().toString().toUtf8());  addParametr("Address"    , m_myInfo->address    ().toUtf8());  addParametr("HomePhone"  , m_myInfo->homePhone  ().toUtf8());  addParametr("WorkPhone"  , m_myInfo->workPhone  ().toUtf8());  addParametr("MobilePhone", m_myInfo->mobilePhone().toUtf8());  addParametr("e-mail"     , m_myInfo->e_mail     ().toUtf8());  addParametr("ICQ"        , m_myInfo->icq        ().toUtf8());  addParametr("Homepage"   , m_myInfo->homepage   ().toUtf8());  addParametr("AboutInfo"  , m_myInfo->aboutInfo  ().toUtf8());  addParametr("Gender"     , QString().setNum(m_myInfo->gender()).toUtf8());  addParametr("IpAddress"  , QString().setNum(m_settings->hostAddressOption("IP").toIPv4Address()).toUtf8());  // TODO sdelat' nastraivaemyi razmer kartinok  buffer.setBuffer(&data);  if(pics_ok & 1)    addParametr("PictureOk", QByteArray());  else  {    if((pix = m_myInfo->newPictureImg()))    {//       if(pix->height() > 500)//         pix->scaledToHeight(500).save(&buffer);//       else        pix->save(&buffer, "PNG", 100);      delete pix;    }    addParametr("Picture"    , data);    addParametr("PictureHash", m_myInfo->pictureHash());  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -