summaryrefslogtreecommitdiffabout
path: root/kaddressbook/xxportmanager.cpp
blob: 4798ffe3dbecff42149ce6d9b5d0fbb23c2b2715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
    This file is part of KAddressbook.
    Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

/*
Enhanced Version of the file for platform independent KDE tools.
Copyright (c) 2004 Ulf Schenk

$Id$
*/

#include <qlayout.h>
#include <qlist.h>

#include <kabc/addressbook.h>
#include <kabc/resource.h>
#include <kdebug.h>
#include <kdialogbase.h>
#include <klocale.h>
#include <kmessagebox.h>

#ifndef KAB_EMBEDDED
#include <ktrader.h>
#else //KAB_EMBEDDED
extern "C"
{
  void* init_kaddrbk_csv_xxport();
  void* init_kaddrbk_kde2_xxport();
  void* init_kaddrbk_vcard_xxport();
}
#endif //KAB_EMBEDDED

#include <libkdepim/addresseeview.h>

#include "kabcore.h"
#include "undocmds.h"
#include "xxportselectdialog.h"

#include "xxportmanager.h"

KURL XXPortManager::importURL = KURL();
QString XXPortManager::importData = QString::null;

class PreviewDialog : public KDialogBase
{
  public:
    PreviewDialog( const KABC::Addressee &addr,
                   QWidget *parent, const char *name = 0 );
};

XXPortManager::XXPortManager( KABCore *core, QObject *parent, const char *name )
  : QObject( parent, name ), mCore( core ), mShowPreview( false )
{
  loadPlugins();
}

XXPortManager::~XXPortManager()
{
}

void XXPortManager::restoreSettings()
{
}

void XXPortManager::saveSettings()
{
}

void XXPortManager::importVCard( const KURL &url )
{
  importVCard( url, false );
}

void XXPortManager::importVCard( const KURL &url, bool showPreview )
{
  importURL = url;
  mShowPreview = showPreview;
  slotImport( "vcard", "<empty>" );
  mShowPreview = false;
  importURL = KURL();
}

void XXPortManager::importVCard( const QString &vCard, bool showPreview )
{
  importData = vCard;
  mShowPreview = showPreview;
  slotImport( "vcard", "<empty>" );
  mShowPreview = false;
  importData = "";
}

void XXPortManager::slotImport( const QString &identifier, const QString &data )
{
  XXPortObject *obj = mXXPortObjects[ identifier ];
  if ( !obj ) {
    KMessageBox::error( mCore, i18n( "<qt>No import plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
    return;
  }

  KABC::Resource *resource = mCore->requestResource( mCore );
  if ( !resource )
    return;

  KABC::AddresseeList list = obj->importContacts( data );
  KABC::AddresseeList::Iterator it;
  bool imported = false;
  for ( it = list.begin(); it != list.end(); ++it ) {
    if ( mShowPreview ) {
      PreviewDialog dlg( *it, mCore );
      if ( !dlg.exec() )
        continue;
    }
    
    (*it).setResource( resource );
    // We use a PwNewCommand so the user can undo it.
    PwNewCommand *command = new PwNewCommand( mCore->addressBook(), *it );
    UndoStack::instance()->push( command );
    RedoStack::instance()->clear();
    imported = true;
  }
  
  if ( imported ) {
    KMessageBox::information( mCore, i18n( "contacts successfully imported." ) );
  
    emit modified();
  }
}

void XXPortManager::slotExport( const QString &identifier, const QString &data )
{
  XXPortObject *obj = mXXPortObjects[ identifier ];
  if ( !obj ) {
    KMessageBox::error( mCore, i18n( "<qt>No export plugin available for <b>%1</b>.</qt>" ).arg( identifier ) );
    return;
  }

  KABC::AddresseeList addrList;
  XXPortSelectDialog dlg( mCore, obj->requiresSorting(), mCore );
  if ( dlg.exec() )
    addrList = dlg.contacts();
  else
    return;

  if ( !obj->exportContacts( addrList, data ) )
    KMessageBox::error( mCore, i18n( "Unable to export contacts." ) );
  else
    KMessageBox::information( mCore, i18n( "contacts successfully exported." ) );
}

void XXPortManager::loadPlugins()
{
  mXXPortObjects.clear();

#ifndef KAB_EMBEDDED
  KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/XXPort" );
  KTrader::OfferList::ConstIterator it;
  for ( it = plugins.begin(); it != plugins.end(); ++it ) {
    if ( !(*it)->hasServiceType( "KAddressBook/XXPort" ) )
      continue;
    
    KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
    if ( !factory ) {
      kdDebug(5720) << "XXPortManager::loadExtensions(): Factory creation failed" << endl;
      continue;
    }

    XXPortFactory *xxportFactory = static_cast<XXPortFactory*>( factory );

    if ( !xxportFactory ) {
      kdDebug(5720) << "XXPortManager::loadExtensions(): Cast failed" << endl;
      continue;
    }
          
#else //KAB_EMBEDDED
  QList<XXPortFactory> factorylist;
  factorylist.append(static_cast<XXPortFactory*>(init_kaddrbk_csv_xxport()));
  factorylist.append(static_cast<XXPortFactory*>(init_kaddrbk_kde2_xxport()));
  factorylist.append(static_cast<XXPortFactory*>(init_kaddrbk_vcard_xxport()));

  //add the opie import library dynamically
  KLibFactory *factory = KLibLoader::self()->factory( "microkaddrbk_opie_xxport" );
  if ( factory ) {
    XXPortFactory *xxportFactory = static_cast<XXPortFactory*>( factory );
    factorylist.append(xxportFactory);
  }

  //add the qtopia import library dynamically
  factory = KLibLoader::self()->factory( "microkaddrbk_qtopia_xxport" );
  if ( factory ) {
    XXPortFactory *xxportFactory = static_cast<XXPortFactory*>( factory );
    factorylist.append(xxportFactory);
  }
  
  QListIterator<XXPortFactory> it(factorylist);  
  for ( ; it.current(); ++it )
  {
    XXPortFactory *xxportFactory = it.current();
#endif //KAB_EMBEDDED

    XXPortObject *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore );
    if ( obj ) {
      mCore->addGUIClient( obj );
      mXXPortObjects.insert( obj->identifier(), obj );
      connect( obj, SIGNAL( exportActivated( const QString&, const QString& ) ),
               this, SLOT( slotExport( const QString&, const QString& ) ) );
      connect( obj, SIGNAL( importActivated( const QString&, const QString& ) ),
               this, SLOT( slotImport( const QString&, const QString& ) ) );
    }
  }
}


PreviewDialog::PreviewDialog( const KABC::Addressee &addr, QWidget *parent,
                              const char *name )
  : KDialogBase( Plain, i18n( "Contact Preview" ), Ok | Cancel, Ok, parent,
                 name, true, true )
{
  QWidget *page = plainPage();
  QVBoxLayout *layout = new QVBoxLayout( page, marginHint(), spacingHint() );
  
  KPIM::AddresseeView *view = new KPIM::AddresseeView( page );
  view->setAddressee( addr );

  layout->addWidget( view );

  resize( 400, 300 );
}

#ifndef KAB_EMBEDDED
#include "xxportmanager.moc"
#endif //KAB_EMBEDDED