summaryrefslogtreecommitdiffabout
path: root/kaddressbook/xxportmanager.cpp
authorzautrix <zautrix>2004-06-26 19:01:18 (UTC)
committer zautrix <zautrix>2004-06-26 19:01:18 (UTC)
commitb9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff)
tree2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kaddressbook/xxportmanager.cpp
downloadkdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz
kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2
Initial revision
Diffstat (limited to 'kaddressbook/xxportmanager.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/xxportmanager.cpp269
1 files changed, 269 insertions, 0 deletions
diff --git a/kaddressbook/xxportmanager.cpp b/kaddressbook/xxportmanager.cpp
new file mode 100644
index 0000000..6cfa9cb
--- a/dev/null
+++ b/kaddressbook/xxportmanager.cpp
@@ -0,0 +1,269 @@
1/*
2 This file is part of KAddressbook.
3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
22*/
23
24/*
25Enhanced Version of the file for platform independent KDE tools.
26Copyright (c) 2004 Ulf Schenk
27
28$Id$
29*/
30
31#include <qlayout.h>
32
33#include <kabc/addressbook.h>
34#include <kabc/resource.h>
35#include <kdebug.h>
36#include <kdialogbase.h>
37#include <klocale.h>
38#include <kmessagebox.h>
39
40#ifndef KAB_EMBEDDED
41#include <ktrader.h>
42#else //KAB_EMBEDDED
43#include "xxport/csv_xxport.h"
44#include "xxport/kde2_xxport.h"
45#include "xxport/vcard_xxport.h"
46#endif //KAB_EMBEDDED
47
48#include <libkdepim/addresseeview.h>
49
50#include "kabcore.h"
51#include "undocmds.h"
52#include "xxportselectdialog.h"
53
54#include "xxportmanager.h"
55
56KURL XXPortManager::importURL = KURL();
57QString XXPortManager::importData = QString::null;
58
59class PreviewDialog : public KDialogBase
60{
61 public:
62 PreviewDialog( const KABC::Addressee &addr,
63 QWidget *parent, const char *name = 0 );
64};
65
66XXPortManager::XXPortManager( KABCore *core, QObject *parent, const char *name )
67 : QObject( parent, name ), mCore( core ), mShowPreview( false )
68{
69 loadPlugins();
70}
71
72XXPortManager::~XXPortManager()
73{
74}
75
76void XXPortManager::restoreSettings()
77{
78}
79
80void XXPortManager::saveSettings()
81{
82}
83
84void XXPortManager::importVCard( const KURL &url )
85{
86 importVCard( url, false );
87}
88
89void XXPortManager::importVCard( const KURL &url, bool showPreview )
90{
91 importURL = url;
92 mShowPreview = showPreview;
93 slotImport( "vcard", "<empty>" );
94 mShowPreview = false;
95 importURL = KURL();
96}
97
98void XXPortManager::importVCard( const QString &vCard, bool showPreview )
99{
100 importData = vCard;
101 mShowPreview = showPreview;
102 slotImport( "vcard", "<empty>" );
103 mShowPreview = false;
104 importData = "";
105}
106
107void XXPortManager::slotImport( const QString &identifier, const QString &data )
108{
109 XXPortObject *obj = mXXPortObjects[ identifier ];
110 if ( !obj ) {
111 KMessageBox::error( mCore, i18n( "<qt>No import plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
112 return;
113 }
114
115 KABC::Resource *resource = mCore->requestResource( mCore );
116 if ( !resource )
117 return;
118
119 KABC::AddresseeList list = obj->importContacts( data );
120 KABC::AddresseeList::Iterator it;
121 bool imported = false;
122 for ( it = list.begin(); it != list.end(); ++it ) {
123 if ( mShowPreview ) {
124 PreviewDialog dlg( *it, mCore );
125 if ( !dlg.exec() )
126 continue;
127 }
128
129 (*it).setResource( resource );
130 // We use a PwNewCommand so the user can undo it.
131 PwNewCommand *command = new PwNewCommand( mCore->addressBook(), *it );
132 UndoStack::instance()->push( command );
133 RedoStack::instance()->clear();
134 imported = true;
135 }
136
137 if ( imported ) {
138 KMessageBox::information( mCore, i18n( "contacts successfully imported." ) );
139
140 emit modified();
141 }
142}
143
144void XXPortManager::slotExport( const QString &identifier, const QString &data )
145{
146 XXPortObject *obj = mXXPortObjects[ identifier ];
147 if ( !obj ) {
148 KMessageBox::error( mCore, i18n( "<qt>No export plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
149 return;
150 }
151
152 KABC::AddresseeList addrList;
153 XXPortSelectDialog dlg( mCore, obj->requiresSorting(), mCore );
154 if ( dlg.exec() )
155 addrList = dlg.contacts();
156 else
157 return;
158
159 if ( !obj->exportContacts( addrList, data ) )
160 KMessageBox::error( mCore, i18n( "Unable to export contacts." ) );
161 else
162 KMessageBox::information( mCore, i18n( "contacts successfully exported." ) );
163}
164
165void XXPortManager::loadPlugins()
166{
167 mXXPortObjects.clear();
168
169#ifndef KAB_EMBEDDED
170 KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/XXPort" );
171 KTrader::OfferList::ConstIterator it;
172 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
173 if ( !(*it)->hasServiceType( "KAddressBook/XXPort" ) )
174 continue;
175
176 KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
177 if ( !factory ) {
178 kdDebug(5720) << "XXPortManager::loadExtensions(): Factory creation failed" << endl;
179 continue;
180 }
181
182 XXPortFactory *xxportFactory = static_cast<XXPortFactory*>( factory );
183
184 if ( !xxportFactory ) {
185 kdDebug(5720) << "XXPortManager::loadExtensions(): Cast failed" << endl;
186 continue;
187 }
188
189 XXPortObject *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore );
190 if ( obj ) {
191 mCore->addGUIClient( obj );
192 mXXPortObjects.insert( obj->identifier(), obj );
193 connect( obj, SIGNAL( exportActivated( const QString&, const QString& ) ),
194 this, SLOT( slotExport( const QString&, const QString& ) ) );
195 connect( obj, SIGNAL( importActivated( const QString&, const QString& ) ),
196 this, SLOT( slotImport( const QString&, const QString& ) ) );
197 }
198 }
199
200#else //KAB_EMBEDDED
201//lets load the export/import stubs directly. Is dynamic loading necessary for KA/Pi?
202
203 // CVS import/export
204 XXPortFactory *xxportFactory = new CSVXXPortFactory();
205
206 XXPortObject *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore );
207 if ( obj ) {
208 mCore->addGUIClient( obj );
209 mXXPortObjects.insert( obj->identifier(), obj );
210 connect( obj, SIGNAL( exportActivated( const QString&, const QString& ) ),
211 this, SLOT( slotExport( const QString&, const QString& ) ) );
212 connect( obj, SIGNAL( importActivated( const QString&, const QString& ) ),
213 this, SLOT( slotImport( const QString&, const QString& ) ) );
214 }
215
216
217
218 // KDE2 import/export
219 xxportFactory = new KDE2XXPortFactory();
220
221 obj = xxportFactory->xxportObject( mCore->addressBook(), mCore );
222 if ( obj ) {
223 mCore->addGUIClient( obj );
224 mXXPortObjects.insert( obj->identifier(), obj );
225 connect( obj, SIGNAL( exportActivated( const QString&, const QString& ) ),
226 this, SLOT( slotExport( const QString&, const QString& ) ) );
227 connect( obj, SIGNAL( importActivated( const QString&, const QString& ) ),
228 this, SLOT( slotImport( const QString&, const QString& ) ) );
229 }
230
231
232
233 // VCARD import/export
234 xxportFactory = new VCardXXPortFactory();
235
236 obj = xxportFactory->xxportObject( mCore->addressBook(), mCore );
237 if ( obj ) {
238 mCore->addGUIClient( obj );
239 mXXPortObjects.insert( obj->identifier(), obj );
240 connect( obj, SIGNAL( exportActivated( const QString&, const QString& ) ),
241 this, SLOT( slotExport( const QString&, const QString& ) ) );
242 connect( obj, SIGNAL( importActivated( const QString&, const QString& ) ),
243 this, SLOT( slotImport( const QString&, const QString& ) ) );
244 }
245#endif //KAB_EMBEDDED
246
247}
248
249
250PreviewDialog::PreviewDialog( const KABC::Addressee &addr, QWidget *parent,
251 const char *name )
252 : KDialogBase( Plain, i18n( "Contact Preview" ), Ok | Cancel, Ok, parent,
253 name, true, true )
254{
255 QWidget *page = plainPage();
256 QVBoxLayout *layout = new QVBoxLayout( page, marginHint(), spacingHint() );
257
258 KPIM::AddresseeView *view = new KPIM::AddresseeView( page );
259 view->setAddressee( addr );
260
261 layout->addWidget( view );
262
263 resize( 400, 300 );
264}
265
266#ifndef KAB_EMBEDDED
267#include "xxportmanager.moc"
268#endif //KAB_EMBEDDED
269