Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SrainBuffer and its derived class #73

Merged
merged 14 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Change Log
- Fixed crashing while connecting from connect popover
- Fixed use after free while removing user
- Improved the performance and extensibility of user list
- Improved compatibility with older versions of GTK(> 3.16)
- Refactor the code of chat panel, helpful for the next development

2017-09-12 Version 0.06.2
=========================
Expand Down
22 changes: 10 additions & 12 deletions src/core/chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ static void add_message(Chat *chat, Message *msg);
static bool whether_merge_last_message(Chat *chat, Message *msg);

Chat *chat_new(Server *srv, const char *name){
bool ischan;
SrnRet ret;
Chat *chat;
SuiSessionFlag flag;

g_return_val_if_fail(name, NULL);

ischan = sirc_is_chan(name);
chat = g_malloc0(sizeof(Chat));

chat->joined = FALSE;
Expand All @@ -63,22 +61,22 @@ Chat *chat_new(Server *srv, const char *name){
ERR_FR("Read sui prefs failed: %s", RET_MSG(ret));
}

flag = ischan ? SUI_SESSION_CHANNEL : SUI_SESSION_DIALOG;
if (strcmp(META_SERVER, name) == 0){
flag = SUI_SESSION_SERVER;
}

flag = 0;
chat->ui = sui_new_session(&ui_events, chat->ui_prefs, flag);

if (!chat->ui){
goto cleanup;
}

sui_set_ctx(chat->ui, chat);
sui_start_session(chat->ui, name, srv->prefs->name);

/* For a dialog, its user_list must have yourself and the dialogue target */
if (flag == SUI_SESSION_DIALOG){
if (strcmp(META_SERVER, name) == 0){
sui_server_session(chat->ui, srv->prefs->name);
// Server
} else if (sirc_is_chan(name)){
// Channel
sui_channel_session(chat->ui, srv->chat->ui, name);
} else {
// Private, its user_list must have yourself and the dialogue target
sui_private_session(chat->ui, srv->chat->ui, name);
chat_add_user(chat, srv->user->nick, USER_CHIGUA);
chat_add_user(chat, name, USER_CHIGUA);
}
Expand Down
10 changes: 6 additions & 4 deletions src/inc/sui/sui.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ void sui_proc_pending_event();
/* SuiSession */
SuiSession *sui_new_session(SuiEvents *events, SuiPrefs *prefs, SuiSessionFlag flag);
void sui_free_session(SuiSession *sui);
int sui_start_session(SuiSession *sui, const char *name, const char *remark);
SrnRet sui_server_session(SuiSession *sui, const char *srv);
SrnRet sui_channel_session(SuiSession *sui, SuiSession *sui_srv, const char *chan);
SrnRet sui_private_session(SuiSession *sui, SuiSession *sui_srv, const char *nick);
void sui_end_session(SuiSession *sui);

SuiSessionFlag sui_get_flag(SuiSession *sui);
Expand Down Expand Up @@ -94,9 +96,9 @@ void sui_add_completion(SuiSession *sui, const char *word);
void sui_rm_completion(SuiSession *sui, const char *word);

/* User */
int sui_add_user(SuiSession *sui, const char *nick, UserType type);
int sui_rm_user(SuiSession *sui, const char *nick);
int sui_ren_user(SuiSession *sui, const char *old_nick, const char *new_nick, UserType type);
SrnRet sui_add_user(SuiSession *sui, const char *nick, UserType type);
SrnRet sui_rm_user(SuiSession *sui, const char *nick);
SrnRet sui_ren_user(SuiSession *sui, const char *old_nick, const char *new_nick, UserType type);

/* Misc */
void sui_set_topic(SuiSession *sui, const char *topic);
Expand Down
2 changes: 1 addition & 1 deletion src/inc/sui/sui_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define SUI_EVENT_PARAM_STRINGS "^a&s"

typedef enum {
SUI_EVENT_OPEN,
SUI_EVENT_OPEN = 0,
SUI_EVENT_ACTIVATE,
SUI_EVENT_CONNECT,
SUI_EVENT_DISCONNECT,
Expand Down
28 changes: 2 additions & 26 deletions src/sui/glade/chat.glade → src/sui/glade/buffer.glade
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<!-- Generated with glade 3.20.2 -->
<interface>
<requires lib="gtk+" version="3.16"/>
<object class="GtkMenu" id="menu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="toggle_topic_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Topic</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="toggle_user_list_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_User list</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="close_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Close</property>
<property name="use_underline">True</property>
</object>
</child>
</object>
<object class="GtkImage" id="upload_image_button_image">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="icon_name">srain-photo</property>
</object>
<template class="SrainChat" parent="GtkBox">
<template class="SrainBuffer" parent="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
Expand Down
41 changes: 41 additions & 0 deletions src/sui/glade/buffer_menu.glade
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.2 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkMenuItem" id="close_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Close</property>
<property name="use_underline">True</property>
</object>
<object class="GtkMenuItem" id="disconn_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Disconnect</property>
<property name="use_underline">True</property>
</object>
<object class="GtkMenuItem" id="leave_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Leave</property>
<property name="use_underline">True</property>
</object>
<object class="GtkMenuItem" id="quit_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Quit</property>
<property name="use_underline">True</property>
</object>
<object class="GtkCheckMenuItem" id="topic_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Show _Topic</property>
<property name="use_underline">True</property>
</object>
<object class="GtkCheckMenuItem" id="user_list_menu_item">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Show _User List</property>
<property name="use_underline">True</property>
</object>
</interface>
3 changes: 2 additions & 1 deletion src/sui/glade/srain.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<gresources>
<gresource prefix="/org/gtk/srain">
<file preprocess="xml-stripblanks">window.glade</file>
<file preprocess="xml-stripblanks">chat.glade</file>
<file preprocess="xml-stripblanks">buffer.glade</file>
<file preprocess="xml-stripblanks">buffer_menu.glade</file>
<file preprocess="xml-stripblanks">sys_msg.glade</file>
<file preprocess="xml-stripblanks">send_msg.glade</file>
<file preprocess="xml-stripblanks">recv_msg.glade</file>
Expand Down
73 changes: 46 additions & 27 deletions src/sui/nick_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,38 @@
#include "sui_event_hdr.h"
#include "srain_app.h"
#include "srain_window.h"
#include "srain_chat.h"
#include "srain_buffer.h"
#include "srain_server_buffer.h"
#include "srain_chat_buffer.h"
#include "srain_channel_buffer.h"

#include "log.h"

static void nick_menu_item_on_activate(GtkWidget* widget, gpointer user_data){
const char *nick;
GVariantDict *params;
SrainChat *chat;
SrainBuffer *buffer;

chat = srain_window_get_cur_chat(srain_win);
buffer = srain_window_get_cur_buffer(srain_win);
nick = user_data;

params = g_variant_dict_new(NULL);
g_variant_dict_insert(params, "nick", SUI_EVENT_PARAM_STRING, nick);

if (strcmp(gtk_widget_get_name(widget), "whois_menu_item") == 0){
sui_event_hdr(srain_chat_get_session(chat), SUI_EVENT_WHOIS, params);
sui_event_hdr(srain_buffer_get_session(buffer), SUI_EVENT_WHOIS, params);
}
else if (strcmp(gtk_widget_get_name(widget), "ignore_menu_item") == 0){
sui_event_hdr(srain_chat_get_session(chat), SUI_EVENT_IGNORE, params);
sui_event_hdr(srain_buffer_get_session(buffer), SUI_EVENT_IGNORE, params);
}
else if (strcmp(gtk_widget_get_name(widget), "kick_menu_item") == 0){
sui_event_hdr(srain_chat_get_session(chat), SUI_EVENT_KICK, params);
sui_event_hdr(srain_buffer_get_session(buffer), SUI_EVENT_KICK, params);
}
else if (strcmp(gtk_widget_get_name(widget), "chat_menu_item") == 0){
sui_event_hdr(srain_chat_get_session(chat), SUI_EVENT_QUERY, params);
sui_event_hdr(srain_buffer_get_session(buffer), SUI_EVENT_QUERY, params);
}
else if (strcmp(gtk_widget_get_name(widget), "invite_submenu_item") == 0){
sui_event_hdr(srain_chat_get_session(chat), SUI_EVENT_INVITE, params);
sui_event_hdr(srain_buffer_get_session(buffer), SUI_EVENT_INVITE, params);
}
else {
ERR_FR("Unknown menu item: %s", gtk_widget_get_name(widget));
Expand All @@ -60,16 +63,19 @@ static void nick_menu_item_on_activate(GtkWidget* widget, gpointer user_data){
}

void nick_menu_popup(GdkEventButton *event, const char *nick){
GList *chats;
int n;
GSList *lst;
GtkBuilder *builder;
GtkMenu *nick_menu;
GtkMenuItem *item;
GtkMenuItem *whois_menu_item;
GtkMenuItem *ignore_menu_item;
GtkMenuItem *kick_menu_item;
GtkMenuItem *chat_menu_item;
GtkMenuItem *invite_menu_item;
SrainChat *chat;
GtkMenu *invite_submenu;
SrainBuffer *buffer;

LOG_FR("%s", nick);

builder = gtk_builder_new_from_resource ("/org/gtk/srain/nick_menu.glade");

Expand All @@ -89,33 +95,46 @@ void nick_menu_popup(GdkEventButton *event, const char *nick){
g_signal_connect(chat_menu_item, "activate",
G_CALLBACK(nick_menu_item_on_activate), (char *)nick);

/******************************************/
chat = srain_window_get_cur_chat(srain_win);
chats = srain_window_get_chats_by_remark(srain_win,
srain_chat_get_remark(chat));

/* Skip META_SERVER */
chats = g_list_next(chats);

/* Create subitem of invite_menu_item */
GtkMenu *invite_submenu = GTK_MENU(gtk_menu_new());
gtk_menu_item_set_submenu(invite_menu_item, GTK_WIDGET(invite_submenu));
buffer = srain_window_get_cur_buffer(srain_win);
if (SRAIN_IS_CHAT_BUFFER(buffer)){
buffer = SRAIN_BUFFER(srain_chat_buffer_get_server_buffer(
SRAIN_CHAT_BUFFER(buffer)));
}
if (!SRAIN_IS_SERVER_BUFFER(buffer)){
goto FIN;
}

while (chats){
item = GTK_MENU_ITEM(gtk_menu_item_new_with_label(
srain_chat_get_name(chats->data)));
n = 0;
invite_submenu = GTK_MENU(gtk_menu_new());
lst = srain_server_buffer_get_buffer_list(SRAIN_SERVER_BUFFER(buffer));
while (lst){
GtkMenuItem *item;

if (!SRAIN_IS_CHANNEL_BUFFER(lst->data)){
lst = g_slist_next(lst);
continue;
}
item = GTK_MENU_ITEM(gtk_menu_item_new_with_label(
srain_buffer_get_name(lst->data)));
gtk_widget_show(GTK_WIDGET(item));
gtk_widget_set_name(GTK_WIDGET(item), "invite_submenu_item");
g_signal_connect(item, "activate",
G_CALLBACK(nick_menu_item_on_activate), (char *)nick);
gtk_menu_shell_append(GTK_MENU_SHELL(invite_submenu), GTK_WIDGET(item));

chats = g_list_next(chats);
n++;
lst = g_slist_next(lst);
}
g_list_free(chats);

if (n > 0) {
gtk_menu_item_set_submenu(invite_menu_item, GTK_WIDGET(invite_submenu));
} else {
g_object_ref_sink(invite_submenu); // remove the floating reference
g_object_unref(invite_submenu);
}
FIN:
gtk_menu_popup(nick_menu, NULL, NULL, NULL, NULL,
event->button, event->time);

g_object_unref(builder);
}
1 change: 0 additions & 1 deletion src/sui/srain_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#define __SRAIN_APP_H

#include <gtk/gtk.h>
#include "srain_chat.h"

#define SRAIN_TYPE_APP (srain_app_get_type())
#define SRAIN_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), SRAIN_TYPE_APP, SrainApp))
Expand Down
Loading