Port things from MSN to WLM plugin:
[kdenetwork.git] / kopete / protocols / groupwise / libgroupwise / privacymanager.cpp
blob13ab120372f6c290af2ba0781313fae5a9bc2f89
1 /*
2 Kopete Groupwise Protocol
3 privacymanager.cpp - stores the user's privacy information and maintains it on the server
5 Copyright (c) 2004 SUSE Linux AG http://www.suse.com
7 Kopete (c) 2002-2004 by the Kopete developers <kopete-devel@kde.org>
9 *************************************************************************
10 * *
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU Lesser General Public *
13 * License as published by the Free Software Foundation; either *
14 * version 2 of the License, or (at your option) any later version. *
15 * *
16 *************************************************************************
19 #include "client.h"
20 #include "tasks/privacyitemtask.h"
21 #include "userdetailsmanager.h"
23 #include "privacymanager.h"
25 PrivacyManager::PrivacyManager( Client * client)
26 : QObject(client), m_client( client )
30 PrivacyManager::~PrivacyManager()
34 bool PrivacyManager::defaultAllow()
36 return !m_defaultDeny;
39 bool PrivacyManager::defaultDeny()
41 return m_defaultDeny;
44 QStringList PrivacyManager::allowList()
46 return m_allowList;
49 QStringList PrivacyManager::denyList()
51 return m_denyList;
54 bool PrivacyManager::isPrivacyLocked()
56 return m_locked;
59 bool PrivacyManager::isBlocked( const QString & dn )
61 if ( m_defaultDeny )
62 return !m_allowList.contains( dn );
63 else
64 return m_denyList.contains( dn );
67 void PrivacyManager::setAllow( const QString & dn )
69 if ( m_defaultDeny )
71 if ( !m_allowList.contains( dn ) )
72 addAllow( dn );
74 else
76 if ( m_denyList.contains( dn ) )
77 removeDeny( dn );
81 void PrivacyManager::setDeny( const QString & dn )
83 if ( m_defaultDeny )
85 if ( m_allowList.contains( dn ) )
86 removeAllow( dn );
88 else
90 if ( !m_denyList.contains( dn ) )
91 addDeny( dn );
96 void PrivacyManager::setDefaultAllow( bool allow )
98 PrivacyItemTask * pit = new PrivacyItemTask( m_client->rootTask() );
99 pit->defaultPolicy( !allow );
100 connect( pit, SIGNAL( finished() ), SLOT( slotDefaultPolicyChanged() ) );
101 pit->go( true );
104 void PrivacyManager::setDefaultDeny( bool deny )
106 PrivacyItemTask * pit = new PrivacyItemTask( m_client->rootTask() );
107 pit->defaultPolicy( deny);
108 connect( pit, SIGNAL( finished() ), SLOT( slotDefaultPolicyChanged() ) );
109 pit->go( true );
112 void PrivacyManager::addAllow( const QString & dn )
114 // start off a CreatePrivacyItemTask
115 PrivacyItemTask * pit = new PrivacyItemTask( m_client->rootTask() );
116 pit->allow( dn );
117 connect( pit, SIGNAL( finished() ), SLOT( slotAllowAdded() ) );
118 pit->go( true );
121 void PrivacyManager::addDeny( const QString & dn )
123 // start off a CreatePrivacyItemTask
124 PrivacyItemTask * pit = new PrivacyItemTask( m_client->rootTask() );
125 pit->deny( dn );
126 connect( pit, SIGNAL( finished() ), SLOT( slotDenyAdded() ) );
127 pit->go( true );
130 void PrivacyManager::removeAllow( const QString & dn )
132 PrivacyItemTask * pit = new PrivacyItemTask( m_client->rootTask() );
133 pit->removeAllow( dn );
134 connect( pit, SIGNAL( finished() ), SLOT( slotAllowRemoved() ) );
135 pit->go( true );
138 void PrivacyManager::removeDeny( const QString & dn )
140 // start off a CreatePrivacyItemTask
141 PrivacyItemTask * pit = new PrivacyItemTask( m_client->rootTask() );
142 pit->removeDeny( dn );
143 connect( pit, SIGNAL( finished() ), SLOT( slotDenyRemoved() ) );
144 pit->go( true );
147 void PrivacyManager::setPrivacy( bool defaultIsDeny, const QStringList & allowList, const QStringList & denyList )
149 if ( defaultIsDeny != m_defaultDeny )
150 setDefaultDeny( defaultIsDeny );
151 // find the DNs no longer in the allow list
152 QStringList allowsToRemove = difference( m_allowList, allowList );
153 // find the DNs no longer in the deny list
154 QStringList denysToRemove = difference( m_denyList, denyList );
155 // find the DNs new in the allow list
156 QStringList allowsToAdd = difference( allowList, m_allowList );
157 // find the DNs new in the deny list
158 QStringList denysToAdd = difference( denyList, m_denyList );
160 QStringList::ConstIterator end = allowsToRemove.constEnd();
161 for ( QStringList::ConstIterator it = allowsToRemove.constBegin(); it != end; ++it )
162 removeAllow( *it );
163 end = denysToRemove.constEnd();
164 for ( QStringList::ConstIterator it = denysToRemove.constBegin(); it != end; ++it )
165 removeDeny( *it );
166 end = allowsToAdd.constEnd();
167 for ( QStringList::ConstIterator it = allowsToAdd.constBegin(); it != end; ++it )
168 addAllow( *it );
169 end = denysToAdd.constEnd();
170 for ( QStringList::ConstIterator it = denysToAdd.constBegin(); it != end; ++it )
171 addDeny( *it );
174 void PrivacyManager::slotGotPrivacySettings( bool locked, bool defaultDeny, const QStringList & allowList, const QStringList & denyList )
176 m_locked = locked;
177 m_defaultDeny = defaultDeny;
178 m_allowList = allowList;
179 m_denyList = denyList;
182 void PrivacyManager::getDetailsForPrivacyLists()
184 if ( !m_allowList.isEmpty() )
186 m_client->userDetailsManager()->requestDetails( m_allowList );
188 if ( !m_denyList.isEmpty() )
189 m_client->userDetailsManager()->requestDetails( m_denyList );
192 void PrivacyManager::slotDefaultPolicyChanged()
194 PrivacyItemTask * pit = ( PrivacyItemTask * )sender();
195 if ( pit->success() )
196 m_defaultDeny = pit->defaultDeny();
199 void PrivacyManager::slotAllowAdded()
201 PrivacyItemTask * pit = ( PrivacyItemTask * )sender();
202 if ( pit->success() )
204 m_allowList.append( pit->dn() );
205 emit privacyChanged( pit->dn(), isBlocked( pit->dn() ) );
209 void PrivacyManager::slotDenyAdded()
211 PrivacyItemTask * pit = ( PrivacyItemTask * )sender();
212 if ( pit->success() )
214 m_denyList.append( pit->dn() );
215 emit privacyChanged( pit->dn(), isBlocked( pit->dn() ) );
219 void PrivacyManager::slotAllowRemoved()
221 PrivacyItemTask * pit = ( PrivacyItemTask * )sender();
222 if ( pit->success() )
224 m_allowList.removeAll( pit->dn() );
225 emit privacyChanged( pit->dn(), isBlocked( pit->dn() ) );
229 void PrivacyManager::slotDenyRemoved()
231 PrivacyItemTask * pit = ( PrivacyItemTask * )sender();
232 if ( pit->success() )
234 m_denyList.removeAll( pit->dn() );
235 emit privacyChanged( pit->dn(), isBlocked( pit->dn() ) );
239 QStringList PrivacyManager::difference( const QStringList & lhs, const QStringList & rhs )
241 QStringList diff;
242 const QStringList::ConstIterator lhsEnd = lhs.constEnd();
243 const QStringList::ConstIterator rhsEnd = rhs.constEnd();
244 for ( QStringList::ConstIterator lhsIt = lhs.constBegin(); lhsIt != lhsEnd; ++lhsIt )
246 if ( !rhs.contains( *lhsIt ) )
247 diff.append( *lhsIt );
249 return diff;
251 #include "privacymanager.moc"