You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdepim/kmobile/kmobileclient.cpp

236 lines
6.3 KiB

/* This file is part of the KDE KMobile library
Copyright (C) 2003-2005 Helge Deller <deller@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tqstringlist.h>
#include <ktrader.h>
#include <klocale.h>
#include <kdebug.h>
#include "kmobileclient.h"
#define KMOBILECLIENT_DEBUG_AREA 0
#define PRINT_DEBUG kdDebug(KMOBILECLIENT_DEBUG_AREA) << "KMobileClient: "
KMobileClient::KMobileClient()
: DCOPClient()
{
// initialize Application and Object of remote DCOP-aware KMobile application
m_kmobileApp = "kmobile";
m_kmobileObj = "kmobileIface";
bool ok = attach();
PRINT_DEBUG << TQString("attached to DCOP server %1\n").tqarg(ok?"sucessful.":"failed.");
// m_clientAppId = registerAs("kmobileclient");
// PRINT_DEBUG << TQString("registered as DCOP client %1\n").tqarg(m_clientAppId);
isKMobileAvailable();
}
KMobileClient::~KMobileClient()
{
detach();
PRINT_DEBUG << TQString("detached from server\n");
}
bool KMobileClient::isKMobileAvailable()
{
bool available = isApplicationRegistered(m_kmobileApp);
PRINT_DEBUG << TQString("KMobile DCOP server: %1\n").tqarg(available?"available.":"not available");
if (!available) {
startKMobileApplication();
// check again...
available = isApplicationRegistered(m_kmobileApp);
}
return available;
}
bool KMobileClient::startKMobileApplication()
{
TQByteArray data;
TQDataStream arg(data, IO_WriteOnly);
TQStringList params;
params << "--minimized";
arg << TQString("kmobile") << params;
TQCString replyType;
TQByteArray replyData;
bool ok = call("klauncher", "klauncher", "kdeinit_exec_wait(TQString,TQStringList)", data, replyType, replyData);
PRINT_DEBUG << TQString("DCOP-CALL to klauncher: %1\n").tqarg(ok?"ok.":"failed.");
return ok;
}
/**
* DCOP - IMPLEMENTATION
*/
#define KMOBILECLIENT_USE_EVENTLOOP true
#define KMOBILECLIENT_TIMEOUT (4*1000) // 4 seconds
#define PREPARE( FUNC, PARAMS ) \
TQByteArray data; \
TQDataStream arg(data, IO_WriteOnly); \
arg << PARAMS; \
TQCString replyType; \
TQByteArray replyData; \
bool ok = call(m_kmobileApp, m_kmobileObj, FUNC, data, replyType, replyData, KMOBILECLIENT_USE_EVENTLOOP, KMOBILECLIENT_TIMEOUT); \
PRINT_DEBUG << TQString("DCOP-CALL to %1: %2\n").tqarg(FUNC).tqarg(ok?"ok.":"FAILED.")
#define RETURN_TYPE( FUNC, PARAMS, RETURN_TYPE ) \
PREPARE( FUNC, PARAMS ); \
TQDataStream reply(replyData, IO_ReadOnly); \
RETURN_TYPE ret; \
if (ok) \
reply >> ret; \
return ret;
#define RETURN_TYPE_DEFAULT( FUNC, PARAMS, RETURN_TYPE, RETURN_DEFAULT ) \
PREPARE( FUNC, PARAMS ); \
TQDataStream reply(replyData, IO_ReadOnly); \
RETURN_TYPE ret = RETURN_DEFAULT; \
if (ok) \
reply >> ret; \
return ret;
#define RETURN_TQSTRING( FUNC, PARAMS ) \
RETURN_TYPE( FUNC, PARAMS, TQString )
#define RETURN_BOOL( FUNC, PARAMS ) \
RETURN_TYPE_DEFAULT( FUNC, PARAMS, bool, false )
#define RETURN_INT( FUNC, PARAMS ) \
RETURN_TYPE_DEFAULT( FUNC, PARAMS, int , 0 )
TQStringList KMobileClient::tqdeviceNames()
{
if (!isKMobileAvailable())
return TQStringList();
RETURN_TYPE( "tqdeviceNames()", TQString::tqfromLatin1(""), TQStringList );
}
void KMobileClient::removeDevice( TQString tqdeviceName )
{
PREPARE( "removeDevice(TQString)", tqdeviceName );
Q_UNUSED(ok);
}
void KMobileClient::configDevice( TQString tqdeviceName )
{
PREPARE( "configDevice(TQString)", tqdeviceName );
Q_UNUSED(ok);
}
bool KMobileClient::connectDevice( TQString tqdeviceName )
{
RETURN_BOOL( "connectDevice(TQString)", tqdeviceName );
}
bool KMobileClient::disconnectDevice( TQString tqdeviceName )
{
RETURN_BOOL( "disconnectDevice(TQString)", tqdeviceName );
}
bool KMobileClient::connected( TQString tqdeviceName )
{
RETURN_BOOL( "connected(TQString)", tqdeviceName );
}
TQString KMobileClient::tqdeviceClassName( TQString tqdeviceName )
{
RETURN_TQSTRING( "tqdeviceClassName(TQString)", tqdeviceName );
}
TQString KMobileClient::tqdeviceName( TQString tqdeviceName )
{
RETURN_TQSTRING( "tqdeviceName(TQString)", tqdeviceName );
}
TQString KMobileClient::revision( TQString tqdeviceName )
{
RETURN_TQSTRING( "revision(TQString)", tqdeviceName );
}
int KMobileClient::classType( TQString tqdeviceName )
{
RETURN_INT( "classType(TQString)", tqdeviceName );
}
int KMobileClient::capabilities( TQString tqdeviceName )
{
RETURN_INT( "capabilities(TQString)", tqdeviceName );
}
TQString KMobileClient::nameForCap( TQString tqdeviceName, int cap )
{
RETURN_TQSTRING( "nameForCap(TQString,int)", tqdeviceName << cap );
}
TQString KMobileClient::iconFileName( TQString tqdeviceName )
{
RETURN_TQSTRING( "iconFileName(TQString)", tqdeviceName );
}
int KMobileClient::numAddresses( TQString tqdeviceName )
{
RETURN_INT( "numAddresses(TQString)", tqdeviceName );
}
TQString KMobileClient::readAddress( TQString tqdeviceName, int index )
{
RETURN_TQSTRING( "readAddress(TQString,int)", tqdeviceName << index );
}
bool KMobileClient::storeAddress( TQString tqdeviceName, int index, TQString vcard, bool append )
{
RETURN_BOOL( "storeAddress(TQString,int,TQString,bool)", tqdeviceName << index << vcard << append );
}
int KMobileClient::numCalendarEntries( TQString tqdeviceName )
{
RETURN_INT( "numCalendarEntries(TQString)", tqdeviceName );
}
int KMobileClient::numNotes( TQString tqdeviceName )
{
RETURN_INT( "numNotes(TQString)", tqdeviceName );
}
TQString KMobileClient::readNote( TQString tqdeviceName, int index )
{
RETURN_TQSTRING( "readNote(TQString,int)", tqdeviceName << index );
}
bool KMobileClient::storeNote( TQString tqdeviceName, int index, TQString note )
{
RETURN_BOOL( "storeNote(TQString,int,TQString)", tqdeviceName << index << note );
}
#undef PRINT_DEBUG
#include "kmobileclient.moc"