1 /* This file is part of the KDE KMobile library
2 Copyright (C) 2003 Helge Deller <deller@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
22 #include <kiconloader.h>
27 #include "kmobileitem.h"
30 #define PRINT_DEBUG kdDebug() << "KMobileItem: "
32 KMobileItem::KMobileItem(QIconView
*parent
, KConfig
*_config
, KService::Ptr service
)
33 : QObject(parent
), QIconViewItem(parent
), m_dev(0L)
39 setText(service
->name());
40 m_deviceDesktopFile
= service
->desktopEntryName();
41 m_deviceConfigFile
= QString("kmobile_%1_rc").arg(text());
42 m_deviceConfigFile
= m_deviceConfigFile
.replace(' ', "");
43 m_iconName
= service
->icon();
46 if (m_iconName
.isEmpty())
47 m_iconName
= KMOBILE_ICON_UNKNOWN
;
50 setRenameEnabled(true);
53 /* restore this item from the config file */
54 KMobileItem::KMobileItem(QIconView
*parent
, KConfig
*_config
, int reload_index
)
55 : QObject(parent
), QIconViewItem(parent
), m_dev(0L)
59 if (!configLoad(reload_index
)) {
65 setRenameEnabled(true);
68 KMobileItem::~KMobileItem()
74 void KMobileItem::configSave() const
76 config
->setGroup( config_SectionName() );
77 config
->writeEntry( "Name", text() );
78 config
->writeEntry( "Config", m_deviceConfigFile
);
79 config
->writeEntry( "DesktopFile", m_deviceDesktopFile
);
80 config
->writeEntry( "IconName", m_iconName
);
84 bool KMobileItem::configLoad(int idx
)
86 config
->setGroup( config_SectionName(idx
) );
87 setText( config
->readEntry("Name") );
88 m_deviceConfigFile
= config
->readEntry( "Config" );
89 m_deviceDesktopFile
= config
->readEntry( "DesktopFile" );
90 m_iconName
= config
->readEntry( "IconName" );
92 if (text().isEmpty() || m_deviceConfigFile
.isEmpty() ||
93 m_deviceDesktopFile
.isEmpty() || m_iconName
.isEmpty() )
99 QPixmap
KMobileItem::getIcon() const
101 return KGlobal::instance()->iconLoader()->loadIcon(m_iconName
, KIcon::Desktop
);
104 QString
KMobileItem::config_SectionName(int idx
) const
106 if (idx
== -1) idx
= index();
107 return QString("MobileDevice_%1").arg(idx
);
110 /* this MimeType is used by konqueror */
111 QString
KMobileItem::getKonquMimeType() const
113 return KMOBILE_MIMETYPE_DEVICE_KONQUEROR(text());
116 /* provide MimeType for konqueror */
117 void KMobileItem::writeKonquMimeFile() const
119 // strip path and file extension of icon name
120 QString icon
= m_iconName
;
121 int p
= icon
.findRev('/');
122 if (p
>=0) icon
= icon
.mid(p
+1);
124 if (p
>=0) icon
= icon
.left(p
);
128 comment
= m_dev
->deviceClassName();
129 if (comment
.isEmpty())
130 comment
= KMobileDevice::defaultClassName(KMobileDevice::Unclassified
);
132 KConfig
conf( getKonquMimeType()+".desktop", false, true, "mime" );
133 conf
.setDesktopGroup();
134 conf
.writeEntry("Encoding", "UTF-8");
135 conf
.writeEntry("Comment", comment
);
136 conf
.writeEntry("Type", "MimeType");
137 conf
.writeEntry("Icon", icon
);
138 conf
.writeEntry("MimeType", getKonquMimeType());
139 conf
.writeEntry("Patterns", "" );
145 * get a list of all services providing a libkmobile device driver
147 KTrader::OfferList
KMobileItem::getMobileDevicesList()
149 KTrader::OfferList offers
= KTrader::self()->query(KMOBILE_MIMETYPE_DEVICE
);
154 KService::Ptr
KMobileItem::getServicePtr() const
156 KTrader::OfferList list
= getMobileDevicesList();
157 KTrader::OfferListIterator it
;
159 for ( it
= list
.begin(); it
!= list
.end(); ++it
) {
160 KService::Ptr ptr
= *it
;
161 if (ptr
->desktopEntryName() == m_deviceDesktopFile
)
164 PRINT_DEBUG
<< QString("Service for library '%1' not found in KService list\n")
165 .arg(m_deviceDesktopFile
);
170 * loads & initializes the device and returns a pointer to it.
172 bool KMobileItem::driverAvailable()
177 KService::Ptr ptr
= getServicePtr();
181 PRINT_DEBUG
<< QString("Loading library %1\n").arg(ptr
->library());
182 KLibFactory
*factory
= KLibLoader::self()->factory( ptr
->library().utf8() );
186 m_dev
= static_cast<KMobileDevice
*>(factory
->create(this, ptr
->name().utf8(),
187 "KMobileDevice", QStringList(m_deviceConfigFile
)));
188 PRINT_DEBUG
<< QString("Got KMobileDevice object at 0x%1, configfile=%2\n")
189 .arg((unsigned long)m_dev
, 0, 16).arg(m_deviceConfigFile
);
194 #include "kmobileitem.moc"