Use macros instead of raw numbers for login type
[rofl0r-ixchat.git] / src / fe-gtk / urlgrab.c
blob7f23af922e773cef2a839392166ea5c98c4078e3
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
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.
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.
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
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
23 #include "fe-gtk.h"
25 #include <gtk/gtkhbox.h>
26 #include <gtk/gtkstock.h>
27 #include <gtk/gtkhbbox.h>
28 #include <gtk/gtkscrolledwindow.h>
30 #include <gtk/gtkliststore.h>
31 #include <gtk/gtktreeview.h>
32 #include <gtk/gtktreeselection.h>
33 #include <gtk/gtkcellrenderertext.h>
35 #include "../common/xchat.h"
36 #include "../common/xchatc.h"
37 #include "../common/cfgfiles.h"
38 #include "../common/fe.h"
39 #include "../common/url.h"
40 #include "../common/tree.h"
41 #include "gtkutil.h"
42 #include "menu.h"
43 #include "maingui.h"
44 #include "urlgrab.h"
46 /* model for the URL treeview */
47 enum
49 URL_COLUMN,
50 N_COLUMNS
53 static GtkWidget *urlgrabberwindow = 0;
56 static gboolean
57 url_treeview_url_clicked_cb (GtkWidget *view, GdkEventButton *event,
58 gpointer data)
60 GtkTreeIter iter;
61 gchar *url;
63 if (!event ||
64 !gtkutil_treeview_get_selected (GTK_TREE_VIEW (view), &iter,
65 URL_COLUMN, &url, -1))
67 return FALSE;
70 switch (event->button)
72 case 1:
73 if (event->type == GDK_2BUTTON_PRESS)
74 fe_open_url (url);
75 break;
76 case 3:
77 menu_urlmenu (event, url);
78 break;
79 default:
80 break;
82 g_free (url);
84 return FALSE;
87 static GtkWidget *
88 url_treeview_new (GtkWidget *box)
90 GtkListStore *store;
91 GtkWidget *view;
93 store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);
94 g_return_val_if_fail (store != NULL, NULL);
96 view = gtkutil_treeview_new (box, GTK_TREE_MODEL (store), NULL,
97 URL_COLUMN, _("URL"), -1);
98 g_signal_connect (G_OBJECT (view), "button_press_event",
99 G_CALLBACK (url_treeview_url_clicked_cb), NULL);
100 /* don't want column headers */
101 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);
102 gtk_widget_show (view);
103 return view;
106 static void
107 url_closegui (GtkWidget *wid, gpointer userdata)
109 urlgrabberwindow = 0;
112 static void
113 url_button_clear (void)
115 GtkListStore *store;
117 url_clear ();
118 store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (urlgrabberwindow),
119 "model"));
120 gtk_list_store_clear (store);
123 static void
124 url_button_copy (GtkWidget *widget, gpointer data)
126 GtkTreeView *view = GTK_TREE_VIEW (data);
127 GtkTreeIter iter;
128 gchar *url = NULL;
130 if (gtkutil_treeview_get_selected (view, &iter, URL_COLUMN, &url, -1))
132 gtkutil_copy_to_clipboard (GTK_WIDGET (view), NULL, url);
133 g_free (url);
137 static void
138 url_save_callback (void *arg1, char *file)
140 if (file)
141 url_save (file, "w", TRUE);
144 static void
145 url_button_save (void)
147 gtkutil_file_req (_("Select an output filename"),
148 url_save_callback, NULL, NULL, FRF_WRITE);
151 void
152 fe_url_add (const char *urltext)
154 GtkListStore *store;
155 GtkTreeIter iter;
156 gboolean valid;
158 if (urlgrabberwindow)
160 store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (urlgrabberwindow),
161 "model"));
162 gtk_list_store_prepend (store, &iter);
163 gtk_list_store_set (store, &iter,
164 URL_COLUMN, urltext,
165 -1);
167 /* remove any overflow */
168 if (prefs.url_grabber_limit > 0)
170 valid = gtk_tree_model_iter_nth_child (
171 GTK_TREE_MODEL (store), &iter, NULL, prefs.url_grabber_limit);
172 while (valid)
173 valid = gtk_list_store_remove (store, &iter);
178 static int
179 populate_cb (char *urltext, gpointer userdata)
181 fe_url_add (urltext);
182 return TRUE;
185 void
186 url_opengui ()
188 GtkWidget *vbox, *hbox, *view;
190 if (urlgrabberwindow)
192 mg_bring_tofront (urlgrabberwindow);
193 return;
196 urlgrabberwindow =
197 mg_create_generic_tab ("UrlGrabber", _("XChat: URL Grabber"), FALSE,
198 TRUE, url_closegui, NULL, 400, 256, &vbox, 0);
199 view = url_treeview_new (vbox);
200 g_object_set_data (G_OBJECT (urlgrabberwindow), "model",
201 gtk_tree_view_get_model (GTK_TREE_VIEW (view)));
203 hbox = gtk_hbutton_box_new ();
204 gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_SPREAD);
205 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
206 gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
207 gtk_widget_show (hbox);
209 gtkutil_button (hbox, GTK_STOCK_CLEAR,
210 _("Clear list"), url_button_clear, 0, _("Clear"));
211 gtkutil_button (hbox, GTK_STOCK_COPY,
212 _("Copy selected URL"), url_button_copy, view, _("Copy"));
213 gtkutil_button (hbox, GTK_STOCK_SAVE_AS,
214 _("Save list to a file"), url_button_save, 0, _("Save As..."));
216 gtk_widget_show (urlgrabberwindow);
218 if (prefs.url_grabber)
219 tree_foreach (url_tree, (tree_traverse_func *)populate_cb, NULL);
220 else
222 gtk_list_store_clear (GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (view))));
223 fe_url_add ("URL Grabber is disabled.");