| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2020 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 "layer.h" |
| 14 | #include "config.h" |
| 15 | #include "video.h" |
| 16 | #include "videosite.h" |
| 17 | #include "videolist.h" |
| 18 | #include "videolistentry.h" |
| 19 | |
| 20 | using namespace smooth::GUI::Dialogs; |
| 21 | using namespace smooth::Net; |
| 22 | |
| 23 | BoCA::LayerYouTube::LayerYouTube() : Layer("Video") |
| 24 | { |
| 25 | Config *config = Config::Get(); |
| 26 | |
| 27 | missingDecoders = False; |
| 28 | |
| 29 | tracks.EnableLocking(); |
| 30 | |
| 31 | /* Load video sites. |
| 32 | */ |
| 33 | LoadVideoSites(); |
| 34 | |
| 35 | text_url = new Text(NIL, Point(7, 10)); |
| 36 | |
| 37 | Add(text_url); |
| 38 | |
| 39 | edit_url = new EditBox(Point(15, 7), Size(100, 0)); |
| 40 | edit_url->onInput.Connect(&LayerYouTube::OnEditDownloadURL, this); |
| 41 | |
| 42 | button_add_url = new Button(String(), Point(88, 6), Size()); |
| 43 | button_add_url->SetOrientation(OR_UPPERRIGHT); |
| 44 | button_add_url->Deactivate(); |
| 45 | button_add_url->onAction.Connect(&LayerYouTube::OnDownloadTrack, this); |
| 46 | |
| 47 | Add(edit_url); |
| 48 | Add(button_add_url); |
| 49 | |
| 50 | check_auto_download = new CheckBox(NIL, Point(8, 34), Size(300, 0), (Bool *) &config->GetPersistentIntValue(ConfigureYouTube::ConfigID, "AutoDownload", False)); |
| 51 | check_auto_download->SetOrientation(OR_UPPERRIGHT); |
| 52 | |
| 53 | check_keep_files = new CheckBox(NIL, Point(8, 34), Size(300, 0), (Bool *) &config->GetPersistentIntValue(ConfigureYouTube::ConfigID, "SaveVideoFiles", False)); |
| 54 | check_keep_files->SetOrientation(OR_UPPERRIGHT); |
| 55 | |
| 56 | if (missingDecoders) check_keep_files->Deactivate(); |
| 57 | |
| 58 | Add(check_auto_download); |
| 59 | Add(check_keep_files); |
| 60 | |
| 61 | text_downloads = new Text(NIL, Point(7, 57)); |
| 62 | |
| 63 | list_downloads = new VideoList(Point(7, 76), Size(100, 75)); |
| 64 | |
| 65 | Add(text_downloads); |
| 66 | Add(list_downloads); |
| 67 | |
| 68 | text_tracks = new Text(NIL, Point(7, 157)); |
| 69 | |
| 70 | list_tracks = new ListBox(Point(7, 176), Size(100, 150)); |
| 71 | list_tracks->onSelectEntry.Connect(&LayerYouTube::OnSelectTrack, this); |
| 72 | |
| 73 | list_tracks->EnableLocking(); |
| 74 | |
| 75 | Add(text_tracks); |
| 76 | Add(list_tracks); |
| 77 | |
| 78 | area_cover = new ActiveArea(Setup::BackgroundColor, Point(7, 112), Size(114, 73)); |
| 79 | area_cover->SetOrientation(OR_LOWERLEFT); |
| 80 | |
| 81 | image_cover = new Image(NIL, Point(8, 111), Size(112, 71)); |
| 82 | image_cover->SetOrientation(OR_LOWERLEFT); |
| 83 | |
| 84 | Add(area_cover); |
| 85 | Add(image_cover); |
| 86 | |
| 87 | text_source = new Text(NIL, Point(129, 109)); |
| 88 | text_source->SetOrientation(OR_LOWERLEFT); |
| 89 | |
| 90 | link_source = new Hyperlink(String(), NIL, Point(129, 109)); |
| 91 | link_source->SetOrientation(OR_LOWERLEFT); |
| 92 | |
| 93 | text_title = new Text(NIL, Point(129, 82)); |
| 94 | text_title->SetOrientation(OR_LOWERLEFT); |
| 95 | |
| 96 | edit_title = new EditBox(Point(129, 85), Size(0, 0)); |
| 97 | edit_title->SetOrientation(OR_LOWERLEFT); |
| 98 | edit_title->onInput.Connect(&LayerYouTube::OnEditMetadata, this); |
| 99 | |
| 100 | text_description = new Text(NIL, Point(129, 55)); |
| 101 | text_description->SetOrientation(OR_LOWERLEFT); |
| 102 | |
| 103 | edit_description = new MultiEdit(Point(129, 58), Size(0, 50)); |
| 104 | edit_description->SetOrientation(OR_LOWERLEFT); |
| 105 | edit_description->onInput.Connect(&LayerYouTube::OnEditMetadata, this); |
| 106 | |
| 107 | Add(text_source); |
| 108 | Add(link_source); |
| 109 | |
| 110 | Add(text_title); |
| 111 | Add(edit_title); |
| 112 | |
| 113 | Add(text_description); |
| 114 | Add(edit_description); |
| 115 | |
| 116 | text_site = new Text(NIL, Point(200, 109)); |
| 117 | text_site->SetOrientation(OR_LOWERRIGHT); |
| 118 | |
| 119 | text_site_value = new Text(NIL, Point(200, 109)); |
| 120 | text_site_value->SetOrientation(OR_LOWERRIGHT); |
| 121 | |
| 122 | text_uploader = new Text(NIL, Point(200, 82)); |
| 123 | text_uploader->SetOrientation(OR_LOWERRIGHT); |
| 124 | |
| 125 | text_uploader_value = new Text(NIL, Point(200, 82)); |
| 126 | text_uploader_value->SetOrientation(OR_LOWERRIGHT); |
| 127 | |
| 128 | text_date = new Text(NIL, Point(200, 55)); |
| 129 | text_date->SetOrientation(OR_LOWERRIGHT); |
| 130 | |
| 131 | text_date_value = new Text(NIL, Point(200, 55)); |
| 132 | text_date_value->SetOrientation(OR_LOWERRIGHT); |
| 133 | |
| 134 | Add(text_site); |
| 135 | Add(text_site_value); |
| 136 | |
| 137 | Add(text_uploader); |
| 138 | Add(text_uploader_value); |
| 139 | |
| 140 | Add(text_date); |
| 141 | Add(text_date_value); |
| 142 | |
| 143 | OnSelectNone(); |
| 144 | |
| 145 | /* Connect slots. |
| 146 | */ |
| 147 | onShow.Connect(&LayerYouTube::OnShowLayer, this); |
| 148 | onChangeSize.Connect(&LayerYouTube::OnChangeSize, this); |
| 149 | |
| 150 | Settings::Get()->onChangeLanguageSettings.Connect(&LayerYouTube::OnChangeLanguageSettings, this); |
| 151 | |
| 152 | JobList::Get()->onApplicationModifyTrack.Connect(&LayerYouTube::OnApplicationModifyTrack, this); |
| 153 | JobList::Get()->onApplicationRemoveTrack.Connect(&LayerYouTube::OnApplicationRemoveTrack, this); |
| 154 | JobList::Get()->onApplicationSelectTrack.Connect(&LayerYouTube::OnApplicationSelectTrack, this); |
| 155 | |
| 156 | JobList::Get()->onApplicationRemoveAllTracks.Connect(&LayerYouTube::OnApplicationRemoveAllTracks, this); |
| 157 | |
| 158 | Application::Get()->onQuit.Connect(&LayerYouTube::OnQuit, this); |
| 159 | |
| 160 | /* Create and start clipboard checking timer. |
| 161 | */ |
| 162 | timer_check_clipboard = new S::System::Timer(); |
| 163 | timer_check_clipboard->onInterval.Connect(&LayerYouTube::OnTimerCheckClipboard, this); |
| 164 | |
| 165 | timer_check_clipboard->Start(500); |
| 166 | } |
| 167 | |
| 168 | BoCA::LayerYouTube::~LayerYouTube() |
| 169 | { |
| 170 | /* Stop clipboard checking timer. |
| 171 | */ |
| 172 | timer_check_clipboard->Stop(); |
| 173 | |
| 174 | DeleteObject(timer_check_clipboard); |
| 175 | |
| 176 | /* Remove all tracks from track list. |
| 177 | */ |
| 178 | OnApplicationRemoveAllTracks(); |
| 179 | |
| 180 | /* Disconnect slots. |
| 181 | */ |
| 182 | Settings::Get()->onChangeLanguageSettings.Disconnect(&LayerYouTube::OnChangeLanguageSettings, this); |
| 183 | |
| 184 | JobList::Get()->onApplicationModifyTrack.Disconnect(&LayerYouTube::OnApplicationModifyTrack, this); |
| 185 | JobList::Get()->onApplicationRemoveTrack.Disconnect(&LayerYouTube::OnApplicationRemoveTrack, this); |
| 186 | JobList::Get()->onApplicationSelectTrack.Disconnect(&LayerYouTube::OnApplicationSelectTrack, this); |
| 187 | |
| 188 | JobList::Get()->onApplicationRemoveAllTracks.Disconnect(&LayerYouTube::OnApplicationRemoveAllTracks, this); |
| 189 | |
| 190 | Application::Get()->onQuit.Disconnect(&LayerYouTube::OnQuit, this); |
| 191 | |
| 192 | /* Delete widgets. |
| 193 | */ |
| 194 | DeleteObject(text_url); |
| 195 | |
| 196 | DeleteObject(edit_url); |
| 197 | DeleteObject(button_add_url); |
| 198 | |
| 199 | DeleteObject(check_auto_download); |
| 200 | |
| 201 | DeleteObject(text_downloads); |
| 202 | DeleteObject(list_downloads); |
| 203 | |
| 204 | DeleteObject(check_keep_files); |
| 205 | |
| 206 | DeleteObject(text_tracks); |
| 207 | DeleteObject(list_tracks); |
| 208 | |
| 209 | DeleteObject(area_cover); |
| 210 | DeleteObject(image_cover); |
| 211 | |
| 212 | DeleteObject(text_site); |
| 213 | DeleteObject(text_site_value); |
| 214 | |
| 215 | DeleteObject(text_source); |
| 216 | DeleteObject(link_source); |
| 217 | |
| 218 | DeleteObject(text_title); |
| 219 | DeleteObject(edit_title); |
| 220 | |
| 221 | DeleteObject(text_description); |
| 222 | DeleteObject(edit_description); |
| 223 | |
| 224 | DeleteObject(text_uploader); |
| 225 | DeleteObject(text_uploader_value); |
| 226 | |
| 227 | DeleteObject(text_date); |
| 228 | DeleteObject(text_date_value); |
| 229 | |
| 230 | /* Free video sites. |
| 231 | */ |
| 232 | FreeVideoSites(); |
| 233 | } |
| 234 | |
| 235 | Void BoCA::LayerYouTube::LoadVideoSites() |
| 236 | { |
| 237 | Directory dir(Utilities::GetBoCADirectory().Append("freac.extension.youtube")); |
| 238 | |
| 239 | if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/freac.extension.youtube"); |
| 240 | |
| 241 | const Array<File> &files = dir.GetFilesByPattern("videosite_*.xml"); |
| 242 | |
| 243 | Config *config = Config::Get(); |
| 244 | AS::Registry &boca = AS::Registry::Get(); |
| 245 | |
| 246 | foreach (const File &file, files) |
| 247 | { |
| 248 | VideoSite *site = new VideoSite(file); |
| 249 | |
| 250 | if (site->IsSane()) |
| 251 | { |
| 252 | sites.Add(site); |
| 253 | |
| 254 | const Array<String> &decoders = site->GetDecoders(); |
| 255 | |
| 256 | foreach (const String &decoder, decoders) |
| 257 | { |
| 258 | if (!boca.ComponentExists(decoder)) missingDecoders = True; |
| 259 | } |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | delete site; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (missingDecoders) config->SetIntValue(ConfigureYouTube::ConfigID, "SaveVideoFiles", True); |
| 268 | |
| 269 | config->SetIntValue(ConfigureYouTube::ConfigID, "DisableSaveOption", missingDecoders); |
| 270 | } |
| 271 | |
| 272 | Void BoCA::LayerYouTube::FreeVideoSites() |
| 273 | { |
| 274 | foreach (VideoSite *site, sites) delete site; |
| 275 | |
| 276 | sites.RemoveAll(); |
| 277 | } |
| 278 | |
| 279 | /* Get VideoSite object that can handle the URL. |
| 280 | * ---- |
| 281 | */ |
| 282 | VideoSite *BoCA::LayerYouTube::GetVideoSiteForURL(const String &URL) |
| 283 | { |
| 284 | if (!(URL.StartsWith("http://") || URL.StartsWith("https://")) || URL.Length() < 11) return NIL; |
| 285 | |
| 286 | VideoSite *videoSite = NIL; |
| 287 | |
| 288 | foreach (VideoSite *site, sites) |
| 289 | { |
| 290 | if (site->CanHandleURL(URL)) { videoSite = site; break; } |
| 291 | } |
| 292 | |
| 293 | return videoSite; |
| 294 | } |
| 295 | |
| 296 | /* Get text from clipboard. |
| 297 | * ---- |
| 298 | */ |
| 299 | String BoCA::LayerYouTube::GetClipboardText() |
| 300 | { |
| 301 | if (container->GetContainerWindow() == NIL) return NIL; |
| 302 | |
| 303 | String clipboardText = Clipboard(container->GetContainerWindow()).GetClipboardText(); |
| 304 | |
| 305 | /* Save clipboard text to compare later. |
| 306 | */ |
| 307 | previousClipboardText = clipboardText; |
| 308 | |
| 309 | return clipboardText; |
| 310 | } |
| 311 | |
| 312 | /* Called when component is displayed. |
| 313 | * ---- |
| 314 | */ |
| 315 | Void BoCA::LayerYouTube::OnShowLayer() |
| 316 | { |
| 317 | static Bool initialized = False; |
| 318 | |
| 319 | if (initialized) return; |
| 320 | if (GetContainerWindow() == NIL) return; |
| 321 | |
| 322 | Config *config = Config::Get(); |
| 323 | |
| 324 | if (missingDecoders && !config->GetIntValue(ConfigureYouTube::ConfigID, "DoNotShowDecoderWarning", False)) |
| 325 | { |
| 326 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 327 | |
| 328 | i18n->SetContext("Extensions::Video Downloader::Errors"); |
| 329 | |
| 330 | Bool doNotShowAgain = False; |
| 331 | MessageDlg messageBox(i18n->TranslateString("Some required video decoders could not be found. Video files\ncannot be added to the joblist for conversion to audio files.\n\nPlease install FFmpeg or avconv to fix this problem!"), i18n->TranslateString("Note"), Message::Buttons::Ok, Message::Icon::Warning, i18n->TranslateString("Do not display this note again"), &doNotShowAgain); |
| 332 | |
| 333 | messageBox.ShowDialog(); |
| 334 | |
| 335 | config->SetIntValue(ConfigureYouTube::ConfigID, "DoNotShowDecoderWarning", doNotShowAgain); |
| 336 | } |
| 337 | |
| 338 | initialized = True; |
| 339 | } |
| 340 | |
| 341 | /* Called when component canvas size changes. |
| 342 | * ---- |
| 343 | */ |
| 344 | Void BoCA::LayerYouTube::OnChangeSize(const Size &nSize) |
| 345 | { |
| 346 | Rect clientRect = Rect(GetPosition(), GetSize()); |
| 347 | Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top); |
| 348 | |
| 349 | edit_url->SetWidth(clientSize.cx - text_url->GetUnscaledTextWidth() - button_add_url->GetWidth() - 30); |
| 350 | |
| 351 | list_downloads->SetWidth(clientSize.cx - 15); |
| 352 | |
| 353 | list_tracks->SetSize(Size(clientSize.cx - 15, clientSize.cy - 296)); |
| 354 | |
| 355 | /* Shorten link text. |
| 356 | */ |
| 357 | Font font; |
| 358 | String text = link_source->GetURL(); |
| 359 | |
| 360 | if (font.GetUnscaledTextSizeX(text) >= clientSize.cx - Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) - 344) |
| 361 | { |
| 362 | for (Int i = text.Length() - 1; i >= 13; i--) |
| 363 | { |
| 364 | if (font.GetUnscaledTextSizeX(text.Head(i).Append("...")) >= clientSize.cx - Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) - 344) continue; |
| 365 | |
| 366 | link_source->SetText(text.Head(i).Append("...")); |
| 367 | |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | else |
| 372 | { |
| 373 | link_source->SetText(text); |
| 374 | } |
| 375 | |
| 376 | /* Video title edit box. |
| 377 | */ |
| 378 | edit_title->SetWidth(clientSize.cx - Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) - 344); |
| 379 | edit_description->SetWidth(clientSize.cx - Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) - 344); |
| 380 | } |
| 381 | |
| 382 | /* Called when application language is changed. |
| 383 | * ---- |
| 384 | */ |
| 385 | Void BoCA::LayerYouTube::OnChangeLanguageSettings() |
| 386 | { |
| 387 | I18n *i18n = I18n::Get(); |
| 388 | |
| 389 | i18n->SetContext("Extensions::Video Downloader"); |
| 390 | |
| 391 | SetText(i18n->TranslateString("Video")); |
| 392 | |
| 393 | /* Hide all affected widgets prior to changing |
| 394 | * labels to avoid flickering. |
| 395 | */ |
| 396 | Bool prevVisible = IsVisible(); |
| 397 | |
| 398 | if (prevVisible) Hide(); |
| 399 | |
| 400 | /* Set texts and positions. |
| 401 | */ |
| 402 | text_url->SetText(i18n->AddColon(i18n->TranslateString("Enter video URL here"))); |
| 403 | |
| 404 | edit_url->SetX(text_url->GetUnscaledTextWidth() + 15); |
| 405 | |
| 406 | button_add_url->SetText(i18n->TranslateString("Download")); |
| 407 | button_add_url->SetWidth(Math::Max(80, button_add_url->GetUnscaledTextWidth() + 13)); |
| 408 | button_add_url->SetX(button_add_url->GetWidth() + 8); |
| 409 | |
| 410 | check_auto_download->SetText(i18n->TranslateString("Automatically download URLs copied to clipboard")); |
| 411 | check_auto_download->SetWidth(check_auto_download->GetUnscaledTextWidth() + 20); |
| 412 | |
| 413 | check_keep_files->SetText(i18n->TranslateString("Save downloaded video files")); |
| 414 | check_keep_files->SetWidth(check_keep_files->GetUnscaledTextWidth() + 20); |
| 415 | |
| 416 | check_auto_download->SetX(check_auto_download->GetWidth() + check_keep_files->GetWidth() + 16); |
| 417 | check_keep_files->SetX(check_keep_files->GetWidth() + 8); |
| 418 | |
| 419 | text_downloads->SetText(i18n->AddColon(i18n->TranslateString("Video downloads"))); |
| 420 | text_tracks->SetText(i18n->AddColon(i18n->TranslateString("Downloaded tracks"))); |
| 421 | |
| 422 | list_tracks->RemoveAllTabs(); |
| 423 | |
| 424 | Int numAlign = i18n->IsActiveLanguageRightToLeft() ? OR_LEFT : OR_RIGHT; |
| 425 | |
| 426 | list_tracks->AddTab(i18n->TranslateString("Uploader"), 150); |
| 427 | list_tracks->AddTab(i18n->TranslateString("Title")); |
| 428 | list_tracks->AddTab(i18n->TranslateString("Length"), 80, numAlign); |
| 429 | list_tracks->AddTab(i18n->TranslateString("Size"), 80, numAlign); |
| 430 | |
| 431 | text_source->SetText(i18n->AddColon(i18n->TranslateString("Video URL"))); |
| 432 | text_title->SetText(i18n->AddColon(i18n->TranslateString("Video title"))); |
| 433 | text_description->SetText(i18n->AddColon(i18n->TranslateString("Video description"))); |
| 434 | |
| 435 | link_source->SetX(129 + Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) + 8); |
| 436 | edit_title->SetX(129 + Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) + 8); |
| 437 | edit_description->SetX(129 + Math::Max(text_source->GetUnscaledTextWidth(), Math::Max(text_title->GetUnscaledTextWidth(), text_description->GetUnscaledTextWidth())) + 8); |
| 438 | |
| 439 | text_site->SetText(i18n->AddColon(i18n->TranslateString("Site name"))); |
| 440 | text_uploader->SetText(i18n->AddColon(i18n->TranslateString("Uploaded by"))); |
| 441 | text_date->SetText(i18n->AddColon(i18n->TranslateString("Uploaded on"))); |
| 442 | |
| 443 | text_site_value->SetX(200 - Math::Max(text_site->GetUnscaledTextWidth(), Math::Max(text_uploader->GetUnscaledTextWidth(), text_date->GetUnscaledTextWidth())) - 8); |
| 444 | text_uploader_value->SetX(200 - Math::Max(text_site->GetUnscaledTextWidth(), Math::Max(text_uploader->GetUnscaledTextWidth(), text_date->GetUnscaledTextWidth())) - 8); |
| 445 | text_date_value->SetX(200 - Math::Max(text_site->GetUnscaledTextWidth(), Math::Max(text_uploader->GetUnscaledTextWidth(), text_date->GetUnscaledTextWidth())) - 8); |
| 446 | |
| 447 | /* OnChangeSize will correct sizes of any other widgets. |
| 448 | */ |
| 449 | OnChangeSize(GetSize()); |
| 450 | |
| 451 | /* Show all widgets again. |
| 452 | */ |
| 453 | if (prevVisible) Show(); |
| 454 | } |
| 455 | |
| 456 | /* Called when clipboard contents need to be checked for an update. |
| 457 | * ---- |
| 458 | * Initiates download of URL in clipboard. |
| 459 | */ |
| 460 | Void BoCA::LayerYouTube::OnTimerCheckClipboard() |
| 461 | { |
| 462 | if (container->GetContainerWindow() == NIL) return; |
| 463 | |
| 464 | /* Initialize clipboard text when running the first time. |
| 465 | */ |
| 466 | static Bool initialized = False; |
| 467 | |
| 468 | if (!initialized) { GetClipboardText(); initialized = True; } |
| 469 | |
| 470 | Config *config = Config::Get(); |
| 471 | |
| 472 | /* Quit if auto download is disabled. |
| 473 | */ |
| 474 | if (!config->GetIntValue(ConfigureYouTube::ConfigID, "AutoDownload", False)) { previousClipboardText = NIL; return; } |
| 475 | |
| 476 | /* Get text from clipboard. |
| 477 | */ |
| 478 | String previousText = previousClipboardText; |
| 479 | String clipboardText = GetClipboardText(); |
| 480 | |
| 481 | if (previousText == clipboardText) return; |
| 482 | |
| 483 | /* Check if it's a valid URL. |
| 484 | */ |
| 485 | if (GetVideoSiteForURL(clipboardText) == NIL) return; |
| 486 | |
| 487 | /* Start download. |
| 488 | */ |
| 489 | StartDownload(clipboardText); |
| 490 | } |
| 491 | |
| 492 | /* Called when the download URL is modified. |
| 493 | * ---- |
| 494 | * Checks for a valid URL and toggles download button state. |
| 495 | */ |
| 496 | Void BoCA::LayerYouTube::OnEditDownloadURL() |
| 497 | { |
| 498 | if ((edit_url->GetText().StartsWith("http://") || |
| 499 | edit_url->GetText().StartsWith("https://")) && |
| 500 | edit_url->GetText().Length() >= 11) button_add_url->Activate(); |
| 501 | else button_add_url->Deactivate(); |
| 502 | } |
| 503 | |
| 504 | /* Called when the download button is pressed. |
| 505 | * ---- |
| 506 | * Starts the download of the video. |
| 507 | */ |
| 508 | Void BoCA::LayerYouTube::OnDownloadTrack() |
| 509 | { |
| 510 | I18n *i18n = I18n::Get(); |
| 511 | |
| 512 | i18n->SetContext("Extensions::Video Downloader::Errors"); |
| 513 | |
| 514 | /* Let's see if we are already downloading this video. |
| 515 | */ |
| 516 | for (Int i = 0; i < list_downloads->Length(); i++) |
| 517 | { |
| 518 | VideoListEntry *entry = (VideoListEntry *) list_downloads->GetNthEntry(i); |
| 519 | Video *video = entry->GetVideo(); |
| 520 | |
| 521 | if (video->GetVideoURL() == edit_url->GetText() && !video->IsDownloadFinished() && !video->IsDownloadCancelled()) { Utilities::ErrorMessage(i18n->TranslateString("You are already downloading this video!")); return; } |
| 522 | } |
| 523 | |
| 524 | /* Find a video site that can handle the URL. |
| 525 | */ |
| 526 | VideoSite *videoSite = GetVideoSiteForURL(edit_url->GetText()); |
| 527 | |
| 528 | if (videoSite == NIL) { Utilities::ErrorMessage(i18n->TranslateString("This URL is not supported or not a video page URL!")); return; } |
| 529 | |
| 530 | /* Start download. |
| 531 | */ |
| 532 | StartDownload(edit_url->GetText()); |
| 533 | |
| 534 | /* Clean up. |
| 535 | */ |
| 536 | edit_url->SetText(NIL); |
| 537 | button_add_url->Deactivate(); |
| 538 | } |
| 539 | |
| 540 | /* Called when the download is finished. |
| 541 | * ---- |
| 542 | * Checks if download succeeded and finishes download. |
| 543 | */ |
| 544 | Void BoCA::LayerYouTube::OnFinishDownload(Video *video) |
| 545 | { |
| 546 | if (video->IsDownloadCancelled()) return; |
| 547 | |
| 548 | I18n *i18n = I18n::Get(); |
| 549 | |
| 550 | i18n->SetContext("Extensions::Video Downloader::Errors"); |
| 551 | |
| 552 | if (!File(video->GetVideoFile()).Exists()) { Utilities::ErrorMessage(i18n->TranslateString("Error downloading video!")); return; } |
| 553 | |
| 554 | /* Finish download. |
| 555 | */ |
| 556 | FinishDownload(video); |
| 557 | } |
| 558 | |
| 559 | /* Called when a list entry is selected. |
| 560 | * ---- |
| 561 | * Finds the corresponding track and emits onSelectTrack. |
| 562 | */ |
| 563 | Void BoCA::LayerYouTube::OnSelectTrack() |
| 564 | { |
| 565 | const Track &track = tracks.GetNth(list_tracks->GetSelectedEntryNumber()); |
| 566 | const Info &info = track.GetInfo(); |
| 567 | |
| 568 | edit_title->Activate(); |
| 569 | edit_title->SetText(info.title); |
| 570 | |
| 571 | edit_description->Activate(); |
| 572 | edit_description->SetText(info.comment); |
| 573 | |
| 574 | link_source->SetText(NIL); |
| 575 | link_source->SetURL(NIL); |
| 576 | |
| 577 | text_site_value->SetText(NIL); |
| 578 | text_uploader_value->SetText(NIL); |
| 579 | text_date_value->SetText(NIL); |
| 580 | |
| 581 | foreach (const String &pair, info.other) |
| 582 | { |
| 583 | String key = pair.Head(pair.Find(":")); |
| 584 | String value = pair.Tail(pair.Length() - pair.Find(":") - 1); |
| 585 | |
| 586 | if (key == INFO_WEB_SOURCE) { link_source->SetURL(value); OnChangeSize(GetSize()); } |
| 587 | |
| 588 | else if (key == "Video site") text_site_value->SetText(value); |
| 589 | else if (key == "Video uploader") text_uploader_value->SetText(value); |
| 590 | else if (key == "Video upload date") text_date_value->SetText(value); |
| 591 | } |
| 592 | |
| 593 | if (track.pictures.Length() > 0) image_cover->SetBitmap(track.pictures.GetFirst().GetBitmap()); |
| 594 | |
| 595 | area_cover->SetColor(Color(255, 255, 255)); |
| 596 | image_cover->Paint(SP_PAINT); |
| 597 | |
| 598 | Font font; |
| 599 | |
| 600 | font.SetColor(Setup::TextColor); |
| 601 | |
| 602 | text_site->SetFont(font); |
| 603 | text_source->SetFont(font); |
| 604 | text_title->SetFont(font); |
| 605 | text_description->SetFont(font); |
| 606 | |
| 607 | text_uploader->SetFont(font); |
| 608 | text_date->SetFont(font); |
| 609 | |
| 610 | JobList::Get()->onComponentSelectTrack.Emit(track); |
| 611 | } |
| 612 | |
| 613 | /* Called when no list entry is selected. |
| 614 | * ---- |
| 615 | * Clears up all info fields. |
| 616 | */ |
| 617 | Void BoCA::LayerYouTube::OnSelectNone() |
| 618 | { |
| 619 | edit_title->SetText(NIL); |
| 620 | edit_title->Deactivate(); |
| 621 | |
| 622 | edit_description->SetText(NIL); |
| 623 | edit_description->Deactivate(); |
| 624 | |
| 625 | link_source->SetText(NIL); |
| 626 | link_source->SetURL(NIL); |
| 627 | |
| 628 | text_site_value->SetText(NIL); |
| 629 | text_uploader_value->SetText(NIL); |
| 630 | text_date_value->SetText(NIL); |
| 631 | |
| 632 | image_cover->SetBitmap(NIL); |
| 633 | area_cover->SetColor(Setup::BackgroundColor); |
| 634 | |
| 635 | Font font; |
| 636 | |
| 637 | font.SetColor(Setup::InactiveTextColor); |
| 638 | |
| 639 | text_site->SetFont(font); |
| 640 | text_source->SetFont(font); |
| 641 | text_title->SetFont(font); |
| 642 | text_description->SetFont(font); |
| 643 | |
| 644 | text_uploader->SetFont(font); |
| 645 | text_date->SetFont(font); |
| 646 | } |
| 647 | |
| 648 | /* Called when info for the selected track is edited. |
| 649 | * ---- |
| 650 | * Updates the selected track. |
| 651 | */ |
| 652 | Void BoCA::LayerYouTube::OnEditMetadata() |
| 653 | { |
| 654 | Track &track = tracks.GetNthReference(list_tracks->GetSelectedEntryNumber()); |
| 655 | Info info = track.GetInfo(); |
| 656 | |
| 657 | info.title = edit_title->GetText(); |
| 658 | info.comment = edit_description->GetText(); |
| 659 | |
| 660 | track.SetInfo(info); |
| 661 | |
| 662 | JobList::Get()->onComponentModifyTrack.Emit(track); |
| 663 | } |
| 664 | |
| 665 | /* Called when a track is modified by the application. |
| 666 | * ---- |
| 667 | * Modifies our corresponding entry accordingly. |
| 668 | */ |
| 669 | Void BoCA::LayerYouTube::OnApplicationModifyTrack(const Track &track) |
| 670 | { |
| 671 | I18n *i18n = I18n::Get(); |
| 672 | |
| 673 | i18n->SetContext("Extensions::Video Downloader"); |
| 674 | |
| 675 | const Info &info = track.GetInfo(); |
| 676 | String jlEntry; |
| 677 | |
| 678 | if (info.artist == NIL && info.title == NIL) jlEntry = String(ListEntry::tabDelimiter).Append(track.fileName).Append(ListEntry::tabDelimiter); |
| 679 | else jlEntry = String(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown")).Append(ListEntry::tabDelimiter).Append(info.title.Length() > 0 ? info.title : i18n->TranslateString("unknown title")).Append(ListEntry::tabDelimiter); |
| 680 | |
| 681 | jlEntry.Append(track.GetLengthString()).Append(ListEntry::tabDelimiter).Append(track.GetFileSizeString()); |
| 682 | |
| 683 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 684 | { |
| 685 | if (tracks.Get(list_tracks->GetNthEntry(i)->GetHandle()).GetTrackID() == track.GetTrackID()) |
| 686 | { |
| 687 | list_tracks->GetNthEntry(i)->SetText(jlEntry); |
| 688 | |
| 689 | tracks.GetReference(list_tracks->GetNthEntry(i)->GetHandle()) = track; |
| 690 | |
| 691 | /* Call OnSelectTrack to update info fields. |
| 692 | */ |
| 693 | if (list_tracks->GetSelectedEntryNumber() == i) OnSelectTrack(); |
| 694 | |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* Called when a track is removed from the application joblist. |
| 701 | * ---- |
| 702 | * Removes our corresponding entry from tracks and list_tracks. |
| 703 | */ |
| 704 | Void BoCA::LayerYouTube::OnApplicationRemoveTrack(const Track &track) |
| 705 | { |
| 706 | Config *config = Config::Get(); |
| 707 | |
| 708 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 709 | { |
| 710 | if (tracks.Get(list_tracks->GetNthEntry(i)->GetHandle()).GetTrackID() == track.GetTrackID()) |
| 711 | { |
| 712 | if (!config->GetIntValue(ConfigureYouTube::ConfigID, "SaveVideoFiles", False) || track.fileName.StartsWith(S::System::System::GetTempDirectory())) File(track.fileName).Delete(); |
| 713 | |
| 714 | tracks.Remove(list_tracks->GetNthEntry(i)->GetHandle()); |
| 715 | |
| 716 | list_tracks->Remove(list_tracks->GetNthEntry(i)); |
| 717 | |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | if (list_tracks->GetSelectedEntry() == NIL || list_tracks->Length() == 0) OnSelectNone(); |
| 723 | } |
| 724 | |
| 725 | /* Called when a track is selected in the application joblist. |
| 726 | * ---- |
| 727 | * Finds and selects the corresponding entry in our track list. |
| 728 | */ |
| 729 | Void BoCA::LayerYouTube::OnApplicationSelectTrack(const Track &track) |
| 730 | { |
| 731 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 732 | { |
| 733 | if (tracks.Get(list_tracks->GetNthEntry(i)->GetHandle()).GetTrackID() == track.GetTrackID()) |
| 734 | { |
| 735 | if (list_tracks->GetSelectedEntryNumber() != i) list_tracks->SelectNthEntry(i); |
| 736 | |
| 737 | break; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /* Called when all tracks are removed from the application joblist at once. |
| 743 | * ---- |
| 744 | * Clears tracks and list_tracks. |
| 745 | */ |
| 746 | Void BoCA::LayerYouTube::OnApplicationRemoveAllTracks() |
| 747 | { |
| 748 | Config *config = Config::Get(); |
| 749 | |
| 750 | foreach (const Track &track, tracks) |
| 751 | { |
| 752 | if (!config->GetIntValue(ConfigureYouTube::ConfigID, "SaveVideoFiles", False) || track.fileName.StartsWith(S::System::System::GetTempDirectory())) File(track.fileName).Delete(); |
| 753 | } |
| 754 | |
| 755 | tracks.RemoveAll(); |
| 756 | |
| 757 | list_tracks->RemoveAllEntries(); |
| 758 | |
| 759 | OnSelectNone(); |
| 760 | } |
| 761 | |
| 762 | /* Called when application is about to quit. |
| 763 | * ---- |
| 764 | * Stop downloads and clean up videolist entries. |
| 765 | */ |
| 766 | Void BoCA::LayerYouTube::OnQuit() |
| 767 | { |
| 768 | /* Delete video list entries. |
| 769 | */ |
| 770 | for (Int i = 0; i < list_downloads->Length(); i++) |
| 771 | { |
| 772 | VideoListEntry *entry = (VideoListEntry *) list_downloads->GetNthEntry(i); |
| 773 | Video *video = entry->GetVideo(); |
| 774 | |
| 775 | list_downloads->Remove(entry); |
| 776 | |
| 777 | DeleteObject(entry); |
| 778 | |
| 779 | delete video; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /* Start video download. |
| 784 | * ---- |
| 785 | */ |
| 786 | Bool BoCA::LayerYouTube::StartDownload(const String &URL) |
| 787 | { |
| 788 | /* Find a video site that can handle the URL. |
| 789 | */ |
| 790 | VideoSite *videoSite = GetVideoSiteForURL(URL); |
| 791 | |
| 792 | if (videoSite == NIL) return False; |
| 793 | |
| 794 | /* Create video object. |
| 795 | */ |
| 796 | Video *video = new Video(videoSite); |
| 797 | |
| 798 | video->SetVideoURL(URL); |
| 799 | video->finishDownload.Connect(&LayerYouTube::OnFinishDownload, this); |
| 800 | |
| 801 | list_downloads->Add(new VideoListEntry(video)); |
| 802 | list_downloads->ScrollToEndOfList(); |
| 803 | |
| 804 | /* Get file extension. |
| 805 | */ |
| 806 | String fileExt = "flv"; |
| 807 | AS::Registry &boca = AS::Registry::Get(); |
| 808 | |
| 809 | for (Int j = 0; j < boca.GetNumberOfComponents(); j++) |
| 810 | { |
| 811 | if (boca.GetComponentID(j) != video->GetDecoderID()) continue; |
| 812 | |
| 813 | const Array<AS::FileFormat *> &formats = boca.GetComponentFormats(j); |
| 814 | |
| 815 | if (formats.Length() >= 1) |
| 816 | { |
| 817 | const Array<String> &extensions = formats.GetFirst()->GetExtensions(); |
| 818 | |
| 819 | if (extensions.Length() >= 1) fileExt = extensions.GetFirst(); |
| 820 | } |
| 821 | |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | /* Start download. |
| 826 | */ |
| 827 | Config *config = Config::Get(); |
| 828 | String videoFile = S::System::System::GetTempDirectory().Append("video_temp_").Append(String::FromInt(S::System::System::Clock())).Append(".").Append(fileExt); |
| 829 | |
| 830 | if (config->GetIntValue(ConfigureYouTube::ConfigID, "SaveVideoFiles", False)) |
| 831 | { |
| 832 | video->QueryMetadata(); |
| 833 | |
| 834 | videoFile = config->GetStringValue(ConfigureYouTube::ConfigID, "VideoOutputDir", S::System::System::GetPersonalFilesDirectory(S::System::PersonalFilesMovies)); |
| 835 | |
| 836 | if (!videoFile.EndsWith(Directory::GetDirectoryDelimiter())) videoFile.Append(Directory::GetDirectoryDelimiter()); |
| 837 | |
| 838 | videoFile.Append(Utilities::ReplaceIncompatibleCharacters(video->GetVideoTitle())); |
| 839 | |
| 840 | Int count = 1; |
| 841 | |
| 842 | while (File(String(videoFile).Append(".").Append(fileExt)).Exists()) |
| 843 | { |
| 844 | count++; |
| 845 | |
| 846 | if (!File(String(videoFile).Append(" (").Append(String::FromInt(count)).Append(").").Append(fileExt)).Exists()) |
| 847 | { |
| 848 | videoFile.Append(" (").Append(String::FromInt(count)).Append(")"); |
| 849 | |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | videoFile.Append(".").Append(fileExt); |
| 855 | } |
| 856 | |
| 857 | video->SetVideoFile(Utilities::NormalizeFileName(videoFile)); |
| 858 | video->Download(); |
| 859 | |
| 860 | return True; |
| 861 | } |
| 862 | |
| 863 | /* Finish video download. |
| 864 | * ---- |
| 865 | */ |
| 866 | Bool BoCA::LayerYouTube::FinishDownload(Video *video) |
| 867 | { |
| 868 | /* Open track and get basic info. |
| 869 | */ |
| 870 | AS::Registry &boca = AS::Registry::Get(); |
| 871 | AS::DecoderComponent *decoder = (AS::DecoderComponent *) boca.CreateComponentByID(video->GetDecoderID()); |
| 872 | |
| 873 | if (decoder == NIL) return False; |
| 874 | |
| 875 | Track track; |
| 876 | |
| 877 | track.fileName = video->GetVideoFile(); |
| 878 | |
| 879 | decoder->GetStreamInfo(track.fileName, track); |
| 880 | |
| 881 | boca.DeleteComponent(decoder); |
| 882 | |
| 883 | /* Fill in metadata. |
| 884 | */ |
| 885 | Info info; |
| 886 | |
| 887 | info.artist = video->GetVideoUploader(); |
| 888 | info.title = video->GetVideoTitle(); |
| 889 | info.comment = video->GetVideoDescription(); |
| 890 | |
| 891 | info.SetOtherInfo(INFO_WEB_SOURCE, video->GetVideoURL()); |
| 892 | |
| 893 | info.SetOtherInfo("Video site", video->GetVideoSiteName()); |
| 894 | info.SetOtherInfo("Video uploader", video->GetVideoUploader()); |
| 895 | info.SetOtherInfo("Video upload date", video->GetVideoDate()); |
| 896 | |
| 897 | if (video->GetVideoThumbnail().mime == "image/jpeg") track.pictures.Add(video->GetVideoThumbnail()); |
| 898 | |
| 899 | track.SetInfo(info); |
| 900 | |
| 901 | I18n *i18n = I18n::Get(); |
| 902 | |
| 903 | i18n->SetContext("Extensions::Video Downloader"); |
| 904 | |
| 905 | /* Add track to joblist. |
| 906 | */ |
| 907 | JobList::Get()->onComponentAddTrack.Emit(track); |
| 908 | |
| 909 | String jlEntry = String(info.artist.Length() > 0 ? info.artist : i18n->TranslateString("unknown")).Append(ListEntry::tabDelimiter).Append(info.title.Length() > 0 ? info.title : i18n->TranslateString("unknown title")).Append(ListEntry::tabDelimiter).Append(track.GetLengthString()).Append(ListEntry::tabDelimiter).Append(track.GetFileSizeString()); |
| 910 | |
| 911 | tracks.Add(track, list_tracks->AddEntry(jlEntry)->GetHandle()); |
| 912 | |
| 913 | return True; |
| 914 | } |