Exemplo n.º 1
0
void ServiceBrowser::reloadAttributesRadioButtonText()
{
    QListWidgetItem *item = interfacesListWidget->currentItem();
    if (!item)
        return;

    QServiceInterfaceDescriptor selectedImpl =
            item->data(Qt::UserRole).value<QServiceInterfaceDescriptor>();
    QServiceInterfaceDescriptor defaultImpl = 
            serviceManager->interfaceDefault(selectedImpl.interfaceName());

    defaultImplRadioButton->setText(tr("Default implementation for %1\n(currently provided by %2)")
            .arg(defaultImpl.interfaceName())
            .arg(defaultImpl.serviceName()));
}
QDebug operator<<(QDebug dbg, const QServiceInterfaceDescriptor &desc)
{
    if (desc.isValid()) {
        QString serviceInterface = QString(QLatin1String("%1 %2.%3")).arg(desc.interfaceName())
                .arg(desc.majorVersion() < 0 ? '?' : desc.majorVersion())
                .arg(desc.minorVersion() < 0 ? '?' : desc.minorVersion());
        dbg.nospace() << "QServiceInterfaceDescriptor(";
        dbg.nospace() << "service=" << desc.serviceName() << ", ";
        dbg.nospace() << "interface=" << serviceInterface;
        dbg.nospace() << ")";
    } else {
        dbg.nospace() << "QServiceInterfaceDescriptor(invalid)";
    }
    return dbg.space();
}
void tst_QServiceInterfaceDescriptor::comparison()
{
    QServiceInterfaceDescriptor desc;
    QVERIFY(desc.majorVersion() == -1);
    QVERIFY(desc.minorVersion() == -1);
    QVERIFY(desc.serviceName().isEmpty());
    QVERIFY(desc.interfaceName().isEmpty());
    QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::Capabilities).isValid());
    QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::Location).isValid());
    QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::InterfaceDescription).isValid());
    QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::ServiceDescription).isValid());
    QVERIFY(desc.scope() == QService::UserScope);
    QVERIFY(!desc.isValid());

    QServiceInterfaceDescriptor copy(desc);
    QVERIFY(copy.majorVersion() == -1);
    QVERIFY(copy.minorVersion() == -1);
    QVERIFY(copy.serviceName().isEmpty());
    QVERIFY(copy.interfaceName().isEmpty());
    QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::Capabilities).isValid());
    QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::Location).isValid());
    QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::InterfaceDescription).isValid());
    QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::ServiceDescription).isValid());
    QVERIFY(copy.scope() == QService::UserScope);
    QVERIFY(!copy.isValid());

    QVERIFY(desc == copy);

    QServiceInterfaceDescriptor valid;
    QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
    QServiceInterfaceDescriptorPrivate::setPrivate(&valid, d);
    d->serviceName = "name";
    d->interfaceName = "interface";
    d->major = 3;
    d->minor = 1;
    d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("mydescription"));
    d->customAttributes.insert(QString("ckey"), QString("cvalue"));
    d->scope = QService::SystemScope;

    QCOMPARE(valid.interfaceName(), QString("interface"));
    QCOMPARE(valid.serviceName(), QString("name"));
    QCOMPARE(valid.majorVersion(), 3);
    QCOMPARE(valid.minorVersion(), 1);
    QCOMPARE(valid.customAttribute("ckey"), QString("cvalue"));
    QCOMPARE(valid.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
    QCOMPARE(valid.attribute(QServiceInterfaceDescriptor::Location).toString(), QString(""));
    QCOMPARE(valid.scope(), QService::SystemScope);
    QVERIFY(valid.isValid());

    QVERIFY(valid != desc);
    QVERIFY(desc != valid);

    //test copy constructor
    QServiceInterfaceDescriptor validCopy(valid);
    QVERIFY(valid==validCopy);
    QVERIFY(validCopy==valid);

    QServiceInterfaceDescriptorPrivate::getPrivate(&validCopy)->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
    QVERIFY(valid!=validCopy);
    QVERIFY(validCopy!=valid);

    QCOMPARE(validCopy.interfaceName(), QString("interface"));
    QCOMPARE(validCopy.serviceName(), QString("name"));
    QCOMPARE(validCopy.majorVersion(), 3);
    QCOMPARE(validCopy.minorVersion(), 1);
    QCOMPARE(validCopy.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("myValue"));
    QCOMPARE(validCopy.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
    QCOMPARE(validCopy.customAttribute("ckey"),QString("cvalue"));
    QCOMPARE(validCopy.scope(), QService::SystemScope);
    QVERIFY(validCopy.isValid());

    //test assignment operator
    QServiceInterfaceDescriptor validCopy2 = valid;
    QVERIFY(valid==validCopy2);
    QVERIFY(validCopy2==valid);

    QServiceInterfaceDescriptorPrivate::getPrivate(&validCopy2)->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue2"));
    QVERIFY(valid!=validCopy2);
    QVERIFY(validCopy2!=valid);

    QCOMPARE(validCopy2.interfaceName(), QString("interface"));
    QCOMPARE(validCopy2.serviceName(), QString("name"));
    QCOMPARE(validCopy2.majorVersion(), 3);
    QCOMPARE(validCopy2.minorVersion(), 1);
    QCOMPARE(validCopy2.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("myValue2"));
    QCOMPARE(validCopy2.customAttribute("ckey"),QString("cvalue"));
    QCOMPARE(validCopy2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
    QCOMPARE(validCopy2.scope(), QService::SystemScope);
    QVERIFY(validCopy2.isValid());

    //test customAttributes
    d->customAttributes.insert(QString("ckey"), QString("cvalue"));
    d->customAttributes.insert(QString("ckey1"), QString("cvalue1"));
    d->customAttributes.insert(QString("ckey2"), QString("cvalue2"));
    QStringList customAttributes = valid.customAttributes();
    QVERIFY(customAttributes.contains("ckey"));
    QVERIFY(customAttributes.contains("ckey1"));
    QVERIFY(customAttributes.contains("ckey2"));
    QCOMPARE(customAttributes.count(), 3);
}
void ctkQtMobilityServiceRuntime::processPlugin(QSharedPointer<ctkPlugin> plugin)
{
  QHash<QString, QString> headers = plugin->getHeaders();
  QHash<QString, QString>::const_iterator it = headers.find(ctkQtMobilityServiceConstants::SERVICE_DESCRIPTOR);
  ctkLogService* log = ctkQtMobilityServiceActivator::getLogService();
  CTK_DEBUG(log)
      << "Process header " << ctkQtMobilityServiceConstants::SERVICE_DESCRIPTOR
      << " for plugin #" << plugin->getPluginId() << ": " << (it != headers.end() ? it.value() : "[missing]");

  if (it != headers.end())
  {
    QString sd = it.value();
    if (sd.isEmpty())
    {
      QString msg = QString("Header ") + ctkQtMobilityServiceConstants::SERVICE_DESCRIPTOR + " empty.";
      ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
      return;
    }

    QByteArray serviceDescription = plugin->getResource(sd);
    QBuffer serviceBuffer(&serviceDescription);
    qServiceManager.addService(&serviceBuffer);
    QServiceManager::Error error = qServiceManager.error();
    if (!(error == QServiceManager::NoError || error == QServiceManager::ServiceAlreadyExists))
    {
      QString msg = QString("Registering the QtMobility service descriptor failed: ") +
          getQServiceManagerErrorString(error);
      ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
      return;
    }

    QString serviceName = plugin->getSymbolicName() + "_" + plugin->getVersion().toString();
    QList<QServiceInterfaceDescriptor> descriptors = qServiceManager.findInterfaces(serviceName);

    if (descriptors.isEmpty())
    {
      QString msg = QString("No interfaces found for service name ") + serviceName;
      ctkQtMobilityServiceActivator::logWarning(plugin->getPluginContext(), msg);
      return;
    }

    QListIterator<QServiceInterfaceDescriptor> it(descriptors);
    while (it.hasNext())
    {
      QServiceInterfaceDescriptor descr = it.next();
      CTK_DEBUG(ctkQtMobilityServiceActivator::getLogService()) << "Registering:" << descr.interfaceName();
      QStringList classes;
      ctkDictionary props;

      QStringList customKeys = descr.customAttributes();
      QStringListIterator keyIt(customKeys);
      bool classAttrFound = false;
      while (keyIt.hasNext())
      {
        QString key = keyIt.next();
        if (key == ctkPluginConstants::OBJECTCLASS)
        {
          classAttrFound = true;
          classes << descr.customAttribute(key);
        }
        else
        {
          props.insert(key, descr.customAttribute(key));
        }
      }

      if (!classAttrFound)
      {
        QString msg = QString("The custom attribute \"") + ctkPluginConstants::OBJECTCLASS
            + "\" is missing in the interface description of \"" + descr.interfaceName();
        ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
        continue;
      }

      ctkQtMobilityServiceFactory* serviceObject = new ctkQtMobilityServiceFactory(descr, this, plugin);
      ctkServiceRegistration serviceReg = plugin->getPluginContext()->registerService(classes, serviceObject, props);

      if (serviceReg)
      {
        mapPluginToServiceFactory.insert(plugin, serviceObject);
        mapPluginToServiceRegistration.insert(plugin, serviceReg);
      }
      else
      {
        QString msg = QString("Could not register QtMobility service ") + descr.serviceName() + " "
            + descr.interfaceName();
        ctkQtMobilityServiceActivator::logError(plugin->getPluginContext(), msg);
        continue;
      }
    }
  }
}