diff --git a/linphone-desktop/resources.qrc b/linphone-desktop/resources.qrc index 8f9d12273..46d5d4fe2 100644 --- a/linphone-desktop/resources.qrc +++ b/linphone-desktop/resources.qrc @@ -194,6 +194,7 @@ ui/modules/Common/Form/Fields/TextAreaField.qml ui/modules/Common/Form/Fields/TextField.qml ui/modules/Common/Form/+linux/SearchBox.qml + ui/modules/Common/Form/ListForm.js ui/modules/Common/Form/ListForm.qml ui/modules/Common/Form/Placements/FormEmptyLine.qml ui/modules/Common/Form/Placements/FormGroup.qml diff --git a/linphone-desktop/src/components/contact/VcardModel.cpp b/linphone-desktop/src/components/contact/VcardModel.cpp index c08f3f797..741f2adb3 100644 --- a/linphone-desktop/src/components/contact/VcardModel.cpp +++ b/linphone-desktop/src/components/contact/VcardModel.cpp @@ -280,13 +280,13 @@ bool VcardModel::addSipAddress (const QString &sipAddress) { shared_ptr value = belcard::BelCardGeneric::create(); value->setValue(linphoneAddress->asStringUriOnly()); - qInfo() << QStringLiteral("Add new sip address on vcard: `%1`.").arg(sipAddress); - if (!belcard->addImpp(value)) { qWarning() << QStringLiteral("Unable to add sip address on vcard: `%1`.").arg(sipAddress); return false; } + qInfo() << QStringLiteral("Add new sip address on vcard: `%1`.").arg(sipAddress); + emit vcardUpdated(); return true; } @@ -338,13 +338,13 @@ bool VcardModel::addCompany (const QString &company) { shared_ptr value = belcard::BelCardGeneric::create(); value->setValue(::Utils::qStringToLinphoneString(company)); - qInfo() << QStringLiteral("Add new company on vcard: `%1`.").arg(company); - if (!belcard->addRole(value)) { qWarning() << QStringLiteral("Unable to add company on vcard: `%1`.").arg(company); return false; } + qInfo() << QStringLiteral("Add new company on vcard: `%1`.").arg(company); + emit vcardUpdated(); return true; } @@ -389,13 +389,13 @@ bool VcardModel::addEmail (const QString &email) { shared_ptr value = belcard::BelCardGeneric::create(); value->setValue(::Utils::qStringToLinphoneString(email)); - qInfo() << QStringLiteral("Add new email on vcard: `%1`.").arg(email); - if (!belcard->addEmail(value)) { qWarning() << QStringLiteral("Unable to add email on vcard: `%1`.").arg(email); return false; } + qInfo() << QStringLiteral("Add new email on vcard: `%1`.").arg(email); + emit vcardUpdated(); return true; } @@ -440,13 +440,13 @@ bool VcardModel::addUrl (const QString &url) { shared_ptr value = belcard::BelCardGeneric::create(); value->setValue(::Utils::qStringToLinphoneString(url)); - qInfo() << QStringLiteral("Add new url on vcard: `%1`.").arg(url); - if (!belcard->addURL(value)) { qWarning() << QStringLiteral("Unable to add url on vcard: `%1`.").arg(url); return false; } + qInfo() << QStringLiteral("Add new url on vcard: `%1`.").arg(url); + emit vcardUpdated(); return true; } diff --git a/linphone-desktop/ui/modules/Common/Form/ComboBox.qml b/linphone-desktop/ui/modules/Common/Form/ComboBox.qml index 77b18d6b4..1d547cc48 100644 --- a/linphone-desktop/ui/modules/Common/Form/ComboBox.qml +++ b/linphone-desktop/ui/modules/Common/Form/ComboBox.qml @@ -5,8 +5,6 @@ import QtQuick.Layouts 1.3 import Common 1.0 import Common.Styles 1.0 -import Utils 1.0 - import 'ComboBox.js' as Logic // ============================================================================= diff --git a/linphone-desktop/ui/modules/Common/Form/ListForm.js b/linphone-desktop/ui/modules/Common/Form/ListForm.js new file mode 100644 index 000000000..2d0426a5a --- /dev/null +++ b/linphone-desktop/ui/modules/Common/Form/ListForm.js @@ -0,0 +1,86 @@ +// ============================================================================= +// `ListForm.qml` Logic. +// ============================================================================= + +.import 'qrc:/ui/scripts/Utils/utils.js' as Utils + +// ============================================================================= + +function setData (data) { + var model = values.model + + model.clear() + data.forEach(function (data) { + model.append({ $value: data, $isInvalid: false }) + }) +} + +function setInvalid (index, status) { + Utils.assert( + index >= 0 && index < values.model.count, + 'Index ' + index + 'not exists.' + ) + + values.model.setProperty(index, '$isInvalid', status) +} + +function updateValue (index, value) { + var model = values.model + + // Unable to set property directly. Qt uses a cache of the value. + // It's necessary to remove then insert. + model.remove(index) + model.insert(index, { + $isInvalid: false, + $value: value + }) +} + +// ----------------------------------------------------------------------------- + +function addValue (value) { + values.model.append({ $value: value, $isInvalid: false }) + + if (value.length === 0) { + addButton.enabled = false + } +} + +function handleEditionFinished (index, text) { + var model = values.model + var oldValue = model.get(index).$value + + if (text.length === 0) { + // No changes. It must exists at least n min values. + if (minValues != null && minValues >= model.count) { + updateValue(index, oldValue) + return + } + + model.remove(index) + + if (oldValue.length !== 0) { + listForm.removed(index, oldValue) + } + } else if (text !== oldValue) { + // Update changes. + updateValue(index, text) + listForm.changed(index, oldValue, text) + } + + addButton.enabled = true +} + +function handleItemCreation () { + if (this.text.length === 0) { + // FIXME: Find the source of this problem. + // + // Magic code. If it's the first inserted value, + // an event or a callback steal the item focus. + // I suppose it's an internal Qt qml event... + // + // So, I choose to run a callback executed after this + // internal event. + Utils.setTimeout(values, 0, this.forceActiveFocus) + } +} diff --git a/linphone-desktop/ui/modules/Common/Form/ListForm.qml b/linphone-desktop/ui/modules/Common/Form/ListForm.qml index d71d22120..ea98080ce 100644 --- a/linphone-desktop/ui/modules/Common/Form/ListForm.qml +++ b/linphone-desktop/ui/modules/Common/Form/ListForm.qml @@ -3,7 +3,8 @@ import QtQuick.Layouts 1.3 import Common 1.0 import Common.Styles 1.0 -import Utils 1.0 + +import 'ListForm.js' as Logic // ============================================================================= @@ -21,64 +22,21 @@ RowLayout { // --------------------------------------------------------------------------- - signal changed (int index, string defaultValue, string newValue) + signal changed (int index, string oldValue, string newValue) signal removed (int index, string value) // --------------------------------------------------------------------------- - function setInvalid (index, status) { - Utils.assert( - index >= 0 && index < values.model.count, - 'Index ' + index + 'not exists.' - ) - - values.model.setProperty(index, '$isInvalid', status) + function setData () { + Logic.setData.apply(this, arguments) } - function setData (data) { - var model = values.model - - model.clear() - data.forEach(function (data) { - model.append({ $value: data, $isInvalid: false }) - }) + function setInvalid () { + Logic.setInvalid.apply(this, arguments) } - function _addValue (value) { - values.model.append({ $value: value, $isInvalid: false }) - - if (value.length === 0) { - addButton.enabled = false - } - } - - function _handleEditionFinished (index, text) { - var model = values.model - var defaultValue = model.get(index).$value - - if (text.length === 0) { - // No changes. It must exists at least n min values. - if (minValues != null && minValues >= model.count) { - // Unable to set property directly. Qt uses a cache of the value. - model.remove(index) - model.insert(index, { - $isInvalid: false, - $value: defaultValue - }) - return - } - - model.remove(index) - - if (defaultValue.length !== 0) { - listForm.removed(index, defaultValue) - } - } else if (text !== defaultValue) { - // Update changes. - listForm.changed(index, defaultValue, text) - } - - addButton.enabled = true + function updateValue () { + Logic.updateValue.apply(this, arguments) } // --------------------------------------------------------------------------- @@ -99,9 +57,9 @@ RowLayout { icon: 'add' iconSize: ListFormStyle.titleArea.iconSize - opacity: _edition ? 1 : 0 + opacity: !listForm.readOnly ? 1 : 0 - onClicked: _edition && _addValue('') + onClicked: !listForm.readOnly && Logic.addValue('') } Text { @@ -140,7 +98,7 @@ RowLayout { MouseArea { anchors.fill: parent - onClicked: !listForm.readOnly && _addValue('') + onClicked: !listForm.readOnly && Logic.addValue('') } } @@ -171,23 +129,9 @@ RowLayout { height: ListFormStyle.lineHeight width: parent.width - onEditingFinished: _handleEditionFinished(index, text) + Component.onCompleted: Logic.handleItemCreation.apply(this) - Component.onCompleted: { - if ($value.length === 0) { - // FIXME: Find the source of this problem. - // - // Magic code. If it's the first inserted value, - // an event or a callback steal the item focus. - // I suppose it's an internal Qt qml event... - // - // So, I choose to run a callback executed after this - // internal event. - Utils.setTimeout(values, 0, function () { - textInput.forceActiveFocus() - }) - } - } + onEditingFinished: Logic.handleEditionFinished(index, text) } } diff --git a/linphone-desktop/ui/views/App/Main/ContactEdit.js b/linphone-desktop/ui/views/App/Main/ContactEdit.js index b33a5a97a..248bc0f9c 100644 --- a/linphone-desktop/ui/views/App/Main/ContactEdit.js +++ b/linphone-desktop/ui/views/App/Main/ContactEdit.js @@ -116,15 +116,16 @@ function setUsername (username) { // ----------------------------------------------------------------------------- -function handleValueChanged (fields, index, defaultValue, newValue, add, update) { - if (newValue === defaultValue) { +function handleValueChanged (fields, index, oldValue, newValue, add, update) { + if (newValue === oldValue) { return } + console.log('handle', oldValue, newValue) var vcard = contactEdit._vcard - var soFarSoGood = (defaultValue.length === 0) + var soFarSoGood = (oldValue.length === 0) ? vcard[add](newValue) - : vcard[update](defaultValue, newValue) + : vcard[update](oldValue, newValue) fields.setInvalid(index, !soFarSoGood) } diff --git a/linphone-desktop/ui/views/App/Main/ContactEdit.qml b/linphone-desktop/ui/views/App/Main/ContactEdit.qml index 4d482b769..f549eb14f 100644 --- a/linphone-desktop/ui/views/App/Main/ContactEdit.qml +++ b/linphone-desktop/ui/views/App/Main/ContactEdit.qml @@ -206,7 +206,7 @@ ColumnLayout { readOnly: !_edition title: qsTr('sipAccounts') - onChanged: Logic.handleSipAddressChanged(addresses, index, defaultValue, newValue) + onChanged: Logic.handleSipAddressChanged(addresses, index, oldValue, newValue) onRemoved: _vcard.removeSipAddress(value) } @@ -226,7 +226,7 @@ ColumnLayout { readOnly: !_edition title: qsTr('companies') - onChanged: Logic.handleCompanyChanged(companies, index, defaultValue, newValue) + onChanged: Logic.handleCompanyChanged(companies, index, oldValue, newValue) onRemoved: _vcard.removeCompany(value) } @@ -246,7 +246,7 @@ ColumnLayout { readOnly: !_edition title: qsTr('emails') - onChanged: Logic.handleEmailChanged(emails, index, defaultValue, newValue) + onChanged: Logic.handleEmailChanged(emails, index, oldValue, newValue) onRemoved: _vcard.removeEmail(value) } @@ -266,7 +266,7 @@ ColumnLayout { readOnly: !_edition title: qsTr('webSites') - onChanged: Logic.handleUrlChanged(urls, index, defaultValue, newValue) + onChanged: Logic.handleUrlChanged(urls, index, oldValue, newValue) onRemoved: _vcard.removeUrl(value) }