1 /*
2  *  This file is part of GNOME Twitch - 'Enjoy Twitch on your GNU/Linux desktop'
3  *  Copyright © 2017 Vincent Szolnoky <vinszent@vinszent.com>
4  *
5  *  GNOME Twitch is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  GNOME Twitch is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with GNOME Twitch. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "gt-game-container-view.h"
20 #include "gt-top-game-container.h"
21 #include "gt-search-game-container.h"
22 #include "gt-game-channel-container.h"
23 #include "utils.h"
24 
25 typedef struct
26 {
27     GtTopGameContainer* top_container;
28     GtSearchGameContainer* search_container;
29     GtGameChannelContainer* game_container;
30 
31     GtkWidget* container_stack;
32 } GtGameContainerViewPrivate;
33 
34 G_DEFINE_TYPE_WITH_PRIVATE(GtGameContainerView, gt_game_container_view, GT_TYPE_CONTAINER_VIEW);
35 
36 static void
search_active_cb(GObject * obj,GParamSpec * pspec,gpointer udata)37 search_active_cb(GObject* obj,
38     GParamSpec* pspec, gpointer udata)
39 {
40     g_assert(GT_IS_GAME_CONTAINER_VIEW(udata));
41 
42     GtGameContainerView* self = GT_GAME_CONTAINER_VIEW(udata);
43     GtGameContainerViewPrivate* priv = gt_game_container_view_get_instance_private(self);
44     GtkWidget* container_stack = gt_container_view_get_container_stack(
45         GT_CONTAINER_VIEW(self));
46 
47     g_assert(GTK_IS_STACK(container_stack));
48 
49     if (gt_container_view_get_search_active(GT_CONTAINER_VIEW(self)))
50         gtk_stack_set_visible_child(GTK_STACK(container_stack), GTK_WIDGET(priv->search_container));
51     else
52     {
53         g_object_set(self, "show-back-button", FALSE, NULL);
54 
55         gtk_stack_set_visible_child(GTK_STACK(container_stack), GTK_WIDGET(priv->top_container));
56 
57         gtk_widget_set_sensitive(gt_container_view_get_search_bar(GT_CONTAINER_VIEW(self)), TRUE);
58     }
59 }
60 
61 static void
go_back(GtContainerView * view)62 go_back(GtContainerView* view)
63 {
64     g_assert(GT_IS_GAME_CONTAINER_VIEW(view));
65 
66     GtGameContainerView* self = GT_GAME_CONTAINER_VIEW(view);
67     GtGameContainerViewPrivate* priv = gt_game_container_view_get_instance_private(self);
68 
69     GtkWidget* container_stack = gt_container_view_get_container_stack(
70         GT_CONTAINER_VIEW(self));
71 
72     g_assert(GTK_IS_STACK(container_stack));
73 
74     if (gt_container_view_get_search_active(GT_CONTAINER_VIEW(self)))
75     {
76         GtkWidget* search_bar = gt_container_view_get_search_bar(GT_CONTAINER_VIEW(self));
77 
78         gtk_widget_set_sensitive(search_bar, TRUE);
79 
80         gtk_stack_set_visible_child(GTK_STACK(container_stack), GTK_WIDGET(priv->search_container));
81     }
82     else
83         gtk_stack_set_visible_child(GTK_STACK(container_stack), GTK_WIDGET(priv->top_container));
84 
85     g_object_set(self, "show-back-button", FALSE, NULL);
86 
87 }
88 
89 static void
refresh(GtContainerView * view)90 refresh(GtContainerView* view)
91 {
92     g_assert(GT_IS_GAME_CONTAINER_VIEW(view));
93 
94     GtGameContainerView* self = GT_GAME_CONTAINER_VIEW(view);
95     GtGameContainerViewPrivate* priv = gt_game_container_view_get_instance_private(self);
96 
97     GtkWidget* container = gtk_stack_get_visible_child(
98         GTK_STACK(gt_container_view_get_container_stack(view)));
99 
100     g_assert(GT_IS_ITEM_CONTAINER(container));
101 
102     gt_item_container_refresh(GT_ITEM_CONTAINER(container));
103 }
104 
105 static void
constructed(GObject * obj)106 constructed(GObject* obj)
107 {
108     GtGameContainerView* self = GT_GAME_CONTAINER_VIEW(obj);
109     GtGameContainerViewPrivate* priv = gt_game_container_view_get_instance_private(self);
110     GtkWidget* search_entry = gt_container_view_get_search_entry(GT_CONTAINER_VIEW(self));
111 
112     G_OBJECT_CLASS(gt_game_container_view_parent_class)->constructed(obj);
113 
114     g_assert(GTK_IS_SEARCH_ENTRY(search_entry));
115 
116     gt_container_view_add_container(GT_CONTAINER_VIEW(self),
117         GT_ITEM_CONTAINER(priv->top_container));
118 
119     gt_container_view_add_container(GT_CONTAINER_VIEW(self),
120         GT_ITEM_CONTAINER(priv->search_container));
121 
122     gt_container_view_add_container(GT_CONTAINER_VIEW(self),
123         GT_ITEM_CONTAINER(priv->game_container));
124 
125     g_signal_connect(self, "notify::search-active", G_CALLBACK(search_active_cb), self);
126 
127     g_object_bind_property(search_entry, "text", priv->search_container, "query", G_BINDING_DEFAULT);
128 }
129 
130 static void
gt_game_container_view_class_init(GtGameContainerViewClass * klass)131 gt_game_container_view_class_init(GtGameContainerViewClass* klass)
132 {
133     G_OBJECT_CLASS(klass)->constructed = constructed;
134 
135     GT_CONTAINER_VIEW_CLASS(klass)->go_back = go_back;
136     GT_CONTAINER_VIEW_CLASS(klass)->refresh = refresh;
137 }
138 
139 static void
gt_game_container_view_init(GtGameContainerView * self)140 gt_game_container_view_init(GtGameContainerView* self)
141 {
142     GtGameContainerViewPrivate* priv = gt_game_container_view_get_instance_private(self);
143 
144     priv->top_container = gt_top_game_container_new();
145     priv->search_container = gt_search_game_container_new();
146     priv->game_container = gt_game_channel_container_new();
147     priv->container_stack = gt_container_view_get_container_stack(GT_CONTAINER_VIEW(self));
148 }
149 
150 void
gt_game_container_view_open_game(GtGameContainerView * self,GtGame * game)151 gt_game_container_view_open_game(GtGameContainerView* self, GtGame* game)
152 {
153     g_assert(GT_IS_GAME_CONTAINER_VIEW(self));
154     g_assert(GT_IS_GAME(game));
155 
156     GtGameContainerViewPrivate* priv = gt_game_container_view_get_instance_private(self);
157 
158     if (gt_container_view_get_search_active(GT_CONTAINER_VIEW(self)))
159     {
160         GtkWidget* search_bar = gt_container_view_get_search_bar(GT_CONTAINER_VIEW(self));
161 
162         gtk_widget_set_sensitive(search_bar, FALSE);
163     }
164 
165     g_object_set(priv->game_container, "game", gt_game_get_name(game), NULL);
166 
167     g_object_set(self, "show-back-button", TRUE, NULL);
168 
169     gtk_stack_set_visible_child(GTK_STACK(priv->container_stack),
170         GTK_WIDGET(priv->game_container));
171 }
172