1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-2010 Licq developers
4  *
5  * Licq 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) any later version.
9  *
10  * Licq 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 Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #ifndef CONTACTGROUP_H
21 #define CONTACTGROUP_H
22 
23 #include <QList>
24 #include <QString>
25 #include <QVariant>
26 
27 #include "contactitem.h"
28 #include "contactlist.h"
29 
30 namespace Licq
31 {
32 class Group;
33 }
34 
35 namespace LicqQtGui
36 {
37 class ContactUser;
38 class ContactBar;
39 
40 /**
41  * A group in the contact list
42  *
43  * This class is used internally by ContactList and should not be accessed from any other class
44  */
45 class ContactGroup : public ContactItem
46 {
47   Q_OBJECT
48 
49 public:
50   /**
51    * Constructor, creates an empty group
52    *
53    * @param id Group id
54    * @param name Group name
55    * @param showMask Bits from user extended status required to be in group
56    * @param hideMask Bits from user extended status to reject
57    */
58   ContactGroup(int id, const QString& name = QString(), unsigned showMask = 0, unsigned hideMask = 0);
59 
60   /**
61    * Constructor, creates an empty group
62    *
63    * @param group Group object from daemon
64    */
65   ContactGroup(const Licq::Group* group);
66 
67   /**
68    * Destructor, will delete all user instances in the group
69    */
70   virtual ~ContactGroup();
71 
72   /**
73    * Group data in daemon has changed and needs to be refetched
74    */
75   void update();
76 
77   /**
78    * Get the Licq id for this group
79    */
groupId()80   int groupId() const
81   { return myGroupId; }
82 
83   /**
84    * Get name of the group
85    */
name()86   const QString& name() const
87   { return myName; }
88 
89   /**
90    * Get number of items in this group (users and separator bars)
91    */
92   int rowCount() const;
93 
94   /**
95    * Get the position of a user in this group
96    *
97    * @param user A user in this group
98    * @return The row index of the user
99    */
100   int indexOf(ContactUser* user) const;
101 
102   /**
103    * Get an item from this group (user or separator bar)
104    *
105    * @param row index of the item to return
106    * @return The item for the requested row if present or 0 otherwise
107    */
108   ContactItem* item(int row) const;
109 
110   /**
111    * Get a user from this group
112    *
113    * @param user The userdata to get a user instance for
114    * @return The user for the requested row if present or 0 otherwise
115    */
116   ContactUser* user(ContactUserData* user) const;
117 
118   /**
119    * Add a user to this sub group
120    * Note: Do not call this method directly, it should only be called from the user instance constructor.
121    *
122    * @param user The user instance to add
123    * @param subGroup The initial sub group to place the user in
124    */
125   void addUser(ContactUser* user, ContactListModel::SubGroupType subGroup);
126 
127   /**
128    * Remove a user from this sub group
129    * Note: Do not call this method directly, it should only be called from the user instance destructor.
130    *
131    * @param user The user instance to remove
132    * @param subGroup The current sub group of the user
133    */
134   void removeUser(ContactUser* user, ContactListModel::SubGroupType subGroup);
135 
136   /**
137    * Update the sub group counters
138    *
139    * @param oldSubGroup The previous sub group
140    * @param newSubGroup The new sub group
141    * @param eventCounter Current number of events for contact that needs to be moved between the subgroups counters
142    */
143   void updateSubGroup(ContactListModel::SubGroupType oldSubGroup, ContactListModel::SubGroupType newSubGroup, int eventCounter);
144 
145   /**
146    * Update unread event counter for group
147    *
148    * @param counter Number to increase or decrease event counter by
149    * @param subGroup Current subgroup for user that also should be updated
150    */
151   void updateNumEvents(int counter, ContactListModel::SubGroupType subGroup);
152 
153   /**
154    * Update visibility counter
155    *
156    * @param increase True if counter should be increased
157    * @param subGroup Current subgroup for user that also should be updated
158    */
159   void updateVisibility(bool increase, ContactListModel::SubGroupType subGroup);
160 
161   /**
162    * Get data for this group
163    *
164    * @param column A valid column in the contact list
165    * @param role The qt role to get data for
166    * @return Data for this group
167    */
168   QVariant data(int column, int role) const;
169 
170   /**
171    * Set data for this group
172    *
173    * @param value New value to set
174    * @param role Role to set
175    * @return True if any data was changed
176    */
177   virtual bool setData(const QVariant& value, int role = ContactListModel::NameRole);
178 
179   /**
180    * Update sort key for this group from daemon
181    */
182   void updateSortKey();
183 
184   /**
185    * Check if a user can be accepted in this group
186    *
187    * @param extendedStatus Extended status bits for user
188    * @return True if user is allowed, false if user shouldn't be here
189    */
190   bool acceptUser(unsigned extendedStatus);
191 
192 signals:
193   /**
194    * Signal emitted when data for the group has changed
195    */
196   void dataChanged(ContactGroup* group);
197 
198   /**
199    * Signal emitted when data for a bar has changed
200    */
201   void barDataChanged(ContactBar* bar, int row);
202 
203   /**
204    * Signal emitted before a user is added
205    *
206    * @param group The affected group (always sent as this)
207    * @param row Row number for the new user
208    */
209   void beginInsert(ContactGroup* group, int row);
210 
211   /**
212    * Signal emitted after a user has been added
213    */
214   void endInsert();
215 
216   /**
217    * Signal emitted before a user is removed
218    *
219    * @param group The affected group (always sent as this)
220    * @param row Row number for the user to be removed
221    */
222   void beginRemove(ContactGroup* gorup, int row);
223 
224   /**
225    * Segnal emitted after a user has been removed
226    */
227   void endRemove();
228 
229 private:
230   int myGroupId;
231   QString myName;
232   int mySortKey;
233   int myEvents;
234   QList<ContactUser*> myUsers;
235   ContactBar* myBars[3];
236   int myVisibleContacts;
237   unsigned myShowMask;
238   unsigned myHideMask;
239 };
240 
241 } // namespace LicqQtGui
242 
243 #endif
244