/* * Author: Andrew Craig * * Copyright (C) 2004 Atomic Blue (info@planeshift.it, http://www.atomicblue.org) * * * 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 (version 2 of the License) * 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 #include #include "../globals.h" #include "pscelclient.h" #include "pawsignore.h" #include "paws/pawslistbox.h" #include "paws/pawstextbox.h" #include "gui/chatwindow.h" #define ADD 3000 #define REMOVE 2000 pawsIgnoreWindow::~pawsIgnoreWindow() { SaveIgnoreList(); } bool pawsIgnoreWindow::PostSetup() { ignoreList = (pawsListBox*)FindWidget( "IgnoreList" ); if ( !ignoreList ) return true; if ( !LoadIgnoreList() ) return false; return true; } void pawsIgnoreWindow::OnListAction( pawsListBox* widget, int status ) { if (status==LISTBOX_HIGHLIGHTED) { pawsListBoxRow* row = widget->GetSelectedRow(); if ( row ) { currentIgnored = ((pawsTextBox*)row->GetColumn(0))->GetText(); } } } bool pawsIgnoreWindow::OnButtonPressed( int mouseButton, int keyModifier, pawsWidget* widget ) { switch ( widget->GetID() ) { case REMOVE: { if ( currentIgnored.Length() == 0 ) return false; pawsStringPromptWindow::Create("Remove", currentIgnored, false, 220, 20, this, "remove" ); return true; } case ADD: { csString empty(""); pawsStringPromptWindow::Create("Add", csString(""), false, 220, 20, this, "add" ); return true; } } return false; } bool pawsIgnoreWindow::LoadIgnoreList() { csRef vfs = CS_QUERY_REGISTRY(PawsManager::GetSingleton().GetObjectRegistry(), iVFS); iDocumentSystem* xml = psengine->GetXMLParser (); if (vfs && xml) { csRef file = vfs->Open ("/this/data/options/ignore.xml", VFS_FILE_READ); if ( file ) { csString ignored; csRef doc = xml->CreateDocument(); const char* error = doc->Parse(file); if (error) { printf("Error Loading Ignorelist: %s\n", error); return false; } csRef iter = doc->GetRoot()->GetNode("Ignorelist")->GetNodes("Ignored"); while(iter->HasNext()) { ignored = iter->Next()->GetAttributeValue("name"); AddIgnore(ignored); } return true; } else { Warning1( LOG_PAWS, "No ignore.xml file found. Assuming empty\n"); return true; } } else { return false; } } void pawsIgnoreWindow::SaveIgnoreList() { csRef vfs = CS_QUERY_REGISTRY(PawsManager::GetSingleton().GetObjectRegistry(), iVFS); csRef xml = csPtr(new csTinyDocumentSystem); csRef doc = xml->CreateDocument(); int length = ignoredNames.Length(); csRef root = doc->CreateRoot(); csRef listNode = root->CreateNodeBefore(CS_NODE_ELEMENT); listNode->SetValue("Ignorelist"); for (int i = 0;i < length; i++) { csRef node = listNode->CreateNodeBefore(CS_NODE_ELEMENT); node->SetValue("Ignored"); node->SetAttribute("name",ignoredNames[i]); } doc->Write(vfs, "/this/data/options/ignore.xml"); } void pawsIgnoreWindow::AddIgnore( csString& name ) { pawsChatWindow* chat = (pawsChatWindow*)PawsManager::GetSingleton().FindWidget("ChatWindow"); if (!strcmp(name, psengine->GetCelClient()->GetMainActor()->GetName())) { if ( chat ) chat->ChatOutput(PawsManager::GetSingleton().Translate("You can not ignore yourself.")); return; } if ( chat ) { char temp[100]; cs_snprintf(temp, 100, PawsManager::GetSingleton().Translate("You will now ignore %s."), (const char*)name); chat->ChatOutput(temp); } if ( ignoredNames.Find( name ) == -1 ) { pawsListBoxRow* row = ignoreList->NewRow(); pawsTextBox* textname = (pawsTextBox*)row->GetColumn(0); ignoredNames.InsertSorted(name); row->SetName( name ); textname->SetText( name ); textname->SetColour( graphics2D->FindRGB( 255,0,0 ) ); } } void pawsIgnoreWindow::RemoveIgnore( csString& name ) { pawsChatWindow* chat = (pawsChatWindow*)PawsManager::GetSingleton().FindWidget("ChatWindow"); if ( !chat ) return; else { char temp[100]; cs_snprintf(temp, 100,PawsManager::GetSingleton().Translate("You will no longer ignore %s."), (const char*)name); chat->ChatOutput(temp); ignoredNames.Delete(name); pawsListBoxRow* row = (pawsListBoxRow*)ignoreList->FindWidget( name ); ignoreList->Select( row ); ignoreList->RemoveSelected(); } } bool pawsIgnoreWindow::IsIgnored(csString &name) { if (ignoredNames.FindSorted(name)==-1) return false; else return true; } void pawsIgnoreWindow::OnStringEntered(const char *name,int param,const char *value) { if (!value || !strlen(value)) return; csString command; if (!strcmp(name,"add")) command.Format( "/add_ignore %s", (const char*)value ); else command.Format( "/remove_ignore %s", (const char*)value ); psengine->GetCmdHandler()->Execute(command); }