1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2010-2012 Licq developers <licq-dev@googlegroups.com>
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 LICQ_CONTACTLIST_GROUP_H
21 #define LICQ_CONTACTLIST_GROUP_H
22 
23 #include <boost/noncopyable.hpp>
24 #include <string>
25 
26 #include "../thread/lockable.h"
27 
28 namespace Licq
29 {
30 class UserId;
31 
32 /**
33  * Class holding data for a user group in the contact list.
34  * System groups only exists as a bitmask in LicqUser.
35  *
36  * Note: LicqGroup objects should only be created, deleted or modified from the
37  * user manager. If set functions are called directly, plugins will not receive
38  * any signal notifying them of the change.
39  */
40 class Group : public Lockable, private boost::noncopyable
41 {
42 public:
43   /**
44    * Get id for group. This is an id used locally by Licq and is persistant for
45    * each group.
46    *
47    * @return Group id
48    */
id()49   int id() const
50   { return myId; }
51 
52   /**
53    * Get name of group as should be displayed in the user interface
54    *
55    * @return Group name
56    */
name()57   const std::string& name() const
58   { return myName; }
59 
60   /**
61    * Get sorting index for the group. This is used by user interface plugins to
62    * determine sorting order for the groups. Lower numbers should be displayed
63    * higher in the list.
64    *
65    * @return Sorting index for this group
66    */
sortIndex()67   int sortIndex() const
68   { return mySortIndex; }
69 
70   /**
71    * Group id for this group at the server side
72    *
73    * @param ownerId Id of owner to get group id for
74    * @return Server side group id or 0 if not set or not known
75    */
76   virtual unsigned long serverId(const UserId& ownerId) const = 0;
77 
78   /**
79    * Set group name
80    *
81    * @param name New group name
82    */
setName(const std::string & name)83   void setName(const std::string& name) { myName = name; }
84 
85   /**
86    * Set sorting index for group
87    *
88    * @param sortIndex Group sorting index
89    */
setSortIndex(int sortIndex)90   void setSortIndex(int sortIndex) { mySortIndex = sortIndex; }
91 
92   /**
93    * Set server side id for this group
94    *
95    * @param ownerId Id of owner to set group id for
96    * @param serverId Id for this group on server side list
97    */
98   virtual void setServerId(const UserId& ownerId, unsigned long serverId) = 0;
99 
100   /**
101    * Remove a server side id for this group
102    *
103    * @param ownerId Id of owner to clear group id for
104    */
105   virtual void unsetServerId(const UserId& ownerId) = 0;
106 
107 protected:
~Group()108   virtual ~Group() { /* Empty */ }
109 
110   int myId;
111   std::string myName;
112   int mySortIndex;
113 };
114 
115 /**
116  * Read mutex guard for Licq::Group
117  */
118 class GroupReadGuard : public ReadMutexGuard<Group>
119 {
120 public:
121   /**
122    * Constructor, will fetch and lock a group based on group id
123    * Note: Always check that the group was actually fetched before using
124    *
125    * @param groupId Id of group to fetch
126    */
127   GroupReadGuard(int groupId);
128 
129   // Derived constructors
130   GroupReadGuard(const Group* group, bool locked = false)
131     : ReadMutexGuard<Group>(group, locked)
132   { }
GroupReadGuard(ReadMutexGuard<Group> * guard)133   GroupReadGuard(ReadMutexGuard<Group>* guard)
134     : ReadMutexGuard<Group>(guard)
135   { }
136 };
137 
138 /**
139  * Write mutex guard for Licq::Group
140  */
141 class GroupWriteGuard : public WriteMutexGuard<Group>
142 {
143 public:
144   /**
145    * Constructor, will fetch and lock a group based on group id
146    * Note: Always check that the group was actually fetched before using
147    *
148    * @param groupId Id of group to fetch
149    */
150   GroupWriteGuard(int groupId);
151 
152   // Derived constructors
153   GroupWriteGuard(Group* group, bool locked = false)
154     : WriteMutexGuard<Group>(group, locked)
155   { }
GroupWriteGuard(WriteMutexGuard<Group> * guard)156   GroupWriteGuard(WriteMutexGuard<Group>* guard)
157     : WriteMutexGuard<Group>(guard)
158   { }
159 };
160 
161 } // namespace Licq
162 
163 #endif // LICQ_CONTACTLIST_GROUP_H
164