From 8f28d7512088aa998c814056ab52830bc8b0255a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Wed, 17 Aug 2016 11:35:55 +0200 Subject: [PATCH] experiment with translation install path --- debian/guh-plugins.install | 2 - debian/guhd.install | 1 - libguh/devicemanager.cpp | 110 ++++++++++++++++----------------- libguh/plugin/deviceplugin.cpp | 17 +++-- libguh/plugin/deviceplugin.h | 4 +- server/main.cpp | 5 ++ translations/guhd_de_DE.ts | 10 +-- translations/guhd_en_US.ts | 10 +-- 8 files changed, 85 insertions(+), 74 deletions(-) diff --git a/debian/guh-plugins.install b/debian/guh-plugins.install index 4cd0861e..a2819aea 100644 --- a/debian/guh-plugins.install +++ b/debian/guh-plugins.install @@ -1,5 +1,3 @@ -usr/share/guh/translations/*.qm - usr/lib/guh/plugins/libguh_devicepluginconrad.so usr/lib/guh/plugins/libguh_devicepluginelro.so usr/lib/guh/plugins/libguh_deviceplugineq3.so diff --git a/debian/guhd.install b/debian/guhd.install index 2e5db01d..b3a74c6e 100644 --- a/debian/guhd.install +++ b/debian/guhd.install @@ -1,4 +1,3 @@ usr/bin/guhd -usr/share/guh/translations/*.qm data/systemd/guhd.service /etc/systemd/system/ data/logrotate/guhd /etc/logrotate.d/ diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 1a366a97..f02636fe 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -221,7 +221,7 @@ DeviceManager::DeviceManager(const QLocale &locale, QObject *parent) : m_avahiBrowser->enable(); // Bluetooth LE - #ifdef BLUETOOTH_LE +#ifdef BLUETOOTH_LE m_bluetoothScanner = new BluetoothScanner(this); if (!m_bluetoothScanner->isAvailable()) { delete m_bluetoothScanner; @@ -229,7 +229,7 @@ DeviceManager::DeviceManager(const QLocale &locale, QObject *parent) : } else { connect(m_bluetoothScanner, &BluetoothScanner::bluetoothDiscoveryFinished, this, &DeviceManager::bluetoothDiscoveryFinished); } - #endif +#endif // Give hardware a chance to start up before loading plugins etc. QMetaObject::invokeMethod(this, "loadPlugins", Qt::QueuedConnection); @@ -968,9 +968,10 @@ void DeviceManager::loadPlugins() } else { fi.setFile(path + "/" + entry + "/libguh_deviceplugin" + entry + ".so"); } - if (!fi.exists()) { + + if (!fi.exists()) continue; - } + QPluginLoader loader; loader.setFileName(fi.absoluteFilePath()); @@ -987,71 +988,68 @@ void DeviceManager::loadPlugins() continue; } - pluginIface->setLocale(m_locale); + if (!pluginIface->setLocale(m_locale)) + qCWarning(dcDeviceManager()) << "Could not load translation" << m_locale.name() << "for plugin" << pluginIface->pluginName(); + qApp->installTranslator(pluginIface->translator()); - if (verifyPluginMetadata(loader.metaData().value("MetaData").toObject())) { - pluginIface->initPlugin(loader.metaData().value("MetaData").toObject(), this); - qCDebug(dcDeviceManager) << "**** Loaded plugin" << pluginIface->pluginName(); - foreach (const Vendor &vendor, pluginIface->supportedVendors()) { - qCDebug(dcDeviceManager) << "* Loaded vendor:" << vendor.name(); - if (m_supportedVendors.contains(vendor.id())) { - continue; - } - m_supportedVendors.insert(vendor.id(), vendor); - } + if (!verifyPluginMetadata(loader.metaData().value("MetaData").toObject())) + continue; - foreach (const DeviceClass &deviceClass, pluginIface->supportedDevices()) { - if (!m_supportedVendors.contains(deviceClass.vendorId())) { - qCWarning(dcDeviceManager) << "! Vendor not found. Ignoring device. VendorId:" << deviceClass.vendorId() << "DeviceClass:" << deviceClass.name() << deviceClass.id(); - continue; - } - m_vendorDeviceMap[deviceClass.vendorId()].append(deviceClass.id()); - m_supportedDevices.insert(deviceClass.id(), deviceClass); - qCDebug(dcDeviceManager) << "* Loaded device class:" << deviceClass.name(); + pluginIface->initPlugin(loader.metaData().value("MetaData").toObject(), this); + qCDebug(dcDeviceManager) << "**** Loaded plugin" << pluginIface->pluginName(); + foreach (const Vendor &vendor, pluginIface->supportedVendors()) { + qCDebug(dcDeviceManager) << "* Loaded vendor:" << vendor.name(); + if (m_supportedVendors.contains(vendor.id())) { + continue; } + m_supportedVendors.insert(vendor.id(), vendor); + } - GuhSettings settings(GuhSettings::SettingsRolePlugins); - settings.beginGroup("PluginConfig"); - ParamList params; - if (settings.childGroups().contains(pluginIface->pluginId().toString())) { - settings.beginGroup(pluginIface->pluginId().toString()); - foreach (const QString ¶mName, settings.allKeys()) { - Param param(paramName, settings.value(paramName)); - params.append(param); - } - settings.endGroup(); - } else if (pluginIface->configurationDescription().count() > 0){ - // plugin requires config but none stored. Init with defaults - foreach (const ParamType ¶mType, pluginIface->configurationDescription()) { - Param param(paramType.name(), paramType.defaultValue()); - params.append(param); - } + foreach (const DeviceClass &deviceClass, pluginIface->supportedDevices()) { + if (!m_supportedVendors.contains(deviceClass.vendorId())) { + qCWarning(dcDeviceManager) << "Vendor not found. Ignoring device. VendorId:" << deviceClass.vendorId() << "DeviceClass:" << deviceClass.name() << deviceClass.id(); + continue; + } + m_vendorDeviceMap[deviceClass.vendorId()].append(deviceClass.id()); + m_supportedDevices.insert(deviceClass.id(), deviceClass); + qCDebug(dcDeviceManager) << "* Loaded device class:" << deviceClass.name(); + } + + GuhSettings settings(GuhSettings::SettingsRolePlugins); + settings.beginGroup("PluginConfig"); + ParamList params; + if (settings.childGroups().contains(pluginIface->pluginId().toString())) { + settings.beginGroup(pluginIface->pluginId().toString()); + foreach (const QString ¶mName, settings.allKeys()) { + Param param(paramName, settings.value(paramName)); + params.append(param); } settings.endGroup(); - - if (params.count() > 0) { - DeviceError status = pluginIface->setConfiguration(params); - if (status != DeviceErrorNoError) { - qCWarning(dcDeviceManager) << "Error setting params to plugin. Broken configuration?"; - } + } else if (pluginIface->configurationDescription().count() > 0){ + // plugin requires config but none stored. Init with defaults + foreach (const ParamType ¶mType, pluginIface->configurationDescription()) { + Param param(paramType.name(), paramType.defaultValue()); + params.append(param); } } settings.endGroup(); - DeviceError status = pluginIface->setConfiguration(params); - if (status != DeviceErrorNoError) { - qWarning() << "Error setting params to plugin. Broken configuration?"; + + if (params.count() > 0) { + DeviceError status = pluginIface->setConfiguration(params); + if (status != DeviceErrorNoError) { + qCWarning(dcDeviceManager) << "Error setting params to plugin. Broken configuration?"; + } } - m_devicePlugins.insert(pluginIface->pluginId(), pluginIface); + m_devicePlugins.insert(pluginIface->pluginId(), pluginIface); - connect(pluginIface, &DevicePlugin::emitEvent, this, &DeviceManager::eventTriggered); - connect(pluginIface, &DevicePlugin::devicesDiscovered, this, &DeviceManager::slotDevicesDiscovered, Qt::QueuedConnection); - connect(pluginIface, &DevicePlugin::deviceSetupFinished, this, &DeviceManager::slotDeviceSetupFinished); - connect(pluginIface, &DevicePlugin::actionExecutionFinished, this, &DeviceManager::actionExecutionFinished); - connect(pluginIface, &DevicePlugin::pairingFinished, this, &DeviceManager::slotPairingFinished); - connect(pluginIface, &DevicePlugin::autoDevicesAppeared, this, &DeviceManager::autoDevicesAppeared); - } + connect(pluginIface, &DevicePlugin::emitEvent, this, &DeviceManager::eventTriggered); + connect(pluginIface, &DevicePlugin::devicesDiscovered, this, &DeviceManager::slotDevicesDiscovered, Qt::QueuedConnection); + connect(pluginIface, &DevicePlugin::deviceSetupFinished, this, &DeviceManager::slotDeviceSetupFinished); + connect(pluginIface, &DevicePlugin::actionExecutionFinished, this, &DeviceManager::actionExecutionFinished); + connect(pluginIface, &DevicePlugin::pairingFinished, this, &DeviceManager::slotPairingFinished); + connect(pluginIface, &DevicePlugin::autoDevicesAppeared, this, &DeviceManager::autoDevicesAppeared); } } } diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 1dfce369..a888fd24 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -196,7 +196,7 @@ QList DevicePlugin::supportedDevices() const foreach (const QJsonValue &deviceClassJson, vendorJson.toObject().value("deviceClasses").toArray()) { QJsonObject jo = deviceClassJson.toObject(); DeviceClass deviceClass(pluginId(), vendorId, jo.value("deviceClassId").toString()); - deviceClass.setName(jo.value("name").toString()); + deviceClass.setName(translateValue(m_metaData.value("idName").toString(), jo.value("name").toString())); DeviceClass::CreateMethods createMethods; foreach (const QJsonValue &createMethodValue, jo.value("createMethods").toArray()) { if (createMethodValue.toString() == "discovery") { @@ -238,7 +238,7 @@ QList DevicePlugin::supportedDevices() const "in deviceClass " << deviceClass.name() << ". Falling back to SetupMethodJustAdd."; deviceClass.setSetupMethod(DeviceClass::SetupMethodJustAdd); } - deviceClass.setPairingInfo(jo.value("pairingInfo").toString()); + deviceClass.setPairingInfo(translateValue(m_metaData.value("idName").toString(), jo.value("pairingInfo").toString())); QPair > paramTypesVerification = parseParamTypes(jo.value("paramTypes").toArray()); if (!paramTypesVerification.first) { broken = true; @@ -446,9 +446,9 @@ QTranslator *DevicePlugin::translator() return m_translator; } -void DevicePlugin::setLocale(const QLocale &locale) +bool DevicePlugin::setLocale(const QLocale &locale) { - m_translator->load(locale, "mock", "_", GuhSettings::translationsPath(), ".qm"); + return m_translator->load(locale, "mock", "_", GuhSettings::translationsPath(), ".qm"); } /*! Override this if your plugin supports Device with DeviceClass::CreationMethodAuto. @@ -864,6 +864,15 @@ QStringList DevicePlugin::verifyFields(const QStringList &fields, const QJsonObj return ret; } +QString DevicePlugin::translateValue(const QString &context, const QString &string) const +{ + QString translation = m_translator->translate(context.toUtf8().constData(), string.toUtf8().constData()); + if (translation.isEmpty()) + translation = string; + + return translation; +} + QPair DevicePlugin::loadAndVerifyUnit(const QString &unitString) const { if (unitString.isEmpty()) diff --git a/libguh/plugin/deviceplugin.h b/libguh/plugin/deviceplugin.h index 3430b6c5..326267b8 100644 --- a/libguh/plugin/deviceplugin.h +++ b/libguh/plugin/deviceplugin.h @@ -62,7 +62,7 @@ public: QList supportedDevices() const; QTranslator *translator(); - void setLocale(const QLocale &locale); + bool setLocale(const QLocale &locale); virtual DeviceManager::HardwareResources requiredHardware() const = 0; @@ -135,6 +135,8 @@ private: QStringList verifyFields(const QStringList &fields, const QJsonObject &value) const; + QString translateValue(const QString &context, const QString &string) const; + // load and verify enum values QPair loadAndVerifyUnit(const QString &unitString) const; QPair loadAndVerifyInputType(const QString &inputType) const; diff --git a/server/main.cpp b/server/main.cpp index c6df0f97..07291bbe 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -130,6 +130,11 @@ int main(int argc, char *argv[]) loggingFiltersPlugins.insert(pluginMetadata.value("idName").toString(), false); } + // Translator for the server application + QTranslator translator; + translator.load(QLocale::system(), application.applicationName(), "_", GuhSettings::translationsPath(), ".qm"); + qApp->installTranslator(&translator); + QCommandLineParser parser; parser.addHelpOption(); parser.addVersionOption(); diff --git a/translations/guhd_de_DE.ts b/translations/guhd_de_DE.ts index 1d0db88d..b5276c5a 100644 --- a/translations/guhd_de_DE.ts +++ b/translations/guhd_de_DE.ts @@ -4,7 +4,7 @@ main - + guh ( /[guːh]/ ) is an open source IoT (Internet of Things) server, which allows to control a lot of different devices from many different @@ -23,12 +23,12 @@ Szenen undVerhaltensweisen des Systems festzulegen. - + Run guhd in the foreground, not as daemon. Starte guhd im Vordergrund, nicht als Service. - + Debug categories to enable. Prefix with "No" to disable. Warnings from all categories will be printed unless explicitly muted with "NoWarnings". Categories are: @@ -36,12 +36,12 @@ Categories are: Es gibt folgende Kategorien: - + Enables all debug categories. This parameter overrides all debug category parameters. Aktiviere alle Debug-Kategorien. Dieser Parameter überschreibt alle anderen Debug-Kategorien Parameter. - + No such debug category: Diese Debug-Kategorie existiert nicht: diff --git a/translations/guhd_en_US.ts b/translations/guhd_en_US.ts index 4b55ebeb..9ef4f030 100644 --- a/translations/guhd_en_US.ts +++ b/translations/guhd_en_US.ts @@ -4,7 +4,7 @@ main - + guh ( /[guːh]/ ) is an open source IoT (Internet of Things) server, which allows to control a lot of different devices from many different @@ -16,24 +16,24 @@ for your environment. - + Run guhd in the foreground, not as daemon. - + Debug categories to enable. Prefix with "No" to disable. Warnings from all categories will be printed unless explicitly muted with "NoWarnings". Categories are: - + Enables all debug categories. This parameter overrides all debug category parameters. - + No such debug category: