From 06ea485752153d9dd4bd8d02e617b28758dd7229 Mon Sep 17 00:00:00 2001 From: stippi Date: Sat, 1 May 2010 19:20:53 +0000 Subject: [PATCH] Add a context menu to the download progress view. It allows to open the containing folder (and selects the file in it), and to copy the download URL to the clipboard. git-svn-id: http://svn.haiku-os.org/webpositive/webkit/trunk@470 94f232f2-1747-11df-bad5-a5bfde151594 --- src/apps/webpositive/DownloadProgressView.cpp | 112 +++++++++++++++++- src/apps/webpositive/DownloadProgressView.h | 2 + 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/src/apps/webpositive/DownloadProgressView.cpp b/src/apps/webpositive/DownloadProgressView.cpp index ddee6317c5..54ca1797e6 100644 --- a/src/apps/webpositive/DownloadProgressView.cpp +++ b/src/apps/webpositive/DownloadProgressView.cpp @@ -32,12 +32,15 @@ #include #include #include +#include #include #include #include #include +#include #include #include +#include #include #include #include @@ -49,10 +52,12 @@ enum { - OPEN_DOWNLOAD = 'opdn', - RESTART_DOWNLOAD = 'rsdn', - CANCEL_DOWNLOAD = 'cndn', - REMOVE_DOWNLOAD = 'rmdn', + OPEN_DOWNLOAD = 'opdn', + RESTART_DOWNLOAD = 'rsdn', + CANCEL_DOWNLOAD = 'cndn', + REMOVE_DOWNLOAD = 'rmdn', + COPY_URL_TO_CLIPBOARD = 'curl', + OPEN_CONTAINING_FOLDER = 'opfd', }; const bigtime_t kMaxUpdateInterval = 100000LL; @@ -203,6 +208,10 @@ DownloadProgressView::DownloadProgressView(const BMessage* archive) bool DownloadProgressView::Init(BMessage* archive) { + // We need to receive mouse events even for the areas of children views, + // so we can pop up a context menu. + SetEventMask(B_POINTER_EVENTS); + fCurrentSize = 0; fExpectedSize = 0; fLastUpdateTime = 0; @@ -330,6 +339,35 @@ DownloadProgressView::AllAttached() } +void +DownloadProgressView::MouseDown(BPoint where) +{ + if (!Bounds().Contains(where)) + return; + + int32 buttons; + if (Window()->CurrentMessage()->FindInt32("buttons", &buttons) != B_OK) + return; + + if ((buttons & B_SECONDARY_MOUSE_BUTTON) == 0) + return; + + where = ConvertToScreen(where) + BPoint(2, 2); + + BPopUpMenu* contextMenu = new BPopUpMenu("download context"); + BMenuItem* copyURL = new BMenuItem("Copy URL to clipboard", + new BMessage(COPY_URL_TO_CLIPBOARD)); + copyURL->SetEnabled(fURL.Length() > 0); + contextMenu->AddItem(copyURL); + BMenuItem* openFolder = new BMenuItem("Open containing folder", + new BMessage(OPEN_CONTAINING_FOLDER)); + contextMenu->AddItem(openFolder); + + contextMenu->SetTargetForItems(this); + contextMenu->Go(where, true, true, true); +} + + void DownloadProgressView::Draw(BRect updateRect) { @@ -477,6 +515,71 @@ DownloadProgressView::MessageReceived(BMessage* message) } break; } + + // Context menu messages + case COPY_URL_TO_CLIPBOARD: + if (be_clipboard->Lock()) { + BMessage* data = be_clipboard->Data(); + if (data != NULL) { + be_clipboard->Clear(); + data->AddData("text/plain", B_MIME_TYPE, fURL.String(), + fURL.Length()); + } + be_clipboard->Commit(); + be_clipboard->Unlock(); + } + break; + case OPEN_CONTAINING_FOLDER: + if (fPath.InitCheck() == B_OK) { + BPath containingFolder; + if (fPath.GetParent(&containingFolder) != B_OK) + break; + BEntry entry(containingFolder.Path()); + if (!entry.Exists()) + break; + entry_ref ref; + if (entry.GetRef(&ref) != B_OK) + break; + be_roster->Launch(&ref); + + // Use Tracker scripting and select the download pose + // in the window. + // TODO: We should somehow get the window that just openend. + // Using the name like this is broken when there are multiple + // windows open with this name. Also Tracker does not scroll + // to this entry. + BString windowName = ref.name; + BString fullWindowName = containingFolder.Path(); + + BMessenger trackerMessenger("application/x-vnd.Be-TRAK"); + if (trackerMessenger.IsValid() + && get_ref_for_path(fPath.Path(), &ref) == B_OK) { + // We need to wait a bit until the folder is open. + // TODO: This is also too fragile... we should be able + // to wait for the roster message. + snooze(250000); + int32 tries = 2; + while (tries > 0) { + BMessage selectionCommand(B_SET_PROPERTY); + selectionCommand.AddSpecifier("Selection"); + selectionCommand.AddSpecifier("Poses"); + selectionCommand.AddSpecifier("Window", + windowName.String()); + selectionCommand.AddRef("data", &ref); + BMessage reply; + trackerMessenger.SendMessage(&selectionCommand, &reply); + int32 error; + if (reply.FindInt32("error", &error) != B_OK + || error == B_OK) { + break; + } + windowName = fullWindowName; + tries--; + } + } + } + break; + default: BGroupView::MessageReceived(message); } @@ -534,6 +637,7 @@ DownloadProgressView::DownloadCanceled() fBottomButton->SetMessage(new BMessage(REMOVE_DOWNLOAD)); fBottomButton->SetEnabled(true); fInfoView->SetText(""); + fPath.Unset(); } diff --git a/src/apps/webpositive/DownloadProgressView.h b/src/apps/webpositive/DownloadProgressView.h index 4c4ae7e48f..256245442d 100644 --- a/src/apps/webpositive/DownloadProgressView.h +++ b/src/apps/webpositive/DownloadProgressView.h @@ -57,6 +57,8 @@ public: virtual void DetachedFromWindow(); virtual void AllAttached(); + virtual void MouseDown(BPoint where); + virtual void Draw(BRect updateRect); virtual void MessageReceived(BMessage* message);