kaddressbook

xxportselectdialog.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2002 Anders Lund <anders.lund@lund.tdcadsl.dk>
4  Tobias Koenig <tokoe@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  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tdeabc/addressbook.h>
26 #include <tdeapplication.h>
27 #include <kcombobox.h>
28 #include <tdelocale.h>
29 
30 #include <tqbuttongroup.h>
31 #include <tqcombobox.h>
32 #include <tqheader.h>
33 #include <tqlabel.h>
34 #include <tqlayout.h>
35 #include <tqlistview.h>
36 #include <tqpushbutton.h>
37 #include <tqradiobutton.h>
38 #include <tqstringlist.h>
39 #include <tqwhatsthis.h>
40 
41 #include "core.h"
42 #include "kabprefs.h"
43 
44 #include "xxportselectdialog.h"
45 
46 XXPortSelectDialog::XXPortSelectDialog( KAB::Core *core, bool sort,
47  TQWidget* parent, const char* name )
48  : KDialogBase( Plain, i18n( "Choose Which Contacts to Export" ), Help | Ok | Cancel,
49  Ok, parent, name, true, true ), mCore( core ),
50  mUseSorting( sort )
51 {
52  initGUI();
53 
54  connect( mFiltersCombo, TQ_SIGNAL( activated( int ) ),
55  TQ_SLOT( filterChanged( int ) ) );
56  connect( mCategoriesView, TQ_SIGNAL( clicked( TQListViewItem* ) ),
57  TQ_SLOT( categoryClicked( TQListViewItem* ) ) );
58 
59  // setup filters
60  mFilters = Filter::restore( kapp->config(), "Filter" );
61  Filter::List::ConstIterator filterIt;
62  TQStringList filters;
63  for ( filterIt = mFilters.begin(); filterIt != mFilters.end(); ++filterIt )
64  filters.append( (*filterIt).name() );
65 
66  mFiltersCombo->insertStringList( filters );
67  mUseFilters->setEnabled( filters.count() > 0 );
68 
69  // setup categories
70  const TQStringList categories = KABPrefs::instance()->customCategories();
71  TQStringList::ConstIterator it;
72  for ( it = categories.begin(); it != categories.end(); ++it )
73  new TQCheckListItem( mCategoriesView, *it, TQCheckListItem::CheckBox );
74  mUseCategories->setEnabled( categories.count() > 0 );
75 
76  int count = mCore->selectedUIDs().count();
77  mUseSelection->setEnabled( count != 0 );
78  mUseSelection->setChecked( count > 0 );
79 
80  mSortTypeCombo->insertItem( i18n( "Ascending" ) );
81  mSortTypeCombo->insertItem( i18n( "Descending" ) );
82 
83  mFields = mCore->addressBook()->fields( TDEABC::Field::All );
84  TDEABC::Field::List::ConstIterator fieldIt;
85  for ( fieldIt = mFields.begin(); fieldIt != mFields.end(); ++fieldIt )
86  mFieldCombo->insertItem( (*fieldIt)->label() );
87 }
88 
89 TDEABC::AddresseeList XXPortSelectDialog::contacts()
90 {
91  const TQStringList selection = mCore->selectedUIDs();
92 
93  TDEABC::AddresseeList list;
94  if ( mUseSelection->isChecked() ) {
95  TQStringList::ConstIterator it;
96  for ( it = selection.begin(); it != selection.end(); ++it ) {
97  TDEABC::Addressee addr = mCore->addressBook()->findByUid( *it );
98  if ( !addr.isEmpty() )
99  list.append( addr );
100  }
101  } else if ( mUseFilters->isChecked() ) {
102  // find contacts that can pass selected filter
103  Filter::List::ConstIterator filterIt;
104  for ( filterIt = mFilters.begin(); filterIt != mFilters.end(); ++filterIt )
105  if ( (*filterIt).name() == mFiltersCombo->currentText() )
106  break;
107 
108  TDEABC::AddressBook::Iterator it;
109  for ( it = mCore->addressBook()->begin(); it != mCore->addressBook()->end(); ++it ) {
110  if ( (*filterIt).filterAddressee( *it ) )
111  list.append( *it );
112  }
113  } else if ( mUseCategories->isChecked() ) {
114  const TQStringList categorieList = categories();
115 
116  TDEABC::AddressBook::ConstIterator it;
117  TDEABC::AddressBook::ConstIterator addressBookEnd( mCore->addressBook()->end() );
118  for ( it = mCore->addressBook()->begin(); it != addressBookEnd; ++it ) {
119  const TQStringList tmp( (*it).categories() );
120  TQStringList::ConstIterator tmpIt;
121  for ( tmpIt = tmp.begin(); tmpIt != tmp.end(); ++tmpIt )
122  if ( categorieList.contains( *tmpIt ) ) {
123  list.append( *it );
124  break;
125  }
126  }
127  } else {
128  // create a string list of all entries:
129  TDEABC::AddressBook::ConstIterator it;
130  for ( it = mCore->addressBook()->begin(); it != mCore->addressBook()->end(); ++it )
131  list.append( *it );
132  }
133 
134  if ( mUseSorting ) {
135  list.setReverseSorting( mSortTypeCombo->currentItem() == 1 );
136  uint pos = mFieldCombo->currentItem();
137  if ( pos < mFields.count() )
138  list.sortByField( mFields[ pos ] );
139  }
140 
141  return list;
142 }
143 
144 TQStringList XXPortSelectDialog::categories() const
145 {
146  TQStringList list;
147 
148  TQListViewItemIterator it( mCategoriesView );
149  for ( ; it.current(); ++it ) {
150  TQCheckListItem* qcli = static_cast<TQCheckListItem*>(it.current());
151  if ( qcli->isOn() )
152  list.append( it.current()->text( 0 ) );
153  }
154 
155  return list;
156 }
157 
158 void XXPortSelectDialog::filterChanged( int )
159 {
160  mUseFilters->setChecked( true );
161 }
162 
163 void XXPortSelectDialog::categoryClicked( TQListViewItem *i )
164 {
165  if ( !i )
166  return;
167 
168  TQCheckListItem *qcli = static_cast<TQCheckListItem*>( i );
169  if ( qcli->isOn() )
170  mUseCategories->setChecked( true );
171 }
172 
173 void XXPortSelectDialog::slotHelp()
174 {
175  kapp->invokeHelp( "import-and-export" );
176 }
177 
178 void XXPortSelectDialog::initGUI()
179 {
180  TQFrame *page = plainPage();
181 
182  TQVBoxLayout *topLayout = new TQVBoxLayout( page, KDialog::marginHint(),
183  KDialog::spacingHint() );
184 
185  TQLabel *label = new TQLabel( i18n( "Which contacts do you want to export?" ), page );
186  topLayout->addWidget( label );
187 
188  mButtonGroup = new TQButtonGroup( i18n( "Selection" ), page );
189  mButtonGroup->setColumnLayout( 0, TQt::Vertical );
190  mButtonGroup->layout()->setSpacing( KDialog::spacingHint() );
191  mButtonGroup->layout()->setMargin( KDialog::marginHint() );
192 
193  TQGridLayout *groupLayout = new TQGridLayout( mButtonGroup->layout() );
194  groupLayout->setAlignment( TQt::AlignTop );
195 
196  mUseWholeBook = new TQRadioButton( i18n( "&All contacts" ), mButtonGroup );
197  mUseWholeBook->setChecked( true );
198  TQWhatsThis::add( mUseWholeBook, i18n( "Export the entire address book" ) );
199  groupLayout->addWidget( mUseWholeBook, 0, 0 );
200  mUseSelection = new TQRadioButton( i18n("&Selected contact", "&Selected contacts (%n selected)", mCore->selectedUIDs().count() ), mButtonGroup );
201  TQWhatsThis::add( mUseSelection, i18n( "Only export contacts selected in KAddressBook.\n"
202  "This option is disabled if no contacts are selected." ) );
203  groupLayout->addWidget( mUseSelection, 1, 0 );
204 
205  mUseFilters = new TQRadioButton( i18n( "Contacts matching &filter" ), mButtonGroup );
206  TQWhatsThis::add( mUseFilters, i18n( "Only export contacts matching the selected filter.\n"
207  "This option is disabled if you have not defined any filters" ) );
208  groupLayout->addWidget( mUseFilters, 2, 0 );
209 
210  mUseCategories = new TQRadioButton( i18n( "Category &members" ), mButtonGroup );
211  TQWhatsThis::add( mUseCategories, i18n( "Only export contacts who are members of a category that is checked on the list to the left.\n"
212  "This option is disabled if you have no categories." ) );
213  groupLayout->addWidget( mUseCategories, 3, 0, TQt::AlignTop );
214 
215  mFiltersCombo = new TQComboBox( false, mButtonGroup );
216  TQWhatsThis::add( mFiltersCombo, i18n( "Select a filter to decide which contacts to export." ) );
217  groupLayout->addWidget( mFiltersCombo, 2, 1 );
218 
219  mCategoriesView = new TQListView( mButtonGroup );
220  mCategoriesView->addColumn( "" );
221  mCategoriesView->header()->hide();
222  TQWhatsThis::add( mCategoriesView, i18n( "Check the categories whose members you want to export." ) );
223  groupLayout->addWidget( mCategoriesView, 3, 1 );
224 
225  topLayout->addWidget( mButtonGroup );
226 
227  TQButtonGroup *sortingGroup = new TQButtonGroup( i18n( "Sorting" ), page );
228  sortingGroup->setColumnLayout( 0, TQt::Vertical );
229  TQGridLayout *sortLayout = new TQGridLayout( sortingGroup->layout(), 2, 2,
230  KDialog::spacingHint() );
231  sortLayout->setAlignment( TQt::AlignTop );
232 
233  label = new TQLabel( i18n( "Criterion:" ), sortingGroup );
234  sortLayout->addWidget( label, 0, 0 );
235 
236  mFieldCombo = new KComboBox( false, sortingGroup );
237  sortLayout->addWidget( mFieldCombo, 0, 1 );
238 
239  label = new TQLabel( i18n( "Order:" ), sortingGroup );
240  sortLayout->addWidget( label, 1, 0 );
241 
242  mSortTypeCombo = new KComboBox( false, sortingGroup );
243  sortLayout->addWidget( mSortTypeCombo, 1, 1 );
244 
245  topLayout->addWidget( sortingGroup );
246 
247  if ( !mUseSorting )
248  sortingGroup->hide();
249 }
250 
251 #include "xxportselectdialog.moc"
void restore(TDEConfig *config)
Loads the filter from the config file.
Definition: filter.cpp:132