Initial fix for bufferlist, needs more robust implementation. Update moment.js to work in Qt 5.2

This commit is contained in:
Mikko Ahlroth 2014-11-03 23:19:37 +02:00
parent f0cbff637e
commit 53593bbf87
3 changed files with 915 additions and 458 deletions

View file

@ -5,7 +5,7 @@
*/
/*
*
* This module handles the internal buffer list and buffer related events of the client.
*/
.pragma library
@ -17,6 +17,49 @@
.import "debug.js" as D
.import "weechatcolors.js" as WC
// Change line into an object insertable into a QML ListModel
function lineToModelItem(moment, prefix, str) {
return {
time: moment.format('HH:mm:ss'),
prefix: prefix,
str: str
};
}
// Create a generic QML ListModel
function createListModel(parent) {
if (typeof parent === 'undefined') {
parent = _parentPage;
}
return Qt.createQmlObject('import QtQuick 2.0; ListModel {}',
parent);
}
// Dict of buffers, pointer as key, for fast referencing
var _buffers = {};
// List of all buffers for easy iteration, will be sorted ascending
// according to buffer number whenever requested
var _buffersList = [];
// ListModel for BufferList
var _buffersListModel = null;
// Call this function whenever the buffer list needs to be refreshed
// completely
var _bufferListRefreshCallback = null;
// Call this function whenever a single buffer has been changed
// (no need to refresh)
var _bufferChangedCallback = null;
// Page that will own all of our textlistmodels
var _parentPage = null;
// A class for storing the information of a single buffer
function Buffer(pointer, number, name, title) {
this.pointer = pointer;
this.number = number;
@ -27,26 +70,45 @@ function Buffer(pointer, number, name, title) {
// Amount of unread lines in this buffer
this.unreadCount = 0;
// Has this buffer had its initial sync done?
// (Has it ever been active?)
this.inited = false;
// A listmodel where we put the messages of this buffer
this.textList = Qt.createQmlObject('import QtQuick 2.0; ListModel {}',
_parentPage);
this.textList = createListModel();
// Add line to this buffer
this.add = function(moment, prefix, str) {
this.textList.insert(0, {
time: moment.format('HH:mm:ss'),
prefix: prefix,
str: str
});
this.textList.insert(0, lineToModelItem(moment, prefix, str));
if (!this.active) {
++this.unreadCount;
_callChangeCallback(this);
}
};
// Replace the line contents of this buffer with the given history
this.addHistory = function(history) {
this.textList.clear();
lineAdded(history);
}
// This buffer has become visible to the user
this.activate = function() {
if (!this.inited && this.pointer !== '0') {
var self = this;
var lines = this.textList.count;
EQ.command('hdata buffer:0x' + this.pointer + '/own_lines/last_line(-' + (lines + 100) + ')/data', function(result) {
var lines = result[0].objectSets;
lines.reverse();
handleLines(lines);
});
}
this.active = true;
this.inited = true;
this.unreadCount = 0;
}
@ -61,15 +123,7 @@ function Buffer(pointer, number, name, title) {
};
}
// Dict of buffers, pointer as key, for fast referencing
var _buffers = {};
// List of all buffers for easy iteration, will be sorted ascending
// according to buffer number whenever requested
var _buffersList = [];
// Page that will own all of our textlistmodels
var _parentPage = null;
function init() {
@ -87,13 +141,23 @@ function setDebugPage(page) {
var buffer = new Buffer(pointer, 1, 'weechat', 'Debug/core');
_buffers[pointer] = buffer;
_buffersList.push(buffer);
_buffersListModel.append({
pointer: pointer,
number: 1,
name: 'weechat',
unreadCount: 0
});
_callRefreshCallback();
D.setDebugBuffer(buffer);
page.changeBuffer(buffer);
}
function getBuffer(i) {
if (_buffers.hasOwnProperty(i)) {
return _buffers[i];
// Get a single buffer by pointer
function getBuffer(pointer) {
if (_buffers.hasOwnProperty(pointer)) {
return _buffers[pointer];
}
return null;
@ -104,6 +168,15 @@ function getBuffersByNumber() {
return _buffersList;
}
// If bufferlistmodel was not created yet, create it and return
function getBufferListModel(parent) {
if (_buffersListModel === null) {
_buffersListModel = createListModel(parent);
}
return _buffersListModel;
}
function connected() {
D.d("Fetching all buffers...");
@ -116,7 +189,7 @@ function disconnected() {
}
// Handle incoming buffer list data
function bufferListData(data) {
data = data[0];
var buffers = data.objectSets;
@ -146,21 +219,37 @@ function bufferListData(data) {
buffer = new Buffer(pointer, number, name, title);
_buffers[pointer] = buffer;
_buffersList.push(buffer);
_buffersListModel.append({
pointer: pointer,
number: number,
name: name,
unreadCount: 0
});
}
}
_sortBuffersList();
_callRefreshCallback();
}
// A new line was added to a buffer
function lineAdded(data) {
data = data[0].objectSets[0];
// Handle a single line
function handleLine(line) {
var date = M.moment(line.date);
var prefix = WC.codedText2StyledText(WC.parseString(line.prefix));
var message = WC.codedText2StyledText(WC.parseString(line.message));
_buffers[line.buffer].add(date, prefix, message);
}
var buffer = data.buffer;
var date = M.moment(data.date);
var prefix = WC.codedText2StyledText(WC.parseString(data.prefix));
var message = WC.codedText2StyledText(WC.parseString(data.message));
_buffers[buffer].add(date, prefix, message);
// Handle a list of lines
function handleLines(lines) {
for (var i = 0; i < lines.length; ++i) {
handleLine(lines[i]);
}
}
// New lines were added to a buffer
function lineAdded(data) {
handleLines(data[0].objectSets);
}
function _sortBuffersList() {
@ -168,3 +257,15 @@ function _sortBuffersList() {
return a.number - b.number;
});
}
function _callChangeCallback(o) {
if (!!_bufferChangedCallback) {
_bufferChangedCallback(o);
}
}
function _callRefreshCallback() {
if (!!_bufferListRefreshCallback) {
_bufferListRefreshCallback();
}
}

File diff suppressed because it is too large Load diff

View file

@ -14,26 +14,15 @@ Page {
property BufferView bufferViewPage : null;
onVisibleChanged: bufferList.updateList();
SilicaListView {
id: bufferList
model: ListModel { id: bufferListModel }
anchors.fill: parent
function updateList() {
bufferListModel.clear();
model: ListModel {}
var buffers = B.getBuffersByNumber();
for (var i = 0; i < buffers.length; ++i) {
bufferListModel.append({
pointer: buffers[i].pointer,
number: buffers[i].number,
name: buffers[i].name,
unreadCount: buffers[i].unreadCount
});
}
Component.onCompleted: {
// Get bufferlistmodel from buffers.js
bufferList.model = B.getBufferListModel(bufferListPage);
}
header: PageHeader {