PoorMan: Close the application when you choose Cancel on first launch.

* If you choose cancel on that first dialog, also don't save a config file
* Also fixes the button labelling in the preference panel (several were clipped)
* Allow the user to cancel the selection of the web folder and return to the initial dialog (to choose Default, for example)
* Make the preference panel closable with escape key
* Multiple coding style changes

(Ref: Ticket #4255)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32486 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Philippe Saint-Pierre
2009-08-18 00:32:57 +00:00
parent f940f1e99c
commit d939e03d05
8 changed files with 345 additions and 248 deletions
+44 -7
View File
@@ -23,19 +23,20 @@ PoorManApplication::PoorManApplication()
mainWindow->Show(); mainWindow->Show();
BDirectory webDir; BDirectory webDir;
if(mainWindow->ReadSettings() != B_OK){ if (mainWindow->ReadSettings() != B_OK) {
if(webDir.SetTo(STR_DEFAULT_WEB_DIRECTORY) != B_OK) if (webDir.SetTo(STR_DEFAULT_WEB_DIRECTORY) != B_OK)
mainWindow->DefaultSettings(); mainWindow->DefaultSettings();
else
PostMessage(kStartServer);
} else { } else {
if(webDir.SetTo(mainWindow->WebDir()) != B_OK) if (webDir.SetTo(mainWindow->WebDir()) != B_OK)
mainWindow->DefaultSettings(); mainWindow->DefaultSettings();
else
PostMessage(kStartServer);
} }
mainWindow->StartServer();
mainWindow->SetDirLabel(mainWindow->WebDir());
mainWindow->Show();
} }
void void
PoorManApplication::AboutRequested() PoorManApplication::AboutRequested()
{ {
@@ -43,3 +44,39 @@ PoorManApplication::AboutRequested()
STR_ABOUT_DESC, STR_ABOUT_BUTTON); STR_ABOUT_DESC, STR_ABOUT_BUTTON);
aboutBox->Go(); aboutBox->Go();
} }
void
PoorManApplication::MessageReceived(BMessage *message)
{
switch (message->what) {
case MSG_FILE_PANEL_SELECT_WEB_DIR:
mainWindow->MessageReceived(message);
break;
case kStartServer:
mainWindow->StartServer();
mainWindow->SetDirLabel(mainWindow->WebDir());
mainWindow->Show();
break;
case B_CANCEL: {
BDirectory webDir;
if (mainWindow->ReadSettings() != B_OK) {
if (webDir.SetTo(STR_DEFAULT_WEB_DIRECTORY) != B_OK)
mainWindow->DefaultSettings();
else
mainWindow->StartServer();
} else {
if (webDir.SetTo(mainWindow->WebDir()) != B_OK)
mainWindow->DefaultSettings();
else
mainWindow->StartServer();
}
}
break;
default:
BApplication::MessageReceived(message);
}
}
+4 -3
View File
@@ -18,9 +18,10 @@ class PoorManWindow;
class PoorManApplication: public BApplication class PoorManApplication: public BApplication
{ {
public: public:
PoorManApplication(); PoorManApplication();
void AboutRequested(); void AboutRequested();
PoorManWindow * GetPoorManWindow() { return mainWindow; } virtual void MessageReceived(BMessage *message);
PoorManWindow * GetPoorManWindow() { return mainWindow; }
private: private:
PoorManWindow * mainWindow; PoorManWindow * mainWindow;
//PoorManPreferencesWindow * prefWindow; //PoorManPreferencesWindow * prefWindow;
+3 -2
View File
@@ -67,7 +67,7 @@ PoorManLoggingView::PoorManLoggingView(BRect rect, const char *name)
logFileName = new BTextControl(tempRect, "File Name", STR_TXT_LOG_FILE_NAME, NULL, NULL); logFileName = new BTextControl(tempRect, "File Name", STR_TXT_LOG_FILE_NAME, NULL, NULL);
logFileName->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT); logFileName->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
logFileName->SetDivider(73.0); logFileName->SetDivider(fileLogging->StringWidth(STR_TXT_LOG_FILE_NAME) + 8.0f);
SetLogFileName(win->LogPath()); SetLogFileName(win->LogPath());
fileLogging->AddChild(logFileName); fileLogging->AddChild(logFileName);
@@ -75,7 +75,8 @@ PoorManLoggingView::PoorManLoggingView(BRect rect, const char *name)
BRect createLogFileRect; BRect createLogFileRect;
createLogFileRect.top = tempRect.bottom + 13.0; createLogFileRect.top = tempRect.bottom + 13.0;
createLogFileRect.right = tempRect.right + 2.0; createLogFileRect.right = tempRect.right + 2.0;
createLogFileRect.left = createLogFileRect.right - 87.0; createLogFileRect.left = createLogFileRect.right
- fileLogging->StringWidth("Create Log File") - 24.0;
createLogFileRect.bottom = createLogFileRect.top + 19.0; createLogFileRect.bottom = createLogFileRect.top + 19.0;
createLogFile = new BButton(createLogFileRect, "Create Log File", STR_BTN_CREATE_LOG_FILE, new BMessage(MSG_PREF_LOG_BTN_CREATE_FILE)); createLogFile = new BButton(createLogFileRect, "Create Log File", STR_BTN_CREATE_LOG_FILE, new BMessage(MSG_PREF_LOG_BTN_CREATE_FILE));
+70 -59
View File
@@ -17,11 +17,11 @@
#include "PoorManServer.h" #include "PoorManServer.h"
PoorManPreferencesWindow::PoorManPreferencesWindow(BRect frame, char * name) PoorManPreferencesWindow::PoorManPreferencesWindow(BRect frame, char * name)
: BWindow(frame, name, B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE), : BWindow(frame, name, B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE
| B_CLOSE_ON_ESCAPE),
webDirFilePanel(NULL), webDirFilePanel(NULL),
logFilePanel(NULL) logFilePanel(NULL)
{ {
frame = Bounds(); frame = Bounds();
prefView = new PoorManView(frame, STR_WIN_NAME_PREF); prefView = new PoorManView(frame, STR_WIN_NAME_PREF);
@@ -30,7 +30,6 @@ PoorManPreferencesWindow::PoorManPreferencesWindow(BRect frame, char * name)
AddChild(prefView); AddChild(prefView);
// Button View // Button View
BRect buttonRect; BRect buttonRect;
buttonRect = Bounds(); buttonRect = Bounds();
@@ -42,26 +41,27 @@ PoorManPreferencesWindow::PoorManPreferencesWindow(BRect frame, char * name)
// Buttons // Buttons
float buttonTop = 0.0f; float buttonTop = 0.0f;
float buttonWidth = 52.0f;
float buttonHeight = 26.0f; float buttonHeight = 26.0f;
float buttonLeft = 265.0f;
BRect button1; float widthCancel = prefView->StringWidth("Cancel") + 24.0f;
button1 = buttonView->Bounds(); float widthDone = prefView->StringWidth("Done") + 24.0f;
button1.Set(buttonLeft, buttonTop, buttonLeft + buttonWidth, buttonTop + buttonHeight);
cancelButton = new BButton(button1, "Cancel Button", "Cancel", new BMessage(MSG_PREF_BTN_CANCEL));
buttonLeft = 325.0f; float gap = 5.0f;
BRect button2;
button2 = buttonView->Bounds(); BRect button1(prefView->Bounds().Width() - 2 * gap - widthCancel
button2.Set(buttonLeft, buttonTop, buttonLeft + buttonWidth, buttonTop + buttonHeight); - widthDone, buttonTop, prefView->Bounds().Width() - 2 * gap - widthDone,
doneButton = new BButton(button2, "Done Button", "Done", new BMessage(MSG_PREF_BTN_DONE)); buttonTop + buttonHeight);
cancelButton = new BButton(button1, "Cancel Button", "Cancel",
new BMessage(MSG_PREF_BTN_CANCEL));
BRect button2(prefView->Bounds().Width() - gap - widthDone, buttonTop,
prefView->Bounds().Width() - gap, buttonTop + buttonHeight);
doneButton = new BButton(button2, "Done Button", "Done",
new BMessage(MSG_PREF_BTN_DONE));
buttonView->AddChild(cancelButton); buttonView->AddChild(cancelButton);
buttonView->AddChild(doneButton); buttonView->AddChild(doneButton);
// Create tabs // Create tabs
BRect r; BRect r;
r = Bounds(); r = Bounds();
@@ -115,88 +115,95 @@ PoorManPreferencesWindow::PoorManPreferencesWindow(BRect frame, char * name)
logFilePanel->SetButtonLabel(B_DEFAULT_BUTTON, "Create"); logFilePanel->SetButtonLabel(B_DEFAULT_BUTTON, "Create");
change_title = logFilePanel->Window(); change_title = logFilePanel->Window();
change_title->SetTitle(STR_FILEPANEL_CREATE_LOG_FILE); change_title->SetTitle(STR_FILEPANEL_CREATE_LOG_FILE);
Show();
} }
PoorManPreferencesWindow::~PoorManPreferencesWindow() PoorManPreferencesWindow::~PoorManPreferencesWindow()
{ {
delete logFilePanel; delete logFilePanel;
delete webDirFilePanel; delete webDirFilePanel;
} }
void void
PoorManPreferencesWindow::MessageReceived(BMessage* message) PoorManPreferencesWindow::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case MSG_PREF_BTN_DONE: case MSG_PREF_BTN_DONE:
PoorManWindow * win; PoorManWindow* win;
PoorManServer* server; PoorManServer* server;
win = ((PoorManApplication *)be_app)->GetPoorManWindow(); win = ((PoorManApplication *)be_app)->GetPoorManWindow();
server = win->GetServer(); server = win->GetServer();
PRINT(("Pref Window: sendDir CheckBox: %d\n", siteView->SendDirValue())); PRINT(("Pref Window: sendDir CheckBox: %d\n",
siteView->SendDirValue()));
server->SetListDir(siteView->SendDirValue()); server->SetListDir(siteView->SendDirValue());
win->SetDirListFlag(siteView->SendDirValue()); win->SetDirListFlag(siteView->SendDirValue());
PRINT(("Pref Window: indexFileName TextControl: %s\n", siteView->IndexFileName())); PRINT(("Pref Window: indexFileName TextControl: %s\n",
if(server->SetIndexName(siteView->IndexFileName()) == B_OK) siteView->IndexFileName()));
if (server->SetIndexName(siteView->IndexFileName()) == B_OK)
win->SetIndexFileName(siteView->IndexFileName()); win->SetIndexFileName(siteView->IndexFileName());
PRINT(("Pref Window: webDir: %s\n", siteView->WebDir())); PRINT(("Pref Window: webDir: %s\n", siteView->WebDir()));
if(server->SetWebDir(siteView->WebDir()) == B_OK){ if (server->SetWebDir(siteView->WebDir()) == B_OK) {
win->SetWebDir(siteView->WebDir()); win->SetWebDir(siteView->WebDir());
win->SetDirLabel(siteView->WebDir()); win->SetDirLabel(siteView->WebDir());
} }
PRINT(("Pref Window: logConsole CheckBox: %d\n", loggingView->LogConsoleValue())); PRINT(("Pref Window: logConsole CheckBox: %d\n",
loggingView->LogConsoleValue()));
win->SetLogConsoleFlag(loggingView->LogConsoleValue()); win->SetLogConsoleFlag(loggingView->LogConsoleValue());
PRINT(("Pref Window: logFile CheckBox: %d\n", loggingView->LogFileValue())); PRINT(("Pref Window: logFile CheckBox: %d\n",
loggingView->LogFileValue()));
win->SetLogFileFlag(loggingView->LogFileValue()); win->SetLogFileFlag(loggingView->LogFileValue());
PRINT(("Pref Window: logFileName: %s\n", loggingView->LogFileName())); PRINT(("Pref Window: logFileName: %s\n",
loggingView->LogFileName()));
win->SetLogPath(loggingView->LogFileName()); win->SetLogPath(loggingView->LogFileName());
PRINT(("Pref Window: MaxConnections Slider: %ld\n", advancedView->MaxSimultaneousConnections())); PRINT(("Pref Window: MaxConnections Slider: %ld\n",
advancedView->MaxSimultaneousConnections()));
server->SetMaxConns(advancedView->MaxSimultaneousConnections()); server->SetMaxConns(advancedView->MaxSimultaneousConnections());
win->SetMaxConnections((int16)advancedView->MaxSimultaneousConnections()); win->SetMaxConnections(
(int16)advancedView->MaxSimultaneousConnections());
if (Lock()) if (Lock())
Quit(); Quit();
break; break;
case MSG_PREF_BTN_CANCEL: case MSG_PREF_BTN_CANCEL:
if (Lock()) if (Lock())
Quit(); Quit();
break; break;
case MSG_PREF_SITE_BTN_SELECT: case MSG_PREF_SITE_BTN_SELECT:
// Select the Web Directory, root directory to look in. // Select the Web Directory, root directory to look in.
webDirFilePanel->SetTarget(this);
webDirFilePanel->SetMessage(new BMessage(MSG_FILE_PANEL_SELECT_WEB_DIR));
if (!webDirFilePanel->IsShowing()) if (!webDirFilePanel->IsShowing())
webDirFilePanel->Show(); webDirFilePanel->Show();
break; break;
case MSG_FILE_PANEL_SELECT_WEB_DIR: case MSG_FILE_PANEL_SELECT_WEB_DIR:
// handle the open BMessage from the Select Web Directory File Panel // handle the open BMessage from the Select Web Directory File Panel
PRINT(("Select Web Directory:\n")); PRINT(("Select Web Directory:\n"));
SelectWebDir(message); SelectWebDir(message);
break; break;
case MSG_PREF_LOG_BTN_CREATE_FILE: case MSG_PREF_LOG_BTN_CREATE_FILE:
// Create the Log File // Create the Log File
logFilePanel->Show(); logFilePanel->Show();
break; break;
case MSG_FILE_PANEL_CREATE_LOG_FILE: case MSG_FILE_PANEL_CREATE_LOG_FILE:
// handle the save BMessage from the Create Log File Panel // handle the save BMessage from the Create Log File Panel
PRINT(("Create Log File:\n")); PRINT(("Create Log File:\n"));
CreateLogFile(message); CreateLogFile(message);
break; break;
case MSG_PREF_ADV_SLD_MAX_CONNECTION: case MSG_PREF_ADV_SLD_MAX_CONNECTION:
max_connections = advancedView->MaxSimultaneousConnections(); max_connections = advancedView->MaxSimultaneousConnections();
PRINT(("Max Connections: %ld\n", max_connections)); PRINT(("Max Connections: %ld\n", max_connections));
break; break;
default:
default: BWindow::MessageReceived(message);
BWindow::MessageReceived(message); break;
break;
} }
} }
void void
PoorManPreferencesWindow::SelectWebDir(BMessage * message) PoorManPreferencesWindow::SelectWebDir(BMessage * message)
{ {
@@ -221,18 +228,21 @@ PoorManPreferencesWindow::SelectWebDir(BMessage * message)
siteView->SetWebDir(path.Path()); siteView->SetWebDir(path.Path());
bool temp; bool temp;
if(message->FindBool("Default Dialog", &temp) == B_OK){ if (message->FindBool("Default Dialog", &temp) == B_OK) {
PoorManWindow* win = ((PoorManApplication *)be_app)->GetPoorManWindow(); PoorManWindow* win = ((PoorManApplication *)be_app)->GetPoorManWindow();
if(win->GetServer()->SetWebDir(siteView->WebDir()) == B_OK){ win->StartServer();
if (win->GetServer()->SetWebDir(siteView->WebDir()) == B_OK) {
win->SetWebDir(siteView->WebDir()); win->SetWebDir(siteView->WebDir());
win->SetDirLabel(siteView->WebDir()); win->SetDirLabel(siteView->WebDir());
win->SaveSettings();
win->Show();
} }
win->StartServer(); if (Lock())
if(Lock())
Quit(); Quit();
} }
} }
void void
PoorManPreferencesWindow::CreateLogFile(BMessage * message) PoorManPreferencesWindow::CreateLogFile(BMessage * message)
{ {
@@ -255,8 +265,7 @@ PoorManPreferencesWindow::CreateLogFile(BMessage * message)
path.Append(name); path.Append(name);
PRINT(("Log File: %s\n", path.Path())); PRINT(("Log File: %s\n", path.Path()));
if (err == B_OK) if (err == B_OK) {
{
loggingView->SetLogFileName(path.Path()); loggingView->SetLogFileName(path.Path());
loggingView->SetLogFileValue(true); loggingView->SetLogFileValue(true);
} }
@@ -264,14 +273,16 @@ PoorManPreferencesWindow::CreateLogFile(BMessage * message)
// mark the checkbox // mark the checkbox
} }
/*A special version for "the default dialog", don't use it in MessageReceived()*/ /*A special version for "the default dialog", don't use it in MessageReceived()*/
void void
PoorManPreferencesWindow::ShowWebDirFilePanel() PoorManPreferencesWindow::ShowWebDirFilePanel()
{ {
BMessage message(MSG_FILE_PANEL_SELECT_WEB_DIR); BMessage message(MSG_FILE_PANEL_SELECT_WEB_DIR);
message.AddBool("Default Dialog", true); message.AddBool("Default Dialog", true);
webDirFilePanel->SetTarget(be_app);
webDirFilePanel->SetMessage(&message); webDirFilePanel->SetMessage(&message);
if(!webDirFilePanel->IsShowing()) if (!webDirFilePanel->IsShowing())
webDirFilePanel->Show(); webDirFilePanel->Show();
} }
+92 -77
View File
@@ -28,11 +28,12 @@
#include "PoorManWindow.h" #include "PoorManWindow.h"
#include "libhttpd/libhttpd.h" #include "libhttpd/libhttpd.h"
PoorManServer::PoorManServer(const char* webDir, PoorManServer::PoorManServer(const char* webDir,
int32 maxConns, bool listDir,const char* idxName) int32 maxConns, bool listDir,const char* idxName)
:fIsRunning(false), :fIsRunning(false),
fMaxConns(maxConns), fMaxConns(maxConns),
fIndexName(new char[strlen(idxName)+1]), fIndexName(new char[strlen(idxName) + 1]),
fCurConns(0) fCurConns(0)
{ {
fHttpdServer = httpd_initialize( fHttpdServer = httpd_initialize(
@@ -59,7 +60,7 @@ PoorManServer::PoorManServer(const char* webDir,
strcpy(fIndexName, idxName); strcpy(fIndexName, idxName);
size_t cwdLen = strlen(fHttpdServer->cwd); size_t cwdLen = strlen(fHttpdServer->cwd);
if(fHttpdServer->cwd[cwdLen-1] == '/'){ if (fHttpdServer->cwd[cwdLen-1] == '/') {
fHttpdServer->cwd[cwdLen-1] = '\0'; fHttpdServer->cwd[cwdLen-1] = '\0';
} }
@@ -70,6 +71,7 @@ PoorManServer::PoorManServer(const char* webDir,
pthread_rwlock_init(&fIndexNameLock, NULL); pthread_rwlock_init(&fIndexNameLock, NULL);
} }
PoorManServer::~PoorManServer() PoorManServer::~PoorManServer()
{ {
Stop(); Stop();
@@ -79,20 +81,21 @@ PoorManServer::~PoorManServer()
pthread_rwlock_destroy(&fIndexNameLock); pthread_rwlock_destroy(&fIndexNameLock);
} }
status_t PoorManServer::Run() status_t PoorManServer::Run()
{ {
if(chdir(fHttpdServer->cwd) == -1){ if (chdir(fHttpdServer->cwd) == -1) {
poorman_log("no web directory, can't start up.\n", false, INADDR_NONE, RED); poorman_log("no web directory, can't start up.\n", false, INADDR_NONE, RED);
return B_ERROR; return B_ERROR;
} }
httpd_sockaddr sa4; httpd_sockaddr sa4;
memset(&sa4,0,sizeof(httpd_sockaddr)); memset(&sa4, 0, sizeof(httpd_sockaddr));
sa4.sa_in.sin_family = AF_INET; sa4.sa_in.sin_family = AF_INET;
sa4.sa_in.sin_port = htons(80); sa4.sa_in.sin_port = htons(80);
sa4.sa_in.sin_addr.s_addr = htonl(INADDR_ANY); sa4.sa_in.sin_addr.s_addr = htonl(INADDR_ANY);
fHttpdServer->listen4_fd = httpd_initialize_listen_socket(&sa4); fHttpdServer->listen4_fd = httpd_initialize_listen_socket(&sa4);
if(fHttpdServer->listen4_fd == -1) if (fHttpdServer->listen4_fd == -1)
return B_ERROR; return B_ERROR;
fListenerTid = spawn_thread( fListenerTid = spawn_thread(
@@ -101,12 +104,12 @@ status_t PoorManServer::Run()
B_NORMAL_PRIORITY, B_NORMAL_PRIORITY,
static_cast<void*>(this) static_cast<void*>(this)
); );
if(fListenerTid < B_OK){ if (fListenerTid < B_OK) {
poorman_log("can't create listener thread.\n", false, INADDR_NONE, RED); poorman_log("can't create listener thread.\n", false, INADDR_NONE, RED);
return B_ERROR; return B_ERROR;
} }
fIsRunning = true; fIsRunning = true;
if(resume_thread(fListenerTid) != B_OK){ if (resume_thread(fListenerTid) != B_OK) {
fIsRunning = false; fIsRunning = false;
return B_ERROR; return B_ERROR;
} }
@@ -115,33 +118,35 @@ status_t PoorManServer::Run()
return B_OK; return B_OK;
} }
status_t PoorManServer::Stop() status_t PoorManServer::Stop()
{ {
if(fIsRunning){ if (fIsRunning) {
fIsRunning = false; fIsRunning = false;
httpd_unlisten(fHttpdServer); httpd_unlisten(fHttpdServer);
} }
return B_OK; return B_OK;
} }
/*The Web Dir is not changed if an error occured. /*The Web Dir is not changed if an error occured.
*/ */
status_t PoorManServer::SetWebDir(const char* webDir) status_t PoorManServer::SetWebDir(const char* webDir)
{ {
if(chdir(webDir) == -1){ if (chdir(webDir) == -1) {
//log it //log it
return B_ERROR; return B_ERROR;
} }
char* tmp = strdup(webDir); char* tmp = strdup(webDir);
if(tmp == NULL) if (tmp == NULL)
return B_ERROR; return B_ERROR;
if(pthread_rwlock_wrlock(&fWebDirLock) == 0){ if (pthread_rwlock_wrlock(&fWebDirLock) == 0) {
free(fHttpdServer->cwd); free(fHttpdServer->cwd);
fHttpdServer->cwd = tmp; fHttpdServer->cwd = tmp;
if(tmp[strlen(tmp)-1] == '/'){ if (tmp[strlen(tmp) - 1] == '/') {
tmp[strlen(tmp)-1] = '\0'; tmp[strlen(tmp) - 1] = '\0';
} }
pthread_rwlock_unlock(&fWebDirLock); pthread_rwlock_unlock(&fWebDirLock);
} else { } else {
@@ -152,30 +157,33 @@ status_t PoorManServer::SetWebDir(const char* webDir)
return B_OK; return B_OK;
} }
status_t PoorManServer::SetMaxConns(int32 count) status_t PoorManServer::SetMaxConns(int32 count)
{ {
fMaxConns = count; fMaxConns = count;
return B_OK; return B_OK;
} }
status_t PoorManServer::SetListDir(bool listDir) status_t PoorManServer::SetListDir(bool listDir)
{ {
fHttpdServer->do_list_dir = (listDir?1:0); fHttpdServer->do_list_dir = (listDir?1:0);
return B_OK; return B_OK;
} }
status_t PoorManServer::SetIndexName(const char* idxName) status_t PoorManServer::SetIndexName(const char* idxName)
{ {
size_t length = strlen(idxName); size_t length = strlen(idxName);
if(length > B_PATH_NAME_LENGTH+1) if (length > B_PATH_NAME_LENGTH + 1)
return B_ERROR; return B_ERROR;
char* tmp = new char[length+1]; char* tmp = new char[length + 1];
if(tmp == NULL) if (tmp == NULL)
return B_ERROR; return B_ERROR;
strcpy(tmp, idxName); strcpy(tmp, idxName);
if(pthread_rwlock_wrlock(&fIndexNameLock) == 0){ if (pthread_rwlock_wrlock(&fIndexNameLock) == 0) {
delete[] fIndexName; delete[] fIndexName;
fIndexName = tmp; fIndexName = tmp;
fHttpdServer->index_name = fIndexName; fHttpdServer->index_name = fIndexName;
@@ -188,6 +196,7 @@ status_t PoorManServer::SetIndexName(const char* idxName)
return B_OK; return B_OK;
} }
int32 PoorManServer::_Listener(void* data) int32 PoorManServer::_Listener(void* data)
{ {
PRINT(("The listener thread is working.\n")); PRINT(("The listener thread is working.\n"));
@@ -196,30 +205,32 @@ int32 PoorManServer::_Listener(void* data)
httpd_conn* hc; httpd_conn* hc;
PoorManServer* s = static_cast<PoorManServer*>(data); PoorManServer* s = static_cast<PoorManServer*>(data);
while(s->fIsRunning){ while (s->fIsRunning) {
hc = new httpd_conn; hc = new httpd_conn;
hc->initialized = 0; hc->initialized = 0;
PRINT(("calling httpd_get_conn()\n")); PRINT(("calling httpd_get_conn()\n"));
retval = //accept(), blocked here retval = //accept(), blocked here
httpd_get_conn(s->fHttpdServer, s->fHttpdServer->listen4_fd, hc); httpd_get_conn(s->fHttpdServer, s->fHttpdServer->listen4_fd, hc);
switch (retval){ switch (retval) {
case GC_OK: case GC_OK:
break; break;
case GC_FAIL: case GC_FAIL:
httpd_destroy_conn(hc); httpd_destroy_conn(hc);
delete hc; delete hc;
s->fIsRunning = false; s->fIsRunning = false;
return -1; return -1;
case GC_NO_MORE: //should not happen, since we have a blocking socket case GC_NO_MORE:
httpd_destroy_conn(hc); //should not happen, since we have a blocking socket
continue; httpd_destroy_conn(hc);
break; continue;
default: //shouldn't happen break;
continue; default:
break; //shouldn't happen
continue;
break;
} }
if(s->fCurConns > s->fMaxConns){ if (s->fCurConns > s->fMaxConns) {
httpd_send_err(hc, 503, httpd_send_err(hc, 503,
httpd_err503title, "", httpd_err503form, ""); httpd_err503title, "", httpd_err503form, "");
httpd_write_response(hc); httpd_write_response(hc);
@@ -232,7 +243,7 @@ int32 PoorManServer::_Listener(void* data)
B_NORMAL_PRIORITY, B_NORMAL_PRIORITY,
static_cast<void*>(s) static_cast<void*>(s)
); );
if(tid < B_OK){ if (tid < B_OK) {
continue; continue;
} }
/*We don't check the return code here. /*We don't check the return code here.
@@ -246,6 +257,7 @@ int32 PoorManServer::_Listener(void* data)
return 0; return 0;
} }
int32 PoorManServer::_Worker(void* data) int32 PoorManServer::_Worker(void* data)
{ {
static const struct timeval kTimeVal = {60, 0}; static const struct timeval kTimeVal = {60, 0};
@@ -253,9 +265,9 @@ int32 PoorManServer::_Worker(void* data)
httpd_conn* hc; httpd_conn* hc;
int retval; int retval;
if(has_data(find_thread(NULL))){ if (has_data(find_thread(NULL))) {
thread_id sender; thread_id sender;
if(receive_data(&sender, &hc, sizeof(httpd_conn*)) != 512) if (receive_data(&sender, &hc, sizeof(httpd_conn*)) != 512)
goto cleanup; goto cleanup;
} else { } else {
goto cleanup; goto cleanup;
@@ -270,29 +282,29 @@ int32 PoorManServer::_Worker(void* data)
hc->read_size - hc->read_idx, hc->read_size - hc->read_idx,
0 0
); );
if(retval < 0) if (retval < 0)
goto cleanup; goto cleanup;
hc->read_idx += retval; hc->read_idx += retval;
switch(httpd_got_request(hc)){ switch(httpd_got_request(hc)) {
case GR_GOT_REQUEST: case GR_GOT_REQUEST:
break; break;
case GR_BAD_REQUEST: case GR_BAD_REQUEST:
httpd_send_err(hc,400,httpd_err400title,"",httpd_err400form,""); httpd_send_err(hc,400,httpd_err400title,"",httpd_err400form,"");
httpd_write_response(hc);//fall through httpd_write_response(hc);//fall through
case GR_NO_REQUEST: //fall through case GR_NO_REQUEST: //fall through
default: //won't happen default: //won't happen
goto cleanup; goto cleanup;
break; break;
} }
if(httpd_parse_request(hc) < 0){ if (httpd_parse_request(hc) < 0) {
httpd_write_response(hc); httpd_write_response(hc);
goto cleanup; goto cleanup;
} }
retval = httpd_start_request(hc,(struct timeval*)0); retval = httpd_start_request(hc,(struct timeval*)0);
if(retval < 0){ if (retval < 0) {
httpd_write_response(hc); httpd_write_response(hc);
goto cleanup; goto cleanup;
} }
@@ -300,7 +312,7 @@ int32 PoorManServer::_Worker(void* data)
/*true means the connection is already handled /*true means the connection is already handled
*by the directory index generator in httpd_start_request(). *by the directory index generator in httpd_start_request().
*/ */
if(hc->file_address == (char*) 0){ if (hc->file_address == (char*) 0) {
static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->SetHits( static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->SetHits(
static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->GetHits()+1 static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->GetHits()+1
); );
@@ -308,18 +320,16 @@ int32 PoorManServer::_Worker(void* data)
goto cleanup; goto cleanup;
} }
switch(hc->method){ switch (hc->method) {
case METHOD_GET: case METHOD_GET:
s->_HandleGet(hc); s->_HandleGet(hc);
break; break;
case METHOD_HEAD: case METHOD_HEAD:
s->_HandleHead(hc); s->_HandleHead(hc);
break; break;
case METHOD_POST: case METHOD_POST:
s->_HandlePost(hc); s->_HandlePost(hc);
break; break;
default:
break;
} }
cleanup: ; cleanup: ;
@@ -331,6 +341,7 @@ cleanup: ;
return 0; return 0;
} }
status_t PoorManServer::_HandleGet(httpd_conn* hc) status_t PoorManServer::_HandleGet(httpd_conn* hc)
{ {
PRINT(("HandleGet() called\n")); PRINT(("HandleGet() called\n"));
@@ -341,24 +352,24 @@ status_t PoorManServer::_HandleGet(httpd_conn* hc)
BString log; BString log;
BFile file(hc->expnfilename, B_READ_ONLY); BFile file(hc->expnfilename, B_READ_ONLY);
if(file.InitCheck() != B_OK) if (file.InitCheck() != B_OK)
return B_ERROR; return B_ERROR;
buf = new uint8[POOR_MAN_BUF_SIZE]; buf = new uint8[POOR_MAN_BUF_SIZE];
if(buf == NULL) if (buf == NULL)
return B_ERROR; return B_ERROR;
if(hc->got_range == 1) if (hc->got_range == 1)
length = hc->last_byte_index + 1 - hc->first_byte_index; length = hc->last_byte_index + 1 - hc->first_byte_index;
else else
length = hc->sb.st_size; length = hc->sb.st_size;
static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->SetHits( static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->SetHits(
static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->GetHits()+1 static_cast<PoorManApplication*>(be_app)->GetPoorManWindow()->GetHits()
); + 1);
log.SetTo("Sending file: "); log.SetTo("Sending file: ");
if(pthread_rwlock_rdlock(&fWebDirLock) == 0){ if (pthread_rwlock_rdlock(&fWebDirLock) == 0) {
log << hc->hs->cwd; log << hc->hs->cwd;
pthread_rwlock_unlock(&fWebDirLock); pthread_rwlock_unlock(&fWebDirLock);
} }
@@ -366,23 +377,23 @@ status_t PoorManServer::_HandleGet(httpd_conn* hc)
poorman_log(log.String(), true, hc->client_addr.sa_in.sin_addr.s_addr); poorman_log(log.String(), true, hc->client_addr.sa_in.sin_addr.s_addr);
//send mime headers //send mime headers
if(send(hc->conn_fd,hc->response,hc->responselen,0) < 0){ if (send(hc->conn_fd,hc->response,hc->responselen,0) < 0) {
delete [] buf; delete [] buf;
return B_ERROR; return B_ERROR;
} }
file.Seek(hc->first_byte_index, SEEK_SET); file.Seek(hc->first_byte_index, SEEK_SET);
while(true){ while (true) {
bytesRead = file.Read(buf, POOR_MAN_BUF_SIZE); bytesRead = file.Read(buf, POOR_MAN_BUF_SIZE);
if(bytesRead == 0) if (bytesRead == 0)
break; break;
else if(bytesRead < 0){ else if (bytesRead < 0) {
delete [] buf; delete [] buf;
return B_ERROR; return B_ERROR;
} }
if(send(hc->conn_fd, (void*)buf, bytesRead, 0) < 0){ if (send(hc->conn_fd, (void*)buf, bytesRead, 0) < 0) {
log.SetTo("Error sending file: "); log.SetTo("Error sending file: ");
if(pthread_rwlock_rdlock(&fWebDirLock) == 0){ if (pthread_rwlock_rdlock(&fWebDirLock) == 0) {
log << hc->hs->cwd; log << hc->hs->cwd;
pthread_rwlock_unlock(&fWebDirLock); pthread_rwlock_unlock(&fWebDirLock);
} }
@@ -397,26 +408,30 @@ status_t PoorManServer::_HandleGet(httpd_conn* hc)
return B_OK; return B_OK;
} }
status_t PoorManServer::_HandleHead(httpd_conn* hc) status_t PoorManServer::_HandleHead(httpd_conn* hc)
{ {
int retval = send(hc->conn_fd,hc->response,hc->responselen,0); int retval = send(hc->conn_fd,hc->response,hc->responselen,0);
if(retval == -1) if (retval == -1)
return B_ERROR; return B_ERROR;
return B_OK; return B_OK;
} }
status_t PoorManServer::_HandlePost(httpd_conn* hc) status_t PoorManServer::_HandlePost(httpd_conn* hc)
{ {
//not implemented //not implemented
return B_OK; return B_OK;
} }
pthread_rwlock_t* get_web_dir_lock() pthread_rwlock_t* get_web_dir_lock()
{ {
return static_cast<PoorManApplication*>(be_app)-> return static_cast<PoorManApplication*>(be_app)->
GetPoorManWindow()->GetServer()->GetWebDirLock(); GetPoorManWindow()->GetServer()->GetWebDirLock();
} }
pthread_rwlock_t* get_index_name_lock() pthread_rwlock_t* get_index_name_lock()
{ {
return static_cast<PoorManApplication*>(be_app)-> return static_cast<PoorManApplication*>(be_app)->
+9 -4
View File
@@ -61,7 +61,8 @@ PoorManSiteView::PoorManSiteView(BRect rect, const char *name)
webSiteLocationRect.bottom = webSiteLocationRect.top + 19.0; webSiteLocationRect.bottom = webSiteLocationRect.top + 19.0;
// Web Directory Text Control // Web Directory Text Control
webDir = new BTextControl(webSiteLocationRect, "Web Dir", STR_TXT_DIRECTORY, NULL, NULL); webDir = new BTextControl(webSiteLocationRect, "Web Dir",
STR_TXT_DIRECTORY, NULL, NULL);
webDir->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT); webDir->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
webDir->SetDivider(80.0); webDir->SetDivider(80.0);
SetWebDir(win->WebDir()); SetWebDir(win->WebDir());
@@ -69,12 +70,15 @@ PoorManSiteView::PoorManSiteView(BRect rect, const char *name)
// Select Web Directory Button // Select Web Directory Button
BRect selectWebDirRect; BRect selectWebDirRect;
selectWebDirRect.top = webSiteLocationRect.bottom + 5.0; selectWebDirRect.top = webSiteLocationRect.bottom + 5.0;
selectWebDirRect.right = webSiteLocationRect.right + 2.0; selectWebDirRect.right = webSiteLocationRect.right + 2.0;
selectWebDirRect.left = selectWebDirRect.right - 123.0; selectWebDirRect.left = selectWebDirRect.right
- webSiteLocation->StringWidth("Select Web Dir") - 24.0;
selectWebDirRect.bottom = selectWebDirRect.top + 19.0; selectWebDirRect.bottom = selectWebDirRect.top + 19.0;
selectWebDir = new BButton(selectWebDirRect, "Select Web Dir", STR_BTN_DIRECTORY, new BMessage(MSG_PREF_SITE_BTN_SELECT)); selectWebDir = new BButton(selectWebDirRect, "Select Web Dir",
STR_BTN_DIRECTORY, new BMessage(MSG_PREF_SITE_BTN_SELECT));
webSiteLocation->AddChild(selectWebDir); webSiteLocation->AddChild(selectWebDir);
// Index File Name Text Control // Index File Name Text Control
@@ -82,7 +86,8 @@ PoorManSiteView::PoorManSiteView(BRect rect, const char *name)
webSiteLocationRect.top += 63.0; webSiteLocationRect.top += 63.0;
webSiteLocationRect.bottom = webSiteLocationRect.top + 19.0; webSiteLocationRect.bottom = webSiteLocationRect.top + 19.0;
indexFileName = new BTextControl(webSiteLocationRect, "Index File Name", STR_TXT_INDEX, NULL, NULL); indexFileName = new BTextControl(webSiteLocationRect,
"Index File Name", STR_TXT_INDEX, NULL, NULL);
indexFileName->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT); indexFileName->SetAlignment(B_ALIGN_RIGHT, B_ALIGN_LEFT);
indexFileName->SetDivider(80.0); indexFileName->SetDivider(80.0);
SetIndexFileName(win->IndexFileName()); SetIndexFileName(win->IndexFileName());
+120 -96
View File
@@ -33,6 +33,7 @@
#include "PoorManLogger.h" #include "PoorManLogger.h"
#include "constants.h" #include "constants.h"
PoorManWindow::PoorManWindow(BRect frame) PoorManWindow::PoorManWindow(BRect frame)
: BWindow(frame, STR_APP_NAME, B_TITLED_WINDOW, 0), : BWindow(frame, STR_APP_NAME, B_TITLED_WINDOW, 0),
status(false), hits(0), prefWindow(NULL), fLogFile(NULL), fServer(NULL) status(false), hits(0), prefWindow(NULL), fLogFile(NULL), fServer(NULL)
@@ -188,8 +189,8 @@ PoorManWindow::PoorManWindow(BRect frame)
NULL, NULL,
B_FILE_NODE, B_FILE_NODE,
false, false,
new BMessage(MSG_FILE_PANEL_SAVE_CONSOLE) new BMessage(MSG_FILE_PANEL_SAVE_CONSOLE));
);
change_title = saveConsoleFilePanel->Window(); change_title = saveConsoleFilePanel->Window();
change_title->SetTitle(STR_FILEPANEL_SAVE_CONSOLE); change_title->SetTitle(STR_FILEPANEL_SAVE_CONSOLE);
@@ -199,8 +200,8 @@ PoorManWindow::PoorManWindow(BRect frame)
NULL, NULL,
B_FILE_NODE, B_FILE_NODE,
false, false,
new BMessage(MSG_FILE_PANEL_SAVE_CONSOLE_SELECTION) new BMessage(MSG_FILE_PANEL_SAVE_CONSOLE_SELECTION));
);
change_title = saveConsoleSelectionFilePanel->Window(); change_title = saveConsoleSelectionFilePanel->Window();
change_title->SetTitle(STR_FILEPANEL_SAVE_CONSOLE_SELECTION); change_title->SetTitle(STR_FILEPANEL_SAVE_CONSOLE_SELECTION);
@@ -210,6 +211,7 @@ PoorManWindow::PoorManWindow(BRect frame)
pthread_rwlock_init(&fLogFileLock, NULL); pthread_rwlock_init(&fLogFileLock, NULL);
} }
PoorManWindow::~PoorManWindow() PoorManWindow::~PoorManWindow()
{ {
delete fServer; delete fServer;
@@ -218,6 +220,7 @@ PoorManWindow::~PoorManWindow()
pthread_rwlock_destroy(&fLogFileLock); pthread_rwlock_destroy(&fLogFileLock);
} }
void void
PoorManWindow::MessageReceived(BMessage* message) PoorManWindow::MessageReceived(BMessage* message)
{ {
@@ -236,14 +239,17 @@ PoorManWindow::MessageReceived(BMessage* message)
printf("FilePanel: Save Console Selection\n"); printf("FilePanel: Save Console Selection\n");
SaveConsole(message, true); SaveConsole(message, true);
break; break;
case MSG_FILE_PANEL_SELECT_WEB_DIR:
prefWindow->MessageReceived(message);
break;
case MSG_MENU_EDIT_PREF: case MSG_MENU_EDIT_PREF:
prefWindow = new PoorManPreferencesWindow( prefWindow = new PoorManPreferencesWindow(
setwindow_frame, setwindow_frame,
STR_WIN_NAME_PREF STR_WIN_NAME_PREF);
); prefWindow->Show();
break; break;
case MSG_MENU_CTRL_RUN: case MSG_MENU_CTRL_RUN:
if(status) if (status)
StopServer(); StopServer();
else else
StartServer(); StartServer();
@@ -261,10 +267,9 @@ PoorManWindow::MessageReceived(BMessage* message)
f = fopen(log_path.String(), "w"); f = fopen(log_path.String(), "w");
fclose(f); fclose(f);
break; break;
case MSG_LOG: case MSG_LOG: {
{ if (!log_console_flag && !log_file_flag)
if(!log_console_flag && !log_file_flag) break;
break;
time_t time; time_t time;
in_addr_t address; in_addr_t address;
@@ -274,34 +279,36 @@ PoorManWindow::MessageReceived(BMessage* message)
const char* msg; const char* msg;
BString line; BString line;
if(message->FindString("cstring", &msg) != B_OK) if (message->FindString("cstring", &msg) != B_OK)
break; break;
if(message->FindData("time_t", B_TIME_TYPE, &pointer, &size) != B_OK) if (message->FindData("time_t", B_TIME_TYPE, &pointer, &size) != B_OK)
time = -1; time = -1;
else else
time = *static_cast<const time_t*>(pointer); time = *static_cast<const time_t*>(pointer);
if(message->FindData("in_addr_t", B_ANY_TYPE, &pointer, &size) != B_OK)
if (message->FindData("in_addr_t", B_ANY_TYPE, &pointer, &size) != B_OK)
address = INADDR_NONE; address = INADDR_NONE;
else else
address = *static_cast<const in_addr_t*>(pointer); address = *static_cast<const in_addr_t*>(pointer);
if(message->FindData("rgb_color", B_RGB_COLOR_TYPE, &pointer, &size) != B_OK)
if (message->FindData("rgb_color", B_RGB_COLOR_TYPE, &pointer, &size) != B_OK)
color = BLACK; color = BLACK;
else else
color = *static_cast<const rgb_color*>(pointer); color = *static_cast<const rgb_color*>(pointer);
if(time != -1){ if (time != -1) {
char timeString[26]; char timeString[26];
if(ctime_r(&time, timeString) != NULL){ if (ctime_r(&time, timeString) != NULL) {
timeString[24] = '\0'; timeString[24] = '\0';
line << '[' << timeString << "]: "; line << '[' << timeString << "]: ";
} }
} }
if(address != INADDR_NONE){ if (address != INADDR_NONE) {
char addr[INET_ADDRSTRLEN]; char addr[INET_ADDRSTRLEN];
struct in_addr sin_addr; struct in_addr sin_addr;
sin_addr.s_addr = address; sin_addr.s_addr = address;
if(inet_ntop(AF_INET, &sin_addr, addr, sizeof(addr)) != NULL){ if (inet_ntop(AF_INET, &sin_addr, addr, sizeof(addr)) != NULL) {
addr[strlen(addr)] = '\0'; addr[strlen(addr)] = '\0';
line << '(' << addr << ") "; line << '(' << addr << ") ";
} }
@@ -319,15 +326,15 @@ PoorManWindow::MessageReceived(BMessage* message)
runs.count = 1; runs.count = 1;
runs.runs[0] = run; runs.runs[0] = run;
if(Lock()){ if (Lock()) {
if(log_console_flag){ if (log_console_flag) {
loggingView->Insert(loggingView->TextLength(), loggingView->Insert(loggingView->TextLength(),
line.String(), line.Length(), &runs); line.String(), line.Length(), &runs);
loggingView->ScrollToOffset(loggingView->TextLength()); loggingView->ScrollToOffset(loggingView->TextLength());
} }
if(log_file_flag){ if (log_file_flag) {
if(pthread_rwlock_rdlock(&fLogFileLock) == 0){ if (pthread_rwlock_rdlock(&fLogFileLock) == 0) {
fLogFile->Write(line.String(), line.Length()); fLogFile->Write(line.String(), line.Length());
pthread_rwlock_unlock(&fLogFileLock); pthread_rwlock_unlock(&fLogFileLock);
} }
@@ -344,6 +351,7 @@ PoorManWindow::MessageReceived(BMessage* message)
} }
} }
void void
PoorManWindow::FrameMoved(BPoint origin) PoorManWindow::FrameMoved(BPoint origin)
{ {
@@ -351,35 +359,36 @@ PoorManWindow::FrameMoved(BPoint origin)
frame.top = origin.y; frame.top = origin.y;
} }
void void
PoorManWindow::FrameResized(float width, float height) PoorManWindow::FrameResized(float width, float height)
{ {
if (is_zoomed) if (is_zoomed) {
{
last_width = width; last_width = width;
last_height = height; last_height = height;
} }
} }
bool bool
PoorManWindow::QuitRequested() PoorManWindow::QuitRequested()
{ {
if(status){ if (status) {
time_t now = time(NULL); time_t now = time(NULL);
char line[] = "[Thu Jan 1 00:00:00 1970]: Shutting down.\n"; char line[] = "[Thu Jan 1 00:00:00 1970]: Shutting down.\n";
ctime_r(&now, line+1); ctime_r(&now, line + 1);
line[25] = ']'; line[25] = ']';
line[26] = ' '; line[26] = ' ';
if(log_console_flag){ if (log_console_flag) {
loggingView->Insert(loggingView->TextLength(), loggingView->Insert(loggingView->TextLength(),
line, strlen(line)); line, strlen(line));
loggingView->ScrollToOffset(loggingView->TextLength()); loggingView->ScrollToOffset(loggingView->TextLength());
} }
if(log_file_flag){ if (log_file_flag) {
if(pthread_rwlock_rdlock(&fLogFileLock) == 0){ if (pthread_rwlock_rdlock(&fLogFileLock) == 0) {
fLogFile->Write(line, strlen(line)); fLogFile->Write(line, strlen(line));
pthread_rwlock_unlock(&fLogFileLock); pthread_rwlock_unlock(&fLogFileLock);
} }
@@ -395,21 +404,22 @@ PoorManWindow::QuitRequested()
return true; return true;
} }
void void
PoorManWindow::Zoom(BPoint origin, float width, float height) PoorManWindow::Zoom(BPoint origin, float width, float height)
{ {
if (is_zoomed) if (is_zoomed) {
{ // Change to the Minimal size // Change to the Minimal size
is_zoomed = false; is_zoomed = false;
ResizeTo(318, 53); ResizeTo(318, 53);
} } else {
else // Change to the Zoomed size
{ // Change to the Zoomed size
is_zoomed = true; is_zoomed = true;
ResizeTo(last_width, last_height); ResizeTo(last_width, last_height);
} }
} }
void void
PoorManWindow::SetHits(uint32 num) PoorManWindow::SetHits(uint32 num)
{ {
@@ -417,8 +427,10 @@ PoorManWindow::SetHits(uint32 num)
UpdateHitsLabel(); UpdateHitsLabel();
} }
// Private: Methods ------------------------------------------ // Private: Methods ------------------------------------------
BMenu * BMenu *
PoorManWindow::BuildFileMenu() const PoorManWindow::BuildFileMenu() const
{ {
@@ -447,6 +459,7 @@ PoorManWindow::BuildFileMenu() const
return ptrFileMenu; return ptrFileMenu;
} }
BMenu * BMenu *
PoorManWindow::BuildEditMenu() const PoorManWindow::BuildEditMenu() const
{ {
@@ -475,6 +488,7 @@ PoorManWindow::BuildEditMenu() const
return ptrEditMenu; return ptrEditMenu;
} }
BMenu * BMenu *
PoorManWindow::BuildControlsMenu() const PoorManWindow::BuildControlsMenu() const
{ {
@@ -502,24 +516,24 @@ PoorManWindow::BuildControlsMenu() const
return ptrControlMenu; return ptrControlMenu;
} }
void void
PoorManWindow::SetDirLabel(const char * name) PoorManWindow::SetDirLabel(const char * name)
{ {
BString dirPath("Directory: "); BString dirPath("Directory: ");
dirPath.Append(name); dirPath.Append(name);
if (Lock()) if (Lock()) {
{
dirView->SetText(dirPath.String()); dirView->SetText(dirPath.String());
Unlock(); Unlock();
} }
} }
void void
PoorManWindow::UpdateStatusLabelAndMenuItem() PoorManWindow::UpdateStatusLabelAndMenuItem()
{ {
if (Lock()) if (Lock()) {
{
if (status) if (status)
statusView->SetText("Status: Running"); statusView->SetText("Status: Running");
else else
@@ -529,11 +543,11 @@ PoorManWindow::UpdateStatusLabelAndMenuItem()
} }
} }
void void
PoorManWindow::UpdateHitsLabel() PoorManWindow::UpdateHitsLabel()
{ {
if (Lock()) if (Lock()) {
{
sprintf(hitsLabel, "Hits: %lu", GetHits()); sprintf(hitsLabel, "Hits: %lu", GetHits());
hitsView->SetText(hitsLabel); hitsView->SetText(hitsLabel);
@@ -541,6 +555,7 @@ PoorManWindow::UpdateHitsLabel()
} }
} }
status_t status_t
PoorManWindow::SaveConsole(BMessage * message, bool selection) PoorManWindow::SaveConsole(BMessage * message, bool selection)
{ {
@@ -566,8 +581,7 @@ PoorManWindow::SaveConsole(BMessage * message, bool selection)
if (!(f = fopen(path.Path(), "w"))) if (!(f = fopen(path.Path(), "w")))
return B_ERROR; return B_ERROR;
if (!selection) if (!selection) {
{
// write the data to the file // write the data to the file
err = fwrite(loggingView->Text(), 1, loggingView->TextLength(), f); err = fwrite(loggingView->Text(), 1, loggingView->TextLength(), f);
} else { } else {
@@ -598,28 +612,34 @@ PoorManWindow::DefaultSettings()
dirAlert->SetShortcut(0, B_ESCAPE); dirAlert->SetShortcut(0, B_ESCAPE);
int32 buttonIndex = dirAlert->Go(); int32 buttonIndex = dirAlert->Go();
switch (buttonIndex){ switch (buttonIndex) {
case 0: case 0:
SetWebDir(""); if (Lock())
break; Quit();
case 1: be_app_messenger.SendMessage(B_QUIT_REQUESTED);
prefWindow = new PoorManPreferencesWindow( break;
setwindow_frame,
STR_WIN_NAME_PREF case 1:
); prefWindow = new PoorManPreferencesWindow(
prefWindow->ShowWebDirFilePanel(); setwindow_frame,
break; STR_WIN_NAME_PREF);
case 2: prefWindow->ShowWebDirFilePanel();
if(create_directory(STR_DEFAULT_WEB_DIRECTORY, 0755) != B_OK){ break;
serverAlert->Go();
SetWebDir(""); case 2:
if (create_directory(STR_DEFAULT_WEB_DIRECTORY, 0755) != B_OK) {
serverAlert->Go();
if (Lock())
Quit();
be_app_messenger.SendMessage(B_QUIT_REQUESTED);
break;
}
BAlert* dirCreatedAlert =
new BAlert("Dir Created", STR_DIR_CREATED, "OK");
dirCreatedAlert->Go();
SetWebDir(STR_DEFAULT_WEB_DIRECTORY);
be_app->PostMessage(kStartServer);
break; break;
}
BAlert* dirCreatedAlert =
new BAlert("Dir Created",STR_DIR_CREATED,"OK");
dirCreatedAlert->Go();
SetWebDir(STR_DEFAULT_WEB_DIRECTORY);
break;
} }
} }
@@ -631,57 +651,58 @@ PoorManWindow::ReadSettings()
BFile f; BFile f;
BMessage m; BMessage m;
if(find_directory(B_USER_SETTINGS_DIRECTORY, &p) != B_OK) if (find_directory(B_USER_SETTINGS_DIRECTORY, &p) != B_OK)
return B_ERROR; return B_ERROR;
p.Append(STR_SETTINGS_FILE_NAME); p.Append(STR_SETTINGS_FILE_NAME);
f.SetTo(p.Path(), B_READ_ONLY); f.SetTo(p.Path(), B_READ_ONLY);
if(f.InitCheck()!=B_OK) if (f.InitCheck() != B_OK)
return B_ERROR; return B_ERROR;
if(m.Unflatten(&f)!=B_OK) if (m.Unflatten(&f) != B_OK)
return B_ERROR; return B_ERROR;
if(MSG_PREF_FILE != m.what) if (MSG_PREF_FILE != m.what)
return B_ERROR; return B_ERROR;
//site tab //site tab
if(m.FindString("web_directory", &web_directory) != B_OK) if (m.FindString("web_directory", &web_directory) != B_OK)
web_directory.SetTo(STR_DEFAULT_WEB_DIRECTORY); web_directory.SetTo(STR_DEFAULT_WEB_DIRECTORY);
if(m.FindString("index_file_name", &index_file_name) != B_OK) if (m.FindString("index_file_name", &index_file_name) != B_OK)
index_file_name.SetTo("index.html"); index_file_name.SetTo("index.html");
if(m.FindBool("dir_list_flag", &dir_list_flag) != B_OK) if (m.FindBool("dir_list_flag", &dir_list_flag) != B_OK)
dir_list_flag = false; dir_list_flag = false;
//logging tab //logging tab
if(m.FindBool("log_console_flag", &log_console_flag) != B_OK) if (m.FindBool("log_console_flag", &log_console_flag) != B_OK)
log_console_flag = true; log_console_flag = true;
if(m.FindBool("log_file_flag", &log_file_flag) != B_OK) if (m.FindBool("log_file_flag", &log_file_flag) != B_OK)
log_file_flag = false; log_file_flag = false;
if(m.FindString("log_path", &log_path) != B_OK) if (m.FindString("log_path", &log_path) != B_OK)
log_path.SetTo(""); log_path.SetTo("");
//advance tab //advance tab
if(m.FindInt16("max_connections", &max_connections) != B_OK) if (m.FindInt16("max_connections", &max_connections) != B_OK)
max_connections = (int16)32; max_connections = (int16)32;
//windows' position and size //windows' position and size
if(m.FindRect("frame", &frame) != B_OK) if (m.FindRect("frame", &frame) != B_OK)
frame.Set(82.0f, 30.0f, 400.0f, 350.0f); frame.Set(82.0f, 30.0f, 400.0f, 350.0f);
if(m.FindRect("setwindow_frame", &setwindow_frame) != B_OK) if (m.FindRect("setwindow_frame", &setwindow_frame) != B_OK)
setwindow_frame.Set(112.0f, 60.0f, 492.0f, 340.0f); setwindow_frame.Set(112.0f, 60.0f, 492.0f, 340.0f);
if(m.FindBool("is_zoomed", &is_zoomed) != B_OK) if (m.FindBool("is_zoomed", &is_zoomed) != B_OK)
is_zoomed = true; is_zoomed = true;
if(m.FindFloat("last_width", &last_width) != B_OK) if (m.FindFloat("last_width", &last_width) != B_OK)
last_width = 318.0f; last_width = 318.0f;
if(m.FindFloat("last_height", &last_height) != B_OK) if (m.FindFloat("last_height", &last_height) != B_OK)
last_height = 320.0f; last_height = 320.0f;
is_zoomed?ResizeTo(last_width, last_height):ResizeTo(318, 53); is_zoomed?ResizeTo(last_width, last_height):ResizeTo(318, 53);
MoveTo(frame.left, frame.top); MoveTo(frame.left, frame.top);
fLogFile = new BFile(log_path.String(), B_CREATE_FILE|B_WRITE_ONLY|B_OPEN_AT_END); fLogFile = new BFile(log_path.String(), B_CREATE_FILE | B_WRITE_ONLY
if(fLogFile->InitCheck() != B_OK){ | B_OPEN_AT_END);
if (fLogFile->InitCheck() != B_OK) {
log_file_flag = false; log_file_flag = false;
//log it to console, "log to file unavailable." //log it to console, "log to file unavailable."
return B_OK; return B_OK;
@@ -692,6 +713,7 @@ PoorManWindow::ReadSettings()
return B_OK; return B_OK;
} }
status_t status_t
PoorManWindow::SaveSettings() PoorManWindow::SaveSettings()
{ {
@@ -719,36 +741,36 @@ PoorManWindow::SaveSettings()
m.AddFloat("last_width", last_width); m.AddFloat("last_width", last_width);
m.AddFloat("last_height", last_height); m.AddFloat("last_height", last_height);
if(find_directory(B_USER_SETTINGS_DIRECTORY, &p) != B_OK) if (find_directory(B_USER_SETTINGS_DIRECTORY, &p) != B_OK)
return B_ERROR; return B_ERROR;
p.Append(STR_SETTINGS_FILE_NAME); p.Append(STR_SETTINGS_FILE_NAME);
f.SetTo(p.Path(), B_WRITE_ONLY| B_ERASE_FILE| B_CREATE_FILE); f.SetTo(p.Path(), B_WRITE_ONLY | B_ERASE_FILE | B_CREATE_FILE);
if(f.InitCheck()!=B_OK) if (f.InitCheck() != B_OK)
return B_ERROR; return B_ERROR;
if(m.Flatten(&f)!=B_OK) if (m.Flatten(&f) != B_OK)
return B_ERROR; return B_ERROR;
return B_OK; return B_OK;
} }
status_t status_t
PoorManWindow::StartServer() PoorManWindow::StartServer()
{ {
if(NULL == fServer) if (fServer == NULL)
fServer = new PoorManServer( fServer = new PoorManServer(
web_directory.String(), web_directory.String(),
max_connections, max_connections,
dir_list_flag, dir_list_flag,
index_file_name.String() index_file_name.String());
);
poorman_log("Starting up... "); poorman_log("Starting up... ");
if(fServer->Run() != B_OK){ if (fServer->Run() != B_OK) {
return B_ERROR; return B_ERROR;
} }
status = true; status = true;
UpdateStatusLabelAndMenuItem(); UpdateStatusLabelAndMenuItem();
poorman_log("done.\n", false, INADDR_NONE, GREEN); poorman_log("done.\n", false, INADDR_NONE, GREEN);
@@ -756,10 +778,11 @@ PoorManWindow::StartServer()
return B_OK; return B_OK;
} }
status_t status_t
PoorManWindow::StopServer() PoorManWindow::StopServer()
{ {
if(NULL == fServer) if (fServer == NULL)
return B_ERROR; return B_ERROR;
poorman_log("Shutting down.\n"); poorman_log("Shutting down.\n");
@@ -769,20 +792,21 @@ PoorManWindow::StopServer()
return B_OK; return B_OK;
} }
void void
PoorManWindow::SetLogPath(const char* str) PoorManWindow::SetLogPath(const char* str)
{ {
if(log_path == str) if (!strcmp(log_path, str))
return; return;
BFile* temp = new BFile(str, B_CREATE_FILE|B_WRITE_ONLY|B_OPEN_AT_END); BFile* temp = new BFile(str, B_CREATE_FILE | B_WRITE_ONLY | B_OPEN_AT_END);
if(temp->InitCheck() != B_OK){ if (temp->InitCheck() != B_OK) {
delete temp; delete temp;
return; return;
} }
if(pthread_rwlock_wrlock(&fLogFileLock) == 0){ if (pthread_rwlock_wrlock(&fLogFileLock) == 0) {
delete fLogFile; delete fLogFile;
fLogFile = temp; fLogFile = temp;
pthread_rwlock_unlock(&fLogFileLock); pthread_rwlock_unlock(&fLogFileLock);
+3
View File
@@ -35,6 +35,9 @@ const int32 MSG_PREF_ADV_SLD_MAX_CONNECTION = 'PASM';
// BMessage Preferences File // BMessage Preferences File
const uint32 MSG_PREF_FILE = 'pref'; const uint32 MSG_PREF_FILE = 'pref';
const uint32 kStartServer = 'strt';
//BMessage for Logger //BMessage for Logger
const int32 MSG_LOG = 'logg'; const int32 MSG_LOG = 'logg';
// ------------------------------------------------------ // ------------------------------------------------------