Alarm browser improved
[public/netxms.git] / src / client / console / libnxmc / tools.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Portable management console - plugin API library
4 ** Copyright (C) 2007 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: tools.cpp
21 **
22 **/
23
24 #include "libnxmc.h"
25
26
27 //
28 // Format time stamp
29 //
30
31 TCHAR LIBNXMC_EXPORTABLE *NXMCFormatTimeStamp(time_t timeStamp, TCHAR *buffer, int type)
32 {
33 struct tm *pt;
34 static TCHAR *format[] = { _T("%d-%b-%Y %H:%M:%S"), _T("%H:%M:%S"), _T("%b/%d"), _T("%b") };
35
36 pt = localtime(&timeStamp);
37 if (pt != NULL)
38 _tcsftime(buffer, 32, format[type], pt);
39 else
40 _tcscpy(buffer, _T("(null)"));
41 return buffer;
42 }
43
44
45 //
46 // Translate given code to text
47 //
48
49 const TCHAR LIBNXMC_EXPORTABLE *NXMCCodeToText(int code, CODE_TO_TEXT *translator, const TCHAR *defaultText)
50 {
51 int i;
52
53 for(i = 0; translator[i].text != NULL; i++)
54 if (translator[i].code == code)
55 return translator[i].text;
56 return defaultText;
57 }
58
59
60 //
61 // Load resource file
62 //
63
64 bool LIBNXMC_EXPORTABLE NXMCLoadResources(const TCHAR *name, NXMC_LIB_INSTANCE instance, TCHAR *resName)
65 {
66 bool success = false;
67 #ifdef _WIN32
68 HRSRC hRes;
69 HGLOBAL hMem;
70 void *data;
71
72 hRes = FindResource(instance, resName, _T("XRS"));
73 if (hRes != NULL)
74 {
75 hMem = LoadResource(instance, hRes);
76 if (hMem != NULL)
77 {
78 data = LockResource(hMem);
79 if (data != NULL)
80 {
81 wxMemoryFSHandler::AddFile(name, data, SizeofResource(instance, hRes));
82 UnlockResource(hMem);
83 }
84 FreeResource(hMem);
85 }
86 else
87 {
88 wxLogWarning(_T("NXMCLoadResources: cannot load resource %s from executable"), name);
89 return false;
90 }
91 }
92 else
93 {
94 wxLogWarning(_T("NXMCLoadResources: cannot find resource %s in executable module"), name);
95 return false;
96 }
97 wxString xrsFile = _T("memory:");
98 #else
99 wxString xrsFile = wxStandardPaths::Get().GetResourcesDir();
100 xrsFile += FS_PATH_SEPARATOR;
101 #endif
102 xrsFile += name;
103 wxLogDebug(_T("Loading resource file %s"), xrsFile.c_str());
104 return wxXmlResource::Get()->Load(xrsFile);
105 }
106
107
108 //
109 // Save dimensions of all list control columns into config
110 //
111
112 void LIBNXMC_EXPORTABLE NXMCSaveListCtrlColumns(wxConfigBase *cfg, wxListCtrl &wndListCtrl, const TCHAR *prefix)
113 {
114 int i, count;
115 wxString path = cfg->GetPath();
116 TCHAR item[64];
117
118 cfg->SetPath(prefix);
119 count = wndListCtrl.GetColumnCount();
120 cfg->Write(_T("ColumnCount"), count);
121 for(i = 0; i < count; i++)
122 {
123 _stprintf(item, _T("Column%d"), i);
124 cfg->Write(item, wndListCtrl.GetColumnWidth(i));
125 }
126 cfg->SetPath(path);
127 }
128
129
130 //
131 // Load and set dimensions of all list control columns
132 //
133
134 void LIBNXMC_EXPORTABLE NXMCLoadListCtrlColumns(wxConfigBase *cfg, wxListCtrl &wndListCtrl, const TCHAR *prefix)
135 {
136 long i, count, width;
137 wxString path = cfg->GetPath();
138 TCHAR item[64];
139
140 cfg->SetPath(prefix);
141 cfg->Read(_T("ColumnCount"), &count, 0);
142
143 for(i = 0; i < count; i++)
144 {
145 _stprintf(item, _T("Column%d"), i);
146 cfg->Read(item, &width, 50);
147 wndListCtrl.SetColumnWidth(i, width);
148 }
149
150 cfg->SetPath(path);
151 }