tools/runonphone/symbianutils/symbiandevicemanager.cpp
branchRCL_3
changeset 8 3f74d0d4af4c
child 30 5dc02b23752f
equal deleted inserted replaced
6:dee5afe5301f 8:3f74d0d4af4c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "symbiandevicemanager.h"
       
    43 
       
    44 #include <QtCore/QSettings>
       
    45 #include <QtCore/QStringList>
       
    46 #include <QtCore/QFileInfo>
       
    47 #include <QtCore/QtDebug>
       
    48 #include <QtCore/QTextStream>
       
    49 #include <QtCore/QSharedData>
       
    50 #include <QtCore/QScopedPointer>
       
    51 
       
    52 namespace SymbianUtils {
       
    53 
       
    54 enum { debug = 0 };
       
    55 
       
    56 static const char REGKEY_CURRENT_CONTROL_SET[] = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet";
       
    57 static const char USBSER[] = "Services/usbser/Enum";
       
    58 
       
    59 const char *SymbianDeviceManager::linuxBlueToothDeviceRootC = "/dev/rfcomm";
       
    60 
       
    61 // ------------- SymbianDevice
       
    62 class SymbianDeviceData : public QSharedData {
       
    63 public:
       
    64     SymbianDeviceData() : type(SerialPortCommunication) {}
       
    65 
       
    66     QString portName;
       
    67     QString friendlyName;
       
    68     QString deviceDesc;
       
    69     QString manufacturer;
       
    70     DeviceCommunicationType type;
       
    71 };
       
    72 
       
    73 SymbianDevice::SymbianDevice(SymbianDeviceData *data) :
       
    74     m_data(data)
       
    75 {
       
    76 
       
    77 }
       
    78 
       
    79 SymbianDevice::SymbianDevice() :
       
    80     m_data(new SymbianDeviceData)
       
    81 {
       
    82 }
       
    83 SymbianDevice::SymbianDevice(const SymbianDevice &rhs) :
       
    84         m_data(rhs.m_data)
       
    85 {
       
    86 }
       
    87 
       
    88 SymbianDevice &SymbianDevice::operator=(const SymbianDevice &rhs)
       
    89 {
       
    90     if (this != &rhs)
       
    91         m_data = rhs.m_data;
       
    92     return *this;
       
    93 }
       
    94 
       
    95 SymbianDevice::~SymbianDevice()
       
    96 {
       
    97 }
       
    98 
       
    99 QString SymbianDevice::portName() const
       
   100 {
       
   101     return m_data->portName;
       
   102 }
       
   103 
       
   104 QString SymbianDevice::friendlyName() const
       
   105 {
       
   106     return m_data->friendlyName;
       
   107 }
       
   108 
       
   109 QString SymbianDevice::deviceDesc() const
       
   110 {
       
   111     return m_data->deviceDesc;
       
   112 }
       
   113 
       
   114 QString SymbianDevice::manufacturer() const
       
   115 {
       
   116     return m_data->manufacturer;
       
   117 }
       
   118 
       
   119 DeviceCommunicationType SymbianDevice::type() const
       
   120 {
       
   121     return m_data->type;
       
   122 }
       
   123 
       
   124 bool SymbianDevice::isNull() const
       
   125 {
       
   126     return !m_data->portName.isEmpty();
       
   127 }
       
   128 
       
   129 QString SymbianDevice::toString() const
       
   130 {
       
   131     QString rc;
       
   132     QTextStream str(&rc);
       
   133     format(str);
       
   134     return rc;
       
   135 }
       
   136 
       
   137 void SymbianDevice::format(QTextStream &str) const
       
   138 {
       
   139     str << (m_data->type == BlueToothCommunication ? "Bluetooth: " : "Serial: ")
       
   140         << m_data->portName;
       
   141     if (!m_data->friendlyName.isEmpty()) {
       
   142         str << " (" << m_data->friendlyName;
       
   143         if (!m_data->deviceDesc.isEmpty())
       
   144           str << " / " << m_data->deviceDesc;
       
   145         str << ')';
       
   146     }
       
   147     if (!m_data->manufacturer.isEmpty())
       
   148         str << " [" << m_data->manufacturer << ']';
       
   149 }
       
   150 
       
   151 // Compare by port and friendly name
       
   152 int SymbianDevice::compare(const SymbianDevice &rhs) const
       
   153 {
       
   154     if (const int prc = m_data->portName.compare(rhs.m_data->portName))
       
   155         return prc;
       
   156     if (const int frc = m_data->friendlyName.compare(rhs.m_data->friendlyName))
       
   157         return frc;
       
   158     return 0;
       
   159 }
       
   160 
       
   161 QDebug operator<<(QDebug d, const SymbianDevice &cd)
       
   162 {
       
   163     d.nospace() << cd.toString();
       
   164     return d;
       
   165 }
       
   166 
       
   167 // ------------- SymbianDeviceManagerPrivate
       
   168 struct SymbianDeviceManagerPrivate {
       
   169     SymbianDeviceManagerPrivate() : m_initialized(false) {}
       
   170 
       
   171     bool m_initialized;
       
   172     SymbianDeviceManager::SymbianDeviceList m_devices;
       
   173 };
       
   174 
       
   175 SymbianDeviceManager::SymbianDeviceManager(QObject *parent) :
       
   176     QObject(parent),
       
   177     d(new SymbianDeviceManagerPrivate)
       
   178 {
       
   179 }
       
   180 
       
   181 SymbianDeviceManager::~SymbianDeviceManager()
       
   182 {
       
   183     delete d;
       
   184 }
       
   185 
       
   186 SymbianDeviceManager::SymbianDeviceList SymbianDeviceManager::devices() const
       
   187 {
       
   188     if (!d->m_initialized)
       
   189         const_cast<SymbianDeviceManager*>(this)->update(false);
       
   190     return d->m_devices;
       
   191 }
       
   192 
       
   193 QString SymbianDeviceManager::toString() const
       
   194 {
       
   195     QString rc;
       
   196     QTextStream str(&rc);
       
   197     const int count = d->m_devices.size();
       
   198     for (int i = 0; i < count; i++) {
       
   199         str << '#' << i << ' ';
       
   200         d->m_devices.at(i).format(str);
       
   201         str << '\n';
       
   202     }
       
   203     return rc;
       
   204 }
       
   205 
       
   206 QString SymbianDeviceManager::friendlyNameForPort(const QString &port) const
       
   207 {
       
   208     foreach (const SymbianDevice &device, d->m_devices) {
       
   209         if (device.portName() == port)
       
   210             return device.friendlyName();
       
   211     }
       
   212     return QString();
       
   213 }
       
   214 
       
   215 void SymbianDeviceManager::update()
       
   216 {
       
   217     update(true);
       
   218 }
       
   219 
       
   220 void SymbianDeviceManager::update(bool emitSignals)
       
   221 {
       
   222     typedef SymbianDeviceList::iterator SymbianDeviceListIterator;
       
   223 
       
   224     if (debug)
       
   225         qDebug(">SerialDeviceLister::update(%d)\n%s", int(emitSignals),
       
   226                qPrintable(toString()));
       
   227 
       
   228     d->m_initialized = true;
       
   229     // Get ordered new list
       
   230     SymbianDeviceList newDevices = serialPorts() + blueToothDevices();
       
   231     if (newDevices.size() > 1)
       
   232         qStableSort(newDevices.begin(), newDevices.end());
       
   233     if (d->m_devices == newDevices) // Happy, nothing changed.
       
   234         return;
       
   235     // Merge the lists and emit the respective added/removed signals, assuming
       
   236     // no one can plug a different device on the same port at the speed of lightning
       
   237     if (!d->m_devices.isEmpty()) {
       
   238         // Find deleted devices
       
   239         for (SymbianDeviceListIterator oldIt = d->m_devices.begin(); oldIt != d->m_devices.end(); ) {
       
   240             if (newDevices.contains(*oldIt)) {
       
   241                 ++oldIt;
       
   242             } else {
       
   243                 const SymbianDevice toBeDeleted = *oldIt;
       
   244                 oldIt = d->m_devices.erase(oldIt);
       
   245                 if (emitSignals)
       
   246                     emit deviceRemoved(toBeDeleted);
       
   247             }
       
   248         }
       
   249     }
       
   250     if (!newDevices.isEmpty()) {
       
   251         // Find new devices and insert in order
       
   252         foreach(const SymbianDevice &newDevice, newDevices) {
       
   253             if (!d->m_devices.contains(newDevice)) {
       
   254                 d->m_devices.append(newDevice);
       
   255                 if (emitSignals)
       
   256                     emit deviceAdded(newDevice);
       
   257             }
       
   258         }
       
   259         if (d->m_devices.size() > 1)
       
   260             qStableSort(d->m_devices.begin(), d->m_devices.end());
       
   261     }
       
   262     if (emitSignals)
       
   263         emit updated();
       
   264 
       
   265     if (debug)
       
   266         qDebug("<SerialDeviceLister::update\n%s\n", qPrintable(toString()));
       
   267 }
       
   268 
       
   269 SymbianDeviceManager::SymbianDeviceList SymbianDeviceManager::serialPorts() const
       
   270 {
       
   271     SymbianDeviceList rc;
       
   272 #ifdef Q_OS_WIN
       
   273     const QSettings registry(REGKEY_CURRENT_CONTROL_SET, QSettings::NativeFormat);
       
   274     const QString usbSerialRootKey = QLatin1String(USBSER) + QLatin1Char('/');
       
   275     const int count = registry.value(usbSerialRootKey + QLatin1String("Count")).toInt();
       
   276     for (int i = 0; i < count; ++i) {
       
   277         QString driver = registry.value(usbSerialRootKey + QString::number(i)).toString();
       
   278         if (driver.contains(QLatin1String("JAVACOMM"))) {
       
   279             driver.replace(QLatin1Char('\\'), QLatin1Char('/'));
       
   280             const QString driverRootKey = QLatin1String("Enum/") + driver + QLatin1Char('/');
       
   281             if (debug > 1)
       
   282                 qDebug() << "SerialDeviceLister::serialPorts(): Checking " << i << count
       
   283                          << REGKEY_CURRENT_CONTROL_SET << usbSerialRootKey << driverRootKey;
       
   284             QScopedPointer<SymbianDeviceData> device(new SymbianDeviceData);
       
   285             device->type = SerialPortCommunication;
       
   286             device->friendlyName = registry.value(driverRootKey + QLatin1String("FriendlyName")).toString();
       
   287             device->portName = registry.value(driverRootKey + QLatin1String("Device Parameters/PortName")).toString();
       
   288             device->deviceDesc = registry.value(driverRootKey + QLatin1String("DeviceDesc")).toString();
       
   289             device->manufacturer = registry.value(driverRootKey + QLatin1String("Mfg")).toString();
       
   290             rc.append(SymbianDevice(device.take()));
       
   291         }
       
   292     }
       
   293 #endif
       
   294     return rc;
       
   295 }
       
   296 
       
   297 SymbianDeviceManager::SymbianDeviceList SymbianDeviceManager::blueToothDevices() const
       
   298 {
       
   299     SymbianDeviceList rc;
       
   300 #if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
       
   301     // Bluetooth devices are created on connection. List the existing ones
       
   302     // or at least the first one.
       
   303     const QString prefix = QLatin1String(linuxBlueToothDeviceRootC);
       
   304     const QString friendlyFormat = QLatin1String("Bluetooth device (%1)");
       
   305     for (int d = 0; d < 4; d++) {
       
   306         QScopedPointer<SymbianDeviceData> device(new SymbianDeviceData);
       
   307         device->type = BlueToothCommunication;
       
   308         device->portName = prefix + QString::number(d);
       
   309         if (d == 0 || QFileInfo(device->portName).exists()) {
       
   310             device->friendlyName = friendlyFormat.arg(device->portName);
       
   311             rc.push_back(SymbianDevice(device.take()));
       
   312         }
       
   313     }
       
   314 #endif
       
   315     return rc;
       
   316 }
       
   317 
       
   318 Q_GLOBAL_STATIC(SymbianDeviceManager, symbianDeviceManager)
       
   319 
       
   320 SymbianDeviceManager *SymbianDeviceManager::instance()
       
   321 {
       
   322     return symbianDeviceManager();
       
   323 }
       
   324 
       
   325 QDebug operator<<(QDebug d, const SymbianDeviceManager &sdm)
       
   326 {
       
   327     d.nospace() << sdm.toString();
       
   328     return d;
       
   329 }
       
   330 
       
   331 } // namespace SymbianUtilsInternal