clang-tidy: Avoid potential memory leak in BufferViewManager
[quassel.git] / src / core / corebufferviewmanager.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 "corebufferviewmanager.h"
22
23 #include "corebufferviewconfig.h"
24
25 #include "core.h"
26 #include "coresession.h"
27
28 INIT_SYNCABLE_OBJECT(CoreBufferViewManager)
29 CoreBufferViewManager::CoreBufferViewManager(SignalProxy *proxy, CoreSession *parent)
30     : BufferViewManager(proxy, parent),
31     _coreSession(parent)
32 {
33     QVariantMap views = Core::getUserSetting(_coreSession->user(), "BufferViews").toMap();
34     QVariantMap::iterator iter = views.begin();
35     QVariantMap::iterator iterEnd = views.end();
36     CoreBufferViewConfig *config = 0;
37     while (iter != iterEnd) {
38         config = new CoreBufferViewConfig(iter.key().toInt(), iter.value().toMap(), this);
39         addBufferViewConfig(config);
40         ++iter;
41     }
42 }
43
44
45 void CoreBufferViewManager::saveBufferViews()
46 {
47     QVariantMap views;
48
49     BufferViewConfigHash::const_iterator iter = bufferViewConfigHash().constBegin();
50     BufferViewConfigHash::const_iterator iterEnd = bufferViewConfigHash().constEnd();
51     while (iter != iterEnd) {
52         views[QString::number((*iter)->bufferViewId())] = (*iter)->toVariantMap();
53         ++iter;
54     }
55
56     Core::setUserSetting(_coreSession->user(), "BufferViews", views);
57 }
58
59
60 void CoreBufferViewManager::requestCreateBufferView(const QVariantMap &properties)
61 {
62     QString bufferViewName = properties["bufferViewName"].toString();
63     int maxId = -1;
64     BufferViewConfigHash::const_iterator iter = bufferViewConfigHash().constBegin();
65     BufferViewConfigHash::const_iterator iterEnd = bufferViewConfigHash().constEnd();
66     while (iter != iterEnd) {
67         if ((*iter)->bufferViewName() == bufferViewName)
68             return;
69
70         if ((*iter)->bufferViewId() > maxId)
71             maxId = (*iter)->bufferViewId();
72
73         ++iter;
74     }
75     maxId++;
76
77     addBufferViewConfig(new CoreBufferViewConfig(maxId, properties, this));
78 }
79
80
81 void CoreBufferViewManager::requestCreateBufferViews(const QVariantList &properties)
82 {
83     QVariantList::const_iterator iter = properties.constBegin();
84     QVariantList::const_iterator iterEnd = properties.constEnd();
85     while (iter != iterEnd) {
86         requestCreateBufferView((*iter).toMap());
87         ++iter;
88     }
89 }
90
91
92 void CoreBufferViewManager::requestDeleteBufferView(int bufferViewId)
93 {
94     deleteBufferViewConfig(bufferViewId);
95 }
96
97
98 void CoreBufferViewManager::requestDeleteBufferViews(const QVariantList &bufferViews)
99 {
100     foreach(QVariant bufferView, bufferViews) {
101         deleteBufferViewConfig(bufferView.toInt());
102     }
103 }