diff --git a/js/md.js b/js/md.js index d8155ea..ceae2ef 100644 --- a/js/md.js +++ b/js/md.js @@ -1,44 +1,7 @@ -/* jshint browser: true, devel: true */ +.pragma library -/** - * preg_replace (from PHP) in JavaScript! - * - * This is basically a pattern replace. You can use a regex pattern to search and - * another for the replace. For more information see the PHP docs on the original - * function (http://php.net/manual/en/function.preg-replace.php), and for more on - * JavaScript flavour regex visit http://www.regular-expressions.info/javascript.html - * - * NOTE: Unlike the PHP version, this function only deals with string inputs. No arrays. - * - * @author William Duyck - * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License 2.0 - * - * @param {String} pattern The pattern to search for. - * @param {String} replace The string to replace. - * @param {String} subject The string to search and replace. - * @param {Integer} limit The maximum possible replacements. - * @return {String} If matches are found, the new subject will be returned. - */ var preg_replace=function(a,b,c,d){void 0===d&&(d=-1);var e=a.substr(a.lastIndexOf(a[0])+1),f=a.substr(1,a.lastIndexOf(a[0])-1),g=RegExp(f,e),i=[],j=0,k=0,l=c,m=[];if(-1===d){do m=g.exec(c),null!==m&&i.push(m);while(null!==m&&-1!==e.indexOf("g"))}else i.push(g.exec(c));for(j=i.length-1;j>-1;j--){for(m=b,k=i[j].length;k>-1;k--)m=m.replace("${"+k+"}",i[j][k]).replace("$"+k,i[j][k]).replace("\\"+k,i[j][k]);l=l.replace(i[j][0],m)}return l}; -/** - * Basic Markdown Parser - * - * This function parses a small subset of the Markdown language as defined by - * [John Gruber](http://daringfireball.net/projects/markdown). It's very basic - * and needs to be refactored a little, and there are plans to add more support - * for the rest of the language in the near future. - * - * This implimentation is based loosely on - * [slimdown.php](https://gist.github.com/jbroadway/2836900) by Johnny Broadway. - * - * @version 0.1 - * @author William Duyck - * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License 2.0 - * - * @param {String} str A Markdown string to be converted to HTML. - * @return {String} The HTML for the given Markdown. - */ var markdown_parser = function(str){ var rules = [ diff --git a/js/util.js b/js/util.js new file mode 100644 index 0000000..56d75da --- /dev/null +++ b/js/util.js @@ -0,0 +1,25 @@ +.pragma library + +function stringToColor(str) { + var hash = 0; + for (var i = 0; i < str.length; i++) { + hash = str.charCodeAt(i) + ((hash << 5) - hash); + } + var colour = '#'; + for (var j = 0; j < 3; j++) { + var value = (hash >> (j * 8)) & 0xFF; + colour += ('00' + value.toString(16)).substr(-2); + } + return colour; +} + +function pushToStack(stack, page) { + if(page && stack.currentItem !== page) { + if(stack.depth === 1) { + stack.replace(page) + } else { + stack.clear() + stack.push(page) + } + } +} diff --git a/qml/component/RoomDrawer.qml b/qml/component/RoomDrawer.qml index fe3ba39..2239150 100644 --- a/qml/component/RoomDrawer.qml +++ b/qml/component/RoomDrawer.qml @@ -4,6 +4,8 @@ import QtQuick.Controls.Material 2.2 import QtQuick.Layouts 1.3 import Matrique 0.1 +import "qrc:/js/util.js" as Util + Drawer { property var room @@ -94,6 +96,10 @@ Drawer { boundsBehavior: Flickable.DragOverBounds + model: UserListModel { + room: roomDrawer.room + } + delegate: ItemDelegate { width: parent.width height: 48 @@ -107,6 +113,7 @@ Drawer { Layout.preferredWidth: height Layout.fillHeight: true + defaultColor: Util.stringToColor(name) image: avatar hint: name } @@ -119,12 +126,6 @@ Drawer { } } - model: UserListModel { - id: userListModel - - room: roomDrawer.room - } - ScrollBar.vertical: ScrollBar {} } diff --git a/qml/component/SideNavButton.qml b/qml/component/SideNavButton.qml index 8c9bf27..9ec0978 100644 --- a/qml/component/SideNavButton.qml +++ b/qml/component/SideNavButton.qml @@ -3,6 +3,8 @@ import QtQuick.Controls 2.2 import QtQuick.Layouts 1.3 import QtQuick.Controls.Material 2.2 +import "qrc:/js/util.js" as Util + ItemDelegate { property var page readonly property bool selected: stackView.currentItem === page @@ -18,14 +20,5 @@ ItemDelegate { } } - onClicked: { - if(page && stackView.currentItem !== page) { - if(stackView.depth === 1) { - stackView.replace(page) - } else { - stackView.clear() - stackView.push(page) - } - } - } + onClicked: Util.pushToStack(stackView, page) } diff --git a/qml/form/RoomListForm.qml b/qml/form/RoomListForm.qml index 5af52d3..d134125 100644 --- a/qml/form/RoomListForm.qml +++ b/qml/form/RoomListForm.qml @@ -9,6 +9,7 @@ import SortFilterProxyModel 0.2 import Matrique.Settings 0.1 import "../component" +import "qrc:/js/util.js" as Util Item { property alias listModel: roomListProxyModel.sourceModel @@ -142,14 +143,6 @@ Item { spacing: 12 - // ImageStatus { - // Layout.preferredWidth: height - // Layout.fillHeight: true - - // source: avatar ? "image://mxc/" + avatar : "" - // displayText: name - // } - ImageItem { id: imageItem @@ -157,7 +150,7 @@ Item { Layout.fillHeight: true hint: name || "No Name" - defaultColor: stringToColor(name || "No Name") + defaultColor: Util.stringToColor(name || "No Name") image: avatar } @@ -224,17 +217,4 @@ Item { } } } - - function stringToColor(str) { - var hash = 0; - for (var i = 0; i < str.length; i++) { - hash = str.charCodeAt(i) + ((hash << 5) - hash); - } - var colour = '#'; - for (var j = 0; j < 3; j++) { - var value = (hash >> (j * 8)) & 0xFF; - colour += ('00' + value.toString(16)).substr(-2); - } - return colour; - } } diff --git a/qml/main.qml b/qml/main.qml index 2f0babc..b0dd9c1 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -290,7 +290,7 @@ ApplicationWindow { id: stackView - // initialItem: roomPage + initialItem: roomPage } } diff --git a/res.qrc b/res.qrc index 300bca8..cebc6a2 100644 --- a/res.qrc +++ b/res.qrc @@ -30,5 +30,6 @@ qml/component/StateDelegate.qml qml/component/AutoLabel.qml qml/component/RoomDrawer.qml + js/util.js diff --git a/src/accountlistmodel.cpp b/src/accountlistmodel.cpp index a66c40d..d9868c1 100644 --- a/src/accountlistmodel.cpp +++ b/src/accountlistmodel.cpp @@ -82,7 +82,7 @@ int AccountListModel::rowCount(const QModelIndex& parent) const { } void AccountListModel::connectConnectionSignals(Connection* conn) { - connect(conn->user(), &User::avatarChanged, [=] { + connect(conn->user(), &User::avatarChanged, this, [=] { const auto it = std::find(m_connections.begin(), m_connections.end(), conn); if (it == m_connections.end()) { return; diff --git a/src/controller.cpp b/src/controller.cpp index 8b64f3b..cb3310e 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -92,8 +92,8 @@ void Controller::addConnection(Connection* c) { m_connections.push_back(c); connect(c, &Connection::syncDone, this, [=] { - static int counter = 0; - if (++counter % 17 == 2) c->saveState(); + c->saveState(); + c->sync(30000); }); connect(c, &Connection::loggedOut, this, [=] { dropConnection(c); }); diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index 6959bad..3ab5b56 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -15,9 +15,9 @@ RoomListModel::~RoomListModel() {} void RoomListModel::setConnection(Connection* connection) { if (connection == m_connection) return; + m_connection->disconnect(this); if (!connection) { qDebug() << "Removing current connection..."; - m_connection->disconnect(this); m_connection = nullptr; beginResetModel(); m_rooms.clear(); @@ -25,9 +25,10 @@ void RoomListModel::setConnection(Connection* connection) { return; } - using QMatrixClient::Room; m_connection = connection; + for (MatriqueRoom* room : m_rooms) room->disconnect(this); + connect(connection, &Connection::connected, this, &RoomListModel::doResetModel); connect(connection, &Connection::invitedRoom, this, @@ -73,7 +74,7 @@ void RoomListModel::connectRoomSignals(MatriqueRoom* room) { [=] { refresh(room, {AvatarRole}); }); connect(room, &Room::addedMessages, this, [=] { refresh(room, {LastEventRole}); }); - connect(room, &QMatrixClient::Room::aboutToAddNewMessages, this, + connect(room, &Room::aboutToAddNewMessages, this, [=](QMatrixClient::RoomEventsRange eventsRange) { RoomEvent* event = (eventsRange.end() - 1)->get(); if (event->isStateEvent()) return;