-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.cc
305 lines (249 loc) · 10.3 KB
/
home.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* home.cc ---
*
* Copyright (C) 2010 Alp Eren Köse
*
* Author: Alp Eren Köse <alperenkose@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3, or
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/*!
\file home.cc
\date Thu Jul 15 16:04:25 2010
\brief Implementation of HomeWidget Class
*/
#include <Wt/WPushButton>
#include <Wt/WAnchor>
#include <Wt/WBreak>
#include <Wt/WPanel>
#include <Wt/WTable>
#include <Wt/WInPlaceEdit>
#include <Wt/WLineEdit>
#include <Wt/WDefaultLoadingIndicator>
#include <Wt/WOverlayLoadingIndicator>
#include <fstream>
#include "harixApp.hpp"
#include "home.hpp"
#include "div.hpp" // Declaration of a WContainerWidget to represent an html <div>
using namespace Wt;
HomeWidget* HomeWidget::instance_ = NULL;
HomeWidget::HomeWidget( WContainerWidget* parent ) : WContainerWidget(parent)
{
parent_ = parent;
/*
* Header Part for navigation buttons
* @{
*/
WContainerWidget *result;
addWidget( result = new WContainerWidget() );
Div *header = new Div(result, "header");
Div *logo = new Div(header, "widgparty");
logo->addWidget( new WText("<p>hari<a>X</a></p>") );
Div *content = new Div(result, "content");
Div *menu = new Div(content, "menu");
WContainerWidget *menulist = new WContainerWidget(menu);
menulist->setList(true);
WAnchor *aHome;
menulist->addWidget( aHome = new WAnchor("","Home") );
aHome->setRefInternalPath("/home");
aHome->setStyleClass("selected");
WAnchor *aAnalyzeOs;
menulist->addWidget( aAnalyzeOs = new WAnchor("","Analyze OS") );
aAnalyzeOs->setRefInternalPath("/analyze_os");
WAnchor *aQueryDevices;
menulist->addWidget( aQueryDevices = new WAnchor("","Query Devices") );
aQueryDevices->setRefInternalPath("/query_devices");
WAnchor *aMainboards;
menulist->addWidget( aMainboards = new WAnchor("","Mainboards") );
aMainboards->setRefInternalPath("/mainboards");
/*
Container 'page' holding contents shown in page apart from the Header Part.
Anything else from the Header to be shown in page is added to 'page' container.
*/
Div *page = new Div(content, "pagearea");
Div *footer = new Div(result, "footr");
footer->addWidget( new WText("<p>HARdware Information management<br />"
"This site is best viewed in a Firefox browser!</p>") );
/*
* @}
*/
WAnchor *aManual;
page->addWidget( new WText("Wellcome to the Linux Hardware Information Management Application, <br />"
"For usage instructions and what hariX is please see the ") );
page->addWidget( aManual = new WAnchor("harixManual.pdf","User Manual.") );
aManual->setTarget(TargetNewWindow);
page->addWidget( new WText("<br/><br/>"
"Don't forget to update the PCI and Class Codes occasionally at here:"
"<br/>"));
page->addWidget( bPciIds_ = new WPushButton("Update PCI IDs") );
// Trigger bPciIds_Click function on 'Update PCI IDs' click.
bPciIds_->clicked().connect(this, &HomeWidget::bPciIds_Click);
/*
* Settings Panel
* @{
*/
WPanel *panelSettings;
WTable *layoutSettings;
page->addWidget( panelSettings = new WPanel() );
panelSettings->setTitle("Settings");
panelSettings->setCollapsible(true);
panelSettings->collapse();
panelSettings->setCentralWidget( layoutSettings = new WTable() );
layoutSettings->columnAt(0)->setWidth( WLength(100) );
layoutSettings->rowAt(0)->setHeight( WLength(40) );
layoutSettings->elementAt(0,0)->addWidget( new WText("<p>HTTP Proxy:</p>") );
layoutSettings->elementAt(0,1)->addWidget( new WText("Proxy form -> http://[proxy_address]:[port]") );
layoutSettings->elementAt(0,1)->addWidget( new WBreak() );
layoutSettings->elementAt(0,1)->addWidget( editProxy_ = new WLineEdit() );
editProxy_->resize( WLength(200), WLength() );
WPushButton *bSaveProxy;
layoutSettings->elementAt(0,1)->addWidget( bSaveProxy = new WPushButton("Save") );
layoutSettings->rowAt(1)->setHeight( WLength(30) );
layoutSettings->elementAt(1,1)->setContentAlignment(AlignMiddle);
WAnchor *aDoc;
layoutSettings->elementAt(1,1)->addWidget( aDoc = new WAnchor("doc/html/index.html",
"--- Documentation of Source Code ---") );
aDoc->setTarget(TargetNewWindow);
/*
* @}
*/
readProxyFile();
editProxy_->setText( settingsProxy_ );
editProxy_->setEmptyText("No Proxy Set!");
// Trigger changeProxy function on 'Save'(bSaveProxy) button click.
bSaveProxy->clicked().connect(this, &HomeWidget::changeProxy);
}
HomeWidget* HomeWidget::Instance( WContainerWidget* parent)
{
if( parent == 0 ){
if ( !instance_ ){
// we didn't receive a parent and there is no instance of HomeWidget, so return NULL..
return NULL;
}
}
else{ // We received a parent container
if( !instance_ ){
instance_ = new HomeWidget(parent); // No instance of HomeWidget, lets create one.
}
// else don't care, we already have an instance we will return that.
}
return instance_;
}
//! Path to the "pci.ids" file to be downloaded and read.
static std::string pci_ids_file = "/tmp/pci.ids";
/*
Global function from pci_ids/pci_ids.cc to update PCI IDs in database,
gets 'pci.ids' file as parameter.
*/
//! \relates pci_ids.cc
int update_pci_ids(std::string pci_ids_file);
void HomeWidget::bPciIds_Click()
{
// Change server busy indicator.
Wt::WApplication::instance()->setLoadingIndicator( loading_ = new Wt::WOverlayLoadingIndicator() );
loading_->setMessage("Updating..");
/*
* Set the download command appropriately depending on proxy address existance.
* @{
*/
std::string download_command;
if( settingsProxy_ != "" )
download_command =
"curl http://pciids.sourceforge.net/v2.2/pci.ids --proxy "+ settingsProxy_ +" > " + pci_ids_file;
else
download_command =
"curl http://pciids.sourceforge.net/v2.2/pci.ids > " + pci_ids_file;
/*
* @}
*/
if( system( download_command.c_str() ) == 0 ){ // Try to download the 'pci.ids' file from net.
dialogDownload_ = new WDialog("PCI IDs Download");
new WText("Downloaded pci.ids file! <br />"
"Now We will update the PCI IDs database", dialogDownload_->contents() );
new WBreak( dialogDownload_->contents() );
WPushButton* dialogDownloadOk = new WPushButton("Ok", dialogDownload_->contents() );
// Sets WDialog::DialogCode to Accepted if OK is clicked.
dialogDownloadOk->clicked().connect( SLOT(dialogDownload_, WDialog::accept) );
WPushButton* dialogDownloadCancel = new WPushButton("Cancel", dialogDownload_->contents() );
// Sets WDialog::DialogCode to Rejected if Cancel is clicked.
dialogDownloadCancel->clicked().connect( SLOT(dialogDownload_, WDialog::reject) );
dialogDownload_->show();
}
else { // 'pci.ids' Download failed!
dialogDownload_ = new WDialog("PCI IDs Download");
new WText("Failed to download pci.ids!!<br />"
"Check proxy settings..", dialogDownload_->contents() );
new WBreak( dialogDownload_->contents() );
WPushButton* dialogDownloadFail = new WPushButton("Close", dialogDownload_->contents() );
// Sets WDialog::DialogCode to Rejected when Close is clicked.
dialogDownloadFail->clicked().connect( SLOT(dialogDownload_, WDialog::reject) );
dialogDownload_->show();
}
// Trigger 'dialogDownload_Close' function on 'dialogDownload_' dialog window close.
dialogDownload_->finished().connect(this, &HomeWidget::dialogDownload_Close);
}
void HomeWidget::dialogDownload_Close( WDialog::DialogCode code )
{
if ( code == WDialog::Accepted ){ // if download is successful and user not rejected update.
dialogPciIds_ = new WDialog("PCI IDs Update");
if( update_pci_ids( pci_ids_file ) == 0 ){ // Try to update PCI IDs.
new WText("PCI IDs succesfully updated!", dialogPciIds_->contents() );
new WBreak( dialogPciIds_->contents() );
WPushButton* dialogPciIdsOk = new WPushButton("Ok", dialogPciIds_->contents());
(*dialogPciIdsOk).clicked().connect( SLOT(dialogPciIds_, WDialog::accept) );
dialogPciIds_->show();
}
else{ // PCI IDs update FAILED.
new WText("FAILED to update PCI IDs!!", dialogPciIds_->contents() );
new WBreak( dialogPciIds_->contents() );
WPushButton* dialogPciIdsFail = new WPushButton("Close", dialogPciIds_->contents());
(*dialogPciIdsFail).clicked().connect( SLOT(dialogPciIds_, WDialog::reject) );
dialogPciIds_->show();
}
dialogPciIds_->finished().connect(this, &HomeWidget::dialogPci_Close);
}
// revert back the server busy indicator to its default.
WApplication::instance()->setLoadingIndicator( new WDefaultLoadingIndicator() );
delete dialogDownload_;
}
void HomeWidget::dialogPci_Close( WDialog::DialogCode code )
{
delete dialogPciIds_;
}
void HomeWidget::changeProxy()
{
settingsProxy_ = editProxy_->text().narrow(); // Put the given proxy by user into settingsProxy_ variable.
std::string save_command = "echo "+ settingsProxy_ +" > /tmp/proxy.harix"; // Command string to save proxy.
if( system( save_command.c_str() ) != 0 ){ // Try to store the proxy address in server for later use.
// Warn in case of save failure! Proxy address couldn't be saved in file.
dialogWarn_ = new WDialog("Proxy Configuration");
new WText("Oops we failed to save file /tmp/proxy.harix!! <br /> "
"Proxy settings will be valid only for the current session!", dialogWarn_->contents() );
new WBreak( dialogWarn_->contents() );
WPushButton* dialogWarnFail = new WPushButton("Close", dialogWarn_->contents() );
dialogWarnFail->clicked().connect( SLOT(dialogWarn_, WDialog::reject) );
dialogWarn_->show();
dialogWarn_->finished().connect(this, &HomeWidget::dialogWarn_Close);
}
}
void HomeWidget::dialogWarn_Close( Wt::WDialog::DialogCode code )
{
delete dialogWarn_;
}
void HomeWidget::readProxyFile()
{
std::ifstream proxy_file( "/tmp/proxy.harix" );
getline( proxy_file, settingsProxy_ ); // read previously stored proxy address into settingsProxy_ variable.
}