| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include "videolistentry.h" |
| 14 | |
| 15 | using namespace smooth::Net; |
| 16 | |
| 17 | BoCA::VideoListEntry::VideoListEntry(Video *iVideo) : ListEntry(NIL) |
| 18 | { |
| 19 | SetWidth(80); |
| 20 | |
| 21 | video = iVideo; |
| 22 | image = CreateImageFromVideo(); |
| 23 | |
| 24 | image->SetMetrics(Point(3, 3), GetSize() - Size(6, 15)); |
| 25 | |
| 26 | progress = new Progressbar(Point(2, 12), Size(GetWidth() - 4, 10), OR_HORZ, PB_NOTEXT, 0, 1000); |
| 27 | progress->SetOrientation(OR_LOWERLEFT); |
| 28 | |
| 29 | video->downloadProgress.Connect(&Progressbar::SetValue, progress); |
| 30 | video->finishDownload.Connect(&VideoListEntry::OnFinishDownload, this); |
| 31 | |
| 32 | Add(progress); |
| 33 | Add(image); |
| 34 | |
| 35 | downloadFinished = False; |
| 36 | |
| 37 | contextMenu = NIL; |
| 38 | |
| 39 | SetTooltipText(video->GetVideoTitle()); |
| 40 | |
| 41 | onChangeSize.Connect(&VideoListEntry::OnChangeSize, this); |
| 42 | |
| 43 | getContextMenu.Connect(&VideoListEntry::GetContextMenu, this); |
| 44 | } |
| 45 | |
| 46 | BoCA::VideoListEntry::~VideoListEntry() |
| 47 | { |
| 48 | if (contextMenu != NIL) DeleteObject(contextMenu); |
| 49 | |
| 50 | video->downloadProgress.Disconnect(&Progressbar::SetValue, progress); |
| 51 | video->finishDownload.Disconnect(&VideoListEntry::OnFinishDownload, this); |
| 52 | |
| 53 | DeleteObject(image); |
| 54 | DeleteObject(progress); |
| 55 | |
| 56 | if (IsRegistered() && container != NIL) container->Remove(this); |
| 57 | } |
| 58 | |
| 59 | Int BoCA::VideoListEntry::Paint(Int message) |
| 60 | { |
| 61 | if (!IsRegistered()) return Error(); |
| 62 | if (!IsVisible()) return Success(); |
| 63 | |
| 64 | Surface *surface = GetDrawSurface(); |
| 65 | Point realPos = GetRealPosition(); |
| 66 | Size realSize = GetRealSize(); |
| 67 | |
| 68 | switch (message) |
| 69 | { |
| 70 | case SP_PAINT: |
| 71 | surface->StartPaint(GetVisibleArea()); |
| 72 | |
| 73 | surface->Box(Rect(realPos, realSize), Setup::ClientColor, Rect::Filled); |
| 74 | |
| 75 | image->Paint(SP_PAINT); |
| 76 | |
| 77 | if (progress->IsVisible()) |
| 78 | { |
| 79 | progress->Paint(SP_PAINT); |
| 80 | |
| 81 | surface->Box(Rect(progress->GetRealPosition(), progress->GetRealSize()), Setup::ClientColor, Rect::Outlined); |
| 82 | } |
| 83 | |
| 84 | surface->EndPaint(); |
| 85 | |
| 86 | break; |
| 87 | case SP_MOUSEIN: |
| 88 | surface->StartPaint(GetVisibleArea()); |
| 89 | |
| 90 | surface->Box(Rect(realPos, Size(realSize.cx, 2)), Setup::GradientStartColor, Rect::Filled); |
| 91 | surface->Box(Rect(realPos, Size(2, realSize.cy)), Setup::GradientStartColor, Rect::Filled); |
| 92 | surface->Box(Rect(realPos + Point(realSize.cx - 2, 0), Size(2, realSize.cy)), Setup::GradientStartColor, Rect::Filled); |
| 93 | surface->Box(Rect(realPos + Point(0, realSize.cy - 2), Size(realSize.cx, 2)), Setup::GradientStartColor, Rect::Filled); |
| 94 | |
| 95 | surface->EndPaint(); |
| 96 | |
| 97 | break; |
| 98 | case SP_MOUSEOUT: |
| 99 | surface->StartPaint(GetVisibleArea()); |
| 100 | |
| 101 | surface->Box(Rect(realPos, Size(realSize.cx, 2)), Setup::ClientColor, Rect::Filled); |
| 102 | surface->Box(Rect(realPos, Size(2, realSize.cy)), Setup::ClientColor, Rect::Filled); |
| 103 | surface->Box(Rect(realPos + Point(realSize.cx - 2, 0), Size(2, realSize.cy)), Setup::ClientColor, Rect::Filled); |
| 104 | surface->Box(Rect(realPos + Point(0, realSize.cy - 2), Size(realSize.cx, 2)), Setup::ClientColor, Rect::Filled); |
| 105 | |
| 106 | surface->EndPaint(); |
| 107 | |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | return Success(); |
| 112 | } |
| 113 | |
| 114 | Image *BoCA::VideoListEntry::CreateImageFromVideo() |
| 115 | { |
| 116 | if (video == NIL) return NIL; |
| 117 | |
| 118 | video->QueryMetadata(); |
| 119 | |
| 120 | if (video->GetVideoThumbnail().mime == "image/jpeg") return new Image(video->GetVideoThumbnail().GetBitmap()); |
| 121 | |
| 122 | return new Image(Bitmap()); |
| 123 | } |
| 124 | |
| 125 | Void BoCA::VideoListEntry::OnFinishDownload() |
| 126 | { |
| 127 | downloadFinished = True; |
| 128 | |
| 129 | progress->Hide(); |
| 130 | image->SetSize(GetSize() - Size(6, 6)); |
| 131 | } |
| 132 | |
| 133 | Void BoCA::VideoListEntry::OnChangeSize(const Size &newSize) |
| 134 | { |
| 135 | if (progress->GetValue() < 1000) image->SetSize(newSize - Size(6, 15)); |
| 136 | else image->SetSize(newSize - Size(6, 6)); |
| 137 | } |
| 138 | |
| 139 | PopupMenu *BoCA::VideoListEntry::GetContextMenu() |
| 140 | { |
| 141 | if (downloadFinished) return NIL; |
| 142 | |
| 143 | I18n *i18n = I18n::Get(); |
| 144 | |
| 145 | i18n->SetContext("Extensions::Video Downloader"); |
| 146 | |
| 147 | if (contextMenu == NIL) contextMenu = new PopupMenu(); |
| 148 | |
| 149 | contextMenu->RemoveAllEntries(); |
| 150 | |
| 151 | MenuEntry *entryCancel = contextMenu->AddEntry(i18n->TranslateString("Cancel download")); |
| 152 | |
| 153 | entryCancel->onAction.Connect(&Video::CancelDownload, video); |
| 154 | |
| 155 | return contextMenu; |
| 156 | } |