1 /**
2  * Copyright (c) 2005 PCMan <pcman.tw@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifdef __GNUG__
20   #pragma implementation "sitelistdlg.h"
21 #endif
22 
23 #include "sitelistdlg.h"
24 #include "appconfig.h"
25 #include "mainframe.h"
26 #include <cstdio>
27 #include <cstring>
28 #include <string>
29 
30 using namespace std;
31 
CSiteListDlg(CWidget * parent)32 CSiteListDlg::CSiteListDlg(CWidget* parent)
33  : CDialog(parent, _("Site List"), false), m_Store(NULL), m_pParent((CMainFrame*)parent)
34 {
35 	GtkWidget *dialog_vbox3;
36 	GtkWidget *hbox16;
37 	GtkWidget *vbox4;
38 	GtkWidget *hbox17;
39 	GtkWidget *scrolledwindow1;
40 	GtkWidget *vbuttonbox1;
41 	GtkWidget *connect_btn;
42 	GtkWidget *alignment1;
43 	GtkWidget *hbox18;
44 	GtkWidget *image349;
45 	GtkWidget *label26;
46 	GtkWidget *close_btn;
47 
48 	dialog_vbox3 = GTK_DIALOG (m_Widget)->vbox;
49 	gtk_widget_show (dialog_vbox3);
50 
51 	hbox16 = gtk_hbox_new (FALSE, 0);
52 	gtk_widget_show (hbox16);
53 	gtk_box_pack_start (GTK_BOX (dialog_vbox3), hbox16, TRUE, TRUE, 0);
54 
55 	vbox4 = gtk_vbox_new (FALSE, 0);
56 	gtk_widget_show (vbox4);
57 	gtk_box_pack_start (GTK_BOX (hbox16), vbox4, TRUE, TRUE, 0);
58 
59 	hbox17 = gtk_hbox_new (FALSE, 0);
60 	gtk_widget_show (hbox17);
61 	gtk_box_pack_start (GTK_BOX (vbox4), hbox17, FALSE, TRUE, 0);
62 	gtk_container_set_border_width (GTK_CONTAINER (hbox17), 1);
63 
64 	m_Keyword = gtk_entry_new ();
65 	gtk_widget_show (m_Keyword);
66 	gtk_box_pack_start (GTK_BOX (hbox17), m_Keyword, TRUE, TRUE, 2);
67 
68 	m_SearchBtn = gtk_button_new_from_stock ("gtk-find");
69 	gtk_widget_show (m_SearchBtn);
70 	gtk_box_pack_start (GTK_BOX (hbox17), m_SearchBtn, FALSE, FALSE, 2);
71 
72 	scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
73 	gtk_widget_show (scrolledwindow1);
74 	gtk_box_pack_start (GTK_BOX (vbox4), scrolledwindow1, TRUE, TRUE, 2);
75 	gtk_container_set_border_width (GTK_CONTAINER (scrolledwindow1), 1);
76 	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN);
77 
78 	m_Tree = gtk_tree_view_new ();
79 	gtk_widget_show (m_Tree);
80 	gtk_container_add (GTK_CONTAINER (scrolledwindow1), m_Tree);
81 	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (m_Tree), FALSE);
82 
83 	vbuttonbox1 = gtk_vbutton_box_new ();
84 	gtk_widget_show (vbuttonbox1);
85 	gtk_box_pack_start (GTK_BOX (hbox16), vbuttonbox1, FALSE, TRUE, 0);
86 	gtk_container_set_border_width (GTK_CONTAINER (vbuttonbox1), 1);
87 	gtk_button_box_set_layout (GTK_BUTTON_BOX (vbuttonbox1), GTK_BUTTONBOX_START);
88 
89 	connect_btn = gtk_button_new ();
90 	gtk_widget_show (connect_btn);
91 	gtk_container_add (GTK_CONTAINER (vbuttonbox1), connect_btn);
92 	GTK_WIDGET_SET_FLAGS (connect_btn, GTK_CAN_DEFAULT);
93 
94 	alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
95 	gtk_widget_show (alignment1);
96 	gtk_container_add (GTK_CONTAINER (connect_btn), alignment1);
97 
98 	hbox18 = gtk_hbox_new (FALSE, 2);
99 	gtk_widget_show (hbox18);
100 	gtk_container_add (GTK_CONTAINER (alignment1), hbox18);
101 
102 	image349 = gtk_image_new_from_stock (GTK_STOCK_NETWORK, GTK_ICON_SIZE_BUTTON);
103 	gtk_widget_show (image349);
104 	gtk_box_pack_start (GTK_BOX (hbox18), image349, FALSE, FALSE, 0);
105 
106 	label26 = gtk_label_new_with_mnemonic (_("C_onnect"));
107 	gtk_widget_show (label26);
108 	gtk_box_pack_start (GTK_BOX (hbox18), label26, FALSE, FALSE, 0);
109 
110 	close_btn = gtk_button_new_from_stock ("gtk-close");
111 	gtk_widget_show (close_btn);
112 	gtk_container_add (GTK_CONTAINER (vbuttonbox1), close_btn);
113 	GTK_WIDGET_SET_FLAGS (close_btn, GTK_CAN_DEFAULT);
114 
115 	g_signal_connect ((gpointer) m_SearchBtn, "clicked",
116 					G_CALLBACK (&CSiteListDlg::OnSearch),
117 					this);
118 	g_signal_connect ((gpointer) connect_btn, "clicked",
119 					G_CALLBACK (&CSiteListDlg::OnConnect),
120 					this);
121 	g_signal_connect ((gpointer) close_btn, "clicked",
122 					G_CALLBACK (&CSiteListDlg::OnClose),
123 					this);
124 
125 	g_signal_connect ((gpointer) m_Tree, "row-activated",
126 					G_CALLBACK (&CSiteListDlg::OnRowActivated),
127 					this);
128 
129 	g_signal_connect ((gpointer) m_Keyword, "activate",
130 					G_CALLBACK (&CSiteListDlg::OnKeywordActivate),
131 					this);
132 
133 
134 	gtk_window_set_default_size((GtkWindow*)m_Widget, 512, 384);
135 
136 	m_ConnectBtn = connect_btn;
137 
138 	LoadSiteList();
139 }
140 
141 
142 static const char ITEM_SEP[]="        ";
143 
144 enum
145 {
146 	COL_ICON,
147 	COL_TEXT,
148 	COL_NUM
149 };
150 
151 
OnSearch(GtkButton * btn UNUSED,CSiteListDlg * _this)152 void CSiteListDlg::OnSearch(GtkButton* btn UNUSED, CSiteListDlg* _this)
153 {
154 	const gchar* keyword = gtk_entry_get_text( GTK_ENTRY(_this->m_Keyword) );
155 	if( !*keyword )
156 		return;
157 
158 	GtkTreeView* tree_view = GTK_TREE_VIEW(_this->m_Tree);
159 	GtkTreeSelection* sel = gtk_tree_view_get_selection(tree_view);
160 	GtkTreeModel* model = GTK_TREE_MODEL(_this->m_Store);
161 	GtkTreeIter it;
162 	gchar* text;
163 	bool found = false;
164 
165 	if( !gtk_tree_selection_get_selected( sel, &model, &it) )
166 	{
167 		if( !gtk_tree_model_get_iter_first(model, &it) )
168 			return;
169 	}
170 	else	// Get next iter
171 	{
172 		GtkTreeIter child_it;
173 		GtkTreeIter next_it;
174 		next_it = it;
175 get_next_iter:
176 		if( gtk_tree_model_iter_children( model, &child_it, &it ) ) // Has children?
177 		{
178 			next_it = child_it;
179 		}
180 		else if( !gtk_tree_model_iter_next( model, &next_it ) ) // Has sibling?
181 		{
182 		get_parent_iter:
183 			// No sibling. Has Parent?
184 			GtkTreeIter parent_it;
185 			if( !gtk_tree_model_iter_parent( model, &parent_it, &it ) )
186 				goto keyword_not_found;	// No parent, stop!
187 
188 			// Does parent has next sibling?
189 			next_it = parent_it;
190 			if( !gtk_tree_model_iter_next( model, &next_it ) )
191 			{
192 				it = parent_it;
193 				goto get_parent_iter;
194 			}
195 		}
196 		it = next_it;
197 	}
198 
199 	gtk_tree_model_get(model, &it, COL_TEXT, &text, -1);
200 	found = strstr(text, keyword);
201 	g_free(text);
202 	if( found )
203 	{
204 		GtkTreePath* path = gtk_tree_model_get_path(model, &it);
205 		gtk_tree_view_expand_to_path( tree_view, path );
206 		gtk_tree_selection_select_iter(sel, &it);
207 		gtk_tree_view_scroll_to_cell( tree_view, path, NULL, false, 0, 0 );
208 	//	FIXME: GtkTreeView doesn't scroll to the selected path.  Is it a bug?
209 		gtk_tree_path_free(path);
210 		return;
211 	}
212 	goto get_next_iter;
213 
214 keyword_not_found:
215 	GtkWidget* dlg = gtk_message_dialog_new( GTK_WINDOW(_this->m_Widget),
216 									GTK_DIALOG_MODAL,
217 									GTK_MESSAGE_INFO,
218 									GTK_BUTTONS_OK,
219 									_("Keyword \"%s\" not found!"), keyword);
220 	gtk_dialog_run(GTK_DIALOG(dlg));
221 	gtk_widget_destroy(dlg);
222 }
223 
OnConnect(GtkButton * btn UNUSED,CSiteListDlg * _this)224 void CSiteListDlg::OnConnect(GtkButton* btn UNUSED, CSiteListDlg* _this)
225 {
226 	GtkTreeView* tree_view = GTK_TREE_VIEW(_this->m_Tree);
227 	GtkTreeSelection* sel = gtk_tree_view_get_selection(tree_view);
228 	GtkTreeModel* model;
229 	GtkTreeIter it;
230 	if( gtk_tree_selection_get_selected( sel, &model, &it) )
231 	{
232 		gchar* text;
233 		gtk_tree_model_get(GTK_TREE_MODEL(_this->m_Store), &it, COL_TEXT, &text, -1);
234 		INFO("%s", text);
235 		char* url = strstr(text, ITEM_SEP);
236 		if( url )
237 		{
238 			*url = '\0';
239 			url += (sizeof(ITEM_SEP)-1);
240 			_this->m_pParent->NewCon(text, url, &AppConfig.m_DefaultSite);
241 			gtk_dialog_response( GTK_DIALOG(_this->m_Widget), GTK_RESPONSE_OK );
242 		}
243 		else if(strlen(text) != 0)
244 		{
245 			GtkTreeSelection* _selection = gtk_tree_view_get_selection(tree_view);
246 			gtk_tree_selection_select_iter(_selection, &it);
247 			GtkTreePath* path = gtk_tree_model_get_path(model, &it);
248 			if( !gtk_tree_view_row_expanded(tree_view, path) )
249 			{
250 				gtk_tree_view_expand_to_path(tree_view, path);
251 			}
252 			else
253 			{
254 				gtk_tree_view_collapse_row(tree_view, path);
255 			}
256 		}
257 		g_free(text);
258 	}
259 }
260 
OnClose(GtkButton * btn UNUSED,CSiteListDlg * _this)261 void CSiteListDlg::OnClose(GtkButton* btn UNUSED, CSiteListDlg* _this)
262 {
263 	gtk_dialog_response( GTK_DIALOG(_this->m_Widget), GTK_RESPONSE_CANCEL );
264 }
265 
LoadSiteList()266 void CSiteListDlg::LoadSiteList()
267 {
268 	GtkIconSet* icon_set = gtk_icon_factory_lookup_default(GTK_STOCK_NETWORK);
269 	m_SiteIcon = gtk_icon_set_render_icon(icon_set, m_Tree->style, GTK_TEXT_DIR_NONE, GTK_STATE_NORMAL, GTK_ICON_SIZE_MENU, NULL, NULL);
270 
271 	icon_set = gtk_icon_factory_lookup_default(GTK_STOCK_DIRECTORY);
272 	m_FolderIcon = gtk_icon_set_render_icon(icon_set, m_Tree->style, GTK_TEXT_DIR_NONE, GTK_STATE_NORMAL, GTK_ICON_SIZE_MENU, NULL, NULL);
273 //	g_object_unref(icon_set);	??
274 
275 	GtkTreeView* tree = GTK_TREE_VIEW(m_Tree);
276 
277 	m_Store = gtk_tree_store_new(COL_NUM, GDK_TYPE_PIXBUF, G_TYPE_STRING);
278 
279 	string fpath = AppConfig.GetDataPath("sitelist");
280 	FILE *file = fopen( fpath.c_str(), "r");
281 	if( file )
282 	{
283 		char buffer[1024];
284 		LoadSiteList( NULL, file, buffer );
285 		fclose(file);
286 	}
287 	else
288 	{
289 //		wxMessageBox( _("Unable to open site list file!"), _("Error"), wxICON_STOP, m_pMainFrame );
290 	}
291 
292 	GtkTreeViewColumn *col = gtk_tree_view_column_new ();
293 	GtkCellRenderer *renderer = GTK_CELL_RENDERER (gtk_cell_renderer_pixbuf_new ());
294         gtk_tree_view_column_pack_start (col, renderer, FALSE);
295 
296 	gtk_tree_view_column_set_attributes
297 		(col, renderer,
298 		 "pixbuf", COL_ICON,
299 		 "pixbuf-expander-open", COL_ICON,
300 		 "pixbuf-expander-closed", COL_ICON,
301 		 NULL);
302 
303 	renderer = GTK_CELL_RENDERER ( gtk_cell_renderer_text_new() );
304         gtk_tree_view_column_pack_start (col, renderer, TRUE);
305 
306 	gtk_tree_view_column_set_attributes( col, renderer, "text", COL_TEXT, NULL);
307 
308 	gtk_tree_view_append_column( tree, col );
309 
310 	gtk_tree_view_set_model(tree, GTK_TREE_MODEL(m_Store));
311 	g_object_unref(m_Store);
312 
313 }
314 
315 
LoadSiteList(GtkTreeIter * parent,FILE * file,char * line)316 void CSiteListDlg::LoadSiteList(GtkTreeIter *parent, FILE *file, char *line)
317 {
318 	GtkTreeIter it;
319 	while( fgets( line, 1024, file ) )
320 	{
321 		char* name = strtok( line, "\t\r\n" );
322 		if( !name || '\0' == *name )
323 			return;
324 
325 		if( 'd' == *name )
326 		{
327 			gtk_tree_store_append(m_Store, &it, parent);
328 			//	gtk_tree_store_set_value(m_Store, &it, 0, (GValue*)_("Site List"));
329 			//	Why can't I use this? What causes the segmentation fault?
330 			const char* text = name+1;
331 			gtk_tree_store_set(m_Store, &it, COL_ICON, m_FolderIcon, COL_TEXT, (GValue*)text, -1);
332 			LoadSiteList( &it, file, line );
333 		}
334 		else if( 's' == *name )
335 		{
336 			char* URL = strtok( NULL, "\t\r\n" );
337 			if( URL )
338 			{
339 				string text = name+1;
340 //				text.Replace(" ", "");
341 				text += ITEM_SEP;
342 				text += URL;
343 
344 				gtk_tree_store_append(m_Store, &it, parent);
345 				gtk_tree_store_set(m_Store, &it, COL_ICON, m_SiteIcon, 1, (GValue*)text.c_str(), -1);
346 			}
347 		}
348 	}
349 }
350 
351 
OnRowActivated(GtkTreeView * tree_view UNUSED,GtkTreePath * path UNUSED,GtkTreeViewColumn * col UNUSED,CSiteListDlg * _this)352 void CSiteListDlg::OnRowActivated(GtkTreeView *tree_view UNUSED,
353                                   GtkTreePath* path UNUSED,
354 				  GtkTreeViewColumn* col UNUSED, CSiteListDlg* _this)
355 {
356 	gtk_button_clicked( GTK_BUTTON(_this->m_ConnectBtn) );
357 }
358 
OnKeywordActivate(GtkEntry * entry UNUSED,CSiteListDlg * _this)359 void CSiteListDlg::OnKeywordActivate(GtkEntry* entry UNUSED, CSiteListDlg* _this)
360 {
361 	gtk_button_clicked( GTK_BUTTON(_this->m_SearchBtn) );
362 }
363