1 /*
2  * %kadu copyright begin%
3  * Copyright 2013, 2014, 2015 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <QtCore/QMap>
23 #include <QtCore/QObject>
24 #include <injeqt/injeqt.h>
25 
26 #include "chat/chat.h"
27 #include "misc/iterator.h"
28 #include "exports.h"
29 
30 class ChatWidget;
31 class ChatWindow;
32 
33 /**
34  * @addtogroup Gui
35  * @{
36  */
37 
38 /**
39  * @class ChatWindowRepository
40  * @short Repository of ChatWindow instances.
41  *
42  * This repository holds instances of ChatWindow class.
43  *
44  * Instances are added and removed by addChatWidget(ChatWindow*) and
45  * removeChatWidget(ChatWindow*) methods.
46  * Access for them is provided by windowForChatWidget(Chat) and windows() methods.
47  *
48  * ChatWindow is also automatically removed when it is destroyed.
49  */
50 class KADUAPI ChatWindowRepository : public QObject
51 {
52 	Q_OBJECT
53 
54 	using Storage = std::map<Chat, ChatWindow *>;
55 	using WrappedIterator = Storage::iterator;
56 
57 public:
58 	using Iterator = IteratorWrapper<WrappedIterator, ChatWindow *>;
59 
60 	Q_INVOKABLE explicit ChatWindowRepository(QObject *parent = nullptr);
61 	virtual ~ChatWindowRepository();
62 
63 	/**
64 	 * @short Begin iterator that returns ChatWindow *.
65 	 */
66 	Iterator begin();
67 
68 	/**
69 	 * @short Begin iterator that returns ChatWindow *.
70 	 */
71 	Iterator end();
72 
73 	/**
74 	 * @short Return true if repository has chat window for given chat.
75 	 */
76 	bool hasWindowForChat(const Chat &chat) const;
77 
78 	/**
79 	 * @short Return ChatWindow for given chatWidget.
80 	 * @param chat chat to get ChatWindow for
81 	 * @return ChatWindow for given chat
82 	 *
83 	 * If chat is null then nullptr is returned. If repository does contain chat then
84 	 * it is returned. Else nullptr is returned.
85 	 */
86 	ChatWindow * windowForChat(const Chat &chat);
87 
88 public slots:
89 	/**
90 	 * @short Add new chatWindow to repository.
91 	 *
92 	 * Add new chatWindow to repository only if it is valid and not already in repository.
93 	 */
94 	void addChatWindow(ChatWindow *chatWindow);
95 
96 	/**
97 	 * @short Remove chatWindow from repository.
98 	 *
99 	 * Remove chatWindow from repository only if it is  already in repository.
100 	 */
101 	void removeChatWindow(ChatWindow *chatWindow);
102 
103 private:
104 	static ChatWindow * converter(WrappedIterator iterator);
105 
106 	Storage m_windows;
107 
108 private slots:
109 	INJEQT_DONE void done();
110 
111 };
112 
begin(ChatWindowRepository * chatWindowRepository)113 inline ChatWindowRepository::Iterator begin(ChatWindowRepository *chatWindowRepository)
114 {
115 	return chatWindowRepository->begin();
116 }
117 
end(ChatWindowRepository * chatWindowRepository)118 inline ChatWindowRepository::Iterator end(ChatWindowRepository *chatWindowRepository)
119 {
120 	return chatWindowRepository->end();
121 }
122 
123 /**
124  * @}
125  */
126