[haiku-webkit-commits] r314 - webkit/trunk/WebKit/haiku/WebPositive

  • From: webkit@xxxxxxxxxxxxxxx
  • To: haiku-webkit-commits@xxxxxxxxxxxxx
  • Date: Tue, 16 Mar 2010 13:04:01 +0000

Author: stippi
Date: Tue Mar 16 13:04:01 2010
New Revision: 314
URL: http://mmlr.dyndns.org/changeset/314

Log:
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.

Modified:
   webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.cpp
   webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.h

Modified: webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.cpp
==============================================================================
--- webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.cpp     Mon Mar 15 
16:52:29 2010        (r313)
+++ webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.cpp     Tue Mar 16 
13:04:01 2010        (r314)
@@ -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 @@
        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 @@
                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 @@
                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 @@
        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;
+       }
+}
+

Modified: webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.h
==============================================================================
--- webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.h       Mon Mar 15 
16:52:29 2010        (r313)
+++ webkit/trunk/WebKit/haiku/WebPositive/BrowserWindow.h       Tue Mar 16 
13:04:01 2010        (r314)
@@ -38,6 +38,7 @@
 class BCheckBox;
 class BLayoutItem;
 class BMenu;
+class BPath;
 class BStatusBar;
 class BStringView;
 class BTextControl;
@@ -119,8 +120,13 @@
                        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;

Other related posts:

  • » [haiku-webkit-commits] r314 - webkit/trunk/WebKit/haiku/WebPositive - webkit