delete obsolete entities, the associated docbooks have been removed with r454821...
[kdepim.git] / kmobile / kmobileitem.cpp
blob0cf61fec7d835789b904578e9d5db08a1e53e225
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.
20 #include <qobject.h>
22 #include <kiconloader.h>
23 #include <klocale.h>
24 #include <kdebug.h>
25 #include <kconfig.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)
35 config = _config;
37 Q_CHECK_PTR(service);
38 if (service) {
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;
49 setPixmap(getIcon());
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)
57 config = _config;
59 if (!configLoad(reload_index)) {
60 delete this;
61 return;
64 setPixmap(getIcon());
65 setRenameEnabled(true);
68 KMobileItem::~KMobileItem()
70 delete m_dev;
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 );
81 config->sync();
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() )
94 return false;
96 return true;
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);
123 p = icon.find('.');
124 if (p>=0) icon = icon.left(p);
126 QString comment;
127 if (m_dev)
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", "" );
140 conf.sync();
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);
150 return offers;
154 KService::Ptr KMobileItem::getServicePtr() const
156 KTrader::OfferList list = getMobileDevicesList();
157 KTrader::OfferListIterator it;
158 KService::Ptr ptr;
159 for ( it = list.begin(); it != list.end(); ++it ) {
160 KService::Ptr ptr = *it;
161 if (ptr->desktopEntryName() == m_deviceDesktopFile)
162 return ptr;
164 PRINT_DEBUG << QString("Service for library '%1' not found in KService list\n")
165 .arg(m_deviceDesktopFile);
166 return 0L;
170 * loads & initializes the device and returns a pointer to it.
172 bool KMobileItem::driverAvailable()
174 if (m_dev)
175 return true;
177 KService::Ptr ptr = getServicePtr();
178 if (!ptr)
179 return false;
181 PRINT_DEBUG << QString("Loading library %1\n").arg(ptr->library());
182 KLibFactory *factory = KLibLoader::self()->factory( ptr->library().utf8() );
183 if (!factory)
184 return false;
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);
191 return (m_dev != 0);
194 #include "kmobileitem.moc"