1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // misc.h - Miscellaneous Utilties
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 #ifndef MISC_H
13 #define MISC_H
14 
15 #include <wx/wx.h>
16 #include <wx/datetime.h>
17 #include <wx/tokenzr.h>
18 
19 #include "utils/misc.h"
20 
21 class wxImageList;
22 extern wxImageList *imageList;
23 
24 class wxHelpControllerBase;
25 
26 #define __(str) wxT(str)
27 
28 typedef unsigned long OID;
29 #define SSH_MAX_PASSWORD_LEN 256
30 
31 // we dont have an appropriate wxLongLong method
32 #ifdef __WIN32__
33 #define atolonglong _atoi64
34 #else
35 #ifdef __WXMAC__
36 #define atolonglong(str) strtoll(str, (char **)NULL, 10)
37 #else
38 #ifdef __FreeBSD__
39 #define atolonglong(str) strtoll(str, (char **)NULL, 10)
40 #else
41 #define atolonglong atoll
42 #endif
43 #endif
44 #endif
45 
46 #ifdef __WXMSW__
47 #define END_OF_LINE wxT("\r\n")
48 #else
49 #define END_OF_LINE wxT("\n")
50 #endif
51 
52 #if !defined(PGSCLI)
53 
54 #define wxCookieType wxTreeItemIdValue
55 
56 class sysSettings;
57 extern sysSettings *settings;
58 
59 #undef wxStaticCast
60 #define wxStaticCast(obj, className) ((className *)(obj))
61 
62 // making life easier
63 #define CTRL(id, typ)           (XRCCTRL(*this, id, typ))
64 
65 #define CTRL_STATIC(id)         (XRCCTRL(*this, id, wxStaticText))
66 #define CTRL_STATICBOX(id)      (XRCCTRL(*this, id, wxStaticBox))
67 #define CTRL_TEXT(id)           (XRCCTRL(*this, id, wxTextCtrl))
68 #define CTRL_TEXTNUMERIC(id)    ((wxTextNumericCtrl*)(XRCCTRL(*this, id, wxTextCtrl)))
69 #define CTRL_LISTBOX(id)        (XRCCTRL(*this, id, wxListBox))
70 #define CTRL_LISTCTRL(id)       (XRCCTRL(*this, id, wxListCtrl))
71 #define CTRL_COMBOBOX(id)       ((ctlComboBoxFix*)(XRCCTRL(*this, id, wxComboBox)))
72 #define CTRL_COMBOBOX1(id)      (XRCCTRL(*this, id, wxComboBox))
73 #define CTRL_LISTVIEW(id)       ((ctlListView*)(XRCCTRL(*this, id, wxListCtrl)))
74 #define CTRL_COMBOBOX2(id)      (XRCCTRL(*this, id, ctlComboBox))
75 #define CTRL_CHOICE(id)         (XRCCTRL(*this, id, wxChoice))
76 #define CTRL_CHECKBOX(id)       (XRCCTRL(*this, id, wxCheckBox))
77 #define CTRL_RADIOBOX(id)       (XRCCTRL(*this, id, wxRadioBox))
78 #define CTRL_BUTTON(id)         (XRCCTRL(*this, id, wxButton))
79 #define CTRL_CALENDAR(id)       (XRCCTRL(*this, id, wxCalendarBox))
80 #define CTRL_TIME(id)           (XRCCTRL(*this, id, wxTimeSpinCtrl))
81 #define CTRL_FLEXGRIDSIZER(id)  (XRCCTRL(*this, id, wxFlexGridSizer))
82 #define CTRL_PANEL(id)          (XRCCTRL(*this, id, wxPanel))
83 #define CTRL_SLIDER(id)         (XRCCTRL(*this, id, wxSlider))
84 #define CTRL_SQLBOX(id)         (XRCCTRL(*this, id, ctlSQLBox))
85 #define CTRL_RADIOBUTTON(id)    (XRCCTRL(*this, id, wxRadioButton))
86 #define CTRL_NOTEBOOK(id)       (XRCCTRL(*this, id, wxNotebook))
87 #define CTRL_SPIN(id)           (XRCCTRL(*this, id, wxSpinCtrl))
88 #define CTRL_CHECKLISTBOX(id)   (XRCCTRL(*this, id, wxCheckListBox))
89 #define CTRL_DATEPICK(id)       (XRCCTRL(*this, id, wxDatePickerCtrl))
90 #define CTRL_TREE(id)           (XRCCTRL(*this, id, ctlTree))
91 #define CTRL_COLOURPICKER(id)   (XRCCTRL(*this, id, ctlColourPicker))
92 #define CTRL_DIRPICKER(id)      (XRCCTRL(*this, id, wxDirPickerCtrl))
93 #define CTRL_FILEPICKER(id)     (XRCCTRL(*this, id, wxFilePickerCtrl))
94 #define CTRL_FONTPICKER(id)     (XRCCTRL(*this, id, wxFontPickerCtrl))
95 #define CTRL_CHECKTREEVIEW(id)  (XRCCTRL(*this, id, ctlCheckTreeView))
96 #define CTRL_GAUGE(id)          (XRCCTRL(*this, id, wxGauge))
97 
98 #endif // PGSCLI
99 
100 // Conversions
101 wxString BoolToStr(bool value);         // english; used for config values
102 wxString DateToAnsiStr(const wxDateTime &datetime);
103 
104 bool StrToBool(const wxString &value);  // english
105 long StrToLong(const wxString &value);
106 double StrToDouble(const wxString &value);
107 wxLongLong StrToLongLong(const wxString &value);
108 wxDateTime StrToDateTime(const wxString &value);
109 OID StrToOid(const wxString &value);
110 
111 wxString generate_spaces(int length);
112 
113 // nls aware
114 wxString BoolToYesNo(bool value);
115 wxString NumToStr(long value);
116 wxString NumToStr(double value);
117 wxString NumToStr(OID value);
118 wxString NumToStr(wxLongLong value);
119 wxString DateToStr(const wxDateTime &datetime);
120 wxString ElapsedTimeToStr(wxLongLong msec);
121 
122 
123 // Quoting
124 wxString qtConnString(const wxString &value); // connection strings always have single quotes escaped with backslash
125 
126 #if !defined(PGSCLI)
127 
128 // check if size/pos have reasonable values
129 void CheckOnScreen(wxWindow *win, wxPoint &pos, wxSize &size, const int w0 = 100, const int h0 = 70);
130 
131 // compile ID and Name into one string
132 wxString IdAndName(long id, const wxString &name);
133 
134 // Quoting
135 wxString qtDbStringDollar(const wxString &value);
136 wxString qtStrip(const wxString &value);    // remove \"
137 
138 
139 // string build helper
140 void AppendIfFilled(wxString &str, const wxString &delimiter, const wxString &what);
141 
142 // Create keyword list from PostgreSQL list
143 void FillKeywords(wxString &str);
144 
145 // Recreate a 9.0 datconfig
146 wxString TransformToNewDatconfig(const wxString &list);
147 
148 // Fill array, splitting the string separated by commas (maybe quoted elements)
149 void FillArray(wxArrayString &array, const wxString &str);
150 
151 
152 // splitting of strings, obeying quotes
153 class queryTokenizer : public wxStringTokenizer
154 {
155 public:
156 	queryTokenizer(const wxString &str, const wxChar delim = (wxChar)' ');
157 	wxString GetNextToken();
158 private:
159 	char delimiter;
160 };
161 
162 // Get an array from a comma(,) separated list
163 bool getArrayFromCommaSeparatedList(const wxString &str, wxArrayString &res);
164 
165 // File handling including encoding according to sysSettings if format<0,
166 // 0-> local charset, 1->utf8
167 wxString FileRead(const wxString &filename, int format = -1);
168 bool FileWrite(const wxString &filename, const wxString &data, int format = -1);
169 
170 typedef enum
171 {
172 	HELP_PGADMIN,
173 	HELP_POSTGRESQL,
174 	HELP_ENTERPRISEDB,
175 	HELP_GREENPLUM,
176 	HELP_SLONY
177 } HelpType;
178 
179 wxString CleanHelpPath(const wxString &path);
180 bool HelpPathValid(const wxString &path);
181 void DisplayHelp(const wxString &helpTopic, const HelpType helpType);
182 void DisplayPgAdminHelp(const wxString &helpTopic);
183 void DisplayExternalHelp(const wxString &helpTopic, const wxString &docPath, wxHelpControllerBase *helpCtl, const bool init);
184 
185 #ifndef WIN32
186 wxString ExecProcess(const wxString &cmd);
187 int ExecProcess(const wxString &command, wxArrayString &result);
188 #endif
189 
190 wxString GetHtmlEntity(const wxChar ch);
191 wxString HtmlEntities(const wxString &str);
192 
193 wxString firstLineOnly(const wxString &str);
194 
195 bool pgAppMinimumVersion(const wxString &cmd, const int majorVer, const int minorVer);
196 bool isPgApp(const wxString &app);
197 bool isEdbApp(const wxString &app);
198 bool isGpApp(const wxString &app);
199 
200 enum
201 {
202 	EDB_PACKAGE,
203 	EDB_PACKAGEFUNCTION,
204 	EDB_PACKAGEVARIABLE,
205 	EDB_SYNONYM,
206 
207 	PGM_CATALOG,
208 	PGM_CATALOGOBJECT,
209 	PGM_CHECK,
210 	PGM_COLUMN,
211 	PGM_CONSTRAINT,
212 	PGM_DATABASE,
213 	PGM_DOMAIN,
214 	PGM_EXCLUDE,
215 	PGM_FOREIGNKEY,
216 	PGM_FOREIGNSERVER,
217 	PGM_FOREIGNTABLE,
218 	PGM_USERMAPPING,
219 	PGM_FUNCTION,
220 	PGM_INDEX,
221 	PGM_OPCLASS,
222 	PGM_OPFAMILY,
223 	PGM_PRIMARYKEY,
224 	PGM_ROLE,
225 	PGM_RULE,
226 	PGM_SCHEMA,
227 	PGM_SERVER,
228 	PGM_SEQUENCE,
229 	PGM_TABLE,
230 	PGM_TABLESPACE,
231 	PGM_TRIGGER,
232 	PGM_UNKNOWN,
233 	PGM_UNIQUE,
234 	PGM_VIEW,
235 
236 	GP_EXTTABLE,
237 	GP_RESOURCE_QUEUE,
238 	GP_PARTITION,
239 
240 	PGM_JOB,
241 	PGM_SCHEDULE,
242 	PGM_STEP,
243 
244 	SLM_LISTEN,
245 	SLM_NODE,
246 	SLM_PATH,
247 	SLM_SEQUENCE,
248 	SLM_SET,
249 	SLM_SUBSCRIPTION,
250 	SLM_TABLE,
251 
252 	PGM_LANGUAGE,
253 	PGM_EVENTTRIGGER
254 };
255 
256 
257 enum        // depends on pgaFactory::addImage order!
258 {
259 	PGICON_PROPERTY,
260 	PGICON_STATISTICS,
261 	PGICON_PUBLIC
262 };
263 
264 // File/directory name cleanup
265 wxString sanitizePath(const wxString &path);
266 wxString commandLineCleanOption(const wxString &option, bool schemaObject = false);
267 #endif // PGSCLI
268 
269 // Quoting
270 wxString qtIdent(const wxString &value);    // add " if necessary
271 wxString qtTypeIdent(const wxString &value);    // add " if necessary
272 
273 #endif
274 
275