From e509fd60c14849be0d9bc390f0761149fab42985 Mon Sep 17 00:00:00 2001 From: stippi Date: Tue, 16 Mar 2010 13:04:01 +0000 Subject: [PATCH] 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 --- src/apps/webpositive/BrowserWindow.cpp | 149 +++++++++++++++++++++++++ src/apps/webpositive/BrowserWindow.h | 6 + 2 files changed, 155 insertions(+) diff --git a/src/apps/webpositive/BrowserWindow.cpp b/src/apps/webpositive/BrowserWindow.cpp index 6f09e9bc74..1bc8e407cc 100644 --- a/src/apps/webpositive/BrowserWindow.cpp +++ b/src/apps/webpositive/BrowserWindow.cpp @@ -47,13 +47,18 @@ #include #include #include +#include #include +#include +#include #include #include #include #include #include +#include #include +#include #include #include #include @@ -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; + } +} + diff --git a/src/apps/webpositive/BrowserWindow.h b/src/apps/webpositive/BrowserWindow.h index 0b2c4788ea..caa53b4571 100644 --- a/src/apps/webpositive/BrowserWindow.h +++ b/src/apps/webpositive/BrowserWindow.h @@ -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;