ircdock.cpp
1 //------------------------------------------------------------------------------
2 // ircdock.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "ircdock.h"
24 #include "ui_ircdock.h"
25 #include "gui/irc/ircdocktabcontents.h"
26 #include "gui/irc/ircnetworkselectionbox.h"
27 #include "gui/irc/ircsounds.h"
28 #include "irc/configuration/chatnetworkscfg.h"
29 #include "irc/configuration/ircconfig.h"
30 #include "irc/ircnetworkadapter.h"
31 
32 #include <QInputDialog>
33 #include <QMessageBox>
34 #include <QToolBar>
35 
36 DClass<IRCDock> : public Ui::IRCDock
37 {
38 public:
39  IRCSounds pSounds;
40  QAction* toolBarConnect;
41  QAction* toolBarOpenChatWindow;
42 };
43 
44 DPointered(IRCDock)
45 
46 IRCDock::IRCDock(QWidget* parent)
47 : QDockWidget(parent)
48 {
49  d->setupUi(this);
50  toggleViewAction()->setIcon(QIcon(":/icons/chat.png"));
51 
52  d->pSounds.loadFromConfig();
53 
54  setupToolbar();
55 
56  connect(d->tabWidget, SIGNAL( currentChanged(int) ),
57  SLOT( tabCurrentChanged(int) ));
58 
59  connect(d->tabWidget, SIGNAL( tabCloseRequested(int) ),
60  SLOT( tabCloseRequestedSlot(int) ));
61 }
62 
63 IRCDock::~IRCDock()
64 {
65 }
66 
67 IRCDockTabContents* IRCDock::addIRCAdapter(IRCAdapterBase* pIRCAdapter)
68 {
69  IRCDockTabContents* pNewAdapterWidget = new IRCDockTabContents(this);
70 
71  connect(pNewAdapterWidget, SIGNAL( chatWindowCloseRequest(IRCDockTabContents*) ), SLOT( chatWindowCloseRequestSlot(IRCDockTabContents*) ) );
72  connect(pNewAdapterWidget, SIGNAL( focusRequest(IRCDockTabContents*) ), SLOT( tabFocusRequest(IRCDockTabContents*) ) );
73  connect(pNewAdapterWidget, SIGNAL( titleChange(IRCDockTabContents*) ), SLOT( titleChange(IRCDockTabContents*) ) );
74  connect(pNewAdapterWidget, SIGNAL(newMessagePrinted()),
75  SLOT(titleChangeWithColorOfSenderIfNotFocused()));
76  connect(pNewAdapterWidget, SIGNAL(titleBlinkRequested()),
77  SLOT(titleChangeWithColorOfSenderIfNotFocused()));
78 
79  pNewAdapterWidget->setIRCAdapter(pIRCAdapter);
80  d->tabWidget->addTab(pNewAdapterWidget, pNewAdapterWidget->icon(), pNewAdapterWidget->title());
81  this->titleChange(pNewAdapterWidget);
82 
83  return pNewAdapterWidget;
84 }
85 
87 {
88  for (int i = 0; i < d->tabWidget->count(); ++i)
89  {
90  IRCDockTabContents* pWidget = (IRCDockTabContents*)d->tabWidget->widget(i);
91  pWidget->applyAppearanceSettings();
92  }
93 }
94 
95 void IRCDock::chatWindowCloseRequestSlot(IRCDockTabContents* pCaller)
96 {
97  int tabIndex = d->tabWidget->indexOf(pCaller);
98  if (tabIndex >= 0)
99  {
100  tabCloseRequestedSlot(tabIndex);
101  }
102 }
103 
104 void IRCDock::connectToNewNetwork(IRCNetworkConnectionInfo& connectionInfo, bool bFocusOnNewTab)
105 {
106  IRCNetworkAdapter* pIRCNetworkAdapter = new IRCNetworkAdapter(connectionInfo);
107 
108  // Switch this to true only for debug.
109  pIRCNetworkAdapter->setEmitAllIRCMessagesEnabled(false);
110 
111  // Setup the UI tab for the new network.
112  IRCDockTabContents* pTab = addIRCAdapter(pIRCNetworkAdapter);
113 
114  // Connect to the network.
115  pIRCNetworkAdapter->connect();
116 
117  if (bFocusOnNewTab)
118  {
119  tabFocusRequest(pTab);
120  }
121 }
122 
123 bool IRCDock::hasTabFocus(const IRCDockTabContents* pTab) const
124 {
125  return (this->d->tabWidget->currentWidget() == pTab);
126 }
127 
128 IRCNetworkAdapter* IRCDock::networkWithUiFocus()
129 {
130  IRCDockTabContents* pWidget = (IRCDockTabContents*)d->tabWidget->currentWidget();
131  if (pWidget == NULL)
132  {
133  return NULL;
134  }
135 
136  return pWidget->ircAdapter()->network();
137 }
138 
139 void IRCDock::performNetworkAutojoins()
140 {
141  IRCNetworkConnectionInfo connectionInfo;
142  connectionInfo.alternateNick = gIRCConfig.personal.alternativeNickname;
143  connectionInfo.nick = gIRCConfig.personal.nickname;
144  connectionInfo.realName = gIRCConfig.personal.fullName;
145 
146  connectionInfo.fillInMissingFields();
147 
148  QList<IRCNetworkEntity> autojoinNetworks = ChatNetworksCfg().autoJoinNetworks();
149  foreach (const IRCNetworkEntity& network, autojoinNetworks)
150  {
151  connectionInfo.networkEntity = network;
152 
153  connectToNewNetwork(connectionInfo, false);
154  }
155 }
156 
157 QString IRCDock::prefixMessage(IRCAdapterBase* pTargetChatWindow, IRCAdapterBase* pMessageSender, const QString& message)
158 {
159  if (pMessageSender != NULL)
160  {
161  IRCNetworkAdapter* pTargetNetwork = pTargetChatWindow->network();
162  if (pTargetNetwork != pMessageSender)
163  {
164  return QString("%1: %2").arg(pMessageSender->title(), message);
165  }
166  }
167 
168  return message;
169 }
170 
171 void IRCDock::setupToolbar()
172 {
173  QToolBar* pToolBar = new QToolBar(this);
174  pToolBar->setMovable(false);
175  pToolBar->setOrientation(Qt::Vertical);
176 
177  d->toolBarConnect = new QAction(QIcon(":/icons/network-connect-3.png"), tr("Connect"), pToolBar);
178  d->toolBarOpenChatWindow = new QAction(QIcon(":/icons/irc_channel.png"), tr("Open chat window"), pToolBar);
179 
180  pToolBar->addAction(d->toolBarConnect);
181  pToolBar->addAction(d->toolBarOpenChatWindow);
182 
183  d->horizontalLayout->insertWidget(0, pToolBar);
184  connect(pToolBar, SIGNAL( actionTriggered(QAction*) ), this, SLOT( toolBarAction(QAction*) ) );
185 }
186 
187 IRCSounds& IRCDock::sounds()
188 {
189  return d->pSounds;
190 }
191 
192 void IRCDock::tabCloseRequestedSlot(int index)
193 {
194  QWidget* pPageWidget = d->tabWidget->widget(index);
195  d->tabWidget->removeTab(index);
196 
197  delete pPageWidget;
198 }
199 
200 void IRCDock::tabCurrentChanged(int index)
201 {
202  if (index >= 0)
203  {
204  d->tabWidget->tabBarPublic()->setTabTextColor(index, "");
205  IRCDockTabContents* pTab = (IRCDockTabContents*) d->tabWidget->widget(index);
206  pTab->grabFocus();
207  }
208 }
209 
210 void IRCDock::tabFocusRequest(IRCDockTabContents* pCaller)
211 {
212  d->tabWidget->setCurrentWidget(pCaller);
213 }
214 
215 IRCDockTabContents *IRCDock::tabWithFocus()
216 {
217  if (d->tabWidget->currentWidget() != NULL)
218  {
219  return static_cast<IRCDockTabContents*>(d->tabWidget->currentWidget());
220  }
221  return NULL;
222 }
223 
224 void IRCDock::titleChange(IRCDockTabContents* caller)
225 {
226  int tabIndex = d->tabWidget->indexOf(caller);
227  if (tabIndex >= 0)
228  {
229  d->tabWidget->setTabText(tabIndex, caller->title());
230  }
231 }
232 
233 void IRCDock::titleChangeWithColorOfSenderIfNotFocused()
234 {
235  IRCDockTabContents* caller = static_cast<IRCDockTabContents*>(sender());
236  int tabIndex = d->tabWidget->indexOf(caller);
237  if (tabIndex >= 0)
238  {
239  d->tabWidget->setTabText(tabIndex, caller->title());
240  d->tabWidget->tabBarPublic()->setTabTextColor(tabIndex, caller->titleColor());
241  }
242 }
243 
244 void IRCDock::toolBarAction(QAction* pAction)
245 {
246  if (pAction == d->toolBarConnect)
247  {
248  IRCNetworkSelectionBox networkSelection(this);
249  if (networkSelection.exec() == QDialog::Accepted)
250  {
251  IRCNetworkConnectionInfo connectionInfo = networkSelection.networkConnectionInfo();
252  ChatNetworksCfg().setLastUsedNetwork(connectionInfo.networkEntity);
253 
254  // We will attempt to remember user credentials for further use.
255  gIRCConfig.personal.alternativeNickname = connectionInfo.alternateNick;
256  gIRCConfig.personal.nickname = connectionInfo.nick;
257  gIRCConfig.personal.fullName = connectionInfo.realName;
258 
259  connectionInfo.fillInMissingFields();
260 
261  connectToNewNetwork(connectionInfo, true);
262  }
263  }
264  else if (pAction == d->toolBarOpenChatWindow)
265  {
266  IRCNetworkAdapter* pNetwork = networkWithUiFocus();
267  if (pNetwork == NULL)
268  {
269  QMessageBox::warning(NULL, tr("Doomseeker IRC - Open chat window"), tr("Cannot obtain network connection adapter."));
270  }
271  else if (!pNetwork->isConnected())
272  {
273  QMessageBox::warning(NULL, tr("Doomseeker IRC - Open chat window"), tr("You are not connected to this network."));
274  }
275  else
276  {
277  QString recipientName = QInputDialog::getText(NULL, tr("Open chat window"), tr("Specify a channel or user name:"));
278  if (!recipientName.isEmpty())
279  {
280  pNetwork->openNewAdapter(recipientName);
281  }
282  }
283  }
284 }
Interprets communication between the client and the IRC server.
void setIRCAdapter(IRCAdapterBase *pAdapter)
Calling this multiple times on the same object will cause memory leaks.
Dockable widget designed for IRC communication.
Definition: ircdock.h:39
void openNewAdapter(const QString &recipientName)
Opens a new chat adapter for specified recipient.
void grabFocus()
Called when tab becomes active.
Provides an unified communication interface between a client and IRC network entities.
void setEmitAllIRCMessagesEnabled(bool b)
Struct containing information about client&#39;s connection to the IRC server.
IRCNetworkEntity networkEntity
Information about the network to which we will connect.
void fillInMissingFields()
Fills missing data with presets.
Dockable widget designed for IRC communication.
virtual QString title() const =0
Gets title for this adapter.
void applyAppearanceSettings()
Applies IRC appearance settings to all open tabs.
Definition: ircdock.cpp:86
Part of IRC UI package. Plays IRC related sounds.
Definition: ircsounds.h:36
virtual IRCNetworkAdapter * network()=0
The idea of the adapter system is that each adapter is either a network or is a child of a network...
Data structure that describes and defines a connection to an IRC network or server.
QString realName
User&#39;s real name. Optional.
void applyAppearanceSettings()
Applies current appearance settings from the IRC config.
QString alternateNick
Alternate nickname in case if &#39; nick &#39; is taken when connecting.
QString nick
Original nickname. This variable will always store the current nickname of the client.