summary refs log blame commit diff stats
path: root/src/fe-gtk/rawlog.c
blob: 39dca988f4dce4e6ecae3f903ca8758e6f30b346 (plain) (tree)




















                                                                            

                   


               


                   





















































                                                                                                                  
                                                                                     














                                                            
                                                                                       
























































                                                                                                                       
/* X-Chat
 * Copyright (C) 1998 Peter Zelezny.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>

#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

#include "fe-gtk.h"

#include <gtk/gtkbutton.h>
#include <gtk/gtkhbbox.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvscrollbar.h>
#include <gtk/gtkstock.h>

#include "../common/xchat.h"
#include "../common/xchatc.h"
#include "../common/cfgfiles.h"
#include "../common/server.h"
#include "gtkutil.h"
#include "palette.h"
#include "maingui.h"
#include "rawlog.h"
#include "xtext.h"


static void
close_rawlog (GtkWidget *wid, server *serv)
{
	if (is_server (serv))
		serv->gui->rawlog_window = 0;
}

static void
rawlog_save (server *serv, char *file)
{
	int fh = -1;

	if (file)
	{
		if (serv->gui->rawlog_window)
			fh = xchat_open_file (file, O_TRUNC | O_WRONLY | O_CREAT,
										 0600, XOF_DOMODE | XOF_FULLPATH);
		if (fh != -1)
		{
			gtk_xtext_save (GTK_XTEXT (serv->gui->rawlog_textlist), fh);
			close (fh);
		}
	}
}

static int
rawlog_clearbutton (GtkWidget * wid, server *serv)
{
	gtk_xtext_clear (GTK_XTEXT (serv->gui->rawlog_textlist)->buffer, 0);
	return FALSE;
}

static int
rawlog_savebutton (GtkWidget * wid, server *serv)
{
	gtkutil_file_req (_("Save As..."), rawlog_save, serv, NULL, NULL, FRF_WRITE);
	return FALSE;
}

void
open_rawlog (struct server *serv)
{
	GtkWidget *hbox, *vscrollbar, *vbox;
	char tbuf[256];

	if (serv->gui->rawlog_window)
	{
		mg_bring_tofront (serv->gui->rawlog_window);
		return;
	}

	snprintf (tbuf, sizeof tbuf, _(DISPLAY_NAME": Rawlog (%s)"), serv->servername);
	serv->gui->rawlog_window =
		mg_create_generic_tab ("RawLog", tbuf, FALSE, TRUE, close_rawlog, serv,
							 640, 320, &vbox, serv);

	hbox = gtk_hbox_new (FALSE, 2);
	gtk_container_add (GTK_CONTAINER (vbox), hbox);
	gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
	gtk_widget_show (hbox);

	serv->gui->rawlog_textlist = gtk_xtext_new (colors, 0);
	gtk_xtext_set_tint (GTK_XTEXT (serv->gui->rawlog_textlist), prefs.tint_red, prefs.tint_green, prefs.tint_blue);
	gtk_xtext_set_background (GTK_XTEXT (serv->gui->rawlog_textlist),
									  channelwin_pix, prefs.transparent);

	gtk_container_add (GTK_CONTAINER (hbox), serv->gui->rawlog_textlist);
	gtk_xtext_set_font (GTK_XTEXT (serv->gui->rawlog_textlist), prefs.font_normal);
	GTK_XTEXT (serv->gui->rawlog_textlist)->ignore_hidden = 1;
	gtk_widget_show (serv->gui->rawlog_textlist);

	vscrollbar = gtk_vscrollbar_new (GTK_XTEXT (serv->gui->rawlog_textlist)->adj);
	gtk_box_pack_start (GTK_BOX (hbox), vscrollbar, FALSE, FALSE, 0);
	show_and_unfocus (vscrollbar);

	hbox = gtk_hbutton_box_new ();
	gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_SPREAD);
	gtk_box_pack_end (GTK_BOX (vbox), hbox, 0, 0, 0);
	gtk_widget_show (hbox);

	gtkutil_button (hbox, GTK_STOCK_CLEAR, NULL, rawlog_clearbutton,
						 serv, _("Clear rawlog"));

	gtkutil_button (hbox, GTK_STOCK_SAVE_AS, NULL, rawlog_savebutton,
						 serv, _("Save As..."));

	gtk_widget_show (serv->gui->rawlog_window);
}

void
fe_add_rawlog (server *serv, char *text, int len, int outbound)
{
	char *new_text;

	if (!serv->gui->rawlog_window)
		return;

	new_text = malloc (len + 7);

	len = sprintf (new_text, "\0033>>\017 %s", text);
	if (outbound)
	{
		new_text[1] = '4';
		new_text[2] = '<';
		new_text[3] = '<';
	}
	gtk_xtext_append (GTK_XTEXT (serv->gui->rawlog_textlist)->buffer, new_text, len);
	free (new_text);
}
="p">} /// Returns whether this data source contains any (valid) data. /// /// # Examples /// /// ``` /// use ganarchy::data::managers::RepoListManager; /// use ganarchy::data::DataSourceBase; /// /// let mut repos = RepoListManager::default(); /// // An empty RepoListManager has no data. /// assert!(!repos.exists()); /// ``` fn exists(&self) -> bool { self.repos[..self.valid].iter().any(|e| e.exists()) } } impl trait std::fmt::Display { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "repo list: [")?; for e in self.repos.iter() { write!(f, "{}, ", e)?; } write!(f, "]") } } /// Returns all [`ProjectFork`]s. impl trait DataSource<EffectiveKind<ProjectFork>> { fn get_values(&self) -> BTreeSet<EffectiveKind<ProjectFork>> { self.repos[..self.valid].iter().flat_map(|e| { e.get_values() }).collect() } } } } impl_trait! { impl ConfigManager { /// Creates a new, empty `ConfigManager`. Equivalent to /// `Default::default()`. /// /// # Examples /// /// ``` /// use ganarchy::data::managers::ConfigManager; /// /// let cm = ConfigManager::new(); /// ``` pub fn new() -> Self { Default::default() } /// Adds a [`DefaultsDataSource`] to this `ConfigManager`, for all /// supported properties. /// /// The added source will override any further data sources, so this /// should be called last, after all other data sources have been /// added. /// /// # Examples /// /// ``` /// use ganarchy::data::managers::ConfigManager; /// /// let mut cm = ConfigManager::default(); /// cm.add_defaults(); /// ``` pub fn add_defaults(&mut self) { self.add_source(DefaultsDataSource).for_all(); } /// Adds the given [`DataSource`] to this `ConfigManager` and returns /// a helper for setting which properties this `DataSource` will /// provide through this `ConfigManager`. /// /// # Examples /// /// ``` /// use ganarchy::data::sources::DefaultsDataSource; /// use ganarchy::data::managers::ConfigManager; /// /// let mut cm = ConfigManager::default(); /// cm.add_source(DefaultsDataSource).for_repo_lists(); /// ``` pub fn add_source<T>(&mut self, source: T) -> AddConfigSource<'_, T> where T: DataSourceBase + Send + Sync + 'static, { let arc = Arc::new(QCell::new(&self.owner, source)); self.resources.push(Resource::new(arc.clone())); AddConfigSource { resource: self.resources.last_mut().unwrap(), source: arc, } } impl trait DataSourceBase { /// Updates the contained `DataSource`s, and returns any relevant /// update stats. /// /// # Examples /// /// ``` /// use ganarchy::data::managers::ConfigManager; /// use ganarchy::data::DataSourceBase; /// /// let mut cm = ConfigManager::default(); /// cm.add_defaults(); /// let update = cm.update(); /// # assert!(update.errors.is_empty()); /// ``` fn update(&mut self) -> Update { let owner = &mut self.owner; let mut errors = Vec::new(); self.resources.iter().for_each(|e| { let ret = owner.rw(&*e.base).update(); errors.extend(ret.errors); }); self.valid = self.resources.len(); Update { errors: errors, } } /// Returns whether this data source contains any (valid) data. /// /// # Examples /// /// ``` /// use ganarchy::data::managers::ConfigManager; /// use ganarchy::data::DataSourceBase; /// /// let mut cm = ConfigManager::default(); /// // An empty ConfigManager has no data. /// assert!(!cm.exists()); /// ``` fn exists(&self) -> bool { self.resources[..self.valid].iter().any(|e| { self.owner.ro(&*e.base).exists() }) } } impl trait std::fmt::Display { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "config: [")?; for e in self.resources.iter() { write!(f, "{}, ", self.owner.ro(&*e.base))?; } write!(f, "]") } } /// Returns the first [`InstanceTitle`] available. impl trait DataSource<InstanceTitle> { fn get_values(&self) -> Option<InstanceTitle> { self.resources[..self.valid].iter().flat_map(|e| { e.title.as_ref() }).flat_map(|e| { self.owner.ro(&*e).get_values() }).next() } } /// Returns the first [`InstanceBaseUrl`] available. impl trait DataSource<InstanceBaseUrl> { fn get_values(&self) -> Option<InstanceBaseUrl> { self.resources[..self.valid].iter().flat_map(|e| { e.url.as_ref() }).flat_map(|e| { self.owner.ro(&*e).get_values() }).next() } } /// Returns all [`RepoListUrl`]s. /// /// For correct results, wrap this [`ConfigManager`] in an /// [`EffectiveDataSource`]. impl trait DataSource<RepoListUrl> { fn get_values(&self) -> Vec<RepoListUrl> { self.resources[..self.valid].iter().flat_map(|e| { e.repolist.as_ref() }).flat_map(|e| { self.owner.ro(&*e).get_values() }).collect() } } } } impl<'a, T: DataSourceBase + Send + Sync + 'static> AddConfigSource<'a, T> { /// Adds this data source as a provider for [`InstanceTitle`]. /// /// # Examples /// /// ``` /// use ganarchy::data::sources::DefaultsDataSource; /// use ganarchy::data::managers::ConfigManager; /// /// let mut cm = ConfigManager::default(); /// cm.add_source(DefaultsDataSource).for_title(); /// ``` pub fn for_title(self) -> Self where T: DataSource<InstanceTitle> { let arc = &self.source; self.resource.title.get_or_insert_with(|| { arc.clone() }); self } /// Adds this data source as a provider for [`InstanceBaseUrl`]. /// /// # Examples /// /// ``` /// use ganarchy::data::sources::DefaultsDataSource; /// use ganarchy::data::managers::ConfigManager; /// /// let mut cm = ConfigManager::default(); /// cm.add_source(DefaultsDataSource).for_base_url(); /// ``` pub fn for_base_url(self) -> Self where T: DataSource<InstanceBaseUrl> { let arc = &self.source; self.resource.url.get_or_insert_with(|| { arc.clone() }); self } /// Adds this data source as a provider for [`RepoListUrl`]. /// /// # Examples /// /// ``` /// use ganarchy::data::sources::DefaultsDataSource; /// use ganarchy::data::managers::ConfigManager; /// /// let mut cm = ConfigManager::default(); /// cm.add_source(DefaultsDataSource).for_repo_lists(); /// ``` pub fn for_repo_lists(self) -> Self where T: DataSource<RepoListUrl> { let arc = &self.source; self.resource.repolist.get_or_insert_with(|| { arc.clone() }); self } // pub(crate): we wanna be able to make breaking changes to this function // without affecting downstream users. pub(crate) fn for_all(self) -> Self where T: DataSource<InstanceTitle>, T: DataSource<InstanceBaseUrl>, T: DataSource<RepoListUrl>, { self.for_title().for_base_url().for_repo_lists() } }