From kde-commits Sun Jun 30 22:37:55 2013 From: David Edmundson Date: Sun, 30 Jun 2013 22:37:55 +0000 To: kde-commits Subject: [libkpeople/dave] src: Disallow changing URI of PersonData after creation Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=137263188932570 Git commit 56ecc360e55baa997ee011f8cab2b701cdab0ba1 by David Edmundson. Committed on 29/06/2013 at 13:49. Pushed by davidedmundson into branch 'dave'. Disallow changing URI of PersonData after creation Currently setUri and setContactId are public which would cause a lot of bug= s if changed. Instead replace with two clear static methods; loadFromUri and loadFromCont= actId A declarative wrapper using QDeclarativeParserStatus provides the same func= tionality in QML. M +33 -44 src/autotests/tests/persondatatests.cpp M +4 -1 src/declarative/CMakeLists.txt A +56 -0 src/declarative/declarativepersondata.cpp [License: GPL = (v3+)] A +50 -0 src/declarative/declarativepersondata.h [License: GPL (v= 3+)] M +2 -1 src/declarative/peopleqmlplugin.cpp M +2 -3 src/examples/personwidget.cpp M +1 -2 src/personactionsmodel.cpp M +19 -30 src/persondata.cpp M +14 -17 src/persondata.h M +1 -1 src/widgets/persondetailsview.cpp http://commits.kde.org/libkpeople/56ecc360e55baa997ee011f8cab2b701cdab0ba1 diff --git a/src/autotests/tests/persondatatests.cpp b/src/autotests/tests/= persondatatests.cpp index 623fb5f..6cf40be 100644 --- a/src/autotests/tests/persondatatests.cpp +++ b/src/autotests/tests/persondatatests.cpp @@ -134,69 +134,58 @@ void PersonDataTests::init() void PersonDataTests::contactProperties() { //create a simple contact with name + email - PersonData personData; - personData.setUri(m_contact1Uri.toString()); - QCOMPARE(personData.isValid(), true); - QCOMPARE(personData.uri(), m_contact1Uri.toString()); - QCOMPARE(personData.isPerson(), false); - QCOMPARE(personData.contactResources().size(), 1); - - QCOMPARE(personData.name(), QLatin1String("Contact 1")); - QCOMPARE(personData.emails(), QStringList() << QLatin1String("contact1= @example.com")); + QScopedPointer personData(PersonData::loadFromUri(m_contac= t1Uri)); + QCOMPARE(personData->isValid(), true); + QCOMPARE(personData->uri(), m_contact1Uri); + QCOMPARE(personData->isPerson(), false); + QCOMPARE(personData->contactResources().size(), 1); + + QCOMPARE(personData->name(), QLatin1String("Contact 1")); + QCOMPARE(personData->emails(), QStringList() << QLatin1String("contact= 1@example.com")); } = void PersonDataTests::personProperties() { - PersonData personData; - personData.setUri(m_personAUri.toString()); - QCOMPARE(personData.isValid(), true); - QCOMPARE(personData.isPerson(), true); - QCOMPARE(personData.contactResources().size(), 2); - QCOMPARE(personData.name(), QLatin1String("Person A")); - QCOMPARE(personData.emails(), QStringList() << QLatin1String("contact3= @example.com") << QLatin1String("contact2@example.com")); + QScopedPointer personData(PersonData::loadFromUri(m_person= AUri)); + QCOMPARE(personData->isValid(), true); + QCOMPARE(personData->isPerson(), true); + QCOMPARE(personData->contactResources().size(), 2); + QCOMPARE(personData->name(), QLatin1String("Person A")); + QCOMPARE(personData->emails(), QStringList() << QLatin1String("contact= 3@example.com") << QLatin1String("contact2@example.com")); } = void PersonDataTests::personFromContactID() { - PersonData personData; - personData.setContactId(QLatin1String("contact2@example.com")); + QScopedPointer personData(PersonData::loadFromContactId(QL= atin1String("contact2@example.com"))); //This should load PersonA NOT Contact2 - QCOMPARE(personData.uri(), m_personAUri.toString()); + QCOMPARE(personData->uri(), m_personAUri); } = void PersonDataTests::contactFromContactID() { - PersonData personData; - personData.setContactId(QLatin1String("contact1@example.com")); - QCOMPARE(personData.uri(), m_contact1Uri.toString()); + QScopedPointer personData(PersonData::loadFromContactId(QL= atin1String("contact1@example.com"))); + QCOMPARE(personData->uri(), m_contact1Uri); } = void PersonDataTests::miscTests() { - PersonData personData; - personData.uri(); - personData.name(); - personData.avatar(); - personData.birthday(); - personData.contactResources(); - personData.emails(); - personData.groups(); - personData.imAccounts(); - personData.isPerson(); - personData.phones(); - QCOMPARE(personData.isValid(), false); - - PersonData test2; - test2.setContactId(QLatin1String("IDThatDoesNotExist")); - QCOMPARE(test2.isValid(), false); - - test2.name(); + QScopedPointer personData(PersonData::loadFromContactId(QL= atin1String("NOTEXIST"))); + personData->uri(); + personData->name(); + personData->avatar(); + personData->birthday(); + personData->contactResources(); + personData->emails(); + personData->groups(); + personData->imAccounts(); + personData->isPerson(); + personData->phones(); + QCOMPARE(personData->isValid(), false); } = void PersonDataTests::contactChanged() { - PersonData personData; - personData.setUri(m_contact1Uri.toString()); + QScopedPointer personData(PersonData::loadFromUri(m_contac= t1Uri)); = Nepomuk2::SimpleResourceGraph graph; = @@ -213,7 +202,7 @@ void PersonDataTests::contactChanged() job->exec(); QVERIFY(!job->error()); = - QVERIFY(QTest::kWaitForSignal(&personData, SIGNAL(dataChanged()), 5000= )); + QVERIFY(QTest::kWaitForSignal(personData.data(), SIGNAL(dataChanged())= , 5000)); = = QCoreApplication::processEvents(); @@ -223,7 +212,7 @@ void PersonDataTests::contactChanged() = //for some reason this fails... //FIXME investigate - QCOMPARE(personData.emails().size(), 2); + QCOMPARE(personData->emails().size(), 2); } = = diff --git a/src/declarative/CMakeLists.txt b/src/declarative/CMakeLists.txt index 9b33b85..815615c 100644 --- a/src/declarative/CMakeLists.txt +++ b/src/declarative/CMakeLists.txt @@ -5,7 +5,10 @@ include_directories( ${CMAKE_BINARY_DIR} ) = -kde4_add_library(kpeopledeclarative SHARED declarativepersonsmodel.cpp peo= pleqmlplugin.cpp) +kde4_add_library(kpeopledeclarative SHARED + declarativepersonsmodel.cpp + declarativepersondata.cpp + peopleqmlplugin.cpp) = target_link_libraries(kpeopledeclarative ${QT_QTCORE_LIBRARY} ${QT_QTGUI_L= IBRARY} ${QT_QTDECLARATIVE_LIBRARY} ${KDE4_KDECORE_LIBRARY} kpeople) = diff --git a/src/declarative/declarativepersondata.cpp b/src/declarative/de= clarativepersondata.cpp new file mode 100644 index 0000000..718e8b4 --- /dev/null +++ b/src/declarative/declarativepersondata.cpp @@ -0,0 +1,56 @@ +/* + + Copyright (C) 2013 David Edmundson + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +#include "declarativepersondata.h" + +#include + +DeclarativePersonData::DeclarativePersonData(QObject* parent) + :KPeople::PersonData(parent) +{ + +} + +void DeclarativePersonData::classBegin() +{ + +} + +void DeclarativePersonData::componentComplete() +{ + if (!m_uri.isEmpty()) { + loadUri(m_uri); + } else if (!m_contactId.isEmpty()) { + loadContact(m_contactId); + } else { + kWarning() << "item has no uri or contactId set"; + } +} + +void DeclarativePersonData::setContactId(const QString& contactId) +{ + m_contactId =3D contactId; +} + +void DeclarativePersonData::setUri(const QString& uri) +{ + m_uri =3D uri; +} + + diff --git a/src/declarative/declarativepersondata.h b/src/declarative/decl= arativepersondata.h new file mode 100644 index 0000000..72c6fec --- /dev/null +++ b/src/declarative/declarativepersondata.h @@ -0,0 +1,50 @@ +/* + + Copyright (C) 2013 David Edmundson + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +#ifndef DECLARATIVEPERSONDATA_H +#define DECLARATIVEPERSONDATA_H + +#include "persondata.h" + +#include + +class DeclarativePersonData : public KPeople::PersonData, public QDeclarat= iveParserStatus +{ + Q_OBJECT + Q_INTERFACES(QDeclarativeParserStatus) + + Q_PROPERTY(QString uri WRITE setUri); + Q_PROPERTY(QString contactId WRITE setContactId); + +public: + DeclarativePersonData(QObject *parent=3D0); + + void setUri(const QString &uri); + void setContactId(const QString &uri); + + + void classBegin(); + void componentComplete(); + +private: + QString m_uri; + QString m_contactId; +}; + +#endif // DECLARATIVEPERSONDATA_H diff --git a/src/declarative/peopleqmlplugin.cpp b/src/declarative/peopleqm= lplugin.cpp index c991064..4bd4b45 100644 --- a/src/declarative/peopleqmlplugin.cpp +++ b/src/declarative/peopleqmlplugin.cpp @@ -24,6 +24,7 @@ #include = #include "declarativepersonsmodel.h" +#include "declarativepersondata.h" = #include = @@ -31,7 +32,7 @@ void PeopleQMLPlugin::registerTypes(const char* uri) { qmlRegisterType(uri, 0, 1, "PersonsModel"); qmlRegisterType(uri, 0, 1, "PersonActions= "); - qmlRegisterType(uri, 0, 1, "PersonData"); + qmlRegisterType(uri, 0, 1, "PersonData"); qmlRegisterType(); = qRegisterMetaType("QModelIndex"); diff --git a/src/examples/personwidget.cpp b/src/examples/personwidget.cpp index bc2eaf2..595ccf2 100644 --- a/src/examples/personwidget.cpp +++ b/src/examples/personwidget.cpp @@ -35,11 +35,10 @@ int main(int argc, char** argv) return 1; } = - KPeople::PersonData person; - person.setContactId(app.arguments()[1]); + QScopedPointer person(KPeople::PersonData::loadFr= omContactId(app.arguments()[1])); = KPeople::PersonDetailsView w; - w.setPerson(&person); + w.setPerson(person.data()); = w.show(); = diff --git a/src/personactionsmodel.cpp b/src/personactionsmodel.cpp index 4580062..5dcd25c 100644 --- a/src/personactionsmodel.cpp +++ b/src/personactionsmodel.cpp @@ -69,8 +69,7 @@ void PersonActionsModel::setPerson(const QPersistentModel= Index& index) if (d->person) { d->person->deleteLater(); } - d->person =3D new PersonData(this); - d->person->setUri(index.data(PersonsModel::UriRole).toString()); + d->person =3D PersonData::loadFromUri(index.data(PersonsModel::UriRole= ).toString(), this); = d->actions.append(PersonPluginManager::actionsForPerson(d->person, thi= s)); = diff --git a/src/persondata.cpp b/src/persondata.cpp index 2341bd4..e564efa 100644 --- a/src/persondata.cpp +++ b/src/persondata.cpp @@ -39,6 +39,7 @@ #include = #include + #include = using namespace Nepomuk2::Vocabulary; @@ -55,8 +56,7 @@ public: q->connect(dataSourceWatcher, SIGNAL(contactChanged(QUrl)), q, SIG= NAL(dataChanged())); } = - QString uri; - QString id; + QUrl uri; QPointer watcher; DataSourceWatcher *dataSourceWatcher; Nepomuk2::Resource personResource; @@ -64,17 +64,24 @@ public: }; } = -PersonData::PersonData(QObject *parent) - : QObject(parent), - d_ptr(new PersonDataPrivate(this)) +PersonData* PersonData::loadFromContactId(const QString& contactId, QObjec= t* parent) { + PersonData *person =3D new PersonData(parent); + person->loadContact(contactId); + return person; } = -PersonData::PersonData(const QString &uri, QObject *parent) +PersonData* PersonData::loadFromUri(const QUrl& uri, QObject* parent) +{ + PersonData *person =3D new PersonData(parent); + person->loadUri(uri); + return person; +} + +PersonData::PersonData(QObject *parent) : QObject(parent), - d_ptr(new PersonDataPrivate(this)) + d_ptr(new PersonDataPrivate(this)) { - setUri(uri); } = PersonData::~PersonData() @@ -82,14 +89,9 @@ PersonData::~PersonData() delete d_ptr; } = -void PersonData::setContactId(const QString &id) +void PersonData::loadContact(const QString &id) { Q_D(PersonData); - if (d->id =3D=3D id) { - return; - } - - d->id =3D id; = QString query =3D QString::fromUtf8( "select DISTINCT ?uri " @@ -108,37 +110,25 @@ void PersonData::setContactId(const QString &id) } = if (d->uri !=3D uri) { - setUri(uri); + loadUri(uri); } } = -QString PersonData::contactId() const -{ - Q_D(const PersonData); - return d->id; -} - -QString PersonData::uri() const +QUrl PersonData::uri() const { Q_D(const PersonData); return d->uri; } = -void PersonData::setUri(const QString &uri) +void PersonData::loadUri(const QUrl &uri) { Q_D(PersonData); = d->uri =3D uri; - d->contactResources.clear(); d->personResource =3D Nepomuk2::Resource(); - d->dataSourceWatcher->clearWatchedContacts(); = Nepomuk2::Resource r(uri); = - if (!d->watcher.isNull()) { - delete d->watcher; - } - d->watcher =3D new Nepomuk2::ResourceWatcher(this); = if (r.type() =3D=3D PIMO::Person()) { @@ -166,7 +156,6 @@ void PersonData::setUri(const QString &uri) } } = - emit uriChanged(); emit dataChanged(); } = diff --git a/src/persondata.h b/src/persondata.h index 177f245..86ea74d 100644 --- a/src/persondata.h +++ b/src/persondata.h @@ -37,7 +37,6 @@ struct PersonDataPrivate; class KPEOPLE_EXPORT PersonData : public QObject { Q_OBJECT - Q_PROPERTY(QString uri READ uri WRITE setUri NOTIFY uriChanged) Q_PROPERTY(QUrl avatar READ avatar NOTIFY dataChanged) Q_PROPERTY(QString name READ name NOTIFY dataChanged) Q_PROPERTY(QString status READ status NOTIFY dataChanged) @@ -45,26 +44,19 @@ class KPEOPLE_EXPORT PersonData : public QObject Q_PROPERTY(QStringList imAccounts READ imAccounts NOTIFY dataChanged) Q_PROPERTY(QStringList phones READ phones NOTIFY dataChanged) Q_PROPERTY(bool isPerson READ isPerson) + Q_PROPERTY(bool isValid READ isValid) = public: - PersonData(QObject *parent =3D 0); - - PersonData(const QString &uri, QObject *parent =3D 0); + static PersonData* loadFromUri(const QUrl &url, QObject *parent=3D= 0); + static PersonData* loadFromContactId(const QString &contactId, QOb= ject *parent=3D0); = virtual ~PersonData(); = - /** @returns the uri of the current person */ - QString uri() const; - - /** sets new contact uri, all data are refetched */ - void setUri(const QString &uri); - /** Returns if the URI represents a valid person or contact*/ bool isValid() const; = - /** @p id will specify the person we're offering by finding the pi= mo:Person related to it */ - void setContactId(const QString &id); - QString contactId() const; + /** @returns the uri of the current person */ + QUrl uri() const; = /** @returns a url pointing to the avatar image */ QUrl avatar() const; @@ -95,18 +87,23 @@ class KPEOPLE_EXPORT PersonData : public QObject QList contactResources() const; = Q_SIGNALS: - /** the person has changed */ - void uriChanged(); - /** Some of the person's data we're offering has changed */ void dataChanged(); = + protected: + PersonData(QObject *parent); + + /** sets new contact uri, all data are refetched */ + void loadUri(const QUrl &uri); + + /** @p id will specify the person we're offering by finding the pi= mo:Person related to it */ + void loadContact(const QString &id); + private: Q_DECLARE_PRIVATE(PersonData) PersonDataPrivate * d_ptr; = QString findMostOnlinePresence(const QStringList &presences) const; - }; } = diff --git a/src/widgets/persondetailsview.cpp b/src/widgets/persondetailsv= iew.cpp index f655eb2..6a1e8c3 100644 --- a/src/widgets/persondetailsview.cpp +++ b/src/widgets/persondetailsview.cpp @@ -120,7 +120,7 @@ PersonDetailsView::PersonDetailsView(QWidget *parent) = m_mainLayout->addSpacerItem(new QSpacerItem(32, 32, QSizePolicy::Minim= um, QSizePolicy::Expanding)); = - m_person =3D new PersonData(this); + m_person =3D 0; = setLayout(m_mainLayout); }