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

📄 chatwgt.cpp

📁 用qt4 编写的局域网聊天工具
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*************************************************************************** *   Copyright (C) 2007-2008 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 "chatwgt.h"#include "globals.h"#include <assert.h>#include <string.h>#include <time.h>#include <QSplitter>#include <QMessageBox>#include <QApplication>#include <QBuffer>#include <QDir>#include <QFile>#include <QFileDialog>#include <QHBoxLayout>#include <QInputDialog>#include <QLocale>#include <QCursor>#include <QStatusBar>#include <QKeySequence>#include <QActionGroup>#include <QPluginLoader>#include "singlemessagewgt.h"#include "filetransferwgt.h"#include "userwgt.h"#include "userinfo.h"#include "addchanneldialog.h"#include "edituserinfodlg.h"#include "receiverthread.h"#include "senderthread.h"#include "largedatagram.h"#include "largedatagramout.h"#include "logwgt.h"#include "qchatsettings.h"#include "preferencesdlg.h"#include "chatcore.h"#include "smileswgt.h"#include "channelwgt.h"#include "chattextwgt.h"#include "inputrichtextwgt.h"#include "qchattrayicon.h"#include "message.h"#include "singlemsgshistoryview.h"#include "singlemsgshistory.h"#include "aboutqchat.h"#include "pluginsinterfaces.h"#include "login2serverdlg.h"#include "tcpreceiverthread.h"#include "formattingtoolbar.h"#include "qchaticon.h"#include "shortcutseditor.h"#include "messagetreeitem.h"#include "singlemessage.h"#include "pluginmanager.h"#include "plugin.h"#include <QDesktopWidget>#include <QScrollBar>const char* BuildDate    = "";ChatWgt::ChatWgt(ChatCore* chc, QWidget* parent) : QMainWindow(parent), m_chatCore    (chc), m_formattingToolBar(NULL), m_menuToolbars(NULL), m_cursorX     (-1), m_cursorY     (-1), m_hidePlugins (true){  m_translator    = new QTranslator;  m_activityTimer = new QTimer(this);  createWidgets();  createActions();  setupLayout();  ChatTextWgt::initSmiles(QChatSettings::settings()->smilesThemePath());  mw_smiles->init();  mw_log->hide();  Globals::m_log = mw_log;  m_trayIcon->show ();  m_trayIcon->setContextMenu(m_menuFile);  //***********************************  connect(this          , SIGNAL(     singleMessage(const QString &, quint64, bool)),          m_chatCore    , SLOT  (slot_singleMessage(const QString &, quint64, bool)));  connect(m_chatCore    , SIGNAL(profileLoaded(const QString &)),          this          , SLOT  (slot_reloadProfileData()));  connect(this          , SIGNAL(wantLoadProfile (const QString &)),          m_chatCore    , SLOT  (slot_loadProfile(const QString &)));  connect(this          , SIGNAL(wantRenameProfile (const QString &, const QString &)),          m_chatCore    , SLOT  (slot_renameProfile(const QString &, const QString &)));  connect(this          , SIGNAL(wantDeleteProfile (const QString &)),          m_chatCore    , SLOT  (slot_deleteProfile(const QString &)));  connect(m_activityTimer, SIGNAL(timeout()),          this           , SLOT  (activity()));  connect(m_chatCore     , SIGNAL(disconnectedFromServer()),          this           , SLOT  (disconnectedFromServer()));  connect(m_preferencesDlg, SIGNAL(formatChanged(UserListIconFormat)), this, SLOT(changeIconFormat(UserListIconFormat)));  connect(m_preferencesDlg, SIGNAL(wantLoadPlugin(QString))  , this, SLOT(loadPlugin(QString)));  connect(m_preferencesDlg, SIGNAL(wantUnloadPlugin(QString)), this, SLOT(unloadPlugin(QString)));  connect(m_preferencesDlg, SIGNAL(useAnimatedSmiles(bool))  , this, SLOT(setAnimationsRunning(bool)));  //***********************************  UserInfo::myInfo()->setStatus(Globals::FREE);  setWindowTitle(QString("Q_Chat %1").arg(Globals::VersionStr));  QApplication::setWindowIcon(QChatIcon::icon("tray-icon"));  if(!m_translator->load("qchat_" + QLocale().name () + ".qm", QChatSettings::settings()->settingsDir()))    if(!m_translator->load("qchat_" + QLocale().name () + ".qm", "/usr/share/qchat/translations/"))      m_translator->load("qchat_" + QLocale().name () + ".qm", "/usr/local/share/qchat/translations/");  QApplication::installTranslator(m_translator);  m_activityTimer->start(1000);  retranslate();  setIcons();}//\*****************************************************************************ChatWgt::~ChatWgt(){  qDebug("[~ChatWgt]\n");  m_addChannelDlg->~AddChannelDlg();}//\*****************************************************************************ChannelWgt* ChatWgt::findChannel(const QString & name, quint32 type) const{  foreach(ChannelWgt* cw, mw_channels)    if(cw->name() == name && (1 || cw->type() == type))      return cw;  return NULL;}//\*****************************************************************************ChannelWgt* ChatWgt::findChannel(quint64 uid, quint32 type) const{  foreach(ChannelWgt* cw, mw_channels)    if(cw->destUid() == uid && (cw->type() == type))      return cw;  return NULL;}//\*****************************************************************************void ChatWgt::slot_singleMessageIn(SingleMessage* msg, bool important){  SingleMessageWgt* smw = new SingleMessageWgt(msg, 1);  connect(smw , SIGNAL(singleMessage   (const QString &, quint64, bool)),          this, SIGNAL(singleMessageOut(const QString &, quint64, bool)));  if(important)    smw->setWindowFlags(smw->windowFlags() | Qt::WindowStaysOnTopHint/* | Qt::Popup*/);  SingleMessageWgt::addNewMessage(smw);  smw->show();}//\*****************************************************************************void ChatWgt::createChannel(const QString & name, quint64 uid){  qDebug("[ChatWgt::createChannel]: begin");  int new_idx = 0;  ChannelWgt* new_ch;  if(uid == 0)  {    if(name == "Log" || name == "log")    {      new_idx = mw_tabs->addTab(mw_log, QString("Log"));      mw_tabs->setCurrentIndex(new_idx);      return;    }    if(findChannel(name))      return;  }  else  {    if(findChannel(name, 1))      return;    foreach(ChannelWgt* cw, mw_channels)      if(cw->destUid() == uid)        return;  }  if(uid == 0)  {    new_ch = new ChannelWgt(name, this);    new_idx = mw_tabs->addTab(new_ch, name);  }  else  {    new_ch = new ChannelWgt(name, this, AbstractChatCore::Private, uid);    new_idx = mw_tabs->addTab(new_ch, name + "(private)");  }  if(!mw_channels.contains(new_ch))    mw_channels.append(new_ch);  new_ch->show();  connect(new_ch, SIGNAL(wantActivate   ()), this, SLOT(slot_activateWindow ()));  connect(new_ch    , SIGNAL(     statusAnswer   (QString, quint64, AbstractChatCore::ChannelType, bool, bool)),          m_chatCore, SLOT  (slot_statusAnswer   (QString, quint64, AbstractChatCore::ChannelType, bool, bool)));  connect(new_ch    , SIGNAL(     infoAnswer     (QString, quint64, AbstractChatCore::ChannelType, uchar)),          m_chatCore, SLOT  (slot_infoAnswer     (QString, quint64, AbstractChatCore::ChannelType, uchar)));  connect(new_ch    , SIGNAL(sendSomeData(QString, quint64, AbstractChatCore::DataType, QString, AbstractChatCore::ChannelType, QByteArray*)),          m_chatCore, SLOT  (slot_prepareAndSend(QString, quint64, AbstractChatCore::DataType, QString, AbstractChatCore::ChannelType, QByteArray*)));  connect(new_ch    , SIGNAL(sendMsgsHistory       (QString, quint64, QByteArray, AbstractChatCore::ChannelType)),          m_chatCore, SLOT  (slot_msgsHistoryAnswer(QString, quint64, QByteArray, AbstractChatCore::ChannelType)));  connect(new_ch    , SIGNAL(sendMsgsNum       (const QString &, quint64, quint32, AbstractChatCore::ChannelType)),          m_chatCore, SLOT  (slot_msgsNumAnswer(const QString &, quint64, quint32, AbstractChatCore::ChannelType)));  connect(m_preferencesDlg, SIGNAL(ulRefreshIntervalChanged(uint)),          new_ch          , SLOT  (slot_changeUlRefreshInterval(uint)));  connect(m_preferencesDlg, SIGNAL(ulDeepRefreshIntervalChanged(uint)),          new_ch          , SLOT  (slot_changeUlDeepRefreshInterval(uint)));  connect(new_ch    , SIGNAL(sendMessage     (const QString &, quint64, AbstractChatCore::ChannelType, QTextDocument*)),          m_chatCore, SLOT  (slot_sendMessage(const QString &, quint64, AbstractChatCore::ChannelType, QTextDocument*)));  connect(new_ch    , SIGNAL(wantSaveState  (const QString &, const QByteArray &)),          m_chatCore, SLOT  (setChannelState(const QString &, const QByteArray &)));  connect(new_ch    , SIGNAL(avatarAnswer     (const QString &, quint64, AbstractChatCore::ChannelType)),          m_chatCore, SLOT  (slot_avatarAnswer(const QString &, quint64, AbstractChatCore::ChannelType)));  connect(m_chatCore, SIGNAL(loginFinished (int, QString)),          new_ch    , SLOT  (slot_refreshUL()));  new_ch->setFocus();  new_ch->setFocus2InputText();  new_ch->setSndOnMsgIn(QChatSettings::settings()->boolOption("SoundOnMessageIn"));  new_ch->initChannel();  mw_tabs->setCurrentIndex(new_idx);  if(uid != 0)    m_chatCore->slot_privateChatRequest(UserInfo::myInfo()->nickname(), uid);  new_ch->restoreState(m_chatCore->channelState(name));  qDebug("[ChatWgt::createChannel]: end");}//\*****************************************************************************void ChatWgt::slot_delChannell(){  ChannelWgt* wgt = qobject_cast<ChannelWgt*>(mw_tabs->currentWidget());  if(!wgt)  {    QChatWidgetPlugin* plug = qobject_cast<QChatWidgetPlugin*>(mw_tabs->currentWidget());    if(plug)    {      QMessageBox* msgbx;      int ans;      msgbx = new QMessageBox(tr("Are you sure?"), tr("Are you sure you want to unload plugin '%1'?").arg(plug->name()),                            QMessageBox::Question, QMessageBox::Yes, QMessageBox::No, 0, this, 0);      ans = msgbx->exec();      delete msgbx;      if(ans == QMessageBox::No)        return;      unloadPlugin(plug->path());    }    return;  }  qDebug("[ChatWgt::slot_delChannell]: name = %s\n", wgt->name().toLocal8Bit().data());  if(mw_tabs->currentWidget() == mw_log)  {    mw_tabs->removeTab(mw_tabs->currentIndex());    return;  }  QString name_id = wgt->name();  quint32 type    = wgt->type();  ChannelWgt* ch = findChannel(name_id, type);  if(name_id == "Main")    return;  QMessageBox* msgbx;  int ans;  msgbx = new QMessageBox(tr("Are you sure?"), tr("Are you sure you want to close channel '%1'?").arg(name_id),                          QMessageBox::Question, QMessageBox::Yes, QMessageBox::No, 0, this, 0);  ans = msgbx->exec();  delete msgbx;  if(ans == QMessageBox::No)    return;  if(ch)    mw_channels.removeAll(ch);  delete ch;}//\*****************************************************************************void ChatWgt::slot_license(){   QString str =     "This program is free software; you can redistribute it and/or modify\n"     "it under the terms of the GNU General Public License version 2\n"     "as published by the Free Software Foundation;\n\n"     "This program is distributed in the hope that it will be useful,\n"     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"     "GNU General Public License for more details.\n\n"     "Copyright (C) 2007-2008 by Anistratov Oleg\n"     "ower@users.sourceforge.net";   QMessageBox::information (this, "License", str);}//\*****************************************************************************void ChatWgt::slot_aboutQt(){  QMessageBox::aboutQt(this);}//\*****************************************************************************void ChatWgt::slot_about(){  AboutQChat* dlg = new AboutQChat(this);  dlg->exec();  delete dlg;}//\*****************************************************************************void ChatWgt::slot_showSettings(){  m_userInfoDlg->setReadOnly(false);  m_userInfoDlg->slot_loadInfo(UserInfo::myInfo());  m_userInfoDlg->slot_notEdited();  m_userInfoDlg->toggleVisible();}//\*****************************************************************************void ChatWgt::slot_showPreferences(){  m_preferencesDlg->init();  m_preferencesDlg->toggleVisible();}//\*****************************************************************************void ChatWgt::slot_showUserInfo(UserWgt* user){  m_userInfoDlg->slot_loadInfo(user->info());  m_userInfoDlg->setReadOnly(true);  m_userInfoDlg->show();}//\*****************************************************************************void ChatWgt::slot_activateWindow(){  if(!QApplication::focusWidget())    m_trayIcon->setAnimatedIcon(QChatIcon::iconPath("animated-new-message"));  if(QChatSettings::settings()->boolOption("ActivateOnMessageIn") && isHidden())

⌨️ 快捷键说明

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