Upstream tarball 9445
[amule.git] / src / ExternalConnector.h
blob1715a26724b9bafece89cb4073999be88d7b6059
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 //
6 // Any parts of this program derived from the xMule, lMule or eMule project,
7 // or contributed by third-party developers are copyrighted by their
8 // respective authors.
9 //
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 * This file must be included with wxUSE_GUI defined to zero or one.
27 * Usually on console applications, this will be taken care of in
28 * configure time. This is because wx classes will be compiled
29 * differently in each case.
33 #ifndef __EXTERNALCONNECTOR_H__
34 #define __EXTERNALCONNECTOR_H__
36 #include <wx/app.h> // For wxApp
37 #include <wx/cmdline.h> // For wxCmdLineEntryDesc
38 #include <ec/cpp/RemoteConnect.h>
40 #include <wx/intl.h>
42 #define CMD_DEPRECATED 0x1000
43 #define CMD_OK 0
44 #define CMD_ID_QUIT -1
45 #define CMD_ID_HELP -2
46 #define CMD_ERR_SYNTAX -3
47 #define CMD_ERR_PROCESS_CMD -4
48 #define CMD_ERR_NO_PARAM -5
49 #define CMD_ERR_MUST_HAVE_PARAM -6
50 #define CMD_ERR_INVALID_ARG -7
51 #define CMD_ERR_INCOMPLETE -8
53 enum Params {
54 CMD_PARAM_NEVER,
55 CMD_PARAM_OPTIONAL,
56 CMD_PARAM_ALWAYS
59 class CCommandTree;
61 typedef std::list<const CCommandTree*> CmdList_t;
62 typedef std::list<const CCommandTree*>::iterator CmdPos_t;
63 typedef std::list<const CCommandTree*>::const_iterator CmdPosConst_t;
65 class CaMuleExternalConnector;
67 class CCommandTree {
68 public:
69 CCommandTree(CaMuleExternalConnector& app)
70 : m_command(wxEmptyString), m_cmd_id(CMD_ERR_SYNTAX), m_short(wxEmptyString), m_verbose(wxEmptyString), m_params(CMD_PARAM_OPTIONAL), m_parent(NULL), m_app(app)
73 ~CCommandTree();
75 CCommandTree* AddCommand(const wxString& command, int cmd_id, const wxString& shortDesc, const wxString& longDesc, enum Params params = CMD_PARAM_OPTIONAL)
77 return AddCommand(new CCommandTree(m_app, command, cmd_id, shortDesc, longDesc, params));
80 int FindCommandId(const wxString& command, wxString& args, wxString& cmdstr) const;
81 wxString GetFullCommand() const;
82 void PrintHelpFor(const wxString& command) const;
84 private:
85 CCommandTree(CaMuleExternalConnector& app, const wxString& command, int cmd_id, const wxString& shortDesc, const wxString& longDesc, enum Params params)
86 : m_command(command), m_cmd_id(cmd_id), m_short(shortDesc), m_verbose(longDesc), m_params(params), m_parent(NULL), m_app(app)
89 CCommandTree* AddCommand(CCommandTree* cmdTree);
91 wxString m_command;
92 int m_cmd_id;
93 wxString m_short;
94 wxString m_verbose;
95 enum Params m_params;
96 const CCommandTree* m_parent;
97 CaMuleExternalConnector& m_app;
98 CmdList_t m_subcommands;
102 class CECFileConfig;
104 class CaMuleExternalConnector : public wxApp
106 public:
108 // Constructor & Destructor
110 CaMuleExternalConnector();
111 ~CaMuleExternalConnector();
114 // Virtual functions
116 virtual void Pre_Shell() {}
117 virtual void Post_Shell() {}
118 virtual int ProcessCommand(int) { return -1; }
119 virtual void TextShell(const wxString &prompt);
120 virtual void LoadConfigFile();
121 virtual void SaveConfigFile();
122 virtual void LoadAmuleConfig(CECFileConfig& cfg);
123 virtual void OnInitCommandSet();
124 virtual bool OnInit();
125 virtual const wxString GetGreetingTitle() = 0;
128 // Other functions
130 void Show(const wxString &s);
131 void DebugShow(const wxString &s) { if (m_Verbose) Show(s); }
132 const wxString& GetCmdArgs() const { return m_cmdargs; }
133 const wxString& GetLastCmdStr() const { return m_lastcmdstr; }
134 int GetIDFromString(const wxString& buffer) { return m_commands.FindCommandId(buffer, m_cmdargs, m_lastcmdstr); }
135 void Process_Answer(const wxString& answer);
136 bool Parse_Command(const wxString& buffer);
137 void GetCommand(const wxString &prompt, char* buffer, size_t buffer_size);
138 const CECPacket *SendRecvMsg_v2(const CECPacket *request) { return m_ECClient->SendRecvPacket(request); }
139 void ConnectAndRun(const wxString &ProgName, const wxString& ProgVersion);
140 void ShowGreet();
143 // Command line processing
145 void OnInitCmdLine(wxCmdLineParser& amuleweb_parser);
146 bool OnCmdLineParsed(wxCmdLineParser& parser);
148 // Exception and assert handling
149 void OnFatalException();
150 #ifdef __WXDEBUG__
151 void OnAssertFailure(const wxChar *file, int line, const wxChar *func, const wxChar *cond, const wxChar *msg);
152 #endif
154 CECFileConfig* m_configFile;
155 wxString m_configFileName;
157 protected:
158 // Set current locale, if language is not empty.
159 // returns canonical name of set (current) locale
160 virtual wxString SetLocale(const wxString& language);
162 long m_port;
163 wxString m_host;
164 CMD4Hash m_password;
165 bool m_KeepQuiet;
166 bool m_Verbose;
167 bool m_interactive;
168 CCommandTree m_commands;
169 const char * m_appname;
171 #if !wxUSE_GUI && defined(__WXMAC__)
172 virtual wxAppTraits* CreateTraits();
173 #endif
175 private:
176 wxString m_cmdargs;
177 wxString m_lastcmdstr;
178 CRemoteConnect* m_ECClient;
179 char * m_InputLine;
180 bool m_NeedsConfigSave;
181 wxString m_language;
182 wxLocale * m_locale;
183 char * m_strFullVersion;
184 char * m_strOSDescription;
187 #endif // __EXTERNALCONNECTOR_H__
188 // File_checked_for_headers