kitchensync

groupconfigcommon.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 
23 #include <kdialog.h>
24 #include <klineedit.h>
25 #include <tdelocale.h>
26 #include <kdebug.h>
27 
28 #include <tqlabel.h>
29 #include <tqlayout.h>
30 #include <tqcheckbox.h>
31 
32 #include <libqopensync/group.h>
33 #include <libqopensync/conversion.h>
34 #include <libqopensync/environment.h>
35 
36 #include "syncprocess.h"
37 #include "syncprocessmanager.h"
38 
39 #include "groupconfigcommon.h"
40 
41 ObjectTypeSelector::ObjectTypeSelector( TQWidget *parent )
42  : TQWidget( parent )
43 {
44  TQGridLayout *layout = new TQGridLayout( this );
45  layout->setMargin( 0 );
46 
47  const QSync::Conversion conversion = SyncProcessManager::self()->environment()->conversion();
48 
49  TQMap<TQString, TQString> objectTypeMap;
50  objectTypeMap.insert( "contact", i18n( "Contacts" ) );
51  objectTypeMap.insert( "event", i18n( "Events" ) );
52  objectTypeMap.insert( "todo", i18n( "To-dos" ) );
53  objectTypeMap.insert( "note", i18n( "Notes" ) );
54 
55  TQStringList objectTypes = conversion.objectTypes();
56 
57  // reorder the entries so that contact and event are the first one
58  qHeapSort( objectTypes );
59 
60  TQStringList reoderedObjectTypes, stack;
61  for ( uint i = 0; i < objectTypes.count(); ++i ) {
62  if ( objectTypes[ i ] == "contact" || objectTypes[ i ] == "event" )
63  reoderedObjectTypes.append( objectTypes[ i ] );
64  else
65  stack.append( objectTypes[ i ] );
66  }
67  reoderedObjectTypes += stack;
68 
69  TQStringList::ConstIterator it;
70 
71  int row = 0;
72  int col = 0;
73  for( it = reoderedObjectTypes.begin(); it != reoderedObjectTypes.end(); ++it ) {
74  TQString objectType = *it;
75 
76  // Don't display object type "data". Object type "data" is a kind of wildcard - so don't filter *
77  if ( objectType == "data" )
78  continue;
79 
80  TQCheckBox *objectCheckBox = new TQCheckBox( objectTypeMap[ objectType ], this );
81  layout->addWidget( objectCheckBox, row, col );
82  mObjectTypeChecks.insert( objectType, objectCheckBox );
83 
84  col++;
85  if ( (row == 0 && col == 2) || col == 3 ) {
86  col = 0;
87  row++;
88  }
89  }
90 }
91 
92 void ObjectTypeSelector::load( const QSync::Group &group )
93 {
94  const QSync::GroupConfig config = group.config();
95 
96  const TQStringList objectTypes = config.activeObjectTypes();
97 
98  // Enable everything on the inital load
99  bool initialLoad = false;
100  if ( objectTypes.isEmpty() )
101  initialLoad = true;
102 
103  TQMap<TQString, TQCheckBox*>::ConstIterator it;
104  for( it = mObjectTypeChecks.begin(); it != mObjectTypeChecks.end(); ++it ) {
105  TQCheckBox *check = it.data();
106  check->setChecked( objectTypes.contains( it.key() ) || initialLoad );
107  }
108 }
109 
110 void ObjectTypeSelector::save( QSync::Group group )
111 {
112  TQStringList objectTypes;
113 
114  TQMap<TQString,TQCheckBox *>::ConstIterator it;
115  for( it = mObjectTypeChecks.begin(); it != mObjectTypeChecks.end(); ++it ) {
116  TQCheckBox *check = it.data();
117  if ( check->isChecked() )
118  objectTypes.append( it.key() );
119  }
120 
121  // Always add object type "data"
122  objectTypes.append( "data" );
123 
124  QSync::GroupConfig config = group.config();
125  config.setActiveObjectTypes( objectTypes );
126 }
127 
128 GroupConfigCommon::GroupConfigCommon( TQWidget *parent )
129  : TQWidget( parent )
130 {
131  TQGridLayout *layout = new TQGridLayout( this, 2, 2, KDialog::marginHint(), KDialog::spacingHint() );
132 
133  layout->addWidget( new TQLabel( i18n( "Name:" ), this ), 0, 0 );
134 
135  mGroupName = new KLineEdit( this );
136  layout->addWidget( mGroupName, 0, 1 );
137 
138  layout->addWidget( new TQLabel( i18n( "Object Types to be Synchronized:"), this ), 1, 0, TQt::AlignTop );
139 
140  mObjectTypeSelector = new ObjectTypeSelector( this );
141  layout->addWidget( mObjectTypeSelector, 1, 1 );
142 
143  layout->setRowStretch( 2, 1 );
144 }
145 
146 void GroupConfigCommon::setSyncProcess( SyncProcess *syncProcess )
147 {
148  mSyncProcess = syncProcess;
149 
150  mGroupName->setText( mSyncProcess->group().name() );
151  mObjectTypeSelector->load( mSyncProcess->group() );
152 }
153 
154 void GroupConfigCommon::save()
155 {
156  mSyncProcess->group().setName( mGroupName->text() );
157  mObjectTypeSelector->save( mSyncProcess->group() );
158 }