Added code for bookmarking the current page and opening the Bookmarks folder

in Tracker (or the preferred file manager). Populating the Bookmarks menu is
still missing.

git-svn-id: http://svn.haiku-os.org/webpositive/webkit/trunk@314 94f232f2-1747-11df-bad5-a5bfde151594
This commit is contained in:
stippi 2010-03-16 13:04:01 +00:00 committed by Alexandre Deckner
parent 88e373028c
commit e509fd60c1
2 changed files with 155 additions and 0 deletions

View File

@ -47,13 +47,18 @@
#include <Bitmap.h>
#include <Button.h>
#include <CheckBox.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <GridLayoutBuilder.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <NodeInfo.h>
#include <Path.h>
#include <Roster.h>
#include <SeparatorView.h>
#include <SpaceLayoutItem.h>
#include <StatusBar.h>
@ -72,6 +77,9 @@ enum {
RELOAD = 'reld',
CLEAR_HISTORY = 'clhs',
CREATE_BOOKMARK = 'crbm',
SHOW_BOOKMARKS = 'shbm',
TEXT_SIZE_INCREASE = 'tsin',
TEXT_SIZE_DECREASE = 'tsdc',
TEXT_SIZE_RESET = 'tsrs',
@ -247,6 +255,14 @@ BrowserWindow::BrowserWindow(BRect frame, ToolbarPolicy toolbarPolicy)
fGoMenu = new BMenu("Go");
mainMenu->AddItem(fGoMenu);
fBookmarkMenu = new BMenu("Bookmarks");
fBookmarkMenu->AddItem(new BMenuItem("Bookmark this page",
new BMessage(CREATE_BOOKMARK), 'B'));
fBookmarkMenu->AddItem(new BMenuItem("Manage bookmarks",
new BMessage(SHOW_BOOKMARKS)));
// fBookmarkMenu->AddSeparatorItem();
mainMenu->AddItem(fBookmarkMenu);
// Back, Forward & Stop
fBackButton = new IconButton("Back", 0, NULL, new BMessage(GO_BACK));
fBackButton->SetIcon(201);
@ -458,6 +474,13 @@ BrowserWindow::MessageReceived(BMessage* message)
break;
}
case CREATE_BOOKMARK:
_CreateBookmark();
break;
case SHOW_BOOKMARKS:
_ShowBookmarks();
break;
case B_SIMPLE_DATA: {
// User possibly dropped files on this window.
// If there is more than one entry_ref, let the app handle it (open one
@ -907,3 +930,129 @@ BrowserWindow::_ShutdownTab(int32 index)
else
delete view;
}
status_t
BrowserWindow::_BookmarkPath(BPath& path) const
{
status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
if (ret != B_OK)
return ret;
ret = path.Append(kApplicationName);
if (ret != B_OK)
return ret;
ret = path.Append("Bookmarks");
if (ret != B_OK)
return ret;
return create_directory(path.Path(), 0777);
}
void
BrowserWindow::_CreateBookmark()
{
BPath path;
status_t status = _BookmarkPath(path);
if (status != B_OK) {
BString message("There was an error retrieving the bookmark "
"folder.\n\n");
message << "Error: " << strerror(status);
BAlert* alert = new BAlert("Bookmark error", message.String(), "OK",
NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
alert->Go();
return;
}
BWebView* webView = CurrentWebView();
BString url(webView->MainFrameURL());
// Create a bookmark file
BFile bookmarkFile;
BString bookmarkName(webView->MainFrameTitle());
BPath entryPath(path);
status = entryPath.Append(bookmarkName);
BEntry entry;
if (status == B_OK)
status = entry.SetTo(entryPath.Path(), true);
if (status == B_OK) {
if (entry.Exists()) {
BString storedURL;
if (bookmarkFile.SetTo(&entry, B_READ_ONLY) == B_OK
&& bookmarkFile.ReadAttrString("META:url",
&storedURL) == B_OK) {
// Just bail if the bookmark already exists
if (storedURL == url) {
BString message("A bookmark for this page (");
message << webView->MainFrameTitle();
message << ") already exists.";
BAlert* alert = new BAlert("Bookmark info",
message.String(), "OK");
alert->Go();
return;
}
}
}
int32 tries = 1;
while (entry.Exists()) {
// Find a unique name for the bookmark
bookmarkName = webView->MainFrameTitle();
bookmarkName << " " << tries++;
entryPath = path;
status = entryPath.Append(bookmarkName);
if (status == B_OK)
status = entry.SetTo(entryPath.Path(), true);
if (status != B_OK)
break;
}
}
if (status == B_OK) {
status = bookmarkFile.SetTo(&entry,
B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
}
if (status == B_OK)
status = bookmarkFile.WriteAttrString("META:url", &url);
if (status == B_OK) {
BString title = webView->MainFrameTitle();
bookmarkFile.WriteAttrString("META:title", &title);
}
BNodeInfo nodeInfo(&bookmarkFile);
if (status == B_OK)
status = nodeInfo.SetType("application/x-vnd.Be-bookmark");
if (status != B_OK) {
BString message("There was an error creating the bookmark "
"file.\n\n");
message << "Error: " << strerror(status);
BAlert* alert = new BAlert("Bookmark error", message.String(), "OK",
NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
alert->Go();
return;
}
}
void
BrowserWindow::_ShowBookmarks()
{
BPath path;
entry_ref ref;
status_t status = _BookmarkPath(path);
if (status == B_OK)
status = get_ref_for_path(path.Path(), &ref);
if (status == B_OK)
status = be_roster->Launch(&ref);
if (status != B_OK && status != B_ALREADY_RUNNING) {
BString message("There was an error trying to show the Bookmarks "
"folder.\n\n");
message << "Error: " << strerror(status);
BAlert* alert = new BAlert("Bookmark error", message.String(), "OK",
NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
alert->Go();
return;
}
}

View File

@ -38,6 +38,7 @@ class BButton;
class BCheckBox;
class BLayoutItem;
class BMenu;
class BPath;
class BStatusBar;
class BStringView;
class BTextControl;
@ -119,8 +120,13 @@ private:
void _UpdateTabGroupVisibility();
void _ShutdownTab(int32 index);
status_t _BookmarkPath(BPath& path) const;
void _CreateBookmark();
void _ShowBookmarks();
private:
BMenu* fGoMenu;
BMenu* fBookmarkMenu;
IconButton* fBackButton;
IconButton* fForwardButton;
IconButton* fStopButton;