Tue, 07 Jan 2025 04:49:09 -0600
Make sure we notify on the n-items property for all objects that have it
Testing Done:
Called in the turtles.
Reviewed at https://reviews.imfreedom.org/r/3736/
| 42844 | 1 | /* |
| 2 | * Purple - Internet Messaging Library | |
| 3 | * Copyright (C) Pidgin Developers <devel@pidgin.im> | |
| 4 | * | |
| 5 | * Purple is the legal property of its developers, whose names are too numerous | |
| 6 | * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 7 | * source distribution. | |
| 8 | * | |
| 9 | * This library is free software; you can redistribute it and/or modify it | |
| 10 | * under the terms of the GNU General Public License as published by the Free | |
| 11 | * Software Foundation; either version 2 of the License, or (at your option) | |
| 12 | * any later version. | |
| 13 | * | |
| 14 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
| 17 | * more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU General Public License along with | |
| 20 | * this library; if not, see <https://www.gnu.org/licenses/>. | |
| 21 | */ | |
| 22 | ||
| 23 | #include "purplemessages.h" | |
| 24 | ||
| 25 | enum { | |
| 26 | PROP_0, | |
| 27 | PROP_CONVERSATION, | |
| 28 | PROP_ITEM_TYPE, | |
| 29 | PROP_N_ITEMS, | |
| 30 | N_PROPERTIES, | |
| 31 | }; | |
| 32 | static GParamSpec *properties[N_PROPERTIES] = {NULL, }; | |
| 33 | ||
| 34 | struct _PurpleMessages { | |
| 35 | GObject parent; | |
| 36 | ||
| 37 | PurpleConversation *conversation; | |
| 38 | ||
| 39 | GListStore *model; | |
| 40 | }; | |
| 41 | ||
| 42 | /****************************************************************************** | |
| 43 | * Helpers | |
| 44 | *****************************************************************************/ | |
| 45 | static void | |
| 46 | purple_messages_set_conversation(PurpleMessages *messages, | |
| 47 | PurpleConversation *conversation) | |
| 48 | { | |
| 49 | g_return_if_fail(PURPLE_IS_MESSAGES(messages)); | |
| 50 | ||
| 51 | if(g_set_object(&messages->conversation, conversation)) { | |
| 52 | g_object_notify_by_pspec(G_OBJECT(messages), | |
| 53 | properties[PROP_CONVERSATION]); | |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | static int | |
| 58 | purple_messages_compare(gconstpointer a, gconstpointer b, | |
| 59 | G_GNUC_UNUSED gpointer data) | |
| 60 | { | |
| 61 | PurpleMessage *message1 = PURPLE_MESSAGE((gpointer)a); | |
| 62 | PurpleMessage *message2 = PURPLE_MESSAGE((gpointer)b); | |
| 63 | ||
| 64 | return purple_message_compare_timestamp(message1, message2); | |
| 65 | } | |
| 66 | ||
| 67 | /****************************************************************************** | |
| 68 | * Callbacks | |
| 69 | *****************************************************************************/ | |
| 70 | static void | |
|
43128
1ce3ad90614c
Make sure we notify on the n-items property for all objects that have it
Gary Kramlich <grim@reaperworld.com>
parents:
42984
diff
changeset
|
71 | purple_messages_items_changed_cb(GListModel *model, guint position, |
|
1ce3ad90614c
Make sure we notify on the n-items property for all objects that have it
Gary Kramlich <grim@reaperworld.com>
parents:
42984
diff
changeset
|
72 | guint removed, guint added, gpointer data) |
| 42844 | 73 | { |
| 74 | g_list_model_items_changed(data, position, removed, added); | |
|
43128
1ce3ad90614c
Make sure we notify on the n-items property for all objects that have it
Gary Kramlich <grim@reaperworld.com>
parents:
42984
diff
changeset
|
75 | |
|
1ce3ad90614c
Make sure we notify on the n-items property for all objects that have it
Gary Kramlich <grim@reaperworld.com>
parents:
42984
diff
changeset
|
76 | if(removed > 0 || added > 0) { |
|
1ce3ad90614c
Make sure we notify on the n-items property for all objects that have it
Gary Kramlich <grim@reaperworld.com>
parents:
42984
diff
changeset
|
77 | g_object_notify_by_pspec(G_OBJECT(model), properties[PROP_N_ITEMS]); |
|
1ce3ad90614c
Make sure we notify on the n-items property for all objects that have it
Gary Kramlich <grim@reaperworld.com>
parents:
42984
diff
changeset
|
78 | } |
| 42844 | 79 | } |
| 80 | ||
| 81 | /****************************************************************************** | |
| 82 | * GListModel Implementation | |
| 83 | *****************************************************************************/ | |
| 84 | static GType | |
| 85 | purple_messages_get_item_type(GListModel *model) { | |
| 86 | PurpleMessages *messages = PURPLE_MESSAGES(model); | |
| 87 | ||
| 88 | return g_list_model_get_item_type(G_LIST_MODEL(messages->model)); | |
| 89 | } | |
| 90 | ||
| 91 | static guint | |
| 92 | purple_messages_get_n_items(GListModel *model) { | |
| 93 | PurpleMessages *messages = PURPLE_MESSAGES(model); | |
| 94 | ||
| 95 | return g_list_model_get_n_items(G_LIST_MODEL(messages->model)); | |
| 96 | } | |
| 97 | ||
| 98 | static gpointer | |
| 99 | purple_messages_get_item(GListModel *model, guint position) { | |
| 100 | PurpleMessages *messages = PURPLE_MESSAGES(model); | |
| 101 | ||
| 102 | return g_list_model_get_item(G_LIST_MODEL(messages->model), position); | |
| 103 | } | |
| 104 | ||
| 105 | static void | |
| 106 | purple_messages_list_model_iface_init(GListModelInterface *iface) { | |
| 107 | iface->get_item_type = purple_messages_get_item_type; | |
| 108 | iface->get_n_items = purple_messages_get_n_items; | |
| 109 | iface->get_item = purple_messages_get_item; | |
| 110 | } | |
| 111 | ||
| 112 | /****************************************************************************** | |
| 113 | * GObject Implementation | |
| 114 | *****************************************************************************/ | |
| 115 | G_DEFINE_FINAL_TYPE_WITH_CODE( | |
| 116 | PurpleMessages, | |
| 117 | purple_messages, | |
| 118 | G_TYPE_OBJECT, | |
| 119 | G_IMPLEMENT_INTERFACE(G_TYPE_LIST_MODEL, | |
| 120 | purple_messages_list_model_iface_init)) | |
| 121 | ||
| 122 | static void | |
| 123 | purple_messages_dispose(GObject *obj) { | |
| 124 | PurpleMessages *message = PURPLE_MESSAGES(obj); | |
| 125 | ||
| 126 | g_clear_object(&message->conversation); | |
| 127 | ||
| 128 | g_list_store_remove_all(message->model); | |
| 129 | ||
| 130 | G_OBJECT_CLASS(purple_messages_parent_class)->dispose(obj); | |
| 131 | } | |
| 132 | ||
| 133 | static void | |
| 134 | purple_messages_finalize(GObject *obj) { | |
| 135 | PurpleMessages *message = PURPLE_MESSAGES(obj); | |
| 136 | ||
| 137 | g_clear_object(&message->model); | |
| 138 | ||
| 139 | G_OBJECT_CLASS(purple_messages_parent_class)->finalize(obj); | |
| 140 | } | |
| 141 | ||
| 142 | static void | |
| 143 | purple_messages_get_property(GObject *obj, guint param_id, GValue *value, | |
| 144 | GParamSpec *pspec) | |
| 145 | { | |
| 146 | PurpleMessages *messages = PURPLE_MESSAGES(obj); | |
| 147 | ||
| 148 | switch(param_id) { | |
| 149 | case PROP_CONVERSATION: | |
| 150 | g_value_set_object(value, purple_messages_get_conversation(messages)); | |
| 151 | break; | |
| 152 | case PROP_ITEM_TYPE: | |
| 153 | g_value_set_gtype(value, | |
| 154 | g_list_model_get_item_type(G_LIST_MODEL(messages->model))); | |
| 155 | break; | |
| 156 | case PROP_N_ITEMS: | |
| 157 | g_value_set_uint(value, | |
| 158 | g_list_model_get_n_items(G_LIST_MODEL(messages->model))); | |
| 159 | break; | |
| 160 | default: | |
| 161 | G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); | |
| 162 | break; | |
| 163 | } | |
| 164 | } | |
| 165 | ||
| 166 | static void | |
| 167 | purple_messages_set_property(GObject *obj, guint param_id, const GValue *value, | |
| 168 | GParamSpec *pspec) | |
| 169 | { | |
| 170 | PurpleMessages *messages = PURPLE_MESSAGES(obj); | |
| 171 | ||
| 172 | switch(param_id) { | |
| 173 | case PROP_CONVERSATION: | |
| 174 | purple_messages_set_conversation(messages, g_value_get_object(value)); | |
| 175 | break; | |
| 176 | default: | |
| 177 | G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); | |
| 178 | break; | |
| 179 | } | |
| 180 | } | |
| 181 | ||
| 182 | static void | |
| 183 | purple_messages_init(PurpleMessages *messages) { | |
| 184 | messages->model = g_list_store_new(PURPLE_TYPE_MESSAGE); | |
| 185 | g_signal_connect_object(messages->model, "items-changed", | |
| 186 | G_CALLBACK(purple_messages_items_changed_cb), | |
| 187 | messages, G_CONNECT_DEFAULT); | |
| 188 | } | |
| 189 | ||
| 190 | static void | |
| 191 | purple_messages_class_init(PurpleMessagesClass *klass) { | |
| 192 | GObjectClass *obj_class = G_OBJECT_CLASS(klass); | |
| 193 | ||
| 194 | obj_class->dispose = purple_messages_dispose; | |
| 195 | obj_class->finalize = purple_messages_finalize; | |
| 196 | obj_class->get_property = purple_messages_get_property; | |
| 197 | obj_class->set_property = purple_messages_set_property; | |
| 198 | ||
| 199 | /** | |
| 200 | * PurpleMessages:conversation: | |
| 201 | * | |
| 202 | * The [class@Conversation] that these messages belong to. | |
| 203 | * | |
| 204 | * Since: 3.0 | |
| 205 | */ | |
| 206 | properties[PROP_CONVERSATION] = g_param_spec_object( | |
| 207 | "conversation", NULL, NULL, | |
| 208 | PURPLE_TYPE_CONVERSATION, | |
| 209 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS); | |
| 210 | ||
| 211 | /** | |
| 212 | * PurpleMessages:item-type: | |
| 213 | * | |
|
42984
818220289e67
Update the gi-docgen subproject and fix some new issues it found
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
214 | * The type of items. See [vfunc@Gio.ListModel.get_item_type]. |
| 42844 | 215 | * |
| 216 | * Since: 3.0 | |
| 217 | */ | |
| 218 | properties[PROP_ITEM_TYPE] = g_param_spec_gtype( | |
| 219 | "item-type", NULL, NULL, | |
| 220 | PURPLE_TYPE_MESSAGE, | |
| 221 | G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); | |
| 222 | ||
| 223 | /** | |
| 224 | * PurpleMessages:n-items: | |
| 225 | * | |
|
42984
818220289e67
Update the gi-docgen subproject and fix some new issues it found
Gary Kramlich <grim@reaperworld.com>
parents:
42844
diff
changeset
|
226 | * The number of items. See [vfunc@Gio.ListModel.get_n_items]. |
| 42844 | 227 | * |
| 228 | * Since: 3.0 | |
| 229 | */ | |
| 230 | properties[PROP_N_ITEMS] = g_param_spec_uint( | |
| 231 | "n-items", NULL, NULL, | |
| 232 | 0, G_MAXUINT, 0, | |
| 233 | G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); | |
| 234 | ||
| 235 | g_object_class_install_properties(obj_class, N_PROPERTIES, properties); | |
| 236 | } | |
| 237 | ||
| 238 | /****************************************************************************** | |
| 239 | * Public API | |
| 240 | *****************************************************************************/ | |
| 241 | PurpleMessages * | |
| 242 | purple_messages_new(PurpleConversation *conversation) { | |
| 243 | g_return_val_if_fail(PURPLE_IS_CONVERSATION(conversation), NULL); | |
| 244 | ||
| 245 | return g_object_new( | |
| 246 | PURPLE_TYPE_MESSAGES, | |
| 247 | "conversation", conversation, | |
| 248 | NULL); | |
| 249 | } | |
| 250 | ||
| 251 | PurpleConversation * | |
| 252 | purple_messages_get_conversation(PurpleMessages *messages) { | |
| 253 | g_return_val_if_fail(PURPLE_IS_MESSAGES(messages), NULL); | |
| 254 | ||
| 255 | return messages->conversation; | |
| 256 | } | |
| 257 | ||
| 258 | void | |
| 259 | purple_messages_add(PurpleMessages *messages, PurpleMessage *message) { | |
| 260 | g_return_if_fail(PURPLE_IS_MESSAGES(messages)); | |
| 261 | g_return_if_fail(PURPLE_IS_MESSAGE(message)); | |
| 262 | ||
| 263 | g_list_store_insert_sorted(messages->model, message, | |
| 264 | (GCompareDataFunc)purple_messages_compare, NULL); | |
| 265 | } |