| 1 | /* |
| 2 | Copyright (C) 1999-2006 Id Software, Inc. and contributors. |
| 3 | For a list of contributors, see the accompanying CONTRIBUTORS file. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 7 | GtkRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GtkRadiant is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GtkRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "help.h" |
| 23 | |
| 24 | #include "debugging/debugging.h" |
| 25 | |
| 26 | #include <vector> |
| 27 | #include <list> |
| 28 | |
| 29 | #include "libxml/parser.h" |
| 30 | #include "generic/callback.h" |
| 31 | #include "gtkutil/menu.h" |
| 32 | #include "stream/stringstream.h" |
| 33 | #include "os/file.h" |
| 34 | |
| 35 | #include "url.h" |
| 36 | #include "preferences.h" |
| 37 | #include "mainframe.h" |
| 38 | |
| 39 | /*! |
| 40 | the urls to fire up in the game packs help menus |
| 41 | */ |
| 42 | namespace |
| 43 | { |
| 44 | std::list<CopiedString> mHelpURLs; |
| 45 | } |
| 46 | |
| 47 | /*! |
| 48 | needed for hooking in Gtk+ |
| 49 | */ |
| 50 | void HandleHelpCommand( CopiedString& str ){ |
| 51 | OpenURL( str.c_str() ); |
| 52 | } |
| 53 | |
| 54 | void process_xlink( const char* filename, const char *menu_name, const char *base_url, ui::Menu menu ){ |
| 55 | if ( file_exists( filename ) ) { |
| 56 | xmlDocPtr pDoc = xmlParseFile( filename ); |
| 57 | if ( pDoc ) { |
| 58 | globalOutputStream() << "Processing .xlink file '" << filename << "'\n"; |
| 59 | // create sub menu |
| 60 | auto menu_in_menu = create_sub_menu_with_mnemonic( menu, menu_name ); |
| 61 | if ( g_Layout_enableDetachableMenus.m_value ) { |
| 62 | menu_tearoff( menu_in_menu ); |
| 63 | } |
| 64 | // start walking the nodes, find the 'links' one |
| 65 | xmlNodePtr pNode = pDoc->children; |
| 66 | while ( pNode && strcmp( (const char*)pNode->name, "links" ) ) |
| 67 | pNode = pNode->next; |
| 68 | if ( pNode ) { |
| 69 | pNode = pNode->children; |
| 70 | while ( pNode ) |
| 71 | { |
| 72 | if ( !strcmp( (const char*)pNode->name, "item" ) ) { |
| 73 | // process the URL |
| 74 | CopiedString url; |
| 75 | |
| 76 | xmlChar* prop = xmlGetProp( pNode, reinterpret_cast<const xmlChar*>( "url" ) ); |
| 77 | ASSERT_NOTNULL( prop ); |
| 78 | if ( strstr( reinterpret_cast<const char*>( prop ), "http://" ) || |
| 79 | strstr( reinterpret_cast<const char*>( prop ), "https://" ) ) { |
| 80 | // complete URL |
| 81 | url = reinterpret_cast<const char*>( prop ); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | // relative URL |
| 86 | StringOutputStream full( 256 ); |
| 87 | full << base_url << reinterpret_cast<const char*>( prop ); |
| 88 | url = full.c_str(); |
| 89 | } |
| 90 | |
| 91 | mHelpURLs.push_back( url ); |
| 92 | |
| 93 | xmlFree( prop ); |
| 94 | |
| 95 | prop = xmlGetProp( pNode, reinterpret_cast<const xmlChar*>( "name" ) ); |
| 96 | ASSERT_NOTNULL( prop ); |
| 97 | create_menu_item_with_mnemonic( menu_in_menu, reinterpret_cast<const char*>( prop ), ReferenceCaller<CopiedString, void(), HandleHelpCommand>( mHelpURLs.back() ) ); |
| 98 | xmlFree( prop ); |
| 99 | } |
| 100 | pNode = pNode->next; |
| 101 | } |
| 102 | } |
| 103 | xmlFreeDoc( pDoc ); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | globalOutputStream() << "'" << filename << "' parse failed\n"; |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | globalOutputStream() << "'" << filename << "' not found\n"; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void create_game_help_menu( ui::Menu menu ){ |
| 117 | StringOutputStream filename( 256 ); |
| 118 | filename << DataPath_get() << "global.xlink"; |
| 119 | process_xlink(filename.c_str(), "General", DataPath_get(), menu); |
| 120 | |
| 121 | #if 1 |
| 122 | filename.clear(); |
| 123 | filename << g_pGameDescription->mGameToolsPath.c_str() << "game.xlink"; |
| 124 | process_xlink( filename.c_str(), g_pGameDescription->getRequiredKeyValue( "name" ), g_pGameDescription->mGameToolsPath.c_str(), menu ); |
| 125 | #else |
| 126 | for ( std::list<CGameDescription *>::iterator iGame = g_GamesDialog.mGames.begin(); iGame != g_GamesDialog.mGames.end(); ++iGame ) |
| 127 | { |
| 128 | filename.clear(); |
| 129 | filename << ( *iGame )->mGameToolsPath.c_str() << "game.xlink"; |
| 130 | process_xlink( filename.c_str(), ( *iGame )->getRequiredKeyValue( "name" ), ( *iGame )->mGameToolsPath.c_str(), menu ); |
| 131 | } |
| 132 | #endif |
| 133 | } |