Skip to content

Commit

Permalink
VENOM-302: Fix ObservableList
Browse files Browse the repository at this point in the history
* Add libgee as dependency to avoid using glib.list
  • Loading branch information
naxuroqa committed Apr 8, 2018
1 parent 5620826 commit 8dd8e1b
Show file tree
Hide file tree
Showing 20 changed files with 205 additions and 207 deletions.
3 changes: 2 additions & 1 deletion src/core/Logger.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Logger.vala
*
* Copyright (C) 2013-2018 Venom authors and contributors
* Copyright (C) 2013-2018 Venom authors and contributors
*
* This file is part of Venom.
*
Expand Down Expand Up @@ -126,6 +126,7 @@ namespace Venom {
GLib.Log.set_handler(null, LogLevelFlags.LEVEL_MASK, glib_log_function);
GLib.Log.set_handler("GLib", LogLevelFlags.LEVEL_MASK, glib_log_function);
GLib.Log.set_handler("GLib-GObject", LogLevelFlags.LEVEL_MASK, glib_log_function);
GLib.Log.set_handler("Gdk", LogLevelFlags.LEVEL_MASK, glib_log_function);
}

public static void log(LogLevel level, string message) {
Expand Down
51 changes: 27 additions & 24 deletions src/core/ObservableList.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ObservableList.vala
*
* Copyright (C) 2018 Venom authors and contributors
* Copyright (C) 2018 Venom authors and contributors
*
* This file is part of Venom.
*
Expand All @@ -20,53 +20,56 @@
*/

namespace Venom {
public class ObservableList<T> : GLib.Object {
public signal void added(T item, uint index);
public signal void removed(T item, uint index);
public class ObservableList : GLib.Object {
public signal void added(GLib.Object item, uint index);
public signal void removed(GLib.Object item, uint index);

private GLib.List<T> list;
private Gee.List<GLib.Object> list = new Gee.ArrayList<GLib.Object>();

public void set_list(owned GLib.List<T> list) {
this.list = (owned) list;
public void set_list(GLib.List<GLib.Object> list) {
foreach (var item in list) {
this.list.add(item);
}
}

public void append(owned T item) {
var idx = list.length();
list.append(item);
public void append(GLib.Object item) {
var idx = list.size;
list.add(item);
added(item, idx);
}

public void remove(T item) {
var idx = list.index(item);
list.remove(item);
public void remove(GLib.Object item) {
var idx = list.index_of(item);
removed(item, idx);
list.remove_at(idx);
}

public uint length() {
return list.length();
return list.size;
}

public uint index(T item) {
return list.index(item);
public uint index(GLib.Object item) {
return (uint) list.index_of(item);
}
public unowned T nth_data(uint index) {
return list.nth_data(index);

public GLib.Object nth_data(uint index) {
return list.@get((int) index);
}
}

public class ObservableListModel<T> : GLib.Object, GLib.ListModel {
private unowned ObservableList<T> list;
public ObservableListModel(ObservableList<T> list) {
public class ObservableListModel : GLib.Object, GLib.ListModel {
private ObservableList list;
public ObservableListModel(ObservableList list) {
this.list = list;
list.added.connect(on_added);
list.removed.connect(on_removed);
}

private void on_added(T item, uint index) {
private void on_added(GLib.Object item, uint index) {
items_changed(index, 0, 1);
}

private void on_removed(T item, uint index) {
private void on_removed(GLib.Object item, uint index) {
items_changed(index, 1, 0);
}

Expand All @@ -75,7 +78,7 @@ namespace Venom {
}

public virtual GLib.Type get_item_type() {
return typeof (T);
return typeof (GLib.Object);
}

public virtual uint get_n_items() {
Expand Down
2 changes: 1 addition & 1 deletion src/db/SqliteSettingsDatabase.vala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace Venom {

private static string STATEMENT_INSERT_SETTINGS =
"INSERT OR REPLACE INTO Settings (id, darktheme, animations, logging, urgencynotification, tray, notify, infinitelog, sendtyping, daystolog, enableproxy, enablecustomproxy, customproxyhost, customproxyport)"
+ " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);".printf(TABLE_ID,
+ " VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);".printf(TABLE_ID,
TABLE_DARKTHEME,
TABLE_ANIMATIONS,
TABLE_LOGGING,
Expand Down
3 changes: 2 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gtk_dep = dependency('gtk+-3.0')
gdk_dep = dependency('gdk-3.0')
gio_dep = dependency('gio-2.0')
glib_dep = dependency('glib-2.0')
gee_dep = dependency('gee-0.8')
gmodule_dep = dependency('gmodule-2.0')
sqlite_dep = dependency('sqlite3')
json_dep = dependency('json-glib-1.0')
Expand Down Expand Up @@ -97,6 +98,6 @@ venom_icons_resources = gnome.compile_resources( 'venom_icons_resources', 'icons
c_name : 'c_venom_icons_res')

venom_binary = executable('venom', [venom_source, venom_ui_resources, venom_icons_resources],
dependencies : [gtk_dep, gio_dep, gmodule_dep, sqlite_dep, json_dep, tox_dep, toxav_dep, config_dep, soup_dep],
dependencies : [gtk_dep, gio_dep, gmodule_dep, gee_dep, sqlite_dep, json_dep, tox_dep, toxav_dep, config_dep, soup_dep],
install : true
)
127 changes: 127 additions & 0 deletions src/testing/TestObservableList.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* TestObservableList.vala
*
* Copyright (C) 2018 Venom authors and contributors
*
* This file is part of Venom.
*
* Venom 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.
*
* Venom 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 Venom. If not, see <http://www.gnu.org/licenses/>.
*/

namespace TestGenerics {
private class SimpleObject : GLib.Object {
public static int count = 0;

public SimpleObject() {
stdout.printf("SimpleObject\n");
count++;
}

~SimpleObject() {
stdout.printf("~SimpleObject\n");
count--;
}
}

private class Container : GLib.Object {
private GLib.List<SimpleObject> list;
construct {
list = new GLib.List<SimpleObject>();
}

public void add(SimpleObject item) {
list.append(item);
}
}

private class GenericContainer<G> : GLib.Object {
private Gee.List<G> list;
construct {
list = new Gee.ArrayList<G>();
}

public void add(G item) {
if (typeof(G).is_object()) {
var o = ((GLib.Object)item);
o.ref();
o.ref();
}
list.add(item);
}

public void add_list(GLib.List<G> list) {
foreach (var item in list) {
this.list.add(item);
if (typeof(G).is_object()) {
var o = ((GLib.Object)item);
o.ref();
o.ref();
}
}
}

~GenericContainer() {
// Vala generics bug
if (typeof(G).is_object()) {
foreach (var o in list) {
((GLib.Object)o).unref();
}
}
}
}

private static void test_container() {
assert(SimpleObject.count == 0);
{
var list = new Container();
list.add(new SimpleObject());
assert(SimpleObject.count == 1);
}
assert(SimpleObject.count == 0);
}

private static void test_generic_container() {
assert(SimpleObject.count == 0);
{
var list = new GenericContainer<SimpleObject>();
list.add(new SimpleObject());
assert(SimpleObject.count == 1);
}
assert(SimpleObject.count == 0);
}

private static void test_generic_container_list_add() {
assert(SimpleObject.count == 0);
{
var container = new GenericContainer<SimpleObject>();
{
var list = new GLib.List<SimpleObject>();
list.append(new SimpleObject());
container.add_list(list);
assert(SimpleObject.count == 1);
}
assert(SimpleObject.count == 1);
}
assert(SimpleObject.count == 0);
}

private static int main(string[] args) {
Test.init(ref args);
Test.add_func("/test_container", test_container);
Test.add_func("/test_generic_container", test_generic_container);
Test.add_func("/test_generic_container_list_add", test_generic_container_list_add);
Test.run();
return 0;
}
}
6 changes: 6 additions & 0 deletions src/testing/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ test_glib_testing = executable('test_glib_testing', ['TestGlibTesting.vala'],
dependencies : [gio_dep]
)

test_observable_list = executable('test_observable_list', ['TestObservableList.vala',
join_paths(root_source_dir, 'core/ObservableList.vala')],
dependencies : [gio_dep, gee_dep]
)

test_mock = executable('test_mock', ['TestMock.vala', 'MockFramework.vala'],
dependencies : [gio_dep]
)
Expand Down Expand Up @@ -115,6 +120,7 @@ test_plugin = executable('test_plugin', ['TestPlugin.vala', 'Mock.vala', 'MockFr
test('test glib', test_glib_testing)
test('test mocking framework', test_mock)
test('test tox core vapi', test_tox_core)
test('test observable list', test_observable_list)
test('test venom config', test_venom_config)
test('test contact', test_contact)
test('test about', test_about)
Expand Down
4 changes: 0 additions & 4 deletions src/tox/DhtNode.vala
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,5 @@ namespace Venom {
this.maintainer = maintainer;
this.location = location;
}

~DhtNode() {
stdout.printf("~DhtNode()\n");
}
}
}
8 changes: 4 additions & 4 deletions src/tox/ToxAdapterConferenceListener.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ namespace Venom {
public class ToxAdapterConferenceListenerImpl : ToxAdapterConferenceListener, ConferenceWidgetListener, ConferenceInfoWidgetListener, CreateGroupchatWidgetListener, GLib.Object {
private unowned ToxSession session;
private ILogger logger;
private ObservableList<IContact> contacts;
private ObservableList contacts;
private NotificationListener notification_listener;
private GLib.HashTable<IContact, ObservableList<IMessage>> conversations;
private GLib.HashTable<IContact, ObservableList> conversations;

private GLib.HashTable<uint32, IContact> conferences;

public ToxAdapterConferenceListenerImpl(ILogger logger, ObservableList<IContact> contacts, GLib.HashTable<IContact, ObservableList<IMessage>> conversations, NotificationListener notification_listener) {
public ToxAdapterConferenceListenerImpl(ILogger logger, ObservableList contacts, GLib.HashTable<IContact, ObservableList> conversations, NotificationListener notification_listener) {
logger.d("ToxAdapterConferenceListenerImpl created.");
this.logger = logger;
this.contacts = contacts;
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace Venom {
var contact = new GroupchatContact(conference_number, title);
contacts.append(contact);
conferences.@set(conference_number, contact);
var conversation = new ObservableList<IMessage>();
var conversation = new ObservableList();
conversation.set_list(new GLib.List<IMessage>());
conversations.@set(contact, conversation);
}
Expand Down
4 changes: 2 additions & 2 deletions src/tox/ToxAdapterFiletransferListener.vala
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace Venom {
private unowned GLib.HashTable<uint32, IContact> friends;
private GLib.HashTable<uint32, GLib.HashTable<uint32, FileTransfer> > file_transfers;

private ObservableList<FileTransfer> transfers;
private ObservableList transfers;

public ToxAdapterFiletransferListenerImpl(ILogger logger, ObservableList<FileTransfer> transfers, NotificationListener notification_listener) {
public ToxAdapterFiletransferListenerImpl(ILogger logger, ObservableList transfers, NotificationListener notification_listener) {
logger.d("ToxAdapterFiletransferListenerImpl created.");
this.logger = logger;
this.notification_listener = notification_listener;
Expand Down
8 changes: 4 additions & 4 deletions src/tox/ToxAdapterFriendListener.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ namespace Venom {
public class ToxAdapterFriendListenerImpl : ToxAdapterFriendListener, AddContactWidgetListener, ConversationWidgetListener, FriendInfoWidgetListener, FriendRequestWidgetListener, GLib.Object {
private unowned ToxSession session;
private ILogger logger;
private ObservableList<IContact> contacts;
private ObservableList contacts;
private NotificationListener notification_listener;
private GLib.HashTable<IContact, ObservableList<IMessage>> conversations;
private GLib.HashTable<IContact, ObservableList> conversations;
private GLib.HashTable<uint32, Message> messages_waiting_for_rr;

private unowned GLib.HashTable<uint32, IContact> friends;
private GLib.HashTable<string, IContact> friend_requests;

public bool show_typing { get; set; }

public ToxAdapterFriendListenerImpl(ILogger logger, ObservableList<IContact> contacts, GLib.HashTable<IContact, ObservableList<IMessage>> conversations, NotificationListener notification_listener) {
public ToxAdapterFriendListenerImpl(ILogger logger, ObservableList contacts, GLib.HashTable<IContact, ObservableList> conversations, NotificationListener notification_listener) {
logger.d("ToxAdapterFriendListenerImpl created.");
this.logger = logger;
this.contacts = contacts;
Expand Down Expand Up @@ -172,7 +172,7 @@ namespace Venom {
}

if (!conversations.contains(contact)) {
var conversation = new ObservableList<IMessage>();
var conversation = new ObservableList();
conversation.set_list(new GLib.List<IMessage>());
conversations.@set(contact, conversation);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ui/application_window.ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.3 -->
<!-- Generated with glade 3.22.0 -->
<interface domain="venom">
<requires lib="gtk+" version="3.20"/>
<template class="VenomApplicationWindow" parent="GtkApplicationWindow">
Expand Down Expand Up @@ -32,6 +32,7 @@
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="transition_type">crossfade</property>
<property name="transition_duration">200</property>
<property name="reveal_child">True</property>
</object>
</child>
Expand Down

0 comments on commit 8dd8e1b

Please sign in to comment.