1/*
2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef TERMINAL_ROSTER_H
6#define TERMINAL_ROSTER_H
7
8
9#include <Clipboard.h>
10#include <Handler.h>
11#include <Locker.h>
12
13#include <ObjectList.h>
14
15
16/*!	Provides access to the (virtual) global terminal roster.
17
18	Register() registers our terminal with the roster and establishes the link
19	to the roster. From then on CountTerminals() and TerminalAt() provide
20	access to the terminal list and allows to post our window status via
21	SetWindowInfo(). The list is automatically kept up to date. Unregister()
22	will remove our terminal from the roster and terminate the connection.
23
24	A single Listener can be set. It is notified whenever the terminal list
25	has changed.
26*/
27class TerminalRoster : private BHandler {
28public:
29			class Listener;
30
31public:
32			struct Info {
33				int32			id;
34				team_id			team;
35				uint32			workspaces;
36				bool			minimized;
37
38			public:
39								Info(int32 id, team_id team);
40								Info(const BMessage& archive);
41
42				status_t		Archive(BMessage& archive) const;
43
44				bool			operator==(const Info& other) const;
45				bool			operator!=(const Info& other) const
46									{ return !(*this == other); }
47			};
48
49public:
50								TerminalRoster();
51
52			bool				Lock();
53			void				Unlock();
54
55			status_t			Register(team_id teamID, BLooper* looper);
56			void				Unregister();
57			bool				IsRegistered() const
58									{ return fOurInfo != NULL; }
59
60			int32				ID() const;
61
62			void				SetWindowInfo(bool minimized,
63									uint32 workspaces);
64
65			// roster must be locked
66			int32				CountTerminals() const
67									{ return fInfos.CountItems(); }
68			const Info*			TerminalAt(int32 index) const
69									{ return fInfos.ItemAt(index); }
70
71			void				SetListener(Listener* listener)
72									{ fListener = listener; }
73
74private:
75	// BHandler
76	virtual	void				MessageReceived(BMessage* message);
77
78private:
79			typedef BObjectList<Info> InfoList;
80
81private:
82			status_t			_UpdateInfos(bool checkApps);
83			status_t			_UpdateClipboard();
84
85			void				_NotifyListener();
86
87	static	int					_CompareInfos(const Info* a, const Info* b);
88	static	int					_CompareIDInfo(const int32* id,
89									const Info* info);
90
91			bool				_TeamIsRunning(team_id teamID);
92
93private:
94			BLocker				fLock;
95			BClipboard			fClipboard;
96			InfoList			fInfos;
97			Info*				fOurInfo;
98			bigtime_t			fLastCheckedTime;
99			Listener*			fListener;
100			bool				fInfosUpdated;
101};
102
103
104class TerminalRoster::Listener {
105public:
106	virtual						~Listener();
107
108	virtual	void				TerminalInfosUpdated(TerminalRoster* roster)
109									= 0;
110};
111
112
113#endif	// TERMINAL_ROSTER_H
114