kitchensync

group.cpp

00001 /*
00002     This file is part of libqopensync.
00003 
00004     Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00023 #include <qdom.h>
00024 #include <qfile.h>
00027 #include <opensync/opensync.h>
00028 
00029 #include "conversion.h"
00030 #include "group.h"
00031 
00032 using namespace QSync;
00033 
00040 GroupConfig::GroupConfig()
00041   : mGroup( 0 )
00042 {
00043 }
00044 
00045 QStringList GroupConfig::activeObjectTypes() const
00046 {
00047   Q_ASSERT( mGroup );
00048 
00049   const QString fileName = QString( "%1/filter.conf" ).arg( osync_group_get_configdir( mGroup ) );
00050 
00051   QFile file( fileName );
00052   if ( !file.open( IO_ReadOnly ) )
00053     return QStringList();
00054 
00055   QDomDocument document;
00056 
00057   QString message;
00058   if ( !document.setContent( &file, &message ) ) {
00059     qDebug( "Error on loading %s: %s", fileName.latin1(), message.latin1() );
00060     return QStringList();
00061   }
00062   file.close();
00063 
00064   QStringList objectTypes;
00065 
00066   QDomElement element = document.documentElement();
00067   QDomNode node = element.firstChild();
00068   while ( !node.isNull() ) {
00069     QDomElement childElement = node.toElement();
00070     if ( !childElement.isNull() )
00071       objectTypes.append( childElement.tagName() );
00072 
00073     node = node.nextSibling();
00074   }
00075 
00076   return objectTypes;
00077 }
00078 
00079 void GroupConfig::setActiveObjectTypes( const QStringList &objectTypes )
00080 {
00081   Q_ASSERT( mGroup );
00082 
00083   QDomDocument document( "Filter" );
00084   document.appendChild( document.createProcessingInstruction(
00085                         "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00086 
00087   QDomElement element = document.createElement( "filter" );
00088   document.appendChild( element );
00089 
00090   for ( uint i = 0; i < objectTypes.count(); ++i ) {
00091     QDomElement entry = document.createElement( objectTypes[ i ] );
00092     element.appendChild( entry );
00093   }
00094 
00095   const QString fileName = QString( "%1/filter.conf" ).arg( osync_group_get_configdir( mGroup ) );
00096 
00097   QFile file( fileName );
00098   if ( !file.open( IO_WriteOnly ) )
00099     return;
00100 
00101   QTextStream s( &file );
00102   s.setEncoding( QTextStream::UnicodeUTF8 );
00103   s << document.toString();
00104   file.close();
00105 }
00106 
00107 
00108 Group::Group()
00109   : mGroup( 0 )
00110 {
00111 }
00112 
00113 Group::~Group()
00114 {
00115 }
00116 
00117 bool Group::isValid() const
00118 {
00119   return ( mGroup != 0 );
00120 }
00121 
00122 Group::Iterator Group::begin()
00123 {
00124   Iterator it( this );
00125   it.mPos = 0;
00126 
00127   return it;
00128 }
00129 
00130 Group::Iterator Group::end()
00131 {
00132   Iterator it( this );
00133   it.mPos = memberCount();
00134 
00135   return it;
00136 }
00137 
00138 void Group::setName( const QString &name )
00139 {
00140   Q_ASSERT( mGroup );
00141 
00142   osync_group_set_name( mGroup, name.latin1() );
00143 }
00144 
00145 QString Group::name() const
00146 {
00147   Q_ASSERT( mGroup );
00148 
00149   return QString::fromLatin1( osync_group_get_name( mGroup ) );
00150 }
00151 
00152 void Group::setLastSynchronization( const QDateTime &dateTime )
00153 {
00154   Q_ASSERT( mGroup );
00155 
00156   if ( dateTime.isValid() )
00157     osync_group_set_last_synchronization( mGroup, dateTime.toTime_t() );
00158 }
00159 
00160 QDateTime Group::lastSynchronization() const
00161 {
00162   Q_ASSERT( mGroup );
00163 
00164   QDateTime dateTime;
00165   time_t time = osync_group_get_last_synchronization( mGroup );
00166   if ( time != 0 )
00167     dateTime.setTime_t( time );
00168 
00169   return dateTime;
00170 }
00171 
00172 Group::LockType Group::lock()
00173 {
00174   Q_ASSERT( mGroup );
00175 
00176   OSyncLockState state = osync_group_lock( mGroup );
00177   switch ( state ) {
00178     default:
00179     case OSYNC_LOCK_OK:
00180       return LockOk;
00181       break;
00182     case OSYNC_LOCKED:
00183       return Locked;
00184       break;
00185     case OSYNC_LOCK_STALE:
00186       return LockStale;
00187       break;
00188   }
00189 }
00190 
00191 void Group::unlock( bool removeFile )
00192 {
00193   Q_ASSERT( mGroup );
00194 
00195   osync_group_unlock( mGroup, removeFile );
00196 }
00197 
00198 Member Group::addMember()
00199 {
00200   Q_ASSERT( mGroup );
00201 
00202   OSyncMember *omember = osync_member_new( mGroup );
00203 
00204   Member member;
00205   member.mMember = omember;
00206 
00207   save();
00208 
00209   return member;
00210 }
00211 
00212 void Group::removeMember( const Member &member )
00213 {
00214   Q_ASSERT( mGroup );
00215 
00216   osync_group_remove_member( mGroup, member.mMember );
00217 }
00218 
00219 int Group::memberCount() const
00220 {
00221   Q_ASSERT( mGroup );
00222 
00223   return osync_group_num_members( mGroup );
00224 }
00225 
00226 Member Group::memberAt( int pos ) const
00227 {
00228   Q_ASSERT( mGroup );
00229 
00230   Member member;
00231 
00232   if ( pos < 0 || pos >= memberCount() )
00233     return member;
00234 
00235   member.mMember = osync_group_nth_member( mGroup, pos );
00236 
00237   return member;
00238 }
00239 
00240 int Group::filterCount() const
00241 {
00242   Q_ASSERT( mGroup );
00243 
00244   return osync_group_num_filters( mGroup );
00245 }
00246 
00247 Filter Group::filterAt( int pos )
00248 {
00249   Q_ASSERT( mGroup );
00250 
00251   Filter filter;
00252 
00253   if ( pos < 0 || pos >= filterCount() )
00254     return filter;
00255 
00256   filter.mFilter = osync_group_nth_filter( mGroup, pos );
00257 
00258   return filter;
00259 }
00260 
00261 Result Group::save()
00262 {
00263   Q_ASSERT( mGroup );
00264 
00265   OSyncError *error = 0;
00266   if ( !osync_group_save( mGroup, &error ) )
00267     return Result( &error );
00268   else
00269     return Result();
00270 }
00271 
00272 void Group::setObjectTypeEnabled( const QString &objectType, bool enabled )
00273 {
00274   Q_ASSERT( mGroup );
00275 
00276   osync_group_set_objtype_enabled( mGroup, objectType.utf8(), enabled );
00277 }
00278 
00279 bool Group::isObjectTypeEnabled( const QString &objectType ) const
00280 {
00281   return osync_group_objtype_enabled( mGroup, objectType.utf8() );
00282 }
00283 
00284 GroupConfig Group::config() const
00285 {
00286   Q_ASSERT( mGroup );
00287 
00288   GroupConfig config;
00289   config.mGroup = mGroup;
00290 
00291   return config;
00292 }