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