Commit 1eb1effb authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Device Support: Add canRestore() to device factory.



Also get rid of supportsDeviceType(), which is no longer needed now that
most services live in IDevice itself.
The new approach is more flexible as well, allowing factories to refuse
loading a device even if they know its type (e.g. because it is no
longer relevant).
We also rename loadDevice() to restore(), because that is more
exact and is also the name that is being used elsewhere in QtCreator.

Change-Id: I51f57c118a048c1a781a55a94966d554d7aed7a2
Reviewed-by: default avatarTobias Hunger <tobias.hunger@nokia.com>
parent 40e76b74
......@@ -66,18 +66,19 @@ IDevice::Ptr MaddeDeviceConfigurationFactory::create() const
return wizard.device();
}
IDevice::Ptr MaddeDeviceConfigurationFactory::loadDevice(const QVariantMap &map) const
bool MaddeDeviceConfigurationFactory::canRestore(const QVariantMap &map) const
{
QTC_ASSERT(supportsDeviceType(IDevice::typeFromMap(map)), return MaddeDevice::Ptr());
MaddeDevice::Ptr device = MaddeDevice::create();
device->fromMap(map);
return device;
const QString type = IDevice::typeFromMap(map);
return type == QLatin1String(Maemo5OsType) || type == QLatin1String(HarmattanOsType)
|| type == QLatin1String(MeeGoOsType);
}
bool MaddeDeviceConfigurationFactory::supportsDeviceType(const QString &type) const
IDevice::Ptr MaddeDeviceConfigurationFactory::restore(const QVariantMap &map) const
{
return type == QLatin1String(Maemo5OsType) || type == QLatin1String(HarmattanOsType)
|| type == QLatin1String(MeeGoOsType);
QTC_ASSERT(canRestore(map), return MaddeDevice::Ptr());
const MaddeDevice::Ptr device = MaddeDevice::create();
device->fromMap(map);
return device;
}
} // namespace Internal
......
......@@ -46,8 +46,8 @@ public:
QString displayName() const;
bool canCreate() const;
ProjectExplorer::IDevice::Ptr create() const;
ProjectExplorer::IDevice::Ptr loadDevice(const QVariantMap &map) const;
bool supportsDeviceType(const QString &type) const;
bool canRestore(const QVariantMap &map) const;
ProjectExplorer::IDevice::Ptr restore(const QVariantMap &map) const;
};
} // namespace Internal
......
......@@ -161,10 +161,10 @@ void DeviceManager::loadPre2_6()
QVariantMap map;
foreach (const QString &key, settings->childKeys())
map.insert(key, settings->value(key));
const IDeviceFactory *factory = factoryForDeviceType(IDevice::typeFromMap(map));
const IDeviceFactory * const factory = restoreFactory(map);
if (!factory)
continue;
IDevice::Ptr device = factory->loadDevice(map);
IDevice::Ptr device = factory->restore(map);
QTC_ASSERT(device, continue);
d->devices << device;
}
......@@ -182,10 +182,10 @@ void DeviceManager::fromMap(const QVariantMap &map)
const QVariantList deviceList = map.value(QLatin1String(DeviceListKey)).toList();
foreach (const QVariant &v, deviceList) {
const QVariantMap map = v.toMap();
const IDeviceFactory * const factory = factoryForDeviceType(IDevice::typeFromMap(map));
const IDeviceFactory * const factory = restoreFactory(map);
if (!factory)
continue;
IDevice::Ptr device = factory->loadDevice(map);
const IDevice::Ptr device = factory->restore(map);
QTC_ASSERT(device, continue);
if (device->isAutoDetected())
d->inactiveAutoDetectedDevices << device;
......@@ -320,14 +320,16 @@ void DeviceManager::setDefaultDevice(int idx)
emit updated();
}
const IDeviceFactory *DeviceManager::factoryForDeviceType(const QString &type)
const IDeviceFactory *DeviceManager::restoreFactory(const QVariantMap &map)
{
const QList<IDeviceFactory *> &factories
= ExtensionSystem::PluginManager::instance()->getObjects<IDeviceFactory>();
foreach (const IDeviceFactory * const factory, factories) {
if (factory->supportsDeviceType(type))
if (factory->canRestore(map))
return factory;
}
qWarning("Warning: No factory found for device of type '%s'.",
qPrintable(IDevice::typeFromMap(map)));
return 0;
}
......
......@@ -72,8 +72,6 @@ public:
void addDevice(const IDevice::Ptr &device);
void removeDevice(int index);
static const IDeviceFactory *factoryForDeviceType(const QString &type);
signals:
void deviceUpdated(const Core::Id &id);
......@@ -90,6 +88,7 @@ private:
void load();
void save();
void loadPre2_6();
static const IDeviceFactory *restoreFactory(const QVariantMap &map);
void fromMap(const QVariantMap &map);
QVariantMap toMap() const;
void ensureOneDefaultDevicePerType();
......
......@@ -76,14 +76,15 @@ public:
virtual IDevice::Ptr create() const = 0;
/*!
Loads a device from a serialized state. The device must be of a matching type.
*/
virtual IDevice::Ptr loadDevice(const QVariantMap &map) const = 0;
Check whether this factory can restore a device from the given serialized state.
*/
virtual bool canRestore(const QVariantMap &map) const = 0;
/*!
Returns true iff this factory supports the given device type.
Loads a device from a serialized state. Will only ever be called if canRestore()
returns true for the given map.
*/
virtual bool supportsDeviceType(const QString &type) const = 0;
virtual IDevice::Ptr restore(const QVariantMap &map) const = 0;
protected:
IDeviceFactory(QObject *parent) : QObject(parent) { }
......
......@@ -57,17 +57,16 @@ ProjectExplorer::IDevice::Ptr SymbianIDeviceFactory::create() const
return ProjectExplorer::IDevice::Ptr();
}
ProjectExplorer::IDevice::Ptr SymbianIDeviceFactory::loadDevice(const QVariantMap &map) const
bool SymbianIDeviceFactory::canRestore(const QVariantMap &map) const
{
QTC_ASSERT(supportsDeviceType(ProjectExplorer::IDevice::typeFromMap(map)),
return ProjectExplorer::IDevice::Ptr());
SymbianIDevice *dev = new SymbianIDevice(map);
return ProjectExplorer::IDevice::Ptr(dev);
return ProjectExplorer::IDevice::typeFromMap(map) == deviceType();
}
bool SymbianIDeviceFactory::supportsDeviceType(const QString &type) const
ProjectExplorer::IDevice::Ptr SymbianIDeviceFactory::restore(const QVariantMap &map) const
{
return type == deviceType();
QTC_ASSERT(canRestore(map), return ProjectExplorer::IDevice::Ptr());
SymbianIDevice *dev = new SymbianIDevice(map);
return ProjectExplorer::IDevice::Ptr(dev);
}
QString SymbianIDeviceFactory::deviceType()
......
......@@ -48,8 +48,8 @@ public:
QString displayName() const;
bool canCreate() const;
ProjectExplorer::IDevice::Ptr create() const;
ProjectExplorer::IDevice::Ptr loadDevice(const QVariantMap &map) const;
bool supportsDeviceType(const QString &type) const;
bool canRestore(const QVariantMap &map) const;
ProjectExplorer::IDevice::Ptr restore(const QVariantMap &map) const;
static QString deviceType();
};
......
......@@ -64,18 +64,17 @@ IDevice::Ptr GenericLinuxDeviceConfigurationFactory::create() const
return wizard.device();
}
IDevice::Ptr GenericLinuxDeviceConfigurationFactory::loadDevice(const QVariantMap &map) const
bool GenericLinuxDeviceConfigurationFactory::canRestore(const QVariantMap &map) const
{
QTC_ASSERT(supportsDeviceType(IDevice::typeFromMap(map)),
return LinuxDeviceConfiguration::Ptr());
LinuxDeviceConfiguration::Ptr device = LinuxDeviceConfiguration::create();
device->fromMap(map);
return device;
return IDevice::typeFromMap(map) == QLatin1String(Constants::GenericLinuxOsType);
}
bool GenericLinuxDeviceConfigurationFactory::supportsDeviceType(const QString &deviceType) const
IDevice::Ptr GenericLinuxDeviceConfigurationFactory::restore(const QVariantMap &map) const
{
return deviceType == QLatin1String(Constants::GenericLinuxOsType);
QTC_ASSERT(canRestore(map), return LinuxDeviceConfiguration::Ptr());
const LinuxDeviceConfiguration::Ptr device = LinuxDeviceConfiguration::create();
device->fromMap(map);
return device;
}
} // namespace RemoteLinux
......@@ -49,8 +49,8 @@ public:
QString displayName() const;
bool canCreate() const;
ProjectExplorer::IDevice::Ptr create() const;
ProjectExplorer::IDevice::Ptr loadDevice(const QVariantMap &map) const;
bool supportsDeviceType(const QString &deviceType) const;
bool canRestore(const QVariantMap &map) const;
ProjectExplorer::IDevice::Ptr restore(const QVariantMap &map) const;
};
} // namespace RemoteLinux
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment