Revert r10854
[amule.git] / src / ExternalConnector.h
blob38b731cdbd403fdc3ef2dcb25874e28b33e32fae
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 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.
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 SendPacket(const CECPacket *request) { m_ECClient->SendPacket(request); }
140 void ConnectAndRun(const wxString &ProgName, const wxString& ProgVersion);
141 void ShowGreet();
144 // Command line processing
146 void OnInitCmdLine(wxCmdLineParser& amuleweb_parser, const char* appname);
147 bool OnCmdLineParsed(wxCmdLineParser& parser);
149 #if wxUSE_ON_FATAL_EXCEPTION
150 // Exception and assert handling
151 void OnFatalException();
152 #endif
153 #ifdef __WXDEBUG__
154 void OnAssertFailure(const wxChar *file, int line, const wxChar *func, const wxChar *cond, const wxChar *msg);
155 #endif
157 CECFileConfig* m_configFile;
158 wxString m_configFileName;
160 protected:
161 // Set current locale, if language is not empty.
162 // returns canonical name of set (current) locale
163 virtual wxString SetLocale(const wxString& language);
165 long m_port;
166 wxString m_host;
167 CMD4Hash m_password;
168 bool m_ZLIB;
169 bool m_KeepQuiet;
170 bool m_Verbose;
171 bool m_interactive;
172 CCommandTree m_commands;
173 const char * m_appname;
175 #if !wxUSE_GUI && defined(__WXMAC__) && !wxCHECK_VERSION(2, 9, 0)
176 virtual wxAppTraits* CreateTraits();
177 #endif
179 private:
180 wxString m_cmdargs;
181 wxString m_lastcmdstr;
182 CRemoteConnect* m_ECClient;
183 char * m_InputLine;
184 bool m_NeedsConfigSave;
185 wxString m_language;
186 wxLocale * m_locale;
187 char * m_strFullVersion;
188 char * m_strOSDescription;
191 #endif // __EXTERNALCONNECTOR_H__
192 // File_checked_for_headers