modernize: Pass arguments by value and move in constructors
[quassel.git] / src / client / client.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
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                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "client.h"
22
23 #include "abstractmessageprocessor.h"
24 #include "abstractui.h"
25 #include "bufferinfo.h"
26 #include "buffermodel.h"
27 #include "buffersettings.h"
28 #include "buffersyncer.h"
29 #include "bufferviewconfig.h"
30 #include "bufferviewoverlay.h"
31 #include "clientaliasmanager.h"
32 #include "clientbacklogmanager.h"
33 #include "clientbufferviewmanager.h"
34 #include "clientirclisthelper.h"
35 #include "clientidentity.h"
36 #include "clientignorelistmanager.h"
37 #include "clienttransfermanager.h"
38 #include "clientuserinputhandler.h"
39 #include "coreaccountmodel.h"
40 #include "coreconnection.h"
41 #include "dccconfig.h"
42 #include "ircchannel.h"
43 #include "ircuser.h"
44 #include "message.h"
45 #include "messagemodel.h"
46 #include "network.h"
47 #include "networkconfig.h"
48 #include "networkmodel.h"
49 #include "quassel.h"
50 #include "signalproxy.h"
51 #include "transfermodel.h"
52 #include "util.h"
53 #include "clientauthhandler.h"
54
55 #include <stdio.h>
56 #include <stdlib.h>
57
58 Client::Client(std::unique_ptr<AbstractUi> ui, QObject *parent)
59     : QObject(parent), Singleton<Client>(this),
60     _signalProxy(new SignalProxy(SignalProxy::Client, this)),
61     _mainUi(std::move(ui)),
62     _networkModel(new NetworkModel(this)),
63     _bufferModel(new BufferModel(_networkModel)),
64     _bufferSyncer(nullptr),
65     _aliasManager(nullptr),
66     _backlogManager(new ClientBacklogManager(this)),
67     _bufferViewManager(nullptr),
68     _bufferViewOverlay(new BufferViewOverlay(this)),
69     _coreInfo(new CoreInfo(this)),
70     _dccConfig(nullptr),
71     _ircListHelper(new ClientIrcListHelper(this)),
72     _inputHandler(new ClientUserInputHandler(this)),
73     _networkConfig(nullptr),
74     _ignoreListManager(nullptr),
75     _highlightRuleManager(nullptr),
76     _transferManager(nullptr),
77     _transferModel(new TransferModel(this)),
78     _messageModel(_mainUi->createMessageModel(this)),
79     _messageProcessor(_mainUi->createMessageProcessor(this)),
80     _coreAccountModel(new CoreAccountModel(this)),
81     _coreConnection(new CoreConnection(this)),
82     _connected(false)
83 {
84 #ifdef EMBED_DATA
85     Q_INIT_RESOURCE(data);
86 #endif
87
88     //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
89     connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
90     connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
91     connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
92
93     connect(this, SIGNAL(networkRemoved(NetworkId)), _networkModel, SLOT(networkRemoved(NetworkId)));
94     connect(this, SIGNAL(networkRemoved(NetworkId)), _messageProcessor, SLOT(networkRemoved(NetworkId)));
95
96     connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
97     connect(coreConnection(), SIGNAL(stateChanged(CoreConnection::ConnectionState)), SLOT(connectionStateChanged(CoreConnection::ConnectionState)));
98
99     SignalProxy *p = signalProxy();
100
101     p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
102     p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
103
104     p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
105     p->attachSignal(inputHandler(), SIGNAL(sendInput(BufferInfo, QString)));
106     p->attachSignal(this, SIGNAL(requestNetworkStates()));
107
108     p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
109     p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
110     p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
111     p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
112
113     p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &, const QStringList &)), SIGNAL(createNetwork(const NetworkInfo &, const QStringList &)));
114     p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
115     p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
116     p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
117
118     p->attachSignal(this, SIGNAL(requestPasswordChange(PeerPtr,QString,QString,QString)), SIGNAL(changePassword(PeerPtr,QString,QString,QString)));
119     p->attachSlot(SIGNAL(passwordChanged(PeerPtr,bool)), this, SLOT(corePasswordChanged(PeerPtr,bool)));
120
121     p->attachSignal(this, SIGNAL(requestKickClient(int)), SIGNAL(kickClient(int)));
122     p->attachSlot(SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
123
124     p->synchronize(backlogManager());
125     p->synchronize(coreInfo());
126     p->synchronize(_ircListHelper);
127
128     coreAccountModel()->load();
129     coreConnection()->init();
130 }
131
132
133 Client::~Client()
134 {
135     disconnectFromCore();
136 }
137
138
139 AbstractUi *Client::mainUi()
140 {
141     return instance()->_mainUi.get();
142 }
143
144
145 bool Client::isCoreFeatureEnabled(Quassel::Feature feature)
146 {
147     return coreConnection()->peer() ? coreConnection()->peer()->hasFeature(feature) : false;
148 }
149
150
151 bool Client::isConnected()
152 {
153     return instance()->_connected;
154 }
155
156
157 bool Client::internalCore()
158 {
159     return currentCoreAccount().isInternal();
160 }
161
162
163 void Client::onDbUpgradeInProgress(bool inProgress)
164 {
165     emit dbUpgradeInProgress(inProgress);
166 }
167
168
169 void Client::onExitRequested(int exitCode, const QString &reason)
170 {
171     if (!reason.isEmpty()) {
172         qCritical() << reason;
173         emit exitRequested(reason);
174     }
175     QCoreApplication::exit(exitCode);
176 }
177
178
179 /*** Network handling ***/
180
181 QList<NetworkId> Client::networkIds()
182 {
183     return instance()->_networks.keys();
184 }
185
186
187 const Network *Client::network(NetworkId networkid)
188 {
189     if (instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
190     else return nullptr;
191 }
192
193
194 void Client::createNetwork(const NetworkInfo &info, const QStringList &persistentChannels)
195 {
196     emit instance()->requestCreateNetwork(info, persistentChannels);
197 }
198
199
200 void Client::removeNetwork(NetworkId id)
201 {
202     emit instance()->requestRemoveNetwork(id);
203 }
204
205
206 void Client::updateNetwork(const NetworkInfo &info)
207 {
208     Network *netptr = instance()->_networks.value(info.networkId, 0);
209     if (!netptr) {
210         qWarning() << "Update for unknown network requested:" << info;
211         return;
212     }
213     netptr->requestSetNetworkInfo(info);
214 }
215
216
217 void Client::addNetwork(Network *net)
218 {
219     net->setProxy(signalProxy());
220     signalProxy()->synchronize(net);
221     networkModel()->attachNetwork(net);
222     connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
223     instance()->_networks[net->networkId()] = net;
224     emit instance()->networkCreated(net->networkId());
225 }
226
227
228 void Client::coreNetworkCreated(NetworkId id)
229 {
230     if (_networks.contains(id)) {
231         qWarning() << "Creation of already existing network requested!";
232         return;
233     }
234     Network *net = new Network(id, this);
235     addNetwork(net);
236 }
237
238
239 void Client::coreNetworkRemoved(NetworkId id)
240 {
241     if (!_networks.contains(id))
242         return;
243     Network *net = _networks.take(id);
244     emit networkRemoved(net->networkId());
245     net->deleteLater();
246 }
247
248
249 /*** Identity handling ***/
250
251 QList<IdentityId> Client::identityIds()
252 {
253     return instance()->_identities.keys();
254 }
255
256
257 const Identity *Client::identity(IdentityId id)
258 {
259     if (instance()->_identities.contains(id)) return instance()->_identities[id];
260     else return nullptr;
261 }
262
263
264 void Client::createIdentity(const CertIdentity &id)
265 {
266     QVariantMap additional;
267 #ifdef HAVE_SSL
268     additional["KeyPem"] = id.sslKey().toPem();
269     additional["CertPem"] = id.sslCert().toPem();
270 #endif
271     emit instance()->requestCreateIdentity(id, additional);
272 }
273
274
275 void Client::updateIdentity(IdentityId id, const QVariantMap &ser)
276 {
277     Identity *idptr = instance()->_identities.value(id, 0);
278     if (!idptr) {
279         qWarning() << "Update for unknown identity requested:" << id;
280         return;
281     }
282     idptr->requestUpdate(ser);
283 }
284
285
286 void Client::removeIdentity(IdentityId id)
287 {
288     emit instance()->requestRemoveIdentity(id);
289 }
290
291
292 void Client::coreIdentityCreated(const Identity &other)
293 {
294     if (!_identities.contains(other.id())) {
295         Identity *identity = new Identity(other, this);
296         _identities[other.id()] = identity;
297         identity->setInitialized();
298         signalProxy()->synchronize(identity);
299         emit identityCreated(other.id());
300     }
301     else {
302         qWarning() << tr("Identity already exists in client!");
303     }
304 }
305
306
307 void Client::coreIdentityRemoved(IdentityId id)
308 {
309     if (_identities.contains(id)) {
310         emit identityRemoved(id);
311         Identity *i = _identities.take(id);
312         i->deleteLater();
313     }
314 }
315
316
317 /*** User input handling ***/
318
319 void Client::userInput(const BufferInfo &bufferInfo, const QString &message)
320 {
321     // we need to make sure that AliasManager is ready before processing input
322     if (aliasManager() && aliasManager()->isInitialized())
323         inputHandler()->handleUserInput(bufferInfo, message);
324     else
325         instance()->_userInputBuffer.append(qMakePair(bufferInfo, message));
326 }
327
328
329 void Client::sendBufferedUserInput()
330 {
331     for (int i = 0; i < _userInputBuffer.count(); i++)
332         userInput(_userInputBuffer.at(i).first, _userInputBuffer.at(i).second);
333
334     _userInputBuffer.clear();
335 }
336
337
338 /*** core connection stuff ***/
339
340 void Client::connectionStateChanged(CoreConnection::ConnectionState state)
341 {
342     switch (state) {
343     case CoreConnection::Disconnected:
344         setDisconnectedFromCore();
345         break;
346     case CoreConnection::Synchronized:
347         setSyncedToCore();
348         break;
349     default:
350         break;
351     }
352 }
353
354
355 void Client::setSyncedToCore()
356 {
357     // create buffersyncer
358     Q_ASSERT(!_bufferSyncer);
359     _bufferSyncer = new BufferSyncer(this);
360     connect(bufferSyncer(), SIGNAL(lastSeenMsgSet(BufferId, MsgId)), _networkModel, SLOT(setLastSeenMsgId(BufferId, MsgId)));
361     connect(bufferSyncer(), SIGNAL(markerLineSet(BufferId, MsgId)), _networkModel, SLOT(setMarkerLineMsgId(BufferId, MsgId)));
362     connect(bufferSyncer(), SIGNAL(bufferRemoved(BufferId)), this, SLOT(bufferRemoved(BufferId)));
363     connect(bufferSyncer(), SIGNAL(bufferRenamed(BufferId, QString)), this, SLOT(bufferRenamed(BufferId, QString)));
364     connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), this, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
365     connect(bufferSyncer(), SIGNAL(buffersPermanentlyMerged(BufferId, BufferId)), _messageModel, SLOT(buffersPermanentlyMerged(BufferId, BufferId)));
366     connect(bufferSyncer(), SIGNAL(bufferMarkedAsRead(BufferId)), SIGNAL(bufferMarkedAsRead(BufferId)));
367     connect(bufferSyncer(), SIGNAL(bufferActivityChanged(BufferId, const Message::Types)), _networkModel, SLOT(bufferActivityChanged(BufferId, const Message::Types)));
368     connect(bufferSyncer(), SIGNAL(highlightCountChanged(BufferId, int)), _networkModel, SLOT(highlightCountChanged(BufferId, int)));
369     connect(networkModel(), SIGNAL(requestSetLastSeenMsg(BufferId, MsgId)), bufferSyncer(), SLOT(requestSetLastSeenMsg(BufferId, const MsgId &)));
370
371     SignalProxy *p = signalProxy();
372     p->synchronize(bufferSyncer());
373
374     // create a new BufferViewManager
375     Q_ASSERT(!_bufferViewManager);
376     _bufferViewManager = new ClientBufferViewManager(p, this);
377     connect(_bufferViewManager, SIGNAL(initDone()), _bufferViewOverlay, SLOT(restore()));
378
379     // create AliasManager
380     Q_ASSERT(!_aliasManager);
381     _aliasManager = new ClientAliasManager(this);
382     connect(aliasManager(), SIGNAL(initDone()), SLOT(sendBufferedUserInput()));
383     p->synchronize(aliasManager());
384
385     // create NetworkConfig
386     Q_ASSERT(!_networkConfig);
387     _networkConfig = new NetworkConfig("GlobalNetworkConfig", this);
388     p->synchronize(networkConfig());
389
390     // create IgnoreListManager
391     Q_ASSERT(!_ignoreListManager);
392     _ignoreListManager = new ClientIgnoreListManager(this);
393     p->synchronize(ignoreListManager());
394
395     // create Core-Side HighlightRuleManager
396     Q_ASSERT(!_highlightRuleManager);
397     _highlightRuleManager = new HighlightRuleManager(this);
398     p->synchronize(highlightRuleManager());
399     // Listen to network removed events
400     connect(this, SIGNAL(networkRemoved(NetworkId)),
401         _highlightRuleManager, SLOT(networkRemoved(NetworkId)));
402
403 /*  not ready yet
404     // create TransferManager and DccConfig if core supports them
405     Q_ASSERT(!_dccConfig);
406     Q_ASSERT(!_transferManager);
407     if (isCoreFeatureEnabled(Quassel::Feature::DccFileTransfer)) {
408         _dccConfig = new DccConfig(this);
409         p->synchronize(dccConfig());
410         _transferManager = new ClientTransferManager(this);
411         _transferModel->setManager(_transferManager);
412         p->synchronize(transferManager());
413     }
414 */
415
416     // trigger backlog request once all active bufferviews are initialized
417     connect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
418
419     _connected = true;
420     emit connected();
421     emit coreConnectionStateChanged(true);
422 }
423
424 void Client::finishConnectionInitialization()
425 {
426     // usually it _should_ take longer until the bufferViews are initialized, so that's what
427     // triggers this slot. But we have to make sure that we know all buffers yet.
428     // so we check the BufferSyncer and in case it wasn't initialized we wait for that instead
429     if (!bufferSyncer()->isInitialized()) {
430         disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
431         connect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
432         return;
433     }
434     disconnect(bufferViewOverlay(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
435     disconnect(bufferSyncer(), SIGNAL(initDone()), this, SLOT(finishConnectionInitialization()));
436
437     requestInitialBacklog();
438     if (isCoreFeatureEnabled(Quassel::Feature::BufferActivitySync)) {
439         bufferSyncer()->markActivitiesChanged();
440         bufferSyncer()->markHighlightCountsChanged();
441     }
442 }
443
444
445 void Client::requestInitialBacklog()
446 {
447     _backlogManager->requestInitialBacklog();
448 }
449
450
451 void Client::requestLegacyCoreInfo()
452 {
453     // On older cores, the CoreInfo object was only synchronized on demand.  Synchronize now if
454     // needed.
455     if (isConnected() && !isCoreFeatureEnabled(Quassel::Feature::SyncedCoreInfo)) {
456         // Delete the existing core info object (it will always exist as client is single-threaded)
457         _coreInfo->deleteLater();
458         // No need to set to null when creating new one immediately after
459
460         // Create a fresh, unsynchronized CoreInfo object, emulating legacy behavior of CoreInfo not
461         // persisting
462         _coreInfo = new CoreInfo(this);
463         // Synchronize the new object
464         signalProxy()->synchronize(_coreInfo);
465
466         // Let others know signal handlers have been reset
467         emit coreInfoResynchronized();
468     }
469 }
470
471
472 void Client::disconnectFromCore()
473 {
474     if (!coreConnection()->isConnected())
475         return;
476
477     coreConnection()->disconnectFromCore();
478 }
479
480
481 void Client::setDisconnectedFromCore()
482 {
483     _connected = false;
484
485     emit disconnected();
486     emit coreConnectionStateChanged(false);
487
488     backlogManager()->reset();
489     messageProcessor()->reset();
490
491     // Clear internal data. Hopefully nothing relies on it at this point.
492
493     if (_bufferSyncer) {
494         _bufferSyncer->deleteLater();
495         _bufferSyncer = nullptr;
496     }
497
498     _coreInfo->reset();
499
500     if (_bufferViewManager) {
501         _bufferViewManager->deleteLater();
502         _bufferViewManager = nullptr;
503     }
504
505     _bufferViewOverlay->reset();
506
507     if (_aliasManager) {
508         _aliasManager->deleteLater();
509         _aliasManager = nullptr;
510     }
511
512     if (_ignoreListManager) {
513         _ignoreListManager->deleteLater();
514         _ignoreListManager = nullptr;
515     }
516
517     if (_highlightRuleManager) {
518         _highlightRuleManager->deleteLater();
519         _highlightRuleManager = nullptr;
520     }
521
522     if (_transferManager) {
523         _transferModel->setManager(nullptr);
524         _transferManager->deleteLater();
525         _transferManager = nullptr;
526     }
527
528     if (_dccConfig) {
529         _dccConfig->deleteLater();
530         _dccConfig = nullptr;
531     }
532
533     // we probably don't want to save pending input for reconnect
534     _userInputBuffer.clear();
535
536     _messageModel->clear();
537     _networkModel->clear();
538
539     QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
540     while (netIter != _networks.end()) {
541         Network *net = netIter.value();
542         emit networkRemoved(net->networkId());
543         disconnect(net, SIGNAL(destroyed()), this, nullptr);
544         netIter = _networks.erase(netIter);
545         net->deleteLater();
546     }
547     Q_ASSERT(_networks.isEmpty());
548
549     QHash<IdentityId, Identity *>::iterator idIter = _identities.begin();
550     while (idIter != _identities.end()) {
551         emit identityRemoved(idIter.key());
552         Identity *id = idIter.value();
553         idIter = _identities.erase(idIter);
554         id->deleteLater();
555     }
556     Q_ASSERT(_identities.isEmpty());
557
558     if (_networkConfig) {
559         _networkConfig->deleteLater();
560         _networkConfig = nullptr;
561     }
562 }
563
564
565 /*** ***/
566
567 void Client::networkDestroyed()
568 {
569     Network *net = static_cast<Network *>(sender());
570     QHash<NetworkId, Network *>::iterator netIter = _networks.begin();
571     while (netIter != _networks.end()) {
572         if (*netIter == net) {
573             netIter = _networks.erase(netIter);
574             break;
575         }
576         else {
577             ++netIter;
578         }
579     }
580 }
581
582
583 // Hmm... we never used this...
584 void Client::recvStatusMsg(QString /*net*/, QString /*msg*/)
585 {
586     //recvMessage(net, Message::server("", QString("[STATUS] %1").arg(msg)));
587 }
588
589
590 void Client::recvMessage(const Message &msg)
591 {
592     Message msg_ = msg;
593     messageProcessor()->process(msg_);
594 }
595
596
597 void Client::setBufferLastSeenMsg(BufferId id, const MsgId &msgId)
598 {
599     if (bufferSyncer())
600         bufferSyncer()->requestSetLastSeenMsg(id, msgId);
601 }
602
603
604 void Client::setMarkerLine(BufferId id, const MsgId &msgId)
605 {
606     if (bufferSyncer())
607         bufferSyncer()->requestSetMarkerLine(id, msgId);
608 }
609
610
611 MsgId Client::markerLine(BufferId id)
612 {
613     if (id.isValid() && networkModel())
614         return networkModel()->markerLineMsgId(id);
615     return MsgId();
616 }
617
618
619 void Client::removeBuffer(BufferId id)
620 {
621     if (!bufferSyncer()) return;
622     bufferSyncer()->requestRemoveBuffer(id);
623 }
624
625
626 void Client::renameBuffer(BufferId bufferId, const QString &newName)
627 {
628     if (!bufferSyncer())
629         return;
630     bufferSyncer()->requestRenameBuffer(bufferId, newName);
631 }
632
633
634 void Client::mergeBuffersPermanently(BufferId bufferId1, BufferId bufferId2)
635 {
636     if (!bufferSyncer())
637         return;
638     bufferSyncer()->requestMergeBuffersPermanently(bufferId1, bufferId2);
639 }
640
641
642 void Client::purgeKnownBufferIds()
643 {
644     if (!bufferSyncer())
645         return;
646     bufferSyncer()->requestPurgeBufferIds();
647 }
648
649
650 void Client::bufferRemoved(BufferId bufferId)
651 {
652     // select a sane buffer (status buffer)
653     /* we have to manually select a buffer because otherwise inconsitent changes
654      * to the model might occur:
655      * the result of a buffer removal triggers a change in the selection model.
656      * the newly selected buffer might be a channel that hasn't been selected yet
657      * and a new nickview would be created (which never heard of the "rowsAboutToBeRemoved").
658      * this new view (and/or) its sort filter will then only receive a "rowsRemoved" signal.
659      */
660     QModelIndex current = bufferModel()->currentIndex();
661     if (current.data(NetworkModel::BufferIdRole).value<BufferId>() == bufferId) {
662         bufferModel()->setCurrentIndex(current.sibling(0, 0));
663     }
664
665     // and remove it from the model
666     networkModel()->removeBuffer(bufferId);
667 }
668
669
670 void Client::bufferRenamed(BufferId bufferId, const QString &newName)
671 {
672     QModelIndex bufferIndex = networkModel()->bufferIndex(bufferId);
673     if (bufferIndex.isValid()) {
674         networkModel()->setData(bufferIndex, newName, Qt::DisplayRole);
675     }
676 }
677
678
679 void Client::buffersPermanentlyMerged(BufferId bufferId1, BufferId bufferId2)
680 {
681     QModelIndex idx = networkModel()->bufferIndex(bufferId1);
682     bufferModel()->setCurrentIndex(bufferModel()->mapFromSource(idx));
683     networkModel()->removeBuffer(bufferId2);
684 }
685
686
687 void Client::markBufferAsRead(BufferId id)
688 {
689     if (bufferSyncer() && id.isValid())
690         bufferSyncer()->requestMarkBufferAsRead(id);
691 }
692
693
694 void Client::refreshLegacyCoreInfo()
695 {
696     instance()->requestLegacyCoreInfo();
697 }
698
699
700 void Client::changePassword(const QString &oldPassword, const QString &newPassword) {
701     CoreAccount account = currentCoreAccount();
702     account.setPassword(newPassword);
703     coreAccountModel()->createOrUpdateAccount(account);
704     emit instance()->requestPasswordChange(nullptr, account.user(), oldPassword, newPassword);
705 }
706
707
708 void Client::kickClient(int peerId)
709 {
710     emit instance()->requestKickClient(peerId);
711 }
712
713
714 void Client::corePasswordChanged(PeerPtr, bool success)
715 {
716     if (success)
717         coreAccountModel()->save();
718     emit passwordChanged(success);
719 }