Converted to Haiku coding style.
git-svn-id: http://svn.haiku-os.org/webpositive/webkit/trunk@337 94f232f2-1747-11df-bad5-a5bfde151594
This commit is contained in:
committed by
Alexandre Deckner
parent
aa5ba348ea
commit
9ccfda9c50
@@ -25,12 +25,10 @@
|
|||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
|
||||||
#include "DownloadWindow.h"
|
#include "DownloadWindow.h"
|
||||||
|
|
||||||
#include "BrowserApp.h"
|
#include <stdio.h>
|
||||||
#include "WebDownload.h"
|
|
||||||
#include "WebPage.h"
|
|
||||||
#include <Alert.h>
|
#include <Alert.h>
|
||||||
#include <Bitmap.h>
|
#include <Bitmap.h>
|
||||||
#include <Button.h>
|
#include <Button.h>
|
||||||
@@ -48,296 +46,310 @@
|
|||||||
#include <SeparatorView.h>
|
#include <SeparatorView.h>
|
||||||
#include <SpaceLayoutItem.h>
|
#include <SpaceLayoutItem.h>
|
||||||
#include <StatusBar.h>
|
#include <StatusBar.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
#include "BrowserApp.h"
|
||||||
|
#include "WebDownload.h"
|
||||||
|
#include "WebPage.h"
|
||||||
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
REMOVE_FINISHED_DOWNLOADS = 'rmfd',
|
REMOVE_FINISHED_DOWNLOADS = 'rmfd',
|
||||||
OPEN_DOWNLOAD = 'opdn',
|
OPEN_DOWNLOAD = 'opdn',
|
||||||
RESTART_DOWNLOAD = 'rsdn',
|
RESTART_DOWNLOAD = 'rsdn',
|
||||||
CANCEL_DOWNLOAD = 'cndn',
|
CANCEL_DOWNLOAD = 'cndn',
|
||||||
REMOVE_DOWNLOAD = 'rmdn',
|
REMOVE_DOWNLOAD = 'rmdn',
|
||||||
SAVE_SETTINGS = 'svst'
|
SAVE_SETTINGS = 'svst'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class IconView : public BView {
|
class IconView : public BView {
|
||||||
public:
|
public:
|
||||||
IconView(const BEntry& entry)
|
IconView(const BEntry& entry)
|
||||||
: BView("Download icon", B_WILL_DRAW)
|
:
|
||||||
, m_iconBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32)
|
BView("Download icon", B_WILL_DRAW),
|
||||||
{
|
fIconBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32)
|
||||||
BNode node(&entry);
|
{
|
||||||
BNodeInfo info(&node);
|
BNode node(&entry);
|
||||||
info.GetTrackerIcon(&m_iconBitmap, B_LARGE_ICON);
|
BNodeInfo info(&node);
|
||||||
SetDrawingMode(B_OP_OVER);
|
info.GetTrackerIcon(&fIconBitmap, B_LARGE_ICON);
|
||||||
}
|
SetDrawingMode(B_OP_OVER);
|
||||||
|
}
|
||||||
|
|
||||||
IconView(BMessage* archive)
|
IconView(BMessage* archive)
|
||||||
: BView("Download icon", B_WILL_DRAW)
|
:
|
||||||
, m_iconBitmap(archive)
|
BView("Download icon", B_WILL_DRAW),
|
||||||
{
|
fIconBitmap(archive)
|
||||||
SetDrawingMode(B_OP_OVER);
|
{
|
||||||
}
|
SetDrawingMode(B_OP_OVER);
|
||||||
|
}
|
||||||
|
|
||||||
status_t saveSettings(BMessage* archive)
|
status_t SaveSettings(BMessage* archive)
|
||||||
{
|
{
|
||||||
return m_iconBitmap.Archive(archive);
|
return fIconBitmap.Archive(archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void AttachedToWindow()
|
virtual void AttachedToWindow()
|
||||||
{
|
{
|
||||||
SetViewColor(Parent()->ViewColor());
|
SetViewColor(Parent()->ViewColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void Draw(BRect updateRect)
|
virtual void Draw(BRect updateRect)
|
||||||
{
|
{
|
||||||
DrawBitmapAsync(&m_iconBitmap);
|
DrawBitmapAsync(&fIconBitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual BSize MinSize()
|
virtual BSize MinSize()
|
||||||
{
|
{
|
||||||
return BSize(m_iconBitmap.Bounds().Width(), m_iconBitmap.Bounds().Height());
|
return BSize(fIconBitmap.Bounds().Width(), fIconBitmap.Bounds().Height());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual BSize PreferredSize()
|
virtual BSize PreferredSize()
|
||||||
{
|
{
|
||||||
return MinSize();
|
return MinSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual BSize MaxSize()
|
virtual BSize MaxSize()
|
||||||
{
|
{
|
||||||
return MinSize();
|
return MinSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BBitmap m_iconBitmap;
|
BBitmap fIconBitmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class SmallButton : public BButton {
|
class SmallButton : public BButton {
|
||||||
public:
|
public:
|
||||||
SmallButton(const char* label, BMessage* message = NULL)
|
SmallButton(const char* label, BMessage* message = NULL)
|
||||||
: BButton(label, message)
|
:
|
||||||
{
|
BButton(label, message)
|
||||||
BFont font;
|
{
|
||||||
GetFont(&font);
|
BFont font;
|
||||||
float size = ceilf(font.Size() * 0.8);
|
GetFont(&font);
|
||||||
font.SetSize(max_c(8, size));
|
float size = ceilf(font.Size() * 0.8);
|
||||||
SetFont(&font, B_FONT_SIZE);
|
font.SetSize(max_c(8, size));
|
||||||
}
|
SetFont(&font, B_FONT_SIZE);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class DownloadProgressView : public BGridView {
|
class DownloadProgressView : public BGridView {
|
||||||
public:
|
public:
|
||||||
DownloadProgressView(BWebDownload* download)
|
DownloadProgressView(BWebDownload* download)
|
||||||
: BGridView(8, 3)
|
:
|
||||||
, m_download(download)
|
BGridView(8, 3),
|
||||||
, m_url(download->URL())
|
fDownload(download),
|
||||||
, m_path(download->Path())
|
fURL(download->URL()),
|
||||||
, m_expectedSize(download->ExpectedSize())
|
fPath(download->Path()),
|
||||||
{
|
fExpectedSize(download->ExpectedSize())
|
||||||
}
|
{
|
||||||
|
}
|
||||||
|
|
||||||
DownloadProgressView(const BMessage* archive)
|
DownloadProgressView(const BMessage* archive)
|
||||||
: BGridView(8, 3)
|
:
|
||||||
, m_download(NULL)
|
BGridView(8, 3),
|
||||||
, m_url()
|
fDownload(NULL),
|
||||||
, m_path()
|
fURL(),
|
||||||
, m_expectedSize(0)
|
fPath(),
|
||||||
{
|
fExpectedSize(0)
|
||||||
const char* string;
|
{
|
||||||
if (archive->FindString("path", &string) == B_OK)
|
const char* string;
|
||||||
m_path.SetTo(string);
|
if (archive->FindString("path", &string) == B_OK)
|
||||||
if (archive->FindString("url", &string) == B_OK)
|
fPath.SetTo(string);
|
||||||
m_url = string;
|
if (archive->FindString("url", &string) == B_OK)
|
||||||
}
|
fURL = string;
|
||||||
|
}
|
||||||
|
|
||||||
bool init(BMessage* archive = NULL)
|
bool Init(BMessage* archive = NULL)
|
||||||
{
|
{
|
||||||
BEntry entry(m_path.Path());
|
BEntry entry(fPath.Path());
|
||||||
if (!entry.Exists() && !archive)
|
if (!entry.Exists() && !archive)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
SetViewColor(245, 245, 245);
|
SetViewColor(245, 245, 245);
|
||||||
SetFlags(Flags() | B_FULL_UPDATE_ON_RESIZE | B_WILL_DRAW);
|
SetFlags(Flags() | B_FULL_UPDATE_ON_RESIZE | B_WILL_DRAW);
|
||||||
|
|
||||||
BGridLayout* layout = GridLayout();
|
BGridLayout* layout = GridLayout();
|
||||||
m_statusBar = new BStatusBar("download progress", m_path.Leaf());
|
fStatusBar = new BStatusBar("download progress", fPath.Leaf());
|
||||||
m_statusBar->SetMaxValue(100);
|
fStatusBar->SetMaxValue(100);
|
||||||
if (archive) {
|
if (archive) {
|
||||||
float value;
|
float value;
|
||||||
if (archive->FindFloat("value", &value) == B_OK)
|
if (archive->FindFloat("value", &value) == B_OK)
|
||||||
m_statusBar->SetTo(value);
|
fStatusBar->SetTo(value);
|
||||||
}
|
}
|
||||||
m_statusBar->SetBarHeight(12);
|
fStatusBar->SetBarHeight(12);
|
||||||
|
|
||||||
if (entry.Exists())
|
if (entry.Exists())
|
||||||
m_iconView = new IconView(entry);
|
fIconView = new IconView(entry);
|
||||||
else
|
else
|
||||||
m_iconView = new IconView(archive);
|
fIconView = new IconView(archive);
|
||||||
|
|
||||||
if (!m_download && m_statusBar->CurrentValue() < 100)
|
if (!fDownload && fStatusBar->CurrentValue() < 100)
|
||||||
m_button1 = new SmallButton("Restart", new BMessage(RESTART_DOWNLOAD));
|
fTopButton = new SmallButton("Restart", new BMessage(RESTART_DOWNLOAD));
|
||||||
else {
|
else {
|
||||||
m_button1 = new SmallButton("Open", new BMessage(OPEN_DOWNLOAD));
|
fTopButton = new SmallButton("Open", new BMessage(OPEN_DOWNLOAD));
|
||||||
m_button1->SetEnabled(m_download == NULL);
|
fTopButton->SetEnabled(fDownload == NULL);
|
||||||
}
|
}
|
||||||
if (m_download)
|
if (fDownload)
|
||||||
m_button2 = new SmallButton("Cancel", new BMessage(CANCEL_DOWNLOAD));
|
fBottomButton = new SmallButton("Cancel", new BMessage(CANCEL_DOWNLOAD));
|
||||||
else {
|
else {
|
||||||
m_button2 = new SmallButton("Remove", new BMessage(REMOVE_DOWNLOAD));
|
fBottomButton = new SmallButton("Remove", new BMessage(REMOVE_DOWNLOAD));
|
||||||
m_button2->SetEnabled(m_download == NULL);
|
fBottomButton->SetEnabled(fDownload == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
layout->SetInsets(8, 5, 5, 6);
|
layout->SetInsets(8, 5, 5, 6);
|
||||||
layout->AddView(m_iconView, 0, 0, 1, 2);
|
layout->AddView(fIconView, 0, 0, 1, 2);
|
||||||
layout->AddView(m_statusBar, 1, 0, 1, 2);
|
layout->AddView(fStatusBar, 1, 0, 1, 2);
|
||||||
layout->AddView(m_button1, 2, 0);
|
layout->AddView(fTopButton, 2, 0);
|
||||||
layout->AddView(m_button2, 2, 1);
|
layout->AddView(fBottomButton, 2, 1);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
status_t saveSettings(BMessage* archive)
|
status_t SaveSettings(BMessage* archive)
|
||||||
{
|
{
|
||||||
if (!archive)
|
if (!archive)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
status_t ret = archive->AddString("path", m_path.Path());
|
status_t ret = archive->AddString("path", fPath.Path());
|
||||||
if (ret == B_OK)
|
if (ret == B_OK)
|
||||||
ret = archive->AddString("url", m_url.String());
|
ret = archive->AddString("url", fURL.String());
|
||||||
if (ret == B_OK)
|
if (ret == B_OK)
|
||||||
ret = archive->AddFloat("value", m_statusBar->CurrentValue());
|
ret = archive->AddFloat("value", fStatusBar->CurrentValue());
|
||||||
if (ret == B_OK)
|
if (ret == B_OK)
|
||||||
ret = m_iconView->saveSettings(archive);
|
ret = fIconView->SaveSettings(archive);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void AttachedToWindow()
|
virtual void AttachedToWindow()
|
||||||
{
|
{
|
||||||
if (m_download)
|
if (fDownload)
|
||||||
m_download->SetProgressListener(BMessenger(this));
|
fDownload->SetProgressListener(BMessenger(this));
|
||||||
m_button1->SetTarget(this);
|
fTopButton->SetTarget(this);
|
||||||
m_button2->SetTarget(this);
|
fBottomButton->SetTarget(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void AllAttached()
|
virtual void AllAttached()
|
||||||
{
|
{
|
||||||
SetViewColor(B_TRANSPARENT_COLOR);
|
SetViewColor(B_TRANSPARENT_COLOR);
|
||||||
SetLowColor(245, 245, 245);
|
SetLowColor(245, 245, 245);
|
||||||
SetHighColor(tint_color(LowColor(), B_DARKEN_1_TINT));
|
SetHighColor(tint_color(LowColor(), B_DARKEN_1_TINT));
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void Draw(BRect updateRect)
|
virtual void Draw(BRect updateRect)
|
||||||
{
|
{
|
||||||
BRect bounds(Bounds());
|
BRect bounds(Bounds());
|
||||||
bounds.bottom--;
|
bounds.bottom--;
|
||||||
FillRect(bounds, B_SOLID_LOW);
|
FillRect(bounds, B_SOLID_LOW);
|
||||||
bounds.bottom++;
|
bounds.bottom++;
|
||||||
StrokeLine(bounds.LeftBottom(), bounds.RightBottom());
|
StrokeLine(bounds.LeftBottom(), bounds.RightBottom());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void MessageReceived(BMessage* message)
|
virtual void MessageReceived(BMessage* message)
|
||||||
{
|
{
|
||||||
switch (message->what) {
|
switch (message->what) {
|
||||||
case B_DOWNLOAD_PROGRESS: {
|
case B_DOWNLOAD_PROGRESS: {
|
||||||
float progress;
|
float progress;
|
||||||
if (message->FindFloat("progress", &progress) == B_OK)
|
if (message->FindFloat("progress", &progress) == B_OK)
|
||||||
m_statusBar->SetTo(progress);
|
fStatusBar->SetTo(progress);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case OPEN_DOWNLOAD: {
|
case OPEN_DOWNLOAD: {
|
||||||
// TODO: In case of executable files, ask the user first!
|
// TODO: In case of executable files, ask the user first!
|
||||||
entry_ref ref;
|
entry_ref ref;
|
||||||
status_t status = get_ref_for_path(m_path.Path(), &ref);
|
status_t status = get_ref_for_path(fPath.Path(), &ref);
|
||||||
if (status == B_OK)
|
if (status == B_OK)
|
||||||
status = be_roster->Launch(&ref);
|
status = be_roster->Launch(&ref);
|
||||||
if (status != B_OK && status != B_ALREADY_RUNNING) {
|
if (status != B_OK && status != B_ALREADY_RUNNING) {
|
||||||
BAlert* alert = new BAlert("Open download error",
|
BAlert* alert = new BAlert("Open download error",
|
||||||
"The download could not be opened.", "OK");
|
"The download could not be opened.", "OK");
|
||||||
alert->Go(NULL);
|
alert->Go(NULL);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case RESTART_DOWNLOAD:
|
case RESTART_DOWNLOAD:
|
||||||
BWebPage::RequestDownload(m_url);
|
BWebPage::RequestDownload(fURL);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case CANCEL_DOWNLOAD:
|
||||||
|
fDownload->Cancel();
|
||||||
|
DownloadCanceled();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REMOVE_DOWNLOAD: {
|
||||||
|
Window()->PostMessage(SAVE_SETTINGS);
|
||||||
|
RemoveSelf();
|
||||||
|
delete this;
|
||||||
|
// TOAST!
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case B_DOWNLOAD_REMOVED:
|
||||||
|
// TODO: This is a bit asymetric. The removed notification
|
||||||
|
// arrives here, but it would be nicer if it arrived
|
||||||
|
// at the window...
|
||||||
|
Window()->PostMessage(message);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
BGridView::MessageReceived(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case CANCEL_DOWNLOAD:
|
BWebDownload* Download() const
|
||||||
m_download->Cancel();
|
{
|
||||||
downloadCanceled();
|
return fDownload;
|
||||||
break;
|
}
|
||||||
|
|
||||||
case REMOVE_DOWNLOAD: {
|
const BString& URL() const
|
||||||
Window()->PostMessage(SAVE_SETTINGS);
|
{
|
||||||
RemoveSelf();
|
return fURL;
|
||||||
delete this;
|
}
|
||||||
// TOAST!
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
case B_DOWNLOAD_REMOVED:
|
|
||||||
// TODO: This is a bit asymetric. The removed notification
|
|
||||||
// arrives here, but it would be nicer if it arrived
|
|
||||||
// at the window...
|
|
||||||
Window()->PostMessage(message);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
BGridView::MessageReceived(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BWebDownload* download() const
|
void DownloadFinished()
|
||||||
{
|
{
|
||||||
return m_download;
|
fDownload = NULL;
|
||||||
}
|
fTopButton->SetEnabled(true);
|
||||||
|
fBottomButton->SetLabel("Remove");
|
||||||
|
fBottomButton->SetMessage(new BMessage(REMOVE_DOWNLOAD));
|
||||||
|
fBottomButton->SetEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
const BString& url() const
|
void DownloadCanceled()
|
||||||
{
|
{
|
||||||
return m_url;
|
fDownload = NULL;
|
||||||
}
|
fTopButton->SetLabel("Restart");
|
||||||
|
fTopButton->SetMessage(new BMessage(RESTART_DOWNLOAD));
|
||||||
void downloadFinished()
|
fTopButton->SetEnabled(true);
|
||||||
{
|
fBottomButton->SetLabel("Remove");
|
||||||
m_download = NULL;
|
fBottomButton->SetMessage(new BMessage(REMOVE_DOWNLOAD));
|
||||||
m_button1->SetEnabled(true);
|
fBottomButton->SetEnabled(true);
|
||||||
m_button2->SetLabel("Remove");
|
}
|
||||||
m_button2->SetMessage(new BMessage(REMOVE_DOWNLOAD));
|
|
||||||
m_button2->SetEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
void downloadCanceled()
|
|
||||||
{
|
|
||||||
m_download = NULL;
|
|
||||||
m_button1->SetLabel("Restart");
|
|
||||||
m_button1->SetMessage(new BMessage(RESTART_DOWNLOAD));
|
|
||||||
m_button1->SetEnabled(true);
|
|
||||||
m_button2->SetLabel("Remove");
|
|
||||||
m_button2->SetMessage(new BMessage(REMOVE_DOWNLOAD));
|
|
||||||
m_button2->SetEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IconView* m_iconView;
|
IconView* fIconView;
|
||||||
BStatusBar* m_statusBar;
|
BStatusBar* fStatusBar;
|
||||||
SmallButton* m_button1;
|
SmallButton* fTopButton;
|
||||||
SmallButton* m_button2;
|
SmallButton* fBottomButton;
|
||||||
BWebDownload* m_download;
|
BWebDownload* fDownload;
|
||||||
BString m_url;
|
BString fURL;
|
||||||
BPath m_path;
|
BPath fPath;
|
||||||
off_t m_expectedSize;
|
off_t fExpectedSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class DownloadsContainerView : public BGroupView {
|
class DownloadsContainerView : public BGroupView {
|
||||||
public:
|
public:
|
||||||
DownloadsContainerView()
|
DownloadsContainerView()
|
||||||
: BGroupView(B_VERTICAL)
|
:
|
||||||
|
BGroupView(B_VERTICAL)
|
||||||
{
|
{
|
||||||
SetViewColor(245, 245, 245);
|
SetViewColor(245, 245, 245);
|
||||||
AddChild(BSpaceLayoutItem::CreateGlue());
|
AddChild(BSpaceLayoutItem::CreateGlue());
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual BSize MinSize()
|
virtual BSize MinSize()
|
||||||
{
|
{
|
||||||
BSize minSize = BGroupView::MinSize();
|
BSize minSize = BGroupView::MinSize();
|
||||||
return BSize(minSize.width, 80);
|
return BSize(minSize.width, 80);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void DoLayout()
|
virtual void DoLayout()
|
||||||
@@ -349,211 +361,245 @@ protected:
|
|||||||
float max = minSize.height - height;
|
float max = minSize.height - height;
|
||||||
scrollBar->SetRange(0, max);
|
scrollBar->SetRange(0, max);
|
||||||
if (minSize.height > 0)
|
if (minSize.height > 0)
|
||||||
scrollBar->SetProportion(height / minSize.height);
|
scrollBar->SetProportion(height / minSize.height);
|
||||||
else
|
else
|
||||||
scrollBar->SetProportion(1);
|
scrollBar->SetProportion(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class DownloadContainerScrollView : public BScrollView {
|
class DownloadContainerScrollView : public BScrollView {
|
||||||
public:
|
public:
|
||||||
DownloadContainerScrollView(BView* target)
|
DownloadContainerScrollView(BView* target)
|
||||||
: BScrollView("Downloads scroll view", target, 0, false, true, B_NO_BORDER)
|
:
|
||||||
{
|
BScrollView("Downloads scroll view", target, 0, false, true,
|
||||||
}
|
B_NO_BORDER)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void DoLayout()
|
virtual void DoLayout()
|
||||||
{
|
{
|
||||||
BScrollView::DoLayout();
|
BScrollView::DoLayout();
|
||||||
// Tweak scroll bar layout to hide part of the frame for better looks.
|
// Tweak scroll bar layout to hide part of the frame for better looks.
|
||||||
BScrollBar* scrollBar = ScrollBar(B_VERTICAL);
|
BScrollBar* scrollBar = ScrollBar(B_VERTICAL);
|
||||||
scrollBar->MoveBy(1, -1);
|
scrollBar->MoveBy(1, -1);
|
||||||
scrollBar->ResizeBy(0, 2);
|
scrollBar->ResizeBy(0, 2);
|
||||||
Target()->ResizeBy(1, 0);
|
Target()->ResizeBy(1, 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
DownloadWindow::DownloadWindow(BRect frame, bool visible)
|
DownloadWindow::DownloadWindow(BRect frame, bool visible)
|
||||||
: BWindow(frame, "Downloads",
|
: BWindow(frame, "Downloads",
|
||||||
B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
|
B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
|
||||||
B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
|
B_AUTO_UPDATE_SIZE_LIMITS | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
|
||||||
{
|
{
|
||||||
SetLayout(new BGroupLayout(B_VERTICAL));
|
SetLayout(new BGroupLayout(B_VERTICAL));
|
||||||
|
|
||||||
DownloadsContainerView* downloadsGroupView = new DownloadsContainerView();
|
DownloadsContainerView* downloadsGroupView = new DownloadsContainerView();
|
||||||
m_downloadViewsLayout = downloadsGroupView->GroupLayout();
|
fDownloadViewsLayout = downloadsGroupView->GroupLayout();
|
||||||
|
|
||||||
BMenuBar* menuBar = new BMenuBar("Menu bar");
|
BMenuBar* menuBar = new BMenuBar("Menu bar");
|
||||||
BMenu* menu = new BMenu("Window");
|
BMenu* menu = new BMenu("Window");
|
||||||
menu->AddItem(new BMenuItem("Hide", new BMessage(B_QUIT_REQUESTED), 'H'));
|
menu->AddItem(new BMenuItem("Hide", new BMessage(B_QUIT_REQUESTED), 'H'));
|
||||||
menuBar->AddItem(menu);
|
menuBar->AddItem(menu);
|
||||||
|
|
||||||
BScrollView* scrollView = new DownloadContainerScrollView(downloadsGroupView);
|
BScrollView* scrollView = new DownloadContainerScrollView(
|
||||||
|
downloadsGroupView);
|
||||||
|
|
||||||
m_removeFinishedButton = new BButton("Remove finished",
|
fRemoveFinishedButton = new BButton("Remove finished",
|
||||||
new BMessage(REMOVE_FINISHED_DOWNLOADS));
|
new BMessage(REMOVE_FINISHED_DOWNLOADS));
|
||||||
m_removeFinishedButton->SetEnabled(false);
|
fRemoveFinishedButton->SetEnabled(false);
|
||||||
|
|
||||||
AddChild(BGroupLayoutBuilder(B_VERTICAL)
|
AddChild(BGroupLayoutBuilder(B_VERTICAL)
|
||||||
.Add(menuBar)
|
.Add(menuBar)
|
||||||
.Add(scrollView)
|
.Add(scrollView)
|
||||||
.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
|
.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
|
||||||
.Add(BGroupLayoutBuilder(B_HORIZONTAL)
|
.Add(BGroupLayoutBuilder(B_HORIZONTAL)
|
||||||
.AddGlue()
|
.AddGlue()
|
||||||
.Add(m_removeFinishedButton)
|
.Add(fRemoveFinishedButton)
|
||||||
.SetInsets(5, 5, 5, 5)
|
.SetInsets(5, 5, 5, 5)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
loadSettings();
|
_LoadSettings();
|
||||||
// Small trick to get the correct enabled status of the Remove finished button
|
// Small trick to get the correct enabled status of the Remove finished button
|
||||||
downloadFinished(NULL);
|
_DownloadFinished(NULL);
|
||||||
|
|
||||||
if (!visible)
|
if (!visible)
|
||||||
Hide();
|
Hide();
|
||||||
Show();
|
Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DownloadWindow::~DownloadWindow()
|
DownloadWindow::~DownloadWindow()
|
||||||
{
|
{
|
||||||
// Only necessary to save the current progress of unfinished downloads:
|
// Only necessary to save the current progress of unfinished downloads:
|
||||||
saveSettings();
|
_SaveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWindow::MessageReceived(BMessage* message)
|
|
||||||
|
void
|
||||||
|
DownloadWindow::MessageReceived(BMessage* message)
|
||||||
{
|
{
|
||||||
switch (message->what) {
|
switch (message->what) {
|
||||||
case B_DOWNLOAD_ADDED: {
|
case B_DOWNLOAD_ADDED: {
|
||||||
BWebDownload* download;
|
BWebDownload* download;
|
||||||
if (message->FindPointer("download", reinterpret_cast<void**>(&download)) == B_OK)
|
if (message->FindPointer("download", reinterpret_cast<void**>(
|
||||||
downloadStarted(download);
|
&download)) == B_OK) {
|
||||||
break;
|
_DownloadStarted(download);
|
||||||
}
|
}
|
||||||
case B_DOWNLOAD_REMOVED: {
|
break;
|
||||||
BWebDownload* download;
|
}
|
||||||
if (message->FindPointer("download", reinterpret_cast<void**>(&download)) == B_OK)
|
case B_DOWNLOAD_REMOVED: {
|
||||||
downloadFinished(download);
|
BWebDownload* download;
|
||||||
break;
|
if (message->FindPointer("download", reinterpret_cast<void**>(
|
||||||
}
|
&download)) == B_OK) {
|
||||||
case REMOVE_FINISHED_DOWNLOADS:
|
_DownloadFinished(download);
|
||||||
removeFinishedDownloads();
|
}
|
||||||
break;
|
break;
|
||||||
case SAVE_SETTINGS:
|
}
|
||||||
saveSettings();
|
case REMOVE_FINISHED_DOWNLOADS:
|
||||||
break;
|
_RemoveFinishedDownloads();
|
||||||
default:
|
break;
|
||||||
BWindow::MessageReceived(message);
|
case SAVE_SETTINGS:
|
||||||
break;
|
_SaveSettings();
|
||||||
}
|
break;
|
||||||
|
default:
|
||||||
|
BWindow::MessageReceived(message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DownloadWindow::QuitRequested()
|
|
||||||
|
bool
|
||||||
|
DownloadWindow::QuitRequested()
|
||||||
{
|
{
|
||||||
if (!IsHidden())
|
if (!IsHidden())
|
||||||
Hide();
|
Hide();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWindow::downloadStarted(BWebDownload* download)
|
|
||||||
|
void
|
||||||
|
DownloadWindow::_DownloadStarted(BWebDownload* download)
|
||||||
{
|
{
|
||||||
int32 finishedCount = 0;
|
int32 finishedCount = 0;
|
||||||
int32 index = 0;
|
int32 index = 0;
|
||||||
for (int32 i = m_downloadViewsLayout->CountItems() - 1;
|
for (int32 i = fDownloadViewsLayout->CountItems() - 1;
|
||||||
BLayoutItem* item = m_downloadViewsLayout->ItemAt(i); i--) {
|
BLayoutItem* item = fDownloadViewsLayout->ItemAt(i); i--) {
|
||||||
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(item->View());
|
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(
|
||||||
if (!view)
|
item->View());
|
||||||
continue;
|
if (!view)
|
||||||
if (view->url() == download->URL()) {
|
continue;
|
||||||
index = i;
|
if (view->URL() == download->URL()) {
|
||||||
view->RemoveSelf();
|
index = i;
|
||||||
delete view;
|
view->RemoveSelf();
|
||||||
} else if (!view->download())
|
delete view;
|
||||||
finishedCount++;
|
} else if (!view->Download())
|
||||||
|
finishedCount++;
|
||||||
}
|
}
|
||||||
m_removeFinishedButton->SetEnabled(finishedCount > 0);
|
fRemoveFinishedButton->SetEnabled(finishedCount > 0);
|
||||||
DownloadProgressView* view = new DownloadProgressView(download);
|
DownloadProgressView* view = new DownloadProgressView(download);
|
||||||
if (!view->init())
|
if (!view->Init())
|
||||||
return;
|
return;
|
||||||
m_downloadViewsLayout->AddView(index, view);
|
fDownloadViewsLayout->AddView(index, view);
|
||||||
saveSettings();
|
_SaveSettings();
|
||||||
|
|
||||||
SetWorkspaces(B_CURRENT_WORKSPACE);
|
SetWorkspaces(B_CURRENT_WORKSPACE);
|
||||||
if (IsHidden())
|
if (IsHidden())
|
||||||
Show();
|
Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWindow::downloadFinished(BWebDownload* download)
|
|
||||||
|
void
|
||||||
|
DownloadWindow::_DownloadFinished(BWebDownload* download)
|
||||||
{
|
{
|
||||||
int32 finishedCount = 0;
|
int32 finishedCount = 0;
|
||||||
for (int32 i = 0; BLayoutItem* item = m_downloadViewsLayout->ItemAt(i); i++) {
|
for (int32 i = 0;
|
||||||
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(item->View());
|
BLayoutItem* item = fDownloadViewsLayout->ItemAt(i); i++) {
|
||||||
if (!view)
|
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(
|
||||||
continue;
|
item->View());
|
||||||
if (view->download() == download) {
|
if (!view)
|
||||||
view->downloadFinished();
|
continue;
|
||||||
finishedCount++;
|
if (view->Download() == download) {
|
||||||
} else if (!view->download())
|
view->DownloadFinished();
|
||||||
finishedCount++;
|
finishedCount++;
|
||||||
|
} else if (!view->Download())
|
||||||
|
finishedCount++;
|
||||||
}
|
}
|
||||||
m_removeFinishedButton->SetEnabled(finishedCount > 0);
|
fRemoveFinishedButton->SetEnabled(finishedCount > 0);
|
||||||
if (download)
|
if (download)
|
||||||
saveSettings();
|
_SaveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWindow::removeFinishedDownloads()
|
|
||||||
|
void
|
||||||
|
DownloadWindow::_RemoveFinishedDownloads()
|
||||||
{
|
{
|
||||||
for (int32 i = m_downloadViewsLayout->CountItems() - 1;
|
for (int32 i = fDownloadViewsLayout->CountItems() - 1;
|
||||||
BLayoutItem* item = m_downloadViewsLayout->ItemAt(i); i--) {
|
BLayoutItem* item = fDownloadViewsLayout->ItemAt(i); i--) {
|
||||||
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(item->View());
|
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(
|
||||||
if (!view)
|
item->View());
|
||||||
continue;
|
if (!view)
|
||||||
if (!view->download()) {
|
continue;
|
||||||
view->RemoveSelf();
|
if (!view->Download()) {
|
||||||
delete view;
|
view->RemoveSelf();
|
||||||
}
|
delete view;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
m_removeFinishedButton->SetEnabled(false);
|
fRemoveFinishedButton->SetEnabled(false);
|
||||||
saveSettings();
|
_SaveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWindow::saveSettings()
|
|
||||||
|
void
|
||||||
|
DownloadWindow::_SaveSettings()
|
||||||
{
|
{
|
||||||
BFile file;
|
BFile file;
|
||||||
if (!openSettingsFile(file, B_ERASE_FILE | B_CREATE_FILE | B_WRITE_ONLY))
|
if (!_OpenSettingsFile(file, B_ERASE_FILE | B_CREATE_FILE | B_WRITE_ONLY))
|
||||||
return;
|
return;
|
||||||
BMessage message;
|
BMessage message;
|
||||||
for (int32 i = m_downloadViewsLayout->CountItems() - 1;
|
for (int32 i = fDownloadViewsLayout->CountItems() - 1;
|
||||||
BLayoutItem* item = m_downloadViewsLayout->ItemAt(i); i--) {
|
BLayoutItem* item = fDownloadViewsLayout->ItemAt(i); i--) {
|
||||||
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(item->View());
|
DownloadProgressView* view = dynamic_cast<DownloadProgressView*>(
|
||||||
if (!view)
|
item->View());
|
||||||
continue;
|
if (!view)
|
||||||
BMessage downloadArchive;
|
continue;
|
||||||
if (view->saveSettings(&downloadArchive) == B_OK)
|
BMessage downloadArchive;
|
||||||
message.AddMessage("download", &downloadArchive);
|
if (view->SaveSettings(&downloadArchive) == B_OK)
|
||||||
|
message.AddMessage("download", &downloadArchive);
|
||||||
}
|
}
|
||||||
message.Flatten(&file);
|
message.Flatten(&file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DownloadWindow::loadSettings()
|
|
||||||
|
void
|
||||||
|
DownloadWindow::_LoadSettings()
|
||||||
{
|
{
|
||||||
BFile file;
|
BFile file;
|
||||||
if (!openSettingsFile(file, B_READ_ONLY))
|
if (!_OpenSettingsFile(file, B_READ_ONLY))
|
||||||
return;
|
return;
|
||||||
BMessage message;
|
BMessage message;
|
||||||
if (message.Unflatten(&file) != B_OK)
|
if (message.Unflatten(&file) != B_OK)
|
||||||
return;
|
return;
|
||||||
BMessage downloadArchive;
|
BMessage downloadArchive;
|
||||||
for (int32 i = 0; message.FindMessage("download", i, &downloadArchive) == B_OK; i++) {
|
for (int32 i = 0;
|
||||||
DownloadProgressView* view = new DownloadProgressView(&downloadArchive);
|
message.FindMessage("download", i, &downloadArchive) == B_OK;
|
||||||
if (!view->init(&downloadArchive))
|
i++) {
|
||||||
|
DownloadProgressView* view = new DownloadProgressView(
|
||||||
|
&downloadArchive);
|
||||||
|
if (!view->Init(&downloadArchive))
|
||||||
continue;
|
continue;
|
||||||
m_downloadViewsLayout->AddView(0, view);
|
fDownloadViewsLayout->AddView(0, view);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DownloadWindow::openSettingsFile(BFile& file, uint32 mode)
|
|
||||||
|
bool
|
||||||
|
DownloadWindow::_OpenSettingsFile(BFile& file, uint32 mode)
|
||||||
{
|
{
|
||||||
BPath path;
|
BPath path;
|
||||||
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
|
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
|
||||||
|
|||||||
@@ -24,9 +24,9 @@
|
|||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
#ifndef DOWNLOAD_WINDOW_H
|
||||||
|
#define DOWNLOAD_WINDOW_H
|
||||||
|
|
||||||
#ifndef DownloadWindow_h
|
|
||||||
#define DownloadWindow_h
|
|
||||||
|
|
||||||
#include <Window.h>
|
#include <Window.h>
|
||||||
|
|
||||||
@@ -35,26 +35,27 @@ class BFile;
|
|||||||
class BGroupLayout;
|
class BGroupLayout;
|
||||||
class BWebDownload;
|
class BWebDownload;
|
||||||
|
|
||||||
|
|
||||||
class DownloadWindow : public BWindow {
|
class DownloadWindow : public BWindow {
|
||||||
public:
|
public:
|
||||||
DownloadWindow(BRect frame, bool visible);
|
DownloadWindow(BRect frame, bool visible);
|
||||||
virtual ~DownloadWindow();
|
virtual ~DownloadWindow();
|
||||||
|
|
||||||
virtual void MessageReceived(BMessage*);
|
virtual void MessageReceived(BMessage* message);
|
||||||
virtual bool QuitRequested();
|
virtual bool QuitRequested();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void downloadStarted(BWebDownload* download);
|
void _DownloadStarted(BWebDownload* download);
|
||||||
void downloadFinished(BWebDownload* download);
|
void _DownloadFinished(BWebDownload* download);
|
||||||
void removeFinishedDownloads();
|
void _RemoveFinishedDownloads();
|
||||||
void saveSettings();
|
void _SaveSettings();
|
||||||
void loadSettings();
|
void _LoadSettings();
|
||||||
bool openSettingsFile(BFile& file, uint32 mode);
|
bool _OpenSettingsFile(BFile& file, uint32 mode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BGroupLayout* m_downloadViewsLayout;
|
BGroupLayout* fDownloadViewsLayout;
|
||||||
BButton* m_removeFinishedButton;
|
BButton* fRemoveFinishedButton;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DownloadWindow_h
|
|
||||||
|
|
||||||
|
#endif // DOWNLOAD_WINDOW_H
|
||||||
|
|||||||
Reference in New Issue
Block a user