Better wording
[kdepim.git] / kaddressbook / xxportmanager.cpp
blobfb3a3e85d3bca76fa11be2758e0702a41a702275
1 /*
2 This file is part of KAddressBook.
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "xxportmanager.h"
22 #include "contactselectiondialog.h"
24 #include <Akonadi/Collection>
25 #include <Akonadi/CollectionDialog>
26 #include <Akonadi/EntityTreeModel>
27 #include <Akonadi/Item>
28 #include <Akonadi/ItemCreateJob>
30 #include <KLocale>
31 #include <KMessageBox>
32 #include <KProgressDialog>
34 #include <QtCore/QPointer>
35 #include <QtCore/QSignalMapper>
36 #include <QtGui/QAction>
37 #include <QtGui/QItemSelectionModel>
38 #include <QtGui/QWidget>
40 XXPortManager::XXPortManager( QWidget *parent )
41 : QObject( parent ), mSelectionModel( 0 ),
42 mParentWidget( parent ), mImportProgressDialog( 0 )
44 mImportMapper = new QSignalMapper( this );
45 mExportMapper = new QSignalMapper( this );
47 connect( mImportMapper, SIGNAL(mapped(QString)),
48 this, SLOT(slotImport(QString)) );
49 connect( mExportMapper, SIGNAL(mapped(QString)),
50 this, SLOT(slotExport(QString)) );
53 XXPortManager::~XXPortManager()
57 void XXPortManager::addImportAction( QAction *action, const QString &identifier )
59 mImportMapper->setMapping( action, identifier );
60 connect( action, SIGNAL(triggered(bool)), mImportMapper, SLOT(map()) );
63 void XXPortManager::addExportAction( QAction *action, const QString &identifier )
65 mExportMapper->setMapping( action, identifier );
66 connect( action, SIGNAL(triggered(bool)), mExportMapper, SLOT(map()) );
69 void XXPortManager::setSelectionModel( QItemSelectionModel *selectionModel )
71 mSelectionModel = selectionModel;
74 void XXPortManager::setDefaultAddressBook( const Akonadi::Collection &addressBook )
76 mDefaultAddressBook = addressBook;
79 void XXPortManager::slotImport( const QString &identifier )
81 const XXPort *xxport = mFactory.createXXPort( identifier, mParentWidget );
82 if( !xxport ) {
83 return;
86 const KABC::Addressee::List contacts = xxport->importContacts();
88 delete xxport;
90 if ( contacts.isEmpty() ) { // nothing to import
91 return;
94 const QStringList mimeTypes( KABC::Addressee::mimeType() );
96 QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog( mParentWidget );
97 dlg->setMimeTypeFilter( mimeTypes );
98 dlg->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
99 dlg->setCaption( i18n( "Select Address Book" ) );
100 dlg->setDescription(
101 i18n( "Select the address book the imported contact(s) shall be saved in:" ) );
102 dlg->setDefaultCollection( mDefaultAddressBook );
104 if ( !dlg->exec() || !dlg ) {
105 delete dlg;
106 return;
109 const Akonadi::Collection collection = dlg->selectedCollection();
110 delete dlg;
112 if ( !mImportProgressDialog ) {
113 mImportProgressDialog = new KProgressDialog( mParentWidget, i18n( "Import Contacts" ) );
114 mImportProgressDialog->setLabelText(
115 i18np( "Importing one contact to %2", "Importing %1 contacts to %2",
116 contacts.count(), collection.name() ) );
117 mImportProgressDialog->setAllowCancel( false );
118 mImportProgressDialog->setAutoClose( true );
119 mImportProgressDialog->progressBar()->setRange( 1, contacts.count() );
122 mImportProgressDialog->show();
124 for ( int i = 0; i < contacts.count(); ++i ) {
125 Akonadi::Item item;
126 item.setPayload<KABC::Addressee>( contacts.at( i ) );
127 item.setMimeType( KABC::Addressee::mimeType() );
129 Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, collection );
130 connect( job, SIGNAL(result(KJob*)), SLOT(slotImportJobDone(KJob*)) );
134 void XXPortManager::slotImportJobDone( KJob * )
136 if ( !mImportProgressDialog ) {
137 return;
140 QProgressBar *progressBar = mImportProgressDialog->progressBar();
142 progressBar->setValue( progressBar->value() + 1 );
144 // cleanup on last step
145 if ( progressBar->value() == progressBar->maximum() ) {
146 mImportProgressDialog->deleteLater();
147 mImportProgressDialog = 0;
151 void XXPortManager::slotExport( const QString &identifier )
153 if ( !mSelectionModel ) {
154 return;
157 QPointer<ContactSelectionDialog> dlg =
158 new ContactSelectionDialog( mSelectionModel, mParentWidget );
159 dlg->setMessageText( i18n( "Which contact do you want to export?" ) );
160 dlg->setDefaultAddressBook( mDefaultAddressBook );
161 if ( !dlg->exec() || !dlg ) {
162 delete dlg;
163 return;
166 const KABC::AddresseeList contacts = dlg->selectedContacts();
167 delete dlg;
169 if ( contacts.isEmpty() ) {
170 KMessageBox::sorry( 0, i18n( "You have not selected any contacts to export." ) );
171 return;
174 const XXPort *xxport = mFactory.createXXPort( identifier, mParentWidget );
175 if ( !xxport ) {
176 return;
179 xxport->exportContacts( contacts );
181 delete xxport;
184 #include "xxportmanager.moc"