kitchensync

groupconfig.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, USA.
19 */
20 
21 #include "groupconfig.h"
22 
23 #include "groupconfigcommon.h"
24 #include "memberconfig.h"
25 #include "memberinfo.h"
26 #include "pluginpicker.h"
27 #include "syncprocess.h"
28 #include "syncprocessmanager.h"
29 
30 #include <libqopensync/group.h>
31 #include <libqopensync/plugin.h>
32 
33 #include <kdialog.h>
34 #include <kiconloader.h>
35 #include <kjanuswidget.h>
36 #include <tdelocale.h>
37 #include <tdemessagebox.h>
38 
39 
40 #include <tqlabel.h>
41 #include <tqlayout.h>
42 #include <tqpushbutton.h>
43 
44 GroupConfig::GroupConfig( TQWidget *parent )
45  : TQWidget( parent )
46 {
47  TQBoxLayout *topLayout = new TQVBoxLayout( this );
48  topLayout->setSpacing( KDialog::spacingHint() );
49 
50  TQFrame *titleFrame = new TQFrame( this );
51  topLayout->addWidget( titleFrame );
52 
53  titleFrame->setPaletteForegroundColor( colorGroup().light() );
54  titleFrame->setPaletteBackgroundColor( colorGroup().mid() );
55 
56  TQBoxLayout *nameLayout = new TQHBoxLayout( titleFrame );
57  nameLayout->setMargin( 4 );
58 
59  TQPixmap icon = TDEGlobal::iconLoader()->loadIcon( "kontact_summary",
60  TDEIcon::Desktop );
61 
62  TQLabel *iconLabel = new TQLabel( titleFrame );
63  iconLabel->setPixmap( icon );
64  nameLayout->addWidget( iconLabel );
65 
66  nameLayout->addSpacing( 8 );
67 
68  TQLabel *label = new TQLabel( i18n("Group:"), titleFrame );
69  TQFont font = label->font();
70  font.setBold( true );
71  font.setPointSize( font.pointSize() + 2 );
72  label->setFont( font );
73  nameLayout->addWidget( label );
74 
75  mNameLabel = new TQLabel( titleFrame );
76  font = mNameLabel->font();
77  font.setBold( true );
78  font.setPointSize( font.pointSize() + 2 );
79  mNameLabel->setFont( font );
80  nameLayout->addWidget( mNameLabel );
81 
82  nameLayout->addStretch( 1 );
83 
84  mMemberView = new KJanusWidget( this, 0, KJanusWidget::IconList );
85  topLayout->addWidget( mMemberView );
86 
87  TQBoxLayout *buttonLayout = new TQHBoxLayout( topLayout );
88 
89  TQPushButton *addButton = new TQPushButton( i18n("Add Member..."), this );
90  connect( addButton, TQT_SIGNAL( clicked() ), TQT_SLOT( addMember() ) );
91  buttonLayout->addWidget( addButton );
92 
93  buttonLayout->addStretch( 1 );
94 
95  icon = TDEGlobal::iconLoader()->loadIcon( "bookmark", TDEIcon::Desktop );
96  TQFrame *page = mMemberView->addPage( i18n("Group"),
97  i18n("General Group Settings"), icon );
98  TQBoxLayout *pageLayout = new TQVBoxLayout( page );
99 
100  mCommonConfig = new GroupConfigCommon( page );
101  pageLayout->addWidget( mCommonConfig );
102 }
103 
104 void GroupConfig::setSyncProcess( SyncProcess *process )
105 {
106  mProcess = process;
107 
108  mNameLabel->setText( mProcess->group().name() );
109  mCommonConfig->setSyncProcess( mProcess );
110 
111  updateMembers();
112 }
113 
114 void GroupConfig::updateMembers()
115 {
116  TQValueList<MemberConfig *>::ConstIterator memberIt;
117  for ( memberIt = mMemberConfigs.begin(); memberIt != mMemberConfigs.end(); ++memberIt )
118  (*memberIt)->saveData();
119 
120  TQValueList<TQFrame *>::ConstIterator it2;
121  for ( it2 = mConfigPages.begin(); it2 != mConfigPages.end(); ++it2 ) {
122  mMemberView->removePage( *it2 );
123  delete *it2;
124  }
125  mConfigPages.clear();
126  mMemberConfigs.clear();
127 
128  QSync::Group group = mProcess->group();
129  QSync::Group::Iterator it( group.begin() );
130  for ( ; it != group.end(); ++it ) {
131  QSync::Member member = *it;
132  MemberInfo mi( member );
133  TQFrame *page = mMemberView->addPage( mi.name(),
134  TQString( "%1 (%2)" ).arg( mi.name() ).arg(member.pluginName()), mi.desktopIcon() );
135 
136  TQBoxLayout *pageLayout = new TQVBoxLayout( page );
137  mConfigPages.append( page );
138 
139  MemberConfig *memberConfig = new MemberConfig( page, member );
140  mMemberConfigs.append( memberConfig );
141  pageLayout->addWidget( memberConfig );
142 
143  memberConfig->loadData();
144  }
145 }
146 
147 void GroupConfig::saveConfig()
148 {
149  mProcess->group().save();
150 
151  TQValueList<MemberConfig *>::ConstIterator it;
152  for ( it = mMemberConfigs.begin(); it != mMemberConfigs.end(); ++it )
153  (*it)->saveData();
154 
155  mCommonConfig->save();
156 
157  mProcess->reinitEngine();
158 }
159 
160 void GroupConfig::addMember()
161 {
162  QSync::Plugin plugin = PluginPickerDialog::getPlugin( this );
163 
164  if ( plugin.isValid() ) {
165  QSync::Result result = SyncProcessManager::self()->addMember( mProcess, plugin );
166  if ( result.isError() ) {
167  KMessageBox::error( this, i18n("Error adding member %1\n%2\nType: %3")
168  .arg( plugin.name() ).arg( result.message() ).arg( result.type() ) );
169  } else {
170  updateMembers();
171 
172  // select last (added) page
173  int index = mMemberView->pageIndex( mConfigPages.last() );
174  mMemberView->showPage( index );
175  }
176  }
177 }
178 
179 #include "groupconfig.moc"