-
Notifications
You must be signed in to change notification settings - Fork 4
/
MainWindow.cpp
872 lines (741 loc) · 20 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/*
* Copyright 2011-2015. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, [email protected]
*
* based on ideas of YAVTD for Haiku
* Version 1.0 by Leszek Lesner (C) 2011
*/
#include "MainWindow.h"
#include <Alert.h>
#include <Application.h>
#include <Catalog.h>
#include <Clipboard.h>
#include <ControlLook.h>
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <InterfaceDefs.h>
#include <LayoutBuilder.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <Roster.h>
#include <Screen.h>
#include <Size.h>
#include <String.h>
#include <SupportDefs.h>
#include <View.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "MainWindow"
int32
_call_script(void* arg)
{
BString* command = static_cast<BString *>(arg);
system(command->String());
return 0;
}
class DirectoryFilter : public BRefFilter {
public:
DirectoryFilter() {};
virtual bool Filter(const entry_ref* ref,
BNode* node, struct stat_beos* st, const char* filetype)
{
// ToDo: Fix this properly in Tracker
// If you create a folder, then rename it, node is NULL.
// The BeBook says: "Note that the function is never sent an
// abstract entry, so the node, st, and filetype arguments will
// always be valid."
return node == NULL ? false : node->IsDirectory();
}
};
// #pragma mark -
MainWindow::MainWindow()
:
BWindow(BRect(), B_TRANSLATE_SYSTEM_NAME("UberTuber"), B_TITLED_WINDOW,
B_NOT_V_RESIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS |
B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS),
fSaveFilePanel(NULL),
fAbortedFlag(false),
fGetFlag(false),
fGotClipFlag(false),
fPlayedFlag(false),
fPlayingFlag(false),
fSavedFlag(false),
fSaveIt(false),
fWorkerThread(new WorkerThread(this))
{
_BuildMenu();
_BuildLayout();
fURLBox->MakeFocus(true);
fPlayButton->MakeDefault(true);
if (fSettings.WindowPosition() == BRect(-1, -1, -1, -1)) {
CenterOnScreen();
ResizeTo(400, 50);
} else {
BRect frame = fSettings.WindowPosition();
MoveTo(frame.LeftTop());
ResizeTo(frame.right - frame.left, frame.bottom - frame.top);
// make sure window is on screen
BScreen screen(this);
if (!screen.Frame().InsetByCopy(10, 10).Intersects(Frame()))
CenterOnScreen();
}
WatchMonitoredSitesList();
be_clipboard->StartWatching(this);
PostMessage(B_CLIPBOARD_CHANGED);
fURLBox->SetText("");
}
void
MainWindow::_BuildMenu()
{
BMenu* menu;
BMenuItem* item;
fMenuBar = new BMenuBar(BRect(), "menubar");
menu = new BMenu(B_TRANSLATE("File"));
menu->AddItem(fPlayMenu = new BMenuItem(B_TRANSLATE("Play"), new BMessage(msgPLAY), 'P'));
fPlayMenu->SetEnabled(false);
menu->AddItem(fSaveMenu = new BMenuItem(B_TRANSLATE("Save as" B_UTF8_ELLIPSIS),
new BMessage(msgSAVE), 'S'));
fSaveMenu->SetEnabled(false);
menu->AddItem(fAbortMenu = new BMenuItem(B_TRANSLATE("Abort download"),
new BMessage(msgABORT)));
fAbortMenu->SetEnabled(false);
menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem(B_TRANSLATE("About UberTuber"),
new BMessage(B_ABOUT_REQUESTED)));
item->SetTarget(be_app);
menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem(B_TRANSLATE("Quit"), new BMessage(B_QUIT_REQUESTED),
'Q'));
fMenuBar->AddItem(menu);
menu = new BMenu(B_TRANSLATE("URL"));
menu->AddItem(item = fClearURLMenu = new BMenuItem(B_TRANSLATE("Clear URL field"),
new BMessage(msgCLEARURL), 'D'));
fClearURLMenu->SetEnabled(false);
menu->AddItem(item = fOpenURLMenu = new BMenuItem(B_TRANSLATE("Open URL in browser"),
new BMessage(msgOPENURL), 'O'));
fOpenURLMenu->SetEnabled(false);
fMenuBar->AddItem(menu);
menu = new BMenu(B_TRANSLATE("Settings"));
menu->AddItem(item = fAutoMenu = new BMenuItem(B_TRANSLATE("Auto-play"),
new BMessage(msgAUTO)));
item->SetMarked(fSettings.StateAuto());
menu->AddItem(item = fCleanMenu = new BMenuItem(
B_TRANSLATE("Remove temporary files on quit"), new BMessage(msgCLEAN)));
item->SetMarked(fSettings.StateClear());
menu->AddSeparatorItem();
menu->AddItem(item = new BMenuItem(
B_TRANSLATE("Edit monitored URLs" B_UTF8_ELLIPSIS), new BMessage(msgEDIT)));
fMenuBar->AddItem(menu);
}
void
MainWindow::_BuildLayout()
{
fURLBox = new BTextControl("urlbox", B_TRANSLATE("URL:"), " ", NULL);
fURLBox->SetModificationMessage(new BMessage(msgURL));
fTitleView = new BStringView("title", " ");
fTitleView->SetFontSize(be_plain_font->Size() - 2);
fTitleView->SetHighColor(tint_color(ui_color(B_CONTROL_TEXT_COLOR), 0.7));
fStatusView = new BStringView("status", " ");
fStatusView->SetFontSize(be_plain_font->Size() - 2);
fStatusView->SetHighColor(tint_color(ui_color(B_CONTROL_TEXT_COLOR), 0.7));
fAbortButton = new BButton("abortbutton", B_TRANSLATE("Abort download"),
new BMessage(msgABORT));
fAbortButton->SetEnabled(false);
fPlayButton = new BButton("playbutton", B_TRANSLATE("Play"), new BMessage(msgPLAY));
fPlayButton->SetEnabled(false);
fSaveButton = new BButton("savebutton", B_TRANSLATE("Save as" B_UTF8_ELLIPSIS),
new BMessage(msgSAVE));
fSaveButton->SetEnabled(false);
BFont font;
float spacing = be_control_look->DefaultItemSpacing();
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.Add(fMenuBar)
.AddGroup(B_HORIZONTAL, spacing)
.Add(fURLBox)
.SetInsets(spacing, spacing, spacing, 0)
.End()
.AddGroup(B_HORIZONTAL, 0)
.AddStrut(font.StringWidth("URL:") + be_control_look->DefaultLabelSpacing())
.Add(fTitleView)
.AddStrut(spacing)
.AddGlue()
.Add(fStatusView)
.SetInsets(spacing, 0, spacing, 0)
.End()
.AddGroup(B_HORIZONTAL, 0)
.AddStrut(font.StringWidth("URL:") + be_control_look->DefaultLabelSpacing())
.Add(fAbortButton)
.AddStrut(spacing * 4)
.AddGlue()
.Add(fSaveButton)
.AddStrut(spacing)
.Add(fPlayButton)
.SetInsets(spacing, 0, spacing, spacing / 2)
.End();
}
// #pragma mark -
status_t
MainWindow::WatchMonitoredSitesList()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return false;
path.Append(kMonitorFile);
BEntry* entry = new BEntry(path.Path());
node_ref nref;
entry->GetNodeRef(&nref);
return (watch_node(&nref, B_WATCH_STAT, this));
}
void
MainWindow::MessageReceived(BMessage* msg)
{
if (msg->WasDropped()) {
entry_ref ref;
int32 i = 0;
if (msg->FindRef("refs", i++, &ref) == B_OK) {
URLofFile(ref);
}
return;
}
entry_ref inRef;
BEntry inEntry;
switch (msg->what)
{
case B_NODE_MONITOR:
{
int32 opcode;
if (msg->FindInt32("opcode", &opcode) == B_OK) {
switch (opcode) {
case B_STAT_CHANGED:
{
printf("Monitored sites list has changed.\n");
fSettings.SetChangedMonitoredList();
break;
}
}
}
break;
}
case B_CLIPBOARD_CHANGED:
{
BString clipboardString(GetClipboard());
BString urlString(fURLBox->Text());
if (!urlString.ICompare(clipboardString)) {
printf("Same contents: break!\n");
break;
}
if (fSettings.ValidURL(clipboardString) == false) {
printf("Not valid: break!\n");
break;
}
fURLBox->SetText(clipboardString.String());
if (!fGetFlag) {
fPlayButton->SetEnabled(true);
fSaveButton->SetEnabled(true);
fPlayMenu->SetEnabled(true);
fSaveMenu->SetEnabled(true);
ResetFlags();
if (fSettings.StateAuto())
PostMessage(msgPLAY);
}
SetStatus(B_TRANSLATE("Auto-inserted URL"));
break;
}
case msgSAVE:
{
// Execute Save Panel
if (fSaveFilePanel == NULL) {
BButton* selectThisDir;
BMessage folderSelect(FOLDER_SELECT_MESSAGE);
BEntry entry(fSettings.LastDir().Path(), true);
entry_ref destRef;
if (entry.Exists() && entry.IsDirectory())
entry.GetRef(&destRef);
fSaveFilePanel = new BFilePanel(B_OPEN_PANEL, NULL, &destRef,
B_DIRECTORY_NODE, true, &folderSelect, NULL, false, true);
fSaveFilePanel->SetButtonLabel(B_DEFAULT_BUTTON, "Select");
fSaveFilePanel->SetTarget(this);
fSaveFilePanel->Window()->Lock();
fSaveFilePanel->Window()->SetTitle(B_TRANSLATE("Choose destination folder"));
BRect buttonRect
= fSaveFilePanel->Window()->ChildAt(0)->FindView(
"cancel button")->Frame();
buttonRect.right = buttonRect.left - 20;
buttonRect.left = buttonRect.right - 130;
selectThisDir = new BButton(buttonRect, NULL, B_TRANSLATE("Select this folder"),
new BMessage(SELECT_THIS_DIR_MESSAGE),
B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
selectThisDir->SetTarget(this);
fSaveFilePanel->Window()->ChildAt(0)->AddChild(selectThisDir);
fSaveFilePanel->Window()->Unlock();
fSaveFilePanel->SetRefFilter(new DirectoryFilter);
}
fURL = new BString(fURLBox->Text());
fSaveFilePanel->Show();
break;
}
case FOLDER_SELECT_MESSAGE:
{
// "SELECT" Button at Save Panel Pushed
fSaveFilePanel->GetNextSelectedRef(&inRef);
inEntry.SetTo(&inRef, true);
BPath path(&inEntry);
fSaveDir = new BString(path.Path());
printf("Save path: %s\n", path.Path());
if (fGotClipFlag)
SaveClip();
else if (!fGetFlag && !fGotClipFlag) {
fSaveIt = true;
GetClip();
} else
fSaveIt = true;
fSaveButton->SetEnabled(false);
fSaveMenu->SetEnabled(false);
fSettings.SetLastDir(path);
fSaveFilePanel->Rewind();
break;
}
case SELECT_THIS_DIR_MESSAGE:
{
// "THIS DIR" Button at Save Panel Pushed
fSaveFilePanel->GetPanelDirectory(&inRef);
fSaveFilePanel->Hide();
inEntry.SetTo(&inRef, true);
BPath path(&inEntry);
fSaveDir = new BString(path.Path());
printf("Save path: %s\n", path.Path());
if (fGotClipFlag)
SaveClip();
else if (!fGetFlag && !fGotClipFlag) {
fSaveIt = true;
GetClip();
} else
fSaveIt = true;
fSaveButton->SetEnabled(false);
fSaveMenu->SetEnabled(false);
fSettings.SetLastDir(path);
break;
}
case msgAUTO:
{
fSettings.SetStateAuto(!fSettings.StateAuto());
fAutoMenu->SetMarked(fSettings.StateAuto());
break;
}
case msgCLEAN:
{
fSettings.SetStateClear(!fSettings.StateClear());
fCleanMenu->SetMarked(fSettings.StateClear());
break;
}
case msgEDIT:
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
path.Append(kMonitorFile);
BString* command = new BString(
"if [ ! -e %FILE% ] ; then "
"touch %FILE% ; "
"printf \""
"# UberTuber's list of monitored websites\n"
"#\n"
"# UberTuber monitors the clipboard and auto-inserts the URL of a known\n"
"# website into its URL text field. The 'known' websites are defined in this\n"
"# text file. See http://rg3.github.io/youtube-dl/supportedsites.html for a\n"
"# list of reportedly working sites.\n"
"#\n"
"# Add, remove or edit URLs to your needs. One URL per line.\n"
"\n"
"blip.tv\n"
"escapistmagazine.com/videos/view\n"
"dailymotion.com\n"
"depositfiles.com\n"
"facebook.com/video\n"
"metacafe.com/watch\n"
"photobucket.com\n"
"video.google.com\n"
"video.yahoo.com\n"
"vimeo.com\n"
"youtu.be\n"
"youtube.com/embed\n"
"youtube.com/watch\n"
"youtube-nocookie.com\n\" > %FILE% ; "
"settype -t text/plain %FILE% ; "
"fi ; "
"open %FILE% ; "
"exit");
command->ReplaceAll("%FILE%", path.Path());
system(command->String());
}
break;
}
case msgABORT:
{
KillThread("ps -o Id Team | grep python | grep youtube-dl | awk '{ print $1; }' ; exit");
KillThread("ps -o Id Team | grep hey | grep UberTuber | awk '{ print $1; }' ; exit");
SetStatus(B_TRANSLATE("Aborted"));
printf("Download aborted\n");
fAbortMenu->SetEnabled(false);
fAbortButton->SetEnabled(false);
fPlayButton->SetEnabled(true);
fSaveButton->SetEnabled(true);
fPlayMenu->SetEnabled(true);
fSaveMenu->SetEnabled(true);
fURLBox->SetEnabled(true);
ResetFlags();
fAbortedFlag = true;
break;
}
case msgPLAY:
{
fURL = new BString(fURLBox->Text());
if (!fGetFlag && !fGotClipFlag)
GetClip();
PlayClip();
fPlayButton->SetEnabled(false);
fPlayMenu->SetEnabled(false);
break;
}
case B_REFS_RECEIVED:
{
entry_ref ref;
int32 i = 0;
if (msg->FindRef("refs", i++, &ref) == B_OK) {
// printf("File dropped: %s\n -------- \n", ref.name);
URLofFile(ref);
}
break;
}
case msgURL:
{
if (!fGetFlag) {
int state = (fURLBox->TextView()->TextLength() > 0) ? true : false;
fPlayButton->SetEnabled(state);
fSaveButton->SetEnabled(state);
fPlayMenu->SetEnabled(state);
fSaveMenu->SetEnabled(state);
fClearURLMenu->SetEnabled(state);
fOpenURLMenu->SetEnabled(state);
SetStatus("");
fTitleView->SetText("");
printf("URL changed:\n");
ResetFlags();
}
break;
}
case msgTITLE:
{
if (msg->FindString("title", &fClipTitle) == B_OK)
TruncateTitle();
if (fClipTitle.FindFirst("ERROR: YouTube said:") == 0) {
PostMessage(msgABORT);
}
break;
}
case msgCLEARURL:
{
fURLBox->SetText("");
break;
}
case msgOPENURL:
{
BString command("open %URL%");
command.ReplaceAll("%URL%", fURLBox->Text());
system(command);
break;
}
// React to messages from 'hey', forwarded from App
case statBUFFER:
{
SetStatus(B_TRANSLATE("Buffering" B_UTF8_ELLIPSIS));
break;
}
case statDOWNLOAD:
{
SetStatus(B_TRANSLATE("Downloading" B_UTF8_ELLIPSIS));
break;
}
case statERROR:
{
if (!fAbortedFlag)
SetStatus(B_TRANSLATE("Download failed"));
else {
SetStatus(B_TRANSLATE("Aborted"));
fAbortMenu->SetEnabled(false);
fAbortButton->SetEnabled(false);
}
fURLBox->SetEnabled(true);
ResetFlags();
break;
}
case statFINISH_GET:
{
if (!fPlayingFlag && !fAbortedFlag)
SetStatus(B_TRANSLATE("Download finished"));
fAbortButton->SetEnabled(false);
fAbortMenu->SetEnabled(false);
fGetFlag = false;
fGotClipFlag = true;
if (fSaveIt)
SaveClip();
fURLBox->SetEnabled(true);
break;
}
case statFINISH_PLAY:
{
if (!fGetFlag)
SetStatus(B_TRANSLATE("Playback finished"));
if (fGetFlag && fSaveIt)
SetStatus(B_TRANSLATE("Downloading" B_UTF8_ELLIPSIS));
if (fGetFlag && !fSaveIt) {
SetStatus(B_TRANSLATE("Aborted"));
fAbortedFlag = true;
KillThread("ps -o Id Team | grep python | grep youtube-dl | awk '{ print $1; }' ; exit");
KillThread("ps -o Id Team | grep hey | grep UberTuber | awk '{ print $1; }' ; exit");
}
if (fAbortedFlag) {
SetStatus(B_TRANSLATE("Aborted"));
fGotClipFlag = false;
}
if (fGotClipFlag)
fPlayedFlag = true;
fPlayingFlag = false;
fPlayButton->SetEnabled(true);
fPlayMenu->SetEnabled(true);
break;
}
case statFINISH_SAVE:
{
SetStatus(B_TRANSLATE("Saving complete"));
fSaveButton->SetEnabled(true);
fSaveMenu->SetEnabled(true);
fSavedFlag = true;
break;
}
case statPLAYING:
{
fPlayingFlag = true;
fAbortButton->SetEnabled(true);
fAbortMenu->SetEnabled(true);
SetStatus(B_TRANSLATE("Playing" B_UTF8_ELLIPSIS));
break;
}
default:
{
BWindow::MessageReceived(msg);
break;
}
}
}
bool
MainWindow::QuitRequested()
{
if (fGetFlag) {
BAlert* alert = new BAlert(B_TRANSLATE("Abort download?"), B_TRANSLATE("A clip is still being "
"downloaded.\nDo you want to abort or continue the download?"),
B_TRANSLATE("Abort download"), B_TRANSLATE("Continue download"), NULL, B_WIDTH_FROM_LABEL,
B_OFFSET_SPACING, B_WARNING_ALERT);
alert->SetShortcut(1, B_ESCAPE);
switch (alert->Go())
{
case 0: // abort
{
KillThread("ps -o Id Team | grep python | grep youtube-dl | awk '{ print $1; }' ; exit");
KillThread("ps -o Id Team | grep hey | grep UberTuber | awk '{ print $1; }' ; exit");
break;
}
case 1: // continue
{
return false;
}
default:
return false;
}
}
if (fSettings.StateClear()) {
system("rm /tmp/ubertuber/*");
printf("Deleting temporary files\n");
}
fSettings.SetWindowPosition(ConvertToScreen(Bounds()));
fWorkerThread->PostMessage(B_QUIT_REQUESTED);
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}
void
MainWindow::FrameResized(float width, float height)
{
TruncateTitle();
}
void
MainWindow::TruncateTitle()
{
float widthURL(fURLBox->Bounds().Width());
BString* title = new BString(fClipTitle);
fTitleView->TruncateString(title, B_TRUNCATE_END, widthURL - 120.0);
fTitleView->SetText(title->String());
return;
}
void
MainWindow::URLofFile(entry_ref &ref)
{
const ssize_t bufferSize = 256;
char buffer[bufferSize];
BNode node(&ref);
ssize_t readBytes;
readBytes = node.ReadAttr("META:url", B_STRING_TYPE, 0, buffer, bufferSize);
if (readBytes < 1)
SetStatus(B_TRANSLATE("No URL found"));
else {
fURLBox->SetText(buffer);
if (!fGetFlag) {
fPlayButton->SetEnabled(true);
fSaveButton->SetEnabled(true);
fPlayMenu->SetEnabled(true);
fSaveMenu->SetEnabled(true);
ResetFlags();
if (fSettings.StateAuto())
PostMessage(msgPLAY);
}
}
return;
}
// #pragma mark -
void
MainWindow::KillThread(char* command)
{
char output[6];
FILE* pget_threadID(NULL);
pget_threadID = popen(command,"r");
fread(output, 1, sizeof(output), pget_threadID);
fclose(pget_threadID);
/* strip trailing newline */
for (int i = 0; (unsigned) i < strlen(output); i++) {
if (output[i] == '\n' || output[i] == '\r')
output[i] = '\0';
}
printf("Kill, Kill, Kill!\nThreadID: %s\n", output);
int ID(atoi(output));
kill_thread((thread_id) ID);
return;
}
void
MainWindow::ResetFlags()
{
fAbortedFlag = false;
fGetFlag = false;
fGotClipFlag = false;
fPlayedFlag = false;
fPlayingFlag = false;
fSavedFlag = false;
fSaveIt = false;
return;
}
void
MainWindow::SetStatus(BString text)
{
fStatusView->SetText(text.String());
return;
}
void
MainWindow::SetURL(BString* url)
{
fURLBox->SetText(url->String());
return;
}
// #pragma mark -
BString
MainWindow::GetClipboard()
{
const char* text;
ssize_t textLen;
BMessage* clipboard;
if (be_clipboard->Lock()) {
if ((clipboard = be_clipboard->Data()))
clipboard->FindData("text/plain", B_MIME_TYPE,
(const void **)&text, &textLen);
be_clipboard->Unlock();
}
BString clipboardString(text, textLen);
return clipboardString;
}
void
MainWindow::GetTitle()
{
BMessage msg(msgGETTITLE);
msg.AddString("url", fURL->String());
fWorkerThread->Looper()->PostMessage(&msg, fWorkerThread);
return;
}
bool
MainWindow::GetClip()
{
if (fSavedFlag || fPlayedFlag)
return true;
GetTitle();
BMessage msg(msgGETCLIP);
msg.AddString("url", fURL->String());
fWorkerThread->Looper()->PostMessage(&msg, fWorkerThread);
fGetFlag = true;
fAbortButton->SetEnabled(fSaveIt);
fAbortMenu->SetEnabled(fSaveIt);
fURLBox->SetEnabled(false);
return true;
}
void
MainWindow::PlayClip()
{
BString* command = new BString(
"hey application/x-vnd.UberTuber buff ; "
"mkdir -p %DIR% ; "
"cd %DIR% ; "
"FILE=$(youtube-dl --restrict-filenames --get-filename %URL% 2>&1 | tail -n 1) ; "
"until [ -e \"$FILE\" ] ; do " // wait until file exists
"sleep 1 ; "
"done ; "
"sleep 2 ; " // buffering a bit
"settype -t video/mpeg4 \"$FILE\" ; " // Force MPEG4 for MediaPlayer"
"hey application/x-vnd.UberTuber play ; "
"open \"$FILE\" ; "
"sleep 1 ; "
"TITLETHREAD=$(echo $FILE | cut -c 1-29) ; "
"waitfor -e \"w>$TITLETHREAD\" ; "
"mimeset -F \"$FILE\" ; " // Reset mimetype
"hey application/x-vnd.UberTuber pfin ; "
"exit");
command->ReplaceAll("%URL%", fURL->String());
if (fSavedFlag)
command->ReplaceAll("%DIR%", fSaveDir->String());
else
command->ReplaceAll("%DIR%", "/tmp/ubertuber");
thread_id playthread = spawn_thread(_call_script, "Clip player",
B_LOW_PRIORITY, command);
if (playthread < B_OK)
return;
resume_thread(playthread);
return;
}
void
MainWindow::SaveClip()
{
BString* command = new BString(
"cd /tmp/ubertuber ; "
"FILE=$(youtube-dl --restrict-filenames --get-filename %URL% 2>&1 | tail -n 1) ; "
"addattr -t string META:url %URL% \"$FILE\" ; "
"mv \"$FILE\" \"%DEST%\" ; "
"hey application/x-vnd.UberTuber sfin ; "
"exit");
command->ReplaceAll("%DEST%", fSaveDir->String());
command->ReplaceAll("%URL%", fURL->String());
thread_id savethread = spawn_thread(_call_script, "Clip saver",
B_LOW_PRIORITY, command);
if (savethread < B_OK)
return;
resume_thread(savethread);
return;
}