group.cpp
1 /*
2  This file is part of libqopensync.
3 
4  Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library 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 GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
23 #include <tqdom.h>
24 #include <tqfile.h>
27 #include <opensync/opensync.h>
28 
29 #include "conversion.h"
30 #include "group.h"
31 
32 using namespace QSync;
33 
40 GroupConfig::GroupConfig()
41  : mGroup( 0 )
42 {
43 }
44 
45 TQStringList GroupConfig::activeObjectTypes() const
46 {
47  Q_ASSERT( mGroup );
48 
49  const TQString fileName = TQString( "%1/filter.conf" ).arg( osync_group_get_configdir( mGroup ) );
50 
51  TQFile file( fileName );
52  if ( !file.open( IO_ReadOnly ) )
53  return TQStringList();
54 
55  TQDomDocument document;
56 
57  TQString message;
58  if ( !document.setContent( &file, &message ) ) {
59  tqDebug( "Error on loading %s: %s", fileName.latin1(), message.latin1() );
60  return TQStringList();
61  }
62  file.close();
63 
64  TQStringList objectTypes;
65 
66  TQDomElement element = document.documentElement();
67  TQDomNode node = element.firstChild();
68  while ( !node.isNull() ) {
69  TQDomElement childElement = node.toElement();
70  if ( !childElement.isNull() )
71  objectTypes.append( childElement.tagName() );
72 
73  node = node.nextSibling();
74  }
75 
76  return objectTypes;
77 }
78 
79 void GroupConfig::setActiveObjectTypes( const TQStringList &objectTypes )
80 {
81  Q_ASSERT( mGroup );
82 
83  TQDomDocument document( "Filter" );
84  document.appendChild( document.createProcessingInstruction(
85  "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
86 
87  TQDomElement element = document.createElement( "filter" );
88  document.appendChild( element );
89 
90  for ( uint i = 0; i < objectTypes.count(); ++i ) {
91  TQDomElement entry = document.createElement( objectTypes[ i ] );
92  element.appendChild( entry );
93  }
94 
95  const TQString fileName = TQString( "%1/filter.conf" ).arg( osync_group_get_configdir( mGroup ) );
96 
97  TQFile file( fileName );
98  if ( !file.open( IO_WriteOnly ) )
99  return;
100 
101  TQTextStream s( &file );
102  s.setEncoding( TQTextStream::UnicodeUTF8 );
103  s << document.toString();
104  file.close();
105 }
106 
107 
108 Group::Group()
109  : mGroup( 0 )
110 {
111 }
112 
113 Group::~Group()
114 {
115 }
116 
117 bool Group::isValid() const
118 {
119  return ( mGroup != 0 );
120 }
121 
122 Group::Iterator Group::begin()
123 {
124  Iterator it( this );
125  it.mPos = 0;
126 
127  return it;
128 }
129 
130 Group::Iterator Group::end()
131 {
132  Iterator it( this );
133  it.mPos = memberCount();
134 
135  return it;
136 }
137 
138 void Group::setName( const TQString &name )
139 {
140  Q_ASSERT( mGroup );
141 
142  osync_group_set_name( mGroup, name.latin1() );
143 }
144 
145 TQString Group::name() const
146 {
147  Q_ASSERT( mGroup );
148 
149  return TQString::fromLatin1( osync_group_get_name( mGroup ) );
150 }
151 
152 void Group::setLastSynchronization( const TQDateTime &dateTime )
153 {
154  Q_ASSERT( mGroup );
155 
156  if ( dateTime.isValid() )
157  osync_group_set_last_synchronization( mGroup, dateTime.toTime_t() );
158 }
159 
160 TQDateTime Group::lastSynchronization() const
161 {
162  Q_ASSERT( mGroup );
163 
164  TQDateTime dateTime;
165  time_t time = osync_group_get_last_synchronization( mGroup );
166  if ( time != 0 )
167  dateTime.setTime_t( time );
168 
169  return dateTime;
170 }
171 
172 Group::LockType Group::lock()
173 {
174  Q_ASSERT( mGroup );
175 
176  OSyncLockState state = osync_group_lock( mGroup );
177  switch ( state ) {
178  default:
179  case OSYNC_LOCK_OK:
180  return LockOk;
181  break;
182  case OSYNC_LOCKED:
183  return Locked;
184  break;
185  case OSYNC_LOCK_STALE:
186  return LockStale;
187  break;
188  }
189 }
190 
191 void Group::unlock( bool removeFile )
192 {
193  Q_ASSERT( mGroup );
194 
195  osync_group_unlock( mGroup, removeFile );
196 }
197 
198 Member Group::addMember()
199 {
200  Q_ASSERT( mGroup );
201 
202  OSyncMember *omember = osync_member_new( mGroup );
203 
204  Member member;
205  member.mMember = omember;
206 
207  save();
208 
209  return member;
210 }
211 
212 void Group::removeMember( const Member &member )
213 {
214  Q_ASSERT( mGroup );
215 
216  osync_group_remove_member( mGroup, member.mMember );
217 }
218 
219 int Group::memberCount() const
220 {
221  Q_ASSERT( mGroup );
222 
223  return osync_group_num_members( mGroup );
224 }
225 
226 Member Group::memberAt( int pos ) const
227 {
228  Q_ASSERT( mGroup );
229 
230  Member member;
231 
232  if ( pos < 0 || pos >= memberCount() )
233  return member;
234 
235  member.mMember = osync_group_nth_member( mGroup, pos );
236 
237  return member;
238 }
239 
240 int Group::filterCount() const
241 {
242  Q_ASSERT( mGroup );
243 
244  return osync_group_num_filters( mGroup );
245 }
246 
247 Filter Group::filterAt( int pos )
248 {
249  Q_ASSERT( mGroup );
250 
251  Filter filter;
252 
253  if ( pos < 0 || pos >= filterCount() )
254  return filter;
255 
256  filter.mFilter = osync_group_nth_filter( mGroup, pos );
257 
258  return filter;
259 }
260 
261 Result Group::save()
262 {
263  Q_ASSERT( mGroup );
264 
265  OSyncError *error = 0;
266  if ( !osync_group_save( mGroup, &error ) )
267  return Result( &error );
268  else
269  return Result();
270 }
271 
272 void Group::setObjectTypeEnabled( const TQString &objectType, bool enabled )
273 {
274  Q_ASSERT( mGroup );
275 
276  osync_group_set_objtype_enabled( mGroup, objectType.utf8(), enabled );
277 }
278 
279 bool Group::isObjectTypeEnabled( const TQString &objectType ) const
280 {
281  return osync_group_objtype_enabled( mGroup, objectType.utf8() );
282 }
283 
284 GroupConfig Group::config() const
285 {
286  Q_ASSERT( mGroup );
287 
288  GroupConfig config;
289  config.mGroup = mGroup;
290 
291  return config;
292 }