kitchensync

syncprocessmanager.cpp
1 /*
2  This file is part of KitchenSync.
3 
4  Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program 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
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19  USA.
20 */
21 
22 #include "syncprocessmanager.h"
23 
24 #include "syncprocess.h"
25 
26 #include <libqopensync/environment.h>
27 
28 #include <kstaticdeleter.h>
29 #include <tdemessagebox.h>
30 #include <tdelocale.h>
31 
32 static KStaticDeleter<SyncProcessManager> selfDeleter;
33 
34 SyncProcessManager *SyncProcessManager::mSelf = 0;
35 
36 SyncProcessManager *SyncProcessManager::self()
37 {
38  if ( !mSelf ) {
39  selfDeleter.setObject( mSelf, new SyncProcessManager() );
40  }
41  return mSelf;
42 }
43 
44 SyncProcessManager::SyncProcessManager()
45 {
46  mEnvironment = new QSync::Environment;
47  QSync::Result result = mEnvironment->initialize();
48  if ( result.isError() ) {
49  KMessageBox::error( 0, i18n("Error initializing OpenSync.\n%1")
50  .arg( result.message() ) );
51  } else {
52  init( mEnvironment );
53  }
54 }
55 
56 SyncProcessManager::~SyncProcessManager()
57 {
58  TQValueList<SyncProcess*>::Iterator it;
59  for ( it = mProcesses.begin(); it != mProcesses.end(); ++it )
60  delete *it;
61 
62  mProcesses.clear();
63 
64  mEnvironment->finalize();
65  delete mEnvironment;
66 }
67 
68 int SyncProcessManager::count() const
69 {
70  return mProcesses.count();
71 }
72 
73 SyncProcess* SyncProcessManager::at( int pos ) const
74 {
75  if ( pos < 0 || pos >= (int)mProcesses.count() )
76  return 0;
77 
78  return mProcesses[ pos ];
79 }
80 
81 SyncProcess* SyncProcessManager::byGroup( const QSync::Group &group )
82 {
83  TQValueList<SyncProcess*>::Iterator it;
84  for ( it = mProcesses.begin(); it != mProcesses.end(); ++it )
85  if ( (*it)->group() == group )
86  return *it;
87 
88  return 0;
89 }
90 
91 SyncProcess* SyncProcessManager::byGroupName( const TQString &name )
92 {
93  TQValueList<SyncProcess*>::Iterator it;
94  for ( it = mProcesses.begin(); it != mProcesses.end(); ++it )
95  if ( (*it)->group().name() == name )
96  return *it;
97 
98  return 0;
99 }
100 
101 void SyncProcessManager::addGroup( const TQString &name )
102 {
103  SyncProcess* process = byGroupName( name );
104  if ( !process ) {
105  QSync::Group group = mEnvironment->addGroup();
106  group.setName( name );
107  group.save();
108 
109  mProcesses.append( new SyncProcess( group ) );
110 
111  emit changed();
112  } else
113  tqDebug( "Try to add duplicate" );
114 }
115 
116 void SyncProcessManager::remove( SyncProcess *syncProcess )
117 {
118  if ( syncProcess ) {
119  mProcesses.remove( syncProcess );
120  const QSync::Group group = syncProcess->group();
121  delete syncProcess;
122 
123  mEnvironment->removeGroup( group );
124 
125  emit changed();
126  }
127 }
128 
129 void SyncProcessManager::init( QSync::Environment *environment )
130 {
131  QSync::Environment::GroupIterator it( environment->groupBegin() );
132  for ( ; it != environment->groupEnd(); ++it ) {
138  const QSync::Group group = *it;
139  int count = group.memberCount();
140 
141  bool isValid = true;
142  for ( int i = 0; i < count; ++i ) {
143  const QSync::Member member = group.memberAt( i );
144 
145  if ( !member.isValid() ) {
146  isValid = false;
147  break;
148  }
149  }
150 
151  if ( isValid )
152  mProcesses.append( new SyncProcess( *it ) );
153  }
154 
155  emit changed();
156 }
157 
158 QSync::Result SyncProcessManager::addMember( SyncProcess *process,
159  const QSync::Plugin &plugin )
160 {
161  Q_ASSERT( process );
162 
163  QSync::Result result = process->addMember( plugin );
164  if ( !result.isError() ) {
165  process->group().save();
166  emit syncProcessChanged( process );
167  }
168 
169  return result;
170 }
171 
172 #include "syncprocessmanager.moc"