2
2
* Copyright 2018. All rights reserved.
3
3
* Distributed under the terms of the MIT license.
4
4
*
5
- * Authors :
5
+ * Author :
6
6
7
- * Werner Freytag, [email protected]
8
7
*
9
8
* Modified code from CopyNameToClipboard
9
+ * _CheckNetworkConnection() borrowed from SoftwareUpdater
10
10
*/
11
11
12
12
#include < stdio.h>
15
15
#include < Catalog.h>
16
16
#include < Clipboard.h>
17
17
#include < Entry.h>
18
+ #include < NetworkInterface.h>
19
+ #include < NetworkRoster.h>
18
20
#include < Path.h>
19
21
#include < String.h>
20
22
21
23
#undef B_TRANSLATION_CONTEXT
22
24
#define B_TRANSLATION_CONTEXT " Add-On"
23
25
24
26
27
+ bool
28
+ CheckNetworkConnection ()
29
+ {
30
+ BNetworkRoster& roster = BNetworkRoster::Default ();
31
+ BNetworkInterface interface;
32
+ uint32 cookie = 0 ;
33
+ while (roster.GetNextInterface (&cookie, interface) == B_OK) {
34
+ uint32 flags = interface.Flags ();
35
+ if ((flags & IFF_LOOPBACK) == 0
36
+ && (flags & (IFF_UP | IFF_LINK)) == (IFF_UP | IFF_LINK)) {
37
+ return true ;
38
+ }
39
+ }
40
+ // No network connection detected, cannot continue
41
+ return false ;
42
+ }
43
+
44
+
45
+ void
46
+ CopyToClipboard (BString text)
47
+ {
48
+ ssize_t textLen = text.Length ();
49
+ BMessage* message = (BMessage *)NULL ;
50
+
51
+ if (be_clipboard->Lock ()) {
52
+ be_clipboard->Clear ();
53
+ if ((message = be_clipboard->Data ())) {
54
+ message->AddData (" text/plain" , B_MIME_TYPE, text.String (),
55
+ textLen);
56
+ be_clipboard->Commit ();
57
+ }
58
+ be_clipboard->Unlock ();
59
+ }
60
+ }
61
+
62
+
25
63
extern " C" void
26
64
process_refs (entry_ref directoryRef, BMessage* msg, void *)
27
65
{
@@ -33,30 +71,16 @@ process_refs(entry_ref directoryRef, BMessage* msg, void*)
33
71
if (entry.IsDirectory ()) {
34
72
BString text (B_TRANSLATE (
35
73
" UploadIt only works on a single file, no folders" ));
36
- ssize_t textLen = text.Length ();
37
- BMessage* message = (BMessage *)NULL ;
38
-
39
- if (be_clipboard->Lock ()) {
40
- be_clipboard->Clear ();
41
- if ((message = be_clipboard->Data ())) {
42
- message->AddData (" text/plain" , B_MIME_TYPE, text.String (),
43
- textLen);
44
- be_clipboard->Commit ();
45
- }
46
- be_clipboard->Unlock ();
47
- }
74
+ CopyToClipboard (text);
75
+ } else if (CheckNetworkConnection () == false ) {
76
+ BString text (B_TRANSLATE (
77
+ " Online upload service not available" ));
78
+ CopyToClipboard (text);
48
79
} else {
49
80
entry.GetPath (&path);
50
81
BString command (
51
- " stat=$(curl -m 2 -s -I http://google.com | grep HTTP/1 | awk {'print $2'}) ; "
52
- " if [ -z \" $stat\" ] ; then " // network up in general?
53
- " clipboard -c \" %ERROR%\" ; "
54
- " else "
55
- " curl -F'file=@'\" %FILENAME%\" https://0x0.st | clipboard -i ; "
56
- " fi ; "
82
+ " curl -F'file=@'\" %FILENAME%\" https://0x0.st | clipboard -i ; "
57
83
" exit" );
58
- command.ReplaceAll (" %ERROR%" ,
59
- B_TRANSLATE (" Online upload service not available" ));
60
84
command.ReplaceAll (" %FILENAME%" , path.Path ());
61
85
system (command.String ());
62
86
}
0 commit comments