syncprocessmanager.cpp
00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org> 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program 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 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00019 USA. 00020 */ 00021 00022 #include "syncprocessmanager.h" 00023 00024 #include "syncprocess.h" 00025 00026 #include <libqopensync/environment.h> 00027 00028 #include <kstaticdeleter.h> 00029 #include <kmessagebox.h> 00030 #include <klocale.h> 00031 00032 static KStaticDeleter<SyncProcessManager> selfDeleter; 00033 00034 SyncProcessManager *SyncProcessManager::mSelf = 0; 00035 00036 SyncProcessManager *SyncProcessManager::self() 00037 { 00038 if ( !mSelf ) { 00039 selfDeleter.setObject( mSelf, new SyncProcessManager() ); 00040 } 00041 return mSelf; 00042 } 00043 00044 SyncProcessManager::SyncProcessManager() 00045 { 00046 mEnvironment = new QSync::Environment; 00047 QSync::Result result = mEnvironment->initialize(); 00048 if ( result.isError() ) { 00049 KMessageBox::error( 0, i18n("Error initializing OpenSync.\n%1") 00050 .arg( result.message() ) ); 00051 } else { 00052 init( mEnvironment ); 00053 } 00054 } 00055 00056 SyncProcessManager::~SyncProcessManager() 00057 { 00058 TQValueList<SyncProcess*>::Iterator it; 00059 for ( it = mProcesses.begin(); it != mProcesses.end(); ++it ) 00060 delete *it; 00061 00062 mProcesses.clear(); 00063 00064 mEnvironment->finalize(); 00065 delete mEnvironment; 00066 } 00067 00068 int SyncProcessManager::count() const 00069 { 00070 return mProcesses.count(); 00071 } 00072 00073 SyncProcess* SyncProcessManager::at( int pos ) const 00074 { 00075 if ( pos < 0 || pos >= (int)mProcesses.count() ) 00076 return 0; 00077 00078 return mProcesses[ pos ]; 00079 } 00080 00081 SyncProcess* SyncProcessManager::byGroup( const QSync::Group &group ) 00082 { 00083 TQValueList<SyncProcess*>::Iterator it; 00084 for ( it = mProcesses.begin(); it != mProcesses.end(); ++it ) 00085 if ( (*it)->group() == group ) 00086 return *it; 00087 00088 return 0; 00089 } 00090 00091 SyncProcess* SyncProcessManager::byGroupName( const TQString &name ) 00092 { 00093 TQValueList<SyncProcess*>::Iterator it; 00094 for ( it = mProcesses.begin(); it != mProcesses.end(); ++it ) 00095 if ( (*it)->group().name() == name ) 00096 return *it; 00097 00098 return 0; 00099 } 00100 00101 void SyncProcessManager::addGroup( const TQString &name ) 00102 { 00103 SyncProcess* process = byGroupName( name ); 00104 if ( !process ) { 00105 QSync::Group group = mEnvironment->addGroup(); 00106 group.setName( name ); 00107 group.save(); 00108 00109 mProcesses.append( new SyncProcess( group ) ); 00110 00111 emit changed(); 00112 } else 00113 qDebug( "Try to add duplicate" ); 00114 } 00115 00116 void SyncProcessManager::remove( SyncProcess *syncProcess ) 00117 { 00118 if ( syncProcess ) { 00119 mProcesses.remove( syncProcess ); 00120 const QSync::Group group = syncProcess->group(); 00121 delete syncProcess; 00122 00123 mEnvironment->removeGroup( group ); 00124 00125 emit changed(); 00126 } 00127 } 00128 00129 void SyncProcessManager::init( QSync::Environment *environment ) 00130 { 00131 QSync::Environment::GroupIterator it( environment->groupBegin() ); 00132 for ( ; it != environment->groupEnd(); ++it ) { 00138 const QSync::Group group = *it; 00139 int count = group.memberCount(); 00140 00141 bool isValid = true; 00142 for ( int i = 0; i < count; ++i ) { 00143 const QSync::Member member = group.memberAt( i ); 00144 00145 if ( !member.isValid() ) { 00146 isValid = false; 00147 break; 00148 } 00149 } 00150 00151 if ( isValid ) 00152 mProcesses.append( new SyncProcess( *it ) ); 00153 } 00154 00155 emit changed(); 00156 } 00157 00158 QSync::Result SyncProcessManager::addMember( SyncProcess *process, 00159 const QSync::Plugin &plugin ) 00160 { 00161 Q_ASSERT( process ); 00162 00163 QSync::Result result = process->addMember( plugin ); 00164 if ( !result.isError() ) { 00165 process->group().save(); 00166 emit syncProcessChanged( process ); 00167 } 00168 00169 return result; 00170 } 00171 00172 #include "syncprocessmanager.moc"