Skip to content

Commit

Permalink
Merge pull request #1 from QMatrixClient/master
Browse files Browse the repository at this point in the history
Merge to latest commit.
  • Loading branch information
Black Hat authored Sep 12, 2018
2 parents 79f3388 + 2a1596c commit 1e78ca6
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 102 deletions.
17 changes: 8 additions & 9 deletions lib/avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ QImage Avatar::Private::get(Connection* connection, QSize size,
if (callback)
callbacks.emplace_back(move(callback));
_thumbnailRequest = connection->getThumbnail(_url, size);
if (_originalImage.isNull() && !_defaultIcon.isNull())
{
_originalImage = QImage(_defaultIcon.actualSize(size),
QImage::Format_ARGB32_Premultiplied);
_originalImage.fill(Qt::transparent);
QPainter p { &_originalImage };
_defaultIcon.paint(&p, { QPoint(), _defaultIcon.actualSize(size) });
}
QObject::connect( _thumbnailRequest, &MediaThumbnailJob::success,
_thumbnailRequest, [this] {
_fetched = true;
Expand All @@ -138,15 +146,6 @@ QImage Avatar::Private::get(Connection* connection, QSize size,
});
}

if( _originalImage.isNull() )
{
if (_defaultIcon.isNull())
return _originalImage;

QPainter p { &_originalImage };
_defaultIcon.paint(&p, { QPoint(), _defaultIcon.actualSize(size) });
}

for (const auto& p: _scaledImages)
if (p.first == size)
return p.second;
Expand Down
12 changes: 7 additions & 5 deletions lib/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,14 +649,14 @@ ForgetRoomJob* Connection::forgetRoom(const QString& id)
forgetJob->start(connectionData());
connect(forgetJob, &BaseJob::success, this, [this, id]
{
// If the room is in the map (possibly in both forms), delete all forms.
// Delete whatever instances of the room are still in the map.
for (auto f: {false, true})
if (auto r = d->roomMap.take({ id, f }))
{
emit aboutToDeleteRoom(r);
qCDebug(MAIN) << "Room" << id
<< "in join state" << toCString(r->joinState())
qCDebug(MAIN) << "Room" << r->objectName()
<< "in state" << toCString(r->joinState())
<< "will be deleted";
emit r->beforeDestruction(r);
r->deleteLater();
}
});
Expand Down Expand Up @@ -995,6 +995,8 @@ Room* Connection::provideRoom(const QString& id, JoinState joinState)
}
d->roomMap.insert(roomKey, room);
d->firstTimeRooms.push_back(room);
connect(room, &Room::beforeDestruction,
this, &Connection::aboutToDeleteRoom);
emit newRoom(room);
}
if (joinState == JoinState::Invite)
Expand All @@ -1015,7 +1017,7 @@ Room* Connection::provideRoom(const QString& id, JoinState joinState)
if (prevInvite)
{
qCDebug(MAIN) << "Deleting Invite state for room" << prevInvite->id();
emit aboutToDeleteRoom(prevInvite);
emit prevInvite->beforeDestruction(prevInvite);
prevInvite->deleteLater();
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/converters.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ namespace QMatrixClient
auto operator()(const QJsonValue& jv) const { return jv.toDouble(); }
};

template <> struct FromJson<float>
{
auto operator()(const QJsonValue& jv) const { return float(jv.toDouble()); }
};

template <> struct FromJson<qint64>
{
auto operator()(const QJsonValue& jv) const { return qint64(jv.toDouble()); }
Expand Down
38 changes: 28 additions & 10 deletions lib/events/accountdataevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,40 @@ namespace QMatrixClient

struct TagRecord
{
TagRecord (QString order = {}) : order(std::move(order)) { }
explicit TagRecord(const QJsonValue& jv)
: order(jv.toObject().value("order"_ls).toString())
{ }
using order_type = Omittable<float>;

order_type order;

QString order;
TagRecord (order_type order = none) : order(order) { }
explicit TagRecord(const QJsonValue& jv)
{
// Parse a float both from JSON double and JSON string because
// libqmatrixclient previously used to use strings to store order.
const auto orderJv = jv.toObject().value("order"_ls);
if (orderJv.isDouble())
order = fromJson<float>(orderJv);
else if (orderJv.isString())
{
bool ok;
order = orderJv.toString().toFloat(&ok);
if (!ok)
order = none;
}
}

bool operator==(const TagRecord& other) const
{ return order == other.order; }
bool operator!=(const TagRecord& other) const
{ return !operator==(other); }
bool operator<(const TagRecord& other) const
{
// Per The Spec, rooms with no order should be after those with order
return !order.omitted() &&
(other.order.omitted() || order.value() < other.order.value());
}
};

inline QJsonValue toJson(const TagRecord& rec)
{
return QJsonObject {{ QStringLiteral("order"), rec.order }};
QJsonObject o;
addParam(o, QStringLiteral("order"), rec.order);
return o;
}

using TagsMap = QHash<QString, TagRecord>;
Expand Down
2 changes: 1 addition & 1 deletion lib/jobs/syncjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ SyncRoomData::SyncRoomData(const QString& roomId_, JoinState joinState_,
switch (joinState) {
case JoinState::Join:
ephemeral = load<Events>(room_, "ephemeral"_ls);
accountData = load<Events>(room_, "account_data"_ls);
FALLTHROUGH;
case JoinState::Leave:
{
accountData = load<Events>(room_, "account_data"_ls);
timeline = load<RoomEvents>(room_, "timeline"_ls);
const auto timelineJson = room_.value("timeline"_ls).toObject();
timelineLimited = timelineJson.value("limited"_ls).toBool();
Expand Down
74 changes: 29 additions & 45 deletions lib/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,13 @@ class Room::Private
*/
bool processRedaction(const RedactionEvent& redaction);

std::pair<TagsMap, QStringList> setTags(TagsMap newTags);
void broadcastTagUpdates(const TagsMap& additions,
const QStringList& removals)
void setTags(TagsMap newTags);
void sendTagUpdates()
{
connection->callApi<SetAccountDataPerRoomJob>(
connection->userId(), id, TagEvent::matrixTypeId(),
TagEvent(tags).contentJson());
emit q->tagsChanged(additions, removals);
emit q->tagsChanged();
}

QJsonObject toJson() const;
Expand Down Expand Up @@ -703,7 +702,7 @@ std::pair<bool, QString> validatedTag(QString name)
return { false, name };

qWarning(MAIN) << "The tag" << name
<< "doesn't follow the CS API conventions, check your client code";
<< "doesn't follow the CS API conventions";
name.prepend("u.");
qWarning(MAIN) << "Using " << name << "instead";

Expand All @@ -717,8 +716,10 @@ void Room::addTag(const QString& name, const TagRecord& record)
(checkRes.first && d->tags.contains(checkRes.second)))
return;

emit tagsAboutToChange();
d->tags.insert(checkRes.second, record);
d->broadcastTagUpdates({{ checkRes.second, record }}, {});
emit tagsChanged();
d->sendTagUpdates();
}

void Room::addTag(const QString& name, const QString& order)
Expand All @@ -728,43 +729,32 @@ void Room::addTag(const QString& name, const QString& order)

void Room::removeTag(const QString& name)
{
if (!d->tags.contains(name))
return;

d->tags.remove(name);
d->broadcastTagUpdates({}, {{ name }});
if (d->tags.contains(name))
{
emit tagsAboutToChange();
d->tags.remove(name);
emit tagsChanged();
d->sendTagUpdates();
} else if (!name.startsWith("u."))
removeTag("u." + name);
else
qWarning(MAIN) << "Tag" << name << "on room" << objectName()
<< "not found, nothing to remove";
}

void Room::setTags(TagsMap newTags)
{
const auto& changes = d->setTags(move(newTags));
d->broadcastTagUpdates(changes.first, changes.second);
d->setTags(move(newTags));
d->sendTagUpdates();
}

std::pair<TagsMap, QStringList> Room::Private::setTags(TagsMap newTags)
void Room::Private::setTags(TagsMap newTags)
{
if (newTags == tags)
return {};

TagsMap additions;
const auto& tagNames = newTags.keys();
for (const auto& t: tagNames)
{
const auto& checkRes = validatedTag(t);
const auto& value = checkRes.first ?
newTags.insert(checkRes.second, newTags.take(t)).value() :
newTags.value(checkRes.second);
if (!tags.contains(checkRes.second))
additions.insert(checkRes.second, value);
}

QStringList removals;
for (const auto& tag: tags.keys())
if (!newTags.contains(tag))
removals.push_back(tag);

tags = newTags;
return { additions, removals };
emit q->tagsAboutToChange();
tags = move(newTags);
qCDebug(MAIN) << "Room" << id << "is tagged with:"
<< q->tagNames().join(", ");
emit q->tagsChanged();
}

bool Room::isFavourite() const
Expand Down Expand Up @@ -1851,15 +1841,8 @@ void Room::processEphemeralEvent(EventPtr&& event)
void Room::processAccountDataEvent(EventPtr&& event)
{
if (auto* evt = eventCast<TagEvent>(event))
{
const auto& changes = d->setTags(evt->tags());
if (!(changes.first.empty() && changes.second.empty()))
{
qCDebug(MAIN) << "Room" << id() << "is tagged with:"
<< tagNames().join(", ");
emit tagsChanged(changes.first, changes.second);
}
}
d->setTags(evt->tags());

if (auto* evt = eventCast<ReadMarkerEvent>(event))
{
auto readEventId = evt->event_id();
Expand All @@ -1877,6 +1860,7 @@ void Room::processAccountDataEvent(EventPtr&& event)
// efficient; maaybe do it another day
if (!currentData || currentData->contentJson() != event->contentJson())
{
emit accountDataAboutToChange(event->matrixType());
currentData = move(event);
qCDebug(MAIN) << "Updated account data of type"
<< currentData->matrixType();
Expand Down
Loading

0 comments on commit 1e78ca6

Please sign in to comment.