• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.14
  • kdepim
  • kaddressbook
  • xxport
xxportmanager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3 
4  Copyright (c) 2009 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 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.
19 */
20 
21 #include "xxportmanager.h"
22 #include "contactselectiondialog.h"
23 
24 #include <Akonadi/Collection>
25 #include <Akonadi/CollectionDialog>
26 #include <Akonadi/EntityTreeModel>
27 #include <Akonadi/Item>
28 #include <Akonadi/ItemCreateJob>
29 
30 #include <KLocalizedString>
31 #include <KMessageBox>
32 #include <KProgressDialog>
33 
34 #include <QtCore/QPointer>
35 #include <QtCore/QSignalMapper>
36 #include <QAction>
37 #include <QItemSelectionModel>
38 #include <QWidget>
39 
40 XXPortManager::XXPortManager( QWidget *parent )
41  : QObject( parent ), mSelectionModel( 0 ),
42  mParentWidget( parent ), mImportProgressDialog( 0 )
43 {
44  mImportMapper = new QSignalMapper( this );
45  mExportMapper = new QSignalMapper( this );
46 
47  connect( mImportMapper, SIGNAL(mapped(QString)),
48  this, SLOT(slotImport(QString)) );
49  connect( mExportMapper, SIGNAL(mapped(QString)),
50  this, SLOT(slotExport(QString)) );
51 }
52 
53 XXPortManager::~XXPortManager()
54 {
55 }
56 
57 void XXPortManager::addImportAction( QAction *action, const QString &identifier )
58 {
59  mImportMapper->setMapping( action, identifier );
60  connect( action, SIGNAL(triggered(bool)), mImportMapper, SLOT(map()) );
61 }
62 
63 void XXPortManager::addExportAction( QAction *action, const QString &identifier )
64 {
65  mExportMapper->setMapping( action, identifier );
66  connect( action, SIGNAL(triggered(bool)), mExportMapper, SLOT(map()) );
67 }
68 
69 void XXPortManager::setSelectionModel( QItemSelectionModel *selectionModel )
70 {
71  mSelectionModel = selectionModel;
72 }
73 
74 void XXPortManager::setDefaultAddressBook( const Akonadi::Collection &addressBook )
75 {
76  mDefaultAddressBook = addressBook;
77 }
78 
79 void XXPortManager::importFile( const KUrl &url)
80 {
81  QString identifier;
82  if (url.path().endsWith(QLatin1String("vcf"))) {
83  identifier = QLatin1String("vcard30");
84  } else if (url.path().endsWith(QLatin1String("ldif"))) {
85  identifier = QLatin1String("ldif");
86  } else if (url.path().endsWith(QLatin1String("gmx"))) {
87  identifier = QLatin1String("gmx");
88  }
89  if (identifier.isEmpty())
90  return;
91  XXPort *xxport = mFactory.createXXPort( identifier, mParentWidget );
92  if( !xxport ) {
93  return;
94  }
95  xxport->setOption(QLatin1String("importUrl"), url.path());
96  ContactList contactList = xxport->importContacts();
97 
98  delete xxport;
99  import(contactList);
100 }
101 
102 void XXPortManager::slotImport( const QString &identifier )
103 {
104  const XXPort *xxport = mFactory.createXXPort( identifier, mParentWidget );
105  if( !xxport ) {
106  return;
107  }
108  ContactList contactList = xxport->importContacts();
109 
110  delete xxport;
111  import(contactList);
112 }
113 
114 void XXPortManager::import(const ContactList &contacts)
115 {
116  if ( contacts.isEmpty() ) { // nothing to import
117  return;
118  }
119 
120  const QStringList mimeTypes( KABC::Addressee::mimeType() );
121 
122  QPointer<Akonadi::CollectionDialog> dlg = new Akonadi::CollectionDialog( mParentWidget );
123  dlg->setMimeTypeFilter( mimeTypes );
124  dlg->setAccessRightsFilter( Akonadi::Collection::CanCreateItem );
125  dlg->setCaption( i18n( "Select Address Book" ) );
126  dlg->setDescription(
127  i18n( "Select the address book the imported contact(s) shall be saved in:" ) );
128  dlg->setDefaultCollection( mDefaultAddressBook );
129 
130  if ( !dlg->exec() || !dlg ) {
131  delete dlg;
132  return;
133  }
134 
135  const Akonadi::Collection collection = dlg->selectedCollection();
136  delete dlg;
137 
138  if ( !mImportProgressDialog ) {
139  mImportProgressDialog = new KProgressDialog( mParentWidget, i18n( "Import Contacts" ) );
140  mImportProgressDialog->setLabelText(
141  i18np( "Importing one contact to %2", "Importing %1 contacts to %2",
142  contacts.count(), collection.name() ) );
143  mImportProgressDialog->setAllowCancel( false );
144  mImportProgressDialog->setAutoClose( true );
145  mImportProgressDialog->progressBar()->setRange( 1, contacts.count() );
146  }
147 
148  mImportProgressDialog->show();
149 
150  for ( int i = 0; i < contacts.addressList().count(); ++i ) {
151  Akonadi::Item item;
152  item.setPayload<KABC::Addressee>( contacts.addressList().at( i ) );
153  item.setMimeType( KABC::Addressee::mimeType() );
154 
155  Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob( item, collection );
156  connect( job, SIGNAL(result(KJob*)), SLOT(slotImportJobDone(KJob*)) );
157  }
158  for (int i = 0; i < contacts.contactGroupList().count(); ++i ) {
159  Akonadi::Item groupItem( KABC::ContactGroup::mimeType() );
160  groupItem.setPayload<KABC::ContactGroup>( contacts.contactGroupList().at(i) );
161 
162  Akonadi::Job *createJob = new Akonadi::ItemCreateJob( groupItem, collection );
163  connect( createJob, SIGNAL(result(KJob*)), this, SLOT(slotImportJobDone(KJob*)) );
164  }
165 
166 }
167 
168 void XXPortManager::slotImportJobDone( KJob * )
169 {
170  if ( !mImportProgressDialog ) {
171  return;
172  }
173 
174  QProgressBar *progressBar = mImportProgressDialog->progressBar();
175 
176  progressBar->setValue( progressBar->value() + 1 );
177 
178  // cleanup on last step
179  if ( progressBar->value() == progressBar->maximum() ) {
180  mImportProgressDialog->deleteLater();
181  mImportProgressDialog = 0;
182  }
183 }
184 
185 void XXPortManager::slotExport( const QString &identifier )
186 {
187  if ( !mSelectionModel ) {
188  return;
189  }
190 
191  const bool selectExportType = (identifier == QLatin1String("vcard21") || identifier == QLatin1String("vcard30") || identifier == QLatin1String("vcard40"));
192  QPointer<ContactSelectionDialog> dlg =
193  new ContactSelectionDialog( mSelectionModel, selectExportType, mParentWidget );
194  dlg->setMessageText( i18n( "Which contact do you want to export?" ) );
195  dlg->setDefaultAddressBook( mDefaultAddressBook );
196  if ( !dlg->exec() || !dlg ) {
197  delete dlg;
198  return;
199  }
200 
201  const KABC::AddresseeList contacts = dlg->selectedContacts().addressList();
202  const VCardExportSelectionWidget::ExportFields exportFields = dlg->exportType();
203  delete dlg;
204 
205  if ( contacts.isEmpty() ) {
206  KMessageBox::sorry( 0, i18n( "You have not selected any contacts to export." ) );
207  return;
208  }
209 
210  const XXPort *xxport = mFactory.createXXPort( identifier, mParentWidget );
211  if ( !xxport ) {
212  return;
213  }
214  ContactList contactLists;
215  contactLists.setAddressList(contacts);
216  xxport->exportContacts( contactLists, exportFields );
217 
218  delete xxport;
219 }
220 
ContactList::count
int count() const
Definition: contactlist.cpp:33
QProgressBar
QWidget
QProgressBar::maximum
maximum
ContactList::addressList
KABC::Addressee::List addressList() const
Definition: contactlist.cpp:64
QPointer
XXPortManager::importFile
void importFile(const KUrl &url)
Definition: xxportmanager.cpp:79
QProgressBar::setValue
void setValue(int value)
XXPort::importContacts
virtual ContactList importContacts() const =0
Imports a list of contacts.
QSignalMapper::setMapping
void setMapping(QObject *sender, int id)
xxportmanager.h
XXPort::setOption
void setOption(const QString &key, const QString &value)
Sets module specific options.
Definition: xxport.cpp:31
XXPortManager::XXPortManager
XXPortManager(QWidget *parent=0)
Creates a new xxport manager.
Definition: xxportmanager.cpp:40
XXPortManager::setSelectionModel
void setSelectionModel(QItemSelectionModel *model)
Sets the model that contains the current selection.
Definition: xxportmanager.cpp:69
QObject
ContactList
Definition: contactlist.h:27
ContactList::setAddressList
void setAddressList(const KABC::Addressee::List &value)
Definition: contactlist.cpp:69
QString::isEmpty
bool isEmpty() const
ContactList::isEmpty
bool isEmpty() const
Definition: contactlist.cpp:28
XXPortManager::setDefaultAddressBook
void setDefaultAddressBook(const Akonadi::Collection &addressBook)
Sets the addressBook that shall be preselected in the ContactSelectionDialog.
Definition: xxportmanager.cpp:74
XXPortManager::addImportAction
void addImportAction(QAction *action, const QString &identifier)
Adds a new action to import contacts.
Definition: xxportmanager.cpp:57
QString
QStringList
XXPort
The base class for all import/export modules.
Definition: xxport.h:35
XXPort::exportContacts
virtual bool exportContacts(const ContactList &contacts, VCardExportSelectionWidget::ExportFields) const =0
Exports the list of contacts.
XXPortManager::~XXPortManager
~XXPortManager()
Destroys the xxport manager.
Definition: xxportmanager.cpp:53
QLatin1String
ContactList::contactGroupList
KABC::ContactGroup::List contactGroupList() const
Definition: contactlist.cpp:54
QAction
QItemSelectionModel
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ContactSelectionDialog
A dialog to select a group of contacts.
Definition: contactselectiondialog.h:40
contactselectiondialog.h
QSignalMapper
XXPortFactory::createXXPort
XXPort * createXXPort(const QString &identifier, QWidget *parentWidget) const
Returns a new import/export module that matches the given identifier or a null pointer if the identif...
Definition: xxportfactory.cpp:28
XXPortManager::addExportAction
void addExportAction(QAction *action, const QString &identifier)
Adds a new action to export contacts.
Definition: xxportmanager.cpp:63
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:34 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal