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/kmobileitem.cpp

196 lines
5.3 KiB

/* This file is part of the KDE KMobile library
Copyright (C) 2003 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 <tqobject.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdebug.h>
#include <kconfig.h>
#include "kmobileitem.h"
#define PRINT_DEBUG kdDebug() << "KMobileItem: "
KMobileItem::KMobileItem(TQIconView *tqparent, KConfig *_config, KService::Ptr service)
: TQObject(tqparent), TQIconViewItem(tqparent), m_dev(0L)
{
config = _config;
Q_CHECK_PTR(service);
if (service) {
setText(service->name());
m_tqdeviceDesktopFile = service->desktopEntryName();
m_tqdeviceConfigFile = TQString("kmobile_%1_rc").tqarg(text());
m_tqdeviceConfigFile = m_tqdeviceConfigFile.tqreplace(' ', "");
m_iconName = service->icon();
};
if (m_iconName.isEmpty())
m_iconName = KMOBILE_ICON_UNKNOWN;
setPixmap(getIcon());
setRenameEnabled(true);
}
/* restore this item from the config file */
KMobileItem::KMobileItem(TQIconView *tqparent, KConfig *_config, int reload_index)
: TQObject(tqparent), TQIconViewItem(tqparent), m_dev(0L)
{
config = _config;
if (!configLoad(reload_index)) {
delete this;
return;
}
setPixmap(getIcon());
setRenameEnabled(true);
}
KMobileItem::~KMobileItem()
{
delete m_dev;
}
void KMobileItem::configSave() const
{
config->setGroup( config_SectionName() );
config->writeEntry( "Name", text() );
config->writeEntry( "Config", m_tqdeviceConfigFile );
config->writeEntry( "DesktopFile", m_tqdeviceDesktopFile );
config->writeEntry( "IconName", m_iconName );
config->sync();
}
bool KMobileItem::configLoad(int idx)
{
config->setGroup( config_SectionName(idx) );
setText( config->readEntry("Name") );
m_tqdeviceConfigFile = config->readEntry( "Config" );
m_tqdeviceDesktopFile = config->readEntry( "DesktopFile" );
m_iconName = config->readEntry( "IconName" );
if (text().isEmpty() || m_tqdeviceConfigFile.isEmpty() ||
m_tqdeviceDesktopFile.isEmpty() || m_iconName.isEmpty() )
return false;
return true;
}
TQPixmap KMobileItem::getIcon() const
{
return KGlobal::instance()->iconLoader()->loadIcon(m_iconName, KIcon::Desktop );
}
TQString KMobileItem::config_SectionName(int idx) const
{
if (idx == -1) idx = index();
return TQString("MobileDevice_%1").tqarg(idx);
}
/* this MimeType is used by konqueror */
TQString KMobileItem::getKonquMimeType() const
{
return KMOBILE_MIMETYPE_DEVICE_KONTQUEROR(text());
}
/* provide MimeType for konqueror */
void KMobileItem::writeKonquMimeFile() const
{
// strip path and file extension of icon name
TQString icon = m_iconName;
int p = icon.tqfindRev('/');
if (p>=0) icon = icon.mid(p+1);
p = icon.tqfind('.');
if (p>=0) icon = icon.left(p);
TQString comment;
if (m_dev)
comment = m_dev->tqdeviceClassName();
if (comment.isEmpty())
comment = KMobileDevice::defaultClassName(KMobileDevice::Unclassified);
KConfig conf( getKonquMimeType()+".desktop", false, true, "mime" );
conf.setDesktopGroup();
conf.writeEntry("Encoding", "UTF-8");
conf.writeEntry("Comment", comment );
conf.writeEntry("Type", "MimeType");
conf.writeEntry("Icon", icon );
conf.writeEntry("MimeType", getKonquMimeType());
conf.writeEntry("Patterns", "" );
conf.sync();
}
/*
* get a list of all services providing a libkmobile tqdevice driver
*/
KTrader::OfferList KMobileItem::getMobileDevicesList()
{
KTrader::OfferList offers = KTrader::self()->query(KMOBILE_MIMETYPE_DEVICE);
return offers;
}
KService::Ptr KMobileItem::getServicePtr() const
{
KTrader::OfferList list = getMobileDevicesList();
KTrader::OfferListIterator it;
KService::Ptr ptr;
for ( it = list.begin(); it != list.end(); ++it ) {
KService::Ptr ptr = *it;
if (ptr->desktopEntryName() == m_tqdeviceDesktopFile)
return ptr;
}
PRINT_DEBUG << TQString("Service for library '%1' not found in KService list\n")
.tqarg(m_tqdeviceDesktopFile);
return 0L;
}
/*
* loads & initializes the tqdevice and returns a pointer to it.
*/
bool KMobileItem::driverAvailable()
{
if (m_dev)
return true;
KService::Ptr ptr = getServicePtr();
if (!ptr)
return false;
PRINT_DEBUG << TQString("Loading library %1\n").tqarg(ptr->library());
KLibFactory *factory = KLibLoader::self()->factory( ptr->library().utf8() );
if (!factory)
return false;
m_dev = static_cast<KMobileDevice *>(factory->create(this, ptr->name().utf8(),
"KMobileDevice", TQStringList(m_tqdeviceConfigFile)));
PRINT_DEBUG << TQString("Got KMobileDevice object at 0x%1, configfile=%2\n")
.tqarg((unsigned long)m_dev, 0, 16).tqarg(m_tqdeviceConfigFile);
return (m_dev != 0);
}
#include "kmobileitem.moc"