summaryrefslogtreecommitdiffstats
path: root/kaddressbook/xxportmanager.cpp
blob: 6d6b74e55546494ed1309c3744ddb498cc3d4485 (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
/*
    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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

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

#include <tqlayout.h>

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

#include "core.h"
#include "kablock.h"
#include "undocmds.h"
#include "xxportselectdialog.h"

#include "xxportmanager.h"

KURL XXPortManager::importURL = KURL();
TQString XXPortManager::importData = TQString();

XXPortManager::XXPortManager( KAB::Core *core, TQObject *parent, const char *name )
  : TQObject( parent, name ), mCore( core )
{
  loadPlugins();
}

XXPortManager::~XXPortManager()
{
}

void XXPortManager::restoreSettings()
{
}

void XXPortManager::saveSettings()
{
}

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

void XXPortManager::importVCardFromData( const TQString &vCard )
{
  importData = vCard;
  slotImport( "vcard", "<empty>" );
  importData = "";
}

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

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

  KABC::AddresseeList list = obj->importContacts( data );
  KABC::AddresseeList::Iterator it;
  for ( it = list.begin(); it != list.end(); ++it )
    (*it).setResource( resource );

  if ( !list.isEmpty() ) {
    NewCommand *command = new NewCommand( mCore->addressBook(), list );
    mCore->commandHistory()->addCommand( command );
    emit modified();
  }
}

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

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

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

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

  const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/XXPort",
    TQString( "[X-TDE-KAddressBook-XXPortPluginVersion] == %1" ).arg( KAB_XXPORT_PLUGIN_VERSION ) );
  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;
    }

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

    if ( !xxportFactory ) {
      kdDebug(5720) << "XXPortManager::loadExtensions(): Cast failed" << endl;
      continue;
    }

    KAB::XXPort *obj = xxportFactory->xxportObject( mCore->addressBook(), mCore->widget() );
    if ( obj ) {
      if ( mCore->guiClient() )
        mCore->guiClient()->insertChildClient( obj );

      mXXPortObjects.insert( obj->identifier(), obj );
      connect( obj, TQT_SIGNAL( exportActivated( const TQString&, const TQString& ) ),
               this, TQT_SLOT( slotExport( const TQString&, const TQString& ) ) );
      connect( obj, TQT_SIGNAL( importActivated( const TQString&, const TQString& ) ),
               this, TQT_SLOT( slotImport( const TQString&, const TQString& ) ) );

      obj->setTDEApplication( kapp );
    }
  }
}

#include "xxportmanager.moc"