Skip to content
Snippets Groups Projects
Commit ff9eaae3 authored by Emmanuel Lepage Vallee's avatar Emmanuel Lepage Vallee
Browse files

constact: Restore support to add/remove contact from history

Refs #69290
parent b6ac1c96
No related branches found
No related tags found
No related merge requests found
......@@ -402,7 +402,7 @@ Qt::ItemFlags CategorizedContactModel::flags( const QModelIndex& index ) const
const ContactTreeNode* modelNode = static_cast<ContactTreeNode*>(index.internalPointer());
return (modelNode->m_pContact && modelNode->m_pContact->isActive() ? Qt::NoItemFlags : Qt::ItemIsEnabled)
return (modelNode->m_pContact && modelNode->m_pContact->isActive() ? Qt::ItemIsEnabled : Qt::NoItemFlags )
| Qt::ItemIsSelectable
| (modelNode->m_pParent? (Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled) : Qt::ItemIsEnabled
);
......
......@@ -632,7 +632,6 @@ bool CategorizedHistoryModel::addItemCallback(const Call* item)
bool CategorizedHistoryModel::removeItemCallback(const Call* item)
{
Q_UNUSED(item)
emit const_cast<Call*>(item)->changed();
return false;
}
......
......@@ -34,10 +34,10 @@ public:
static ItemModelStateSerializationDelegate* instance();
//Getter
virtual bool isChecked(CollectionInterface* backend) const = 0;
virtual bool isChecked(const CollectionInterface* backend) const = 0;
//Setter
virtual bool setChecked(CollectionInterface* backend, bool enabled) = 0;
virtual bool setChecked(const CollectionInterface* backend, bool enabled) = 0;
private:
static ItemModelStateSerializationDelegate* m_spInstance;
......
......@@ -24,6 +24,7 @@
#include <QtCore/QHash>
#include <QtCore/QTimer>
#include <QtCore/QUrl>
#include <QtCore/QCryptographicHash>
#include <QtWidgets/QApplication>
#include <QtCore/QStandardPaths>
......@@ -47,8 +48,9 @@ public:
virtual bool addNew ( const Person* item ) override;
virtual bool addExisting( const Person* item ) override;
QVector<Person*> m_lItems;
QString m_Path ;
QVector<Person*> m_lItems;
QString m_Path ;
QHash<const Person*,QString> m_hPaths;
private:
virtual QVector<Person*> items() const override;
......@@ -74,6 +76,7 @@ FallbackPersonCollectionPrivate::FallbackPersonCollectionPrivate(FallbackPersonC
//Default to somewhere ~/.local/share
if (m_Path.isEmpty()) {
m_Path = (QStandardPaths::writableLocation(QStandardPaths::DataLocation)) + "/vCard/";
static_cast<FallbackPersonBackendEditor*>(q_ptr->editor<Person>())->m_Path = m_Path;
}
m_Name = path.split('/').last();
......@@ -95,6 +98,25 @@ FallbackPersonCollection::~FallbackPersonCollection()
bool FallbackPersonBackendEditor::save(const Person* item)
{
if (!item)
return false;
//An UID is required
if (item->uid().isEmpty()) {
QCryptographicHash hash(QCryptographicHash::Sha1);
for (ContactMethod* n : item->phoneNumbers())
hash.addData(n->uri().toLatin1());
hash.addData(item->formattedName().toLatin1());
QByteArray random;
for (int i=0;i<5;i++)
random.append(QChar((char)(rand()%255)));
hash.addData(random);
const_cast<Person*>(item)->setUid(hash.result().toHex());
}
QFile file(m_Path+'/'+item->uid()+".vcf");
file.open(QIODevice::WriteOnly);
file.write(item->toVCard({}));
......@@ -104,8 +126,21 @@ bool FallbackPersonBackendEditor::save(const Person* item)
bool FallbackPersonBackendEditor::remove(const Person* item)
{
Q_UNUSED(item)
return false;
if (!item)
return false;
QString path = m_hPaths[item];
if (path.isEmpty())
path = m_Path+'/'+item->uid()+".vcf";
bool ret = QFile::remove(path);
if (ret) {
ret &= mediator()->removeItem(item);
}
return ret;
}
bool FallbackPersonBackendEditor::edit( Person* item)
......@@ -116,8 +151,7 @@ bool FallbackPersonBackendEditor::edit( Person* item)
bool FallbackPersonBackendEditor::addNew(const Person* item)
{
Q_UNUSED(item)
return false;
return save(item);
}
bool FallbackPersonBackendEditor::addExisting(const Person* item)
......@@ -149,15 +183,15 @@ QVariant FallbackPersonCollection::icon() const
bool FallbackPersonCollection::isEnabled() const
{
return true;
return ItemModelStateSerializationDelegate::instance()->isChecked(this);
}
bool FallbackPersonCollection::load()
{
bool ok;
QList< Person* > ret = VCardUtils::loadDir(QUrl(d_ptr->m_Path),ok);
QList< Person* > ret = VCardUtils::loadDir(QUrl(d_ptr->m_Path),ok,static_cast<FallbackPersonBackendEditor*>(editor<Person>())->m_hPaths);
for(Person* p : ret) {
qDebug() << "add" << p->formattedName();
p->setCollection(this);
editor<Person>()->addExisting(p);
}
......@@ -179,7 +213,7 @@ CollectionInterface::SupportedFeatures FallbackPersonCollection::supportedFeatur
CollectionInterface::SupportedFeatures::LOAD |
CollectionInterface::SupportedFeatures::CLEAR |
CollectionInterface::SupportedFeatures::MANAGEABLE |
// CollectionInterface::SupportedFeatures::REMOVE|
CollectionInterface::SupportedFeatures::REMOVE |
CollectionInterface::SupportedFeatures::ADD );
}
......
......@@ -100,7 +100,7 @@ public:
//Mutator
Q_INVOKABLE void addAddress(Address* addr);
Q_INVOKABLE void addCustomField(const QString& key, const QString& value);
Q_INVOKABLE const QByteArray toVCard(QList<Account*> accounts) const;
Q_INVOKABLE const QByteArray toVCard(QList<Account*> accounts = {}) const;
protected:
//The D-Pointer can be shared if a PlaceHolderPerson is merged with a real one
......
......@@ -302,13 +302,15 @@ bool PersonModel::addItemCallback(const Person* c)
d_ptr->m_hPlaceholders[c->uid()] = nullptr;
}
}
return true;
}
bool PersonModel::removeItemCallback(const Person* item)
{
Q_UNUSED(item)
return false;
if (item)
emit const_cast<Person*>(item)->changed();
return item;
}
bool PersonModel::addPerson(Person* c)
......@@ -329,10 +331,20 @@ void PersonModel::disablePerson(Person* c)
bool PersonModel::addNewPerson(Person* c, CollectionInterface* backend)
{
if ((!backend) || (!collections().size()))
if ((!backend) && (!collections().size()))
return false;
return (backend?backend:collections()[0])->editor<Person>()->addNew(c);
bool ret = false;
if (backend) {
ret |= backend->editor<Person>()->addNew(c);
}
else for (Collection* col :collections(CollectionInterface::SupportedFeatures::ADD)) {
if (col->id() != "trcb") //Do not add to the transitional contact backend
ret |= col->editor<Person>()->addNew(c);
}
return ret;
}
......
......@@ -244,7 +244,7 @@ const QByteArray VCardUtils::endVCard()
return result.toUtf8();
}
QList< Person* > VCardUtils::loadDir (const QUrl& path, bool& ok)
QList< Person* > VCardUtils::loadDir (const QUrl& path, bool& ok, QHash<const Person*,QString>& paths)
{
QList< Person* > ret;
......@@ -257,6 +257,7 @@ QList< Person* > VCardUtils::loadDir (const QUrl& path, bool& ok)
Person* p = new Person();
mapToPerson(p,QUrl(dir.absoluteFilePath(file)));
ret << p;
paths[p] = dir.absoluteFilePath(file);
}
}
......
......@@ -79,7 +79,7 @@ public:
const QByteArray endVCard();
//Loading
static QList<Person*> loadDir(const QUrl& path, bool& ok);
static QList<Person*> loadDir(const QUrl& path, bool& ok, QHash<const Person*, QString>& paths);
//Mapping
static bool mapToPerson(Person* p, const QUrl& url, Account** a = nullptr);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment