예제 #1
0
파일: main.cpp 프로젝트: PyroOS/Pyro
void BrowserWindow::HandleMessage( Message * pcMsg )
{
	BrowserWebView *pcWebView = GetCurrentWebView();

	switch( pcMsg->GetCode() )
	{
		case ID_URL_CHANGED:
		{
			bool bFinal = false;
			pcMsg->FindBool( "final", &bFinal );

			if( bFinal )
			{
				String cURL = "";
				int nSelection = -1;
				if( pcMsg->FindInt32( "selection", &nSelection ) == EOK )
					cURL = m_pcUrlEdit->GetItem( nSelection );
				else
				{
					cURL = m_pcUrlEdit->GetCurrentString();
					m_pcUrlEdit->AppendItem( cURL );
				}

				OpenURL( cURL );
			}
			break;
		}
		case ID_SET_STATUS_BAR_TEXT:
		{
			String cText;
			if( pcMsg->FindString( "text", &cText ) == EOK )
				m_pcStatusBar->SetText( "text", cText );
			break;
		}
		case ID_CLEAR_STATUS_BAR_TEXT:
		{
			m_pcStatusBar->SetText( "text", "" );
			break;
		}
		case ID_CREATE_WINDOW:
		{
			BrowserWindow *pcWindow;
			String cURL;
			Rect cFrame;

			/* Use the supplied dimensions if they've been provided, otherwise
			   clone the current Window */
			if( pcMsg->FindRect( "frame", &cFrame ) != EOK )
				cFrame = GetFrame();

			pcWindow = new BrowserWindow( cFrame );

			if( pcMsg->FindString( "url", &cURL ) == EOK )
				pcWindow->OpenURL( cURL );

			pcWindow->Show();
			pcWindow->MakeFocus();

			/* Tell BrowserApp about the new window */
			Application::GetInstance()->PostMessage( ID_WINDOW_OPENED );

			if( pcMsg->IsSourceWaiting() )
			{
				WebCore::Page *pcPage;
				View *pcTab;

				/* The new window will only have one tab at this stage so it's safe to always get tab #0 */
				pcTab = pcWindow->m_pcTabView->GetTabView( 0 );
				pcPage = static_cast<BrowserWebView*>( pcTab )->GetWebCoreFrame()->page();

				Message cReply( ID_CREATE_WINDOW_REPLY );
				cReply.AddPointer( "page", pcPage );

				pcMsg->SendReply( &cReply );
			}

			break;
		}
		case ID_CREATE_TAB:
		{
			String cURL;
			if( pcMsg->FindString( "url", &cURL ) == EOK )
			{
				uint nTabIndex;

				nTabIndex = CreateTab( cURL );

				if( pcMsg->IsSourceWaiting() )
				{
					WebCore::Page *pcPage;
					View *pcTab;

					pcTab = m_pcTabView->GetTabView( nTabIndex );
					pcPage = static_cast<BrowserWebView*>( pcTab )->GetWebCoreFrame()->page();

					Message cReply( ID_CREATE_WINDOW_REPLY );
					cReply.AddPointer( "page", pcPage );

					pcMsg->SendReply( &cReply );
				}
			}

			break;
		}
		case ID_BUTTON_BACK:
		{
			pcWebView->GoBack();

			UpdateButtonState( false );
			break;
		}
		case ID_BUTTON_FORWARD:
		{
			pcWebView->GoForward();

			UpdateButtonState( false );
			break;
		}
		case ID_BUTTON_RELOAD:
		{
			pcWebView->Reload();
			break;
		}
		case ID_BUTTON_STOP:
		{
			pcWebView->Stop();

			UpdateButtonState( false );
			break;
		}
		case ID_BUTTON_HOME:
		{
			OpenURL( m_pcSettings->GetString( "homepage", "about:blank" ) );
			break;
		}
		case ID_MENU_APP_ABOUT:
		{
			Alert* pcAbout = new Alert( "About Webster",
										"Webster " WEBSTER_VERSION " (Alpha)\nA WebCore based web browser\n\n© Copyright Kristian Van Der Vliet, 2008\nArno Klenke, 2004-2007\nKurt Skauen, 2001\n"
										"\nWebster is released under the Gnu Public License (GPL)\n",
										Alert::ALERT_INFO, 0x00, "Ok", NULL );
			pcAbout->Go( NULL );

			break;
		}
		case ID_MENU_WIN_NEW_TAB:
		{
			CreateTab( "" );
			break;
		}
		case ID_MENU_WIN_CLOSE_TAB:
		{
			DestroyTab( m_pcTabView->GetSelection() );
			break;
		}
		case ID_MENU_EDIT_CUT:
		{
			pcWebView->Cut();
			break;
		}
		case ID_MENU_EDIT_COPY:
		{
			pcWebView->Copy();
			break;
		}
		case ID_MENU_EDIT_PASTE:
		{
			pcWebView->Paste();
			break;
		}
		case ID_MENU_EDIT_DELETE:
		{
			pcWebView->Delete();
			break;
		}
		case ID_MENU_SETTINGS_CONFIGURE:
		{
			Rect cSettingsFrame = m_pcGuiSettings->GetRect( "settings", m_cSettingsFrame );
			SettingsWindow *pcSettingsWindow = new SettingsWindow( cSettingsFrame, "Webster settings", this, m_pcWebSettings, m_pcSettings, m_pcGuiSettings );
			pcSettingsWindow->Show();
			pcSettingsWindow->MakeFocus();

			break;
		}
		case ID_SETTINGS_SAVE:
		{
			Settings *pcSettings;
			if( pcMsg->FindPointer( "settings", (void**)&pcSettings ) == EOK )
			{
				delete m_pcSettings;
				m_pcSettings = pcSettings;

				m_pcSettings->Save();
			}

			WebSettings *pcWebSettings;
			if( pcMsg->FindPointer( "web_settings", (void**)&pcWebSettings ) == EOK )
			{
				delete m_pcWebSettings;
				m_pcWebSettings = pcWebSettings;
			}

			break;
		}
		case ID_SETTINGS_APPLY:
		{
			Settings *pcSettings;
			if( pcMsg->FindPointer( "settings", (void**)&pcSettings ) == EOK )
			{
				delete m_pcSettings;
				m_pcSettings = pcSettings;
			}

			WebSettings *pcWebSettings;
			if( pcMsg->FindPointer( "web_settings", (void**)&pcWebSettings ) == EOK )
			{
				delete m_pcWebSettings;
				m_pcWebSettings = pcWebSettings;
			}

			break;
		}
		case ID_MENU_BOOKMARKS_ADD:
		{
			String cShortTitle, cLongTitle, cURL;

			pcWebView->GetTitle( cShortTitle, cLongTitle );
			cURL = pcWebView->GetCurrentURL();

			m_pcBookmarksManager->AddBookmark( cLongTitle, cURL );

			break;
		}
		case ID_MENU_BOOKMARKS_MANAGE:
		{
			String cPath;
			if( pcMsg->FindString( "path", &cPath ) != EOK )
				break;

			/* XXXKV: This is a really rubbish way to manage bookmarks: we should have
			   a proper in-application dialog to do it */

			/* Open a filebrowser window */
			if( fork() == 0 )
			{
				set_thread_priority( -1, 0 );
				execlp( "/system/bin/FileBrowser", "/system/bin/FileBrowser", cPath.c_str(), NULL );
			}
			break;
		}
		case ID_BOOKMARK_GO:
		{
			String cURL;

			if( pcMsg->FindString( "url", &cURL ) == EOK )
				OpenURL( cURL );

			break;
		}
		case ID_TAB_CHANGED:
		{
			ChangeTab( m_pcTabView->GetSelection() );
			break;
		}
		case ID_WEBVIEW_SET_TITLE:
		{
			String cShortTitle, cLongTitle;
			uint nTabIndex;

			if( pcMsg->FindString( "short", &cShortTitle ) == EOK &&
				pcMsg->FindString( "long", &cLongTitle ) == EOK &&
				pcMsg->FindInt32( "index", (int32*)&nTabIndex ) == EOK )
			{
				m_pcTabView->SetTabTitle( nTabIndex, cShortTitle );

				/* If this is the currently selected tab, set the Window title to the full page title */
				if( nTabIndex == m_pcTabView->GetSelection() )
					SetTitle( cLongTitle );
			}

			break;
		}
		case ID_WEBVIEW_LOAD_STARTED:
		{
			String cURL;
			uint nTabIndex;

			if( pcMsg->FindString( "url", &cURL ) == EOK &&
				pcMsg->FindInt32( "index", (int32*)&nTabIndex ) == EOK )
			{
				if( nTabIndex == m_pcTabView->GetSelection() )
				{
					m_pcUrlEdit->SetCurrentString( cURL.c_str() );
					UpdateButtonState( true );
				}
			}
			break;
		}
		case ID_WEBVIEW_LOAD_FINISHED:
		{
			uint nTabIndex;
			if( pcMsg->FindInt32( "index", (int32*)&nTabIndex ) == EOK )
				if( nTabIndex == m_pcTabView->GetSelection() )
					UpdateButtonState( false );
			break;
		}
		default:
		{
			fprintf( stderr, "Unknown message with code #%d\n", pcMsg->GetCode() );
			Window::HandleMessage( pcMsg );
			break;
		}
	}
}
예제 #2
0
void BrowserWindow::MessageReceived( BMessage* pcMsg )
{
	switch( pcMsg->what )
	{
	case ID_URL_CHANGED:
	{

		std::string cURL = m_pcURLView->Text();

		for ( uint i = 0 ; i <= cURL.size() ; ++i ) {
			if ( i == cURL.size() || isalnum( cURL[i] ) == false ) {
				if ( i == cURL.size() || cURL[i] != ':' ) {
					std::string cTmp = cURL;
					cURL = "http://";
					for ( uint j = 0 ; j < cTmp.size() ; ++j ) {
						if ( cTmp[j] != '/' ) {
							cURL.insert( cURL.end(), cTmp.begin() + j, cTmp.end() );
							break;
						}
					}
				}
				break;
			}
		}
		m_pcHTMLPart->view()->MakeFocus();
		OpenURL( cURL, KParts::URLArgs() );
//----------------------------------------------
#if 0
		uint32 nEvents = 0;
		pcMsg->FindInt( "events", &nEvents );
		
		if ( nEvents & os::TextView::EI_ESC_PRESSED ) {
			m_pcURLView->Set( m_pcHTMLPart->url().prettyURL().utf8().data() );
			m_pcHTMLPart->view()->MakeFocus();
		break;
		}
		if ( nEvents & os::TextView::EI_ENTER_PRESSED ) {
		std::string cURL = m_pcURLView->GetBuffer()[0];

		for ( uint i = 0 ; i <= cURL.size() ; ++i ) {
			if ( i == cURL.size() || isalnum( cURL[i] ) == false ) {
				if ( i == cURL.size() || cURL[i] != ':' ) {
					std::string cTmp = cURL;
					cURL = "http://";
					for ( uint j = 0 ; j < cTmp.size() ; ++j ) {
						if ( cTmp[j] != '/' ) {
							cURL.insert( cURL.end(), cTmp.begin() + j, cTmp.end() );
							break;
						}
					}
				}
				break;
			}
		}
		UpdateHistory();
		AddHistoryEntry();
		m_pcHTMLPart->view()->MakeFocus();
		OpenURL( cURL, KParts::URLArgs() );
		UpdateHistory();
		}
#endif
		break;
	}
	case ID_ACTIVATE_URLEDIT:
		m_pcURLView->MakeFocus( true );
#if 0
		m_pcURLView->SetCursor( -1, 0 );
#endif
		m_pcURLView->TextView()->SelectAll();
		break;
	case ID_RELOAD:
	{
		KParts::URLArgs cArgs;
		cArgs.reload = true;
		cArgs.xOffset = m_pcHTMLPart->browserExtension()->xOffset();
		cArgs.yOffset = m_pcHTMLPart->browserExtension()->yOffset();
#if 0
		OpenURL( m_pcURLView->GetBuffer()[0], cArgs );
#endif
		OpenURL( m_pcURLView->Text(), cArgs );
		break;
	}
	case ID_NEW_WINDOW:
	{
		BrowserWindow* pcNewWindow = new BrowserWindow( Frame().OffsetByCopy( 30.0f, 30.0f ), false );
		pcNewWindow->Show();
#if 0
		UpdateHistory();
		std::vector<HistoryEntry*> cHistory = m_cHistory;
		cHistory.resize( m_nCurHistoryPos + 1 );
		BrowserWindow* pcNewWindow = new BrowserWindow( GetFrame() + os::Point( 30.0f, 30.0f ), &cHistory, false );
		pcNewWindow->GoHistory(0);
		pcNewWindow->Show( true );
		pcNewWindow->MakeFocus( true );	
#endif
		break;
	}
	case ID_OPEN_LINK:
	{
		BString cURL;
		pcMsg->FindString( "url", &cURL );
		BrowserWindow* pcNewWindow = new BrowserWindow( Frame().OffsetByCopy( 30.0f, 30.0f ), false  );
		pcNewWindow->OpenURL( cURL.String(), KParts::URLArgs() );
		pcNewWindow->Show();
#if 0
		std::string cURL;
		pcMsg->FindString( "url", &cURL );
		std::vector<HistoryEntry*> cHistory = m_cHistory;
		cHistory.resize( m_nCurHistoryPos + 1 );
		BrowserWindow* pcNewWindow = new BrowserWindow( GetFrame() + os::Point( 30.0f, 30.0f ), &cHistory, false );
		pcNewWindow->OpenURL( cURL, KParts::URLArgs() );
		pcNewWindow->AddHistoryEntry();
		pcNewWindow->Show( true );
		pcNewWindow->MakeFocus( true );	
#endif
		break;
	}
	case ID_SAVE_LINK:
	{
		BString cURL;
		pcMsg->FindString( "url", &cURL );

		m_pcHTMLPart->browserExtension()->SaveURL( cURL.String(), "Save link as..." );
		break;
	}
	case ID_COPY_LINK_LOCATION:
	{
#if 0
		std::string cURL;
		pcMsg->FindString( "url", &cURL );

		os::Clipboard cClipboard;
		cClipboard.Lock();
		cClipboard.Clear();
		os::Message* pcData = cClipboard.GetData();
		pcData->AddString( "text/plain", cURL );
		cClipboard.Commit();
		cClipboard.Unlock();
#endif
		break;
	}
	case ID_COPY:
	{
#if 0
		if ( m_cSelectedText.empty() ) {
			break;
		}
		os::Clipboard cClipboard;
		cClipboard.Lock();
		cClipboard.Clear();
		os::Message* pcData = cClipboard.GetData();
		pcData->AddString( "text/plain", m_cSelectedText );
		cClipboard.Commit();
		cClipboard.Unlock();
#endif
		break;
	}
	case ID_FIND:
#if 0
		if ( m_pcSearchDialog == NULL ) {
		m_pcHTMLPart->findTextBegin();
		m_pcSearchDialog = new FindDialog( os::Messenger(this), os::Message(ID_DO_SEARCH), os::Message(ID_SEARCH_CLOSED) );
		m_pcSearchDialog->Show();
		m_pcSearchDialog->MakeFocus();
		}
#endif
		break;
	case ID_DO_SEARCH:
	{
#if 0
		bool bFromTop = false;
		pcMsg->FindBool( "from_top", &bFromTop );
		pcMsg->FindBool( "case_sensitive", &m_bSearchCaseSensitive );
		pcMsg->FindString( "string", &m_cLastSearchStr );

		KHTMLView* pcActiveView = dynamic_cast<KHTMLView*>(GetFocusChild());
		
		KHTMLPart* pcPart = m_pcHTMLPart;
		if ( pcActiveView != NULL ) {
		pcPart = pcActiveView->part();
		}
		if ( bFromTop ) {
		pcPart->findTextBegin();
		}
		pcPart->findTextNext( m_cLastSearchStr.c_str(), true, m_bSearchCaseSensitive );
		break;
#endif
	}
	case ID_REPEAT_FIND:
#if 0
		if ( m_cLastSearchStr.empty() == false ) {
		KHTMLView* pcActiveView = dynamic_cast<KHTMLView*>(GetFocusChild());
		
		KHTMLPart* pcPart = m_pcHTMLPart;
		if ( pcActiveView != NULL ) {
			pcPart = pcActiveView->part();
		}		
		pcPart->findTextNext( m_cLastSearchStr.c_str(), true, m_bSearchCaseSensitive );
		} else if ( m_pcSearchDialog == NULL ) {
		m_pcHTMLPart->findTextBegin();
		m_pcSearchDialog = new FindDialog( os::Messenger(this), os::Message(ID_DO_SEARCH), os::Message(ID_SEARCH_CLOSED) );
		m_pcSearchDialog->Show();
		m_pcSearchDialog->MakeFocus();
		}
		break;
	case ID_SEARCH_CLOSED:
		m_pcSearchDialog = NULL;
		break;
#endif
	case ID_GOHOME:
		OpenURL( "http://www.openbeos.org/", KParts::URLArgs() );
#if 0
		UpdateHistory();
		AddHistoryEntry();
		OpenURL( "http://www.atheos.cx/", KParts::URLArgs() );
		UpdateHistory();
#endif
		break;
#if 0
	case ID_PREV_URL:
		GoHistory( -1 );
		break;
	case ID_NEXT_URL:
		GoHistory( 1 );
		break;
#endif
	case ID_START_DOWNLOAD:
	{
#if 0
		DownloadNode* psNode = NULL;
		std::string   cPath;
		pcMsg->FindPointer( "node", (void**) &psNode );
		pcMsg->FindString( "path", &cPath );

		
		if ( psNode == NULL ) {
		break; // Should never happen
		}
		psNode->m_cPath = cPath;
		const std::string cDstDir = os::Path( cPath.c_str() ).GetDir();

		try {
		os::FSNode cDirNode( cDstDir );
		if ( cDirNode.GetDev() == psNode->m_pcTmpFile->GetDev() ) {
			rename( psNode->m_pcTmpFile->GetPath().c_str(), cPath.c_str() );
			psNode->m_pcTmpFile->Detatch(); // Make sure the destructor don't do anything silly.
		} else {
			psNode->m_pcFile = new os::File( cPath, O_WRONLY | O_CREAT );
			psNode->m_pcTmpFile->Seek( 0, SEEK_SET );
			char anBuffer[32*1024];
			for (;;) {
			int nLen = psNode->m_pcTmpFile->Read( anBuffer, sizeof(anBuffer) );
			if ( nLen < 0 ) {
				throw os::errno_exception( "Failed to copy temporary file" );
			}
			if ( psNode->m_pcFile->Write( anBuffer, nLen ) != nLen ) {
				throw os::errno_exception( "Failed to copy temporary file" );
			}
			if ( nLen < int(sizeof(anBuffer)) ) {
				break;
			}
			}
			delete psNode->m_pcTmpFile;
			psNode->m_pcTmpFile = NULL;
		}
		} catch(...) {
		(new os::Alert( "Error: Failed to create file!",
				os::String().Format( "Failed to create '%s'", cPath.c_str() ), 0,
				"Sorry!", NULL ))->Go(NULL);
		}
		
		psNode->m_pcRequester = NULL;

		if ( psNode->m_bDone == false ) {
		Message cCancelMsg( ID_CANCEL_DOWNLOAD );
		cCancelMsg.AddPointer( "node", psNode );
		
		psNode->m_pcProgressDlg = new DownloadProgress( os::Rect( 100, 100, 449, 349 ), os::String().Format( "Download: %s", cPath.c_str() ),
								psNode->m_cURL,
								cPath,
								psNode->m_nContentSize,
								psNode->m_nStartTime,
								os::Messenger( this ),
								cCancelMsg );

		psNode->m_pcProgressDlg->Show();
		} else {
		delete psNode->m_pcTmpFile;
		delete psNode->m_pcFile;
		delete psNode;
		}
#endif
		break;
	}
	case ID_CANCEL_DOWNLOAD:
	{
#if 0
		DownloadNode* psNode = NULL;
		pcMsg->FindPointer( "node", (void**) &psNode );
		
		if ( psNode == NULL ) {
		break; // Should never happen
		}
		if ( psNode->m_pcProgressDlg != NULL ) {
		psNode->m_pcProgressDlg->Terminate();
		psNode->m_pcProgressDlg = NULL;
		}
		if ( psNode->m_cPath.empty() == false ) {
		unlink( psNode->m_cPath.c_str() );
		}
		if ( psNode->m_bDone == false ) {
		psNode->m_pcRequester = NULL;
		psNode->m_bCanceled = true;
		delete psNode->m_pcTmpFile;
		psNode->m_pcTmpFile = NULL;
		delete psNode->m_pcFile;
		psNode->m_pcFile = NULL;
		} else {
		delete psNode->m_pcTmpFile;
		delete psNode->m_pcFile;
		delete psNode;		
		}
#endif
		break;
	}
	default:
		BWindow::MessageReceived( pcMsg );
		break;
	}
}