2019-03-15 08:14:23 -03:00
# include "IRCAppWindow.h"
2019-03-15 21:15:19 -03:00
# include "IRCWindow.h"
# include "IRCWindowListModel.h"
2019-03-15 21:10:48 -03:00
# include <LibGUI/GApplication.h>
2019-03-15 12:12:51 -03:00
# include <LibGUI/GStackWidget.h>
2019-03-15 10:01:04 -03:00
# include <LibGUI/GTableView.h>
2019-03-15 08:14:23 -03:00
# include <LibGUI/GBoxLayout.h>
2019-03-15 19:24:40 -03:00
# include <LibGUI/GToolBar.h>
# include <LibGUI/GAction.h>
2019-03-15 21:10:48 -03:00
# include <LibGUI/GMenu.h>
# include <LibGUI/GMenuBar.h>
2019-03-15 19:24:40 -03:00
# include <stdio.h>
2019-03-15 08:14:23 -03:00
IRCAppWindow : : IRCAppWindow ( )
2019-03-15 22:14:53 -03:00
: m_client ( " 127.0.0.1 " , 6667 )
2019-03-15 08:14:23 -03:00
{
2019-03-15 14:02:38 -03:00
set_title ( String : : format ( " IRC Client: %s@%s:%d " , m_client . nickname ( ) . characters ( ) , m_client . hostname ( ) . characters ( ) , m_client . port ( ) ) ) ;
2019-03-15 08:14:23 -03:00
set_rect ( 200 , 200 , 600 , 400 ) ;
2019-03-15 21:10:48 -03:00
setup_actions ( ) ;
setup_menus ( ) ;
2019-03-15 08:14:23 -03:00
setup_widgets ( ) ;
2019-03-15 21:45:49 -03:00
setup_client ( ) ;
2019-03-15 08:14:23 -03:00
}
IRCAppWindow : : ~ IRCAppWindow ( )
{
}
void IRCAppWindow : : setup_client ( )
{
2019-03-15 22:14:53 -03:00
m_client . aid_create_window = [ this ] ( void * owner , IRCWindow : : Type type , const String & name ) {
2019-03-15 21:45:49 -03:00
return & create_window ( owner , type , name ) ;
2019-03-15 14:02:38 -03:00
} ;
2019-03-15 22:14:53 -03:00
m_client . aid_get_active_window = [ this ] {
return static_cast < IRCWindow * > ( m_container - > active_widget ( ) ) ;
} ;
m_client . aid_update_window_list = [ this ] {
m_window_list - > model ( ) - > update ( ) ;
} ;
2019-03-15 14:02:38 -03:00
2019-03-15 21:45:49 -03:00
m_client . on_connect = [ this ] {
m_client . join_channel ( " #test " ) ;
2019-03-15 14:02:38 -03:00
} ;
2019-03-15 08:14:23 -03:00
m_client . connect ( ) ;
}
2019-03-15 21:10:48 -03:00
void IRCAppWindow : : setup_actions ( )
2019-03-15 08:14:23 -03:00
{
2019-03-15 21:10:48 -03:00
m_join_action = GAction : : create ( " Join channel " , GraphicsBitmap : : load_from_file ( GraphicsBitmap : : Format : : RGBA32 , " /res/icons/16x16/irc-join.rgb " , { 16 , 16 } ) , [ ] ( auto & ) {
2019-03-15 19:24:40 -03:00
printf ( " FIXME: Implement join action \n " ) ;
} ) ;
2019-03-15 21:10:48 -03:00
m_part_action = GAction : : create ( " Part from channel " , GraphicsBitmap : : load_from_file ( GraphicsBitmap : : Format : : RGBA32 , " /res/icons/16x16/irc-part.rgb " , { 16 , 16 } ) , [ ] ( auto & ) {
2019-03-15 19:24:40 -03:00
printf ( " FIXME: Implement part action \n " ) ;
} ) ;
2019-03-15 21:10:48 -03:00
m_whois_action = GAction : : create ( " Whois user " , GraphicsBitmap : : load_from_file ( GraphicsBitmap : : Format : : RGBA32 , " /res/icons/16x16/irc-whois.rgb " , { 16 , 16 } ) , [ ] ( auto & ) {
2019-03-15 20:21:11 -03:00
printf ( " FIXME: Implement whois action \n " ) ;
} ) ;
2019-03-15 21:10:48 -03:00
m_open_query_action = GAction : : create ( " Open query " , GraphicsBitmap : : load_from_file ( GraphicsBitmap : : Format : : RGBA32 , " /res/icons/16x16/irc-open-query.rgb " , { 16 , 16 } ) , [ ] ( auto & ) {
2019-03-15 20:21:11 -03:00
printf ( " FIXME: Implement open-query action \n " ) ;
} ) ;
2019-03-15 21:10:48 -03:00
m_close_query_action = GAction : : create ( " Close query " , GraphicsBitmap : : load_from_file ( GraphicsBitmap : : Format : : RGBA32 , " /res/icons/16x16/irc-close-query.rgb " , { 16 , 16 } ) , [ ] ( auto & ) {
2019-03-15 20:21:11 -03:00
printf ( " FIXME: Implement close-query action \n " ) ;
} ) ;
2019-03-15 21:10:48 -03:00
}
void IRCAppWindow : : setup_menus ( )
{
auto menubar = make < GMenuBar > ( ) ;
auto app_menu = make < GMenu > ( " IRC Client " ) ;
app_menu - > add_action ( GAction : : create ( " Quit " , { Mod_Alt , Key_F4 } , [ ] ( const GAction & ) {
dbgprintf ( " Terminal: Quit menu activated! \n " ) ;
GApplication : : the ( ) . quit ( 0 ) ;
return ;
} ) ) ;
menubar - > add_menu ( move ( app_menu ) ) ;
auto server_menu = make < GMenu > ( " Server " ) ;
server_menu - > add_action ( * m_join_action ) ;
server_menu - > add_action ( * m_part_action ) ;
server_menu - > add_separator ( ) ;
server_menu - > add_action ( * m_whois_action ) ;
server_menu - > add_action ( * m_open_query_action ) ;
server_menu - > add_action ( * m_close_query_action ) ;
menubar - > add_menu ( move ( server_menu ) ) ;
auto help_menu = make < GMenu > ( " Help " ) ;
help_menu - > add_action ( GAction : : create ( " About " , [ ] ( const GAction & ) {
dbgprintf ( " FIXME: Implement Help/About \n " ) ;
} ) ) ;
menubar - > add_menu ( move ( help_menu ) ) ;
GApplication : : the ( ) . set_menubar ( move ( menubar ) ) ;
}
void IRCAppWindow : : setup_widgets ( )
{
auto * widget = new GWidget ( nullptr ) ;
set_main_widget ( widget ) ;
widget - > set_layout ( make < GBoxLayout > ( Orientation : : Vertical ) ) ;
2019-03-15 20:21:11 -03:00
2019-03-15 19:24:40 -03:00
auto * toolbar = new GToolBar ( widget ) ;
2019-03-15 21:10:48 -03:00
toolbar - > add_action ( * m_join_action ) ;
toolbar - > add_action ( * m_part_action . copy_ref ( ) ) ;
2019-03-15 20:21:11 -03:00
toolbar - > add_separator ( ) ;
2019-03-15 21:10:48 -03:00
toolbar - > add_action ( * m_whois_action ) ;
toolbar - > add_action ( * m_open_query_action ) ;
toolbar - > add_action ( * m_close_query_action ) ;
2019-03-15 19:24:40 -03:00
auto * horizontal_container = new GWidget ( widget ) ;
horizontal_container - > set_layout ( make < GBoxLayout > ( Orientation : : Horizontal ) ) ;
2019-03-15 22:14:53 -03:00
m_window_list = new GTableView ( horizontal_container ) ;
m_window_list - > set_headers_visible ( false ) ;
m_window_list - > set_alternating_row_colors ( false ) ;
m_window_list - > set_model ( OwnPtr < IRCWindowListModel > ( m_client . client_window_list_model ( ) ) ) ;
m_window_list - > set_size_policy ( SizePolicy : : Fixed , SizePolicy : : Fill ) ;
m_window_list - > set_preferred_size ( { 120 , 0 } ) ;
2019-03-15 21:15:19 -03:00
m_client . client_window_list_model ( ) - > on_activation = [ this ] ( IRCWindow & window ) {
2019-03-15 12:12:51 -03:00
m_container - > set_active_widget ( & window ) ;
2019-03-15 22:14:53 -03:00
window . clear_unread_count ( ) ;
2019-03-15 12:12:51 -03:00
} ;
2019-03-15 08:14:23 -03:00
2019-03-15 19:24:40 -03:00
m_container = new GStackWidget ( horizontal_container ) ;
2019-03-15 08:14:23 -03:00
2019-03-15 21:45:49 -03:00
create_window ( & m_client , IRCWindow : : Server , " Server " ) ;
2019-03-15 10:01:04 -03:00
}
2019-03-15 21:45:49 -03:00
IRCWindow & IRCAppWindow : : create_window ( void * owner , IRCWindow : : Type type , const String & name )
2019-03-15 09:07:04 -03:00
{
2019-03-15 21:45:49 -03:00
return * new IRCWindow ( m_client , owner , type , name , m_container ) ;
2019-03-15 08:14:23 -03:00
}